diff --git a/lib/graph/graph.hpp b/lib/graph/graph.hpp index 5ea2941..135b01b 100644 --- a/lib/graph/graph.hpp +++ b/lib/graph/graph.hpp @@ -53,14 +53,6 @@ public: Graph& operator=(Graph o) { swap(o); return *this; } void swap(Graph& o) { std::swap(m_vertices, o.m_vertices); } - // Capacity - bool empty() const { return m_vertices.empty(); } - size_type numberOfVertices() const { return m_vertices.size(); } - size_type numberOfEdges() const; - - // Modifiers - void clear() { m_vertices.clear(); } - void addVertex(const_reference data); void removeVertex(const_reference data); void modifyVertex(const_reference old_data, const_reference new_data); @@ -69,14 +61,13 @@ public: void setEdges(const_reference source, const std::vector& destinations); void removeEdge(const_reference source, const_reference destination); - // Lookup - bool contains(const_reference data) const { return m_vertices.find(data) != m_vertices.end(); } std::vector vertices() const; - bool connected(const_reference source, const_reference destination) const; - std::vector neighboursOf(const_reference data) const; std::vector edges() const; - // iterator + void clear() { m_vertices.clear(); } + std::vector neighboursOf(const_reference data) const; + + class vertex_iterator : public std::iterator +bool empty(const Graph& g) { return g.vertices().empty(); } + +template +typename Graph::size_type numberOfVertices(const Graph& g) { return g.vertices().size(); } + +template +typename Graph::size_type numberOfEdges(const Graph& g) { return g.edges().size(); } + +template +bool contains(const Graph& g, typename Graph::const_reference data) { + const auto v = g.vertices(); + return std::find(v.begin(), v.end(), data) != v.end(); +} + +template +bool connected(const Graph& g, typename Graph::const_reference source, typename Graph::const_reference destination) { + const auto n = g.neighboursOf(source); + return std::find(n.begin(), n.end(), destination) != n.end(); +} -// Graph +// Graph implementation template inline Graph::Graph(std::initializer_list vertex_list) @@ -146,17 +159,7 @@ inline Graph::Graph(std::initializer_list edge_list) : Graph() { for (const Edge& e : edge_list ) - addEdge(e.source, e.destination, e.weight); -} - -template -inline typename Graph::size_type Graph::numberOfEdges() const -{ - int sum = 0; - for (const auto& v : m_vertices) - sum += v.second.size(); - - return sum; + addEdge(e.source, e.destination); } template @@ -252,13 +255,6 @@ inline std::vector::value_type> Graph::vertices() const return retval; } -template -inline bool Graph::connected(const_reference source, const_reference destination) const -{ - std::vector neightbours = neighboursOf(source); - return std::find(neightbours.begin(), neightbours.end(), destination) != neightbours.end(); -} - template inline std::vector Graph::neighboursOf(const_reference data) const {