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);
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);
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<Node*>(n->getParentNode())->key;
return static_cast<Node*>(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<void*>* oldParent = targetNode->parent;
TreeNode<void*>* oldParent = targetNode->parent;
for (S32 i = 0; i < oldParent->size(); i++) {
if ((*oldParent)[i] == targetNode) {
oldParent->erase(i);
@ -222,7 +222,7 @@ Vector<S32> TreeObject::getChildren(S32 key)
Node* n = findNode(key);
if (n)
{
Vector<Tree<void*>*> children = n->getChildrenNodes();
Vector<TreeNode<void*>*> children = n->getChildren();
keys.reserve(children.size());
for (U32 i = 0; i < children.size(); i++)
keys.push_back(static_cast<Node*>(children[i])->key);
@ -236,7 +236,7 @@ Vector<S32> TreeObject::getSiblings(S32 key)
Node* n = findNode(key);
if (n)
{
Vector<Tree<void*>*> siblings = n->getSiblingsNodes();
Vector<TreeNode<void*>*> siblings = n->getSiblings();
keys.reserve(siblings.size());
for (U32 i = 0; i < siblings.size(); i++)
keys.push_back(static_cast<Node*>(siblings[i])->key);

View file

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