Skip to content

Commit 5dfcbe4

Browse files
authored
Merge pull request #446 from bipashabg/week-2-sloanordering
Week 2 sloanordering
2 parents ac5cc88 + 75a7aa7 commit 5dfcbe4

File tree

9 files changed

+1268
-1110
lines changed

9 files changed

+1268
-1110
lines changed
Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
/*PGR-GNU*****************************************************************
2-
File: sloanOrdering_driver.hpp
2+
File: ordering_driver.hpp
33
44
Generated with Template by:
55
Copyright (c) 2025 pgRouting developers
66
77
8-
Function's developer:
8+
Developer:
99
Copyright (c) 2025 Bipasha Gayary
10-
Mail: bipashagayary@gmail.com
10+
Mail: bipashagayary at gmail.com
1111
1212
------
1313
@@ -27,37 +27,23 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
2727
2828
********************************************************************PGR-GNU*/
2929

30-
#ifndef INCLUDE_DRIVERS_ORDERING_SLOANORDERING_DRIVER_HPP_
31-
#define INCLUDE_DRIVERS_ORDERING_SLOANORDERING_DRIVER_HPP_
30+
#ifndef INCLUDE_DRIVERS_ORDERING_DRIVER_HPP_
31+
#define INCLUDE_DRIVERS_ORDERING_DRIVER_HPP_
32+
#pragma once
3233

33-
34-
#ifdef __cplusplus
35-
# include <cstddef>
36-
# include <cstdint>
34+
#include <cstddef>
35+
#include <cstddef>
3736
using Edge_t = struct Edge_t;
3837
using II_t_rt = struct II_t_rt;
39-
#else
40-
# include <stddef.h>
41-
# include <stdint.h>
42-
typedef struct Edge_t Edge_t;
43-
typedef struct II_t_rt II_t_rt;
44-
#endif
45-
38+
#include <string>
4639

