PGROUTING  3.2
edge_disjoint_paths.c
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: edge_disjoint_paths_many_to_many.c
3 
4 Generated with Template by:
5 Copyright (c) 2015 pgRouting developers
7 
8 Function's developer:
9 Copyright (c) 2016 Andrea Nardelli
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 <stdbool.h>
31 
33 #include "utils/array.h"
34 
35 #include "c_common/debug_macro.h"
36 #include "c_common/e_report.h"
37 #include "c_common/time_msg.h"
38 #include "c_common/edges_input.h"
39 #include "c_common/arrays_input.h"
42 
43 PGDLLEXPORT Datum
44 _pgr_edgedisjointpaths(PG_FUNCTION_ARGS);
45 
46 static
47 void
49  char *edges_sql,
50  char *combinations_sql,
51  ArrayType *starts,
52  ArrayType *ends,
53 
54  bool directed,
55  General_path_element_t **result_tuples,
56  size_t *result_count) {
58 
59  int64_t *source_vertices = NULL;
60  size_t size_source_verticesArr = 0;
61 
62  int64_t *sink_vertices = NULL;
63  size_t size_sink_verticesArr = 0;
64 
65 
66  pgr_edge_t *edges = NULL;
67  size_t total_edges = 0;
68 
69  pgr_combination_t *combinations = NULL;
70  size_t total_combinations = 0;
71 
72  if (starts && ends) {
73  source_vertices = (int64_t*)
74  pgr_get_bigIntArray(&size_source_verticesArr, starts);
75  sink_vertices = (int64_t*)
76  pgr_get_bigIntArray(&size_sink_verticesArr, ends);
77  } else if (combinations_sql) {
78  pgr_get_combinations(combinations_sql, &combinations, &total_combinations);
79  if (total_combinations == 0) {
80  if (combinations)
81  pfree(combinations);
83  return;
84  }
85  }
86 
87  pgr_get_edges(edges_sql, &edges, &total_edges);
88 
89  if (total_edges == 0) {
90  if (source_vertices) pfree(source_vertices);
91  if (sink_vertices) pfree(sink_vertices);
93  return;
94  }
95 
96 
97  PGR_DBG("Starting timer");
98  clock_t start_t = clock();
99  char* log_msg = NULL;
100  char* notice_msg = NULL;
101  char* err_msg = NULL;
102 
104  edges, total_edges,
105  combinations, total_combinations,
106  source_vertices, size_source_verticesArr,
107  sink_vertices, size_sink_verticesArr,
108  directed,
109 
110  result_tuples, result_count,
111 
112  &log_msg,
113  &notice_msg,
114  &err_msg);
115 
116  time_msg("pgr_edgeDisjointPaths(many to many)", start_t, clock());
117 
118  if (edges) pfree(edges);
119  if (source_vertices) pfree(source_vertices);
120  if (sink_vertices) pfree(sink_vertices);
121 
122  if (err_msg && (*result_tuples)) {
123  pfree(*result_tuples);
124  (*result_tuples) = NULL;
125  (*result_count) = 0;
126  }
127 
128  pgr_global_report(log_msg, notice_msg, err_msg);
129 
130  if (log_msg) pfree(log_msg);
131  if (notice_msg) pfree(notice_msg);
132  if (err_msg) pfree(err_msg);
133  pgr_SPI_finish();
134 }
135 
137 PGDLLEXPORT Datum
138 _pgr_edgedisjointpaths(PG_FUNCTION_ARGS) {
139  FuncCallContext *funcctx;
140  TupleDesc tuple_desc;
141 
142  General_path_element_t *result_tuples = NULL;
143  size_t result_count = 0;
144 
145  if (SRF_IS_FIRSTCALL()) {
146  MemoryContext oldcontext;
147  funcctx = SRF_FIRSTCALL_INIT();
148  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
149 
150 
151  if (PG_NARGS() == 4) {
152  /*
153  * many to many
154  */
155  process(
156  text_to_cstring(PG_GETARG_TEXT_P(0)),
157  NULL,
158  PG_GETARG_ARRAYTYPE_P(1),
159  PG_GETARG_ARRAYTYPE_P(2),
160  PG_GETARG_BOOL(3),
161  &result_tuples,
162  &result_count);
163 
164  } else if (PG_NARGS() == 3) {
165  /*
166  * combinations
167  */
168  process(
169  text_to_cstring(PG_GETARG_TEXT_P(0)),
170  text_to_cstring(PG_GETARG_TEXT_P(1)),
171  NULL,
172  NULL,
173  PG_GETARG_BOOL(2),
174  &result_tuples,
175  &result_count);
176  }
177 
178 
179 #if PGSQL_VERSION > 95
180  funcctx->max_calls = result_count;
181 #else
182  funcctx->max_calls = (uint32_t)result_count;
183 #endif
184  funcctx->user_fctx = result_tuples;
185  if (get_call_result_type(fcinfo, NULL, &tuple_desc)
186  != TYPEFUNC_COMPOSITE) {
187  ereport(ERROR,
188  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
189  errmsg("function returning record called in context "
190  "that cannot accept type record")));
191  }
192 
193  funcctx->tuple_desc = tuple_desc;
194  MemoryContextSwitchTo(oldcontext);
195  }
196 
197  funcctx = SRF_PERCALL_SETUP();
198  tuple_desc = funcctx->tuple_desc;
199  result_tuples = (General_path_element_t *) funcctx->user_fctx;
200 
201  if (funcctx->call_cntr < funcctx->max_calls) {
202  HeapTuple tuple;
203  Datum result;
204  Datum *values;
205  bool *nulls;
206 
207 
208  values = palloc(9 * sizeof(Datum));
209  nulls = palloc(9 * sizeof(bool));
210 
211  size_t i;
212  for (i = 0; i < 9; ++i) {
213  nulls[i] = false;
214  }
215 
216  values[0] = Int32GetDatum(funcctx->call_cntr + 1);
217  values[1] = Int32GetDatum(
218  result_tuples[funcctx->call_cntr].start_id + 1);
219  values[2] = Int32GetDatum(result_tuples[funcctx->call_cntr].seq);
220  values[3] = Int64GetDatum(result_tuples[funcctx->call_cntr].start_id);
221  values[4] = Int64GetDatum(result_tuples[funcctx->call_cntr].end_id);
222  values[5] = Int64GetDatum(result_tuples[funcctx->call_cntr].node);
223  values[6] = Int64GetDatum(result_tuples[funcctx->call_cntr].edge);
224  values[7] = Float8GetDatum(result_tuples[funcctx->call_cntr].cost);
225  values[8] = Float8GetDatum(result_tuples[funcctx->call_cntr].agg_cost);
226 
227  tuple = heap_form_tuple(tuple_desc, values, nulls);
228  result = HeapTupleGetDatum(tuple);
229  SRF_RETURN_NEXT(funcctx, result);
230  } else {
231  SRF_RETURN_DONE(funcctx);
232  }
233 }
234 
combinations_input.h
General_path_element_t::edge
int64_t edge
Definition: general_path_element_t.h:42
time_msg.h
postgres_connection.h
pgr_edge_t
Definition: pgr_edge_t.h:37
edge_disjoint_paths_driver.h
pgr_SPI_connect
void pgr_SPI_connect(void)
Definition: postgres_connection.c:82
General_path_element_t::seq
int seq
Definition: general_path_element_t.h:38
pgr_SPI_finish
void pgr_SPI_finish(void)
Definition: postgres_connection.c:71
e_report.h
arrays_input.h
PG_FUNCTION_INFO_V1
PG_FUNCTION_INFO_V1(_pgr_edgedisjointpaths)
General_path_element_t::start_id
int64_t start_id
Definition: general_path_element_t.h:39
debug_macro.h
process
static void process(char *edges_sql, char *combinations_sql, ArrayType *starts, ArrayType *ends, bool directed, General_path_element_t **result_tuples, size_t *result_count)
Definition: edge_disjoint_paths.c:48
pgr_get_bigIntArray
int64_t * pgr_get_bigIntArray(size_t *arrlen, ArrayType *input)
Enforces the input array to be NOT empty.
Definition: arrays_input.c:146
PGR_DBG
#define PGR_DBG(...)
Definition: debug_macro.h:34
pgr_combination_t
Definition: pgr_combination_t.h:43
_pgr_edgedisjointpaths
PGDLLEXPORT Datum _pgr_edgedisjointpaths(PG_FUNCTION_ARGS)
Definition: edge_disjoint_paths.c:138
General_path_element_t::node
int64_t node
Definition: general_path_element_t.h:41
pgr_get_combinations
void pgr_get_combinations(char *combinations_sql, pgr_combination_t **combinations, size_t *total_combinations)
combinations_sql
Definition: combinations_input.c:147
pgr_get_edges
void pgr_get_edges(char *edges_sql, pgr_edge_t **edges, size_t *total_edges)
basic edge_sql
Definition: edges_input.c:711
General_path_element_t
Definition: general_path_element_t.h:37
if
if(DOXYGEN_FOUND) configure_file($
Definition: doxygen/CMakeLists.txt:13
do_pgr_edge_disjoint_paths
void do_pgr_edge_disjoint_paths(pgr_edge_t *data_edges, size_t total_edges, pgr_combination_t *combinations, size_t total_combinations, int64_t *sources, size_t size_source_verticesArr, int64_t *sinks, size_t size_sink_verticesArr, bool directed, General_path_element_t **return_tuples, size_t *return_count, char **log_msg, char **notice_msg, char **err_msg)
Definition: edge_disjoint_paths_driver.cpp:66
time_msg
void time_msg(char *msg, clock_t start_t, clock_t end_t)
Definition: time_msg.c:32
General_path_element_t::end_id
int64_t end_id
Definition: general_path_element_t.h:40
edges_input.h
General_path_element_t::cost
double cost
Definition: general_path_element_t.h:43
pgr_global_report
void pgr_global_report(char *log, char *notice, char *err)
notice & error
Definition: e_report.c:93
General_path_element_t::agg_cost
double agg_cost
Definition: general_path_element_t.h:44