From 473e26f0725e160cc7abdda74e9b182af214ea1b Mon Sep 17 00:00:00 2001 From: Eberhard Graether Date: Sat, 26 Jul 2014 23:32:16 +0200 Subject: [PATCH] data: Switched Graph implementation to use std::map with Id as key for Nodes and Edges --- src/lib/data/graph/Graph.cpp | 91 ++++++++++++++---------------------- src/lib/data/graph/Graph.h | 10 ++-- 2 files changed, 41 insertions(+), 60 deletions(-) diff --git a/src/lib/data/graph/Graph.cpp b/src/lib/data/graph/Graph.cpp index e9a83904..25c2e156 100644 --- a/src/lib/data/graph/Graph.cpp +++ b/src/lib/data/graph/Graph.cpp @@ -39,22 +39,22 @@ Edge* Graph::getEdge(Edge::EdgeType type, Node* from, Node* to) const Node* Graph::getNodeById(Id id) const { - return findNode( - [id](Node* n) - { - return n->getId() == id; - } - ); + std::map>::const_iterator it = m_nodes.find(id); + if (it != m_nodes.end()) + { + return it->second.get(); + } + return nullptr; } Edge* Graph::getEdgeById(Id id) const { - return findEdge( - [id](Edge* e) - { - return e->getId() == id; - } - ); + std::map>::const_iterator it = m_edges.find(id); + if (it != m_edges.end()) + { + return it->second.get(); + } + return nullptr; } Token* Graph::getTokenById(Id id) const @@ -158,15 +158,7 @@ Edge* Graph::createEdge(Edge::EdgeType type, Node* from, Node* to) void Graph::removeNode(Node* node) { - std::vector >::const_iterator it; - for (it = m_nodes.begin(); it != m_nodes.end(); it++) - { - if (it->get() == node) - { - break; - } - } - + std::map>::const_iterator it = m_nodes.find(node->getId()); if (it == m_nodes.end()) { LOG_WARNING("Node was not found in the graph."); @@ -198,15 +190,7 @@ void Graph::removeNode(Node* node) void Graph::removeEdge(Edge* edge) { - std::vector >::const_iterator it; - for (it = m_edges.begin(); it != m_edges.end(); it++) - { - if (it->get() == edge) - { - break; - } - } - + std::map>::const_iterator it = m_edges.find(edge->getId()); if (it == m_edges.end()) { LOG_WARNING("Edge was not found in the graph."); @@ -223,16 +207,16 @@ void Graph::removeEdge(Edge* edge) Node* Graph::findNode(std::function func) const { - std::vector >::const_iterator it = find_if(m_nodes.begin(), m_nodes.end(), - [&func](const std::shared_ptr& n) + std::map>::const_iterator it = find_if(m_nodes.begin(), m_nodes.end(), + [&func](const std::pair>& n) { - return func(n.get()); + return func(n.second.get()); } ); if (it != m_nodes.end()) { - return it->get(); + return it->second.get(); } return nullptr; @@ -240,16 +224,16 @@ Node* Graph::findNode(std::function func) const Edge* Graph::findEdge(std::function func) const { - std::vector >::const_iterator it = find_if(m_edges.begin(), m_edges.end(), - [func](const std::shared_ptr& e) + std::map>::const_iterator it = find_if(m_edges.begin(), m_edges.end(), + [func](const std::pair>& e) { - return func(e.get()); + return func(e.second.get()); } ); if (it != m_edges.end()) { - return it->get(); + return it->second.get(); } return nullptr; @@ -274,17 +258,17 @@ Token* Graph::findToken(std::function func) const void Graph::forEachNode(std::function func) const { - for (const std::shared_ptr& node : m_nodes) + for (const std::pair>& node : m_nodes) { - func(node.get()); + func(node.second.get()); } } void Graph::forEachEdge(std::function func) const { - for (const std::shared_ptr& edge : m_edges) + for (const std::pair>& edge : m_edges) { - func(edge.get()); + func(edge.second.get()); } } @@ -300,12 +284,12 @@ void Graph::clear() m_nodes.clear(); } -const std::vector >& Graph::getNodes() const +const std::map >& Graph::getNodes() const { return m_nodes; } -const std::vector >& Graph::getEdges() const +const std::map >& Graph::getEdges() const { return m_edges; } @@ -319,7 +303,7 @@ Node* Graph::addNodeAsPlainCopy(Node* node) } std::shared_ptr copy = std::make_shared(*node); - m_nodes.push_back(copy); + m_nodes.emplace(copy->getId(), copy); return copy.get(); } @@ -335,7 +319,7 @@ Edge* Graph::addEdgeAsPlainCopy(Edge* edge) Node* to = addNodeAsPlainCopy(edge->getTo()); std::shared_ptr copy = std::make_shared(*edge, from, to); - m_edges.push_back(copy); + m_edges.emplace(copy->getId(), copy); return copy.get(); } @@ -395,7 +379,7 @@ Node* Graph::insertNodeHierarchy(Node::NodeType type, std::deque na Node* Graph::insertNode(Node::NodeType type, const std::string& name, Node* parentNode) { std::shared_ptr nodePtr = std::make_shared(type, name); - m_nodes.push_back(nodePtr); + m_nodes.emplace(nodePtr->getId(), nodePtr); Node* node = nodePtr.get(); @@ -410,20 +394,17 @@ Node* Graph::insertNode(Node::NodeType type, const std::string& name, Node* pare Edge* Graph::insertEdge(Edge::EdgeType type, Node* from, Node* to) { std::shared_ptr edgePtr = std::make_shared(type, from, to); - m_edges.push_back(edgePtr); + m_edges.emplace(edgePtr->getId(), edgePtr); return edgePtr.get(); } void Graph::removeEdgeInternal(Edge* edge) { - std::vector >::const_iterator it; - for (it = m_edges.begin(); it != m_edges.end(); it++) + std::map >::const_iterator it = m_edges.find(edge->getId()); + if (it != m_edges.end() && it->second.get() == edge) { - if (it->get() == edge) - { - m_edges.erase(it); - return; - } + m_edges.erase(it); + return; } } diff --git a/src/lib/data/graph/Graph.h b/src/lib/data/graph/Graph.h index a72f4d88..2a687e91 100644 --- a/src/lib/data/graph/Graph.h +++ b/src/lib/data/graph/Graph.h @@ -1,10 +1,10 @@ #ifndef GRAPH_H #define GRAPH_H +#include #include #include #include -#include #include "data/graph/Edge.h" #include "data/graph/Node.h" @@ -49,8 +49,8 @@ public: void clear(); protected: - const std::vector >& getNodes() const; - const std::vector >& getEdges() const; + const std::map>& getNodes() const; + const std::map>& getEdges() const; private: static const std::string DELIMITER; @@ -63,8 +63,8 @@ private: const std::string m_delimiter; - std::vector > m_nodes; - std::vector > m_edges; + std::map> m_nodes; + std::map> m_edges; friend std::ostream& operator<<(std::ostream& ostream, const Graph& graph); };