Skip to content

Commit 7bba222

Browse files
authored
Merge pull request #466 from bipashabg/week-6-sloanordering
Week 6 sloan ordering
2 parents 8d19133 + dba0a26 commit 7bba222

File tree

7 files changed

+65
-69
lines changed

7 files changed

+65
-69
lines changed

docqueries/ordering/sloanOrdering.pg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
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'
5+
'SELECT id, source, target, cost, reverse_cost FROM edges WHERE id < 2'
66
);
77
/* -- q2 */
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
BEGIN;
2+
BEGIN
3+
SET client_min_messages TO NOTICE;
4+
SET
5+
/* -- q1 */
6+
SELECT * FROM pgr_sloanOrdering(
7+
'SELECT id, source, target, cost, reverse_cost FROM edges WHERE id < 2'
8+
);
9+
seq | node
10+
-----+------
11+
1 | 1
12+
(1 row)
13+
14+
/* -- q2 */
15+
ROLLBACK;
16+
ROLLBACK

docqueries/ordering/test.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
%main::tests = (
44
'any' => {
55
'files' => [qw(
6+
sloanOrdering.pg
67
cuthillMckeeOrdering.pg
78
topologicalSort.pg
89
sloanOrdering.pg

include/ordering/sloanOrdering.hpp

Lines changed: 39 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/*PGR-GNU*****************************************************************
2-
File: ordering.hpp
2+
File: sloanOrdering.hpp
33
44
Generated with Template by:
5-
Copyright (c) 2015 pgRouting developers
5+
Copyright (c) 2022 pgRouting developers
66
Mail: project@pgrouting.org
77
88
Developer:
@@ -25,70 +25,63 @@ You should have received a copy of the GNU General Public License
2525
along with this program; if not, write to the Free Software
2626
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
2727
28-
********************************************************************PGR-GNU*/
28+
********************************************************************PGR-GNU*/
2929

30-
#ifndef INCLUDE_ORDERING_ORDERING_HPP_
31-
#define INCLUDE_ORDERING_ORDERING_HPP_
30+
#ifndef INCLUDE_ORDERING_SLOANORDERING_HPP_
31+
#define INCLUDE_ORDERING_SLOANORDERING_HPP_
3232
#pragma once
3333

34+
#include <algorithm>
3435
#include <vector>
35-
#include <limits>
36+
#include <map>
37+
#include <cstdint>
3638
#include <iterator>
37-
#include <utility>
3839

39-
#include <boost/config.hpp>
40-
#include <boost/graph/adjacency_list.hpp>
4140
#include <boost/property_map/property_map.hpp>
41+
#include <boost/graph/graph_traits.hpp>
42+
#include <boost/property_map/vector_property_map.hpp>
43+
#include <boost/type_traits.hpp>
44+
#include <boost/graph/adjacency_list.hpp>
4245
#include <boost/graph/sloan_ordering.hpp>
4346

4447
#include "cpp_common/base_graph.hpp"
4548
#include "cpp_common/interruption.hpp"
49+
#include "cpp_common/messages.hpp"
4650

47-
48-
namespace pgrouting {
49-
namespace detail {
50-
51-
template <typename T>
52-
struct inf_plus {
53-
T operator()(const T& a, const T& b) const {
54-
T inf = (std::numeric_limits<T>::max)();
55-
if (a == inf || b == inf) return inf;
56-
return a + b;
57-
}
58-
};
59-
60-
} // namespace detail
61-
51+
namespace pgrouting {
6252

6353
template <class G>
64-
std::vector<std::vector<int64_t>>
65-
sloan(G &graph) {
66-
CHECK_FOR_INTERRUPTS();
67-
68-
std::pair<typename G::V, typename G::V> starting_nodes = boost::sloan_starting_nodes(graph.graph);
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;
6965

70-
std::vector<typename G::V> inv_perm(graph.num_vertices());
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);
7170

72-
boost::sloan_ordering(
73-
graph.graph,
74-
inv_perm.begin(),
75-
boost::get(boost::vertex_color_t(), graph.graph),
76-
boost::make_degree_map(graph.graph),
77-
starting_nodes.first,
78-
starting_nodes.second);
71+
std::vector<Vertex> inv_perm(boost::num_vertices(graph.graph));
7972

80-
CHECK_FOR_INTERRUPTS();
73+
CHECK_FOR_INTERRUPTS();
8174

82-
std::vector<int64_t> result;
83-
result.reserve(inv_perm.size());
75+
boost::sloan_ordering(graph.graph, inv_perm.rbegin(), color_map, degree_map, priority_map, i_map);
8476

85-
for (const auto& vertex_desc : inv_perm) {
86-
result.push_back(graph[vertex_desc].id);
87-
}
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++;}
8881

89-
return result;
90-
} // namespace ordering
82+
return results;
83+
}
9184

9285
} // namespace pgrouting
9386

94-
#endif // INCLUDE_ORDERING_ORDERING_HPP_
87+
#endif // INCLUDE_ORDERING_SLOANORDERING_HPP_

sql/ordering/sloanOrdering.sql

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
2727
2828
********************************************************************PGR-GNU*/
2929

30-
----------------------------
31-
-- pgr_sloanOrdering
32-
----------------------------
33-
3430
--v4.0.0
3531
CREATE FUNCTION pgr_sloanOrdering(
3632
TEXT, -- edges_sql (required)

src/ordering/ordering_driver.cpp

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,8 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
3838
#include "cpp_common/alloc.hpp"
3939
#include "cpp_common/assert.hpp"
4040

41-
#if 0
42-
#include "ordering/ordering.hpp"
43-
#endif
41+
#include "ordering/sloanOrdering.hpp"
42+
4443

4544

4645
void
@@ -90,28 +89,13 @@ do_ordering(
9089
pgrouting::UndirectedGraph undigraph;
9190
undigraph.insert_edges(edges);
9291

93-
if (start_vid != 0 && !undigraph.has_vertex(start_vid)) {
94-
err << "Start vertex" << start_vid << "not found in graph";
95-
*err_msg = to_pg_msg(err);
96-
*log_msg = to_pg_msg(log);
97-
return;
98-
}
99-
100-
if (end_vid != 0 && !undigraph.has_vertex(end_vid)) {
101-
err << "End vertex" << end_vid << "not found in graph";
102-
*err_msg = to_pg_msg(err);
103-
*log_msg = to_pg_msg(log);
104-
return;
105-
}
106-
10792
std::vector<II_t_rt> results;
10893

10994
if (which == 0) {
11095
results = sloan(undigraph);
11196
}
11297

11398

114-
11599
auto count = results.size();
116100

117101
if (count == 0) {

src/ordering/sloanOrdering.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ _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+
6167
funcctx->max_calls = result_count;
6268
funcctx->user_fctx = result_tuples;
6369
if (get_call_result_type(fcinfo, NULL, &tuple_desc)

0 commit comments

Comments
 (0)