From 8e2f06459807991a3d154443dd4c1fc153c04125 Mon Sep 17 00:00:00 2001 From: Denes Matetelki Date: Wed, 6 Mar 2013 19:58:45 +0100 Subject: [PATCH] Vector and Edge pointers are not const anymore. iterator class is implemented. --- graph.h | 75 ++++++++++++++++++++++++++++++-------------------------- main.cpp | 14 +++++++++++ 2 files changed, 54 insertions(+), 35 deletions(-) diff --git a/graph.h b/graph.h index 78bd855..c5529e4 100644 --- a/graph.h +++ b/graph.h @@ -29,7 +29,7 @@ private: Edge(const Edge& other); Edge& operator=(const Edge& other); - const_pointer m_destination; + pointer m_destination; float m_weight; }; @@ -42,7 +42,7 @@ private: void removeEdge(const_reference destination, float weight = 0); void removeAllEdgesTo(const_reference destination); - const_pointer m_data; + pointer m_data; std::vector m_edges; }; @@ -81,35 +81,40 @@ private: public: - class iterator : public std::iterator - { - public: - typedef iterator self_type; - typedef iterator& reference_self_type; - typedef const iterator& const_reference_self_type; - - iterator() { /** @todo impelemnt me */ } - ~iterator() { /** @todo impelemnt me */ } - iterator(const_reference_self_type o) { /** @todo impelemnt me */ } - reference_self_type operator=(const_reference_self_type o) { /** @todo impelemnt me */ } - - reference operator*() { /** @todo impelemnt me */ } - pointer operator->() { /** @todo impelemnt me */ } - - self_type &operator++() { /** @todo impelemnt me */ } - self_type operator++(int) { /** @todo impelemnt me */ } - self_type operator+(difference_type n) { /** @todo impelemnt me */ } - self_type &operator+=(difference_type n) { /** @todo impelemnt me */ } - bool operator==(const_reference_self_type o) { /** @todo impelemnt me */ return false; } - bool operator!=(const_reference_self_type o) { return !(*this == o); } - }; - - iterator begin() { /** @todo impelemnt me */ } - iterator end() { /** @todo impelemnt me */ } + class iterator : public std::iterator + { + public: + typedef iterator self_type; + typedef iterator& reference_self_type; + typedef const iterator& const_reference_self_type; + + iterator() : m_it() {} + iterator(typename std::vector::iterator it) : m_it(it) {} + ~iterator() {} + iterator(const_reference_self_type o) : m_it(o.m_it) {} + reference_self_type operator=(const_reference_self_type o) + { if (this != &o) { m_it = o.m_it; } return *this; } + + reference operator*() { return *((*m_it).m_data); } + pointer operator->() { return (*m_it).m_data; } + + self_type &operator++() { ++m_it; return *this; } + self_type operator++(int) { self_type tmp(*this); ++(*this); return tmp; } + self_type operator+(difference_type n) { self_type tmp(*this); tmp.pos_ += n; return tmp; } + self_type &operator+=(difference_type n) { m_it += n; return *this; } + bool operator==(const_reference_self_type o) { return m_it == o.m_it; } + bool operator!=(const_reference_self_type o) { return !(*this == o); } + + private: + typename std::vector::iterator m_it; + }; + + iterator begin() { return iterator(m_vertices.begin()); } + iterator end() { return iterator(m_vertices.begin()); } /// @todo const iterator and cbegin and cend }; @@ -119,7 +124,7 @@ public: template Graph::Edge::Edge(const_reference destination, float weight) - : m_destination(&destination) + : m_destination(const_cast(&destination)) , m_weight(weight) { @@ -149,7 +154,7 @@ typename Graph::Edge& Graph::Edge::operator=(const Edge& other) template Graph::Vertex::Vertex(const_reference data) - : m_data(&data) + : m_data(const_cast(&data)) , m_edges() { @@ -300,7 +305,7 @@ std::vector::pointer> Graph::vertices() const std::vector retval; std::for_each(m_vertices.begin(), m_vertices.end(), [&retval](const Vertex& v) - { retval.push_back( const_cast(v.m_data)); }); + { retval.push_back(v.m_data); }); return retval; } @@ -314,7 +319,7 @@ std::vector::pointer> Graph::neighboursOf(const_reference d std::for_each((*vertex_it).m_edges.begin(), (*vertex_it).m_edges.end(), [&retval](const Edge& e) - { retval.push_back( const_cast(e.m_destination)); }); + { retval.push_back(e.m_destination); }); return retval; } diff --git a/main.cpp b/main.cpp index 903116f..7be70a7 100644 --- a/main.cpp +++ b/main.cpp @@ -55,6 +55,20 @@ int main() assert(*(n[0]) == 5); } + int c = 13; + int d = 1; + g.addVertex(d); + g.addVertex(c); + int vertices_array[] = {2, 5, 13, 1}; + std::vector v(vertices_array, vertices_array + sizeof(vertices_array) / sizeof(int) ); + Graph::iterator g_it; + std::vector::iterator v_it; + for (g_it = g.begin(), v_it = v.begin(); + g_it != g.end(); + g_it++, v_it++) + assert(*g_it == *v_it); + + assert(g.neighboursOf(5).size() == 0); return 0;