From ee9b8b8c39edc000bd476a1caaf09a8e3b205ca4 Mon Sep 17 00:00:00 2001 From: Nathan Braswell Date: Sun, 23 Jun 2013 05:54:58 -0400 Subject: [PATCH] Modified the NodeTree with a unique id used in construction of the DOT representation, so that two different nodes with the same name don't end up as the same on the graph. --- include/NodeTree.h | 7 +++++++ include/util.h | 2 ++ src/NodeTree.cpp | 14 +++++++++++++- src/util.cpp | 15 +++++++++++++++ 4 files changed, 37 insertions(+), 1 deletion(-) diff --git a/include/NodeTree.h b/include/NodeTree.h index 5f31b23..d24627d 100644 --- a/include/NodeTree.h +++ b/include/NodeTree.h @@ -5,8 +5,11 @@ #define NULL 0 #endif +#include + #include #include +#include class NodeTree { public: @@ -32,9 +35,13 @@ class NodeTree { private: std::string DOTGraphStringHelper(); + std::string getDOTName(); std::string name; NodeTree* parent; std::vector children; + + static int idCounter; + int id; }; #endif \ No newline at end of file diff --git a/include/util.h b/include/util.h index 6192b1b..75fe1c4 100644 --- a/include/util.h +++ b/include/util.h @@ -9,5 +9,7 @@ #include std::string intToString(int theInt); +std::string truncateEnd(std::string to_truncate); +std::string removeBeginning(std::string to_remove); #endif \ No newline at end of file diff --git a/src/NodeTree.cpp b/src/NodeTree.cpp index 63c307b..11225cc 100644 --- a/src/NodeTree.cpp +++ b/src/NodeTree.cpp @@ -1,13 +1,19 @@ #include "NodeTree.h" +int NodeTree::idCounter; + NodeTree::NodeTree() { parent = NULL; name = "UnnamedNode"; + + id = idCounter++; } NodeTree::NodeTree(std::string name) { parent = NULL; this->name = name; + + id = idCounter++; } NodeTree::~NodeTree() { @@ -78,7 +84,13 @@ std::string NodeTree::DOTGraphString() { std::string NodeTree::DOTGraphStringHelper() { std::string ourDOTRelation = ""; for (int i = 0; i < children.size(); i++) { - ourDOTRelation += name + " -> " + children[i]->name + ";\n" + children[i]->DOTGraphStringHelper(); + ourDOTRelation += getDOTName() + " -> " + children[i]->getDOTName() + ";\n" + children[i]->DOTGraphStringHelper(); } return(ourDOTRelation); +} + +std::string NodeTree::getDOTName() { + if (name.at(0) == '\"') + return truncateEnd(name) + "_" + intToString(id) + "\""; //Note that terminals already have a quote in the front of their name, so we don't need to add one + return "\"" + name + "_" + intToString(id) + "\""; } \ No newline at end of file diff --git a/src/util.cpp b/src/util.cpp index 2b69251..c8e2d0b 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -4,4 +4,19 @@ std::string intToString(int theInt) { std::stringstream converter; converter << theInt; return converter.str(); +} +std::string truncateEnd(std::string to_truncate) +{ + std::string to_return = ""; + for (unsigned int i = 0; i < to_truncate.length()-1; i++) + to_return = to_return + to_truncate[i]; + return to_return; +} + +std::string removeBeginning(std::string to_remove) +{ + std::string to_return = ""; + for (unsigned int i = 1; i < to_remove.length(); i++) + to_return = to_return + to_remove[i]; + return to_return; } \ No newline at end of file