ScummVM API documentation
rb_tree.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef COMMON_RB_TREE_H
23 #define COMMON_RB_TREE_H
24 
25 #include "common/func.h"
26 #include "common/util.h"
27 #include "common/scummsys.h"
28 
29 namespace Common {
30 
31 template<typename Key, typename Val>
32 struct PairFirst {
33  const Key &operator()(const Pair<Key, Val> &p) {
34  return p.first;
35  }
36 };
37 
38 template<typename T>
39 struct Identity {
40  const T &operator()(const T &t) {
41  return t;
42  }
43 };
44 
59 template<class ValueType, class Key, class KeyProj, class KeyComp = Common::Less<Key> >
60 class RBTree {
61 public:
62  enum class Color {
63  kRed,
64  kBlack,
65  };
66 
67  struct Node {
68  Node *parent;
69  Node *left;
70  Node *right;
71  Color color;
72  ValueType value;
73  };
74 
75  template<typename Ref, typename Ptr>
76  class Iterator {
77  public:
78  friend RBTree;
79 
80  Iterator() = default;
81  Iterator(const Iterator &) = default;
82  Iterator &operator=(const Iterator &) = default;
83 
84  Ref operator*() const { return _current->value; }
85 
86  Ptr operator->() const { return &_current->value; }
87 
88  Iterator &operator++() {
89  if (_current->right) {
90  _current = leftmost(_current->right);
91  } else {
92  auto p = _current->parent;
93  while (p && p->right == _current) {
94  _current = p;
95  p = p->parent;
96  }
97  _current = p;
98  }
99  return *this;
100  }
101 
102  Iterator operator++(int) {
103  auto temp = *this;
104  ++(*this);
105  return temp;
106  }
107 
108  bool operator==(const Iterator &rhs) const {
109  return _current == rhs._current;
110  }
111 
112  bool operator!=(const Iterator &rhs) const {
113  return _current != rhs._current;
114  }
115 
116  private:
117  explicit Iterator(Node *n) : _current(n) {}
118 
119  Node *_current = nullptr;
120  };
121 
122  using BasicIterator = Iterator<ValueType &, ValueType *>;
123  using ConstIterator = Iterator<const ValueType &, const ValueType *>;
125  RBTree(KeyComp comp = {}) : _comp(Common::move(comp)) {
126  }
127 
129  RBTree(const RBTree &other) : _comp(other._comp) {
130  for (const auto &val : other)
131  insert(val);
132  }
133 
135  RBTree(RBTree &&other) : _root(other._root), _leftmost(other._leftmost), _size(other._size), _comp(Common::move(other._comp)) {
136  }
137 
139  RBTree &operator=(const RBTree &rhs) {
140  *this = RBTree(rhs);
141  return *this;
142  }
143 
146  clear();
147  _root = rhs._root;
148  _leftmost = rhs._leftmost;
149  _size = rhs._size;
150  _comp = Common::move(rhs._comp);
151  rhs._root = nullptr;
152  rhs._leftmost = nullptr;
153  rhs._size = 0;
154  return *this;
155  }
156 
158  void clear() {
159  erase(begin(), end());
160  _size = 0;
161  _root = nullptr;
162  _leftmost = nullptr;
163  }
164 
166  BasicIterator begin() { return BasicIterator{_leftmost}; }
167 
169  ConstIterator begin() const { return ConstIterator{_leftmost}; }
170 
172  BasicIterator end() { return BasicIterator{nullptr}; }
173 
175  ConstIterator end() const { return ConstIterator{nullptr}; }
176 
181  BasicIterator lowerBound(const Key &key) {
182  Node *it = _root;
183  Node *res = nullptr;
184  while (it) {
185  if (!_comp(KeyProj()(it->value), key)) {
186  res = it;
187  it = it->left;
188  } else {
189  it = it->right;
190  }
191  }
192  return BasicIterator{res};
193  }
194 
199  ConstIterator lowerBound(const Key &key) const {
200  Node *it = _root;
201  Node *res = nullptr;
202  while (it) {
203  if (!_comp(KeyProj()(it->value), key)) {
204  res = it;
205  it = it->left;
206  } else {
207  it = it->right;
208  }
209  }
210  return ConstIterator{res};
211  }
212 
217  BasicIterator upperBound(const Key &key) {
218  Node *it = _root;
219  Node *res = nullptr;
220  while (it) {
221  if (!_comp(key, KeyProj()(it->value))) {
222  it = it->right;
223  } else {
224  res = it;
225  it = it->left;
226  }
227  }
228  return BasicIterator{res};
229  }
230 
235  ConstIterator upperBound(const Key &key) const {
236  Node *it = _root;
237  Node *res = nullptr;
238  while (it) {
239  if (!_comp(key, KeyProj()(it->value))) {
240  it = it->right;
241  } else {
242  res = it;
243  it = it->left;
244  }
245  }
246  return ConstIterator{res};
247  }
248 
251  Node *const z = it._current;
252  Node *y = z;
253  assert(y);
254  const auto ret = it++;
255  Color y_prev_color = y->color;
256  Node *x;
257  Node *xp = nullptr;
258  if (!y->left) {
259  x = y->right;
260  xp = y->parent; // since x is put in y's place by the next call
261  transplant(y, y->right);
262  } else if (!y->right) {
263  x = y->left;
264  xp = y->parent;
265  transplant(y, y->left);
266  } else {
267  y = leftmost(z->right);
268  y_prev_color = y->color;
269  x = y->right;
270  if (y != z->right) {
271  xp = y->parent;
272  transplant(y, y->right);
273  y->right = z->right;
274  y->right->parent = y;
275  } else {
276  xp = y;
277  }
278  transplant(z, y);
279  y->left = z->left;
280  y->left->parent = y;
281  y->color = z->color;
282  }
283  if (y_prev_color == Color::kBlack)
284  fixDelete(x, xp);
285  delete z;
286  --_size;
287  return ret;
288  }
289 
296  while (first != last)
297  erase(first++);
298  return last;
299  }
300 
302  BasicIterator insert(const ValueType &val) {
303  return internalInsert(&_root, val);
304  }
305 
320  BasicIterator insert(BasicIterator start, const ValueType &val) {
321  if (start == end())
322  return insert(val);
323  return internalInsert(&start._current, val);
324  }
325 
327  size_t size() const { return _size; }
328 
336  bool isEmpty() const { return _size == 0; }
337 
338  ~RBTree() { clear(); }
339 
340 private:
341  KeyComp _comp;
342  Node *_root = nullptr;
343  Node *_leftmost = nullptr;
344  size_t _size = 0;
345 
346  BasicIterator internalInsert(Node **starting_point, const ValueType &val) {
347  auto it = starting_point;
348  Node *parent = nullptr;
349  while (*it) {
350  parent = *it;
351  if (_comp(KeyProj()((*it)->value), KeyProj()(val))) {
352  it = &(*it)->right;
353  } else {
354  it = &(*it)->left;
355  }
356  }
357  *it = new Node{
358  parent,
359  nullptr,
360  nullptr,
361  Color::kRed,
362  val,
363  };
364  if (!_leftmost || (parent == _leftmost && _leftmost->left == *it))
365  _leftmost = *it;
366  ++_size;
367  auto ret = BasicIterator{*it};
368  fixInsert(*it);
369  return ret;
370  }
371 
372  static Node *leftmost(Node *n) {
373  while (n->left)
374  n = n->left;
375  return n;
376  }
377 
378  // neither rotate changes _leftmost
379  void rotateLeft(Node *t) {
380  assert(t);
381  assert(t->right);
382  Node *r = t->right;
383  Node *p = t->parent;
384  // set r->left as t->right
385  t->right = r->left;
386  if (r->left)
387  r->left->parent = t;
388  // set the parent of r
389  r->parent = p;
390  if (!p)
391  _root = r;
392  else if (p->right == t)
393  p->right = r;
394  else
395  p->left = r;
396  // set the parent of t
397  t->parent = r;
398  r->left = t;
399  }
400 
401  void rotateRight(Node *t) {
402  assert(t);
403  assert(t->left);
404  Node *l = t->left;
405  Node *p = t->parent;
406  assert(p != l);
407  // set l->right as t->left
408  t->left = l->right;
409  if (l->right)
410  l->right->parent = t;
411  // set the parent of l
412  l->parent = p;
413  if (!p)
414  _root = l;
415  else if (p->right == t)
416  p->right = l;
417  else
418  p->left = l;
419  // set the parent of t
420  l->right = t;
421  t->parent = l;
422  }
423 
424  void transplant(Node *t, Node *u) {
425  if (!t->parent) {
426  _root = u;
427  } else if (t == t->parent->left) {
428  t->parent->left = u;
429  } else {
430  t->parent->right = u;
431  }
432  if (u) {
433  u->parent = t->parent;
434  }
435  if (t == _leftmost)
436  _leftmost = u ? leftmost(u) : t->parent;
437  }
438 
439  void fixInsert(Node *t) {
440  while (t->parent && t->parent->color == Color::kRed) {
441  Node *p = t->parent;
442  Node *g = p->parent;
443  // cannot be null since p is not _root
444  assert(g);
445  if (p == g->left) {
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;
450  t = g;
451  } else {
452  if (t == p->right) {
453  rotateLeft(p);
454  t = p;
455  p = t->parent;
456  }
457  p->color = Color::kBlack;
458  // g is not changed by the previous rotation
459  g->color = Color::kRed;
460  rotateRight(g);
461  }
462  } else {
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;
467  t = g;
468  } else {
469  if (t == p->left) {
470  rotateRight(p);
471  t = p;
472  p = t->parent;
473  }
474  p->color = Color::kBlack;
475  g->color = Color::kRed;
476  rotateLeft(g);
477  }
478  }
479  }
480  _root->color = Color::kBlack;
481  }
482 
483  void fixDelete(Node *t, Node *p) {
484  while (t != _root && (!t || t->color == Color::kBlack)) {
485  if (t == p->left) {
486  // since the deleted node was black and t is in its
487  // place, it has to have a sibling
488  Node *b = p->right;
489  assert(b);
490  if (b->color == Color::kRed) {
491  b->color = Color::kBlack;
492  p->color = Color::kRed;
493  rotateLeft(p);
494  p = b->left;
495  // since b was red, it had two black children,
496  // the right one now being p->right
497  b = p->right;
498  }
499  if ((!b->left || b->left->color == Color::kBlack) &&
500  (!b->right || b->right->color == Color::kBlack)) {
501  b->color = Color::kRed;
502  t = p;
503  p = p->parent;
504  } else {
505  if (!b->right || b->right->color == Color::kBlack) {
506  // b->left exists, since b->right is black, and it is red
507  // because of one of the above checks
508  b->left->color = Color::kBlack;
509  b->color = Color::kRed;
510  rotateRight(b);
511  b = p->right; // p is not changed by the rotation
512  }
513  b->color = p->color;
514  p->color = Color::kBlack;
515  if (b->right)
516  b->right->color = Color::kBlack;
517  rotateLeft(p);
518  break;
519  }
520  } else { // same as above case but left and right swapped
521  Node *b = p->left;
522  assert(b);
523  if (b->color == Color::kRed) {
524  b->color = Color::kBlack;
525  p->color = Color::kRed;
526  rotateRight(p);
527  p = b->right;
528  b = p->left;
529  }
530  if ((!b->left || b->left->color == Color::kBlack) &&
531  (!b->right || b->right->color == Color::kBlack)) {
532  b->color = Color::kRed;
533  t = p;
534  p = p->parent;
535  } else {
536  if (!b->left || b->left->color == Color::kBlack) {
537  b->right->color = Color::kBlack;
538  b->color = Color::kRed;
539  rotateLeft(b);
540  b = p->left;
541  }
542  b->color = p->color;
543  p->color = Color::kBlack;
544  if (b->left)
545  b->left->color = Color::kBlack;
546  rotateRight(p);
547  break;
548  }
549  }
550  }
551  if (t)
552  t->color = Color::kBlack;
553  }
554 };
555 
558 } // End of namespace Common
559 
560 #endif
Definition: util.h:213
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
Definition: rb_tree.h:60
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
Definition: rb_tree.h:39
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: rb_tree.h:67
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
Definition: rb_tree.h:76
Definition: rb_tree.h:32
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