PGROUTING  2.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
edge_disjoint_paths_many_to_one.c
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: edge_disjoint_paths_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 
31 #include "./../../common/src/postgres_connection.h"
32 #include "utils/array.h"
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 
42 
43 PGDLLEXPORT Datum
44 edge_disjoint_paths_many_to_one(PG_FUNCTION_ARGS);
45 
46 /******************************************************************************/
47 /* MODIFY AS NEEDED */
48 static
49 void
51  char *edges_sql,
52  ArrayType *starts,
53  int64_t sink_vertex,
54  bool directed,
55  General_path_element_t **result_tuples,
56  size_t *result_count) {
58 
59  size_t size_source_verticesArr = 0;
60  int64_t* source_vertices =
61  pgr_get_bigIntArray(&size_source_verticesArr, starts);
62 
63  pgr_basic_edge_t *edges = NULL;
64  size_t total_edges = 0;
65  pgr_get_basic_edges(edges_sql, &edges, &total_edges);
66 
67  if (total_edges == 0) {
68  if (source_vertices) pfree(source_vertices);
70  return;
71  }
72 
73  PGR_DBG("Starting timer");
74  clock_t start_t = clock();
75  char* log_msg = NULL;
76  char* notice_msg = NULL;
77  char* err_msg = NULL;
78 
80  edges, total_edges,
81  source_vertices, size_source_verticesArr,
82  &sink_vertex, 1,
83  directed,
84 
85  result_tuples, result_count,
86 
87  &log_msg,
88  &notice_msg,
89  &err_msg);
90 
91  time_msg("pgr_edgeDisjointPaths(many_to_one)", start_t, clock());
92  if (edges) pfree(edges);
93  if (source_vertices) pfree(source_vertices);
94 
95  if (err_msg && (*result_tuples)) {
96  pfree(*result_tuples);
97  (*result_tuples) = NULL;
98  (*result_count) = 0;
99  }
100 
101  pgr_global_report(log_msg, notice_msg, err_msg);
102 
103  if (log_msg) pfree(log_msg);
104  if (notice_msg) pfree(notice_msg);
105  if (err_msg) pfree(err_msg);
106  pgr_SPI_finish();
107 }
108 
110 PGDLLEXPORT Datum
112  FuncCallContext *funcctx;
113  TupleDesc tuple_desc;
114 
115  /**************************************************************************/
116  General_path_element_t *result_tuples = NULL;
117  size_t result_count = 0;
118  /**************************************************************************/
119 
120  if (SRF_IS_FIRSTCALL()) {
121  MemoryContext oldcontext;
122  funcctx = SRF_FIRSTCALL_INIT();
123  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
124 
125 
126  /**********************************************************************/
127 
128  process(
129  text_to_cstring(PG_GETARG_TEXT_P(0)),
130  PG_GETARG_ARRAYTYPE_P(1),
131  PG_GETARG_INT64(2),
132  PG_GETARG_BOOL(3),
133  &result_tuples,
134  &result_count);
135 
136  /**********************************************************************/
137 
138 #if PGSQL_VERSION > 95
139  funcctx->max_calls = result_count;
140 #else
141  funcctx->max_calls = (uint32_t)result_count;
142 #endif
143  funcctx->user_fctx = result_tuples;
144  if (get_call_result_type(fcinfo, NULL, &tuple_desc)
145  != TYPEFUNC_COMPOSITE) {
146  ereport(ERROR,
147  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
148  errmsg("function returning record called in context "
149  "that cannot accept type record")));
150  }
151 
152  funcctx->tuple_desc = tuple_desc;
153  MemoryContextSwitchTo(oldcontext);
154  }
155 
156  funcctx = SRF_PERCALL_SETUP();
157  tuple_desc = funcctx->tuple_desc;
158  result_tuples = (General_path_element_t *) funcctx->user_fctx;
159 
160  if (funcctx->call_cntr < funcctx->max_calls) {
161  HeapTuple tuple;
162  Datum result;
163  Datum *values;
164  bool *nulls;
165 
166  /**********************************************************************/
167 
168  size_t numb = 5;
169  values = palloc(numb * sizeof(Datum));
170  nulls = palloc(numb * sizeof(bool));
171 
172  size_t i;
173  for (i = 0; i < numb; ++i) {
174  nulls[i] = false;
175  }
176 
177  values[0] = Int32GetDatum(funcctx->call_cntr + 1);
178  values[1] = Int32GetDatum(result_tuples[funcctx->call_cntr].seq);
179  values[2] = Int64GetDatum(result_tuples[funcctx->call_cntr].start_id);
180  values[3] = Int64GetDatum(result_tuples[funcctx->call_cntr].node);
181  values[4] = Int64GetDatum(result_tuples[funcctx->call_cntr].edge);
182 
183  /**********************************************************************/
184 
185  tuple = heap_form_tuple(tuple_desc, values, nulls);
186  result = HeapTupleGetDatum(tuple);
187  SRF_RETURN_NEXT(funcctx, result);
188  } else {
189  SRF_RETURN_DONE(funcctx);
190  }
191 }
192 
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
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_one)
edge_astar_t * edges
Definition: BDATester.cpp:46
void pgr_SPI_connect(void)
PGDLLEXPORT Datum edge_disjoint_paths_many_to_one(PG_FUNCTION_ARGS)
char * err_msg
Definition: BDATester.cpp:50
static void process(char *edges_sql, ArrayType *starts, int64_t sink_vertex, bool directed, General_path_element_t **result_tuples, size_t *result_count)