From 8afddd3bdbde640a01f3e505d1d391c13dca4059 Mon Sep 17 00:00:00 2001 From: disinvite Date: Sat, 29 Jul 2023 18:34:18 -0400 Subject: [PATCH] Few more cleanup items before opening up to comments * Tried a "Find" method to reduce repeated code between MxAtomId::Destroy and TreeAdd functions * Renamed the "Successor" function * There is a destructor for the RB tree. I started on one of the companion functions but it is unfinished here. --- LEGO1/mxatomid.cpp | 32 +++++++------------------------- LEGO1/mxbinarytree.cpp | 29 ++++++++++++++++++++++++++--- LEGO1/mxbinarytree.h | 15 ++++++++++++++- 3 files changed, 47 insertions(+), 29 deletions(-) diff --git a/LEGO1/mxatomid.cpp b/LEGO1/mxatomid.cpp index 02f33236..b5c03eb9 100644 --- a/LEGO1/mxatomid.cpp +++ b/LEGO1/mxatomid.cpp @@ -37,16 +37,8 @@ void MxAtomId::Destroy() TreeValue *p = &value; // 100ad052 - MxBinaryTree *tree = AtomIdTree(); - TreeNode *root = tree->m_root; - // should inline Search but NOT TreeValueCompare - TreeNode *node = tree->Search(p); - - TreeNode *ass = node; - if (node == root->m_parent || TreeValueCompare(p, node->m_value)) { - ass = root->m_parent; - } + TreeNode *node = AtomIdTree()->Find(p); node->m_value->RefCountDec(); } @@ -71,7 +63,6 @@ MxAtomId &MxAtomId::operator=(const MxAtomId &atomId) TreeValue *MxAtomId::try_to_open(const char *p_str, LookupMode p_mode) { TreeValue *value = new TreeValue(p_str); - TreeNode *node; switch (p_mode) { case LookupMode_LowerCase: @@ -84,22 +75,13 @@ TreeValue *MxAtomId::try_to_open(const char *p_str, LookupMode p_mode) } // LAB_100ad2a1 - MxBinaryTree *tree = AtomIdTree(); - // get the closest node that matches the given value - // should NOT inline - node = tree->Search(value); + TreeNode *node = AtomIdTree()->Find(value); - TreeNode *t; - - // is the node an exact match? - if (tree->m_root == node || TreeValueCompare(value, node->m_value)) { - t = tree->m_root; - } else { - t = node; - } - - if (t->m_child0 != AtomIdTree()->m_root) { - delete value; + MxBinaryTree *tree = AtomIdTree(); + if (node->m_child0 != tree->m_root) { + // unnecessary check? + if (value) + delete value; value = node->m_value; } else { // repeat? diff --git a/LEGO1/mxbinarytree.cpp b/LEGO1/mxbinarytree.cpp index 737cc5d2..abfb5a36 100644 --- a/LEGO1/mxbinarytree.cpp +++ b/LEGO1/mxbinarytree.cpp @@ -3,11 +3,13 @@ // 0x101013f0 TreeNode *MxBinaryTree::g_Node_Nil = NULL; +/* // OFFSET: LEGO1 0x100ad170 TreeValue::~TreeValue() { // nothing. } +*/ inline void MxBinaryTree::RightRotate(TreeNode *x) { @@ -76,8 +78,9 @@ inline TreeNode *minimum(TreeNode *p_node) return p_node; } + // OFFSET: LEGO1 0x100ad480 -void mini_walk(TreeNode* &p_node) +void Successor(TreeNode* &p_node) { // I think this is checking whether this is the tree "root" node // i.e. MxBinaryTree->m_root @@ -91,7 +94,6 @@ void mini_walk(TreeNode* &p_node) return; } - // successor? if (p_node->m_child0 != MxBinaryTree::g_Node_Nil) { p_node = minimum(p_node->m_child0); return; @@ -103,9 +105,9 @@ void mini_walk(TreeNode* &p_node) p_node = y; y = y->m_parent; } - } + // OFFSET: LEGO1 0x100ad4d0 void MxBinaryTree::Insert(TreeNode **p_output, TreeNode *p_leaf, TreeNode *p_parent, TreeValue *&p_value) { @@ -232,3 +234,24 @@ void TreeValue::RefCountDec() if (m_t0) m_t0--; } + +// OFFSET: LEGO1 0x100af6d0 STUB +MxBinaryTree::~MxBinaryTree() +{ +} + +// OFFSET: LEGO1 0x100af7a0 +void somethingWithNode(TreeNode*& p_node) +{ + // TODO + if (p_node->m_child1 != MxBinaryTree::g_Node_Nil) { + p_node = minimum(p_node->m_child1); + return; + } + + while (p_node->m_parent->m_child0 == p_node) + p_node = p_node->m_parent; + + if (p_node->m_child1 == p_node->m_parent) + p_node = p_node->m_parent; +} diff --git a/LEGO1/mxbinarytree.h b/LEGO1/mxbinarytree.h index 75bd064b..37d6851e 100644 --- a/LEGO1/mxbinarytree.h +++ b/LEGO1/mxbinarytree.h @@ -15,7 +15,6 @@ class TreeValue { m_str = p_str; m_t0 = 0; } - ~TreeValue(); void RefCountInc(); void RefCountDec(); @@ -71,11 +70,13 @@ class MxBinaryTree m_root = newTreeNode(g_Node_Nil, NODE_COLOR_RED); } + ~MxBinaryTree(); void LeftRotate(TreeNode *); void RightRotate(TreeNode *); void Insert(TreeNode **, TreeNode *, TreeNode *, TreeValue *&); TreeNode *Search(TreeValue *&); + TreeNode *Find(TreeValue *&); int m_p0; // +0 TreeNode *m_root; // +4 @@ -83,4 +84,16 @@ class MxBinaryTree int m_nodeCount; // +c }; +inline TreeNode *MxBinaryTree::Find(TreeValue *&p_value) +{ + TreeNode *node = Search(p_value); + + // we should only get the root back if the tree is empty. + if (m_root == node || TreeValueCompare(p_value, node->m_value)) { + node = m_root; // ?? + } + + return node->m_child0; +} + #endif //MXBINARYTREE_H \ No newline at end of file