|
| 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_ |
0 commit comments