PGROUTING  2.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
max_flow_many_to_many.c
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: max_flow_many_to_many.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 #include "utils/array.h"
32 
33 
34 #include "./../../common/src/debug_macro.h"
35 #include "./../../common/src/e_report.h"
36 #include "./../../common/src/time_msg.h"
37 #include "./../../common/src/pgr_types.h"
38 #include "./../../common/src/edges_input.h"
39 #include "./../../common/src/arrays_input.h"
40 #include "./max_flow_driver.h"
41 
42 PGDLLEXPORT Datum
43 max_flow_many_to_many(PG_FUNCTION_ARGS);
44 
45 static
46 void
48  char *edges_sql,
49  ArrayType *starts,
50  ArrayType *ends,
51  char *algorithm,
52  bool only_flow,
53  pgr_flow_t **result_tuples,
54  size_t *result_count) {
55  if (!(strcmp(algorithm, "push_relabel") == 0
56  || strcmp(algorithm, "edmonds_karp") == 0
57  || strcmp(algorithm, "boykov_kolmogorov") == 0)) {
58  elog(ERROR, "Unknown algorithm");
59  }
60 
62 
63  size_t size_source_verticesArr = 0;
64  int64_t* source_vertices =
65  pgr_get_bigIntArray(&size_source_verticesArr, starts);
66 
67  size_t size_sink_verticesArr = 0;
68  int64_t* sink_vertices =
69  pgr_get_bigIntArray(&size_sink_verticesArr, ends);
70 
71  pgr_edge_t *edges = NULL;
72 
73  size_t total_edges = 0;
74 
75  /* NOTE:
76  * For flow, cost and reverse_cost are really capacity and reverse_capacity
77  */
78  pgr_get_flow_edges(edges_sql, &edges, &total_edges);
79 
80  if (total_edges == 0) {
81  if (source_vertices) pfree(source_vertices);
82  if (sink_vertices) pfree(sink_vertices);
84  return;
85  }
86 
87 
88  PGR_DBG("Starting timer");
89  clock_t start_t = clock();
90  char* log_msg = NULL;
91  char* notice_msg = NULL;
92  char *err_msg = NULL;
93 
95  edges, total_edges,
96  source_vertices, size_source_verticesArr,
97  sink_vertices, size_sink_verticesArr,
98  algorithm,
99  only_flow,
100 
101  result_tuples, result_count,
102 
103  &log_msg,
104  &notice_msg,
105  &err_msg);
106 
107  if (only_flow) {
108  time_msg("pgr_maxFlow(many to many)",
109  start_t, clock());
110  } else if (strcmp(algorithm, "push_relabel") == 0) {
111  time_msg("pgr_maxFlowPushRelabel(many to many)",
112  start_t, clock());
113  } else if (strcmp(algorithm, "edmonds_karp") == 0) {
114  time_msg("pgr_maxFlowEdmondsKarp(many to many)",
115  start_t, clock());
116  } else {
117  time_msg("pgr_maxFlowBoykovKolmogorov(many to many)",
118  start_t, clock());
119  }
120 
121 
122  if (edges) pfree(edges);
123  if (source_vertices) pfree(source_vertices);
124  if (sink_vertices) pfree(sink_vertices);
125 
126  if (err_msg && (*result_tuples)) {
127  pfree(*result_tuples);
128  (*result_tuples) = NULL;
129  (*result_count) = 0;
130  }
131 
132  pgr_global_report(log_msg, notice_msg, err_msg);
133 
134  if (log_msg) pfree(log_msg);
135  if (notice_msg) pfree(notice_msg);
136  if (err_msg) pfree(err_msg);
137 
138  pgr_SPI_finish();
139 }
140 
141 
143 PGDLLEXPORT Datum
144 max_flow_many_to_many(PG_FUNCTION_ARGS) {
145  FuncCallContext *funcctx;
146  TupleDesc tuple_desc;
147 
148  /**************************************************************************/
149  pgr_flow_t *result_tuples = 0;
150  size_t result_count = 0;
151  /**************************************************************************/
152 
153  if (SRF_IS_FIRSTCALL()) {
154  MemoryContext oldcontext;
155  funcctx = SRF_FIRSTCALL_INIT();
156  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
157 
158 
159  /**********************************************************************/
160 
161  process(
162  text_to_cstring(PG_GETARG_TEXT_P(0)),
163  PG_GETARG_ARRAYTYPE_P(1),
164  PG_GETARG_ARRAYTYPE_P(2),
165  text_to_cstring(PG_GETARG_TEXT_P(3)),
166  PG_GETARG_BOOL(4),
167  &result_tuples,
168  &result_count);
169 
170  /* */
171  /**********************************************************************/
172 
173 #if PGSQL_VERSION > 95
174  funcctx->max_calls = result_count;
175 #else
176  funcctx->max_calls = (uint32_t)result_count;
177 #endif
178  funcctx->user_fctx = result_tuples;
179  if (get_call_result_type(fcinfo, NULL, &tuple_desc)
180  != TYPEFUNC_COMPOSITE) {
181  ereport(ERROR,
182  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
183  errmsg("function returning record called in context "
184  "that cannot accept type record")));
185  }
186 
187  funcctx->tuple_desc = tuple_desc;
188  MemoryContextSwitchTo(oldcontext);
189  }
190 
191  funcctx = SRF_PERCALL_SETUP();
192  tuple_desc = funcctx->tuple_desc;
193  result_tuples = (pgr_flow_t *) funcctx->user_fctx;
194 
195  if (funcctx->call_cntr < funcctx->max_calls) {
196  HeapTuple tuple;
197  Datum result;
198  Datum *values;
199  bool *nulls;
200  size_t call_cntr = funcctx->call_cntr;
201 
202  /**********************************************************************/
203  /* MODIFY AS NEEDED */
204  values = palloc(6 * sizeof(Datum));
205  nulls = palloc(6 * sizeof(bool));
206 
207  size_t i;
208  for (i = 0; i < 6; ++i) {
209  nulls[i] = false;
210  }
211 
212  values[0] = Int32GetDatum(call_cntr + 1);
213  values[1] = Int64GetDatum(result_tuples[call_cntr].edge);
214  values[2] = Int64GetDatum(result_tuples[call_cntr].source);
215  values[3] = Int64GetDatum(result_tuples[call_cntr].target);
216  values[4] = Int64GetDatum(result_tuples[call_cntr].flow);
217  values[5] = Int64GetDatum(result_tuples[call_cntr].residual_capacity);
218  /**********************************************************************/
219 
220  tuple = heap_form_tuple(tuple_desc, values, nulls);
221  result = HeapTupleGetDatum(tuple);
222  SRF_RETURN_NEXT(funcctx, result);
223  } else {
224  SRF_RETURN_DONE(funcctx);
225  }
226 }
227 
#define PGR_DBG(...)
Definition: debug_macro.h:34
int64_t * pgr_get_bigIntArray(size_t *arrlen, ArrayType *input)
Definition: arrays_input.c:127
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)
void pgr_global_report(char *log, char *notice, char *err)
notice & error
Definition: e_report.c:93
void pgr_SPI_finish(void)
PGDLLEXPORT Datum max_flow_many_to_many(PG_FUNCTION_ARGS)
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
PG_FUNCTION_INFO_V1(max_flow_many_to_many)
static void process(char *edges_sql, ArrayType *starts, ArrayType *ends, char *algorithm, bool only_flow, pgr_flow_t **result_tuples, size_t *result_count)