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