alternative Standard Libary  0.29.8
std::rb_tree< TKey, TAllocator > Template-Klassenreferenz

#include <rb_tree.hpp>

+ Klassendiagramm für std::rb_tree< TKey, TAllocator >:
+ Zusammengehörigkeiten von std::rb_tree< TKey, TAllocator >:

Öffentliche Typen

using key_type = typename internal::rb_tree_traits< TKey > ::key_type
 
using value_type = typename internal::rb_tree_traits< TKey > ::value_type
 
using allocator_type = TAllocator
 
typedef void(* TravFunc) (node *n, int left, int depth)
 
enum  color_e { red, black }
 
using size_type = int
 

Öffentliche Methoden

 rb_tree (TAllocator allocator=TAllocator())
 
node * insert (const value_type &v)
 
node * find_node (const key_type &key)
 
size_type erase (const key_type &key)
 
void erase (node *n)
 
void clear ()
 
void swap (rb_tree_base &other)
 
bool empty () const
 
size_type size () const
 
void traverse_node (node *n, TravFunc func, int depth)
 
void traverse (TravFunc func)
 
node * get_begin_node () const
 
node * find_next_node (node *n) const
 
size_type num_nodes (const node *n) const
 
void rebalance (node *new_node)
 
void rebalance_after_erase (node *n)
 
void validate () const
 
void validate_node (node *n) const
 
void rotate_left (node *n)
 
void rotate_right (node *n)
 
node * alloc_node ()
 
void free_node (node *n, bool recursive)
 

Ausführliche Beschreibung

template<typename TKey, class TAllocator = std::allocator>
class std::rb_tree< TKey, TAllocator >

Dokumentation der benutzerdefinierten Datentypen

◆ allocator_type

using std::rb_tree_base< internal::rb_tree_traits< TKey > , TAllocator >::allocator_type = TAllocator
inherited

◆ key_type

using std::rb_tree_base< internal::rb_tree_traits< TKey > , TAllocator >::key_type = typename internal::rb_tree_traits< TKey > ::key_type
inherited

◆ size_type

◆ TravFunc

typedef void(* std::rb_tree_base< internal::rb_tree_traits< TKey > , TAllocator >::TravFunc) (node *n, int left, int depth)
inherited

◆ value_type

using std::rb_tree_base< internal::rb_tree_traits< TKey > , TAllocator >::value_type = typename internal::rb_tree_traits< TKey > ::value_type
inherited

Dokumentation der Aufzählungstypen

◆ color_e

Aufzählungswerte
red 
black 
46  {
47  red,
48  black
49  };
Definition: rb_tree.hpp:48
Definition: rb_tree.hpp:47

Beschreibung der Konstruktoren und Destruktoren

◆ rb_tree()

template<typename TKey , class TAllocator = std::allocator>
std::rb_tree< TKey, TAllocator >::rb_tree ( TAllocator  allocator = TAllocator())
inlineexplicit
554 : internal::rb_tree_base(allocator) {}

Dokumentation der Elementfunktionen

◆ alloc_node()

node* std::rb_tree_base< internal::rb_tree_traits< TKey > , TAllocator >::alloc_node ( )
inlineinherited
516  {
517  node* mem = (node*)m_allocator.allocate(sizeof(node));
518  return new (mem) node();
519  }

◆ clear()

void std::rb_tree_base< internal::rb_tree_traits< TKey > , TAllocator >::clear ( )
inlineinherited
212  {
213  if (!empty())
214  {
215  free_node(m_root, true);
216  m_root = &ms_sentinel;
217  m_size = 0;
218  }
219  }
void free_node(node *n, bool recursive)
Definition: rb_tree.hpp:520

◆ empty()

bool std::rb_tree_base< internal::rb_tree_traits< TKey > , TAllocator >::empty ( ) const
inlineinherited
233  {
234  return m_size == 0;
235  }

◆ erase() [1/2]

size_type std::rb_tree_base< internal::rb_tree_traits< TKey > , TAllocator >::erase ( const key_type key)
inlineinherited
160  {
161  node* toErase = find_node(key);
162  size_type erased(0);
163  if (toErase != 0)
164  {
165  erase(toErase);
166  erased = 1;
167  }
168  return erased;
169  }
int size_type
Definition: rb_tree.hpp:44
size_type erase(const key_type &key)
Definition: rb_tree.hpp:159
node * find_node(const key_type &key)
Definition: rb_tree.hpp:143

◆ erase() [2/2]

