66 RemoveStruct (std::size_t s)
67 : size(s), node(
nullptr)
97 add (
void* ptr, std::size_t size)
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];
117 root = stack[0] = m_root;
119 for (depth = 0; ; ++depth)
123 setParent (node, stack[depth - 1]);
124 node->left =
nullptr;
125 node->right =
nullptr;
126 node->next =
nullptr;
129 root = balance (stack, depth);
137 if (size < root->size)
139 root = stack[depth + 1] = root->left;
142 else if (size > root->size)
144 root = stack[depth + 1] = root->right;
152 Node* next = root->next;
183 remove (std::size_t& size, std::size_t limit = ~
static_cast<std::size_t
> (0))
185 Node* stack[
sizeof(Node*) * 8 + 1];
187 Node* candidate =
nullptr;
188 std::int16_t depth = 0;
189 std::int16_t candidateDepth = 0;
191 while (root !=
nullptr)
195 if (size <= root->size)
198 candidateDepth = depth;
208 if (candidate ==
nullptr || candidate->size >= limit)
216 Node* next = candidate->next;
217 candidate->next = next->next;
218 if (candidate->next !=
nullptr)
220 candidate->next->left = candidate;
227 size = candidate->size;
228 removeNode (candidate, stack, candidateDepth);
241 Node* node =
reinterpret_cast<Node*
>(ptr);
243 if (node->height == -1)
246 Node* prev = node->left;
247 Node* next = node->next;
261 Node* stack[
sizeof(Node*) * 8 + 1];
264 std::size_t size = node->size;
266 COOKMEM_ASSERT(m_root !=
nullptr);
268 root = stack[0] = m_root;
270 for (depth = 0; ; ++depth)
277 if (size < root->size)
279 root = stack[depth + 1] = root->left;
282 else if (size > root->size)
284 root = stack[depth + 1] = root->right;
291 COOKMEM_ASSERT(root == node);
297 Node* next = root->next;
298 next->left = root->left;
299 next->right = root->right;
300 next->height = root->height;
308 setParent (next, stack[depth - 1]);
313 removeNode (root, stack, depth);
325 if (m_root ==
nullptr)
330 Node* node =
reinterpret_cast<Node*
>(ptr);
331 const std::size_t size = node->size;
334 while (root !=
nullptr)
336 if (size < root->size)
340 else if (size > root->size)
354 while (root !=
nullptr);
374 Node* node =
reinterpret_cast<Node*
>(ptr);
377 if (node->height == -1 || node->next !=
nullptr)
385 std::size_t size = node->size;
386 Node* nextLarger =
nullptr;
394 if (size < root->size)
405 if (node->right !=
nullptr)
407 nextLarger = node->right;
408 while (nextLarger->left !=
nullptr)
410 nextLarger = nextLarger->left;
414 return nextLarger ==
nullptr || newSize < nextLarger->size;
425 return m_root ==
nullptr;
434 printf (
"graph G {\n");
439 inline static std::int16_t
440 getMax (std::int16_t a, std::int16_t b)
442 return (a < b) ? b : a;
445 inline static std::int16_t
446 getHeight (
const Node* node)
448 return node ? node->height : 0;
452 updateHeight (Node* node)
454 node->height = getMax (getHeight (node->left), getHeight (node->right)) + 1;
458 setParent (Node* node, Node* parent)
460 if (node->size < parent->size)
466 parent->right = node;
471 setLeftChild (Node* root, Node* left)
477 setRightChild (Node* root, Node* right)
483 balance (Node** stack, std::int16_t depth)
488 Node* root = stack[depth];
489 Node* left = root->left;
490 Node* right = root->right;
492 std::int16_t oldHeight = root->height;
493 int16_t diff = getHeight (left) - getHeight (right);
496 if (getHeight (left->left) >= getHeight (left->right))
498 root = rotateWithLeftChild (root);
502 root = doubleWithLeftChild (root);
509 setParent (root, stack[depth - 1]);
514 if (getHeight (right->right) >= getHeight (right->left))
516 root = rotateWithRightChild (root);
520 root = doubleWithRightChild (root);
527 setParent (root, stack[depth - 1]);
540 if (oldHeight == root->height)
547 rotateWithLeftChild (Node* root)
549 Node* left = root->left;
550 setLeftChild (root, left->right);
551 setRightChild (left, root);
558 rotateWithRightChild (Node* root)
560 Node* right = root->right;
561 setRightChild (root, right->left);
562 setLeftChild (right, root);
564 updateHeight (right);
569 doubleWithLeftChild (Node* root)
571 setLeftChild (root, rotateWithRightChild (root->left));
572 return rotateWithLeftChild (root);
576 doubleWithRightChild (Node* root)
578 setRightChild (root, rotateWithLeftChild (root->right));
579 return rotateWithRightChild (root);
583 printNode (Node* root)
587 printf (
"%016zx -- {", (std::size_t)root);
590 printf (
" %016zx", (std::size_t)root->left);
594 printf (
" %016zx", (std::size_t)root->right);
597 printf (
"%016zx [ label = %zd ]\n",
600 printNode (root->left);
601 printNode (root->right);
605 removeNode (Node* root, Node** stack, std::int16_t depth)
607 Node* left = root->left;
608 Node* right = root->right;
615 else if (right ==
nullptr)
624 std::int16_t newDepth;
625 Node* newStack[
sizeof (Node*) * 8 + 1];
627 for (newDepth = 0; newStack[newDepth]->left !=
nullptr; ++newDepth)
629 newStack[newDepth + 1] = newStack[newDepth]->left;
631 Node* min = newStack[newDepth];
635 setLeftChild (newRoot, left);
636 updateHeight (newRoot);
641 Node* parent = newStack[newDepth - 1];
642 parent->left = min->right;
645 Node* newRight = balance (newStack, newDepth);
646 if (newRight !=
nullptr)
651 setLeftChild (min, left);
652 setRightChild (min, right);
662 if (newRoot !=
nullptr)
665 newRoot = balance (stack, 1);
666 if (newRoot !=
nullptr)
673 if (newRoot ==
nullptr)
676 if (root->size < stack[depth - 1]->size)
678 stack[depth - 1]->left =
nullptr;
682 stack[depth - 1]->right =
nullptr;
684 newRoot = balance (stack, depth);
685 if (newRoot !=
nullptr)
692 setParent (newRoot, stack[depth - 1]);
694 stack[depth] = newRoot;
695 newRoot = balance (stack,
static_cast<std::int16_t
> (depth + 1));
697 if (newRoot !=
nullptr)