baseline node augs

tree->treenode for clarity, foreshorted a few method names, added a few inline utility methods
This commit is contained in:
AzaezelX 2026-01-25 13:18:01 -06:00
parent b692aa14c2
commit c72d8a1f9b
2 changed files with 53 additions and 22 deletions

View file

@ -140,7 +140,7 @@ void TreeObject::deleteNode(S32 key)
Node* parentNode = static_cast<Node*>(node->parent); Node* parentNode = static_cast<Node*>(node->parent);
for (S32 i = 0; i < parentNode->size(); i++) for (S32 i = 0; i < parentNode->size(); i++)
{ {
if ((*parentNode)[i] == static_cast<Tree<void*>*>(node)) if ((*parentNode)[i] == static_cast<TreeNode<void*>*>(node))
{ {
parentNode->erase(i); parentNode->erase(i);
break; break;
@ -181,7 +181,7 @@ S32 TreeObject::getParent(S32 key)
Node* n = findNode(key); Node* n = findNode(key);
if (n == NULL || n->parent == NULL) if (n == NULL || n->parent == NULL)
return -1; return -1;
return static_cast<Node*>(n->getParentNode())->key; return static_cast<Node*>(n->getParent())->key;
} }
bool TreeObject::toParent(S32 key, S32 newParentKey) bool TreeObject::toParent(S32 key, S32 newParentKey)
@ -201,7 +201,7 @@ bool TreeObject::toParent(S32 key, S32 newParentKey)
} }
if (targetNode->parent) if (targetNode->parent)
{ {
Tree<void*>* oldParent = targetNode->parent; TreeNode<void*>* oldParent = targetNode->parent;
for (S32 i = 0; i < oldParent->size(); i++) { for (S32 i = 0; i < oldParent->size(); i++) {
if ((*oldParent)[i] == targetNode) { if ((*oldParent)[i] == targetNode) {
oldParent->erase(i); oldParent->erase(i);
@ -222,7 +222,7 @@ Vector<S32> TreeObject::getChildren(S32 key)
Node* n = findNode(key); Node* n = findNode(key);
if (n) if (n)
{ {
Vector<Tree<void*>*> children = n->getChildrenNodes(); Vector<TreeNode<void*>*> children = n->getChildren();
keys.reserve(children.size()); keys.reserve(children.size());
for (U32 i = 0; i < children.size(); i++) for (U32 i = 0; i < children.size(); i++)
keys.push_back(static_cast<Node*>(children[i])->key); keys.push_back(static_cast<Node*>(children[i])->key);
@ -236,7 +236,7 @@ Vector<S32> TreeObject::getSiblings(S32 key)
Node* n = findNode(key); Node* n = findNode(key);
if (n) if (n)
{ {
Vector<Tree<void*>*> siblings = n->getSiblingsNodes(); Vector<TreeNode<void*>*> siblings = n->getSiblings();
keys.reserve(siblings.size()); keys.reserve(siblings.size());
for (U32 i = 0; i < siblings.size(); i++) for (U32 i = 0; i < siblings.size(); i++)
keys.push_back(static_cast<Node*>(siblings[i])->key); keys.push_back(static_cast<Node*>(siblings[i])->key);

View file

@ -25,59 +25,90 @@
#include "core/util/tDictionary.h" #include "core/util/tDictionary.h"
#include "console/consoleTypes.h" #include "console/consoleTypes.h"
template <class T> template <class T>
class Tree : public Vector<Tree<T>*> { class TreeNode : public Vector<TreeNode<T>*> {
public: public:
T data; T data;
Tree<T>* parent; TreeNode<T>* parent;
TreeNode(const T& val = T(), TreeNode<T>* 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<T>* p = NULL) : data(val), parent(p) {} //parent logic
virtual ~Tree() { clear();} inline TreeNode<T>* getParent() const { return parent; }
inline void setParent(TreeNode<T>* p)
{
if (parent) parent->remove(this);
parent = p;
parent->push_back(this);
}
Tree<T>* addChild(const T& val) { //children logic
Tree<T>* child = new Tree<T>(val, this); inline TreeNode<T>* addChild(const T& val) {
TreeNode<T>* child = new TreeNode<T>(val, this);
push_back(child); push_back(child);
return child; return child;
} }
inline void addChild(TreeNode<T>* child) {
void addChild(Tree<T>* child) {
if (child) { if (child) {
child->parent = this; child->parent = this;
push_back(child); push_back(child);
} }
} }
inline void addChildren(const Vector<T>* children) {
if (!children) return;
for (U32 i = 0; i < children->size(); i++) {
TreeNode<T>* child = new TreeNode<T>((*children)[i], this);
push_back(child);
}
}
void operator =(Vector<T>* other) { clear(); addChildren(other); }
Tree<T>* getParentNode() const { return parent; } inline U32 getNumChildren() const { return size(); }
inline bool hasChildren() const { return size() > 0; }
Vector<Tree<T>*> getChildrenNodes() const { inline Vector<TreeNode<T>*> getChildren() const {
Vector<Tree<T>*> children; Vector<TreeNode<T>*> children;
children.reserve(size()); children.reserve(size());
for (U32 i = 0; i < size(); i++) for (U32 i = 0; i < size(); i++)
children.push_back((*this)[i]); children.push_back((*this)[i]);
return children; return children;
} }
inline void deleteChildren()
{
for (U32 i = 0; i < size(); i++)
delete (*this)[i];
clear();
}
Vector<Tree<T>*> getSiblingsNodes() const { //sibling logic
Vector<Tree<T>*> siblings; inline Vector<TreeNode<T>*> getSiblings() const {
Vector<TreeNode<T>*> siblings;
if (!parent) return siblings; if (!parent) return siblings;
siblings.reserve(parent->size() - 1); siblings.reserve(parent->size() - 1);
for (U32 i = 0; i < parent->size(); i++) { for (U32 i = 0; i < parent->size(); i++) {
Tree<T>* sibling = (*parent)[i]; TreeNode<T>* sibling = (*parent)[i];
if (sibling != this) if (sibling != this)
siblings.push_back(sibling); siblings.push_back(sibling);
} }
return siblings; 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 { class TreeObject : public SimObject {
typedef SimObject Parent; typedef SimObject Parent;
public: public:
struct Node : public Tree<void*> { struct Node : public TreeNode<void*> {
S32 key; S32 key;
ConsoleBaseType* type; ConsoleBaseType* type;
Node(S32 k, ConsoleBaseType* t) : Tree<void*>(NULL, NULL), key(k), type(t) {} Node(S32 k, ConsoleBaseType* t) : TreeNode<void*>(NULL, NULL), key(k), type(t) {}
~Node() { ~Node() {
if (data) if (data)
{ {