TreeNode.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. /*
  2. * Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Assertions.h>
  8. #include <AK/TypeCasts.h>
  9. #include <LibJS/Heap/Cell.h>
  10. #include <LibJS/Heap/GCPtr.h>
  11. #include <LibWeb/Forward.h>
  12. namespace Web {
  13. template<typename T>
  14. class TreeNode {
  15. public:
  16. T* parent() { return m_parent; }
  17. T const* parent() const { return m_parent; }
  18. bool has_children() const { return m_first_child; }
  19. T* next_sibling() { return m_next_sibling; }
  20. T* previous_sibling() { return m_previous_sibling; }
  21. T* first_child() { return m_first_child; }
  22. T* last_child() { return m_last_child; }
  23. T const* next_sibling() const { return m_next_sibling; }
  24. T const* previous_sibling() const { return m_previous_sibling; }
  25. T const* first_child() const { return m_first_child; }
  26. T const* last_child() const { return m_last_child; }
  27. // https://dom.spec.whatwg.org/#concept-tree-index
  28. size_t index() const
  29. {
  30. // The index of an object is its number of preceding siblings, or 0 if it has none.
  31. size_t index = 0;
  32. for (auto* node = previous_sibling(); node; node = node->previous_sibling())
  33. ++index;
  34. return index;
  35. }
  36. template<typename ChildType>
  37. Optional<size_t> index_of_child(T const& search_child)
  38. {
  39. VERIFY(search_child.parent() == this);
  40. size_t index = 0;
  41. auto* child = first_child();
  42. VERIFY(child);
  43. do {
  44. if (!is<ChildType>(child))
  45. continue;
  46. if (child == &search_child)
  47. return index;
  48. index++;
  49. } while (child && (child = child->next_sibling()));
  50. return {};
  51. }
  52. bool is_ancestor_of(TreeNode const&) const;
  53. bool is_inclusive_ancestor_of(TreeNode const&) const;
  54. void append_child(JS::NonnullGCPtr<T> node);
  55. void prepend_child(JS::NonnullGCPtr<T> node);
  56. void insert_before(JS::NonnullGCPtr<T> node, JS::GCPtr<T> child);
  57. void remove_child(JS::NonnullGCPtr<T> node);
  58. T* next_in_pre_order()
  59. {
  60. if (first_child())
  61. return first_child();
  62. T* node;
  63. if (!(node = next_sibling())) {
  64. node = parent();
  65. while (node && !node->next_sibling())
  66. node = node->parent();
  67. if (node)
  68. node = node->next_sibling();
  69. }
  70. return node;
  71. }
  72. T* next_in_pre_order(T const* stay_within)
  73. {
  74. if (first_child())
  75. return first_child();
  76. T* node = static_cast<T*>(this);
  77. T* next = nullptr;
  78. while (!(next = node->next_sibling())) {
  79. node = node->parent();
  80. if (!node || node == stay_within)
  81. return nullptr;
  82. }
  83. return next;
  84. }
  85. T const* next_in_pre_order() const
  86. {
  87. return const_cast<TreeNode*>(this)->next_in_pre_order();
  88. }
  89. T const* next_in_pre_order(T const* stay_within) const
  90. {
  91. return const_cast<TreeNode*>(this)->next_in_pre_order(stay_within);
  92. }
  93. T* previous_in_pre_order()
  94. {
  95. if (auto* node = previous_sibling()) {
  96. while (node->last_child())
  97. node = node->last_child();
  98. return node;
  99. }
  100. return parent();
  101. }
  102. T const* previous_in_pre_order() const
  103. {
  104. return const_cast<TreeNode*>(this)->previous_in_pre_order();
  105. }
  106. template<typename Callback>
  107. IterationDecision for_each_in_inclusive_subtree(Callback callback) const
  108. {
  109. if (callback(static_cast<T const&>(*this)) == IterationDecision::Break)
  110. return IterationDecision::Break;
  111. for (auto* child = first_child(); child; child = child->next_sibling()) {
  112. if (child->for_each_in_inclusive_subtree(callback) == IterationDecision::Break)
  113. return IterationDecision::Break;
  114. }
  115. return IterationDecision::Continue;
  116. }
  117. template<typename Callback>
  118. IterationDecision for_each_in_inclusive_subtree(Callback callback)
  119. {
  120. if (callback(static_cast<T&>(*this)) == IterationDecision::Break)
  121. return IterationDecision::Break;
  122. for (auto* child = first_child(); child; child = child->next_sibling()) {
  123. if (child->for_each_in_inclusive_subtree(callback) == IterationDecision::Break)
  124. return IterationDecision::Break;
  125. }
  126. return IterationDecision::Continue;
  127. }
  128. template<typename U, typename Callback>
  129. IterationDecision for_each_in_inclusive_subtree_of_type(Callback callback)
  130. {
  131. if (is<U>(static_cast<T const&>(*this))) {
  132. if (callback(static_cast<U&>(*this)) == IterationDecision::Break)
  133. return IterationDecision::Break;
  134. }
  135. for (auto* child = first_child(); child; child = child->next_sibling()) {
  136. if (child->template for_each_in_inclusive_subtree_of_type<U>(callback) == IterationDecision::Break)
  137. return IterationDecision::Break;
  138. }
  139. return IterationDecision::Continue;
  140. }
  141. template<typename U, typename Callback>
  142. IterationDecision for_each_in_inclusive_subtree_of_type(Callback callback) const
  143. {
  144. if (is<U>(static_cast<T const&>(*this))) {
  145. if (callback(static_cast<U const&>(*this)) == IterationDecision::Break)
  146. return IterationDecision::Break;
  147. }
  148. for (auto* child = first_child(); child; child = child->next_sibling()) {
  149. if (child->template for_each_in_inclusive_subtree_of_type<U>(callback) == IterationDecision::Break)
  150. return IterationDecision::Break;
  151. }
  152. return IterationDecision::Continue;
  153. }
  154. template<typename Callback>
  155. IterationDecision for_each_in_subtree(Callback callback) const
  156. {
  157. for (auto* child = first_child(); child; child = child->next_sibling()) {
  158. if (child->for_each_in_inclusive_subtree(callback) == IterationDecision::Break)
  159. return IterationDecision::Break;
  160. }
  161. return IterationDecision::Continue;
  162. }
  163. template<typename Callback>
  164. IterationDecision for_each_in_subtree(Callback callback)
  165. {
  166. for (auto* child = first_child(); child; child = child->next_sibling()) {
  167. if (child->for_each_in_inclusive_subtree(callback) == IterationDecision::Break)
  168. return IterationDecision::Break;
  169. }
  170. return IterationDecision::Continue;
  171. }
  172. template<typename U, typename Callback>
  173. IterationDecision for_each_in_subtree_of_type(Callback callback)
  174. {
  175. for (auto* child = first_child(); child; child = child->next_sibling()) {
  176. if (child->template for_each_in_inclusive_subtree_of_type<U>(callback) == IterationDecision::Break)
  177. return IterationDecision::Break;
  178. }
  179. return IterationDecision::Continue;
  180. }
  181. template<typename U, typename Callback>
  182. IterationDecision for_each_in_subtree_of_type(Callback callback) const
  183. {
  184. for (auto* child = first_child(); child; child = child->next_sibling()) {
  185. if (child->template for_each_in_inclusive_subtree_of_type<U>(callback) == IterationDecision::Break)
  186. return IterationDecision::Break;
  187. }
  188. return IterationDecision::Continue;
  189. }
  190. template<typename Callback>
  191. void for_each_child(Callback callback) const
  192. {
  193. return const_cast<TreeNode*>(this)->template for_each_child(move(callback));
  194. }
  195. template<typename Callback>
  196. void for_each_child(Callback callback)
  197. {
  198. for (auto* node = first_child(); node; node = node->next_sibling())
  199. callback(*node);
  200. }
  201. template<typename U, typename Callback>
  202. void for_each_child_of_type(Callback callback)
  203. {
  204. for (auto* node = first_child(); node; node = node->next_sibling()) {
  205. if (is<U>(node))
  206. callback(verify_cast<U>(*node));
  207. }
  208. }
  209. template<typename U, typename Callback>
  210. void for_each_child_of_type(Callback callback) const
  211. {
  212. return const_cast<TreeNode*>(this)->template for_each_child_of_type<U>(move(callback));
  213. }
  214. template<typename U>
  215. U const* next_sibling_of_type() const
  216. {
  217. return const_cast<TreeNode*>(this)->template next_sibling_of_type<U>();
  218. }
  219. template<typename U>
  220. inline U* next_sibling_of_type()
  221. {
  222. for (auto* sibling = next_sibling(); sibling; sibling = sibling->next_sibling()) {
  223. if (is<U>(*sibling))
  224. return &verify_cast<U>(*sibling);
  225. }
  226. return nullptr;
  227. }
  228. template<typename U>
  229. U const* previous_sibling_of_type() const
  230. {
  231. return const_cast<TreeNode*>(this)->template previous_sibling_of_type<U>();
  232. }
  233. template<typename U>
  234. U* previous_sibling_of_type()
  235. {
  236. for (auto* sibling = previous_sibling(); sibling; sibling = sibling->previous_sibling()) {
  237. if (is<U>(*sibling))
  238. return &verify_cast<U>(*sibling);
  239. }
  240. return nullptr;
  241. }
  242. template<typename U>
  243. U const* first_child_of_type() const
  244. {
  245. return const_cast<TreeNode*>(this)->template first_child_of_type<U>();
  246. }
  247. template<typename U>
  248. U const* last_child_of_type() const
  249. {
  250. return const_cast<TreeNode*>(this)->template last_child_of_type<U>();
  251. }
  252. template<typename U>
  253. U* first_child_of_type()
  254. {
  255. for (auto* child = first_child(); child; child = child->next_sibling()) {
  256. if (is<U>(*child))
  257. return &verify_cast<U>(*child);
  258. }
  259. return nullptr;
  260. }
  261. template<typename U>
  262. U* last_child_of_type()
  263. {
  264. for (auto* child = last_child(); child; child = child->previous_sibling()) {
  265. if (is<U>(*child))
  266. return &verify_cast<U>(*child);
  267. }
  268. return nullptr;
  269. }
  270. template<typename U>
  271. U const* first_ancestor_of_type() const
  272. {
  273. return const_cast<TreeNode*>(this)->template first_ancestor_of_type<U>();
  274. }
  275. template<typename U>
  276. U* first_ancestor_of_type()
  277. {
  278. for (auto* ancestor = parent(); ancestor; ancestor = ancestor->parent()) {
  279. if (is<U>(*ancestor))
  280. return &verify_cast<U>(*ancestor);
  281. }
  282. return nullptr;
  283. }
  284. ~TreeNode() = default;
  285. protected:
  286. TreeNode() = default;
  287. void visit_edges(JS::Cell::Visitor& visitor)
  288. {
  289. visitor.visit(m_parent);
  290. visitor.visit(m_first_child);
  291. visitor.visit(m_last_child);
  292. visitor.visit(m_next_sibling);
  293. visitor.visit(m_previous_sibling);
  294. }
  295. private:
  296. T* m_parent { nullptr };
  297. T* m_first_child { nullptr };
  298. T* m_last_child { nullptr };
  299. T* m_next_sibling { nullptr };
  300. T* m_previous_sibling { nullptr };
  301. };
  302. template<typename T>
  303. inline void TreeNode<T>::remove_child(JS::NonnullGCPtr<T> node)
  304. {
  305. VERIFY(node->m_parent == this);
  306. if (m_first_child == node)
  307. m_first_child = node->m_next_sibling;
  308. if (m_last_child == node)
  309. m_last_child = node->m_previous_sibling;
  310. if (node->m_next_sibling)
  311. node->m_next_sibling->m_previous_sibling = node->m_previous_sibling;
  312. if (node->m_previous_sibling)
  313. node->m_previous_sibling->m_next_sibling = node->m_next_sibling;
  314. node->m_next_sibling = nullptr;
  315. node->m_previous_sibling = nullptr;
  316. node->m_parent = nullptr;
  317. }
  318. template<typename T>
  319. inline void TreeNode<T>::append_child(JS::NonnullGCPtr<T> node)
  320. {
  321. VERIFY(!node->m_parent);
  322. if (m_last_child)
  323. m_last_child->m_next_sibling = node.ptr();
  324. node->m_previous_sibling = m_last_child;
  325. node->m_parent = static_cast<T*>(this);
  326. m_last_child = node.ptr();
  327. if (!m_first_child)
  328. m_first_child = m_last_child;
  329. }
  330. template<typename T>
  331. inline void TreeNode<T>::insert_before(JS::NonnullGCPtr<T> node, JS::GCPtr<T> child)
  332. {
  333. if (!child)
  334. return append_child(move(node));
  335. VERIFY(!node->m_parent);
  336. VERIFY(child->parent() == this);
  337. node->m_previous_sibling = child->m_previous_sibling;
  338. node->m_next_sibling = child;
  339. if (child->m_previous_sibling)
  340. child->m_previous_sibling->m_next_sibling = node;
  341. if (m_first_child == child)
  342. m_first_child = node;
  343. child->m_previous_sibling = node;
  344. node->m_parent = static_cast<T*>(this);
  345. }
  346. template<typename T>
  347. inline void TreeNode<T>::prepend_child(JS::NonnullGCPtr<T> node)
  348. {
  349. VERIFY(!node->m_parent);
  350. if (m_first_child)
  351. m_first_child->m_previous_sibling = node.ptr();
  352. node->m_next_sibling = m_first_child;
  353. node->m_parent = static_cast<T*>(this);
  354. m_first_child = node.ptr();
  355. if (!m_last_child)
  356. m_last_child = m_first_child;
  357. node->inserted_into(static_cast<T&>(*this));
  358. static_cast<T*>(this)->children_changed();
  359. }
  360. template<typename T>
  361. inline bool TreeNode<T>::is_ancestor_of(TreeNode<T> const& other) const
  362. {
  363. for (auto* ancestor = other.parent(); ancestor; ancestor = ancestor->parent()) {
  364. if (ancestor == this)
  365. return true;
  366. }
  367. return false;
  368. }
  369. template<typename T>
  370. inline bool TreeNode<T>::is_inclusive_ancestor_of(TreeNode<T> const& other) const
  371. {
  372. return &other == this || is_ancestor_of(other);
  373. }
  374. }