void std::rb_tree_base< internal::rb_tree_traits< TKey > , TAllocator >::erase ( node *  n)
inlineinherited
171  {
172  assert(m_size > 0);
173  node* toErase;
174 
175  if (n->left == &ms_sentinel || n->right == &ms_sentinel)
176  {
177  toErase = n;
178  }
179  else
180  {
181  toErase = n->right;
182  while (toErase->left != &ms_sentinel)
183  toErase = toErase->left;
184  }
185 
186  node* eraseChild = (toErase->left != &ms_sentinel ? toErase->left : toErase->right);
187  eraseChild->parent = toErase->parent;
188  if (toErase->parent != &ms_sentinel)
189  {
190  if (toErase == toErase->parent->left)
191  toErase->parent->left = eraseChild;
192  else
193  toErase->parent->right = eraseChild;
194  }
195  else
196  {
197  m_root = eraseChild;
198  }
199 
200  n->value = toErase->value;
201 
202  if (toErase->color == black)
203  rebalance_after_erase(eraseChild);
204 
205  free_node(toErase, false);
206 
207  validate();
208  --m_size;
209  }
void rebalance_after_erase(node *n)
Definition: rb_tree.hpp:375
Definition: rb_tree.hpp:48
void free_node(node *n, bool recursive)
Definition: rb_tree.hpp:520

◆ find_next_node()

node* std::rb_tree_base< internal::rb_tree_traits< TKey > , TAllocator >::find_next_node ( node *  n) const
inlineinherited
276  {
277  node* next(0);
278  if (n != 0)
279  {
280  if (n->right != &ms_sentinel)
281  {
282  next = n->right;
283  while (next->left != &ms_sentinel)
284  next = next->left;
285  }
286  else if (n->parent != &ms_sentinel)
287  {
288  if (n == n->parent->left)
289  return n->parent;
290  else
291  {
292  next = n;
293  while (next->parent != &ms_sentinel)
294  {
295  if (next == next->parent->right)
296  next = next->parent;
297  else
298  {
299  return next->parent;
300  }
301  }
302  next = 0;
303  }
304  }
305  else // 'n' is root.
306  {
307  assert(n == m_root);
308  }
309  }
310  return next;
311  }

◆ find_node()

node* std::rb_tree_base< internal::rb_tree_traits< TKey > , TAllocator >::find_node ( const key_type key)
inlineinherited
144  {
145  node* iter(m_root);
146  while (iter != &ms_sentinel)
147  {
148  const key_type& iter_key = iter->value.get_key();
149  if (iter_key < key)
150  iter = iter->right;
151  else if (key < iter_key)
152  iter = iter->left;
153  else // key == iter->key
154  return iter;
155  }
156  return 0; // not found
157  }
typename internal::rb_tree_traits< TKey > ::key_type key_type
Definition: rb_tree.hpp:69

◆ free_node()

void std::rb_tree_base< internal::rb_tree_traits< TKey > , TAllocator >::free_node ( node *  n,
bool  recursive 
)
inlineinherited
521  {
522  if (recursive)
523  {
524  if (n->left != &ms_sentinel)
525  free_node(n->left, true);
526  if (n->right != &ms_sentinel)
527  free_node(n->right, true);
528  }
529  if (n != &ms_sentinel)
530  {
531  n->~node();
532  m_allocator.deallocate(n, sizeof(node));
533  }
534  }
void free_node(node *n, bool recursive)
Definition: rb_tree.hpp:520

◆ get_begin_node()

node* std::rb_tree_base< internal::rb_tree_traits< TKey > , TAllocator >::get_begin_node ( ) const
inlineinherited
264  {
265  node* iter(0);
266  if (m_root != &ms_sentinel)
267  {
268  iter = m_root;
269  while (iter->left != &ms_sentinel)
270  iter = iter->left;
271  }
272  return iter;
273  }

◆ insert()

node* std::rb_tree_base< internal::rb_tree_traits< TKey > , TAllocator >::insert ( const value_type v)
inlineinherited
105  {
106  node* iter(m_root);
107  node* parent(&ms_sentinel);
108  while (iter != &ms_sentinel)
109  {
110  parent = iter;
111  if (iter->value.get_key() < v.get_key())
112  iter = iter->right;
113  else if (v.get_key() < iter->value.get_key())
114  iter = iter->left;
115  else // v.key == iter->key
116  return iter;
117  }
118 
119  node* new_node = alloc_node();
120  new_node->color = red;
121  new_node->value = v;
122  new_node->left = &ms_sentinel;
123  new_node->right = &ms_sentinel;
124  new_node->parent = parent;
125  if (parent != &ms_sentinel)
126  {
127  if (v.get_key() < parent->value.get_key())
128  parent->left = new_node;
129  else
130  parent->right = new_node;
131  }
132  else // empty tree
133  {
134  m_root = new_node;
135  }
136 
137  rebalance(new_node);
138  validate();
139  ++m_size;
140  return new_node;
141  }
Definition: rb_tree.hpp:47
void rebalance(node *new_node)
Definition: rb_tree.hpp:317

