mn_rb_tree.hpp
Go to the documentation of this file.
1 
18 #ifndef _MINILIB_4b715a5a_43d4_4442_88e5_4dcc9d8860bb_H_
19 #define _MINILIB_4b715a5a_43d4_4442_88e5_4dcc9d8860bb_H_
20 
21 #include "../mn_config.hpp"
22 #include "../mn_typetraits.hpp"
23 #include <stddef.h>
24 #include "../mn_allocator.hpp"
25 #include "../mn_algorithm.hpp"
26 
27 
28 namespace mn {
29  namespace container {
30  namespace internal {
31 
32  template<typename TKey>
34  TKey key;
36  rb_tree_key_wrapper(const TKey& key_): key(key_) {}
37  const TKey& get_key() const { return key; }
38  };
39 
40  template<typename TKey>
41  struct rb_tree_traits {
42  using key_type = TKey;
44  };
45  }
46 
47  enum class rb_tree_color {
48  red,
49  black
50  };
51 
52  template<typename TVALUE>
53  struct rb_tree_node {
54 
58 
60  : left(left_), parent(parent_), right(right_), color(color_) { }
61 
62  rb_tree_node(const rb_tree_node& other)
63  : left(other.left), parent(other.parent), right(other.right), color(other.color) { }
64 
65  void swap(rb_tree_node& other) {
66  rb_tree_node tmp(this);
67  left = other.left; other.left = tmp.left;
68  parent = other.parent; other.parent = tmp.parent;
69  right = other.right; other.right = tmp.right;
70  value = other.value; other.left = tmp.value;
71  color = other.color; other.color = tmp.color;
72  }
73 
77  TVALUE value;
79  };
80 
81  template<typename TVALUE>
83  a.swap(b);
84  }
85 
86  template<class TTreeTraits, class TAllocator>
87  class base_rb_tree {
88  public:
89  using key_type = typename TTreeTraits::key_type;
90  using value_type = typename TTreeTraits::value_type;
91  using allocator_type = TAllocator;
95 
96  static const size_type NodeSize = sizeof(node_type);
97 
98  typedef void (*TravFunc)(node_type* n, size_type left, size_type depth);
99 
100  explicit base_rb_tree(const allocator_type& allocator = allocator_type())
101  : m_size(0), m_allocator(allocator) {
106  m_root = &ms_sentinel;
107  }
109  clear();
110  }
111 
112 
114  node_type* iter(m_root);
115  node_type* parent(&ms_sentinel);
116  while (iter != &ms_sentinel)
117  {
118  parent = iter;
119  if (iter->value.get_key() < v.get_key())
120  iter = iter->right;
121  else if (v.get_key() < iter->value.get_key())
122  iter = iter->left;
123  else // v.key == iter->key
124  return iter;
125  }
126 
127  node_type* new_node = construct_node();
128  if(new_node == nullptr) return nullptr;
129 
130  new_node->color = rb_tree_color::red;
131  new_node->value = v;
132  new_node->left = &ms_sentinel;
133  new_node->right = &ms_sentinel;
134  new_node->parent = parent;
135 
136  if (parent != &ms_sentinel) {
137  if (v.get_key() < parent->value.get_key())
138  parent->left = new_node;
139  else
140  parent->right = new_node;
141  } else { // empty tree
142  m_root = new_node;
143  }
144 
145  rebalance(new_node);
146  validate();
147  ++m_size;
148  return new_node;
149  }
150  node_type* find_node(const key_type& key) {
151  node_type* iter(m_root);
152  while (iter != &ms_sentinel) {
153  const key_type& iter_key = iter->value.get_key();
154  if (iter_key < key)
155  iter = iter->right;
156  else if (key < iter_key)
157  iter = iter->left;
158  else // key == iter->key
159  return iter;
160  }
161  return 0; // not found
162  }
163 
164  size_type erase(const key_type& key) {
165  node_type* toErase = find_node(key);
166  size_type erased(0);
167  if (toErase != 0) {
168  erase(toErase);
169  erased = 1;
170  }
171  return erased;
172  }
173  void erase(node_type* n) {
174  assert(m_size > 0);
175  node_type* toErase;
176 
177  if (n->left == &ms_sentinel || n->right == &ms_sentinel) {
178  toErase = n;
179  } else {
180  toErase = n->right;
181  while (toErase->left != &ms_sentinel)
182  toErase = toErase->left;
183  }
184 
185  node_type* eraseChild = (toErase->left != &ms_sentinel ? toErase->left : toErase->right);
186  eraseChild->parent = toErase->parent;
187 
188  if (toErase->parent != &ms_sentinel) {
189  if (toErase == toErase->parent->left)
190  toErase->parent->left = eraseChild;
191  else
192  toErase->parent->right = eraseChild;
193  } else {
194  m_root = eraseChild;
195  }
196 
197  n->value = toErase->value;
198 
199  if (toErase->color == rb_tree_color::black)
200  rebalance_after_erase(eraseChild);
201 
202  free_node(toErase, false);
203 
204  validate();
205  --m_size;
206  }
207 
208  void clear() {
209  if (!empty()) {
210  free_node(m_root, true);
211  m_root = &ms_sentinel;
212  m_size = 0;
213  }
214  }
215  void swap(base_rb_tree& other) {
216  if (&other != this) {
217  validate();
218  assert(m_allocator == other.m_allocator);
219  m_root.swap(other.m_root);
220  mn::swap<size_type>(m_size, other.m_size);
221  validate();
222  }
223  }
224 
225  bool empty() const {
226  return m_size == 0;
227  }
228  size_type size() const {
229  return m_size;
230  }
231 
232  const node_type* begin() {
233  node_type* iter(0);
234  if (m_root != &ms_sentinel) {
235  iter = m_root;
236  while (iter->left != &ms_sentinel)
237  iter = iter->left;
238  }
239  return iter;
240  }
241  const node_type* find_next(node_type* n) const {
242  node_type* next(0);
243 
244  if (n != 0) {
245  if (n->right != &ms_sentinel) {
246  next = n->right;
247  while (next->left != &ms_sentinel)
248  next = next->left;
249  } else if (n->parent != &ms_sentinel) {
250  if (n == n->parent->left) {
251  return n->parent;
252  } else {
253  next = n;
254 
255  while (next->parent != &ms_sentinel) {
256  if (next == next->parent->right)
257  next = next->parent;
258  else {
259  return next->parent;
260  }
261  }
262  next = 0;
263  }
264  } else { // 'n' is root.
265  assert(n == m_root);
266  }
267  }
268  return next;
269  }
271  return n == &ms_sentinel ? 0 : 1 + size(n->left) + size(n->right);
272  }
273  void validate() {
274  assert(m_root->color == rb_tree_color::black);
275  validate_node(m_root);
276  }
277  void validate(node_type* n) {
278  // - we're child of our parent.
279  assert(n->parent == &ms_sentinel || n->parent->left == n || n->parent->right == n);
280 
281  // - both children of rb_tree_color::red node_type are rb_tree_color::black
282  if (n->color == rb_tree_color::red) {
283  assert(n->left->color == rb_tree_color::black);
284  assert(n->right->color == rb_tree_color::black);
285  }
286  if (n->left != &ms_sentinel) validate(n->left);
287  if (n->right != &ms_sentinel) validate(n->right);
288  }
290  // Right child's left child becomes n's right child.
291  node_type* rightChild = n->right;
292  n->right = rightChild->left;
293 
294  if (n->right != &ms_sentinel) n->right->parent = n;
295 
296  // n's right child replaces n
297  rightChild->parent = n->parent;
298 
299  if (n->parent == &ms_sentinel) {
300  m_root = rightChild;
301  } else {
302  if (n == n->parent->left)
303  n->parent->left = rightChild;
304  else
305  n->parent->right = rightChild;
306  }
307 
308  rightChild->left = n;
309  n->parent = rightChild;
310  }
312 
313  node_type* leftChild(n->left);
314  n->left = leftChild->right;
315 
316  if (n->left != &ms_sentinel) n->left->parent = n;
317 
318  leftChild->parent = n->parent;
319 
320  if (n->parent == &ms_sentinel) {
321  m_root = leftChild;
322  } else {
323  // Substitute us in the parent list with left child.
324  if (n == n->parent->left)
325  n->parent->left = leftChild;
326  else
327  n->parent->right = leftChild;
328  }
329  leftChild->right = n;
330  n->parent = leftChild;
331  }
332  void free_node(node_type* n, bool recursive) {
333  if (recursive) {
334  if (n->left != &ms_sentinel) free_node(n->left, true);
335  if (n->right != &ms_sentinel) free_node(n->right, true);
336  }
337  if (n != &ms_sentinel) {
338  destruct_node(n) ;
339  }
340  }
341 
342 
343  base_rb_tree(const base_rb_tree&) = delete;
345  public:
346  void traverse_node(node_type* n, TravFunc func, int depth) {
347  int left(-1);
348  if (n->parent != &ms_sentinel) {
349  left = n->parent->left == n;
350  }
351  func(n, left, depth);
352 
353  if (n->left != &ms_sentinel)
354  traverse_node(n->left, func, depth + 1);
355  if (n->right != &ms_sentinel)
356  traverse_node(n->right, func, depth + 1);
357  }
358 
359  void traverse(TravFunc func) {
360  int depth(0);
361  traverse_node(m_root, func, depth);
362  }
363  protected:
364  inline void rebalance(node_type* new_node) {
365  assert(new_node->color == rb_tree_color::red);
366 
367  node_type* iter(new_node);
368 
369  while (iter->parent->color == rb_tree_color::red) {
370 
371  node_type* grandparent(iter->parent->parent);
372 
373  if (iter->parent == grandparent->left) {
374  node_type* uncle = grandparent->right;
375 
376  if (uncle->color == rb_tree_color::red) {
378  uncle->color = rb_tree_color::black;
379  grandparent->color = rb_tree_color::red;
380  iter = grandparent;
381  } else {
382 
383  if (iter == iter->parent->right) {
384  iter = iter->parent;
385  rotate_left(iter);
386  }
387 
388  grandparent = iter->parent->parent;
390  grandparent->color = rb_tree_color::red;
391  rotate_right(grandparent);
392  }
393  } else {
394  node_type* uncle = grandparent->left;
395  if (uncle->color == rb_tree_color::red) {
396  grandparent->color = rb_tree_color::red;
398  uncle->color = rb_tree_color::black;
399  iter = grandparent;
400  } else {
401  if (iter == iter->parent->left) {
402  iter = iter->parent;
403  rotate_right(iter);
404  }
405  grandparent = iter->parent->parent;
407  grandparent->color = rb_tree_color::red;
408  rotate_left(grandparent);
409  }
410  }
411  }
413  }
414 
416  node_type* iter(n);
417 
418  while (iter != m_root && iter->color == rb_tree_color::black) {
419 
420  if (iter == iter->parent->left) {
421  node_type* sibling = iter->parent->right;
422  if (sibling->color == rb_tree_color::red) {
423  sibling->color = rb_tree_color::black;
425  rotate_left(iter->parent);
426  sibling = iter->parent->right;
427  }
428 
429  if (sibling->left->color == rb_tree_color::black &&
430  sibling->right->color == rb_tree_color::black) {
431  sibling->color = rb_tree_color::red;
432  iter = iter->parent;
433  } else {
434 
435  if (sibling->right->color == rb_tree_color::black) {
436  sibling->left->color = rb_tree_color::black;
437  sibling->color = rb_tree_color::red;
438  rotate_right(sibling);
439  sibling = iter->parent->right;
440  }
441 
442  sibling->color = iter->parent->color;
444  sibling->right->color = rb_tree_color::black;
445  rotate_left(iter->parent);
446  iter = m_root;
447  }
448  } else { // iter == right child
449 
450  node_type* sibling = iter->parent->left;
451  if (sibling->color == rb_tree_color::red) {
452  sibling->color = rb_tree_color::black;
454  rotate_right(iter->parent);
455  sibling = iter->parent->left;
456  }
457  if (sibling->left->color == rb_tree_color::black &&
458  sibling->right->color == rb_tree_color::black) {
459  sibling->color = rb_tree_color::red;
460  iter = iter->parent;
461  } else {
462 
463  if (sibling->left->color == rb_tree_color::black) {
464  sibling->right->color = rb_tree_color::black;
465  sibling->color = rb_tree_color::red;
466  rotate_left(sibling);
467  sibling = iter->parent->left;
468  }
469 
470  sibling->color = iter->parent->color;
472  sibling->left->color = rb_tree_color::black;
473  rotate_right(iter->parent);
474  iter = m_root;
475  }
476  }
477  }
478  iter->color = rb_tree_color::black;
479  }
480  private:
482  //void* mem = m_allocator.allocate(NodeSize, mn::alignment_for(NodeSize) );
483  //return new (mem) node_type();
484 
485  return m_allocator.construct<node_type>());
486  }
488  if(n == nullptr) return;
489 
490  // n->~node_type();
491  // m_allocator.deallocate(n, NodeSize, mn::alignment_for(NodeSize));
492  // n = nullptr;
493  m_allocator.destroy<node_type>(n));
494  }
495  private:
500  };
501 
502  template<class TTreeTraits, class TAllocator>
506 
507 
508 
509  template<typename TKey, class TAllocator = memory::default_allocator>
511  }
512 }
513 
514 #endif
Definition: mn_rb_tree.hpp:87
allocator_type m_allocator
Definition: mn_rb_tree.hpp:498
void clear()
Definition: mn_rb_tree.hpp:208
node_type * find_node(const key_type &key)
Definition: mn_rb_tree.hpp:150
~base_rb_tree()
Definition: mn_rb_tree.hpp:108
void rotate_right(node_type *n)
Definition: mn_rb_tree.hpp:311
void validate()
Definition: mn_rb_tree.hpp:273
void traverse_node(node_type *n, TravFunc func, int depth)
Definition: mn_rb_tree.hpp:346
size_type erase(const key_type &key)
Definition: mn_rb_tree.hpp:164
void rebalance_after_erase(node_type *n)
Definition: mn_rb_tree.hpp:415
size_type m_size
Definition: mn_rb_tree.hpp:497
typename TTreeTraits::key_type key_type
Definition: mn_rb_tree.hpp:89
void validate(node_type *n)
Definition: mn_rb_tree.hpp:277
const node_type * find_next(node_type *n) const
Definition: mn_rb_tree.hpp:241
node_type * construct_node()
Definition: mn_rb_tree.hpp:481
void rebalance(node_type *new_node)
Definition: mn_rb_tree.hpp:364
size_type size(const node_type *n)
Definition: mn_rb_tree.hpp:270
mn::size_t size_type
Definition: mn_rb_tree.hpp:93
void(* TravFunc)(node_type *n, size_type left, size_type depth)
Definition: mn_rb_tree.hpp:98
void free_node(node_type *n, bool recursive)
Definition: mn_rb_tree.hpp:332
node_type * insert(const value_type &v)
Definition: mn_rb_tree.hpp:113
TAllocator allocator_type
Definition: mn_rb_tree.hpp:91
void rotate_left(node_type *n)
Definition: mn_rb_tree.hpp:289
void erase(node_type *n)
Definition: mn_rb_tree.hpp:173
void swap(base_rb_tree &other)
Definition: mn_rb_tree.hpp:215
static node_type ms_sentinel
Definition: mn_rb_tree.hpp:499
const node_type * begin()
Definition: mn_rb_tree.hpp:232
size_type size() const
Definition: mn_rb_tree.hpp:228
typename TTreeTraits::value_type value_type
Definition: mn_rb_tree.hpp:90
node_type * m_root
Definition: mn_rb_tree.hpp:496
static const size_type NodeSize
Definition: mn_rb_tree.hpp:96
base_rb_tree & operator=(const base_rb_tree &)=delete
base_rb_tree(const base_rb_tree &)=delete
void traverse(TravFunc func)
Definition: mn_rb_tree.hpp:359
rb_tree_node< value_type > node_type
Definition: mn_rb_tree.hpp:94
void destruct_node(node_type *n)
Definition: mn_rb_tree.hpp:487
base_rb_tree(const allocator_type &allocator=allocator_type())
Definition: mn_rb_tree.hpp:100
bool empty() const
Definition: mn_rb_tree.hpp:225
Definition: mn_node.hpp:113
TKey key_type
Definition: mn_rb_tree.hpp:42
Definition: mn_rb_tree.hpp:41
void swap(basic_any &x, basic_any &y) noexcept
Definition: mn_any.hpp:272
rb_tree_color
Definition: mn_rb_tree.hpp:47
Definition: mn_allocator_typetraits.hpp:25
TForwardIter next(TForwardIter x, TDistance n=1)
Definition: mn_iterator.hpp:112
MN_THREAD_CONFIG_SIZE_TYPE size_t
Definition: mn_def.hpp:48
TKey key
Definition: mn_rb_tree.hpp:34
const TKey & get_key() const
Definition: mn_rb_tree.hpp:37
rb_tree_key_wrapper()
Definition: mn_rb_tree.hpp:35
rb_tree_key_wrapper(const TKey &key_)
Definition: mn_rb_tree.hpp:36
Definition: mn_rb_tree.hpp:53
TVALUE value
Definition: mn_rb_tree.hpp:77
rb_tree_node(rb_tree_color color_, rb_tree_node *left_, rb_tree_node *right_, rb_tree_node *parent_)
Definition: mn_rb_tree.hpp:59
rb_tree_color color
Definition: mn_rb_tree.hpp:78
void swap(rb_tree_node &other)
Definition: mn_rb_tree.hpp:65
rb_tree_node(rb_tree_node *node)
Definition: mn_rb_tree.hpp:56
rb_tree_node()
Definition: mn_rb_tree.hpp:55
rb_tree_node(const rb_tree_node &other)
Definition: mn_rb_tree.hpp:62
rb_tree_node * parent
Definition: mn_rb_tree.hpp:75
rb_tree_node * left
Definition: mn_rb_tree.hpp:74
rb_tree_node * right
Definition: mn_rb_tree.hpp:76