47-
#ifdef __cplusplus
48-
extern "C" {
49-
#endif
5040

5141
void
52-
pgr_do_sloanOrdering(
53-
const char*, int64_t, int64_t,
42+
do_ordering(
43+
std::string, int64_t, int64_t,
5444

5545
II_t_rt**, size_t*,
5646
char **, char **, char **);
5747

5848

59-
#ifdef __cplusplus
60-
}
61-
#endif
62-
63-
#endif // INCLUDE_DRIVERS_ORDERING_SLOANORDERING_DRIVER_HPP_
49+
#endif // INCLUDE_DRIVERS_ORDERING_DRIVER_HPP_

include/ordering/sloanOrdering.hpp

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
/*PGR-GNU*****************************************************************
2+
File: sloanOrdering.hpp
3+
4+
Generated with Template by:
5+
Copyright (c) 2025 pgRouting developers
6+
7+
8+
Function's developer:
9+
Copyright (c) 2025 Bipasha Gayary
10+
Mail: bipashagayary at gmail.com
11+
12+
------
13+
14+
This program is free software; you can redistribute it and/or modify
15+
it under the terms of the GNU General Public License as published by
16+
the Free Software Foundation; either version 2 of the License, or
17+
(at your option) any later version.
18+
19+
This program is distributed in the hope that it will be useful,
20+
but WITHOUT ANY WARRANTY; without even the implied warranty of
21+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22+
GNU General Public License for more details.
23+
24+
You should have received a copy of the GNU General Public License
25+
along with this program; if not, write to the Free Software
26+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27+
28+
********************************************************************PGR-GNU*/
29+
30+
#ifndef INCLUDE_ORDERING_SLOANORDERING_HPP_
31+
#define INCLUDE_ORDERING_SLOANORDERING_HPP_
32+
#pragma once
33+
34+
#include <algorithm>
35+
#include <vector>
36+
#include <map>
37+
#include <cstdint>
38+
39+
#include <boost/property_map/property_map.hpp>
40+
#include <boost/graph/graph_traits.hpp>
41+
#include <boost/property_map/vector_property_map.hpp>
42+
#include <boost/type_traits.hpp>
43+
#include <boost/graph/adjacency_list.hpp>
44+
#include <boost/graph/sloan_ordering.hpp>
45+
46+
#include "cpp_common/base_graph.hpp"
47+
#include "cpp_common/interruption.hpp"
48+
#include "cpp_common/messages.hpp"
49+
50+
#include "c_types/ii_t_rt.h"
51+
52+
/** @file sloanOrdering.hpp
53+
* @brief The main file which calls the respective boost function.
54+
*
55+
* Contains actual implementation of the function and the calling
56+
* of the respective boost function.
57+
*/
58+
59+
60+
namespace pgrouting {
61+
namespace functions {
62+
63+
template <class G>
64+
class SloanOrdering : public Pgr_messages {
65+
public:
66+
typedef typename G::V V;
67+
typedef typename G::E E;
68+
typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS> Graph;
69+
typedef boost::graph_traits<Graph>::vertices_size_type size_type;
70+
typedef boost::graph_traits<Graph>::vertex_descriptor Vertex;
71+
72+
/** @name SloanOrdering
73+
* @{
74+
*
75+
*/
76+
77+
/** @brief sloanOrdering function
78+
*
79+
* It does all the processing and returns the results.
80+
*
81+
* @param graph the graph containing the edges
82+
* @param start_vertex starting vertex id for ordering
83+
* @param end_vertex end vertex for ordering
84+
*
85+
* @returns results, when results are found
86+
*
87+
* @see [boost::sloan_ordering]
88+
* (https://www.boost.org/libs/graph/doc/sloan_ordering.html)
89+
*/
90+
91+
std::vector<II_t_rt>
92+
sloanOrdering(G &graph, int64_t start_vertex, int64_t end_vertex) {
93+
std::vector<II_t_rt> results;
94+
95+
auto start_v = graph.get_V(start_vertex);
96+
auto end_v = graph.get_V(end_vertex);
97+
98+
if (start_v == boost::graph_traits<Graph>::null_vertex() ||
99+
end_v == boost::graph_traits<Graph>::null_vertex()) {
100+
throw std::invalid_argument("Start or end vertex not found in graph");
101+
}
102+
103+
// map which store the indices with their nodes.
104+
auto i_map = boost::get(boost::vertex_index, graph.graph);
105+
106+
// vector which will store the order of the indices.
107+
std::vector<Vertex> inv_perm(boost::num_vertices(graph.graph));
108+
109+
// vector which will store the color of all the vertices in the graph
110+
std::vector <boost::default_color_type> colors(boost::num_vertices(graph.graph));
111+
112+
// An iterator property map which records the color of each vertex
113+
auto color_map = boost::make_iterator_property_map(&colors[0], i_map, colors[0]);
114+
115+
// map which store the degree of each vertex.
116+
auto out_deg = boost::make_out_degree_map(graph.graph);
117+
118+
std::vector<int> priorities(boost::num_vertices(graph.graph));
119+
auto priority_map = boost::make_iterator_property_map(&priorities[0], i_map, priorities[0]);
120+
121+
122+
/* abort in case of an interruption occurs (e.g. the query is being cancelled) */
123+
CHECK_FOR_INTERRUPTS();
124+
125+
try {
126+
// default weights
127+
128+
boost::sloan_ordering(graph.graph, inv_perm.rbegin(), color_map, out_deg, priority_map, start_v, end_v);
129+
} catch (boost::exception const& ex) {
130+
(void)ex;
131+
throw;
132+
} catch (std::exception &e) {
133+
(void)e;
134+
throw;
135+
} catch (...) {
136+
throw;
137+
}
138+
139+
results = get_results(inv_perm, graph);
140+
141+
return results;
142+
}
143+
144+
//@}
145+
146+
private:
147+
/** @brief to get the results
148+
*
149+
* Uses the `inv_perm` vector to get the results i.e. the ordering.
150+
*
151+
* @param inv_perm vector which contains the new ordering of indices.
152+
* @param graph the graph containing the edges
153+
*
154+
* @returns `results` vector
155+
*/
156+
std::vector <II_t_rt> get_results(
157+
std::vector <size_type> & inv_perm,
158+
const G &graph) {
159+
std::vector <II_t_rt> results;
160+
161+
for (std::vector<Vertex>::const_iterator i = inv_perm.begin();
162+
i != inv_perm.end(); ++i) {
163+
log << inv_perm[*i] << " ";
164+
auto seq = graph[*i].id;
165+
results.push_back({{seq}, {static_cast<int64_t>(graph.graph[*i].id)}});
166+
seq++;
167+
}
168+
169+
return results;
170+
}
171+
};
172+
} // namespace functions
173+
} // namespace pgrouting
174+
175+
#endif // INCLUDE_ORDERING_SLOANORDERING_HPP_
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*PGR-GNU*****************************************************************
2-
File: sloan_ordering_process.h
2+
File: ordering_process.h
33
44
Function's developer:
55
Copyright (c) 2025 Bipasha Gayary
@@ -23,8 +23,8 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
2323
2424
********************************************************************PGR-GNU*/
2525

26-
#ifndef INCLUDE_PROCESS_SLOAN_ORDERING_PROCESS_H_
27-
#define INCLUDE_PROCESS_SLOAN_ORDERING_PROCESS_H_
26+
#ifndef INCLUDE_PROCESS_ORDERING_PROCESS_H_
27+
#define INCLUDE_PROCESS_ORDERING_PROCESS_H_
2828
#pragma once
2929

3030
#ifdef __cplusplus
@@ -44,10 +44,10 @@ typedef struct II_t_rt II_t_rt;
4444
extern "C" {
4545
#endif
4646

47-
void pgr_process_ordering(const Edge_t*, size_t, int64_t, int64_t, bool, II_t_rt **, size_t *);
47+
void pgr_process_ordering(const Edge_t*, size_t, int64_t, int64_t, II_t_rt **, size_t *);
4848

4949
#ifdef __cplusplus
5050
}
5151
#endif
5252

53-
#endif // INCLUDE_PROCESS_SLOAN_ORDERING_PROCESS_H_
53+
#endif // INCLUDE_PROCESS_ORDERING_PROCESS_H_

0 commit comments

Comments
 (0)