◆ num_nodes()

size_type std::rb_tree_base< internal::rb_tree_traits< TKey > , TAllocator >::num_nodes ( const node *  n) const
inlineinherited
314  {
315  return n == &ms_sentinel ? 0 : 1 + num_nodes(n->left) + num_nodes(n->right);
316  }
size_type num_nodes(const node *n) const
Definition: rb_tree.hpp:313

◆ rebalance()

void std::rb_tree_base< internal::rb_tree_traits< TKey > , TAllocator >::rebalance ( node *  new_node)
inlineinherited
318  {
319  assert(new_node->color == red);
320  node* iter(new_node);
321  while (iter->parent->color == red)
322  {
323  node* grandparent(iter->parent->parent);
324  if (iter->parent == grandparent->left)
325  {
326  node* uncle = grandparent->right;
327 
328  if (uncle->color == red)
329  {
330  iter->parent->color = black;
331  uncle->color = black;
332  grandparent->color = red;
333  iter = grandparent;
334  }
335  else
336  {
337  if (iter == iter->parent->right)
338  {
339  iter = iter->parent;
340  rotate_left(iter);
341  }
342  grandparent = iter->parent->parent;
343  iter->parent->color = black;
344  grandparent->color = red;
345  rotate_right(grandparent);
346  }
347  }
348  else
349  {
350  node* uncle = grandparent->left;
351  if (uncle->color == red)
352  {
353  grandparent->color = red;
354  iter->parent->color = black;
355  uncle->color = black;
356  iter = grandparent;
357  }
358  else
359  {
360  if (iter == iter->parent->left)
361  {
362  iter = iter->parent;
363  rotate_right(iter);
364  }
365  grandparent = iter->parent->parent;
366  iter->parent->color = black;
367  grandparent->color = red;
368  rotate_left(grandparent);
369  }
370  }
371  }
372  m_root->color = black;
373  }
Definition: rb_tree.hpp:48
void rotate_left(node *n)
Definition: rb_tree.hpp:467
Definition: rb_tree.hpp:47
void rotate_right(node *n)
Definition: rb_tree.hpp:491

◆ rebalance_after_erase()

void std::rb_tree_base< internal::rb_tree_traits< TKey > , TAllocator >::rebalance_after_erase ( node *  n)
inlineinherited
376  {
377  node* iter(n);
378  while (iter != m_root && iter->color == black)
379  {
380  if (iter == iter->parent->left)
381  {
382  node* sibling = iter->parent->right;
383  if (sibling->color == red)
384  {
385  sibling->color = black;
386  iter->parent->color = red;
387  rotate_left(iter->parent);
388  sibling = iter->parent->right;
389  }
390  if (sibling->left->color == black && sibling->right->color == black)
391  {
392  sibling->color = red;
393  iter = iter->parent;
394  }
395  else
396  {
397  if (sibling->right->color == black)
398  {
399  sibling->left->color = black;
400  sibling->color = red;
401  rotate_right(sibling);
402  sibling = iter->parent->right;
403  }
404  sibling->color = iter->parent->color;
405  iter->parent->color = black;
406  sibling->right->color = black;
407  rotate_left(iter->parent);
408  iter = m_root;
409  }
410  }
411  else // iter == right child
412  {
413  node* sibling = iter->parent->left;
414  if (sibling->color == red)
415  {
416  sibling->color = black;
417  iter->parent->color = red;
418  rotate_right(iter->parent);
419  sibling = iter->parent->left;
420  }
421  if (sibling->left->color == black && sibling->right->color == black)
422  {
423  sibling->color = red;
424  iter = iter->parent;
425  }
426  else
427  {
428  if (sibling->left->color == black)
429  {
430  sibling->right->color = black;
431  sibling->color = red;
432  rotate_left(sibling);
433  sibling = iter->parent->left;
434  }
435  sibling->color = iter->parent->color;
436  iter->parent->color = black;
437  sibling->left->color = black;
438  rotate_right(iter->parent);
439  iter = m_root;
440  }
441  }
442  }
443  iter->color = black;
444  }
Definition: rb_tree.hpp:48
void rotate_left(node *n)
Definition: rb_tree.hpp:467
Definition: rb_tree.hpp:47
void rotate_right(node *n)
Definition: rb_tree.hpp:491

