Skip to content

Commit 6532eb9

Browse files
committed
(C/C++) Reorganizing code: removing duplicated get_route code
1 parent b5ec2bb commit 6532eb9

File tree

9 files changed

+187
-320
lines changed

9 files changed

+187
-320
lines changed

include/cpp_common/to_postgres.hpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,16 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
3131

3232
#include "c_types/contractionHierarchies_rt.h"
3333
#include "c_types/iid_t_rt.h"
34+
#include "c_types/routes_t.h"
3435

36+
#include "cpp_common/path.hpp"
3537
#include "cpp_common/base_graph.hpp"
3638
#include "cpp_common/alloc.hpp"
3739
#include "cpp_common/identifiers.hpp"
3840

3941

4042
namespace pgrouting {
43+
namespace to_postgres {
4144
namespace detail {
4245

4346
/** @brief Count results that are going to be passed to postgres
@@ -68,8 +71,6 @@ count_rows(
6871

6972
} // namespace detail
7073

71-
namespace to_postgres {
72-
7374
/** @brief Stored results on a vector are saved on a C array
7475
*
7576
* @param[in] graph Created graph with the base Graph
@@ -203,6 +204,11 @@ void graph_to_tuple(
203204
}
204205
}
205206

207+
/*
208+
* @brief Via Routes save on a C array
209+
*/
210+
size_t get_viaRoute(std::deque<pgrouting::Path>&, Routes_t**);
211+
206212
} // namespace to_postgres
207213
} // namespace pgrouting
208214

src/common/ch_vertex.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ Identifiers<int64_t>&
5555

5656

5757
bool CH_vertex::has_contracted_vertices() const {
58-
return (m_contracted_vertices.size() == 0);
58+
return !m_contracted_vertices.empty();
5959
}
6060

6161
void CH_vertex::add_contracted_vertex(CH_vertex& v) {

src/cpp_common/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ ADD_LIBRARY(cpp_common OBJECT
66
messages.cpp
77
combinations.cpp
88

9+
to_postgres.cpp
910
get_check_data.cpp
1011
pgdata_fetchers.cpp
1112
pgdata_getters.cpp

src/cpp_common/compPaths.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,14 @@ bool compPathsLess::operator()(const Path &p1, const Path &p2) const {
4444
if (!(std::fabs(p2.tot_cost() - p1.tot_cost())
4545
<
4646
std::numeric_limits<double>::epsilon())) {
47-
if (p1.tot_cost() > p2.tot_cost()) return false;
48-
if (p1.tot_cost() < p2.tot_cost()) return true;
47+
if (p1.tot_cost() > p2.tot_cost()) return false;
48+
if (p1.tot_cost() < p2.tot_cost()) return true;
4949
}
5050

5151
// paths costs are equal now check by length
5252
if (p1.size() > p2.size()) return false;
5353
if (p1.size() < p2.size()) return true;
5454

55-
// pgassert(p1.tot_cost() == p2.tot_cost());
5655
pgassert(p1.size() == p2.size());
5756

5857
// paths weights & lengths are equal now check by node ID
@@ -62,7 +61,6 @@ bool compPathsLess::operator()(const Path &p1, const Path &p2) const {
6261
if (p1[i].node < p2[i].node) return true;
6362
}
6463

65-
// pgassert(p1.tot_cost() == p2.tot_cost());
6664
pgassert(p1.size() == p2.size());
6765
#ifdef NDEBUG
6866
for (i = 0; i < p1.size(); i++) {

src/cpp_common/to_postgres.cpp

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*PGR-GNU*****************************************************************
2+
File: to_postgres.cpp
3+
4+
Copyright (c) 2025 pgRouting developers
5+
6+
7+
------
8+
9+
This program is free software; you can redistribute it and/or modify
10+
it under the terms of the GNU General Public License as published by
11+
the Free Software Foundation; either version 2 of the License, or
12+
(at your option) any later version.
13+
14+
This program is distributed in the hope that it will be useful,
15+
but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
GNU General Public License for more details.
18+
19+
You should have received a copy of the GNU General Public License
20+
along with this program; if not, write to the Free Software
21+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22+
23+
********************************************************************PGR-GNU*/
24+
25+
#include "cpp_common/to_postgres.hpp"
26+
27+
#include <cstddef>
28+
#include <deque>
29+
30+
#include "c_types/routes_t.h"
31+
32+
#include "cpp_common/path.hpp"
33+
#include "cpp_common/alloc.hpp"
34+
#include "cpp_common/assert.hpp"
35+
36+
namespace {
37+
38+
void
39+
get_path(
40+
int route_id,
41+
int path_id,
42+
const pgrouting::Path &path,
43+
Routes_t **postgres_data,
44+
double &route_agg_cost,
45+
size_t &sequence) {
46+
int path_seq = 0;
47+
for (const auto e : path) {
48+
(*postgres_data)[sequence] = {
49+
route_id,
50+
path_id,
51+
path_seq,
52+
path.start_id(),
53+
path.end_id(),
54+
e.node,
55+
e.edge,
56+
e.cost,
57+
e.agg_cost,
58+
route_agg_cost};
59+
route_agg_cost += path[path_seq].cost;
60+
path_seq++;
61+
++sequence;
62+
}
63+
}
64+
65+
} // namespace
66+
67+
namespace pgrouting {
68+
namespace to_postgres {
69+
70+
/**
71+
* @param[in] paths The set of Paths
72+
* @param[out] tuples The C array of Route_t
73+
* @returns number of tuples on the C array
74+
*
75+
* Currently works for
76+
* - pgr_dijkstraVia
77+
* - pgr_trspVia
78+
* - pgr_trspVia_withPoints
79+
* - pgr_withPointsVia
80+
*/
81+
size_t
82+
get_viaRoute(
83+
std::deque<pgrouting::Path> &paths,
84+
Routes_t **tuples) {
85+
pgassert(!(*tuples));
86+
87+
auto count = count_tuples(paths);
88+
if (count == 0) return 0;
89+
90+
(*tuples) = pgr_alloc(count, (*tuples));
91+
92+
size_t sequence = 0;
93+
int path_id = 1;
94+
int route_id = 1;
95+
double route_agg_cost = 0;
96+
for (auto &p : paths) {
97+
p.recalculate_agg_cost();
98+
}
99+
for (const auto &path : paths) {
100+
if (path.size() > 0) {
101+
::get_path(route_id, path_id, path, tuples, route_agg_cost, sequence);
102+
}
103+
++path_id;
104+
}
105+
(*tuples)[count - 1].edge = -2;
106+
107+
pgassert(count == sequence);
108+
109+
return sequence;
110+
}
111+
112+
} // namespace to_postgres
113+
} // namespace pgrouting

src/dijkstra/dijkstraVia_driver.cpp

Lines changed: 9 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -34,61 +34,11 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
3434

3535
#include "c_types/routes_t.h"
3636
#include "cpp_common/pgdata_getters.hpp"
37-
#include "cpp_common/alloc.hpp"
37+
#include "cpp_common/to_postgres.hpp"
3838
#include "cpp_common/assert.hpp"
3939
#include "dijkstra/dijkstraVia.hpp"
4040

4141

42-
namespace {
43-
void
44-
get_path(
45-
int route_id,
46-
int path_id,
47-
const pgrouting::Path &path,
48-
Routes_t **postgres_data,
49-
double &route_cost,
50-
size_t &sequence) {
51-
size_t i = 0;
52-
for (const auto e : path) {
53-
(*postgres_data)[sequence] = {
54-
route_id,
55-
path_id,
56-
static_cast<int>(i),
57-
path.start_id(),
58-
path.end_id(),
59-
e.node,
60-
e.edge,
61-
e.cost,
62-
e.agg_cost,
63-
route_cost};
64-
route_cost += path[i].cost;
65-
++i;
66-
++sequence;
67-
}
68-
}
69-
70-
71-
size_t
72-
get_route(
73-
Routes_t **ret_path,
74-
std::deque<pgrouting::Path> &paths) {
75-
size_t sequence = 0;
76-
int path_id = 1;
77-
int route_id = 1;
78-
double route_cost = 0; // routes_agg_cost
79-
for (auto &p : paths) {
80-
p.recalculate_agg_cost();
81-
}
82-
for (const auto &path : paths) {
83-
if (path.size() > 0) {
84-
get_path(route_id, path_id, path, ret_path, route_cost, sequence);
85-
}
86-
++path_id;
87-
}
88-
return sequence;
89-
}
90-
} // namespace
91-
9242
void
9343
pgr_do_dijkstraVia(
9444
const char *edges_sql,
@@ -97,7 +47,7 @@ pgr_do_dijkstraVia(
9747
bool directed,
9848
bool strict,
9949
bool U_turn_on_edge,
100-
Routes_t** return_tuples, size_t* return_count,
50+
Routes_t** return_tuples, size_t* return_count,
10151

10252
char** log_msg,
10353
char** notice_msg,
@@ -108,10 +58,11 @@ pgr_do_dijkstraVia(
10858
using pgrouting::pgr_free;
10959
using pgrouting::pgget::get_intArray;
11060
using pgrouting::pgget::get_edges;
61+
using pgrouting::to_postgres::get_viaRoute;
11162

11263
std::ostringstream log;
113-
std::ostringstream err;
11464
std::ostringstream notice;
65+
std::ostringstream err;
11566
const char *hint = nullptr;
11667

11768
try {
@@ -121,21 +72,19 @@ pgr_do_dijkstraVia(
12172
pgassert(!(*return_tuples));
12273
pgassert(*return_count == 0);
12374

124-
125-
12675
auto via = get_intArray(viaArr, false);
12776

12877
hint = edges_sql;
12978
auto edges = get_edges(std::string(edges_sql), true, false);
13079

13180
if (edges.empty()) {
13281
*notice_msg = to_pg_msg("No edges found");
133-
*log_msg = hint? to_pg_msg(hint) : to_pg_msg(log);
82+
*log_msg = to_pg_msg(edges_sql);
13483
return;
13584
}
13685
hint = nullptr;
13786

138-
std::deque<Path>paths;
87+
std::deque<Path> paths;
13988
if (directed) {
14089
pgrouting::DirectedGraph digraph;
14190
digraph.insert_edges(edges);
@@ -158,23 +107,13 @@ pgr_do_dijkstraVia(
158107
log);
159108
}
160109

161-
size_t count(count_tuples(paths));
110+
(*return_count) = get_viaRoute(paths, return_tuples);
162111

163-
if (count == 0) {
164-
(*return_tuples) = NULL;
165-
(*return_count) = 0;
166-
notice <<
167-
"No paths found";
168-
*log_msg = to_pg_msg(notice);
112+
if ((*return_count) == 0) {
113+
*log_msg = to_pg_msg("No paths found");
169114
return;
170115
}
171116

172-
// get the space required to store all the paths
173-
(*return_tuples) = pgr_alloc(count, (*return_tuples));
174-
log << "\nConverting a set of paths into the tuples";
175-
(*return_count) = (get_route(return_tuples, paths));
176-
(*return_tuples)[count - 1].edge = -2;
177-
178117
*log_msg = to_pg_msg(log);
179118
*notice_msg = to_pg_msg(notice);
180119
} catch (AssertFailedException &except) {

0 commit comments

Comments
 (0)