diff --git a/lib/graph/graph.hpp b/lib/graph/graph.hpp index ca99b0a..4f6e8b2 100644 --- a/lib/graph/graph.hpp +++ b/lib/graph/graph.hpp @@ -123,6 +123,7 @@ public: private: + std::vector& nonConstNeighboursOf(const_reference data); static void eraseEdge(edge_container& v, const_reference data); v_container m_vertices; @@ -183,14 +184,18 @@ inline void Graph::removeVertex(const_reference data) template inline void Graph::modifyVertex(const_reference old_data, const_reference new_data) { + if (old_data == new_data) + return; + 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; + std::vector& n_v = nonConstNeighboursOf(v); + typename std::vector::iterator n_it = std::find(n_v.begin(), n_v.end(), old_data); + *n_it = new_data; } m_vertices.erase(it); @@ -256,6 +261,13 @@ inline std::vector Graph::neighboursOf(const_reference data) const return vertex_it->second; } +template +inline std::vector& Graph::nonConstNeighboursOf(const_reference data) +{ + v_iterator vertex_it = m_vertices.find(data); + return vertex_it->second; +} + template inline std::vector::Edge> Graph::edges() const { diff --git a/lib/graph/graph_xml.hpp b/lib/graph/graph_xml.hpp index 25af7a0..169ff03 100644 --- a/lib/graph/graph_xml.hpp +++ b/lib/graph/graph_xml.hpp @@ -1,4 +1,4 @@ -#include +#include "graph.hpp" #include #include @@ -20,19 +20,24 @@ void readVertices(Graph& g, F vertexCreator, const xmlNodePtr root_element) std::vector edges; for (xmlNodePtr cur_kid = cur_node->children; cur_kid; cur_kid = cur_kid->next) if (cur_kid->type == XML_ELEMENT_NODE && cur_kid->children->type == XML_TEXT_NODE) { - const std::string e( reinterpret_cast(cur_kid->children->content)); - edges.push_back(vertexCreator(e)); + const std::string edge( reinterpret_cast(cur_kid->children->content)); + V e = vertexCreator(edge); + edges.push_back(e); } - g.setEdges(v, edges); } } } // anonym namespace + template Graph readGraphFromXML(const std::string& filename, F vertexCreator) { + std::ifstream file(filename); + if (!file.good()) + throw std::runtime_error("Failed to open " + filename + " to read."); + xmlDocPtr doc = xmlReadFile(filename.c_str(), NULL, 0); if (doc == NULL) throw std::runtime_error("Failed to parse " + filename); @@ -64,7 +69,7 @@ void writeGraphToXML(const Graph& g, const std::string& filename, F vertexSer file << "" << std::endl; for (const auto cit2 : g.neighboursOf(cit)) { const std::string n = vertexSerializer(cit2); - file << " " << v << "" << std::endl; + file << " " << n << "" << std::endl; } file << "" << std::endl; }