diff --git a/lib/graph/graph.hpp b/lib/graph/graph.hpp index 26649eb..00b4e33 100644 --- a/lib/graph/graph.hpp +++ b/lib/graph/graph.hpp @@ -121,6 +121,7 @@ private: std::vector& nonConstNeighboursOf(const_reference data); static void eraseEdge(edge_container& v, const_reference data); + v_iterator addVertexAndReturnIterator(const_reference data); v_container m_vertices; }; @@ -206,7 +207,7 @@ inline Graph::Graph(const std::vector& edge_list) template inline void Graph::addVertex(const_reference data) { - m_vertices.emplace(data, edge_container()); + addVertexAndReturnIterator(data); } template @@ -230,8 +231,8 @@ inline void Graph::modifyVertex(const_reference old_data, const_reference new auto n_it = std::find(n.begin(), n.end(), old_data); *n_it = new_data; } - const auto number_of_removed_elements = m_vertices.erase(old_data); + const auto number_of_removed_elements = m_vertices.erase(old_data); if (number_of_removed_elements > 0) m_vertices.emplace(new_data, neighbours); } @@ -246,22 +247,16 @@ inline void Graph::addEdge(const_reference source, const_reference destinatio if (std::find(n.begin(), n.end(), destination) != n.end()) return; - addVertex(source); - addVertex(destination); - - auto source_it = m_vertices.find(source); - auto destination_it = m_vertices.find(destination); - + auto source_it = addVertexAndReturnIterator(source); source_it->second.push_back(destination); + auto destination_it = addVertexAndReturnIterator(destination); destination_it->second.push_back(source); } template inline void Graph::setEdges(const_reference source, const std::vector& destinations) { - addVertex(source); - auto source_it = m_vertices.find(source); - + auto source_it = addVertexAndReturnIterator(source); source_it->second.clear(); source_it->second = destinations; } @@ -316,5 +311,9 @@ inline void Graph::eraseEdge(edge_container& v, const_reference data) { v.end()); } - +template +inline typename Graph::v_iterator Graph::addVertexAndReturnIterator(const_reference data) +{ + return m_vertices.emplace(data, edge_container()).first; +} #endif // GRAPH_HPP