diff --git a/include/duckhandy/implem/scapegoat_tree.inl b/include/duckhandy/implem/scapegoat_tree.inl index 40408d6..6095fc7 100644 --- a/include/duckhandy/implem/scapegoat_tree.inl +++ b/include/duckhandy/implem/scapegoat_tree.inl @@ -31,7 +31,7 @@ namespace duckmem { ///------------------------------------------------------------------------- template ScapegoatTree::~ScapegoatTree() { - DeleteNodes(m_root); + delete_nodes(m_root); #if defined(DUCK_DEBUG) m_root = NULL; m_count = 0xDEADBEEF; @@ -54,12 +54,12 @@ namespace duckmem { ///------------------------------------------------------------------------- template std::pair::iterator, bool> ScapegoatTree::insert (const K& parKey) { - const size_type depthHint = GetMaxBalancedDepth(std::max(m_reserved, m_count + 1), m_alphainvloginv) + 3; + const size_type depthHint = get_max_balanced_depth(std::max(m_reserved, m_count + 1), m_alphainvloginv) + 3; #if defined(SCAPEGOATTREE_VERBOSE) std::cout << "insert(): depthHint = " << depthHint << ", m_count = " << m_count << ", m_countMax = " << m_countMax << ", m_reserved = " << m_reserved << std::endl; #endif if (NULL == m_root) { - m_root = GetNewNode(parKey); + m_root = get_new_node(parKey); m_root->left = m_root->right = NULL; m_root->size = 1; m_count = 1; @@ -79,13 +79,13 @@ namespace duckmem { NodeStack stack; stack.reserve(depthHint); - NodeType* const closestMatch = GetInsertParent(m_root, parKey, stack); + NodeType* const closestMatch = get_insert_parent(m_root, parKey, stack); Assert(stack.size() <= depthHint); if (closestMatch->content == parKey) return std::pair(iterator(stack.begin(), stack.size(), depthHint), false); Assert(NULL == closestMatch->left or closestMatch->left->content < parKey); Assert(NULL == closestMatch->right or parKey < closestMatch->right->content); - NodeType* const newNode = GetNewNode(parKey); + NodeType* const newNode = get_new_node(parKey); newNode->left = newNode->right = NULL; newNode->size = 1; if (parKey < closestMatch->content) { @@ -116,14 +116,14 @@ namespace duckmem { size_type newNodeDepth = stack.size() - 1; //Rebalance if necessary - if (not IsAlphaHeightBalanced(newNodeDepth, this->size())) { + if (not is_alpha_height_balanced(newNodeDepth, this->size())) { #if defined(SCAPEGOATTREE_PARANOID) - Assert(FindMaxDepth(m_root) == newNodeDepth); + Assert(find_max_depth(m_root) == newNodeDepth); #endif - Assert(GetMaxBalancedDepth(static_cast(m_root->size), m_alphainvloginv) + 1 == newNodeDepth); - std::pair scapegoatAndParent = FindScapegoat(stack); + Assert(get_max_balanced_depth(static_cast(m_root->size), m_alphainvloginv) + 1 == newNodeDepth); + std::pair scapegoatAndParent = find_scapegoat(stack); AssertRelease(NULL != scapegoatAndParent.first); - NodeType* const newRoot = Rebalance(scapegoatAndParent.first, newNodeDepth + 1); + NodeType* const newRoot = rebalance(scapegoatAndParent.first, newNodeDepth + 1); NodeType* const parent = scapegoatAndParent.second; if (parent == NULL) { Assert(scapegoatAndParent.first == m_root); @@ -142,14 +142,14 @@ namespace duckmem { } #if defined(SCAPEGOATTREE_PARANOID) AssertNodeSize(m_root); - Assert(FindMaxDepth(m_root) <= GetMaxBalancedDepth(static_cast(m_root->size), m_alphainvloginv)); + Assert(find_max_depth(m_root) <= get_max_balanced_depth(static_cast(m_root->size), m_alphainvloginv)); #endif //Rebuild the stack. //TODO: this is ugly and slow, see if you can find a better way stack.clear(); - GetInsertParent(m_root, parKey, stack); + get_insert_parent(m_root, parKey, stack); newNodeDepth = stack.size() - 1; - Assert(IsAlphaHeightBalanced(newNodeDepth, this->size())); + Assert(is_alpha_height_balanced(newNodeDepth, this->size())); } return std::pair(iterator(stack.begin(), newNodeDepth + 1, duckmath::log2_fast(m_reserved + 1)), true); } @@ -158,26 +158,26 @@ namespace duckmem { ///------------------------------------------------------------------------- ///------------------------------------------------------------------------- template - typename ScapegoatTree::NodeType* ScapegoatTree::Rebalance (NodeType* parSubtree, size_type parDepthHint) { + typename ScapegoatTree::NodeType* ScapegoatTree::rebalance (NodeType* parSubtree, size_type parDepthHint) { #if defined(SCAPEGOATTREE_VERBOSE) std::cout << "Rebalancing subtree" << std::endl; #endif Assert(NULL != parSubtree); - NodeType* const newRoot = Compress_first(parSubtree, parDepthHint); - NodeType* const retVal = Compress(newRoot); + NodeType* const newRoot = compress_first(parSubtree, parDepthHint); + NodeType* const retVal = compress(newRoot); return retVal; } ///------------------------------------------------------------------------- ///------------------------------------------------------------------------- template - typename ScapegoatTree::NodeTypePair ScapegoatTree::FindScapegoat (NodeStack& parParents) const { + typename ScapegoatTree::NodeTypePair ScapegoatTree::find_scapegoat (NodeStack& parParents) const { Assert(not parParents.empty()); const size_type height = parParents.size(); for (size_type z = parParents.size() - 1; z > 0; --z) { NodeType& currNode = *(parParents[z - 1]); Assert(height - z > 0); - if (not IsAlphaHeightBalanced(height - z, static_cast(currNode.size))) { + if (not is_alpha_height_balanced(height - z, static_cast(currNode.size))) { NodeType* parent; if (z == 1) parent = NULL; @@ -194,7 +194,7 @@ namespace duckmem { ///------------------------------------------------------------------------- ///------------------------------------------------------------------------- template - typename ScapegoatTree::NodeType* ScapegoatTree::GetNewNode (const K& parKey) { + typename ScapegoatTree::NodeType* ScapegoatTree::get_new_node (const K& parKey) { return new NodeType(parKey); } @@ -202,7 +202,7 @@ namespace duckmem { ///------------------------------------------------------------------------- template template - T* ScapegoatTree::FindClosestMatch (T* parTree, const K& parKey) { + T* ScapegoatTree::find_closest_match (T* parTree, const K& parKey) { Assert(NULL != parTree); //if (parTree->left and parKey <= parTree->left->content) if (parTree->left and not (parTree->left->content < parKey)) @@ -217,7 +217,7 @@ namespace duckmem { ///------------------------------------------------------------------------- ///------------------------------------------------------------------------- template - typename ScapegoatTree::NodeType* ScapegoatTree::GetInsertParent (NodeType* parTree, const K& parKey, NodeStack& parRewind) { + typename ScapegoatTree::NodeType* ScapegoatTree::get_insert_parent (NodeType* parTree, const K& parKey, NodeStack& parRewind) { Assert(NULL != parTree); NodeType* retVal = parTree; bool goLeft; @@ -243,10 +243,10 @@ namespace duckmem { ///------------------------------------------------------------------------- ///------------------------------------------------------------------------- template - void ScapegoatTree::DeleteNodes (NodeType* parNode) { + void ScapegoatTree::delete_nodes (NodeType* parNode) { if (parNode) { - DeleteNodes(parNode->left); - DeleteNodes(parNode->right); + delete_nodes(parNode->left); + delete_nodes(parNode->right); delete parNode; } } @@ -254,7 +254,7 @@ namespace duckmem { ///------------------------------------------------------------------------- ///------------------------------------------------------------------------- template - bool ScapegoatTree::IsAlphaHeightBalanced (size_type parHeight, size_type parSize) const { + bool ScapegoatTree::is_alpha_height_balanced (size_type parHeight, size_type parSize) const { const float sz = static_cast(parSize); const float ha = std::floor(std::log(sz) * m_alphainvloginv); const float height = static_cast(parHeight); @@ -264,15 +264,15 @@ namespace duckmem { ///------------------------------------------------------------------------- ///Stout/Warren vine to tree. ///This function destroys the links of the given subtree and creates a - ///structure that is suitable for Compress(). Input is treated as if it + ///structure that is suitable for compress(). Input is treated as if it ///was a linked list. ///------------------------------------------------------------------------- template - typename ScapegoatTree::NodeType* ScapegoatTree::Compress_first (NodeType* parFrom, size_type parDepthHint) { + typename ScapegoatTree::NodeType* ScapegoatTree::compress_first (NodeType* parFrom, size_type parDepthHint) { const size_type vineSize = static_cast(parFrom->size); const size_type iteratorDepth = parDepthHint; #if defined(SCAPEGOATTREE_VERBOSE) - std::cout << "Compress_first(): vineSize = " << vineSize << ", iteratorDepth = " << iteratorDepth << std::endl; + std::cout << "compress_first(): vineSize = " << vineSize << ", iteratorDepth = " << iteratorDepth << std::endl; #endif iterator itFirstStep(parFrom, iteratorDepth); @@ -302,7 +302,7 @@ namespace duckmem { NodeWrapper child(&pseudorootMem); size_type iterationsCount; - if (implem::IsPowerOfTwo(vineSize + 1)) + if (implem::is_power_of_two(vineSize + 1)) iterationsCount = (1 << (duckmath::log2_fast(vineSize))) - 1; else iterationsCount = (vineSize - ((1 << duckmath::log2_fast(vineSize)) - 1)); @@ -347,7 +347,7 @@ namespace duckmem { Assert(vineSize == static_cast(pseudorootMem.right->size)); #if defined(SCAPEGOATTREE_PARANOID) AssertNodeSize(pseudorootMem.right); - Assert(FindMaxDepth(pseudorootMem.right) <= vineSize); + Assert(find_max_depth(pseudorootMem.right) <= vineSize); #endif return pseudorootMem.right; } @@ -357,7 +357,7 @@ namespace duckmem { ///Performs the second and subsequent steps for compressing. ///------------------------------------------------------------------------- template - typename ScapegoatTree::NodeType* ScapegoatTree::Compress (NodeType* parFrom) { + typename ScapegoatTree::NodeType* ScapegoatTree::compress (NodeType* parFrom) { Assert(NULL != parFrom); //We don't know if the tree is complete, so let's calculate its depth //rounded up to the nearest complete tree @@ -366,7 +366,7 @@ namespace duckmem { const size_type treeHeight = duckmath::log2_fast(treeSize + (1 << m)); #if defined(SCAPEGOATTREE_PARANOID) - size_type maxDepthInTree = FindMaxDepth(parFrom); + size_type maxDepthInTree = find_max_depth(parFrom); { //We know step 0 of compression has already been done, so on with //the spine size at step 1 (the one we're willing to do) @@ -381,7 +381,7 @@ namespace duckmem { #if defined(SCAPEGOATTREE_VERBOSE) std::cout << "treeSize = " << treeSize << ", vineSize = " << vineSize << ", treeHeight = " << treeHeight << ", manually counted " << countedNodes << " nodes (" << count << ")\n"; #endif - Assert(implem::IsPowerOfTwo(countedNodes + 1)); + Assert(implem::is_power_of_two(countedNodes + 1)); Assert(vineSize == countedNodes); } #endif @@ -392,7 +392,7 @@ namespace duckmem { for (size_type k = 1; k < treeHeight - 1; ++k) { #if defined(SCAPEGOATTREE_VERBOSE) - std::cout << "Compress() step " << k << std::endl; + std::cout << "compress() step " << k << std::endl; #endif //The rebalanced tree takes treeHeight-1 steps. One step has been done //already, so we perform the remaining treeHeight-2 steps. @@ -431,7 +431,7 @@ namespace duckmem { #if defined(SCAPEGOATTREE_PARANOID) AssertNodeSize(retVal); { - const size_type newDepth = FindMaxDepth(retVal); + const size_type newDepth = find_max_depth(retVal); Assert(newDepth <= maxDepthInTree); maxDepthInTree = newDepth; } @@ -446,7 +446,7 @@ namespace duckmem { ///------------------------------------------------------------------------- ///------------------------------------------------------------------------- template - typename ScapegoatTree::size_type ScapegoatTree::AssertNodeSize (const NodeType* parSubtree) { + typename ScapegoatTree::size_type ScapegoatTree::assert_node_size (const NodeType* parSubtree) { Assert(parSubtree); typename NodeType::size_type localSize = 1; @@ -463,7 +463,7 @@ namespace duckmem { ///------------------------------------------------------------------------- ///------------------------------------------------------------------------- template - typename ScapegoatTree::size_type ScapegoatTree::FindMaxDepth_rec (const NodeType* parSubtree) { + typename ScapegoatTree::size_type ScapegoatTree::find_max_depth_rec (const NodeType* parSubtree) { Assert(parSubtree); typename NodeType::size_type depthLeft = 0; typename NodeType::size_type depthRight = 0; @@ -482,7 +482,7 @@ namespace duckmem { ///------------------------------------------------------------------------- ///------------------------------------------------------------------------- template - typename ScapegoatTree::size_type ScapegoatTree::GetMaxBalancedDepth (size_type parSize, float parAlphaInvLogInv) { + typename ScapegoatTree::size_type ScapegoatTree::get_max_balanced_depth (size_type parSize, float parAlphaInvLogInv) { const float ha = std::log(static_cast(parSize)) * parAlphaInvLogInv; return static_cast(ha); } @@ -494,10 +494,10 @@ namespace duckmem { if (NULL == m_root) return iterator(); - const size_type depthHint = GetTreeMinDepthIB(m_reserved, m_alphainvloginv) + 1; + const size_type depthHint = get_tree_min_depth_ib(m_reserved, m_alphainvloginv) + 1; #if defined(SCAPEGOATTREE_PARANOID) - Assert(IsAlphaHeightBalanced(FindMaxDepth(m_root), static_cast(m_root->size))); - Assert(FindMaxDepth(m_root) + 1 <= depthHint); + Assert(is_alpha_height_balanced(find_max_depth(m_root), static_cast(m_root->size))); + Assert(find_max_depth(m_root) + 1 <= depthHint); #endif return iterator(m_root, depthHint + 1); } @@ -509,10 +509,10 @@ namespace duckmem { if (NULL == m_root) return iterator(); - const size_type depthHint = GetTreeMinDepthIB(m_count, m_alphainvloginv) + 1; + const size_type depthHint = get_tree_min_depth_ib(m_count, m_alphainvloginv) + 1; #if defined(SCAPEGOATTREE_PARANOID) - Assert(IsAlphaHeightBalanced(FindMaxDepth(m_root), static_cast(m_root->size))); - Assert(FindMaxDepth(m_root) + 1 <= depthHint); + Assert(is_alpha_height_balanced(find_max_depth(m_root), static_cast(m_root->size))); + Assert(find_max_depth(m_root) + 1 <= depthHint); #endif return const_iterator(m_root, depthHint + 1); } @@ -521,7 +521,7 @@ namespace duckmem { ///Get min tree depth if balanced ///------------------------------------------------------------------------- template - typename ScapegoatTree::size_type ScapegoatTree::GetTreeMinDepthIB (size_type parSize, float parAlphaInvLogInv) { + typename ScapegoatTree::size_type ScapegoatTree::get_tree_min_depth_ib (size_type parSize, float parAlphaInvLogInv) { const float sz = static_cast(parSize); const size_type roundedDownDepthBase2 = duckmath::log2_fast(parSize + 1); const float nodesAtLastLevel = (roundedDownDepthBase2 == 0 ? 0 : static_cast(1 << roundedDownDepthBase2) - 1.0f); @@ -533,7 +533,7 @@ namespace duckmem { ///------------------------------------------------------------------------- template void ScapegoatTree::clear() { - DeleteNodes(m_root); + delete_nodes(m_root); m_count = 0; m_countMax = 0; m_root = NULL; @@ -542,7 +542,7 @@ namespace duckmem { ///------------------------------------------------------------------------- ///------------------------------------------------------------------------- template - typename ScapegoatTree::NodeType* ScapegoatTree::FindIFP (const NodeType* parTree, const K& parKey) { + typename ScapegoatTree::NodeType* ScapegoatTree::find_ifp (const NodeType* parTree, const K& parKey) { while (parTree) { if (parKey < parTree->content) parTree = parTree->left; @@ -557,7 +557,7 @@ namespace duckmem { ///------------------------------------------------------------------------- ///------------------------------------------------------------------------- template - typename ScapegoatTree::NodeType* ScapegoatTree::FindParentIFP (NodeType* parTree, const NodeType* parSearchNodeAddr) { + typename ScapegoatTree::NodeType* ScapegoatTree::find_parent_ifp (NodeType* parTree, const NodeType* parSearchNodeAddr) { if (parTree == parSearchNodeAddr) return NULL; NodeType* parent = parTree; @@ -578,7 +578,7 @@ namespace duckmem { ///get the in-order predecessor. ///------------------------------------------------------------------------- template - typename ScapegoatTree::NodeType* ScapegoatTree::DetachBottomNode (NodeType* parTree, bool parSuccessor) { + typename ScapegoatTree::NodeType* ScapegoatTree::detach_bottom_node (NodeType* parTree, bool parSuccessor) { AssertRelease(NULL != parTree); AssertRelease(NULL != parTree->left and NULL != parTree->right); if (parSuccessor) { @@ -614,7 +614,7 @@ namespace duckmem { ///------------------------------------------------------------------------- ///------------------------------------------------------------------------- template - bool ScapegoatTree::Include (const K& parSearch) const { + bool ScapegoatTree::include (const K& parSearch) const { const NodeType* const found = FindIFP(m_root, parSearch); Assert(not found or not (parSearch < found->content or found->content < parSearch)); return static_cast(NULL != found); @@ -623,12 +623,12 @@ namespace duckmem { ///------------------------------------------------------------------------- ///------------------------------------------------------------------------- template - void ScapegoatTree::RebalanceAfterDeletionIFN() { + void ScapegoatTree::rebalance_after_deletion_ifn() { const float sz = static_cast(m_count); const float m = static_cast(m_countMax); if (sz < m_alpha * m) { const size_type sizeHint = static_cast(std::ceil(std::log(sz) * m_alphainvloginv)) + 1; - Rebalance(m_root, sizeHint); + rebalance(m_root, sizeHint); m_countMax = m_count; } } @@ -669,7 +669,7 @@ namespace duckmem { } --m_count; delete dele; - RebalanceAfterDeletionIFN(); + rebalance_after_deletion_ifn(); } // ///------------------------------------------------------------------------- diff --git a/include/duckhandy/scapegoat_tree.hpp b/include/duckhandy/scapegoat_tree.hpp index e7b3413..362e07a 100644 --- a/include/duckhandy/scapegoat_tree.hpp +++ b/include/duckhandy/scapegoat_tree.hpp @@ -31,7 +31,7 @@ namespace duckmem { namespace implem { [[gnu::pure]] - inline bool IsPowerOfTwo (unsigned int parValue) { + inline bool is_power_of_two (unsigned int parValue) { return (parValue != 0 and (parValue bitand (~parValue + 1)) == parValue); } } //namespace implem @@ -88,8 +88,8 @@ namespace duckmem { explicit ScapegoatTree ( float parAlpha ); ~ScapegoatTree ( void ); - float GetAlpha ( void ) const { return m_alpha; } - bool Include ( const K& parSearch ) const; + float get_alpha ( void ) const { return m_alpha; } + bool include ( const K& parSearch ) const; std::pair insert ( const K& parKey ); iterator insert ( const iterator&, const K& parValue ); bool empty ( void ) const { return m_count == 0; } @@ -108,26 +108,26 @@ namespace duckmem { private: template - static T* FindClosestMatch ( T* parTree, const K& parKey ); - static NodeType* GetInsertParent ( NodeType* parTree, const K& parKey, NodeStack& parRewind ); - static NodeType* FindIFP ( const NodeType* parTree, const K& parKey ); - static NodeType* FindParentIFP ( NodeType* parTree, const NodeType* parSearchNodeAddr ); - static NodeType* GetNewNode ( const K& parKey ); - static void DeleteNodes ( NodeType* parNode ); - [[gnu::pure]] bool IsAlphaHeightBalanced ( size_type parHeight, size_type parSize ) const; - NodeTypePair FindScapegoat ( NodeStack& parParents ) const; - static NodeType* Rebalance ( NodeType* parSubtree, size_type parDepthHint ); - static NodeType* Compress_first ( NodeType* parFrom, size_type parDepthHint ); - static NodeType* Compress ( NodeType* parFrom ); - [[gnu::pure]] static size_type GetMaxBalancedDepth ( size_type parSize, float parAlphaInvLogInv ); - [[gnu::pure]] static size_type GetTreeMinDepthIB ( size_type parSize, float parAlphaInvLogInv ); - static NodeType* DetachBottomNode ( NodeType* parTree, bool parSuccessor ); - void RebalanceAfterDeletionIFN ( void ); + static T* find_closest_match ( T* parTree, const K& parKey ); + static NodeType* get_insert_parent ( NodeType* parTree, const K& parKey, NodeStack& parRewind ); + static NodeType* find_ifp ( const NodeType* parTree, const K& parKey ); + static NodeType* find_parent_ifp ( NodeType* parTree, const NodeType* parSearchNodeAddr ); + static NodeType* get_new_node ( const K& parKey ); + static void delete_nodes ( NodeType* parNode ); + [[gnu::pure]] bool is_alpha_height_balanced ( size_type parHeight, size_type parSize ) const; + NodeTypePair find_scapegoat ( NodeStack& parParents ) const; + static NodeType* rebalance ( NodeType* parSubtree, size_type parDepthHint ); + static NodeType* compress_first ( NodeType* parFrom, size_type parDepthHint ); + static NodeType* compress ( NodeType* parFrom ); + [[gnu::pure]] static size_type get_max_balanced_depth ( size_type parSize, float parAlphaInvLogInv ); + [[gnu::pure]] static size_type get_tree_min_depth_ib ( size_type parSize, float parAlphaInvLogInv ); + static NodeType* detach_bottom_node ( NodeType* parTree, bool parSuccessor ); + void rebalance_after_deletion_ifn ( void ); #if defined(SCAPEGOATTREE_PARANOID) - static size_type AssertNodeSize ( const NodeType* parSubtree ); - static size_type FindMaxDepth ( const NodeType* parSubtree ) { return FindMaxDepth_rec(parSubtree) - 1; } - static size_type FindMaxDepth_rec ( const NodeType* parSubtree ); + static size_type assert_node_size ( const NodeType* parSubtree ); + static size_type find_max_depth ( const NodeType* parSubtree ) { return FindMaxDepth_rec(parSubtree) - 1; } + static size_type find_max_depth_rec ( const NodeType* parSubtree ); #endif NodeType* m_root;