Skip to content

Commit a6b424e

Browse files
authored
Merge pull request pgRouting#469 from bipashabg/week-7-sloanordering
Week 7 sloanordering
2 parents 7bba222 + 0f19b29 commit a6b424e

File tree

5 files changed

+133
-53
lines changed

5 files changed

+133
-53
lines changed

docqueries/ordering/sloanOrdering.pg

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,30 @@
22
-- Creative Commons Attribution-Share Alike 3.0 License : https://creativecommons.org/licenses/by-sa/3.0/
33
/* -- q1 */
44
SELECT * FROM pgr_sloanOrdering(
5-
'SELECT id, source, target, cost, reverse_cost FROM edges WHERE id < 2'
5+
'SELECT id, source, target, cost, reverse_cost FROM edges'
66
);
77
/* -- q2 */
8+
9+
CREATE TABLE expected_result (
10+
seq BIGINT,
11+
node BIGINT);
12+
13+
INSERT INTO expected_result (seq, node) VALUES
14+
(1,13),
15+
(2,14),
16+
(3,2),
17+
(4,4),
18+
(5,9),
19+
(6,1),
20+
(7,8),
21+
(8,3),
22+
(9,12),
23+
(10,7),
24+
(11,5),
25+
(12,17),
26+
(13,11),
27+
(14,6),
28+
(15,16),
29+
(16,10),
30+
(17,15);
31+

docqueries/ordering/sloanOrdering.result

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,52 @@ SET client_min_messages TO NOTICE;
44
SET
55
/* -- q1 */
66
SELECT * FROM pgr_sloanOrdering(
7-
'SELECT id, source, target, cost, reverse_cost FROM edges WHERE id < 2'
7+
'SELECT id, source, target, cost, reverse_cost FROM edges'
88
);
99
seq | node
1010
-----+------
1111
1 | 1
12-
(1 row)
12+
2 | 3
13+
3 | 6
14+
4 | 5
15+
5 | 10
16+
6 | 7
17+
7 | 9
18+
8 | 15
19+
9 | 8
20+
10 | 11
21+
11 | 16
22+
12 | 12
23+
13 | 17
24+
14 | 1
25+
15 | 1
26+
16 | 1
27+
17 | 1
28+
(17 rows)
1329

1430
/* -- q2 */
31+
CREATE TABLE expected_result (
32+
seq BIGINT,
33+
node BIGINT);
34+
CREATE TABLE
35+
INSERT INTO expected_result (seq, node) VALUES
36+
(1,13),
37+
(2,14),
38+
(3,2),
39+
(4,4),
40+
(5,9),
41+
(6,1),
42+
(7,8),
43+
(8,3),
44+
(9,12),
45+
(10,7),
46+
(11,5),
47+
(12,17),
48+
(13,11),
49+
(14,6),
50+
(15,16),
51+
(16,10),
52+
(17,15);
53+
INSERT 0 17
1554
ROLLBACK;
1655
ROLLBACK

include/ordering/sloanOrdering.hpp

Lines changed: 55 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
3535
#include <vector>
3636
#include <map>
3737
#include <cstdint>
38-
#include <iterator>
3938

4039
#include <boost/property_map/property_map.hpp>
4140
#include <boost/graph/graph_traits.hpp>
@@ -49,39 +48,63 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
4948
#include "cpp_common/messages.hpp"
5049

