pgRouting
pgRouting extends the PostGIS / PostgreSQL geospatial database to provide geospatial routing functionality.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
max_flow_many_to_one.c
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: max_flow_many_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 "postgres.h"
31 #include "executor/spi.h"
32 #include "funcapi.h"
33 #include "utils/array.h"
34 #include "catalog/pg_type.h"
35 #if PGSQL_VERSION > 92
36 #include "access/htup_details.h"
37 #endif
38 
39 /*
40  * Uncomment when needed
41  */
42 
43 // #define DEBUG
44 
45 #include "fmgr.h"
46 #include "./../../common/src/debug_macro.h"
47 #include "./../../common/src/time_msg.h"
48 #include "./../../common/src/pgr_types.h"
49 #include "./../../common/src/postgres_connection.h"
50 #include "./../../common/src/edges_input.h"
51 #include "./../../common/src/arrays_input.h"
53 
54 PGDLLEXPORT Datum
55 max_flow_many_to_one(PG_FUNCTION_ARGS);
56 
57 /******************************************************************************/
58 /* MODIFY AS NEEDED */
59 static
60 void
62  char *edges_sql,
63  int64_t *source_vertices, size_t size_source_verticesArr,
64  int64_t sink_vertex,
65  char *algorithm,
66  pgr_flow_t **result_tuples,
67  size_t *result_count) {
69 
70  if (!(strcmp(algorithm, "push_relabel") == 0
71  || strcmp(algorithm, "edmonds_karp") == 0
72  || strcmp(algorithm, "boykov_kolmogorov") == 0)) {
73  elog(ERROR, "Unknown algorithm");
74  }
75 
76  PGR_DBG("Load data");
77  pgr_edge_t *edges = NULL;
78 
79  size_t total_tuples = 0;
80 
81  /* NOTE:
82  * For flow, cost and reverse_cost are really capacity and reverse_capacity
83  */
84  pgr_get_flow_edges(edges_sql, &edges, &total_tuples);
85 
86  if (total_tuples == 0) {
87  PGR_DBG("No edges found");
88  (*result_count) = 0;
89  (*result_tuples) = NULL;
91  return;
92  }
93  PGR_DBG("Total %ld tuples in query:", total_tuples);
94 
95  PGR_DBG("Starting processing");
96  clock_t start_t = clock();
97  char *err_msg = NULL;
99  edges,
100  total_tuples,
101  source_vertices, size_source_verticesArr,
102  sink_vertex,
103  algorithm,
104  result_tuples,
105  result_count,
106  &err_msg);
107 
108  time_msg("processing max flow", start_t, clock());
109  PGR_DBG("Returning %ld tuples\n", *result_count);
110  PGR_DBG("Returned message = %s\n", err_msg);
111 
112  free(err_msg);
113  pfree(edges);
114  pgr_SPI_finish();
115 }
116 /* */
117 /******************************************************************************/
118 
120 PGDLLEXPORT Datum
121 max_flow_many_to_one(PG_FUNCTION_ARGS) {
122  FuncCallContext *funcctx;
123  uint32_t call_cntr;
124  uint32_t max_calls;
125  TupleDesc tuple_desc;
126 
127  /**************************************************************************/
128  /* MODIFY AS NEEDED */
129  /* */
130  pgr_flow_t *result_tuples = 0;
131  size_t result_count = 0;
132  /* */
133  /**************************************************************************/
134 
135  if (SRF_IS_FIRSTCALL()) {
136  MemoryContext oldcontext;
137  funcctx = SRF_FIRSTCALL_INIT();
138  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
139 
140 
141  /**********************************************************************/
142  /* MODIFY AS NEEDED */
143 
144 
145  int64_t *source_vertices;
146  size_t size_source_verticesArr;
147  source_vertices = (int64_t *)
148  pgr_get_bigIntArray(&size_source_verticesArr,
149  PG_GETARG_ARRAYTYPE_P(1));
150  PGR_DBG("source_verticesArr size %ld ", size_source_verticesArr);
151 
152  PGR_DBG("Calling process");
153  process(
154  pgr_text2char(PG_GETARG_TEXT_P(0)),
155  source_vertices, size_source_verticesArr,
156  PG_GETARG_INT64(2),
157  pgr_text2char(PG_GETARG_TEXT_P(3)),
158  &result_tuples,
159  &result_count);
160 
161  /* */
162  /**********************************************************************/
163 
164  funcctx->max_calls = (uint32_t) result_count;
165  funcctx->user_fctx = result_tuples;
166  if (get_call_result_type(fcinfo, NULL, &tuple_desc)
167  != TYPEFUNC_COMPOSITE) {
168  ereport(ERROR,
169  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
170  errmsg("function returning record called in context "
171  "that cannot accept type record")));
172  }
173 
174  funcctx->tuple_desc = tuple_desc;
175  MemoryContextSwitchTo(oldcontext);
176  }
177 
178  funcctx = SRF_PERCALL_SETUP();
179  call_cntr = funcctx->call_cntr;
180  max_calls = funcctx->max_calls;
181  tuple_desc = funcctx->tuple_desc;
182  result_tuples = (pgr_flow_t *) funcctx->user_fctx;
183 
184  if (call_cntr < max_calls) {
185  HeapTuple tuple;
186  Datum result;
187  Datum *values;
188  bool *nulls;
189 
190  /**********************************************************************/
191  /* MODIFY AS NEEDED */
192  values = palloc(6 * sizeof(Datum));
193  nulls = palloc(6 * sizeof(bool));
194 
195  size_t i;
196  for (i = 0; i < 6; ++i) {
197  nulls[i] = false;
198  }
199 
200  // postgres starts counting from 1
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  // cleanup
214  if (result_tuples) free(result_tuples);
215 
216  SRF_RETURN_DONE(funcctx);
217  }
218 }
219 
static void process(char *edges_sql, int64_t *source_vertices, size_t size_source_verticesArr, int64_t sink_vertex, char *algorithm, pgr_flow_t **result_tuples, size_t *result_count)
PGDLLEXPORT Datum max_flow_many_to_one(PG_FUNCTION_ARGS)
#define PGR_DBG(...)
Definition: debug_macro.h:33
int64_t * pgr_get_bigIntArray(size_t *arrlen, ArrayType *input)
Definition: arrays_input.c:38
void time_msg(char *msg, clock_t start_t, clock_t end_t)
Definition: time_msg.c:31
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:526
edge_astar_t * edges
Definition: BDATester.cpp:46
void pgr_SPI_connect(void)
PG_FUNCTION_INFO_V1(max_flow_many_to_one)
void do_pgr_max_flow_many_to_one(pgr_edge_t *data_edges, size_t total_tuples, int64_t *source_vertices, size_t size_source_verticesArr, int64_t sink_vertex, char *algorithm, pgr_flow_t **return_tuples, size_t *return_count, char **err_msg)
char * err_msg
Definition: BDATester.cpp:50
char * pgr_text2char(text *in)