diff --git a/lib/graph/graph.hpp b/lib/graph/graph.hpp index 00bd2b0..d42193a 100644 --- a/lib/graph/graph.hpp +++ b/lib/graph/graph.hpp @@ -96,7 +96,6 @@ public: void removeEdge(const_reference source, const_reference destination); std::vector vertices() const; - std::vector edges() const; void clear() noexcept { m_vertices.clear(); } const std::vector& neighboursOf(const_reference data) const; @@ -158,26 +157,38 @@ private: // Free functions template -bool empty(const Graph& g) { return g.vertices().empty(); } +inline bool empty(const Graph& g) noexcept { return g.vertices().empty(); } template -typename Graph::size_type numberOfVertices(const Graph& g) { return g.vertices().size(); } +inline typename Graph::size_type numberOfVertices(const Graph& g) noexcept { return g.vertices().size(); } template -typename Graph::size_type numberOfEdges(const Graph& g) { return g.edges().size(); } +inline typename Graph::size_type numberOfEdges(const Graph& g) { return edges(g).size(); } template -bool contains(const Graph& g, typename Graph::const_reference data) { +inline 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) { +inline 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(); } +template +inline std::vector::Edge> edges(const Graph& g) +{ + std::vector::Edge> retval; + for (const auto& v : g.vertices()) + for (const auto& e : g.neighboursOf(v)) + retval.push_back(typename Graph::Edge(v, e)); + + return retval; +} + + // Graph implementation template @@ -321,17 +332,6 @@ inline std::vector& Graph::nonConstNeighboursOf(const_reference data) return vertex_it->second; } -template -inline std::vector::Edge> Graph::edges() const -{ - std::vector::Edge> retval; - for (const auto& v : m_vertices) - for (const auto& e : v.second) - retval.push_back(Graph::Edge(v.first, e)); - - return retval; -} - template inline void Graph::eraseEdge(edge_container& v, const_reference data) { v.erase(std::remove_if(v.begin(), v.end(),