◆ rotate_left()

void std::rb_tree_base< internal::rb_tree_traits< TKey > , TAllocator >::rotate_left ( node *  n)
inlineinherited
468  {
469  // Right child's left child becomes n's right child.
470  node* rightChild = n->right;
471  n->right = rightChild->left;
472  if (n->right != &ms_sentinel)
473  n->right->parent = n;
474 
475  // n's right child replaces n
476  rightChild->parent = n->parent;
477  if (n->parent == &ms_sentinel)
478  {
479  m_root = rightChild;
480  }
481  else
482  {
483  if (n == n->parent->left)
484  n->parent->left = rightChild;
485  else
486  n->parent->right = rightChild;
487  }
488  rightChild->left = n;
489  n->parent = rightChild;
490  }

◆ rotate_right()

void std::rb_tree_base< internal::rb_tree_traits< TKey > , TAllocator >::rotate_right ( node *  n)
inlineinherited
492  {
493  node* leftChild(n->left);
494  n->left = leftChild->right;
495 
496  if (n->left != &ms_sentinel)
497  n->left->parent = n;
498 
499  leftChild->parent = n->parent;
500  if (n->parent == &ms_sentinel)
501  {
502  m_root = leftChild;
503  }
504  else
505  {
506  // Substitute us in the parent list with left child.
507  if (n == n->parent->left)
508  n->parent->left = leftChild;
509  else
510  n->parent->right = leftChild;
511  }
512  leftChild->right = n;
513  n->parent = leftChild;
514  }

◆ size()

size_type std::rb_tree_base< internal::rb_tree_traits< TKey > , TAllocator >::size ( ) const
inlineinherited
237  {
238  return m_size;
239  }

◆ swap()

void std::rb_tree_base< internal::rb_tree_traits< TKey > , TAllocator >::swap ( rb_tree_base< internal::rb_tree_traits< TKey >, TAllocator > &  other)
inlineinherited
221  {
222  if (&other != this)
223  {
224  validate();
225  assert(m_allocator == other.m_allocator);
226  std::swap(m_root, other.m_root);
227  std::swap(m_size, other.m_size);
228  validate();
229  }
230  }
void swap(TAssignable &a, TAssignable &b)
Definition: algorithm.hpp:205

◆ traverse()

void std::rb_tree_base< internal::rb_tree_traits< TKey > , TAllocator >::traverse ( TravFunc  func)
inlineinherited
258  {
259  int depth(0);
260  traverse_node(m_root, func, depth);
261  }
void traverse_node(node *n, TravFunc func, int depth)
Definition: rb_tree.hpp:243

◆ traverse_node()

void std::rb_tree_base< internal::rb_tree_traits< TKey > , TAllocator >::traverse_node ( node *  n,
TravFunc  func,
int  depth 
)
inlineinherited
244  {
245  int left(-1);
246  if (n->parent != &ms_sentinel)
247  {
248  left = n->parent->left == n;
249  }
250  func(n, left, depth);
251  if (n->left != &ms_sentinel)
252  traverse_node(n->left, func, depth + 1);
253  if (n->right != &ms_sentinel)
254  traverse_node(n->right, func, depth + 1);
255  }
void traverse_node(node *n, TravFunc func, int depth)
Definition: rb_tree.hpp:243

◆ validate()

void std::rb_tree_base< internal::rb_tree_traits< TKey > , TAllocator >::validate ( ) const
inlineinherited
447  {
448  assert(m_root->color == black);
449  validate_node(m_root);
450  }
Definition: rb_tree.hpp:48
void validate_node(node *n) const
Definition: rb_tree.hpp:451

◆ validate_node()

void std::rb_tree_base< internal::rb_tree_traits< TKey > , TAllocator >::validate_node ( node *  n) const
inlineinherited
452  {
453  // - we're child of our parent.
454  assert(n->parent == &ms_sentinel ||
455  n->parent->left == n || n->parent->right == n);
456  // - both children of red node are black
457  if (n->color == red)
458  {
459  assert(n->left->color == black);
460  assert(n->right->color == black);
461  }
462  if (n->left != &ms_sentinel)
463  validate_node(n->left);
464  if (n->right != &ms_sentinel)
465  validate_node(n->right);
466  }
Definition: rb_tree.hpp:48
Definition: rb_tree.hpp:47
void validate_node(node *n) const
Definition: rb_tree.hpp:451

Die Dokumentation für diese Klasse wurde erzeugt aufgrund der Datei: