2020-01-18 08:38:21 +00:00
|
|
|
/*
|
2024-10-04 11:19:50 +00:00
|
|
|
* Copyright (c) 2018-2022, Andreas Kling <andreas@ladybird.org>
|
2020-01-18 08:38:21 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2019-06-25 17:46:01 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/Assertions.h>
|
2020-07-26 15:16:18 +00:00
|
|
|
#include <AK/TypeCasts.h>
|
2024-11-14 15:01:23 +00:00
|
|
|
#include <LibGC/Ptr.h>
|
2023-01-28 16:26:03 +00:00
|
|
|
#include <LibJS/Heap/Cell.h>
|
2020-10-11 19:52:59 +00:00
|
|
|
#include <LibWeb/Forward.h>
|
2024-05-04 13:47:04 +00:00
|
|
|
#include <LibWeb/TraversalDecision.h>
|
2019-06-25 17:46:01 +00:00
|
|
|
|
2020-03-07 09:27:02 +00:00
|
|
|
namespace Web {
|
|
|
|
|
2019-06-25 17:46:01 +00:00
|
|
|
template<typename T>
|
2022-10-17 12:41:50 +00:00
|
|
|
class TreeNode {
|
2019-06-25 17:46:01 +00:00
|
|
|
public:
|
|
|
|
T* parent() { return m_parent; }
|
2022-10-17 12:41:50 +00:00
|
|
|
T const* parent() const { return m_parent; }
|
2019-06-25 17:46:01 +00:00
|
|
|
|
|
|
|
bool has_children() const { return m_first_child; }
|
|
|
|
T* next_sibling() { return m_next_sibling; }
|
|
|
|
T* previous_sibling() { return m_previous_sibling; }
|
|
|
|
T* first_child() { return m_first_child; }
|
|
|
|
T* last_child() { return m_last_child; }
|
2022-10-17 12:41:50 +00:00
|
|
|
T const* next_sibling() const { return m_next_sibling; }
|
|
|
|
T const* previous_sibling() const { return m_previous_sibling; }
|
|
|
|
T const* first_child() const { return m_first_child; }
|
|
|
|
T const* last_child() const { return m_last_child; }
|
2019-06-25 17:46:01 +00:00
|
|
|
|
2022-01-31 18:05:54 +00:00
|
|
|
// https://dom.spec.whatwg.org/#concept-tree-index
|
|
|
|
size_t index() const
|
|
|
|
{
|
|
|
|
// The index of an object is its number of preceding siblings, or 0 if it has none.
|
|
|
|
size_t index = 0;
|
|
|
|
for (auto* node = previous_sibling(); node; node = node->previous_sibling())
|
|
|
|
++index;
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
2021-04-17 21:05:56 +00:00
|
|
|
template<typename ChildType>
|
2022-10-17 12:41:50 +00:00
|
|
|
Optional<size_t> index_of_child(T const& search_child)
|
2021-04-17 21:05:56 +00:00
|
|
|
{
|
|
|
|
VERIFY(search_child.parent() == this);
|
|
|
|
size_t index = 0;
|
|
|
|
auto* child = first_child();
|
|
|
|
VERIFY(child);
|
|
|
|
|
|
|
|
do {
|
|
|
|
if (!is<ChildType>(child))
|
|
|
|
continue;
|
|
|
|
if (child == &search_child)
|
|
|
|
return index;
|
|
|
|
index++;
|
|
|
|
} while (child && (child = child->next_sibling()));
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
bool is_ancestor_of(TreeNode const&) const;
|
|
|
|
bool is_inclusive_ancestor_of(TreeNode const&) const;
|
2022-01-31 18:05:54 +00:00
|
|
|
|
2024-11-14 15:01:23 +00:00
|
|
|
void append_child(GC::Ref<T> node);
|
|
|
|
void prepend_child(GC::Ref<T> node);
|
|
|
|
void insert_before(GC::Ref<T> node, GC::Ptr<T> child);
|
|
|
|
void remove_child(GC::Ref<T> node);
|
2019-06-25 17:46:01 +00:00
|
|
|
|
2019-11-05 17:36:06 +00:00
|
|
|
T* next_in_pre_order()
|
|
|
|
{
|
|
|
|
if (first_child())
|
|
|
|
return first_child();
|
|
|
|
T* node;
|
|
|
|
if (!(node = next_sibling())) {
|
|
|
|
node = parent();
|
|
|
|
while (node && !node->next_sibling())
|
|
|
|
node = node->parent();
|
|
|
|
if (node)
|
|
|
|
node = node->next_sibling();
|
|
|
|
}
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2022-01-17 10:38:14 +00:00
|
|
|
T* next_in_pre_order(T const* stay_within)
|
|
|
|
{
|
|
|
|
if (first_child())
|
|
|
|
return first_child();
|
|
|
|
|
|
|
|
T* node = static_cast<T*>(this);
|
|
|
|
T* next = nullptr;
|
|
|
|
while (!(next = node->next_sibling())) {
|
|
|
|
node = node->parent();
|
|
|
|
if (!node || node == stay_within)
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return next;
|
|
|
|
}
|
|
|
|
|
2021-09-07 07:15:34 +00:00
|
|
|
T const* next_in_pre_order() const
|
2019-11-05 17:36:06 +00:00
|
|
|
{
|
|
|
|
return const_cast<TreeNode*>(this)->next_in_pre_order();
|
|
|
|
}
|
|
|
|
|
2022-01-17 10:38:14 +00:00
|
|
|
T const* next_in_pre_order(T const* stay_within) const
|
|
|
|
{
|
|
|
|
return const_cast<TreeNode*>(this)->next_in_pre_order(stay_within);
|
|
|
|
}
|
|
|
|
|
2021-09-07 07:15:34 +00:00
|
|
|
T* previous_in_pre_order()
|
|
|
|
{
|
|
|
|
if (auto* node = previous_sibling()) {
|
|
|
|
while (node->last_child())
|
|
|
|
node = node->last_child();
|
|
|
|
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
return parent();
|
|
|
|
}
|
|
|
|
|
|
|
|
T const* previous_in_pre_order() const
|
|
|
|
{
|
|
|
|
return const_cast<TreeNode*>(this)->previous_in_pre_order();
|
|
|
|
}
|
|
|
|
|
2019-10-19 16:14:54 +00:00
|
|
|
template<typename Callback>
|
2024-05-04 13:47:04 +00:00
|
|
|
TraversalDecision for_each_in_inclusive_subtree(Callback callback) const
|
2019-10-19 16:14:54 +00:00
|
|
|
{
|
2024-05-04 13:47:04 +00:00
|
|
|
if (auto decision = callback(static_cast<T const&>(*this)); decision != TraversalDecision::Continue)
|
|
|
|
return decision;
|
2019-10-19 16:14:54 +00:00
|
|
|
for (auto* child = first_child(); child; child = child->next_sibling()) {
|
2021-04-06 17:38:10 +00:00
|
|
|
if (child->for_each_in_inclusive_subtree(callback) == IterationDecision::Break)
|
2024-05-04 13:47:04 +00:00
|
|
|
return TraversalDecision::Break;
|
2019-10-19 16:14:54 +00:00
|
|
|
}
|
2024-05-04 13:47:04 +00:00
|
|
|
return TraversalDecision::Continue;
|
2019-10-21 10:01:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Callback>
|
2024-05-04 13:47:04 +00:00
|
|
|
TraversalDecision for_each_in_inclusive_subtree(Callback callback)
|
2019-10-21 10:01:30 +00:00
|
|
|
{
|
2024-05-04 13:47:04 +00:00
|
|
|
if (auto decision = callback(static_cast<T&>(*this)); decision != TraversalDecision::Continue)
|
|
|
|
return decision;
|
2019-10-21 10:01:30 +00:00
|
|
|
for (auto* child = first_child(); child; child = child->next_sibling()) {
|
2024-05-04 13:47:04 +00:00
|
|
|
if (child->for_each_in_inclusive_subtree(callback) == TraversalDecision::Break)
|
|
|
|
return TraversalDecision::Break;
|
2019-10-21 10:01:30 +00:00
|
|
|
}
|
2024-05-04 13:47:04 +00:00
|
|
|
return TraversalDecision::Continue;
|
2019-10-19 16:14:54 +00:00
|
|
|
}
|
|
|
|
|
2019-12-18 20:34:03 +00:00
|
|
|
template<typename U, typename Callback>
|
2024-05-04 13:47:04 +00:00
|
|
|
TraversalDecision for_each_in_inclusive_subtree_of_type(Callback callback)
|
2019-12-18 20:34:03 +00:00
|
|
|
{
|
2022-10-17 12:41:50 +00:00
|
|
|
if (is<U>(static_cast<T const&>(*this))) {
|
2024-05-04 13:47:04 +00:00
|
|
|
if (auto decision = callback(static_cast<U&>(*this)); decision != TraversalDecision::Continue)
|
|
|
|
return decision;
|
2019-12-18 20:34:03 +00:00
|
|
|
}
|
|
|
|
for (auto* child = first_child(); child; child = child->next_sibling()) {
|
2024-05-04 13:47:04 +00:00
|
|
|
if (child->template for_each_in_inclusive_subtree_of_type<U>(callback) == TraversalDecision::Break)
|
|
|
|
return TraversalDecision::Break;
|
2019-12-18 20:34:03 +00:00
|
|
|
}
|
2024-05-04 13:47:04 +00:00
|
|
|
return TraversalDecision::Continue;
|
2019-12-18 20:34:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U, typename Callback>
|
2024-05-04 13:47:04 +00:00
|
|
|
TraversalDecision for_each_in_inclusive_subtree_of_type(Callback callback) const
|
2019-12-18 20:34:03 +00:00
|
|
|
{
|
2022-10-17 12:41:50 +00:00
|
|
|
if (is<U>(static_cast<T const&>(*this))) {
|
2024-05-04 13:47:04 +00:00
|
|
|
if (auto decision = callback(static_cast<U const&>(*this)); decision != TraversalDecision::Continue)
|
|
|
|
return decision;
|
2019-12-18 20:34:03 +00:00
|
|
|
}
|
|
|
|
for (auto* child = first_child(); child; child = child->next_sibling()) {
|
2024-05-04 13:47:04 +00:00
|
|
|
if (child->template for_each_in_inclusive_subtree_of_type<U>(callback) == TraversalDecision::Break)
|
|
|
|
return TraversalDecision::Break;
|
2019-12-18 20:34:03 +00:00
|
|
|
}
|
2024-05-04 13:47:04 +00:00
|
|
|
return TraversalDecision::Continue;
|
2019-12-18 20:34:03 +00:00
|
|
|
}
|
|
|
|
|
2021-04-06 17:49:02 +00:00
|
|
|
template<typename Callback>
|
2024-05-04 13:47:04 +00:00
|
|
|
TraversalDecision for_each_in_subtree(Callback callback) const
|
2021-04-06 17:49:02 +00:00
|
|
|
{
|
|
|
|
for (auto* child = first_child(); child; child = child->next_sibling()) {
|
2024-05-04 13:47:04 +00:00
|
|
|
if (child->for_each_in_inclusive_subtree(callback) == TraversalDecision::Break)
|
|
|
|
return TraversalDecision::Break;
|
2021-04-06 17:49:02 +00:00
|
|
|
}
|
2024-05-04 13:47:04 +00:00
|
|
|
return TraversalDecision::Continue;
|
2021-04-06 17:49:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Callback>
|
2024-05-04 13:47:04 +00:00
|
|
|
TraversalDecision for_each_in_subtree(Callback callback)
|
2021-04-06 17:49:02 +00:00
|
|
|
{
|
|
|
|
for (auto* child = first_child(); child; child = child->next_sibling()) {
|
2024-05-04 13:47:04 +00:00
|
|
|
if (child->for_each_in_inclusive_subtree(callback) == TraversalDecision::Break)
|
|
|
|
return TraversalDecision::Break;
|
2021-04-06 17:49:02 +00:00
|
|
|
}
|
2024-05-04 13:47:04 +00:00
|
|
|
return TraversalDecision::Continue;
|
2021-04-06 17:49:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U, typename Callback>
|
2024-05-04 13:47:04 +00:00
|
|
|
TraversalDecision for_each_in_subtree_of_type(Callback callback)
|
2021-04-06 17:49:02 +00:00
|
|
|
{
|
|
|
|
for (auto* child = first_child(); child; child = child->next_sibling()) {
|
2024-05-04 13:47:04 +00:00
|
|
|
if (child->template for_each_in_inclusive_subtree_of_type<U>(callback) == TraversalDecision::Break)
|
|
|
|
return TraversalDecision::Break;
|
2021-04-06 17:49:02 +00:00
|
|
|
}
|
2024-05-04 13:47:04 +00:00
|
|
|
return TraversalDecision::Continue;
|
2021-04-06 17:49:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U, typename Callback>
|
2024-05-04 13:47:04 +00:00
|
|
|
TraversalDecision for_each_in_subtree_of_type(Callback callback) const
|
2021-04-06 17:49:02 +00:00
|
|
|
{
|
|
|
|
for (auto* child = first_child(); child; child = child->next_sibling()) {
|
2024-05-04 13:47:04 +00:00
|
|
|
if (child->template for_each_in_inclusive_subtree_of_type<U>(callback) == TraversalDecision::Break)
|
|
|
|
return TraversalDecision::Break;
|
2021-04-06 17:49:02 +00:00
|
|
|
}
|
2024-05-04 13:47:04 +00:00
|
|
|
return TraversalDecision::Continue;
|
2021-04-06 17:49:02 +00:00
|
|
|
}
|
|
|
|
|
2020-08-10 11:52:49 +00:00
|
|
|
template<typename Callback>
|
|
|
|
void for_each_child(Callback callback) const
|
|
|
|
{
|
2024-06-04 20:19:15 +00:00
|
|
|
return const_cast<TreeNode*>(this)->for_each_child(move(callback));
|
2020-08-10 11:52:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Callback>
|
|
|
|
void for_each_child(Callback callback)
|
|
|
|
{
|
2024-05-04 13:59:52 +00:00
|
|
|
for (auto* node = first_child(); node; node = node->next_sibling()) {
|
|
|
|
if (callback(*node) == IterationDecision::Break)
|
|
|
|
return;
|
|
|
|
}
|
2020-08-10 11:52:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U, typename Callback>
|
|
|
|
void for_each_child_of_type(Callback callback)
|
|
|
|
{
|
|
|
|
for (auto* node = first_child(); node; node = node->next_sibling()) {
|
2024-05-04 13:59:52 +00:00
|
|
|
if (is<U>(node)) {
|
|
|
|
if (callback(verify_cast<U>(*node)) == IterationDecision::Break)
|
|
|
|
return;
|
|
|
|
}
|
2020-08-10 11:52:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U, typename Callback>
|
|
|
|
void for_each_child_of_type(Callback callback) const
|
|
|
|
{
|
|
|
|
return const_cast<TreeNode*>(this)->template for_each_child_of_type<U>(move(callback));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U>
|
2022-10-17 12:41:50 +00:00
|
|
|
U const* next_sibling_of_type() const
|
2020-08-10 11:52:49 +00:00
|
|
|
{
|
|
|
|
return const_cast<TreeNode*>(this)->template next_sibling_of_type<U>();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U>
|
|
|
|
inline U* next_sibling_of_type()
|
|
|
|
{
|
|
|
|
for (auto* sibling = next_sibling(); sibling; sibling = sibling->next_sibling()) {
|
|
|
|
if (is<U>(*sibling))
|
2021-06-24 17:53:42 +00:00
|
|
|
return &verify_cast<U>(*sibling);
|
2020-08-10 11:52:49 +00:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U>
|
2022-10-17 12:41:50 +00:00
|
|
|
U const* previous_sibling_of_type() const
|
2020-08-10 11:52:49 +00:00
|
|
|
{
|
|
|
|
return const_cast<TreeNode*>(this)->template previous_sibling_of_type<U>();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U>
|
|
|
|
U* previous_sibling_of_type()
|
|
|
|
{
|
|
|
|
for (auto* sibling = previous_sibling(); sibling; sibling = sibling->previous_sibling()) {
|
|
|
|
if (is<U>(*sibling))
|
2021-06-24 17:53:42 +00:00
|
|
|
return &verify_cast<U>(*sibling);
|
2020-08-10 11:52:49 +00:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U>
|
2022-10-17 12:41:50 +00:00
|
|
|
U const* first_child_of_type() const
|
2020-08-10 11:52:49 +00:00
|
|
|
{
|
|
|
|
return const_cast<TreeNode*>(this)->template first_child_of_type<U>();
|
|
|
|
}
|
|
|
|
|
2020-11-21 18:49:09 +00:00
|
|
|
template<typename U>
|
2022-10-17 12:41:50 +00:00
|
|
|
U const* last_child_of_type() const
|
2020-11-21 18:49:09 +00:00
|
|
|
{
|
|
|
|
return const_cast<TreeNode*>(this)->template last_child_of_type<U>();
|
|
|
|
}
|
|
|
|
|
2020-08-10 11:52:49 +00:00
|
|
|
template<typename U>
|
|
|
|
U* first_child_of_type()
|
|
|
|
{
|
|
|
|
for (auto* child = first_child(); child; child = child->next_sibling()) {
|
|
|
|
if (is<U>(*child))
|
2021-06-24 17:53:42 +00:00
|
|
|
return &verify_cast<U>(*child);
|
2020-08-10 11:52:49 +00:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-11-21 18:49:09 +00:00
|
|
|
template<typename U>
|
|
|
|
U* last_child_of_type()
|
|
|
|
{
|
|
|
|
for (auto* child = last_child(); child; child = child->previous_sibling()) {
|
|
|
|
if (is<U>(*child))
|
2021-06-24 17:53:42 +00:00
|
|
|
return &verify_cast<U>(*child);
|
2020-11-21 18:49:09 +00:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-08-10 11:52:49 +00:00
|
|
|
template<typename U>
|
2022-10-17 12:41:50 +00:00
|
|
|
U const* first_ancestor_of_type() const
|
2020-08-10 11:52:49 +00:00
|
|
|
{
|
|
|
|
return const_cast<TreeNode*>(this)->template first_ancestor_of_type<U>();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U>
|
|
|
|
U* first_ancestor_of_type()
|
|
|
|
{
|
|
|
|
for (auto* ancestor = parent(); ancestor; ancestor = ancestor->parent()) {
|
|
|
|
if (is<U>(*ancestor))
|
2021-06-24 17:53:42 +00:00
|
|
|
return &verify_cast<U>(*ancestor);
|
2020-08-10 11:52:49 +00:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2022-10-17 12:41:50 +00:00
|
|
|
~TreeNode() = default;
|
2021-04-06 13:50:47 +00:00
|
|
|
|
2019-06-25 17:46:01 +00:00
|
|
|
protected:
|
2021-09-16 06:24:47 +00:00
|
|
|
TreeNode() = default;
|
2019-06-25 17:46:01 +00:00
|
|
|
|
2022-10-17 12:41:50 +00:00
|
|
|
void visit_edges(JS::Cell::Visitor& visitor)
|
|
|
|
{
|
|
|
|
visitor.visit(m_parent);
|
|
|
|
visitor.visit(m_first_child);
|
|
|
|
visitor.visit(m_last_child);
|
|
|
|
visitor.visit(m_next_sibling);
|
|
|
|
visitor.visit(m_previous_sibling);
|
|
|
|
}
|
2020-10-22 21:38:14 +00:00
|
|
|
|
2019-06-25 17:46:01 +00:00
|
|
|
private:
|
|
|
|
T* m_parent { nullptr };
|
|
|
|
T* m_first_child { nullptr };
|
|
|
|
T* m_last_child { nullptr };
|
|
|
|
T* m_next_sibling { nullptr };
|
|
|
|
T* m_previous_sibling { nullptr };
|
|
|
|
};
|
|
|
|
|
2019-11-06 19:25:40 +00:00
|
|
|
template<typename T>
|
2024-11-14 15:01:23 +00:00
|
|
|
inline void TreeNode<T>::remove_child(GC::Ref<T> node)
|
2019-11-06 19:25:40 +00:00
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(node->m_parent == this);
|
2019-11-06 19:25:40 +00:00
|
|
|
|
|
|
|
if (m_first_child == node)
|
|
|
|
m_first_child = node->m_next_sibling;
|
|
|
|
|
|
|
|
if (m_last_child == node)
|
|
|
|
m_last_child = node->m_previous_sibling;
|
|
|
|
|
2019-11-07 21:42:55 +00:00
|
|
|
if (node->m_next_sibling)
|
|
|
|
node->m_next_sibling->m_previous_sibling = node->m_previous_sibling;
|
|
|
|
|
|
|
|
if (node->m_previous_sibling)
|
|
|
|
node->m_previous_sibling->m_next_sibling = node->m_next_sibling;
|
|
|
|
|
2019-11-06 19:25:40 +00:00
|
|
|
node->m_next_sibling = nullptr;
|
|
|
|
node->m_previous_sibling = nullptr;
|
|
|
|
node->m_parent = nullptr;
|
|
|
|
}
|
|
|
|
|
2019-06-25 17:46:01 +00:00
|
|
|
template<typename T>
|
2024-11-14 15:01:23 +00:00
|
|
|
inline void TreeNode<T>::append_child(GC::Ref<T> node)
|
2019-06-25 17:46:01 +00:00
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(!node->m_parent);
|
2019-10-12 21:26:47 +00:00
|
|
|
|
2019-06-25 17:46:01 +00:00
|
|
|
if (m_last_child)
|
|
|
|
m_last_child->m_next_sibling = node.ptr();
|
|
|
|
node->m_previous_sibling = m_last_child;
|
|
|
|
node->m_parent = static_cast<T*>(this);
|
2019-09-29 15:40:39 +00:00
|
|
|
m_last_child = node.ptr();
|
2019-06-25 17:46:01 +00:00
|
|
|
if (!m_first_child)
|
|
|
|
m_first_child = m_last_child;
|
|
|
|
}
|
2019-09-25 09:26:26 +00:00
|
|
|
|
2020-06-21 14:45:21 +00:00
|
|
|
template<typename T>
|
2024-11-14 15:01:23 +00:00
|
|
|
inline void TreeNode<T>::insert_before(GC::Ref<T> node, GC::Ptr<T> child)
|
2020-06-21 14:45:21 +00:00
|
|
|
{
|
|
|
|
if (!child)
|
2021-04-06 17:29:12 +00:00
|
|
|
return append_child(move(node));
|
2020-06-21 14:45:21 +00:00
|
|
|
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(!node->m_parent);
|
|
|
|
VERIFY(child->parent() == this);
|
2020-06-21 14:45:21 +00:00
|
|
|
|
|
|
|
node->m_previous_sibling = child->m_previous_sibling;
|
|
|
|
node->m_next_sibling = child;
|
|
|
|
|
2020-11-27 22:00:53 +00:00
|
|
|
if (child->m_previous_sibling)
|
|
|
|
child->m_previous_sibling->m_next_sibling = node;
|
|
|
|
|
|
|
|
if (m_first_child == child)
|
|
|
|
m_first_child = node;
|
|
|
|
|
2020-11-27 20:06:15 +00:00
|
|
|
child->m_previous_sibling = node;
|
|
|
|
|
2020-06-21 14:45:21 +00:00
|
|
|
node->m_parent = static_cast<T*>(this);
|
|
|
|
}
|
|
|
|
|
2019-10-09 18:17:01 +00:00
|
|
|
template<typename T>
|
2024-11-14 15:01:23 +00:00
|
|
|
inline void TreeNode<T>::prepend_child(GC::Ref<T> node)
|
2019-10-09 18:17:01 +00:00
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(!node->m_parent);
|
2019-10-12 21:26:47 +00:00
|
|
|
|
2019-10-09 18:17:01 +00:00
|
|
|
if (m_first_child)
|
|
|
|
m_first_child->m_previous_sibling = node.ptr();
|
|
|
|
node->m_next_sibling = m_first_child;
|
|
|
|
node->m_parent = static_cast<T*>(this);
|
|
|
|
m_first_child = node.ptr();
|
|
|
|
if (!m_last_child)
|
|
|
|
m_last_child = m_first_child;
|
2020-04-03 21:06:09 +00:00
|
|
|
node->inserted_into(static_cast<T&>(*this));
|
|
|
|
|
|
|
|
static_cast<T*>(this)->children_changed();
|
2019-10-09 18:17:01 +00:00
|
|
|
}
|
|
|
|
|
2019-10-09 19:33:34 +00:00
|
|
|
template<typename T>
|
2022-04-01 17:58:27 +00:00
|
|
|
inline bool TreeNode<T>::is_ancestor_of(TreeNode<T> const& other) const
|
2019-10-09 19:33:34 +00:00
|
|
|
{
|
|
|
|
for (auto* ancestor = other.parent(); ancestor; ancestor = ancestor->parent()) {
|
|
|
|
if (ancestor == this)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2020-03-07 09:27:02 +00:00
|
|
|
|
2021-04-06 18:34:49 +00:00
|
|
|
template<typename T>
|
2022-04-01 17:58:27 +00:00
|
|
|
inline bool TreeNode<T>::is_inclusive_ancestor_of(TreeNode<T> const& other) const
|
2021-04-06 18:34:49 +00:00
|
|
|
{
|
|
|
|
return &other == this || is_ancestor_of(other);
|
|
|
|
}
|
|
|
|
|
2020-03-07 09:27:02 +00:00
|
|
|
}
|