graph has modifyVertex member

for/release
dmatetelki 11 years ago
parent 5c610fff0b
commit 738b6ce345

@ -123,6 +123,7 @@ public:
private: private:
std::vector<value_type>& nonConstNeighboursOf(const_reference data);
static void eraseEdge(edge_container& v, const_reference data); static void eraseEdge(edge_container& v, const_reference data);
v_container m_vertices; v_container m_vertices;
@ -183,14 +184,18 @@ inline void Graph<V>::removeVertex(const_reference data)
template <typename V> template <typename V>
inline void Graph<V>::modifyVertex(const_reference old_data, const_reference new_data) inline void Graph<V>::modifyVertex(const_reference old_data, const_reference new_data)
{ {
if (old_data == new_data)
return;
v_iterator it = m_vertices.find(old_data); v_iterator it = m_vertices.find(old_data);
if (it == m_vertices.end()) if (it == m_vertices.end())
return; return;
std::vector<value_type> neighbours = neighboursOf(old_data); std::vector<value_type> neighbours = neighboursOf(old_data);
for (auto &v : neighbours) { for (auto &v : neighbours) {
std::vector<value_type>::iterator n_it = neighbours.find(old_data); std::vector<value_type>& n_v = nonConstNeighboursOf(v);
*it = new_data; typename std::vector<value_type>::iterator n_it = std::find(n_v.begin(), n_v.end(), old_data);
*n_it = new_data;
} }
m_vertices.erase(it); m_vertices.erase(it);
@ -256,6 +261,13 @@ inline std::vector<V> Graph<V>::neighboursOf(const_reference data) const
return vertex_it->second; return vertex_it->second;
} }
template <typename V>
inline std::vector<V>& Graph<V>::nonConstNeighboursOf(const_reference data)
{
v_iterator vertex_it = m_vertices.find(data);
return vertex_it->second;
}
template <typename V> template <typename V>
inline std::vector<typename Graph<V>::Edge> Graph<V>::edges() const inline std::vector<typename Graph<V>::Edge> Graph<V>::edges() const
{ {

@ -1,4 +1,4 @@
#include <graph.hpp> #include "graph.hpp"
#include <stdexcept> #include <stdexcept>
#include <fstream> #include <fstream>
@ -20,19 +20,24 @@ void readVertices(Graph<V>& g, F vertexCreator, const xmlNodePtr root_element)
std::vector<V> edges; std::vector<V> edges;
for (xmlNodePtr cur_kid = cur_node->children; cur_kid; cur_kid = cur_kid->next) 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) { if (cur_kid->type == XML_ELEMENT_NODE && cur_kid->children->type == XML_TEXT_NODE) {
const std::string e( reinterpret_cast<const char*>(cur_kid->children->content)); const std::string edge( reinterpret_cast<const char*>(cur_kid->children->content));
edges.push_back(vertexCreator(e)); V e = vertexCreator(edge);
edges.push_back(e);
} }
g.setEdges(v, edges); g.setEdges(v, edges);
} }
} }
} // anonym namespace } // anonym namespace
template <typename V, typename F> template <typename V, typename F>
Graph<V> readGraphFromXML(const std::string& filename, F vertexCreator) Graph<V> 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); xmlDocPtr doc = xmlReadFile(filename.c_str(), NULL, 0);
if (doc == NULL) if (doc == NULL)
throw std::runtime_error("Failed to parse " + filename); throw std::runtime_error("Failed to parse " + filename);
@ -64,7 +69,7 @@ void writeGraphToXML(const Graph<V>& g, const std::string& filename, F vertexSer
file << "<vertex pos=\"" << v << "\">" << std::endl; file << "<vertex pos=\"" << v << "\">" << std::endl;
for (const auto cit2 : g.neighboursOf(cit)) { for (const auto cit2 : g.neighboursOf(cit)) {
const std::string n = vertexSerializer(cit2); const std::string n = vertexSerializer(cit2);
file << " <edge>" << v << "</edge>" << std::endl; file << " <edge>" << n << "</edge>" << std::endl;
} }
file << "</vertex>" << std::endl; file << "</vertex>" << std::endl;
} }

Loading…
Cancel
Save