TreeBase.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #pragma once
  24. #include "Allocator.h"
  25. #include "Swap.h"
  26. // Based on Red Black Trees by Julienne Walker
  27. // http://eternallyconfuzzled.com/tuts/datastructures/jsw_tut_rbtree.aspx
  28. /// Red-black tree node base
  29. struct TreeNodeBase
  30. {
  31. /// Construct
  32. TreeNodeBase() :
  33. parent_(0),
  34. isRed_(true)
  35. {
  36. link_[0] = 0;
  37. link_[1] = 0;
  38. }
  39. /// Set a child link, adjusting the child's parent as necessary
  40. void SetChild(unsigned dir, TreeNodeBase* newChild)
  41. {
  42. link_[dir] = newChild;
  43. if (newChild)
  44. newChild->parent_ = this;
  45. }
  46. /// Parent node
  47. TreeNodeBase* parent_;
  48. /// Child links
  49. TreeNodeBase* link_[2];
  50. /// Color flag
  51. bool isRed_;
  52. };
  53. /// Red-black tree iterator base class
  54. class TreeIteratorBase
  55. {
  56. public:
  57. /// Construct
  58. TreeIteratorBase() :
  59. ptr_(0),
  60. prev_(0)
  61. {
  62. }
  63. /// Construct with a node pointer
  64. TreeIteratorBase(TreeNodeBase* ptr) :
  65. ptr_(ptr),
  66. prev_(0)
  67. {
  68. }
  69. /// Test for equality with another iterator
  70. bool operator == (const TreeIteratorBase& rhs) const { return ptr_ == rhs.ptr_; }
  71. /// Test for inequality with another iterator
  72. bool operator != (const TreeIteratorBase& rhs) const { return ptr_ != rhs.ptr_; }
  73. /// Go to the next node
  74. void GotoNext()
  75. {
  76. if (!ptr_)
  77. {
  78. prev_ = 0;
  79. return;
  80. }
  81. prev_ = ptr_;
  82. if (!ptr_->link_[1])
  83. {
  84. while ((ptr_->parent_) && (ptr_->parent_->link_[1] == ptr_))
  85. ptr_ = ptr_->parent_;
  86. ptr_ = ptr_->parent_;
  87. return;
  88. }
  89. ptr_ = ptr_->link_[1];
  90. while (ptr_->link_[0])
  91. ptr_ = ptr_->link_[0];
  92. }
  93. /// Go to the previous node
  94. void GotoPrev()
  95. {
  96. if (!ptr_)
  97. {
  98. ptr_ = prev_;
  99. prev_ = 0;
  100. return;
  101. }
  102. prev_ = 0;
  103. if (!ptr_->link_[0])
  104. {
  105. while ((ptr_->parent_) && (ptr_->parent_->link_[0] == ptr_))
  106. ptr_ = ptr_->parent_;
  107. ptr_ = ptr_->parent_;
  108. return;
  109. }
  110. ptr_ = ptr_->link_[0];
  111. while (ptr_->link_[1])
  112. ptr_ = ptr_->link_[1];
  113. }
  114. /// Current node pointer
  115. TreeNodeBase* ptr_;
  116. /// Previous node pointer, needed to back from the end
  117. TreeNodeBase* prev_;
  118. };
  119. /// Red-black tree base class
  120. class TreeBase
  121. {
  122. public:
  123. /// Construct
  124. TreeBase() :
  125. root_(0),
  126. allocator_(0),
  127. size_(0)
  128. {
  129. }
  130. /// Swap with another tree
  131. void Swap(TreeBase& rhs)
  132. {
  133. ::Swap(root_, rhs.root_);
  134. ::Swap(allocator_, rhs.allocator_);
  135. ::Swap(size_, rhs.size_);
  136. }
  137. protected:
  138. /// Check whether a node is red
  139. bool IsRed(TreeNodeBase* node) const { return (node) && (node->isRed_); }
  140. /// Single rotation
  141. TreeNodeBase* RotateSingle(TreeNodeBase* node, unsigned dir)
  142. {
  143. TreeNodeBase* save = node->link_[!dir];
  144. node->SetChild(!dir, save->link_[dir]);
  145. save->SetChild(dir, node);
  146. node->isRed_ = true;
  147. save->isRed_ = false;
  148. return save;
  149. }
  150. /// Double rotation
  151. TreeNodeBase* RotateDouble(TreeNodeBase* node, unsigned dir)
  152. {
  153. node->SetChild(!dir, RotateSingle(node->link_[!dir], !dir));
  154. return RotateSingle(node, dir);
  155. }
  156. /// Root node
  157. TreeNodeBase* root_;
  158. /// Node allocator
  159. AllocatorBlock* allocator_;
  160. /// Number of nodes
  161. unsigned size_;
  162. };