-
-
Notifications
You must be signed in to change notification settings - Fork 395
Expand file tree
/
Copy pathto_postgres.cpp
More file actions
378 lines (313 loc) · 8.64 KB
/
Copy pathto_postgres.cpp
File metadata and controls
378 lines (313 loc) · 8.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/*PGR-GNU*****************************************************************
File: to_postgres.cpp
Copyright (c) 2025-2026 pgRouting developers
Mail: project@pgrouting.org
------
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
********************************************************************PGR-GNU*/
#include "cpp_common/to_postgres.hpp"
#include <cstddef>
#include <deque>
#include <map>
#include <vector>
#include <limits>
#include <cmath>
#include <algorithm>
#include "c_types/routes_t.h"
#include "c_types/path_rt.h"
#include "c_types/mst_rt.h"
#include "c_types/flow_t.h"
#include "cpp_common/path.hpp"
#include "cpp_common/alloc.hpp"
#include "cpp_common/assert.hpp"
namespace {
double
to_inf(double value) {
return std::fabs(value - (std::numeric_limits<double>::max)()) < 1?
std::numeric_limits<double>::infinity() : value;
}
void
get_path(
int route_id,
int path_id,
const pgrouting::Path &path,
Routes_t **tuples,
double &route_agg_cost,
size_t &sequence) {
int path_seq = 0;
for (const auto e : path) {
(*tuples)[sequence] = {
route_id,
path_id,
path_seq,
path.start_id(),
path.end_id(),
e.node,
e.edge,
e.cost,
e.agg_cost,
route_agg_cost};
route_agg_cost += path[static_cast<uint32_t>(path_seq)].cost;
path_seq++;
++sequence;
}
}
void get_path(
const pgrouting::Path &path,
Path_rt* &tuples,
size_t &sequence) {
double prev_cost = 0;
double aggcost = path.size() == 1? path[0].agg_cost : 0;
for (const auto e : path) {
aggcost += prev_cost;
tuples[sequence] = {
path.start_id(),
path.end_id(),
e.node,
e.edge,
to_inf(e.cost),
to_inf(aggcost)
};
prev_cost = e.cost;
sequence++;
}
}
void get_path(
const pgrouting::Path &path,
MST_rt* &tuples,
size_t &sequence) {
for (const auto &e : path) {
tuples[sequence] = {
path.start_id(),
0,
e.pred,
e.node,
e.edge,
to_inf(e.cost),
to_inf(e.agg_cost)};
++sequence;
}
}
} // namespace
namespace pgrouting {
namespace to_postgres {
namespace detail {
/**
* @param[in] matrix matrix[i,j] -> the i,j element contains result
* @returns total number of valid results
*
* a result is not valid when:
* - is in the diagonal: matrix[i,i]
* - has "infinity" as value
*/
size_t
count_rows(const std::vector<std::vector<double>> &matrix) {
int64_t count = 0;
for (size_t i = 0; i < matrix.size(); i++) {
count += std::count_if(
matrix[i].begin(), matrix[i].end(),
[](double value) {
return value != (std::numeric_limits<double>::max)();
});
}
return static_cast<size_t>(count) - matrix.size();
}
} // namespace detail
/**
* @param[in] paths The set of Paths
* @param[out] tuples The C array of Route_t
* @returns number of tuples on the C array
*
* Currently works for
* - pgr_dijkstraVia
* - pgr_trspVia
* - pgr_trspVia_withPoints
* - pgr_withPointsVia
*/
size_t
get_viaRoute(
std::deque<pgrouting::Path> &paths,
Routes_t **tuples) {
pgassert(!(*tuples));
auto count = count_tuples(paths);
if (count == 0) return 0;
(*tuples) = pgr_alloc(count, (*tuples));
size_t sequence = 0;
int path_id = 1;
int route_id = 1;
double route_agg_cost = 0;
for (auto &p : paths) {
p.recalculate_agg_cost();
}
for (const auto &path : paths) {
if (path.size() > 0) {
::get_path(route_id, path_id, path, tuples, route_agg_cost, sequence);
}
++path_id;
}
(*tuples)[count - 1].edge = -2;
pgassert(count == sequence);
return sequence;
}
size_t
get_tuples(
const std::deque<pgrouting::Path> &paths,
Path_rt* &tuples) {
pgassert(!tuples);
auto count = count_tuples(paths);
if (count == 0) return 0;
tuples = pgr_alloc(count, tuples);
size_t sequence = 0;
for (const auto &path : paths) {
if (path.size() > 0) {
::get_path(path, tuples, sequence);
}
}
return sequence;
}
size_t
get_tuples(
const std::deque<pgrouting::Path> &paths,
MST_rt* &tuples) {
pgassert(!tuples);
auto count = count_tuples(paths);
if (count == 0) return 0;
tuples = pgr_alloc(count, tuples);
size_t sequence = 0;
for (const Path &path : paths) {
if (path.size() > 0) {
::get_path(path, tuples, sequence);
}
}
return sequence;
}
size_t
get_tuples(const std::vector<II_t_rt> &data, II_t_rt* &tuples) {
pgassert(!tuples);
auto count = data.size();
if (count == 0) return 0;
tuples = pgrouting::pgr_alloc(count, tuples);
size_t i = 0;
for (const auto &d : data) {
tuples[i++] = d;
}
return count;
}
size_t
get_tuples(std::vector<std::vector<int64_t>> &components, II_t_rt* &tuples) {
size_t count = 0;
for (auto &component : components) {
count += component.size();
std::sort(component.begin(), component.end());
}
std::sort(components.begin(), components.end());
tuples = pgrouting::pgr_alloc(count, tuples);
size_t i = 0;
for (const auto& component : components) {
auto component_id = component[0];
for (const auto edge_id : component) {
tuples[i++]= {edge_id, component_id};
}
}
return count;
}
size_t
get_tuples(
const std::vector<MST_rt> &results,
MST_rt* &tuples) {
pgassert(!tuples);
auto count = results.size();
if (count == 0) return 0;
tuples = pgr_alloc(count, tuples);
for (size_t i = 0; i < count; i++) {
tuples[i] = results[i];
}
return count;
}
size_t
get_tuples(
const std::vector<Flow_t> &results,
Flow_t* &tuples) {
pgassert(!tuples);
auto count = results.size();
if (count == 0) return 0;
tuples = pgr_alloc(count, tuples);
for (size_t i = 0; i < count; i++) {
tuples[i] = results[i];
}
return count;
}
size_t
get_tuples(
const std::vector<MST_rt> &results,
const std::deque<pgrouting::Path> &paths,
const std::vector<std::map<int64_t, int64_t>>& depths,
MST_rt* &tuples) {
pgassert(!tuples);
if (!results.empty()) {
/*
* These are not driving distance results
*/
return get_tuples(results, tuples);
}
/*
* This are the driving distance results
*/
auto count = get_tuples(paths, tuples);
if (count == 0) return 0;
for (size_t i = 0; i < count; i++) {
const auto& row = tuples[i];
/* given the depth assign the correct depth */
int64_t depth = -1;
for (const auto &d : depths) {
/* look for the correct path */
auto itr = d.find(row.from_v);
if (itr == d.end() || !(itr->second == 0)) continue;
auto node_itr = d.find(row.node);
if (node_itr != d.end()) {
depth = node_itr->second;
}
break;
}
tuples[i].depth = depth;
}
return count;
}
size_t
get_tuples(
std::vector<Path_rt> &paths,
const std::vector<Edge_t> &edges,
Path_rt* &tuples) {
pgassert(!tuples);
if (paths.empty()) return 0;
/*
* Calculating the cost
*/
auto found = paths.size();
for (const auto &e : edges) {
for (auto &r : paths) {
if (r.edge == e.id) {
r.cost = (r.node == e.source) ? e.cost : e.reverse_cost;
--found;
}
}
if (found == 0) break;
}
tuples = pgr_alloc(paths.size(), tuples);
for (size_t i = 0; i < paths.size(); ++i) {
tuples[i] = paths[i];
}
return paths.size();
}
} // namespace to_postgres
} // namespace pgrouting