blob: 75f878fd80a6f74a02b2f7209c5035d951a0c87c (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
#pragma once
#include <AK/Assertions.h>
#include <AK/NonnullRefPtr.h>
template<typename T>
class TreeNode {
public:
void ref()
{
ASSERT(m_ref_count);
++m_ref_count;
}
void deref()
{
ASSERT(m_ref_count);
if (!--m_ref_count) {
if (m_next_sibling)
m_next_sibling->m_previous_sibling = m_previous_sibling;
if (m_previous_sibling)
m_previous_sibling->m_next_sibling = m_next_sibling;
T* next_child;
for (auto* child = m_first_child; child; child = next_child) {
next_child = child->m_next_sibling;
child->m_parent = nullptr;
child->deref();
}
delete static_cast<T*>(this);
}
}
int ref_count() const { return m_ref_count; }
T* parent() { return m_parent; }
const T* parent() const { return m_parent; }
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; }
const T* next_sibling() const { return m_next_sibling; }
const T* previous_sibling() const { return m_previous_sibling; }
const T* first_child() const { return m_first_child; }
const T* last_child() const { return m_last_child; }
bool is_ancestor_of(const TreeNode&) const;
void prepend_child(NonnullRefPtr<T> node, bool call_inserted_into = true);
void append_child(NonnullRefPtr<T> node, bool call_inserted_into = true);
void donate_all_children_to(T& node);
protected:
TreeNode() { }
private:
int m_ref_count { 1 };
T* m_parent { nullptr };
T* m_first_child { nullptr };
T* m_last_child { nullptr };
T* m_next_sibling { nullptr };
T* m_previous_sibling { nullptr };
};
template<typename T>
inline void TreeNode<T>::append_child(NonnullRefPtr<T> node, bool call_inserted_into)
{
ASSERT(!node->m_parent);
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);
m_last_child = node.ptr();
if (!m_first_child)
m_first_child = m_last_child;
if (call_inserted_into)
node->inserted_into(static_cast<T&>(*this));
(void)node.leak_ref();
}
template<typename T>
inline void TreeNode<T>::prepend_child(NonnullRefPtr<T> node, bool call_inserted_into)
{
ASSERT(!node->m_parent);
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;
if (call_inserted_into)
node->inserted_into(static_cast<T&>(*this));
(void)node.leak_ref();
}
template<typename T>
inline void TreeNode<T>::donate_all_children_to(T& node)
{
for (T* child = m_first_child; child != nullptr;) {
T* next_child = child->m_next_sibling;
child->m_parent = nullptr;
child->m_next_sibling = nullptr;
child->m_previous_sibling = nullptr;
node.append_child(adopt(*child));
child = next_child;
}
m_first_child = nullptr;
m_last_child = nullptr;
}
template<typename T>
inline bool TreeNode<T>::is_ancestor_of(const TreeNode<T>& other) const
{
for (auto* ancestor = other.parent(); ancestor; ancestor = ancestor->parent()) {
if (ancestor == this)
return true;
}
return false;
}
|