PGROUTING  2.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
max_flow_one_to_one.c
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: max_flow_one_to_one.c
3 
4 Generated with Template by:
5 Copyright (c) 2015 pgRouting developers
6 Mail: project@pgrouting.org
7 
8 Function's developer:
9 Copyright (c) 2016 Andrea Nardelli
10 Mail: nrd.nardelli@gmail.com
11 
12 ------
13 
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 2 of the License, or
17 (at your option) any later version.
18 
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23 
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 
28 ********************************************************************PGR-GNU*/
29 
30 #include "./../../common/src/postgres_connection.h"
31 
32 #include "./../../common/src/debug_macro.h"
33 #include "./../../common/src/e_report.h"
34 #include "./../../common/src/time_msg.h"
35 #include "./../../common/src/pgr_types.h"
36 #include "./../../common/src/edges_input.h"
37 #include "./max_flow_driver.h"
38 
39 PGDLLEXPORT Datum
40 max_flow_one_to_one(PG_FUNCTION_ARGS);
41 
42 /******************************************************************************/
43 /* MODIFY AS NEEDED */
44 static
45 void
47  char *edges_sql,
48  int64_t source_vertex,
49  int64_t sink_vertex,
50  char *algorithm,
51  bool only_flow,
52  pgr_flow_t **result_tuples,
53  size_t *result_count) {
54  if (!(strcmp(algorithm, "push_relabel") == 0
55  || strcmp(algorithm, "edmonds_karp") == 0
56  || strcmp(algorithm, "boykov_kolmogorov") == 0)) {
57  elog(ERROR, "Unknown algorithm");
58  }
59 
60  if (source_vertex == sink_vertex) {
61  return;
62  }
63 
65 
66  /* NOTE:
67  * For flow, cost and reverse_cost are really capacity and reverse_capacity
68  */
69  size_t total_tuples = 0;
70  pgr_edge_t *edges = NULL;
71  pgr_get_flow_edges(edges_sql, &edges, &total_tuples);
72 
73  if (total_tuples == 0) {
75  return;
76  }
77 
78  PGR_DBG("Starting timer");
79  clock_t start_t = clock();
80  char* log_msg = NULL;
81  char* notice_msg = NULL;
82  char* err_msg = NULL;
83 
85  edges,
86  total_tuples,
87  &source_vertex, 1,
88  &sink_vertex, 1,
89  algorithm,
90  only_flow,
91 
92  result_tuples, result_count,
93 
94  &log_msg,
95  &notice_msg,
96  &err_msg);
97 
98  if (only_flow) {
99  time_msg("pgr_maxFlow(one to one)",
100  start_t, clock());
101  } else if (strcmp(algorithm, "push_relabel") == 0) {
102  time_msg("pgr_maxFlowPushRelabel(one to one)",
103  start_t, clock());
104  } else if (strcmp(algorithm, "edmonds_karp") == 0) {
105  time_msg("pgr_maxFlowEdmondsKarp(one to one)",
106  start_t, clock());
107  } else {
108  time_msg("pgr_maxFlowBoykovKolmogorov(one to one)",
109  start_t, clock());
110  }
111 
112 
113  if (edges) pfree(edges);
114 
115  if (err_msg && (*result_tuples)) {
116  pfree(*result_tuples);
117  (*result_tuples) = NULL;
118  (*result_count) = 0;
119  }
120 
121  pgr_global_report(log_msg, notice_msg, err_msg);
122 
123  if (log_msg) pfree(log_msg);
124  if (notice_msg) pfree(notice_msg);
125  if (err_msg) pfree(err_msg);
126 
127  pgr_SPI_finish();
128 }
129 /* */
130 /******************************************************************************/
131 
133 PGDLLEXPORT Datum
134 max_flow_one_to_one(PG_FUNCTION_ARGS) {
135  FuncCallContext *funcctx;
136  TupleDesc tuple_desc;
137 
138  /**************************************************************************/
139  pgr_flow_t *result_tuples = 0;
140  size_t result_count = 0;
141  /**************************************************************************/
142 
143  if (SRF_IS_FIRSTCALL()) {
144  MemoryContext oldcontext;
145  funcctx = SRF_FIRSTCALL_INIT();
146  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
147 
148 
149  /**********************************************************************/
150 
151  process(
152  text_to_cstring(PG_GETARG_TEXT_P(0)),
153  PG_GETARG_INT64(1),
154  PG_GETARG_INT64(2),
155  text_to_cstring(PG_GETARG_TEXT_P(3)),
156  PG_GETARG_BOOL(4),
157  &result_tuples,
158  &result_count);
159 
160  /**********************************************************************/
161 
162 #if PGSQL_VERSION > 95
163  funcctx->max_calls = result_count;
164 #else
165  funcctx->max_calls = (uint32_t)result_count;
166 #endif
167  funcctx->user_fctx = result_tuples;
168  if (get_call_result_type(fcinfo, NULL, &tuple_desc)
169  != TYPEFUNC_COMPOSITE) {
170  ereport(ERROR,
171  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
172  errmsg("function returning record called in context "
173  "that cannot accept type record")));
174  }
175 
176  funcctx->tuple_desc = tuple_desc;
177  MemoryContextSwitchTo(oldcontext);
178  }
179 
180  funcctx = SRF_PERCALL_SETUP();
181  tuple_desc = funcctx->tuple_desc;
182  result_tuples = (pgr_flow_t *) funcctx->user_fctx;
183 
184  if (funcctx->call_cntr < funcctx->max_calls) {
185  HeapTuple tuple;
186  Datum result;
187  Datum *values;
188  bool *nulls;
189  size_t call_cntr = funcctx->call_cntr;
190 
191  /**********************************************************************/
192 
193  values = palloc(6 * sizeof(Datum));
194  nulls = palloc(6 * sizeof(bool));
195 
196  size_t i;
197  for (i = 0; i < 6; ++i) {
198  nulls[i] = false;
199  }
200 
201  values[0] = Int32GetDatum(call_cntr + 1);
202  values[1] = Int64GetDatum(result_tuples[call_cntr].edge);
203  values[2] = Int64GetDatum(result_tuples[call_cntr].source);
204  values[3] = Int64GetDatum(result_tuples[call_cntr].target);
205  values[4] = Int64GetDatum(result_tuples[call_cntr].flow);
206  values[5] = Int64GetDatum(result_tuples[call_cntr].residual_capacity);
207  /**********************************************************************/
208 
209  tuple = heap_form_tuple(tuple_desc, values, nulls);
210  result = HeapTupleGetDatum(tuple);
211  SRF_RETURN_NEXT(funcctx, result);
212  } else {
213  SRF_RETURN_DONE(funcctx);
214  }
215 }
216 
PGDLLEXPORT Datum max_flow_one_to_one(PG_FUNCTION_ARGS)
#define PGR_DBG(...)
Definition: debug_macro.h:34
void time_msg(char *msg, clock_t start_t, clock_t end_t)
Definition: time_msg.c:32
void do_pgr_max_flow(pgr_edge_t *data_edges, size_t total_tuples, int64_t *source_vertices, size_t size_source_verticesArr, int64_t *sink_vertices, size_t size_sink_verticesArr, char *algorithm, bool only_flow, pgr_flow_t **return_tuples, size_t *return_count, char **log_msg, char **notice_msg, char **err_msg)
PG_FUNCTION_INFO_V1(max_flow_one_to_one)
void pgr_global_report(char *log, char *notice, char *err)
notice & error
Definition: e_report.c:93
void pgr_SPI_finish(void)
void pgr_get_flow_edges(char *sql, pgr_edge_t **edges, size_t *total_edges)
read edges for flow
Definition: edges_input.c:529
edge_astar_t * edges
Definition: BDATester.cpp:46
void pgr_SPI_connect(void)
char * err_msg
Definition: BDATester.cpp:50
static void process(char *edges_sql, int64_t source_vertex, int64_t sink_vertex, char *algorithm, bool only_flow, pgr_flow_t **result_tuples, size_t *result_count)