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)
319 while (node->parent !=
nullptr)
322 if (((node->parent !=
nullptr) && (node->parent->left == node)) && (node->parent->balance == 0))
324 node->parent->balance = -1;
328 if (((node->parent !=
nullptr) && (node->parent->right == node)) && (node->parent->balance == 0))
330 node->parent->balance = 1;
336 if (((node->parent !=
nullptr) && (node->parent->left == node)) && (node->parent->balance == 1))
338 node->parent->balance = 0;
341 if (((node->parent !=
nullptr) && (node->parent->right == node)) && (node->parent->balance == -1))
343 node->parent->balance = 0;
348 if (((node->parent !=
nullptr) && (node->parent->left == node)) && (node->parent->balance == -1))
350 if (node->balance == 1)
351 RotateLeftLeft(node->parent);
353 RotateRight(node->parent);
356 if (((node->parent !=
nullptr) && (node->parent->right == node)) && (node->parent->balance == 1))
358 if (node->balance == -1)
359 RotateRightRight(node->parent);
361 RotateLeft(node->parent);
367 while (_root->parent !=
nullptr)
368 _root = _root->parent;
370 return std::make_pair(
iterator(
this, &item),
true);
373 template <
typename T,
typename TCompare>
376 return erase(find(item)).operator->();
379 template <
typename T,
typename TCompare>
382 T* result = ((
iterator&)it).operator->();
383 if (result ==
nullptr)
390 if (node->parent ==
nullptr)
393 if ((node->left ==
nullptr) && (node->right ==
nullptr))
401 if ((node !=
nullptr) && (node->left ==
nullptr) && (node->right ==
nullptr))
403 if (node->parent->left == node)
404 node->parent->left=
nullptr;
406 node->parent->right=
nullptr;
407 start = node->parent;
412 if ((node !=
nullptr) && (node->left ==
nullptr) && (node->right !=
nullptr))
415 if (node->parent ==
nullptr)
418 node->right =
nullptr;
423 if ((node !=
nullptr) && (node->left !=
nullptr) && (node->right ==
nullptr))
426 if (node->parent ==
nullptr)
429 node->left =
nullptr;
434 if ((node !=
nullptr) && (node->left !=
nullptr) && (node->right !=
nullptr))
437 while (a->right !=
nullptr)
441 if (node->parent ==
nullptr)
447 if (a->parent ==
nullptr)
455 if (a->parent->left == a)
456 a->parent->left =
nullptr;
458 a->parent->right =
nullptr;
464 if (start !=
nullptr)
468 if (_root !=
nullptr)
470 while (_root->parent !=
nullptr)
471 _root = _root->parent;
474 result->parent =
nullptr;
475 result->left =
nullptr;
476 result->right =
nullptr;
481 template <
typename T,
typename TCompare>
484 if (node->right ==
nullptr)
487 T* current = node->right;
489 if (node->parent !=
nullptr)
491 if ((node->parent !=
nullptr) && (node->parent->left == node))
492 node->parent->left = current;
494 node->parent->right = current;
495 current->parent = node->parent;
498 current->parent =
nullptr;
499 node->right = current->left;
500 current->left = node;
501 node->parent = current;
502 if (node->right !=
nullptr)
503 node->right->parent = node;
505 if (current->balance == 0)
508 current->balance = -1;
513 current->balance = 0;
517 template <
typename T,
typename TCompare>
518 inline void BinTreeAVL<T, TCompare>::RotateRight(T* node)
520 if (node->left ==
nullptr)
523 T* current = node->left;
525 if (node->parent !=
nullptr)
527 if ((node->parent !=
nullptr) && (node->parent->left == node))
528 node->parent->left = current;
530 node->parent->right = current;
531 current->parent = node->parent;
534 current->parent =
nullptr;
535 node->left = current->right;
536 current->right = node;
537 node->parent = current;
538 if (node->left !=
nullptr)
539 node->left->parent = node;
541 if (current->balance == 0)
544 current->balance = 1;
549 current->balance = 0;
553 template <
typename T,
typename TCompare>
554 inline void BinTreeAVL<T, TCompare>::RotateLeftLeft(T* node)
556 if ((node->left ==
nullptr) || (node->left->right ==
nullptr))
559 T* current = node->left;
560 T* next = node->left->right;
562 if (node->parent !=
nullptr)
564 if ((node->parent !=
nullptr) && (node->parent->left == node))
565 node->parent->left = next;
567 node->parent->right = next;
569 current->right = next->left;
570 node->left = next->right;
571 next->left = current;
573 next->parent = node->parent;
575 current->parent = next;
576 if (current->right !=
nullptr)
577 current->right->parent = current;
578 if (node->left !=
nullptr)
579 node->left->parent = node;
581 switch (next->balance)
585 current->balance = 0;
589 current->balance = 0;
593 current->balance = -1;
599 template <
typename T,
typename TCompare>
600 inline void BinTreeAVL<T, TCompare>::RotateRightRight(T* node)
602 if ((node->right ==
nullptr) || (node->right->left ==
nullptr))
605 T* current = node->right;
606 T* next = node->right->left;
608 if (node->parent !=
nullptr)
610 if ((node->parent !=
nullptr) && (node->parent->left == node))
611 node->parent->left = next;
613 node->parent->right = next;
615 node->right = next->left;
616 current->left = next->right;
618 next->right = current;
619 next->parent = node->parent;
621 current->parent = next;
622 if (node->right !=
nullptr)
623 node->right->parent = node;
624 if (current->left !=
nullptr)
625 current->left->parent = current;
627 switch (next->balance)
631 current->balance = 1;
635 current->balance = 0;
639 current->balance = 0;
645 template <
typename T,
typename TCompare>
646 inline void BinTreeAVL<T, TCompare>::Unlink(T* node)
649 if ((node->balance == 0) && (node->left ==
nullptr))
654 if ((node->balance == 0) && (node->right ==
nullptr))
661 if ((node->balance == -1) && (node->left ==
nullptr))
663 if ((node->balance == 1) && (node->right ==
nullptr))
667 if ((node->balance == -1) && (node->right ==
nullptr))
669 if (node->left->balance == 1)
670 RotateLeftLeft(node);
674 if (node->balance == 1)
677 if ((node->balance == 1) && (node->left ==
nullptr))
679 if (node->right->balance == -1)
680 RotateRightRight(node);
684 if (node->balance == -1)
688 while (node->parent !=
nullptr)
691 if (((node->parent !=
nullptr) && (node->parent->left == node)) && (node->parent->balance == 0))
693 node->parent->balance = 1;
696 if (((node->parent !=
nullptr) && (node->parent->right == node)) && (node->parent->balance == 0))
698 node->parent->balance = -1;
703 if (((node->parent !=
nullptr) && (node->parent->left == node)) && (node->parent->balance == -1))
705 node->parent->balance = 0;
709 if (((node->parent !=
nullptr) && (node->parent->right == node)) && (node->parent->balance == 1))
711 node->parent->balance = 0;
717 if (((node->parent !=
nullptr) && (node->parent->right == node)) && (node->parent->balance == -1))
719 if (node->parent->left->balance == 1)
720 RotateLeftLeft(node->parent);
722 RotateRight(node->parent);
723 node = node->parent->parent;
724 if (node->balance == 1)
728 if (((node->parent !=
nullptr) && (node->parent->left == node)) && (node->parent->balance == 1))
730 if (node->parent->right->balance == -1)
731 RotateRightRight(node->parent);
733 RotateLeft(node->parent);
734 node = node->parent->parent;
735 if (node->balance == -1)
745 template <
typename T,
typename TCompare>
746 inline void BinTreeAVL<T, TCompare>::Swap(T*& node1, T*& node2)
748 T* first_parent = node1->parent;
749 T* first_left = node1->left;
750 T* first_right = node1->right;
751 T* second_parent = node2->parent;
752 T* second_left = node2->left;
753 T* second_right = node2->right;
754 bool first_is_left = ((first_parent !=
nullptr) && (first_parent->left == node1));
755 bool second_is_left = ((second_parent !=
nullptr) && (second_parent->left == node2));
758 if (first_parent !=
nullptr)
761 first_parent->left = node2;
763 first_parent->right = node2;
765 if (first_left !=
nullptr)
766 first_left->parent = node2;
767 if (first_right !=
nullptr)
768 first_right->parent = node2;
771 if (second_parent !=
nullptr)
774 second_parent->left = node1;
776 second_parent->right = node1;
778 if (second_left !=
nullptr)
779 second_left->parent = node1;
780 if (second_right !=
nullptr)
781 second_right->parent = node1;
787 std::swap(node1->balance, node2->balance);
793 template <
typename T,
typename TCompare>
800 template <
typename T,
typename TCompare>
804 swap(_compare, bintree._compare);
805 swap(_size, bintree._size);
806 swap(_root, bintree._root);
809 template <
typename T,
typename TCompare>
812 bintree1.swap(bintree2);
Intrusive balanced AVL binary tree container.
T * lowest() noexcept
Get the lowest binary tree item.
T * erase(const T &item) noexcept
Erase the given item from the binary tree.
void clear() noexcept
Clear the binary tree.
const_reverse_iterator crbegin() const noexcept
reverse_iterator rend() noexcept
Get the reverse end binary tree iterator.
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_reverse_iterator crend() const noexcept
const_iterator cend() const noexcept
void swap(BinTreeAVL &bintree) noexcept
Swap two instances.
reverse_iterator rbegin() noexcept
Get the reverse begin binary tree iterator.
std::pair< iterator, bool > insert(T &item) noexcept
Insert a new item into the binary tree.
iterator end() noexcept
Get the end binary tree iterator.
T * highest() noexcept
Get the highest binary tree item.
const_iterator cbegin() const noexcept
BinTreeAVL(const TCompare &compare=TCompare()) 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.
iterator begin() noexcept
Get the begin binary tree iterator.
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 ...
Intrusive binary tree constant iterator.
Intrusive binary tree constant reverse iterator.
Intrusive binary tree iterator.
Intrusive binary tree reverse iterator.
C++ Common project definitions.
void swap(FileCache &cache1, FileCache &cache2) noexcept
void swap(BinTreeAVL< T, TCompare > &bintree1, BinTreeAVL< T, TCompare > &bintree2) noexcept