Skip to content

Commit 615a9a2

Browse files
committed
Path::collapse_paths get_tuples in to_postgres namespace
1 parent 78f765f commit 615a9a2

4 files changed

Lines changed: 109 additions & 59 deletions

File tree

include/cpp_common/path.hpp

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -143,17 +143,6 @@ class Path {
143143
Path_rt **ret_path,
144144
size_t &sequence, int routeId) const;
145145

146-
void generate_postgres_data(
147-
Path_rt **postgres_data,
148-
size_t &sequence) const;
149-
150-
void generate_tuples(MST_rt**, size_t&) const;
151-
friend size_t collapse_paths(MST_rt**, const std::deque<Path>&);
152-
153-
friend size_t collapse_paths(
154-
Path_rt **ret_path,
155-
const std::deque< Path > &paths);
156-
157146

158147
/** @brief discards common vertices with greater agg_cost */
159148
friend void equi_cost(std::deque< Path > &paths);

include/cpp_common/to_postgres.hpp

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,14 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
2727
#pragma once
2828

2929
#include <vector>
30+
#include <deque>
3031
#include <cstddef>
3132

3233
#include "c_types/contractionHierarchies_rt.h"
3334
#include "c_types/iid_t_rt.h"
3435
#include "c_types/routes_t.h"
36+
#include "c_types/path_rt.h"
37+
#include "c_types/mst_rt.h"
3538

3639
#include "cpp_common/path.hpp"
3740
#include "cpp_common/base_graph.hpp"
@@ -48,6 +51,21 @@ size_t count_rows(const std::vector<std::vector<double>>&);
4851

4952
} // namespace detail
5053

54+
/*
55+
* @brief Via Routes save on a C array
56+
*/
57+
size_t get_viaRoute(std::deque<pgrouting::Path>&, Routes_t**);
58+
59+
/*
60+
* @brief get tuples from a Path to a Path_rt
61+
*/
62+
size_t get_tuples(const std::deque<pgrouting::Path>&, Path_rt*&);
63+
64+
/*
65+
* @brief get tuples from a Path to a MST_rt
66+
*/
67+
size_t get_tuples(const std::deque<pgrouting::Path>&, MST_rt*&);
68+
5169
/** @brief Vector of vertices id are saved on a C array
5270
*
5371
* @param[in] graph Created graph with the base Graph
@@ -206,11 +224,6 @@ void graph_to_tuple(
206224
}
207225
}
208226

209-
/*
210-
* @brief Via Routes save on a C array
211-
*/
212-
size_t get_viaRoute(std::deque<pgrouting::Path>&, Routes_t**);
213-
214227
} // namespace to_postgres
215228
} // namespace pgrouting
216229

src/common/path.cpp

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -228,36 +228,6 @@ void Path::append(const Path &other) {
228228
}
229229

230230

231-
void Path::generate_postgres_data(
232-
Path_rt **postgres_data,
233-
size_t &sequence) const {
234-
for (const auto e : path) {
235-
auto agg_cost = std::fabs(
236-
e.agg_cost - (std::numeric_limits<double>::max)()) < 1?
237-
std::numeric_limits<double>::infinity() : e.agg_cost;
238-
auto cost = std::fabs(e.cost - (std::numeric_limits<double>::max)()) < 1?
239-
std::numeric_limits<double>::infinity() : e.cost;
240-
241-
(*postgres_data)[sequence] = {start_id(), end_id(), e.node, e.edge, cost, agg_cost};
242-
++sequence;
243-
}
244-
}
245-
246-
void Path::generate_tuples(
247-
MST_rt **tuples,
248-
size_t &sequence) const {
249-
for (const auto e : path) {
250-
auto agg_cost = std::fabs(
251-
e.agg_cost - (std::numeric_limits<double>::max)()) < 1?
252-
std::numeric_limits<double>::infinity() : e.agg_cost;
253-
auto cost = std::fabs(e.cost - (std::numeric_limits<double>::max)()) < 1?
254-
std::numeric_limits<double>::infinity() : e.cost;
255-
256-
(*tuples)[sequence] = {start_id(), 0, e.pred, e.node, e.edge, cost, agg_cost};
257-
++sequence;
258-
}
259-
}
260-
261231

