-
-
Notifications
You must be signed in to change notification settings - Fork 370
/
dijkstra_driver.cpp
201 lines (167 loc) · 6.61 KB
/
dijkstra_driver.cpp
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
/*PGR-GNU*****************************************************************
File: dijkstra_driver.cpp
Generated with Template by:
Copyright (c) 2015 pgRouting developers
Mail: project@pgrouting.org
Function's developer:
Copyright (c) 2023 Celia Virginia Vergara Castillo
Copyright (c) 2015 Celia Virginia Vergara Castillo
Mail: vicky at erosion.dev
Copyright (c) 2020 The combinations_sql signature is added by Mahmoud SAKR
and Esteban ZIMANYI
mail: m_attia_sakr at yahoo.com, estebanzimanyi at gmail.com
------
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 "drivers/dijkstra/dijkstra_driver.h"
#include <sstream>
#include <deque>
#include <vector>
#include <algorithm>
#include <limits>
#include "c_types/ii_t_rt.h"
#include "cpp_common/combinations.h"
#include "cpp_common/pgr_alloc.hpp"
#include "cpp_common/pgr_assert.h"
#include "dijkstra/dijkstra.hpp"
namespace detail {
void
post_process(std::deque<pgrouting::Path> &paths, bool only_cost, bool normal, size_t n_goals, bool global) {
using pgrouting::Path;
paths.erase(std::remove_if(paths.begin(), paths.end(),
[](const Path &p) {
return p.size() == 0;
}),
paths.end());
using difference_type = std::deque<double>::difference_type;
if (!normal) {
for (auto &path : paths) path.reverse();
}
if (!only_cost) {
for (auto &p : paths) {
p.recalculate_agg_cost();
}
}
if (n_goals != (std::numeric_limits<size_t>::max)()) {
std::sort(paths.begin(), paths.end(),
[](const Path &e1, const Path &e2)->bool {
return e1.end_id() < e2.end_id();
});
std::stable_sort(paths.begin(), paths.end(),
[](const Path &e1, const Path &e2)->bool {
return e1.start_id() < e2.start_id();
});
std::stable_sort(paths.begin(), paths.end(),
[](const Path &e1, const Path &e2)->bool {
return e1.tot_cost() < e2.tot_cost();
});
if (global && n_goals < paths.size()) {
paths.erase(paths.begin() + static_cast<difference_type>(n_goals), paths.end());
}
} else {
std::sort(paths.begin(), paths.end(),
[](const Path &e1, const Path &e2)->bool {
return e1.end_id() < e2.end_id();
});
std::stable_sort(paths.begin(), paths.end(),
[](const Path &e1, const Path &e2)->bool {
return e1.start_id() < e2.start_id();
});
}
}
} // namespace detail
void
pgr_do_dijkstra(
Edge_t *data_edges, size_t total_edges,
II_t_rt *combinationsArr, size_t total_combinations,
int64_t *start_vidsArr, size_t size_start_vidsArr,
int64_t *end_vidsArr, size_t size_end_vidsArr,
bool directed,
bool only_cost,
bool normal,
int64_t n_goals,
bool global,
Path_rt **return_tuples,
size_t *return_count,
char ** log_msg,
char ** notice_msg,
char ** err_msg) {
using pgrouting::Path;
using pgrouting::pgr_alloc;
using pgrouting::pgr_msg;
using pgrouting::pgr_free;
std::ostringstream log;
std::ostringstream err;
std::ostringstream notice;
try {
pgassert(total_edges != 0);
pgassert(!(*log_msg));
pgassert(!(*notice_msg));
pgassert(!(*err_msg));
pgassert(!(*return_tuples));
pgassert(*return_count == 0);
pgassert(total_combinations != 0 || (size_start_vidsArr != 0 && size_end_vidsArr != 0));
graphType gType = directed? DIRECTED: UNDIRECTED;
size_t n = n_goals <= 0? (std::numeric_limits<size_t>::max)() : static_cast<size_t>(n_goals);
std::deque<Path>paths;
auto combinations = total_combinations?
pgrouting::utilities::get_combinations(combinationsArr, total_combinations)
: pgrouting::utilities::get_combinations(start_vidsArr, size_start_vidsArr, end_vidsArr, size_end_vidsArr);
if (directed) {
pgrouting::DirectedGraph graph(gType);
graph.insert_edges(data_edges, total_edges);
paths = pgrouting::algorithms::dijkstra(graph, combinations, only_cost, n);
} else {
pgrouting::UndirectedGraph graph(gType);
graph.insert_edges(data_edges, total_edges);
paths = pgrouting::algorithms::dijkstra(graph, combinations, only_cost, n);
}
detail::post_process(paths, only_cost, normal, n, global);
combinations.clear();
size_t count(0);
count = count_tuples(paths);
if (count == 0) {
(*return_tuples) = NULL;
(*return_count) = 0;
notice << "No paths found";
*log_msg = pgr_msg(notice.str().c_str());
return;
}
(*return_tuples) = pgr_alloc(count, (*return_tuples));
(*return_count) = (collapse_paths(return_tuples, paths));
*log_msg = log.str().empty()?
*log_msg :
pgr_msg(log.str().c_str());
*notice_msg = notice.str().empty()?
*notice_msg :
pgr_msg(notice.str().c_str());
} catch (AssertFailedException &except) {
(*return_tuples) = pgr_free(*return_tuples);
(*return_count) = 0;
err << except.what();
*err_msg = pgr_msg(err.str().c_str());
*log_msg = pgr_msg(log.str().c_str());
} catch (std::exception &except) {
(*return_tuples) = pgr_free(*return_tuples);
(*return_count) = 0;
err << except.what();
*err_msg = pgr_msg(err.str().c_str());
*log_msg = pgr_msg(log.str().c_str());
} catch(...) {
(*return_tuples) = pgr_free(*return_tuples);
(*return_count) = 0;
err << "Caught unknown exception!";
*err_msg = pgr_msg(err.str().c_str());
*log_msg = pgr_msg(log.str().c_str());
}
}