Skip to content

Commit d35cbe2

Browse files
committed
(dagShortestPath) using the shortestPath process & driver
1 parent 19eba9d commit d35cbe2

7 files changed

Lines changed: 56 additions & 62 deletions

File tree

include/c_common/enums.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ enum Which {
3232
TOPOSORT = 11,
3333
/** shortest_paths */
3434
DIJKSTRA = 21, WITHPOINTS, OLD_WITHPOINTS, BDDIJKSTRA, EDWARDMOORE,
35+
DAGSP,
3536
/** allpairs **/
3637
FLOYD = 31, JOHNSON,
3738
/** metrics **/

include/dagShortestPath/dagShortestPath.hpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,20 @@ class Pgr_dag {
218218
};
219219
};
220220

221+
namespace algorithms {
222+
223+
template <class G>
224+
std::deque<pgrouting::Path>
225+
dagShortestPath(
226+
G &graph,
227+
std::map<int64_t, std::set<int64_t>> &combinations,
228+
bool only_cost = false) {
229+
pgrouting::Pgr_dag<G> fn_dag;
230+
auto paths = fn_dag.dag(graph, combinations, only_cost);
231+
return paths;
232+
}
233+
234+
} // namespace algorithms
221235
} // namespace pgrouting
222236

223237
#endif // INCLUDE_DAGSHORTESTPATH_DAGSHORTESTPATH_HPP_

pgtap/others/dagShortestPath/no_crash_test.pg

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
BEGIN;
77

88
UPDATE edges SET cost = sign(cost), reverse_cost = -sign(cost);
9-
SELECT CASE WHEN min_version('4.0.0') THEN plan(81) ELSE plan(1) END;
9+
SELECT CASE WHEN min_version('4.0.0') THEN plan(82) ELSE plan(1) END;
1010

1111
CREATE OR REPLACE FUNCTION no_crash()
1212
RETURNS SETOF TEXT AS
@@ -135,6 +135,7 @@ BEGIN
135135
'NULL::TEXT'
136136
]::TEXT[];
137137
RETURN query SELECT * FROM no_crash_test('pgr_dagShortestPath', params, subs);
138+
RETURN query SELECT throw_on_empty_edges_sql('pgr_dagShortestPath', ',1,5');
138139

139140
END
140141
$BODY$

src/cpp_common/utilities.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ get_name(Which which, bool is_only_cost, bool is_near, bool is_matrix) {
7878
case EDWARDMOORE:
7979
base = "pgr_edwardMoore";
8080
break;
81+
case DAGSP :
82+
base = "pgr_dagShortestPath";
83+
break;
8184
case OLD_WITHPOINTS:
8285
case WITHPOINTS:
8386
base = "pgr_withPoints";
@@ -96,26 +99,21 @@ estimate_drivingSide(char driving_side, Which which) {
9699
d_side = ' ';
97100
}
98101
switch (which) {
102+
case DAGSP :
99103
case EDWARDMOORE:
100104
case BDDIJKSTRA:
101105
case DIJKSTRA:
102-
{
103106
return ' ';
104107
break;
105-
}
106108
case WITHPOINTS:
107-
{
108109
if (d_side == ' ') {
109110
throw std::make_pair(std::string("Invalid value of 'driving side'"),
110111
std::string("Valid value are 'r', 'l', 'b'"));
111112
}
112113
break;
113-
}
114114
default:
115-
{
116115
/* For the moment its old signature of pgr_withPoints */
117116
if (!((d_side == 'r') || (d_side == 'l'))) d_side = 'b';
118-
}
119117
}
120118
return d_side;
121119
}

src/dagShortestPath/CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,4 @@
33
# License: GPL-2 See https://github.com/pgRouting/pgrouting/blob/main/LICENSE
44
ADD_LIBRARY(dagShortestPath OBJECT
55
dagShortestPath.c
6-
dagShortestPath_driver.cpp
7-
)
6+
)

src/dagShortestPath/dagShortestPath.c

Lines changed: 27 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -30,59 +30,12 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
3030

3131
#include <stdbool.h>
3232
#include "c_common/postgres_connection.h"
33-
3433
#include "c_types/path_rt.h"
35-
#include "c_common/debug_macro.h"
36-
#include "c_common/e_report.h"
37-
#include "c_common/time_msg.h"
38-
#include "drivers/dagShortestPath/dagShortestPath_driver.h"
34+
#include "process/shortestPath_process.h"
3935

4036
PGDLLEXPORT Datum _pgr_dagshortestpath_v4(PG_FUNCTION_ARGS);
4137
PG_FUNCTION_INFO_V1(_pgr_dagshortestpath_v4);
4238

