TreeNode.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. #pragma once
  2. #include <AK/Assertions.h>
  3. #include <AK/NonnullRefPtr.h>
  4. #include <AK/Weakable.h>
  5. template<typename T>
  6. class TreeNode : public Weakable<T> {
  7. public:
  8. void ref()
  9. {
  10. ASSERT(m_ref_count);
  11. ++m_ref_count;
  12. }
  13. void deref()
  14. {
  15. ASSERT(m_ref_count);
  16. if (!--m_ref_count) {
  17. if (m_next_sibling)
  18. m_next_sibling->m_previous_sibling = m_previous_sibling;
  19. if (m_previous_sibling)
  20. m_previous_sibling->m_next_sibling = m_next_sibling;
  21. T* next_child;
  22. for (auto* child = m_first_child; child; child = next_child) {
  23. next_child = child->m_next_sibling;
  24. child->m_parent = nullptr;
  25. child->deref();
  26. }
  27. delete static_cast<T*>(this);
  28. }
  29. }
  30. int ref_count() const { return m_ref_count; }
  31. T* parent() { return m_parent; }
  32. const T* parent() const { return m_parent; }
  33. bool has_children() const { return m_first_child; }
  34. T* next_sibling() { return m_next_sibling; }
  35. T* previous_sibling() { return m_previous_sibling; }
  36. T* first_child() { return m_first_child; }
  37. T* last_child() { return m_last_child; }
  38. const T* next_sibling() const { return m_next_sibling; }
  39. const T* previous_sibling() const { return m_previous_sibling; }
  40. const T* first_child() const { return m_first_child; }
  41. const T* last_child() const { return m_last_child; }
  42. int child_count() const
  43. {
  44. int count = 0;
  45. for (auto* child = first_child(); child; child = child->next_sibling())
  46. ++count;
  47. return count;
  48. }
  49. T* child_at_index(int index)
  50. {
  51. int count = 0;
  52. for (auto* child = first_child(); child; child = child->next_sibling()) {
  53. if (count == index)
  54. return child;
  55. ++count;
  56. }
  57. return nullptr;
  58. }
  59. const T* child_at_index(int index) const
  60. {
  61. return const_cast<TreeNode*>(this)->child_at_index(index);
  62. }
  63. bool is_ancestor_of(const TreeNode&) const;
  64. void prepend_child(NonnullRefPtr<T> node, bool call_inserted_into = true);
  65. void append_child(NonnullRefPtr<T> node, bool call_inserted_into = true);
  66. NonnullRefPtr<T> remove_child(NonnullRefPtr<T> node, bool call_removed_from = true);
  67. void donate_all_children_to(T& node);
  68. bool is_child_allowed(const T&) const { return true; }
  69. T* next_in_pre_order()
  70. {
  71. if (first_child())
  72. return first_child();
  73. T* node;
  74. if (!(node = next_sibling())) {
  75. node = parent();
  76. while (node && !node->next_sibling())
  77. node = node->parent();
  78. if (node)
  79. node = node->next_sibling();
  80. }
  81. return node;
  82. }
  83. const T* next_in_pre_order() const
  84. {
  85. return const_cast<TreeNode*>(this)->next_in_pre_order();
  86. }
  87. template<typename Callback>
  88. IterationDecision for_each_in_subtree(Callback callback) const
  89. {
  90. if (callback(static_cast<const T&>(*this)) == IterationDecision::Break)
  91. return IterationDecision::Break;
  92. for (auto* child = first_child(); child; child = child->next_sibling()) {
  93. if (child->for_each_in_subtree(callback) == IterationDecision::Break)
  94. return IterationDecision::Break;
  95. }
  96. return IterationDecision::Continue;
  97. }
  98. template<typename Callback>
  99. IterationDecision for_each_in_subtree(Callback callback)
  100. {
  101. if (callback(static_cast<T&>(*this)) == IterationDecision::Break)
  102. return IterationDecision::Break;
  103. for (auto* child = first_child(); child; child = child->next_sibling()) {
  104. if (child->for_each_in_subtree(callback) == IterationDecision::Break)
  105. return IterationDecision::Break;
  106. }
  107. return IterationDecision::Continue;
  108. }
  109. protected:
  110. TreeNode() {}
  111. private:
  112. int m_ref_count { 1 };
  113. T* m_parent { nullptr };
  114. T* m_first_child { nullptr };
  115. T* m_last_child { nullptr };
  116. T* m_next_sibling { nullptr };
  117. T* m_previous_sibling { nullptr };
  118. };
  119. template<typename T>
  120. inline NonnullRefPtr<T> TreeNode<T>::remove_child(NonnullRefPtr<T> node, bool call_removed_from)
  121. {
  122. ASSERT(node->m_parent == this);
  123. if (m_first_child == node)
  124. m_first_child = node->m_next_sibling;
  125. if (m_last_child == node)
  126. m_last_child = node->m_previous_sibling;
  127. if (node->m_next_sibling)
  128. node->m_next_sibling->m_previous_sibling = node->m_previous_sibling;
  129. if (node->m_previous_sibling)
  130. node->m_previous_sibling->m_next_sibling = node->m_next_sibling;
  131. node->m_next_sibling = nullptr;
  132. node->m_previous_sibling = nullptr;
  133. node->m_parent = nullptr;
  134. if (call_removed_from)
  135. node->removed_from(static_cast<T&>(*this));
  136. node->deref();
  137. return node;
  138. }
  139. template<typename T>
  140. inline void TreeNode<T>::append_child(NonnullRefPtr<T> node, bool call_inserted_into)
  141. {
  142. ASSERT(!node->m_parent);
  143. if (!static_cast<T*>(this)->is_child_allowed(*node))
  144. return;
  145. if (m_last_child)
  146. m_last_child->m_next_sibling = node.ptr();
  147. node->m_previous_sibling = m_last_child;
  148. node->m_parent = static_cast<T*>(this);
  149. m_last_child = node.ptr();
  150. if (!m_first_child)
  151. m_first_child = m_last_child;
  152. if (call_inserted_into)
  153. node->inserted_into(static_cast<T&>(*this));
  154. (void)node.leak_ref();
  155. }
  156. template<typename T>
  157. inline void TreeNode<T>::prepend_child(NonnullRefPtr<T> node, bool call_inserted_into)
  158. {
  159. ASSERT(!node->m_parent);
  160. if (!static_cast<T*>(this)->is_child_allowed(*node))
  161. return;
  162. if (m_first_child)
  163. m_first_child->m_previous_sibling = node.ptr();
  164. node->m_next_sibling = m_first_child;
  165. node->m_parent = static_cast<T*>(this);
  166. m_first_child = node.ptr();
  167. if (!m_last_child)
  168. m_last_child = m_first_child;
  169. if (call_inserted_into)
  170. node->inserted_into(static_cast<T&>(*this));
  171. (void)node.leak_ref();
  172. }
  173. template<typename T>
  174. inline void TreeNode<T>::donate_all_children_to(T& node)
  175. {
  176. for (T* child = m_first_child; child != nullptr;) {
  177. T* next_child = child->m_next_sibling;
  178. child->m_parent = nullptr;
  179. child->m_next_sibling = nullptr;
  180. child->m_previous_sibling = nullptr;
  181. node.append_child(adopt(*child));
  182. child = next_child;
  183. }
  184. m_first_child = nullptr;
  185. m_last_child = nullptr;
  186. }
  187. template<typename T>
  188. inline bool TreeNode<T>::is_ancestor_of(const TreeNode<T>& other) const
  189. {
  190. for (auto* ancestor = other.parent(); ancestor; ancestor = ancestor->parent()) {
  191. if (ancestor == this)
  192. return true;
  193. }
  194. return false;
  195. }