From 4745047b607a75b6e7285174d643d6e279d782de Mon Sep 17 00:00:00 2001 From: Denes Matetelki Date: Sun, 24 Mar 2013 17:26:39 +0100 Subject: [PATCH] private subclasses: Vertex and EdgeTo declarations got moved to the end of the class declaration of Graph --- graph.h | 114 ++++++++++++++++++++++++++++++++++++++----------------- main.cpp | 7 ++++ 2 files changed, 86 insertions(+), 35 deletions(-) diff --git a/graph.h b/graph.h index 0bdfb64..d62c497 100644 --- a/graph.h +++ b/graph.h @@ -6,12 +6,16 @@ #include #include + // directed, weighted /// @todo weight type as param too? template class Graph { +private: + class Vertex; + public: typedef size_t size_type; @@ -22,33 +26,23 @@ public: typedef const T& const_reference; typedef std::ptrdiff_t difference_type; -private: + class Edge { + public: + Edge() : m_source(0), m_destination(0), m_weight(0) {} - struct EdgeTo { + Edge(pointer source, pointer destination, float weight) : + m_source(source), m_destination(destination), m_weight(weight) {} - EdgeTo(const_reference destination, float weight = 0); - EdgeTo(const EdgeTo& other); - EdgeTo& operator=(const EdgeTo& other); + pointer getSource() const { return m_source; } + pointer getDestination() const { return m_destination; } + float getWeight() const { return m_weight; } + private: + pointer m_source; pointer m_destination; float m_weight; }; - struct Vertex { - - Vertex(const_reference data); - Vertex(const Vertex& other); - Vertex& operator=(const Vertex& other); - void addEdge(const_reference destination, float weight = 0); - void removeEdge(const_reference destination, float weight = 0); - void removeAllEdgesTo(const_reference destination); - - pointer m_data; - std::list m_edges; - }; - - -public: Graph(); @@ -69,19 +63,9 @@ public: std::vector vertices() const; std::vector neighboursOf(const_reference data) const; std::vector edgesBetween(const_reference source, const_reference destination) const; + std::vector edges() const; -private: - - Graph(const Graph& o) { /** @todo impelemnt me */ } - Graph& operator=(const Graph& o) { /** @todo impelemnt me */ } - - typename std::vector::const_iterator find(const_reference data) const; - typename std::vector::iterator find(const_reference data); - - std::vector m_vertices; - -public: - + // iterators class iterator : public std::iterator::iterator m_it; }; - iterator begin() { return iterator(m_vertices.begin()); } - iterator end() { return iterator(m_vertices.begin()); } + iterator begin() { return iterator(m_vertices.begin()); } + iterator end() { return iterator(m_vertices.begin()); } + - /// @todo const iterator and cbegin and cend +private: + + struct EdgeTo { + + EdgeTo(const_reference destination, float weight = 0); + EdgeTo(const EdgeTo& other); + EdgeTo& operator=(const EdgeTo& other); + + pointer m_destination; + float m_weight; + }; + + struct Vertex { + + Vertex(const_reference data); + Vertex(const Vertex& other); + Vertex& operator=(const Vertex& other); + void addEdge(const_reference destination, float weight = 0); + void removeEdge(const_reference destination, float weight = 0); + void removeAllEdgesTo(const_reference destination); + std::vector edges() const; + + pointer m_data; + std::list m_edges; + }; + + Graph(const Graph& o) { /** @todo impelemnt me */ } + Graph& operator=(const Graph& o) { /** @todo impelemnt me */ } + + typename std::vector::const_iterator find(const_reference data) const; + typename std::vector::iterator find(const_reference data); + + std::vector m_vertices; }; @@ -205,6 +222,19 @@ void Graph::Vertex::removeAllEdgesTo(const_reference destination) { return e.m_destination == destination; }); } +template +std::vector::Edge> Graph::Vertex::edges() const +{ + std::vector::Edge> retval; + + std::for_each(m_edges.begin(), m_edges.end(), + [&retval, this](const EdgeTo& e) + { retval.push_back(Edge(this->m_data, e.m_destination, e.m_weight)); }); + + return retval; +} + + // Graph @@ -341,6 +371,20 @@ std::vector Graph::edgesBetween(const_reference source, const_referenc return retval; } +template +std::vector::Edge> Graph::edges() const +{ + std::vector::Edge> retval; + + 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; +} + template typename std::vector::Vertex >::const_iterator Graph::find(const_reference data) const diff --git a/main.cpp b/main.cpp index fef0823..0e74bfe 100644 --- a/main.cpp +++ b/main.cpp @@ -84,6 +84,13 @@ int main() g_it++, v_it++) assert(*g_it == *v_it); + + assert(g.addEdge(b, c) == true); + assert(g.addEdge(a, d) == true); + std::vector::Edge> e = g.edges(); + + assert(e.size() == 3); + return 0; }