11 template <
typename T,
typename TCompare>
12 template <
class InputIterator>
16 for (
auto it = first; it != last; ++it)
20 template <
typename T,
typename TCompare>
23 return (T*)InternalLowest();
26 template <
typename T,
typename TCompare>
29 return InternalLowest();
32 template <
typename T,
typename TCompare>
35 const T* result = _root;
36 if (result !=
nullptr)
37 while (result->left !=
nullptr)
38 result = result->left;
42 template <
typename T,
typename TCompare>
45 return (T*)InternalHighest();
48 template <
typename T,
typename TCompare>
51 return InternalHighest();
54 template <
typename T,
typename TCompare>
57 const T* result = _root;
58 if (result !=
nullptr)
59 while (result->right !=
nullptr)
60 result = result->right;
64 template <
typename T,
typename TCompare>
70 template <
typename T,
typename TCompare>
76 template <
typename T,
typename TCompare>
82 template <
typename T,
typename TCompare>
88 template <
typename T,
typename TCompare>
94 template <
typename T,
typename TCompare>
100 template <
typename T,
typename TCompare>
106 template <
typename T,
typename TCompare>
112 template <
typename T,
typename TCompare>
118 template <
typename T,
typename TCompare>
124 template <
typename T,
typename TCompare>
130 template <
typename T,
typename TCompare>
136 template <
typename T,
typename TCompare>
139 return iterator(
this, (T*)InternalFind(item));
142 template <
typename T,
typename TCompare>
148 template <
typename T,
typename TCompare>
152 const T* current = _root;
154 while (current !=
nullptr)
157 if (compare(item, *current))
159 current = current->left;
164 if (compare(*current, item))
166 current = current->right;
178 template <
typename T,
typename TCompare>
181 return iterator(
this, (T*)InternalLowerBound(item));
184 template <
typename T,
typename TCompare>
190 template <
typename T,
typename TCompare>
194 const T* current = _root;
195 const T* previous =
nullptr;
197 while (current !=
nullptr)
200 if (compare(item, *current))
203 current = current->left;
208 if (compare(*current, item))
210 current = current->right;
222 template <
typename T,
typename TCompare>
225 return iterator(
this, (T*)InternalUpperBound(item));
228 template <
typename T,
typename TCompare>
234 template <
typename T,
typename TCompare>
238 const T* current = _root;
239 const T* previous =
nullptr;
241 while (current !=
nullptr)
244 if (compare(item, *current))
247 current = current->left;
252 current = current->right;
259 template <
typename T,
typename TCompare>
265 template <
typename T,
typename TCompare>
269 T* current = (T*)position.operator->();
271 while (current !=
nullptr)
274 if (compare(item, *current))
276 if (current->left !=
nullptr)
278 current = current->left;
284 current->left = &item;
290 if (compare(*current, item))
292 if (current->right !=
nullptr)
294 current = current->right;
300 current->right = &item;
306 return std::make_pair(
iterator(
this, current),
false);
309 item.parent = current;
311 item.right =
nullptr;
312 if (_root ==
nullptr)
321 while ((node->parent !=
nullptr) && node->parent->rb)
324 if (node->parent == node->parent->parent->left)
326 T* uncle = node->parent->parent->right;
327 if ((uncle !=
nullptr) && uncle->rb)
330 node->parent->rb =
false;
332 node->parent->parent->rb =
true;
333 node = node->parent->parent;
338 if (node == node->parent->right)
346 node->parent->rb =
false;
347 node->parent->parent->rb =
true;
348 RotateRight(node->parent->parent);
354 T* uncle = node->parent->parent->left;
355 if ((uncle !=
nullptr) && uncle->rb)
358 node->parent->rb =
false;
360 node->parent->parent->rb =
true;
361 node = node->parent->parent;
366 if (node == node->parent->left)
373 node->parent->rb =
false;
374 node->parent->parent->rb =
true;
375 RotateLeft(node->parent->parent);
381 return std::make_pair(
iterator(
this, &item),
true);
384 template <
typename T,
typename TCompare>
387 return erase(find(item)).operator->();
390 template <
typename T,
typename TCompare>
393 T* result = ((
iterator&)it).operator->();
394 if (result ==
nullptr)
401 if ((node->left ==
nullptr) || (node->right ==
nullptr))
410 while (y->left !=
nullptr)
417 if (node->parent ==
nullptr)
423 if (y->left !=
nullptr)
430 x->parent = y->parent;
431 if (y->parent !=
nullptr)
433 if (y == y->parent->left)
436 y->parent->right = x;
443 Unlink(x, y->parent);
445 result->parent =
nullptr;
446 result->left =
nullptr;
447 result->right =
nullptr;
452 template <
typename T,
typename TCompare>
455 T* current = node->right;
458 node->right = current->left;
459 if (current->left !=
nullptr)
460 current->left->parent = node;
463 current->parent = node->parent;
464 if (node->parent !=
nullptr)
466 if (node == node->parent->left)
467 node->parent->left = current;
469 node->parent->right = current;
475 current->left = node;
476 node->parent = current;
479 template <
typename T,
typename TCompare>
480 inline void BinTreeRB<T, TCompare>::RotateRight(T* node)
482 T* current = node->left;
485 node->left = current->right;
486 if (current->right !=
nullptr)
487 current->right->parent = node;
490 current->parent = node->parent;
491 if (node->parent !=
nullptr)
493 if (node == node->parent->right)
494 node->parent->right = current;
496 node->parent->left = current;
502 current->right = node;
503 node->parent = current;
506 template <
typename T,
typename TCompare>
507 inline void BinTreeRB<T, TCompare>::Unlink(T* node, T* parent)
511 while ((parent !=
nullptr) && ((node ==
nullptr) || !node->rb))
513 if (node == parent->left)
516 if ((w !=
nullptr) && w->rb)
525 if (((w->left ==
nullptr) || !w->left->rb) && ((w->right ==
nullptr) || !w->right->rb))
529 parent = parent->parent;
533 if ((w->right ==
nullptr) || !w->right->rb)
547 w->right->rb =
false;
556 if ((w !=
nullptr) && w->rb)
565 if (((w->left ==
nullptr) || !w->left->rb) && ((w->right ==
nullptr) || !w->right->rb))
569 parent = parent->parent;
573 if ((w->left ==
nullptr) || !w->left->rb)
575 w->right->rb =
false;
599 template <
typename T,
typename TCompare>
600 inline void BinTreeRB<T, TCompare>::Swap(T*& node1, T*& node2)
602 T* first_parent = node1->parent;
603 T* first_left = node1->left;
604 T* first_right = node1->right;
605 T* second_parent = node2->parent;
606 T* second_left = node2->left;
607 T* second_right = node2->right;
608 bool first_is_left = ((first_parent !=
nullptr) && (first_parent->left == node1));
609 bool second_is_left = ((second_parent !=
nullptr) && (second_parent->left == node2));
612 if (first_parent !=
nullptr)
615 first_parent->left = node2;
617 first_parent->right = node2;
619 if (first_left !=
nullptr)
620 first_left->parent = node2;
621 if (first_right !=
nullptr)
622 first_right->parent = node2;
625 if (second_parent !=
nullptr)
628 second_parent->left = node1;
630 second_parent->right = node1;
632 if (second_left !=
nullptr)
633 second_left->parent = node1;
634 if (second_right !=
nullptr)
635 second_right->parent = node1;
641 std::swap(node1->balance, node2->balance);
647 template <
typename T,
typename TCompare>
654 template <
typename T,
typename TCompare>
658 swap(_compare, bintree._compare);
659 swap(_size, bintree._size);
660 swap(_root, bintree._root);
663 template <
typename T,
typename TCompare>
666 bintree1.swap(bintree2);
Intrusive binary tree constant iterator.
Intrusive binary tree constant reverse iterator.
Intrusive binary tree iterator.
Intrusive balanced Red-Black binary tree container.
T * highest() noexcept
Get the highest binary tree item.
const_iterator cend() const noexcept
const_reverse_iterator crbegin() const noexcept
BinTreeRB(const TCompare &compare=TCompare()) noexcept
iterator begin() noexcept
Get the begin binary tree iterator.
reverse_iterator rbegin() noexcept
Get the reverse begin binary tree iterator.
T * erase(const T &item) noexcept
Erase the given item from the binary tree.
void clear() noexcept
Clear the binary tree.
iterator upper_bound(const T &item) noexcept
Find the iterator which points to the first item that greater than the given item in the binary tree ...
reverse_iterator rend() noexcept
Get the reverse end binary tree iterator.
iterator end() noexcept
Get the end binary tree iterator.
T * lowest() noexcept
Get the lowest binary tree item.
void swap(BinTreeRB &bintree) noexcept
Swap two instances.
const_reverse_iterator crend() const noexcept
std::pair< iterator, bool > insert(T &item) noexcept
Insert a new item into the binary tree.
iterator lower_bound(const T &item) noexcept
Find the iterator which points to the first item that not less than the given item in the binary tree...
const_iterator cbegin() const noexcept
iterator find(const T &item) noexcept
Find the iterator which points to the first equal item in the binary tree or return end iterator.
Intrusive binary tree reverse iterator.
C++ Common project definitions.
void swap(BinTreeRB< T, TCompare > &bintree1, BinTreeRB< T, TCompare > &bintree2) noexcept
void swap(FileCache &cache1, FileCache &cache2) noexcept