diff --git a/Engine/source/core/util/treeObject.cpp b/Engine/source/core/util/treeObject.cpp index 4bc905092..18f89a212 100644 --- a/Engine/source/core/util/treeObject.cpp +++ b/Engine/source/core/util/treeObject.cpp @@ -140,7 +140,7 @@ void TreeObject::deleteNode(S32 key) Node* parentNode = static_cast(node->parent); for (S32 i = 0; i < parentNode->size(); i++) { - if ((*parentNode)[i] == static_cast*>(node)) + if ((*parentNode)[i] == static_cast*>(node)) { parentNode->erase(i); break; @@ -181,7 +181,7 @@ S32 TreeObject::getParent(S32 key) Node* n = findNode(key); if (n == NULL || n->parent == NULL) return -1; - return static_cast(n->getParentNode())->key; + return static_cast(n->getParent())->key; } bool TreeObject::toParent(S32 key, S32 newParentKey) @@ -201,7 +201,7 @@ bool TreeObject::toParent(S32 key, S32 newParentKey) } if (targetNode->parent) { - Tree* oldParent = targetNode->parent; + TreeNode* oldParent = targetNode->parent; for (S32 i = 0; i < oldParent->size(); i++) { if ((*oldParent)[i] == targetNode) { oldParent->erase(i); @@ -222,7 +222,7 @@ Vector TreeObject::getChildren(S32 key) Node* n = findNode(key); if (n) { - Vector*> children = n->getChildrenNodes(); + Vector*> children = n->getChildren(); keys.reserve(children.size()); for (U32 i = 0; i < children.size(); i++) keys.push_back(static_cast(children[i])->key); @@ -236,7 +236,7 @@ Vector TreeObject::getSiblings(S32 key) Node* n = findNode(key); if (n) { - Vector*> siblings = n->getSiblingsNodes(); + Vector*> siblings = n->getSiblings(); keys.reserve(siblings.size()); for (U32 i = 0; i < siblings.size(); i++) keys.push_back(static_cast(siblings[i])->key); diff --git a/Engine/source/core/util/treeObject.h b/Engine/source/core/util/treeObject.h index 13987671b..5723b0827 100644 --- a/Engine/source/core/util/treeObject.h +++ b/Engine/source/core/util/treeObject.h @@ -25,59 +25,90 @@ #include "core/util/tDictionary.h" #include "console/consoleTypes.h" template -class Tree : public Vector*> { +class TreeNode : public Vector*> { public: T data; - Tree* parent; + TreeNode* parent; + TreeNode(const T& val = T(), TreeNode* p = NULL) : data(val), parent(p){} + virtual ~TreeNode() { clear();} + //description logic + inline bool isRoot() const { return parent == NULL; } + inline bool isLeaf() const { return size() == 0; } - Tree(const T& val = T(), Tree* p = NULL) : data(val), parent(p) {} - virtual ~Tree() { clear();} + //parent logic + inline TreeNode* getParent() const { return parent; } + inline void setParent(TreeNode* p) + { + if (parent) parent->remove(this); + parent = p; + parent->push_back(this); + } - Tree* addChild(const T& val) { - Tree* child = new Tree(val, this); + //children logic + inline TreeNode* addChild(const T& val) { + TreeNode* child = new TreeNode(val, this); push_back(child); return child; } - - void addChild(Tree* child) { + inline void addChild(TreeNode* child) { if (child) { child->parent = this; push_back(child); } } + inline void addChildren(const Vector* children) { + if (!children) return; + for (U32 i = 0; i < children->size(); i++) { + TreeNode* child = new TreeNode((*children)[i], this); + push_back(child); + } + } + void operator =(Vector* other) { clear(); addChildren(other); } - Tree* getParentNode() const { return parent; } - - Vector*> getChildrenNodes() const { - Vector*> children; + inline U32 getNumChildren() const { return size(); } + inline bool hasChildren() const { return size() > 0; } + inline Vector*> getChildren() const { + Vector*> children; children.reserve(size()); for (U32 i = 0; i < size(); i++) children.push_back((*this)[i]); return children; } + inline void deleteChildren() + { + for (U32 i = 0; i < size(); i++) + delete (*this)[i]; + clear(); + } - Vector*> getSiblingsNodes() const { - Vector*> siblings; + //sibling logic + inline Vector*> getSiblings() const { + Vector*> siblings; if (!parent) return siblings; siblings.reserve(parent->size() - 1); for (U32 i = 0; i < parent->size(); i++) { - Tree* sibling = (*parent)[i]; + TreeNode* sibling = (*parent)[i]; if (sibling != this) siblings.push_back(sibling); } return siblings; } + + //data logic + inline T getData() const { return data; } + inline void setData(const T& val) { data = val; } + inline void operator =(const T& val) { data = val; } }; class TreeObject : public SimObject { typedef SimObject Parent; public: - struct Node : public Tree { + struct Node : public TreeNode { S32 key; ConsoleBaseType* type; - Node(S32 k, ConsoleBaseType* t) : Tree(NULL, NULL), key(k), type(t) {} + Node(S32 k, ConsoleBaseType* t) : TreeNode(NULL, NULL), key(k), type(t) {} ~Node() { if (data) {