data: saving graph nodes with short name only

This change saves only the short names in the node, getName() no only gives the short version. The fullName is assembled
from the parent nodes on demand using the getFullName() method.
This commit is contained in:
Eberhard Graether
2014-07-26 14:58:06 +02:00
parent 641acc2bde
commit 68c6ede244
9 changed files with 268 additions and 75 deletions
+81 -34
View File
@@ -16,12 +16,15 @@ Graph::~Graph()
Node* Graph::getNode(const std::string& fullName) const
{
return findNode(
[fullName](Node* n)
{
return n->getName() == fullName;
}
);
std::deque<std::string> names = utility::split<std::deque<std::string>>(fullName, DELIMITER);
Node* node = getLastValidNode(&names);
if (node && !names.size())
{
return node;
}
return nullptr;
}
Edge* Graph::getEdge(Edge::EdgeType type, Node* from, Node* to) const
@@ -71,8 +74,9 @@ Node* Graph::createNodeHierarchy(const std::string& fullName)
Node* Graph::createNodeHierarchy(Node::NodeType type, const std::string& fullName)
{
Node* node = getNode(fullName);
if (node)
std::deque<std::string> names = utility::split<std::deque<std::string>>(fullName, DELIMITER);
Node* node = getLastValidNode(&names);
if (node && !names.size())
{
if (type != Node::NODE_UNDEFINED)
{
@@ -81,7 +85,7 @@ Node* Graph::createNodeHierarchy(Node::NodeType type, const std::string& fullNam
return node;
}
return insertNodeHierarchy(type, fullName);
return insertNodeHierarchy(type, names, node);
}
Node* Graph::createNodeHierarchyWithDistinctSignature(const std::string& fullName, const std::string& signature)
@@ -92,10 +96,12 @@ Node* Graph::createNodeHierarchyWithDistinctSignature(const std::string& fullNam
Node* Graph::createNodeHierarchyWithDistinctSignature(
Node::NodeType type, const std::string& fullName, const std::string& signature
){
Node* node = getNode(fullName);
if (node)
std::deque<std::string> names = utility::split<std::deque<std::string>>(fullName, DELIMITER);
Node* node = getLastValidNode(&names);
if (node && !names.size())
{
if (node->getComponent<TokenComponentSignature>()->getSignature() == signature)
TokenComponentSignature* sigComponent = node->getComponent<TokenComponentSignature>();
if (sigComponent && sigComponent->getSignature() == signature)
{
if (type != Node::NODE_UNDEFINED)
{
@@ -104,11 +110,35 @@ Node* Graph::createNodeHierarchyWithDistinctSignature(
return node;
}
node = insertNode(type, fullName, node->getParentNode());
Node* parentNode = node->getParentNode();
const std::string& name = node->getName();
std::function<bool(Node*)> findSignature =
[&name, &signature](Node* n)
{
TokenComponentSignature* c = n->getComponent<TokenComponentSignature>();
return n->getName() == name && c && c->getSignature() == signature;
};
if (parentNode)
{
node = parentNode->findChildNode(findSignature);
}
else
{
node = findNode(findSignature);
}
if (node)
{
return node;
}
node = insertNode(type, name, parentNode);
}
else
{
node = insertNodeHierarchy(type, fullName);
node = insertNodeHierarchy(type, names, node);
}
node->addComponentSignature(std::make_shared<TokenComponentSignature>(signature));
@@ -311,43 +341,60 @@ Edge* Graph::addEdgeAsPlainCopy(Edge* edge)
const std::string Graph::DELIMITER = "::";
Node* Graph::insertNodeHierarchy(Node::NodeType type, const std::string& fullName)
Node* Graph::getLastValidNode(std::deque<std::string>* names) const
{
Node* node = nullptr;
std::vector<std::string> names = utility::split(fullName, DELIMITER);
std::string name;
const std::string& name = names->front();
for (size_t i = 0; i < names.size(); i++)
{
if (i > 0)
Node* node = findNode(
[&name](Node* n)
{
name += DELIMITER;
return n->getName() == name && n->getParentNode() == nullptr;
}
name += names[i];
);
Node* childNode = getNode(name);
if (!node)
{
return nullptr;
}
names->pop_front();
while (names->size())
{
const std::string& name = names->front();
Node* childNode = node->findChildNode(
[&name](Node* n)
{
return n->getName() == name;
}
);
if (!childNode)
{
if (i == names.size() - 1)
{
childNode = insertNode(type, name, node);
}
else
{
childNode = insertNode(Node::NODE_UNDEFINED, name, node);
}
break;
}
node = childNode;
names->pop_front();
}
return node;
}
Node* Graph::insertNode(Node::NodeType type, const std::string& fullName, Node* parentNode)
Node* Graph::insertNodeHierarchy(Node::NodeType type, std::deque<std::string> names, Node* parentNode)
{
std::shared_ptr<Node> nodePtr = std::make_shared<Node>(type, fullName);
while (names.size())
{
parentNode = insertNode(names.size() == 1 ? type : Node::NODE_UNDEFINED, names.front(), parentNode);
names.pop_front();
}
return parentNode;
}
Node* Graph::insertNode(Node::NodeType type, const std::string& name, Node* parentNode)
{
std::shared_ptr<Node> nodePtr = std::make_shared<Node>(type, name);
m_nodes.push_back(nodePtr);
Node* node = nodePtr.get();
+5 -3
View File
@@ -2,8 +2,9 @@
#define GRAPH_H
#include <memory>
#include <vector>
#include <ostream>
#include <deque>
#include <vector>
#include "data/graph/Edge.h"
#include "data/graph/Node.h"
@@ -54,8 +55,9 @@ protected:
private:
static const std::string DELIMITER;
Node* insertNodeHierarchy(Node::NodeType type, const std::string& fullName);
Node* insertNode(Node::NodeType type, const std::string& fullName, Node* parentNode);
Node* getLastValidNode(std::deque<std::string>* names) const;
Node* insertNodeHierarchy(Node::NodeType type, std::deque<std::string> names, Node* parentNode);
Node* insertNode(Node::NodeType type, const std::string& name, Node* parentNode);
Edge* insertEdge(Edge::EdgeType type, Node* from, Node* to);
void removeEdgeInternal(Edge* edge);
+46 -2
View File
@@ -46,6 +46,19 @@ const std::string& Node::getName() const
return m_name;
}
std::string Node::getFullName() const
{
Node* parent = getParentNode();
if (parent)
{
return parent->getFullName() + "::" + m_name;
}
else
{
return m_name;
}
}
const std::vector<Edge*>& Node::getEdges() const
{
return m_edges;
@@ -90,7 +103,7 @@ Edge* Node::findEdge(std::function<bool(Edge*)> func) const
return *it;
}
return NULL;
return nullptr;
}
Edge* Node::findEdgeOfType(Edge::EdgeType type) const
@@ -116,7 +129,28 @@ Edge* Node::findEdgeOfType(Edge::EdgeType type, std::function<bool(Edge*)> func)
return *it;
}
return NULL;
return nullptr;
}
Node* Node::findChildNode(std::function<bool(Node*)> func) const
{
std::vector<Edge*>::const_iterator it = find_if(m_edges.begin(), m_edges.end(),
[&func](Edge* e)
{
if (e->getType() == Edge::EDGE_MEMBER)
{
return func(e->getTo());
}
return false;
}
);
if (it != m_edges.end())
{
return (*it)->getTo();
}
return nullptr;
}
void Node::forEachEdge(std::function<void(Edge*)> func) const
@@ -137,6 +171,16 @@ void Node::forEachEdgeOfType(Edge::EdgeType type, std::function<void(Edge*)> fun
);
}
void Node::forEachChildNode(std::function<void(Node*)> func) const
{
forEachEdgeOfType(Edge::EDGE_MEMBER,
[func](Edge* e)
{
func(e->getTo());
}
);
}
bool Node::isNode() const
{
return true;
+4
View File
@@ -38,6 +38,8 @@ public:
void setType(NodeType type);
const std::string& getName() const;
std::string getFullName() const;
const std::vector<Edge*>& getEdges() const;
void addEdge(Edge* edge);
@@ -49,9 +51,11 @@ public:
Edge* findEdge(std::function<bool(Edge*)> func) const;
Edge* findEdgeOfType(Edge::EdgeType type) const;
Edge* findEdgeOfType(Edge::EdgeType type, std::function<bool(Edge*)> func) const;
Node* findChildNode(std::function<bool(Node*)> func) const;
void forEachEdge(std::function<void(Edge*)> func) const;
void forEachEdgeOfType(Edge::EdgeType type, std::function<void(Edge*)> func) const;
void forEachChildNode(std::function<void(Node*)> func) const;
// Token implementation.
virtual bool isNode() const;