262232
/* used by ksp */
263233
void Path::get_pg_nksp_path(
@@ -325,17 +295,6 @@ collapse_paths(
325295
return sequence;
326296
}
327297

328-
size_t
329-
collapse_paths(
330-
MST_rt **tuples,
331-
const std::deque<Path> &paths) {
332-
size_t sequence = 0;
333-
for (const Path &path : paths) {
334-
if (path.path.size() > 0) path.generate_tuples(tuples, sequence);
335-
}
336-
return sequence;
337-
}
338-
339298
/*
340299
* sort the paths by size from greater to smaller
341300
* and sort each path by node

src/cpp_common/to_postgres.cpp

Lines changed: 91 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,35 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
2727
#include <cstddef>
2828
#include <deque>
2929
#include <vector>
30+
#include <limits>
3031

3132
#include "c_types/routes_t.h"
33+
#include "c_types/path_rt.h"
34+
#include "c_types/mst_rt.h"
3235

3336
#include "cpp_common/path.hpp"
3437
#include "cpp_common/alloc.hpp"
3538
#include "cpp_common/assert.hpp"
3639

3740
namespace {
3841

42+
double
43+
to_inf(double value) {
44+
return std::fabs(value - (std::numeric_limits<double>::max)()) < 1?
45+
std::numeric_limits<double>::infinity() : value;
46+
}
47+
3948
void
4049
get_path(
4150
int route_id,
4251
int path_id,
4352
const pgrouting::Path &path,
44-
Routes_t **postgres_data,
53+
Routes_t **tuples,
4554
double &route_agg_cost,
4655
size_t &sequence) {
4756
int path_seq = 0;
4857
for (const auto e : path) {
49-
(*postgres_data)[sequence] = {
58+
(*tuples)[sequence] = {
5059
route_id,
5160
path_id,
5261
path_seq,
@@ -63,6 +72,45 @@ get_path(
6372
}
6473
}
6574

75+
void get_path(
76+
const pgrouting::Path &path,
77+
Path_rt* &tuples,
78+
size_t &sequence) {
79+
double prev_cost = 0;
80+
double aggcost = path.size() == 1? path[0].agg_cost : 0;
81+
for (const auto e : path) {
82+
aggcost += prev_cost;
83+
tuples[sequence] = {
84+
path.start_id(),
85+
path.end_id(),
86+
e.node,
87+
e.edge,
88+
to_inf(e.cost),
89+
to_inf(aggcost)
90+
};
91+
prev_cost = e.cost;
92+
sequence++;
93+
}
94+
}
95+
96+
void get_path(
97+
const pgrouting::Path &path,
98+
MST_rt* &tuples,
99+
size_t &sequence) {
100+
for (const auto e : path) {
101+
tuples[sequence] = {
102+
path.start_id(),
103+
0,
104+
e.pred,
105+
e.node,
106+
e.edge,
107+
to_inf(e.cost),
108+
to_inf(e.agg_cost)};
109+
++sequence;
110+
}
111+
}
112+
113+
66114
} // namespace
67115

68116
namespace pgrouting {
@@ -134,5 +182,46 @@ get_viaRoute(
134182
return sequence;
135183
}
136184

185+
size_t
186+
get_tuples(
187+
const std::deque<pgrouting::Path> &paths,
188+
Path_rt* &tuples) {
189+
pgassert(!tuples);
190+
191+
auto count = count_tuples(paths);
192+
if (count == 0) return 0;
193+
194+
tuples = pgr_alloc(count, tuples);
195+
196+
size_t sequence = 0;
197+
for (const auto &path : paths) {
198+
if (path.size() > 0) {
199+
::get_path(path, tuples, sequence);
200+
}
201+
}
202+
return sequence;
203+
}
204+
205+
206+
size_t
207+
get_tuples(
208+
const std::deque<pgrouting::Path> &paths,
209+
MST_rt* &tuples) {
210+
pgassert(!tuples);
211+
212+
auto count = count_tuples(paths);
213+
if (count == 0) return 0;
214+
215+
tuples = pgr_alloc(count, tuples);
216+
217+
size_t sequence = 0;
218+
for (const Path &path : paths) {
219+
if (path.size() > 0) {
220+
::get_path(path, tuples, sequence);
221+
}
222+
}
223+
return sequence;
224+
}
225+
137226
} // namespace to_postgres
138227
} // namespace pgrouting

0 commit comments

Comments
 (0)