From 3e28a3a7d542e630ddb233a8531011f89214ad11 Mon Sep 17 00:00:00 2001 From: dmatetelki Date: Thu, 15 May 2014 16:05:08 +0200 Subject: [PATCH] Edge stores value_type not iterator. --- lib/graph/graph.hpp | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/lib/graph/graph.hpp b/lib/graph/graph.hpp index 099e827..6427c51 100644 --- a/lib/graph/graph.hpp +++ b/lib/graph/graph.hpp @@ -63,7 +63,7 @@ private: void addEdge(v_iterator destination, const_weight_reference weight = weight_type()); void removeEdge(v_iterator destination, const_weight_reference weight = weight_type()); void removeAllEdgesTo(v_iterator destination); - std::vector edges(v_const_iterator this_it) const; + std::vector edges() const; value_type m_data; std::list m_edges; @@ -76,15 +76,15 @@ public: public: Edge() : source(), destination(), weight() {} - Edge(vertex_iterator source, vertex_iterator destination, const_weight_reference weight); + Edge(const_reference source, const_reference destination, const_weight_reference weight); Edge(const Edge& o); Edge& operator=(Edge o) { swap(o); return *this; } void swap(Edge& o); bool operator==(const Edge& o) const { return source == o.source && destination == o.destination && weight == o.weight; } - vertex_iterator source; /// @todo shall it be value_type ? - vertex_iterator destination; /// @todo shall it be value_type ? - weight_type weight; + value_type source; + value_type destination; + weight_type weight; }; typedef Edge* edge_pointer; @@ -213,7 +213,7 @@ private: v_iterator find(const_reference data); void adjustEdges(v_iterator vit); - bool m_directed; + const bool m_directed; v_container m_vertices; }; @@ -221,7 +221,7 @@ private: // Edge template -inline Graph::Edge::Edge(vertex_iterator s, vertex_iterator d, const_weight_reference w) +inline Graph::Edge::Edge(const_reference s, const_reference d, const_weight_reference w) : source(s) , destination(d) , weight(w) @@ -296,7 +296,7 @@ inline void Graph::edge_iterator::resetEdge() if (m_vertex_it == m_vertices.end() || (*m_vertex_it).m_edges.empty()) { m_edge = Edge(); } else { - m_edge = Edge( m_vertex_it, (*m_edge_it).m_destination, (*m_edge_it).m_weight); + m_edge = Edge( m_vertex_it->m_data, (m_edge_it->m_destination)->m_data, (*m_edge_it).m_weight); } } @@ -379,14 +379,14 @@ inline void Graph::Vertex::removeAllEdgesTo(v_iterator destination) } template -inline std::vector::Edge> Graph::Vertex::edges(v_const_iterator this_it) const +inline std::vector::Edge> Graph::Vertex::edges() const { std::vector::Edge> retval; /// @todo rewrite for_each to parallel aware std::for_each(m_edges.begin(), m_edges.end(), - [&retval, this_it](const EdgeTo& e) - { retval.push_back(Edge(vertex_iterator(this_it), vertex_iterator(e.m_destination), e.m_weight)); }); + [this, &retval](const EdgeTo& e) + { retval.push_back(Edge(m_data, (e.m_destination)->m_data, e.m_weight)); }); return retval; } @@ -532,11 +532,12 @@ inline std::vector::Edge> Graph::edges() const { std::vector::Edge> retval; - /// @todo rewrite use some STL alg with parallel in mind - for (v_const_iterator it = m_vertices.begin(); it != m_vertices.end(); ++it) { - const std::vector e = (*it).edges(it); - retval.insert(retval.end(), e.begin(), e.end()); - } + /// @todo rewrite for_each to parallel aware + std::for_each(m_vertices.begin(), m_vertices.end(), + [&retval](const Vertex& v) + { const std::vector e = v.edges(); + retval.insert(retval.end(), e.begin(), e.end()); + }); return retval; }