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->level != ((node->left !=
nullptr) ? node->left->level + 1 : 1))
325 if ((node->right ==
nullptr) || (node->level != node->right->level))
329 if (!Split(node->parent))
333 return std::make_pair(
iterator(
this, &item),
true);
336 template <
typename T,
typename TCompare>
339 return erase(find(item)).operator->();
342 template <
typename T,
typename TCompare>
345 T* result = ((
iterator&)it).operator->();
346 if (result ==
nullptr)
352 if (result->left !=
nullptr)
355 while (node->right !=
nullptr)
358 else if (result->right !=
nullptr)
359 node = result->right;
361 temp = ((node->parent == result) ? node : node->parent);
362 if (node->parent !=
nullptr)
364 if (node->parent->left == node)
365 node->parent->left =
nullptr;
367 node->parent->right =
nullptr;
374 if (result->parent !=
nullptr)
376 if (result->parent->left == result)
377 result->parent->left = node;
379 result->parent->right = node;
384 node->parent = result->parent;
385 if (result->left !=
nullptr)
386 result->left->parent = node;
387 node->left = result->left;
388 if (result->right !=
nullptr)
389 result->right->parent = node;
390 node->right = result->right;
393 node->level = result->level;
396 while (temp !=
nullptr)
398 if (temp->level > ((temp->left !=
nullptr) ? temp->left->level + 1 : 1))
400 temp->level = temp->level - 1;
404 Skew(temp->parent->parent);
408 else if (temp->level <= ((temp->right !=
nullptr) ? temp->right->level + 1 : 1))
413 if (temp->level > temp->parent->level)
416 Split(temp->parent->parent);
424 result->parent =
nullptr;
425 result->left =
nullptr;
426 result->right =
nullptr;
431 template <
typename T,
typename TCompare>
437 T* current = node->left;
438 if (node->parent !=
nullptr)
440 if (node->parent->left == node)
441 node->parent->left = current;
443 node->parent->right = current;
447 current->parent = node->parent;
448 node->parent = current;
450 node->left = current->right;
451 if (node->left !=
nullptr)
452 node->left->parent = node;
453 current->right = node;
455 if (node->left !=
nullptr)
456 node->level = node->left->level + 1;
461 template <
typename T,
typename TCompare>
462 inline bool BinTreeAA<T, TCompare>::Split(T* node)
467 T* current = node->right;
468 if ((current !=
nullptr) && (current->right !=
nullptr) && (current->right->level == node->level))
470 if (node->parent !=
nullptr)
472 if (node->parent->left == node)
473 node->parent->left = current;
475 node->parent->right = current;
479 current->parent = node->parent;
480 node->parent = current;
482 node->right = current->left;
483 if (node->right !=
nullptr)
484 node->right->parent = node;
485 current->left = node;
486 current->level = node->level + 1;
493 template <
typename T,
typename TCompare>
500 template <
typename T,
typename TCompare>
504 swap(_compare, bintree._compare);
505 swap(_size, bintree._size);
506 swap(_root, bintree._root);
509 template <
typename T,
typename TCompare>
512 bintree1.swap(bintree2);
Intrusive balanced A.Andersson binary tree container.
void clear() noexcept
Clear the binary tree.
const_reverse_iterator crend() const noexcept
void swap(BinTreeAA &bintree) noexcept
Swap two instances.
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 ...
BinTreeAA(const TCompare &compare=TCompare()) noexcept
reverse_iterator rend() noexcept
Get the reverse end binary tree iterator.
const_reverse_iterator crbegin() const noexcept
iterator end() noexcept
Get the end binary tree iterator.
const_iterator cbegin() const noexcept
T * lowest() noexcept
Get the lowest binary tree item.
const_iterator cend() const noexcept
T * highest() noexcept
Get the highest binary tree item.
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 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...
T * erase(const T &item) noexcept
Erase the given item from the binary tree.
std::pair< iterator, bool > insert(T &item) noexcept
Insert a new item into the binary tree.
reverse_iterator rbegin() noexcept
Get the reverse begin binary tree iterator.
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(BinTreeAA< T, TCompare > &bintree1, BinTreeAA< T, TCompare > &bintree2) noexcept