PGROUTING  2.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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"
38 
40 
41 PGDLLEXPORT Datum astarManyToMany(PG_FUNCTION_ARGS);
43 
44 
45 static
46 void
47 process(char* edges_sql,
48  ArrayType *starts,
49  ArrayType *ends,
50  bool directed,
51  int heuristic,
52  double factor,
53  double epsilon,
54  bool only_cost,
55  bool normal,
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 
65  int64_t* end_vidsArr = NULL;
66  size_t size_end_vidsArr = 0;
67 
68  Pgr_edge_xy_t *edges = NULL;
69  size_t total_edges = 0;
70 
71  if (normal) {
72  pgr_get_edges_xy(edges_sql, &edges, &total_edges);
73  start_vidsArr = (int64_t*)
74  pgr_get_bigIntArray(&size_start_vidsArr, starts);
75  end_vidsArr = (int64_t*)
76  pgr_get_bigIntArray(&size_end_vidsArr, ends);
77  } else {
78  pgr_get_edges_xy_reversed(edges_sql, &edges, &total_edges);
79  end_vidsArr = (int64_t*)
80  pgr_get_bigIntArray(&size_end_vidsArr, starts);
81  start_vidsArr = (int64_t*)
82  pgr_get_bigIntArray(&size_start_vidsArr, ends);
83  }
84 
85  if (total_edges == 0) {
86  PGR_DBG("No edges found");
87  (*result_count) = 0;
88  (*result_tuples) = NULL;
90  return;
91  }
92 
93  PGR_DBG("Starting processing");
94  char *log_msg = NULL;
95  char *notice_msg = NULL;
96  char *err_msg = NULL;
97  clock_t start_t = clock();
99  edges, total_edges,
100  start_vidsArr, size_start_vidsArr,
101  end_vidsArr, size_end_vidsArr,
102  directed,
103  heuristic,
104  factor,
105  epsilon,
106  only_cost,
107  normal,
108  result_tuples, result_count,
109  &log_msg,
110  &notice_msg,
111  &err_msg);
112 
113  if (only_cost) {
114  time_msg("processing pgr_astarCost(many to many)", start_t, clock());
115  } else {
116  time_msg("processing pgr_astar(many to many)", start_t, clock());
117  }
118 
119 
120  if (err_msg && (*result_tuples)) {
121  pfree(*result_tuples);
122  (*result_tuples) = NULL;
123  (*result_count) = 0;
124  }
125 
126  pgr_global_report(log_msg, notice_msg, err_msg);
127 
128  if (log_msg) pfree(log_msg);
129  if (notice_msg) pfree(notice_msg);
130  if (err_msg) pfree(err_msg);
131  if (edges) pfree(edges);
132  if (start_vidsArr) pfree(start_vidsArr);
133  if (end_vidsArr) pfree(end_vidsArr);
134 
135  pgr_SPI_finish();
136 }
137 
138 PGDLLEXPORT Datum
139 astarManyToMany(PG_FUNCTION_ARGS) {
140  FuncCallContext *funcctx;
141  TupleDesc tuple_desc;
142 
143  /**********************************************************************/
144  General_path_element_t *result_tuples = NULL;
145  size_t result_count = 0;
146  /**********************************************************************/
147 
148  if (SRF_IS_FIRSTCALL()) {
149  MemoryContext oldcontext;
150  funcctx = SRF_FIRSTCALL_INIT();
151  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
152 
153 
154  /**********************************************************************/
155  /*
156  edges_sql TEXT,
157  start_vids ARRAY[ANY_INTEGER], -- anyarray
158  end_vids ARRAY[ANY_INTEGER], -- anyarray
159  directed BOOLEAN DEFAULT true,
160  heuristic INTEGER DEFAULT 0,
161  factor FLOAT DEFAULT 1.0,
162  epsilon FLOAT DEFAULT 1.0,
163 
164  **********************************************************************/
165 
166  process(
167  text_to_cstring(PG_GETARG_TEXT_P(0)),
168  PG_GETARG_ARRAYTYPE_P(1),
169  PG_GETARG_ARRAYTYPE_P(2),
170  PG_GETARG_BOOL(3),
171  PG_GETARG_INT32(4),
172  PG_GETARG_FLOAT8(5),
173  PG_GETARG_FLOAT8(6),
174  PG_GETARG_BOOL(7),
175  PG_GETARG_BOOL(8),
176  &result_tuples,
177  &result_count);
178 
179 
180 #if PGSQL_VERSION > 95
181  funcctx->max_calls = result_count;
182 #else
183  funcctx->max_calls = (uint32_t)result_count;
184 #endif
185  funcctx->max_calls = (uint32_t) result_count;
186  funcctx->user_fctx = result_tuples;
187  if (get_call_result_type(fcinfo, NULL, &tuple_desc)
188  != TYPEFUNC_COMPOSITE)
189  ereport(ERROR,
190  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
191  errmsg("function returning record called in context "
192  "that cannot accept type record")));
193 
194  funcctx->tuple_desc = tuple_desc;
195  MemoryContextSwitchTo(oldcontext);
196  }
197 
198  funcctx = SRF_PERCALL_SETUP();
199  tuple_desc = funcctx->tuple_desc;
200  result_tuples = (General_path_element_t*) funcctx->user_fctx;
201 
202  if (funcctx->call_cntr < funcctx->max_calls) {
203  HeapTuple tuple;
204  Datum result;
205  Datum *values;
206  bool* nulls;
207 
208  /*********************************************************************
209  OUT seq INTEGER,
210  OUT path_seq INTEGER,
211  OUT start_vid BIGINT,
212  OUT end_vid BIGINT,
213  OUT node BIGINT,
214  OUT edge BIGINT,
215  OUT cost FLOAT,
216  OUT agg_cost FLOAT
217  **********************************************************************/
218 
219 
220  size_t numb = 8;
221  values = palloc(numb * sizeof(Datum));
222  nulls = palloc(numb * sizeof(bool));
223 
224  size_t i;
225  for (i = 0; i < numb; ++i) {
226  nulls[i] = false;
227  }
228 
229 
230  values[0] = Int32GetDatum(funcctx->call_cntr + 1);
231  values[1] = Int32GetDatum(result_tuples[funcctx->call_cntr].seq);
232  values[2] = Int64GetDatum(result_tuples[funcctx->call_cntr].start_id);
233  values[3] = Int64GetDatum(result_tuples[funcctx->call_cntr].end_id);
234  values[4] = Int64GetDatum(result_tuples[funcctx->call_cntr].node);
235  values[5] = Int64GetDatum(result_tuples[funcctx->call_cntr].edge);
236  values[6] = Float8GetDatum(result_tuples[funcctx->call_cntr].cost);
237  values[7] = Float8GetDatum(result_tuples[funcctx->call_cntr].agg_cost);
238 
239 
240  tuple = heap_form_tuple(tuple_desc, values, nulls);
241  result = HeapTupleGetDatum(tuple);
242  SRF_RETURN_NEXT(funcctx, result);
243  } else {
244  SRF_RETURN_DONE(funcctx);
245  }
246 }
static edge_t edges[22573]
#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:573
void check_parameters(int heuristic, double factor, double epsilon)
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:586
PGDLLEXPORT Datum astarManyToMany(PG_FUNCTION_ARGS)
Definition: astar.c:139
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:47
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)