PGROUTING  2.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
edge_disjoint_paths_many_to_many.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
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 #include "./../../common/src/debug_macro.h"
34 #include "./../../common/src/e_report.h"
35 #include "./../../common/src/time_msg.h"
36 #include "./../../common/src/pgr_types.h"
37 #include "./../../common/src/edges_input.h"
38 #include "./../../common/src/arrays_input.h"
40 
41 PGDLLEXPORT Datum
42 edge_disjoint_paths_many_to_many(PG_FUNCTION_ARGS);
43 
44 static
45 void
47  char *edges_sql,
48  ArrayType *starts,
49  ArrayType *ends,
50 
51  bool directed,
52  General_path_element_t **result_tuples,
53  size_t *result_count) {
55 
56  size_t size_source_verticesArr = 0;
57  int64_t* source_vertices =
58  pgr_get_bigIntArray(&size_source_verticesArr, starts);
59 
60  size_t size_sink_verticesArr = 0;
61  int64_t* sink_vertices =
62  pgr_get_bigIntArray(&size_sink_verticesArr, ends);
63 
64 
65  pgr_basic_edge_t *edges = NULL;
66  size_t total_edges = 0;
67 
68  pgr_get_basic_edges(edges_sql, &edges, &total_edges);
69 
70  if (total_edges == 0) {
71  if (source_vertices) pfree(source_vertices);
72  if (sink_vertices) pfree(sink_vertices);
74  return;
75  }
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, total_edges,
86  source_vertices, size_source_verticesArr,
87  sink_vertices, size_sink_verticesArr,
88  directed,
89 
90  result_tuples, result_count,
91 
92  &log_msg,
93  &notice_msg,
94  &err_msg);
95 
96  time_msg("pgr_edgeDisjointPaths(many to many)", start_t, clock());
97 
98  if (edges) pfree(edges);
99  if (source_vertices) pfree(source_vertices);
100  if (sink_vertices) pfree(sink_vertices);
101 
102  if (err_msg && (*result_tuples)) {
103  pfree(*result_tuples);
104  (*result_tuples) = NULL;
105  (*result_count) = 0;
106  }
107 
108  pgr_global_report(log_msg, notice_msg, err_msg);
109 
110  if (log_msg) pfree(log_msg);
111  if (notice_msg) pfree(notice_msg);
112  if (err_msg) pfree(err_msg);
113  pgr_SPI_finish();
114 }
115 
117 PGDLLEXPORT Datum
119  FuncCallContext *funcctx;
120  TupleDesc tuple_desc;
121 
122  /**************************************************************************/
123  General_path_element_t *result_tuples = NULL;
124  size_t result_count = 0;
125  /**************************************************************************/
126 
127  if (SRF_IS_FIRSTCALL()) {
128  MemoryContext oldcontext;
129  funcctx = SRF_FIRSTCALL_INIT();
130  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
131 
132 
133  /**********************************************************************/
134 
135  process(
136  text_to_cstring(PG_GETARG_TEXT_P(0)),
137  PG_GETARG_ARRAYTYPE_P(1),
138  PG_GETARG_ARRAYTYPE_P(2),
139  PG_GETARG_BOOL(3),
140  &result_tuples,
141  &result_count);
142 
143  /**********************************************************************/
144 
145 #if PGSQL_VERSION > 95
146  funcctx->max_calls = result_count;
147 #else
148  funcctx->max_calls = (uint32_t)result_count;
149 #endif
150  funcctx->user_fctx = result_tuples;
151  if (get_call_result_type(fcinfo, NULL, &tuple_desc)
152  != TYPEFUNC_COMPOSITE) {
153  ereport(ERROR,
154  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
155  errmsg("function returning record called in context "
156  "that cannot accept type record")));
157  }
158 
159  funcctx->tuple_desc = tuple_desc;
160  MemoryContextSwitchTo(oldcontext);
161  }
162 
163  funcctx = SRF_PERCALL_SETUP();
164  tuple_desc = funcctx->tuple_desc;
165  result_tuples = (General_path_element_t *) funcctx->user_fctx;
166 
167  if (funcctx->call_cntr < funcctx->max_calls) {
168  HeapTuple tuple;
169  Datum result;
170  Datum *values;
171  bool *nulls;
172 
173  /**********************************************************************/
174  /* MODIFY AS NEEDED */
175 
176  values = palloc(6 * sizeof(Datum));
177  nulls = palloc(6 * sizeof(bool));
178 
179  size_t i;
180  for (i = 0; i < 6; ++i) {
181  nulls[i] = false;
182  }
183 
184  values[0] = Int32GetDatum(funcctx->call_cntr + 1);
185  values[1] = Int32GetDatum(result_tuples[funcctx->call_cntr].seq);
186  values[2] = Int64GetDatum(result_tuples[funcctx->call_cntr].start_id);
187  values[3] = Int64GetDatum(result_tuples[funcctx->call_cntr].end_id);
188  values[4] = Int64GetDatum(result_tuples[funcctx->call_cntr].node);
189  values[5] = Int64GetDatum(result_tuples[funcctx->call_cntr].edge);
190  /**********************************************************************/
191 
192  tuple = heap_form_tuple(tuple_desc, values, nulls);
193  result = HeapTupleGetDatum(tuple);
194  SRF_RETURN_NEXT(funcctx, result);
195  } else {
196  SRF_RETURN_DONE(funcctx);
197  }
198 }
199 
PGDLLEXPORT Datum edge_disjoint_paths_many_to_many(PG_FUNCTION_ARGS)
void pgr_get_basic_edges(char *sql, pgr_basic_edge_t **edges, size_t *total_edges)
read basic edges
Definition: edges_input.c:594
#define PGR_DBG(...)
Definition: debug_macro.h:34
static void process(char *edges_sql, ArrayType *starts, ArrayType *ends, bool directed, General_path_element_t **result_tuples, size_t *result_count)
void do_pgr_edge_disjoint_paths(pgr_basic_edge_t *data_edges, size_t total_edges, int64_t *source_vertices, size_t size_source_verticesArr, int64_t *sink_vertices, 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)
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 pgr_global_report(char *log, char *notice, char *err)
notice & error
Definition: e_report.c:93
void pgr_SPI_finish(void)
PG_FUNCTION_INFO_V1(edge_disjoint_paths_many_to_many)
edge_astar_t * edges
Definition: BDATester.cpp:46
void pgr_SPI_connect(void)
char * err_msg
Definition: BDATester.cpp:50