From 684e953c3b0c3beb077d1ec3cf69c24693b9ac36 Mon Sep 17 00:00:00 2001 From: dmatetelki Date: Fri, 1 Aug 2014 14:06:46 +0200 Subject: [PATCH] new function: modifyVertex, internal edgecontainer is a typedef --- lib/graph/graph.hpp | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/lib/graph/graph.hpp b/lib/graph/graph.hpp index 803f2a3..ca99b0a 100644 --- a/lib/graph/graph.hpp +++ b/lib/graph/graph.hpp @@ -26,7 +26,8 @@ public: private: - typedef std::unordered_map > v_container; + typedef std::vector edge_container; + typedef std::unordered_map v_container; typedef typename v_container::iterator v_iterator; typedef typename v_container::const_iterator v_const_iterator; @@ -62,6 +63,8 @@ public: void addVertex(const_reference data); void removeVertex(const_reference data); + void modifyVertex(const_reference old_data, const_reference new_data); + void addEdge(const_reference source, const_reference destination); void setEdges(const_reference source, const std::vector& destinations); void removeEdge(const_reference source, const_reference destination); @@ -120,7 +123,7 @@ public: private: - static void eraseEdge(typename std::vector& v, const_reference data); + static void eraseEdge(edge_container& v, const_reference data); v_container m_vertices; }; @@ -160,7 +163,7 @@ inline void Graph::addVertex(const_reference data) if (m_vertices.find(data) != m_vertices.end()) return; - std::pair > p(data, std::vector()); + std::pair p(data, edge_container()); m_vertices.insert(p); } @@ -177,6 +180,24 @@ inline void Graph::removeVertex(const_reference data) m_vertices.erase(it); } +template +inline void Graph::modifyVertex(const_reference old_data, const_reference new_data) +{ + v_iterator it = m_vertices.find(old_data); + if (it == m_vertices.end()) + return; + + std::vector neighbours = neighboursOf(old_data); + for (auto &v : neighbours) { + std::vector::iterator n_it = neighbours.find(old_data); + *it = new_data; + } + + m_vertices.erase(it); + std::pair p(new_data, neighbours); + m_vertices.insert(p); +} + template inline void Graph::addEdge(const_reference source, const_reference destination) { @@ -197,7 +218,6 @@ inline void Graph::setEdges(const_reference source, const std::vectorsecond.clear(); - source_it->second.reserve(destinations.size()); // it is needed? source_it->second = destinations; } @@ -248,7 +268,7 @@ inline std::vector::Edge> Graph::edges() const } template -inline void Graph::eraseEdge(typename std::vector& v, const_reference data) { +inline void Graph::eraseEdge(edge_container& v, const_reference data) { v.erase(std::remove(v.begin(), v.end()), v.end()); }