43-
44-
static
45-
void
46-
process(
47-
char* edges_sql,
48-
char* combinations_sql,
49-
ArrayType *starts,
50-
ArrayType *ends,
51-
bool only_cost,
52-
bool normal,
53-
Path_rt **result_tuples,
54-
size_t *result_count) {
55-
pgr_SPI_connect();
56-
char* log_msg = NULL;
57-
char* notice_msg = NULL;
58-
char* err_msg = NULL;
59-
(*result_tuples) = NULL;
60-
(*result_count) = 0;
61-
62-
clock_t start_t = clock();
63-
pgr_do_dagShortestPath(
64-
edges_sql,
65-
combinations_sql,
66-
starts, ends,
67-
68-
only_cost,
69-
normal,
70-
71-
result_tuples,
72-
result_count,
73-
&log_msg,
74-
&notice_msg,
75-
&err_msg);
76-
time_msg(" processing pgr_dagShortestPath", start_t, clock());
77-
78-
if (err_msg) {
79-
if (*result_tuples) pfree(*result_tuples);
80-
}
81-
pgr_global_report(&log_msg, &notice_msg, &err_msg);
82-
83-
pgr_SPI_finish();
84-
}
85-
8639
PGDLLEXPORT Datum _pgr_dagshortestpath_v4(PG_FUNCTION_ARGS) {
8740
FuncCallContext *funcctx;
8841
TupleDesc tuple_desc;
@@ -99,26 +52,48 @@ PGDLLEXPORT Datum _pgr_dagshortestpath_v4(PG_FUNCTION_ARGS) {
9952
/*
10053
* many to many
10154
*/
102-
process(
55+
pgr_process_shortestPath(
10356
text_to_cstring(PG_GETARG_TEXT_P(0)),
10457
NULL,
58+
NULL,
59+
10560
PG_GETARG_ARRAYTYPE_P(1),
10661
PG_GETARG_ARRAYTYPE_P(2),
62+
63+
true,
10764
PG_GETARG_BOOL(3),
10865
PG_GETARG_BOOL(4),
66+
67+
0,
68+
true,
69+
' ',
70+
true,
71+
72+
DAGSP,
10973
&result_tuples,
11074
&result_count);
11175

11276
} else if (PG_NARGS() == 3) {
11377
/*
11478
* combinations
11579
*/
116-
process(
80+
pgr_process_shortestPath(
11781
text_to_cstring(PG_GETARG_TEXT_P(0)),
82+
NULL,
11883
text_to_cstring(PG_GETARG_TEXT_P(1)),
84+
11985
NULL, NULL,
86+
87+
true,
12088
PG_GETARG_BOOL(2),
12189
true,
90+
91+
0,
92+
true,
93+
' ',
94+
true,
95+
96+
DAGSP,
12297
&result_tuples,
12398
&result_count);
12499
}
@@ -129,7 +104,8 @@ PGDLLEXPORT Datum _pgr_dagshortestpath_v4(PG_FUNCTION_ARGS) {
129104
!= TYPEFUNC_COMPOSITE) {
130105
ereport(ERROR,
131106
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
132-
errmsg("function returning record called in context that cannot accept type record")));
107+
errmsg("function returning record called in context "
108+
"that cannot accept type record")));
133109
}
134110

135111
funcctx->tuple_desc = tuple_desc;

src/dijkstra/shortestPath_driver.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
5454
#include "bellman_ford/edwardMoore.hpp"
5555
#include "bdDijkstra/bdDijkstra.hpp"
5656
#include "withPoints/withPoints.hpp"
57+
#include "dagShortestPath/dagShortestPath.hpp"
5758

5859
namespace {
5960

@@ -153,6 +154,7 @@ do_shortestPath(
153154
using pgrouting::algorithms::dijkstra;
154155
using pgrouting::algorithms::bdDijkstra;
155156
using pgrouting::algorithms::edwardMoore;
157+
using pgrouting::algorithms::dagShortestPath;
156158

157159
hint = combinations_sql;
158160
auto combinations = get_combinations(combinations_sql, starts, ends, normal, is_matrix);
@@ -238,6 +240,9 @@ do_shortestPath(
238240
case EDWARDMOORE:
239241
paths = edwardMoore(digraph, combinations);
240242
break;
243+
case DAGSP:
244+
paths = dagShortestPath(digraph, combinations, only_cost);
245+
break;
241246
default:
242247
err << "INTERNAL: wrong function call: " << which;
243248
return;
@@ -255,8 +260,8 @@ do_shortestPath(
255260
paths = bdDijkstra(undigraph, combinations, only_cost);
256261
break;
257262
case EDWARDMOORE:
258-
paths = edwardMoore(undigraph, combinations);
259-
break;
263+
paths = edwardMoore(undigraph, combinations);
264+
break;
260265
default:
261266
err << "INTERNAL: wrong function call: " << which;
262267
return;

0 commit comments

Comments
 (0)