66 RemoveStruct (std::size_t
s)
67 : size(
s), node(
nullptr)
99 Node* node =
reinterpret_cast<Node*
>(
ptr);
102 if (m_root ==
nullptr)
104 node->left =
nullptr;
105 node->right =
nullptr;
106 node->next =
nullptr;
114 Node*
stack[
sizeof(Node*) * 8 + 1];
124 node->left =
nullptr;
125 node->right =
nullptr;
126 node->next =
nullptr;
142 else if (size >
root->size)
152 Node* next =
root->next;
184 remove (std::size_t& size, std::size_t
limit = ~
static_cast<std::size_t
> (0))
186 Node*
stack[
sizeof(Node*) * 8 + 1];
189 std::int16_t
depth = 0;
192 while (
root !=
nullptr)
242 Node* node =
reinterpret_cast<Node*
>(
ptr);
244 if (node->height == -1)
247 Node* prev = node->left;
248 Node* next = node->next;
262 Node*
stack[
sizeof(Node*) * 8 + 1];
265 std::size_t size = node->size;
267 COOKMEM_ASSERT(m_root !=
nullptr);
283 else if (size >
root->size)
292 COOKMEM_ASSERT(
root == node);
298 Node* next =
root->next;
299 next->left =
root->left;
300 next->right =
root->right;
301 next->height =
root->height;
326 if (m_root ==
nullptr)
331 Node* node =
reinterpret_cast<Node*
>(
ptr);
332 const std::size_t size = node->size;
335 while (
root !=
nullptr)
341 else if (size >
root->size)
355 while (
root !=
nullptr);
375 Node* node =
reinterpret_cast<Node*
>(
ptr);
378 if (node->height == -1 || node->next !=
nullptr)
386 std::size_t size = node->size;
406 if (node->right !=
nullptr)
426 return m_root ==
nullptr;
440 inline static std::int16_t
441 getMax (std::int16_t
a, std::int16_t
b)
443 return (
a <
b) ?
b :
a;
446 inline static std::int16_t
447 getHeight (
const Node* node)
449 return node ? node->height : 0;
453 updateHeight (Node* node)
455 node->height = getMax (getHeight (node->left), getHeight (node->right)) + 1;
459 setParent (Node* node, Node* parent)
461 if (node->size < parent->size)
467 parent->right = node;
472 setLeftChild (Node* root, Node* left)
478 setRightChild (Node* root, Node* right)
484 balance (Node** stack, std::int16_t depth)
489 Node* root = stack[depth];
490 Node* left = root->left;
491 Node* right = root->right;
493 std::int16_t oldHeight = root->height;
494 int16_t diff = getHeight (left) - getHeight (right);
497 if (getHeight (left->left) >= getHeight (left->right))
499 root = rotateWithLeftChild (root);
503 root = doubleWithLeftChild (root);
510 setParent (root, stack[depth - 1]);
515 if (getHeight (right->right) >= getHeight (right->left))
517 root = rotateWithRightChild (root);
521 root = doubleWithRightChild (root);
528 setParent (root, stack[depth - 1]);
541 if (oldHeight == root->height)
548 rotateWithLeftChild (Node* root)
550 Node* left = root->left;
551 setLeftChild (root, left->right);
552 setRightChild (left, root);
559 rotateWithRightChild (Node* root)
561 Node* right = root->right;
562 setRightChild (root, right->left);
563 setLeftChild (right, root);
565 updateHeight (right);
570 doubleWithLeftChild (Node* root)
572 setLeftChild (root, rotateWithRightChild (root->left));
573 return rotateWithLeftChild (root);
577 doubleWithRightChild (Node* root)
579 setRightChild (root, rotateWithLeftChild (root->right));
580 return rotateWithRightChild (root);
584 printNode (Node* root)
588 printf (
"%016zx -- {", (std::size_t)root);
591 printf (
" %016zx", (std::size_t)root->left);
595 printf (
" %016zx", (std::size_t)root->right);
598 printf (
"%016zx [ label = %zd ]\n",
601 printNode (root->left);
602 printNode (root->right);
606 removeNode (Node* root, Node** stack,
int depth)
608 Node* left = root->left;
609 Node* right = root->right;
616 else if (right ==
nullptr)
625 std::int16_t newDepth;
626 Node* newStack[
sizeof (Node*) * 8 + 1];
628 for (newDepth = 0; newStack[newDepth]->left !=
nullptr; ++newDepth)
630 newStack[newDepth + 1] = newStack[newDepth]->left;
632 Node* min = newStack[newDepth];
636 setLeftChild (newRoot, left);
637 updateHeight (newRoot);
642 Node* parent = newStack[newDepth - 1];
643 parent->left = min->right;
646 Node* newRight = balance (newStack, newDepth);
647 if (newRight !=
nullptr)
652 setLeftChild (min, left);
653 setRightChild (min, right);
663 if (newRoot !=
nullptr)
666 newRoot = balance (stack, 1);
667 if (newRoot !=
nullptr)
674 if (newRoot ==
nullptr)
677 if (root->size < stack[depth - 1]->size)
679 stack[depth - 1]->left =
nullptr;
683 stack[depth - 1]->right =
nullptr;
685 newRoot = balance (stack, depth);
686 if (newRoot !=
nullptr)
693 setParent (newRoot, stack[depth - 1]);
695 stack[depth] = newRoot;
696 newRoot = balance (stack, depth + 1);
698 if (newRoot !=
nullptr)
Definition allocator.h:39