PGROUTING  2.6
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 
31 #include "utils/array.h"
32 
33 #include "c_common/debug_macro.h"
34 #include "c_common/e_report.h"
35 #include "c_common/time_msg.h"
36 #include "c_common/edges_input.h"
37 #include "c_common/arrays_input.h"
39 
41 
42 PGDLLEXPORT Datum astarManyToMany(PG_FUNCTION_ARGS);
44 
45 #if 0
46 void
48  int heuristic,
49  double factor,
50  double epsilon) {
51  if (heuristic > 5 || heuristic < 0) {
52  ereport(ERROR,
53  (errmsg("Unknown heuristic"),
54  errhint("Valid values: 0~5")));
55  }
56  if (factor <= 0) {
57  ereport(ERROR,
58  (errmsg("Factor value out of range"),
59  errhint("Valid values: positive non zero")));
60  }
61  if (epsilon < 1) {
62  ereport(ERROR,
63  (errmsg("Epsilon value out of range"),
64  errhint("Valid values: 1 or greater than 1")));
65  }
66 }
67 #endif
68 
69 static
70 void
71 process(char* edges_sql,
72  ArrayType *starts,
73  ArrayType *ends,
74  bool directed,
75  int heuristic,
76  double factor,
77  double epsilon,
78  bool only_cost,
79  bool normal,
80  General_path_element_t **result_tuples,
81  size_t *result_count) {
82  check_parameters(heuristic, factor, epsilon);
83 
85 
86  int64_t* start_vidsArr = NULL;
87  size_t size_start_vidsArr = 0;
88 
89  int64_t* end_vidsArr = NULL;
90  size_t size_end_vidsArr = 0;
91 
92  Pgr_edge_xy_t *edges = NULL;
93  size_t total_edges = 0;
94 
95  if (normal) {
96  pgr_get_edges_xy(edges_sql, &edges, &total_edges);
97  start_vidsArr = (int64_t*)
98  pgr_get_bigIntArray(&size_start_vidsArr, starts);
99  end_vidsArr = (int64_t*)
100  pgr_get_bigIntArray(&size_end_vidsArr, ends);
101  } else {
102  pgr_get_edges_xy_reversed(edges_sql, &edges, &total_edges);
103  end_vidsArr = (int64_t*)
104  pgr_get_bigIntArray(&size_end_vidsArr, starts);
105  start_vidsArr = (int64_t*)
106  pgr_get_bigIntArray(&size_start_vidsArr, ends);
107  }
108 
109  if (total_edges == 0) {
110  PGR_DBG("No edges found");
111  (*result_count) = 0;
112  (*result_tuples) = NULL;
113  pgr_SPI_finish();
114  return;
115  }
116 
117  PGR_DBG("Starting processing");
118  char *log_msg = NULL;
119  char *notice_msg = NULL;
120  char *err_msg = NULL;
121  clock_t start_t = clock();
123  edges, total_edges,
124  start_vidsArr, size_start_vidsArr,
125  end_vidsArr, size_end_vidsArr,
126  directed,
127  heuristic,
128  factor,
129  epsilon,
130  only_cost,
131  normal,
132  result_tuples, result_count,
133  &log_msg,
134  &notice_msg,
135  &err_msg);
136 
137  if (only_cost) {
138  time_msg("processing pgr_astarCost(many to many)", start_t, clock());
139  } else {
140  time_msg("processing pgr_astar(many to many)", start_t, clock());
141  }
142 
143 
144  if (err_msg && (*result_tuples)) {
145  pfree(*result_tuples);
146  (*result_tuples) = NULL;
147  (*result_count) = 0;
148  }
149 
150  pgr_global_report(log_msg, notice_msg, err_msg);
151 
152  if (log_msg) pfree(log_msg);
153  if (notice_msg) pfree(notice_msg);
154  if (err_msg) pfree(err_msg);
155  if (edges) pfree(edges);
156  if (start_vidsArr) pfree(start_vidsArr);
157  if (end_vidsArr) pfree(end_vidsArr);
158 
159  pgr_SPI_finish();
160 }
161 
162 PGDLLEXPORT Datum
163 astarManyToMany(PG_FUNCTION_ARGS) {
164  FuncCallContext *funcctx;
165  TupleDesc tuple_desc;
166 
167  /**********************************************************************/
168  General_path_element_t *result_tuples = NULL;
169  size_t result_count = 0;
170  /**********************************************************************/
171 
172  if (SRF_IS_FIRSTCALL()) {
173  MemoryContext oldcontext;
174  funcctx = SRF_FIRSTCALL_INIT();
175  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
176 
177 
178  /**********************************************************************/
179  /*
180  edges_sql TEXT,
181  start_vids ARRAY[ANY_INTEGER], -- anyarray
182  end_vids ARRAY[ANY_INTEGER], -- anyarray
183  directed BOOLEAN DEFAULT true,
184  heuristic INTEGER DEFAULT 0,
185  factor FLOAT DEFAULT 1.0,
186  epsilon FLOAT DEFAULT 1.0,
187 
188  **********************************************************************/
189 
190  process(
191  text_to_cstring(PG_GETARG_TEXT_P(0)),
192  PG_GETARG_ARRAYTYPE_P(1),
193  PG_GETARG_ARRAYTYPE_P(2),
194  PG_GETARG_BOOL(3),
195  PG_GETARG_INT32(4),
196  PG_GETARG_FLOAT8(5),
197  PG_GETARG_FLOAT8(6),
198  PG_GETARG_BOOL(7),
199  PG_GETARG_BOOL(8),
200  &result_tuples,
201  &result_count);
202 
203 
204 #if PGSQL_VERSION > 95
205  funcctx->max_calls = result_count;
206 #else
207  funcctx->max_calls = (uint32_t)result_count;
208 #endif
209  funcctx->max_calls = (uint32_t) result_count;
210  funcctx->user_fctx = result_tuples;
211  if (get_call_result_type(fcinfo, NULL, &tuple_desc)
212  != TYPEFUNC_COMPOSITE)
213  ereport(ERROR,
214  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
215  errmsg("function returning record called in context "
216  "that cannot accept type record")));
217 
218  funcctx->tuple_desc = tuple_desc;
219  MemoryContextSwitchTo(oldcontext);
220  }
221 
222  funcctx = SRF_PERCALL_SETUP();
223  tuple_desc = funcctx->tuple_desc;
224  result_tuples = (General_path_element_t*) funcctx->user_fctx;
225 
226  if (funcctx->call_cntr < funcctx->max_calls) {
227  HeapTuple tuple;
228  Datum result;
229  Datum *values;
230  bool* nulls;
231 
232  /*********************************************************************
233  OUT seq INTEGER,
234  OUT path_seq INTEGER,
235  OUT start_vid BIGINT,
236  OUT end_vid BIGINT,
237  OUT node BIGINT,
238  OUT edge BIGINT,
239  OUT cost FLOAT,
240  OUT agg_cost FLOAT
241  **********************************************************************/
242 
243 
244  size_t numb = 8;
245  values = palloc(numb * sizeof(Datum));
246  nulls = palloc(numb * sizeof(bool));
247 
248  size_t i;
249  for (i = 0; i < numb; ++i) {
250  nulls[i] = false;
251  }
252 
253 
254  values[0] = Int32GetDatum(funcctx->call_cntr + 1);
255  values[1] = Int32GetDatum(result_tuples[funcctx->call_cntr].seq);
256  values[2] = Int64GetDatum(result_tuples[funcctx->call_cntr].start_id);
257  values[3] = Int64GetDatum(result_tuples[funcctx->call_cntr].end_id);
258  values[4] = Int64GetDatum(result_tuples[funcctx->call_cntr].node);
259  values[5] = Int64GetDatum(result_tuples[funcctx->call_cntr].edge);
260  values[6] = Float8GetDatum(result_tuples[funcctx->call_cntr].cost);
261  values[7] = Float8GetDatum(result_tuples[funcctx->call_cntr].agg_cost);
262 
263 
264  tuple = heap_form_tuple(tuple_desc, values, nulls);
265  result = HeapTupleGetDatum(tuple);
266  SRF_RETURN_NEXT(funcctx, result);
267  } else {
268  SRF_RETURN_DONE(funcctx);
269  }
270 }
#define PGR_DBG(...)
Definition: debug_macro.h:34
int64_t * pgr_get_bigIntArray(size_t *arrlen, ArrayType *input)
enforces the input array to be NOT empty
Definition: arrays_input.c:124
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)
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:578
void check_parameters(int heuristic, double factor, double epsilon)
if(DOXYGEN_FOUND) configure_file($
Definition: CMakeLists.txt:13
void pgr_SPI_connect(void)
void pgr_get_edges_xy_reversed(char *edges_sql, Pgr_edge_xy_t **edges, size_t *total_edges)
for many to 1 on aStar
Definition: edges_input.c:591
PGDLLEXPORT Datum astarManyToMany(PG_FUNCTION_ARGS)
Definition: astar.c:163
PG_FUNCTION_INFO_V1(astarManyToMany)
static void process(char *edges_sql, ArrayType *starts, ArrayType *ends, bool directed, int heuristic, double factor, double epsilon, bool only_cost, bool normal, General_path_element_t **result_tuples, size_t *result_count)
Definition: astar.c:71
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)