5150
namespace pgrouting {
51+
namespace functions {
5252

5353
template <class G>
54-
std::vector<int64_t>
55-
sloanOrdering(G &graph) {
56-
typedef boost::adjacency_list<
57-
boost::vecS,
58-
boost::vecS,
59-
boost::undirectedS,
60-
boost::property<boost::vertex_color_t, boost::default_color_type,
61-
boost::property<boost::vertex_degree_t, int,
62-
boost::property<boost::vertex_priority_t, int>>>> GraphType;
63-
typedef typename boost::graph_traits<typename G::graph_t>::vertex_descriptor Vertex;
64-
std::vector<int64_t>results;
65-
66-
auto i_map = boost::get(boost::vertex_index, graph.graph);
67-
auto color_map = boost::get(boost::vertex_color, graph.graph);
68-
auto degree_map = boost::make_degree_map(graph.graph);
69-
auto priority_map = boost::get(boost::vertex_priority, graph.graph);
70-
71-
std::vector<Vertex> inv_perm(boost::num_vertices(graph.graph));
72-
73-
CHECK_FOR_INTERRUPTS();
74-
75-
boost::sloan_ordering(graph.graph, inv_perm.rbegin(), color_map, degree_map, priority_map, i_map);
76-
77-
for (typename std::vector<Vertex>::const_iterator i = inv_perm.begin(); i != inv_perm.end(); ++i) {
78-
auto seq = graph[*i].id;
79-
results.push_back(static_cast<int64_t>(graph[*i].id));
80-
seq++;}
81-
82-
return results;
83-
}
84-
54+
class SloanOrdering : public Pgr_messages {
55+
public:
56+
typedef typename G::V V;
57+
typedef typename G::E E;
58+
typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS> Graph;
59+
typedef boost::graph_traits<Graph>::vertices_size_type size_type;
60+
typedef boost::graph_traits<Graph>::vertex_descriptor Vertex;
61+
62+
std::vector<int64_t>
63+
sloanOrdering(G &graph) {
64+
std::vector<int64_t>results;
65+
66+
auto i_map = boost::get(boost::vertex_index, graph.graph);
67+
std::vector<Vertex> inv_perm(boost::num_vertices(graph.graph));
68+
std::vector <boost::default_color_type> colors(boost::num_vertices(graph.graph));
69+
auto color_map = boost::make_iterator_property_map(&colors[0], i_map, colors[0]);
70+
auto out_deg = boost::make_out_degree_map(graph.graph);
71+
std::vector<int> priorities(boost::num_vertices(graph.graph));
72+
auto priority_map = boost::make_iterator_property_map(&priorities[0], i_map, priorities[0]);
73+
74+
CHECK_FOR_INTERRUPTS();
75+
76+
try {
77+
boost::sloan_ordering(graph.graph, inv_perm.begin(), color_map, out_deg, priority_map);
78+
} catch (boost::exception const& ex) {
79+
(void)ex;
80+
throw;
81+
} catch (std::exception &e) {
82+
(void)e;
83+
throw;
84+
} catch (...) {
85+
throw;
86+
}
87+
88+
results = get_results(inv_perm, graph);
89+
90+
return results;
91+
}
92+
93+
private:
94+
std::vector <int64_t> get_results(
95+
std::vector <size_type> & inv_perm,
96+
const G &graph) {
97+
std::vector <int64_t> results;
98+
for (std::vector<Vertex>::const_iterator i = inv_perm.begin();
99+
i != inv_perm.end(); ++i) {
100+
log << inv_perm[*i] << " ";
101+
results.push_back(static_cast<int64_t>(graph.graph[*i].id));
102+
}
103+
104+
return results;
105+
}
106+
};
107+
} // namespace functions
85108
} // namespace pgrouting
86109

87110
#endif // INCLUDE_ORDERING_SLOANORDERING_HPP_

src/ordering/ordering_driver.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,15 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
4040

4141
#include "ordering/sloanOrdering.hpp"
4242

43-
43+
namespace {
44+
template <class G>
45+
std::vector <int64_t>
46+
sloanOrdering(G &graph) {
47+
pgrouting::functions::SloanOrdering <G> fn_sloanOrdering;
48+
auto results = fn_sloanOrdering.sloanOrdering(graph);
49+
return results;
50+
}
51+
}
4452

4553
void
4654
do_ordering(
@@ -70,12 +78,6 @@ do_ordering(
7078
pgassert(!(*return_tuples));
7179
pgassert(*return_count == 0);
7280

73-
#if 0
74-
using pgrouting::sloan;
75-
#endif
76-
77-
#if 0
78-
7981
hint = edges_sql;
8082
auto edges = pgrouting::pgget::get_edges(std::string(edges_sql), true, true);
8183
if (edges.empty()) {
@@ -86,13 +88,12 @@ do_ordering(
8688
log << "Processing Undirected graph\n";
8789

8890
std::vector<int64_t> results;
91+
8992
pgrouting::UndirectedGraph undigraph;
9093
undigraph.insert_edges(edges);
9194

92-
std::vector<II_t_rt> results;
93-
9495
if (which == 0) {
95-
results = sloan(undigraph);
96+
results = sloanOrdering(undigraph);
9697
}
9798

9899

@@ -108,7 +109,7 @@ do_ordering(
108109

109110
(*return_tuples) = pgr_alloc(count, (*return_tuples));
110111

111-
for (size_t i = 0; i < count; ++1) {
112+
for (size_t i = 0; i < count; ++i) {
112113
(*return_tuples)[i] = results[i];
113114
}
114115

@@ -117,7 +118,6 @@ do_ordering(
117118
pgassert(*err_msg == NULL);
118119
*log_msg = to_pg_msg(log);
119120
*notice_msg = to_pg_msg(notice);
120-
#endif
121121
} catch (AssertFailedException &except) {
122122
(*return_tuples) = pgr_free(*return_tuples);
123123
(*return_count) = 0;

src/ordering/sloanOrdering.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,6 @@ _pgr_sloanordering(PG_FUNCTION_ARGS) {
5858
&result_tuples,
5959
&result_count);
6060

61-
if (result_count == 0) {
62-
result_tuples = (int64_t*)palloc(sizeof(int64_t));
63-
result_tuples[0] = 1;
64-
result_count = 1;
65-
}
66-
6761
funcctx->max_calls = result_count;
6862
funcctx->user_fctx = result_tuples;
6963
if (get_call_result_type(fcinfo, NULL, &tuple_desc)

0 commit comments

Comments
 (0)