diff --git a/include/coloring/edgeColoring.hpp b/include/coloring/edgeColoring.hpp index aed021ce67..db57893929 100644 --- a/include/coloring/edgeColoring.hpp +++ b/include/coloring/edgeColoring.hpp @@ -30,35 +30,23 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #define INCLUDE_COLORING_EDGECOLORING_HPP_ #pragma once -#include -#include #include #include +#include #include -#include #include +#include "cpp_common/base_graph.hpp" #include "cpp_common/edge_t.hpp" #include "c_types/ii_t_rt.h" -#include "cpp_common/assert.hpp" #include "cpp_common/messages.hpp" namespace pgrouting { namespace functions { class Pgr_edgeColoring : public Pgr_messages { - public: - using EdgeColoring_Graph = - boost::adjacency_list; - - using V = boost::graph_traits::vertex_descriptor; - using E = boost::graph_traits::edge_descriptor; - using V_it = boost::graph_traits::vertex_iterator; - using E_it = boost::graph_traits::edge_iterator; - - public: + public: std::vector edgeColoring(); explicit Pgr_edgeColoring(const std::vector&); @@ -69,16 +57,7 @@ class Pgr_edgeColoring : public Pgr_messages { #endif private: - V get_boost_vertex(int64_t id) const; - int64_t get_vertex_id(V v) const; - int64_t get_edge_id(E e) const; - - - private: - EdgeColoring_Graph graph; - std::map id_to_V; - std::map V_to_id; - std::map E_to_id; + pgrouting::UndirectedGraph graph; }; } // namespace functions diff --git a/src/coloring/edgeColoring.cpp b/src/coloring/edgeColoring.cpp index 02ffa43dd8..aa0c94ff8a 100644 --- a/src/coloring/edgeColoring.cpp +++ b/src/coloring/edgeColoring.cpp @@ -28,14 +28,16 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "coloring/edgeColoring.hpp" +#include + #include #include #include -#include "cpp_common/identifiers.hpp" #include #include +#include "cpp_common/base_graph.hpp" #include "cpp_common/assert.hpp" #include "cpp_common/interruption.hpp" @@ -50,91 +52,31 @@ Pgr_edgeColoring::edgeColoring() { CHECK_FOR_INTERRUPTS(); try { - boost::edge_coloring(graph, boost::get(boost::edge_bundle, graph)); - } catch (...) { - throw std::make_pair( - std::string("INTERNAL: something went wrong while calling boost::edge_coloring"), - std::string(__PGR_PRETTY_FUNCTION__)); - } - - for (auto e_i : boost::make_iterator_range(boost::edges(graph))) { - auto edge = get_edge_id(e_i); - int64_t color = graph[e_i]; - results.push_back({{edge}, {(color + 1)}}); - } - return results; -} - -Pgr_edgeColoring::Pgr_edgeColoring(const std::vector &edges) { - /* - * Inserting vertices - */ - Identifiers ids; - for (const auto &e : edges) { - ids += e.source; - ids += e.target; - } + using E = pgrouting::UndirectedGraph::E; - for (const auto id : ids) { - auto v = add_vertex(graph); - id_to_V.insert(std::make_pair(id, v)); - V_to_id.insert(std::make_pair(v, id)); - } - - /* - * Inserting edges - */ - bool added = false; - for (const auto &edge : edges) { - auto v1 = get_boost_vertex(edge.source); - auto v2 = get_boost_vertex(edge.target); - auto e_exists = boost::edge(v1, v2, graph); - // NOLINTNEXTLINE - if (e_exists.second) continue; + std::map color_storage; + auto color_map = boost::make_assoc_property_map(color_storage); - if (edge.source == edge.target) continue; + boost::edge_coloring(graph.graph, color_map); - if (edge.cost < 0 && edge.reverse_cost < 0) continue; + for (auto e_i : boost::make_iterator_range(boost::edges(graph.graph))) { + int64_t edge_id = graph.graph[e_i].id; + int64_t color = color_map[e_i]; - E e; - // NOLINTNEXTLINE - boost::tie(e, added) = boost::add_edge(v1, v2, graph); - - E_to_id.insert(std::make_pair(e, edge.id)); - } -} - -Pgr_edgeColoring::V -Pgr_edgeColoring::get_boost_vertex(int64_t id) const { - try { - return id_to_V.at(id); + results.push_back({{edge_id}, {(color + 1)}}); + } } catch (...) { - throw std::make_pair( - std::string("INTERNAL: something went wrong when getting the vertex descriptor"), + throw std::make_pair + ( + std::string("INTERNAL: something went wrong while calling boost::edge_coloring"), std::string(__PGR_PRETTY_FUNCTION__)); } -} -int64_t -Pgr_edgeColoring::get_vertex_id(V v) const { - try { - return V_to_id.at(v); - } catch (...) { - throw std::make_pair( - std::string("INTERNAL: something went wrong when getting the vertex id"), - std::string(__PGR_PRETTY_FUNCTION__)); - } + return results; } -int64_t -Pgr_edgeColoring::get_edge_id(E e) const { - try { - return E_to_id.at(e); - } catch (...) { - throw std::make_pair( - std::string("INTERNAL: something went wrong when getting the edge id"), - std::string(__PGR_PRETTY_FUNCTION__)); - } +Pgr_edgeColoring::Pgr_edgeColoring(const std::vector &edges) { + graph.insert_edges(edges); } } // namespace functions