PGROUTING  2.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
one_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/pgr_types.h"
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/edges_input.h"
38 #include "./../../common/src/arrays_input.h"
40 
41 PGDLLEXPORT Datum astarOneToMany(PG_FUNCTION_ARGS);
43 
44 static
45 void
46 process(char* edges_sql,
47  int64_t start_vid,
48  ArrayType *ends,
49  bool directed,
50  int heuristic,
51  double factor,
52  double epsilon,
53  bool only_cost,
54  General_path_element_t **result_tuples,
55  size_t *result_count) {
56  check_parameters(heuristic, factor, epsilon);
57 
58  size_t size_end_vidsArr = 0;
59  int64_t* end_vidsArr = pgr_get_bigIntArray(&size_end_vidsArr, ends);
60 
62 
63  Pgr_edge_xy_t *edges = NULL;
64  size_t total_edges = 0;
65 
66  pgr_get_edges_xy(edges_sql, &edges, &total_edges);
67 
68  if (total_edges == 0) {
69  PGR_DBG("No edges found");
70  (*result_count) = 0;
71  (*result_tuples) = NULL;
73  return;
74  }
75 
76  PGR_DBG("Starting processing");
77  char* log_msg = NULL;
78  char* notice_msg = NULL;
79  char* err_msg = NULL;
80  clock_t start_t = clock();
82  edges, total_edges,
83  &start_vid, 1,
84  end_vidsArr, size_end_vidsArr,
85  directed,
86  heuristic,
87  factor,
88  epsilon,
89  only_cost,
90  true,
91  result_tuples,
92  result_count,
93  &log_msg,
94  &notice_msg,
95  &err_msg);
96 
97  if (only_cost) {
98  time_msg("processing pgr_astarCost(one to many)", start_t, clock());
99  } else {
100  time_msg("processing pgr_astar(one to many)", start_t, clock());
101  }
102 
103 
104  if (err_msg && (*result_tuples)) {
105  pfree(*result_tuples);
106  (*result_tuples) = NULL;
107  (*result_count) = 0;
108  }
109 
110  pgr_global_report(log_msg, notice_msg, err_msg);
111 
112  if (log_msg) pfree(log_msg);
113  if (notice_msg) pfree(notice_msg);
114  if (err_msg) pfree(err_msg);
115  if (edges) pfree(edges);
116  if (end_vidsArr) pfree(end_vidsArr);
117 
118  pgr_SPI_finish();
119 }
120 
121 PGDLLEXPORT Datum
122 astarOneToMany(PG_FUNCTION_ARGS) {
123  FuncCallContext *funcctx;
124  TupleDesc tuple_desc;
125 
126  General_path_element_t *result_tuples = 0;
127  size_t result_count = 0;
128 
129  if (SRF_IS_FIRSTCALL()) {
130  MemoryContext oldcontext;
131  funcctx = SRF_FIRSTCALL_INIT();
132  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
133 
134 
135  /*****************************************************************
136  edges_sql TEXT,
137  start_vid BIGINT,
138  end_vids ARRAY[ANY_INTEGER], -- anyarray
139  directed BOOLEAN DEFAULT true,
140  heuristic INTEGER DEFAULT 0,
141  factor FLOAT DEFAULT 1.0,
142  epsilon FLOAT DEFAULT 1.0,
143 
144  ****************************************************************/
145 
146 
147  process(
148  text_to_cstring(PG_GETARG_TEXT_P(0)),
149  PG_GETARG_INT64(1),
150  PG_GETARG_ARRAYTYPE_P(2),
151  PG_GETARG_BOOL(3),
152  PG_GETARG_INT32(4),
153  PG_GETARG_FLOAT8(5),
154  PG_GETARG_FLOAT8(6),
155  PG_GETARG_BOOL(7),
156  &result_tuples,
157  &result_count);
158 
159 
160 
161 #if PGSQL_VERSION > 95
162  funcctx->max_calls = result_count;
163 #else
164  funcctx->max_calls = (uint32_t)result_count;
165 #endif
166 
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].end_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 
static void process(char *edges_sql, int64_t start_vid, ArrayType *ends, bool directed, int heuristic, double factor, double epsilon, bool only_cost, General_path_element_t **result_tuples, size_t *result_count)
#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
PGDLLEXPORT Datum astarOneToMany(PG_FUNCTION_ARGS)
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:572
edge_astar_t * edges
Definition: BDATester.cpp:46
void pgr_SPI_connect(void)
char * err_msg
Definition: BDATester.cpp:50
void check_parameters(int heuristic, double factor, double epsilon)
PG_FUNCTION_INFO_V1(astarOneToMany)