PGROUTING  2.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
many_to_many_astar.c
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: astarOneToOne.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) 2015 Celia Virginia Vergara Castillo
10 Mail:
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"
39 
41 
42 PGDLLEXPORT Datum astarManyToMany(PG_FUNCTION_ARGS);
44 
45 
46 static
47 void
48 process(char* edges_sql,
49  ArrayType *starts,
50  ArrayType *ends,
51  bool directed,
52  int heuristic,
53  double factor,
54  double epsilon,
55  bool only_cost,
56  General_path_element_t **result_tuples,
57  size_t *result_count) {
58  check_parameters(heuristic, factor, epsilon);
59 
61 
62  int64_t* start_vidsArr = NULL;
63  size_t size_start_vidsArr = 0;
64  start_vidsArr = (int64_t*)
65  pgr_get_bigIntArray(&size_start_vidsArr, starts);
66 
67  int64_t* end_vidsArr = NULL;
68  size_t size_end_vidsArr = 0;
69  end_vidsArr = (int64_t*)
70  pgr_get_bigIntArray(&size_end_vidsArr, ends);
71 
72 
73  Pgr_edge_xy_t *edges = NULL;
74  size_t total_edges = 0;
75 
76  pgr_get_edges_xy(edges_sql, &edges, &total_edges);
77 
78  if (total_edges == 0) {
79  PGR_DBG("No edges found");
80  (*result_count) = 0;
81  (*result_tuples) = NULL;
83  return;
84  }
85 
86  PGR_DBG("Starting processing");
87  char *log_msg = NULL;
88  char *notice_msg = NULL;
89  char *err_msg = NULL;
90  clock_t start_t = clock();
92  edges, total_edges,
93  start_vidsArr, size_start_vidsArr,
94  end_vidsArr, size_end_vidsArr,
95  directed,
96  heuristic,
97  factor,
98  epsilon,
99  only_cost,
100  true,
101  result_tuples, result_count,
102  &log_msg,
103  &notice_msg,
104  &err_msg);
105 
106  if (only_cost) {
107  time_msg("processing pgr_astarCost(many to many)", start_t, clock());
108  } else {
109  time_msg("processing pgr_astar(many to many)", start_t, clock());
110  }
111 
112 
113  if (err_msg && (*result_tuples)) {
114  pfree(*result_tuples);
115  (*result_tuples) = NULL;
116  (*result_count) = 0;
117  }
118 
119  pgr_global_report(log_msg, notice_msg, err_msg);
120 
121  if (log_msg) pfree(log_msg);
122  if (notice_msg) pfree(notice_msg);
123  if (err_msg) pfree(err_msg);
124  if (edges) pfree(edges);
125  if (start_vidsArr) pfree(start_vidsArr);
126  if (end_vidsArr) pfree(end_vidsArr);
127 
128  pgr_SPI_finish();
129 }
130 
131 PGDLLEXPORT Datum
132 astarManyToMany(PG_FUNCTION_ARGS) {
133  FuncCallContext *funcctx;
134  TupleDesc tuple_desc;
135 
136  /**********************************************************************/
137  General_path_element_t *result_tuples = NULL;
138  size_t result_count = 0;
139  /**********************************************************************/
140 
141  if (SRF_IS_FIRSTCALL()) {
142  MemoryContext oldcontext;
143  funcctx = SRF_FIRSTCALL_INIT();
144  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
145 
146 
147  /**********************************************************************/
148  /*
149  edges_sql TEXT,
150  start_vids ARRAY[ANY_INTEGER], -- anyarray
151  end_vids ARRAY[ANY_INTEGER], -- anyarray
152  directed BOOLEAN DEFAULT true,
153  heuristic INTEGER DEFAULT 0,
154  factor FLOAT DEFAULT 1.0,
155  epsilon FLOAT DEFAULT 1.0,
156 
157  **********************************************************************/
158 
159  process(
160  text_to_cstring(PG_GETARG_TEXT_P(0)),
161  PG_GETARG_ARRAYTYPE_P(1),
162  PG_GETARG_ARRAYTYPE_P(2),
163  PG_GETARG_BOOL(3),
164  PG_GETARG_INT32(4),
165  PG_GETARG_FLOAT8(5),
166  PG_GETARG_FLOAT8(6),
167  PG_GETARG_BOOL(7),
168  &result_tuples,
169  &result_count);
170 
171 
172 #if PGSQL_VERSION > 95
173  funcctx->max_calls = result_count;
174 #else
175  funcctx->max_calls = (uint32_t)result_count;
176 #endif
177  funcctx->max_calls = (uint32_t) result_count;
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  funcctx->tuple_desc = tuple_desc;
187  MemoryContextSwitchTo(oldcontext);
188  }
189 
190  funcctx = SRF_PERCALL_SETUP();
191  tuple_desc = funcctx->tuple_desc;
192  result_tuples = (General_path_element_t*) funcctx->user_fctx;
193 
194  if (funcctx->call_cntr < funcctx->max_calls) {
195  HeapTuple tuple;
196  Datum result;
197  Datum *values;
198  bool* nulls;
199 
200  /*********************************************************************
201  OUT seq INTEGER,
202  OUT path_seq INTEGER,
203  OUT start_vid BIGINT,
204  OUT end_vid BIGINT,
205  OUT node BIGINT,
206  OUT edge BIGINT,
207  OUT cost FLOAT,
208  OUT agg_cost FLOAT
209  **********************************************************************/
210 
211 
212  size_t numb = 8;
213  values = palloc(numb * sizeof(Datum));
214  nulls = palloc(numb * sizeof(bool));
215 
216  size_t i;
217  for (i = 0; i < numb; ++i) {
218  nulls[i] = false;
219  }
220 
221 
222  values[0] = Int32GetDatum(funcctx->call_cntr + 1);
223  values[1] = Int32GetDatum(result_tuples[funcctx->call_cntr].seq);
224  values[2] = Int64GetDatum(result_tuples[funcctx->call_cntr].start_id);
225  values[3] = Int64GetDatum(result_tuples[funcctx->call_cntr].end_id);
226  values[4] = Int64GetDatum(result_tuples[funcctx->call_cntr].node);
227  values[5] = Int64GetDatum(result_tuples[funcctx->call_cntr].edge);
228  values[6] = Float8GetDatum(result_tuples[funcctx->call_cntr].cost);
229  values[7] = Float8GetDatum(result_tuples[funcctx->call_cntr].agg_cost);
230 
231 
232  tuple = heap_form_tuple(tuple_desc, values, nulls);
233  result = HeapTupleGetDatum(tuple);
234  SRF_RETURN_NEXT(funcctx, result);
235  } else {
236  SRF_RETURN_DONE(funcctx);
237  }
238 }
#define PGR_DBG(...)
Definition: debug_macro.h:34
void do_pgr_astarManyToMany(Pgr_edge_xy_t *edges, size_t total_edges, int64_t *start_vidsArr, size_t size_start_vidsArr, int64_t *end_vidsArr, size_t size_end_vidsArr, bool directed, int heuristic, double factor, double epsilon, bool only_cost, bool normal, 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
PGDLLEXPORT Datum astarManyToMany(PG_FUNCTION_ARGS)
void pgr_SPI_finish(void)
void pgr_get_edges_xy(char *edges_sql, Pgr_edge_xy_t **edges, size_t *total_edges)
Edges with x, y vertices values.
Definition: edges_input.c:572
edge_astar_t * edges
Definition: BDATester.cpp:46
void pgr_SPI_connect(void)
PG_FUNCTION_INFO_V1(astarManyToMany)
char * err_msg
Definition: BDATester.cpp:50
void check_parameters(int heuristic, double factor, double epsilon)
static void process(char *edges_sql, ArrayType *starts, ArrayType *ends, bool directed, int heuristic, double factor, double epsilon, bool only_cost, General_path_element_t **result_tuples, size_t *result_count)