TreeBase.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 class.
  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. struct TreeIteratorBase
  55. {
  56. /// Construct.
  57. TreeIteratorBase() :
  58. ptr_(0),
  59. prev_(0)
  60. {
  61. }
  62. /// Construct with a node pointer.
  63. explicit TreeIteratorBase(TreeNodeBase* ptr) :
  64. ptr_(ptr),
  65. prev_(0)
  66. {
  67. }
  68. /// Test for equality with another iterator.
  69. bool operator == (const TreeIteratorBase& rhs) const { return ptr_ == rhs.ptr_; }
  70. /// Test for inequality with another iterator.
  71. bool operator != (const TreeIteratorBase& rhs) const { return ptr_ != rhs.ptr_; }
  72. /// Go to the next node.
  73. void GotoNext()
  74. {
  75. if (!ptr_)
  76. return;
  77. prev_ = ptr_;
  78. if (!ptr_->link_[1])
  79. {
  80. while (ptr_->parent_ && ptr_->parent_->link_[1] == ptr_)
  81. ptr_ = ptr_->parent_;
  82. ptr_ = ptr_->parent_;
  83. return;
  84. }
  85. ptr_ = ptr_->link_[1];
  86. while (ptr_->link_[0])
  87. ptr_ = ptr_->link_[0];
  88. }
  89. /// Go to the previous node.
  90. void GotoPrev()
  91. {
  92. if (!ptr_)
  93. {
  94. ptr_ = prev_;
  95. prev_ = 0;
  96. return;
  97. }
  98. prev_ = 0;
  99. if (!ptr_->link_[0])
  100. {
  101. while (ptr_->parent_ && ptr_->parent_->link_[0] == ptr_)
  102. ptr_ = ptr_->parent_;
  103. ptr_ = ptr_->parent_;
  104. return;
  105. }
  106. ptr_ = ptr_->link_[0];
  107. while (ptr_->link_[1])
  108. ptr_ = ptr_->link_[1];
  109. }
  110. /// Current node pointer.
  111. TreeNodeBase* ptr_;
  112. /// Previous node pointer, needed to go back from the end.
  113. TreeNodeBase* prev_;
  114. };
  115. /// Red-black tree base class.
  116. class TreeBase
  117. {
  118. public:
  119. /// Construct.
  120. TreeBase() :
  121. head_(0),
  122. allocator_(0),
  123. size_(0)
  124. {
  125. }
  126. /// Swap with another tree.
  127. void Swap(TreeBase& rhs)
  128. {
  129. ::Swap(head_, rhs.head_);
  130. ::Swap(allocator_, rhs.allocator_);
  131. ::Swap(size_, rhs.size_);
  132. }
  133. protected:
  134. /// Check whether a node is red.
  135. bool IsRed(TreeNodeBase* node) const { return node && node->isRed_; }
  136. /// Single rotation.
  137. TreeNodeBase* RotateSingle(TreeNodeBase* node, unsigned dir)
  138. {
  139. TreeNodeBase* save = node->link_[!dir];
  140. node->SetChild(!dir, save->link_[dir]);
  141. save->SetChild(dir, node);
  142. node->isRed_ = true;
  143. save->isRed_ = false;
  144. return save;
  145. }
  146. /// Double rotation.
  147. TreeNodeBase* RotateDouble(TreeNodeBase* node, unsigned dir)
  148. {
  149. node->SetChild(!dir, RotateSingle(node->link_[!dir], !dir));
  150. return RotateSingle(node, dir);
  151. }
  152. /// Head node.
  153. TreeNodeBase* head_;
  154. /// Node allocator.
  155. AllocatorBlock* allocator_;
  156. /// Number of nodes.
  157. unsigned size_;
  158. };