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