22 #ifndef COMMON_RB_TREE_H 23 #define COMMON_RB_TREE_H 25 #include "common/func.h" 26 #include "common/util.h" 27 #include "common/scummsys.h" 31 template<
typename Key,
typename Val>
40 const T &operator()(
const T &t) {
59 template<
class ValueType,
class Key,
class KeyProj,
class KeyComp = Common::Less<Key> >
75 template<
typename Ref,
typename Ptr>
84 Ref operator*()
const {
return _current->value; }
86 Ptr operator->()
const {
return &_current->value; }
89 if (_current->right) {
90 _current = leftmost(_current->right);
92 auto p = _current->parent;
93 while (p && p->right == _current) {
108 bool operator==(
const Iterator &rhs)
const {
109 return _current == rhs._current;
112 bool operator!=(
const Iterator &rhs)
const {
113 return _current != rhs._current;
119 Node *_current =
nullptr;
130 for (
const auto &val : other)
135 RBTree(
RBTree &&other) : _root(other._root), _leftmost(other._leftmost), _size(other._size), _comp(
Common::
move(other._comp)) {
148 _leftmost = rhs._leftmost;
152 rhs._leftmost =
nullptr;
159 erase(begin(), end());
185 if (!_comp(KeyProj()(it->value), key)) {
203 if (!_comp(KeyProj()(it->value), key)) {
221 if (!_comp(key, KeyProj()(it->value))) {
239 if (!_comp(key, KeyProj()(it->value))) {
251 Node *
const z = it._current;
254 const auto ret = it++;
255 Color y_prev_color = y->color;
261 transplant(y, y->right);
262 }
else if (!y->right) {
265 transplant(y, y->left);
267 y = leftmost(z->right);
268 y_prev_color = y->color;
272 transplant(y, y->right);
274 y->right->parent = y;
283 if (y_prev_color == Color::kBlack)
296 while (first != last)
303 return internalInsert(&_root, val);
323 return internalInsert(&start._current, val);
327 size_t size()
const {
return _size; }
342 Node *_root =
nullptr;
343 Node *_leftmost =
nullptr;
347 auto it = starting_point;
348 Node *parent =
nullptr;
351 if (_comp(KeyProj()((*it)->value), KeyProj()(val))) {
364 if (!_leftmost || (parent == _leftmost && _leftmost->left == *it))
379 void rotateLeft(
Node *t) {
392 else if (p->right == t)
401 void rotateRight(
Node *t) {
410 l->right->parent = t;
415 else if (p->right == t)
427 }
else if (t == t->parent->left) {
430 t->parent->right = u;
433 u->parent = t->parent;
436 _leftmost = u ? leftmost(u) : t->parent;
439 void fixInsert(
Node *t) {
440 while (t->parent && t->parent->color == Color::kRed) {
446 Node *
const u = g->right;
447 if (u && u->color == Color::kRed) {
448 p->color = u->color = Color::kBlack;
449 g->color = Color::kRed;
457 p->color = Color::kBlack;
459 g->color = Color::kRed;
463 Node *
const u = g->left;
464 if (u && u->color == Color::kRed) {
465 p->color = u->color = Color::kBlack;
466 g->color = Color::kRed;
474 p->color = Color::kBlack;
475 g->color = Color::kRed;
480 _root->color = Color::kBlack;
484 while (t != _root && (!t || t->color == Color::kBlack)) {
490 if (b->color == Color::kRed) {
491 b->color = Color::kBlack;
492 p->color = Color::kRed;
499 if ((!b->left || b->left->color == Color::kBlack) &&
500 (!b->right || b->right->color == Color::kBlack)) {
501 b->color = Color::kRed;
505 if (!b->right || b->right->color == Color::kBlack) {
508 b->left->color = Color::kBlack;
509 b->color = Color::kRed;
514 p->color = Color::kBlack;
516 b->right->color = Color::kBlack;
523 if (b->color == Color::kRed) {
524 b->color = Color::kBlack;
525 p->color = Color::kRed;
530 if ((!b->left || b->left->color == Color::kBlack) &&
531 (!b->right || b->right->color == Color::kBlack)) {
532 b->color = Color::kRed;
536 if (!b->left || b->left->color == Color::kBlack) {
537 b->right->color = Color::kBlack;
538 b->color = Color::kRed;
543 p->color = Color::kBlack;
545 b->left->color = Color::kBlack;
552 t->color = Color::kBlack;
ConstIterator begin() const
Definition: rb_tree.h:169
ConstIterator lowerBound(const Key &key) const
Definition: rb_tree.h:199
RBTree(RBTree &&other)
Definition: rb_tree.h:135
bool isEmpty() const
Definition: rb_tree.h:336
BasicIterator upperBound(const Key &key)
Definition: rb_tree.h:217
BasicIterator insert(BasicIterator start, const ValueType &val)
Definition: rb_tree.h:320
RBTree & operator=(const RBTree &rhs)
Definition: rb_tree.h:139
BasicIterator lowerBound(const Key &key)
Definition: rb_tree.h:181
BasicIterator insert(const ValueType &val)
Definition: rb_tree.h:302
RBTree(const RBTree &other)
Definition: rb_tree.h:129
BasicIterator erase(BasicIterator first, BasicIterator last)
Definition: rb_tree.h:295
RBTree & operator=(RBTree &&rhs)
Definition: rb_tree.h:145
Definition: algorithm.h:29
Iterator< const cGuiRenderObject &, const cGuiRenderObject *> ConstIterator
Definition: rb_tree.h:123
Iterator< cGuiRenderObject &, cGuiRenderObject *> BasicIterator
Definition: rb_tree.h:122
Out move(In first, In last, Out dst)
Definition: algorithm.h:109
BasicIterator erase(BasicIterator it)
Definition: rb_tree.h:250
void clear()
Definition: rb_tree.h:158
size_t size() const
Definition: rb_tree.h:327
ConstIterator upperBound(const Key &key) const
Definition: rb_tree.h:235
Definition: lobject.h:332
BasicIterator begin()
Definition: rb_tree.h:166
BasicIterator end()
Definition: rb_tree.h:172
ConstIterator end() const
Definition: rb_tree.h:175