Skip to content

Commit 02a8c9c

Browse files
committed
(astar) using the process and driver
1 parent 8a370d5 commit 02a8c9c

File tree

10 files changed

+67
-90
lines changed

10 files changed

+67
-90
lines changed

include/c_common/enums.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ enum Which {
3636
/** allpairs **/
3737
FLOYD = 31, JOHNSON,
3838
/** metrics **/
39-
BANDWIDTH
39+
BANDWIDTH,
40+
/* with edges that have x y*/
41+
ASTAR= 400, BDASTAR
4042
};
4143

4244
#endif // INCLUDE_C_COMMON_ENUMS_H_

include/cpp_common/utilities.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
3333
namespace pgrouting {
3434

3535
std::string get_name(Which);
36+
std::string get_name(Which, bool, bool);
3637
std::string get_name(Which, bool, bool, bool);
3738
char estimate_drivingSide(char, Which);
3839
void get_new_queries(const std::string&, const std::string&, std::string&, std::string&);

pgtap/astar/aStarCostMatrix/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(reverse_cost);
9-
SELECT plan(15);
9+
SELECT plan(16);
1010

1111
PREPARE edges_q AS
1212
SELECT id, source, target, cost, reverse_cost, x1, y1, x2, y2 FROM edges;
@@ -49,6 +49,7 @@ LANGUAGE plpgsql VOLATILE;
4949

5050

5151
SELECT * FROM test_function();
52+
SELECT throw_on_empty_edges_sql('pgr_astarCostMatrix', ',ARRAY[5,6]');
5253

5354
SELECT finish();
5455
ROLLBACK;

pgtap/astar/astar/no_crash_test.pg

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@ BEGIN;
77

88

99
UPDATE edges SET cost = sign(cost), reverse_cost = sign(reverse_cost);
10-
SELECT CASE WHEN min_version('3.2.0') THEN plan(97) ELSE plan(84) END;
10+
SELECT CASE WHEN min_version('3.2.0') THEN plan(98) ELSE plan(85) END;
1111

1212
SELECT general_no_crash('pgr_aStar');
13+
SELECT throw_on_empty_edges_sql('pgr_aStar', ',1,2');
14+
1315
SELECT finish();
16+
1417
ROLLBACK;

pgtap/astar/astarCost/no_crash_test.pg

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ BEGIN;
77

88

99
UPDATE edges SET cost = sign(cost), reverse_cost = sign(reverse_cost);
10-
SELECT CASE WHEN min_version('3.2.0') THEN plan(97) ELSE plan(84) END;
10+
SELECT CASE WHEN min_version('3.2.0') THEN plan(98) ELSE plan(85) END;
1111

1212

13-
SELECT general_no_crash('pgr_aStar');
13+
SELECT general_no_crash('pgr_aStarCost');
14+
SELECT throw_on_empty_edges_sql('pgr_aStarCost', ',1,2');
15+
1416
SELECT finish();
1517
ROLLBACK;
16-

sql/astar/astarCostMatrix.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ CREATE FUNCTION pgr_aStarCostMatrix(
4949
RETURNS SETOF RECORD AS
5050
$BODY$
5151
SELECT a.start_vid, a.end_vid, a.agg_cost
52-
FROM _pgr_astar(_pgr_get_statement($1), $2, $2, $3, $4, $5::FLOAT, $6::FLOAT, true) a;
52+
FROM _pgr_astar(_pgr_get_statement($1), $2::BIGINT[], '{}'::BIGINT[], $3, $4, $5::FLOAT, $6::FLOAT, true) a;
5353
$BODY$
5454
LANGUAGE SQL VOLATILE STRICT
5555
COST ${COST_HIGH} ROWS ${ROWS_HIGH};

src/astar/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
# This file is part of the pgRouting project.
22
# Copyright (c) 2007-2026 pgRouting developers
33
# License: GPL-2 See https://github.com/pgRouting/pgrouting/blob/main/LICENSE
4+
45
ADD_LIBRARY(astar OBJECT
56
astar.c
67

78
astar_driver.cpp
8-
)
9+
astar_process.cpp
10+
)

src/astar/astar.c

Lines changed: 21 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -31,75 +31,15 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
3131
#include "c_common/postgres_connection.h"
3232

3333
#include "c_types/path_rt.h"
34-
#include "c_common/debug_macro.h"
35-
#include "c_common/e_report.h"
36-
#include "c_common/time_msg.h"
37-
#include "c_common/check_parameters.h"
38-
#include "drivers/astar/astar_driver.h"
34+
#include "process/astar_process.h"
3935

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

43-
static
44-
void
45-
process(char* edges_sql,
46-
char* combinations_sql,
47-
ArrayType *starts,
48-
ArrayType *ends,
49-
bool directed,
50-
int heuristic,
51-
double factor,
52-
double epsilon,
53-
bool only_cost,
54-
bool normal,
55-
Path_rt **result_tuples,
56-
size_t *result_count) {
57-
check_parameters(heuristic, factor, epsilon);
58-
59-
pgr_SPI_connect();
60-
char* log_msg = NULL;
61-
char* notice_msg = NULL;
62-
char* err_msg = NULL;
63-
64-
clock_t start_t = clock();
65-
pgr_do_astar(
66-
edges_sql,
67-
combinations_sql,
68-
starts,
69-
ends,
70-
71-
directed,
72-
heuristic,
73-
factor,
74-
epsilon,
75-
only_cost,
76-
normal,
77-
result_tuples, result_count,
78-
&log_msg,
79-
&notice_msg,
80-
&err_msg);
81-
82-
if (only_cost) {
83-
time_msg("processing pgr_astarCost", start_t, clock());
84-
} else {
85-
time_msg("processing pgr_astar", start_t, clock());
86-
}
87-
88-
if (err_msg && (*result_tuples)) {
89-
pfree(*result_tuples);
90-
(*result_tuples) = NULL;
91-
(*result_count) = 0;
92-
}
9339

94-
pgr_global_report(&log_msg, &notice_msg, &err_msg);
95-
96-
pgr_SPI_finish();
97-
}
98-
99-
PGDLLEXPORT Datum
100-
_pgr_astar(PG_FUNCTION_ARGS) {
40+
PGDLLEXPORT Datum _pgr_astar(PG_FUNCTION_ARGS) {
10141
FuncCallContext *funcctx;
102-
TupleDesc tuple_desc;
42+
TupleDesc tuple_desc;
10343

10444
Path_rt *result_tuples = NULL;
10545
size_t result_count = 0;
@@ -108,53 +48,59 @@ _pgr_astar(PG_FUNCTION_ARGS) {
10848
MemoryContext oldcontext;
10949
funcctx = SRF_FIRSTCALL_INIT();
11050
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
111-
112-
11351
if (PG_NARGS() == 9) {
11452
/*
11553
* many to many
11654
*/
117-
process(
55+
pgr_process_astar(
11856
text_to_cstring(PG_GETARG_TEXT_P(0)),
11957
NULL,
12058
PG_GETARG_ARRAYTYPE_P(1),
12159
PG_GETARG_ARRAYTYPE_P(2),
60+
12261
PG_GETARG_BOOL(3),
62+
PG_GETARG_BOOL(7),
63+
PG_GETARG_BOOL(8),
64+
12365
PG_GETARG_INT32(4),
12466
PG_GETARG_FLOAT8(5),
12567
PG_GETARG_FLOAT8(6),
126-
PG_GETARG_BOOL(7),
127-
PG_GETARG_BOOL(8),
68+
69+
ASTAR,
12870
&result_tuples,
12971
&result_count);
13072
} else if (PG_NARGS() == 7) {
13173
/*
13274
* Combinations
13375
*/
134-
process(
76+
pgr_process_astar(
13577
text_to_cstring(PG_GETARG_TEXT_P(0)),
13678
text_to_cstring(PG_GETARG_TEXT_P(1)),
137-
NULL,
138-
NULL,
79+
80+
NULL, NULL,
81+
13982
PG_GETARG_BOOL(2),
83+
PG_GETARG_BOOL(6),
84+
true,
85+
14086
PG_GETARG_INT32(3),
14187
PG_GETARG_FLOAT8(4),
14288
PG_GETARG_FLOAT8(5),
143-
PG_GETARG_BOOL(6),
144-
true,
89+
90+
ASTAR,
14591
&result_tuples,
14692
&result_count);
14793
}
14894

149-
15095
funcctx->max_calls = result_count;
15196
funcctx->user_fctx = result_tuples;
15297
if (get_call_result_type(fcinfo, NULL, &tuple_desc)
153-
!= TYPEFUNC_COMPOSITE)
98+
!= TYPEFUNC_COMPOSITE) {
15499
ereport(ERROR,
155100
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
156101
errmsg("function returning record called in context "
157102
"that cannot accept type record")));
103+
}
158104

159105
funcctx->tuple_desc = tuple_desc;
160106
MemoryContextSwitchTo(oldcontext);
@@ -171,7 +117,6 @@ _pgr_astar(PG_FUNCTION_ARGS) {
171117
bool* nulls;
172118
size_t call_cntr = funcctx->call_cntr;
173119

174-
175120
size_t numb = 8;
176121
values = palloc(numb * sizeof(Datum));
177122
nulls = palloc(numb * sizeof(bool));

src/cpp_common/combinations.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,17 +158,17 @@ get_combinations(
158158
using pgrouting::pgget::get_combinations;
159159
std::map<int64_t, std::set<int64_t>> result;
160160

161-
auto starts = normal? get_intSet(startsArr) : get_intSet(endsArr);
162-
auto ends = endsArr? (normal? get_intSet(endsArr) : get_intSet(startsArr)) : std::set<int64_t>();
161+
auto starts = startsArr? (normal? get_intSet(startsArr) : get_intSet(endsArr)) : std::set<int64_t>();
162+
auto ends = endsArr? (normal? get_intSet(endsArr) : get_intSet(startsArr)) : std::set<int64_t>();
163163

164164
/* TODO Read query storing like std::map<int64_t , std::set<int64_t>> */
165165
/* queries are stored in vectors */
166166
auto combinations = !combinations_sql.empty()? get_combinations(combinations_sql) : std::vector<II_t_rt>();
167167

168-
ends = (!starts.empty() && ends.empty()) ? starts : ends;
169-
170168
/* data comes from CostMatrix */
171-
is_matrix = combinations.empty() && starts == ends;
169+
is_matrix = combinations.empty() && !starts.empty() && ends.empty();
170+
171+
ends = is_matrix? starts : ends;
172172

173173
/* data comes from a combinations */
174174
for (const auto &row : combinations) {

src/cpp_common/utilities.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ get_name(Which which, bool is_only_cost, bool is_near, bool is_matrix) {
6969
switch (which) {
7070
case DIJKSTRA :
7171
base = "pgr_dijkstra";
72-
suffix = std::string(is_near? "Near" : "") + (is_only_cost? "Cost" : "") + (is_matrix? "Matrix" : "");
72+
suffix = std::string(is_near? "Near" : "") + (is_only_cost? "Cost" : "") + (is_only_cost && is_matrix? "Matrix" : "");
7373
break;
7474
case BDDIJKSTRA :
7575
base = "pgr_bdDijkstra";
@@ -92,13 +92,35 @@ get_name(Which which, bool is_only_cost, bool is_near, bool is_matrix) {
9292
return base + suffix;
9393
}
9494

95+
std::string
96+
get_name(Which which, bool is_only_cost, bool is_matrix) {
97+
std::string base = "";
98+
std::string suffix = "";
99+
100+
switch (which) {
101+
case ASTAR :
102+
base = "pgr_astar";
103+
suffix = std::string(is_only_cost? "Cost" : "") + (is_only_cost && is_matrix? "Matrix" : "");
104+
break;
105+
case BDASTAR :
106+
base = "pgr_bdAstar";
107+
suffix = std::string(is_only_cost? "Cost" : "") + (is_matrix? "Matrix" : "");
108+
break;
109+
default : base = "unknown";
110+
}
111+
112+
return base + suffix;
113+
}
114+
95115
char
96116
estimate_drivingSide(char driving_side, Which which) {
97117
char d_side = static_cast<char>(tolower(driving_side));
98118
if (!((d_side == 'r') || (d_side == 'l') || (d_side == 'b'))) {
99119
d_side = ' ';
100120
}
101121
switch (which) {
122+
case ASTAR :
123+
case BDASTAR :
102124
case DAGSP :
103125
case EDWARDMOORE:
104126
case BDDIJKSTRA:

0 commit comments

Comments
 (0)