diff --git a/lib/graph/graph.hpp b/lib/graph/graph.hpp index c7bb0b5..cbc0abe 100644 --- a/lib/graph/graph.hpp +++ b/lib/graph/graph.hpp @@ -149,50 +149,6 @@ public: vertex_iterator vertex_end() { return vertex_iterator(m_vertices.end()); } const vertex_iterator vertex_end() const { return vertex_iterator(m_vertices.end()); } - class edge_iterator : public std::iterator - { - friend class Graph; - - public: - typedef edge_iterator self_type; - typedef edge_iterator& reference_self_type; - typedef const edge_iterator& const_reference_self_type; - - edge_iterator() : m_vertices(), m_vertex_it(), m_edge_it(), m_edge() {} - ~edge_iterator() {} - edge_iterator(const_reference_self_type o); - reference_self_type operator=(self_type o) { swap(o); return *this; } - void swap(reference_self_type other); - - edge_reference operator*() { resetEdge(); return m_edge; } - edge_pointer operator->() { resetEdge(); return &m_edge; } - self_type &operator++() { advance(1); return *this; } - self_type operator++(int) { self_type tmp(*this); advance(1); return tmp; } - self_type operator+(difference_type n) { self_type tmp(*this); tmp.pos_ += n; return tmp; } - self_type &operator+=(difference_type n) { advance(n); return *this; } - bool operator==(const_reference_self_type o) const; - bool operator!=(const_reference_self_type o) const { return !(*this == o); } - - private: - edge_iterator(v_container vertices, bool begin = true); - void resetEdge(); - void advance(int n); - - v_container m_vertices; - v_iterator m_vertex_it; - typename std::vector::iterator m_edge_it; - Edge m_edge; - }; - - edge_iterator edge_begin() { return edge_iterator(m_vertices); } - const edge_iterator edge_begin() const { return edge_iterator(m_vertices); } - edge_iterator edge_end() { return edge_iterator(m_vertices, false); } - const edge_iterator edge_end() const { return edge_iterator(m_vertices, false); } - private: static void eraseEdge(typename std::vector& v, const_reference data); @@ -228,83 +184,6 @@ inline void Graph::Edge::swap(Edge& o) } -// edge iterator - -template -inline Graph::edge_iterator::edge_iterator(const_reference_self_type o) - : m_vertices(o.m_vertices) - , m_vertex_it(o.m_vertex_it) - , m_edge_it(o.m_edge_it) - , m_edge() -{} - -template -bool Graph::edge_iterator::operator==(const_reference_self_type o) const -{ - const bool this_is_at_end = m_vertex_it == m_vertices.end(); - const bool other_is_at_end = o.m_vertex_it == o.m_vertices.end(); - if ( this_is_at_end && other_is_at_end ) return true; - if ( this_is_at_end != other_is_at_end ) return false; - - return *m_vertex_it == *(o.m_vertex_it) && *m_edge_it == *(o.m_edge_it); -} - -template -inline void Graph::edge_iterator::swap(reference_self_type other) -{ - std::swap(m_vertices, other.m_vertices); - std::swap(m_vertex_it, other.m_vertex_it); - std::swap(m_edge_it, other.m_edge_it); - std::swap(m_edge, other.m_edge); -} - -template -inline Graph::edge_iterator::edge_iterator(v_container vertices, bool begin) - : m_vertices(vertices), m_vertex_it(), m_edge_it(), m_edge() -{ - if (begin) { - m_vertex_it = m_vertices.begin(); - while (m_vertex_it != m_vertices.end() && m_vertices.empty()) - ++m_vertex_it; - - if (m_vertex_it != m_vertices.end()) - m_edge_it = m_vertex_it->second.begin(); - } else { - m_vertex_it = m_vertices.end(); - } -} - -template -inline void Graph::edge_iterator::resetEdge() -{ - if (m_vertex_it == m_vertices.end() || m_vertex_it->second.empty()) { - m_edge = Edge(); - } else { - m_edge = Edge( m_vertex_it->first, (m_edge_it->m_destination)->first, m_edge_it->m_weight); - } -} - -template -void Graph::edge_iterator::advance(int n) -{ - while (n > 0 && m_vertex_it != m_vertices.end()) { - const int edgesAhead = std::distance(m_edge_it, m_vertex_it->second.end()) - 1; - if (n <= edgesAhead) { - std::advance(m_edge_it, n); - return; - } - - if (edgesAhead > 0) n -= edgesAhead; - ++m_vertex_it; - - if (m_vertex_it != m_vertices.end()) { - m_edge_it = m_vertex_it->second.begin(); - if (m_edge_it != m_vertex_it->second.end()) - --n; - } - } -} - // EdgeTo template inline Graph::EdgeTo::EdgeTo(v_iterator destination, const_weight_reference weight)