TreeBase.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 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. /// Child links.
  47. TreeNodeBase* link_[2];
  48. /// Parent node.
  49. TreeNodeBase* parent_;
  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. explicit 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. return;
  78. prev_ = ptr_;
  79. if (!ptr_->link_[1])
  80. {
  81. while (ptr_->parent_ && ptr_->parent_->link_[1] == ptr_)
  82. ptr_ = ptr_->parent_;
  83. ptr_ = ptr_->parent_;
  84. return;
  85. }
  86. ptr_ = ptr_->link_[1];
  87. while (ptr_->link_[0])
  88. ptr_ = ptr_->link_[0];
  89. }
  90. /// Go to the previous node.
  91. void GotoPrev()
  92. {
  93. if (!ptr_)
  94. {
  95. ptr_ = prev_;
  96. prev_ = 0;
  97. return;
  98. }
  99. prev_ = 0;
  100. if (!ptr_->link_[0])
  101. {
  102. while (ptr_->parent_ && ptr_->parent_->link_[0] == ptr_)
  103. ptr_ = ptr_->parent_;
  104. ptr_ = ptr_->parent_;
  105. return;
  106. }
  107. ptr_ = ptr_->link_[0];
  108. while (ptr_->link_[1])
  109. ptr_ = ptr_->link_[1];
  110. }
  111. /// Current node pointer.
  112. TreeNodeBase* ptr_;
  113. /// Previous node pointer, needed to go back from the end.
  114. TreeNodeBase* prev_;
  115. };
  116. /// Red-black tree base class.
  117. class TreeBase
  118. {
  119. public:
  120. /// Construct.
  121. TreeBase() :
  122. head_(0),
  123. allocator_(0),
  124. size_(0)
  125. {
  126. }
  127. /// Swap with another tree.
  128. void Swap(TreeBase& rhs)
  129. {
  130. ::Swap(head_, rhs.head_);
  131. ::Swap(allocator_, rhs.allocator_);
  132. ::Swap(size_, rhs.size_);
  133. }
  134. protected:
  135. /// Check whether a node is red.
  136. bool IsRed(TreeNodeBase* node) const { return node && node->isRed_; }
  137. /// Single rotation.
  138. TreeNodeBase* RotateSingle(TreeNodeBase* node, unsigned dir)
  139. {
  140. TreeNodeBase* save = node->link_[!dir];
  141. node->SetChild(!dir, save->link_[dir]);
  142. save->SetChild(dir, node);
  143. node->isRed_ = true;
  144. save->isRed_ = false;
  145. return save;
  146. }
  147. /// Double rotation.
  148. TreeNodeBase* RotateDouble(TreeNodeBase* node, unsigned dir)
  149. {
  150. node->SetChild(!dir, RotateSingle(node->link_[!dir], !dir));
  151. return RotateSingle(node, dir);
  152. }
  153. /// Head node.
  154. TreeNodeBase* head_;
  155. /// Node allocator.
  156. AllocatorBlock* allocator_;
  157. /// Number of nodes.
  158. unsigned size_;
  159. };