blob: 08e4fbdb2652ed3d955599a9e17b9016b53ba434 (
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
|
#pragma once
#include "Assertions.h"
#include "Types.h"
namespace AK {
template<typename T>
class InlineLinkedListNode {
public:
InlineLinkedListNode();
void set_prev(T*);
void set_next(T*);
T* prev() const;
T* next() const;
};
template<typename T>
inline InlineLinkedListNode<T>::InlineLinkedListNode()
{
set_prev(0);
set_next(0);
}
template<typename T>
inline void InlineLinkedListNode<T>::set_prev(T* prev)
{
static_cast<T*>(this)->m_prev = prev;
}
template<typename T>
inline void InlineLinkedListNode<T>::set_next(T* next)
{
static_cast<T*>(this)->m_next = next;
}
template<typename T>
inline T* InlineLinkedListNode<T>::prev() const
{
return static_cast<const T*>(this)->m_prev;
}
template<typename T>
inline T* InlineLinkedListNode<T>::next() const
{
return static_cast<const T*>(this)->m_next;
}
template<typename T>
class InlineLinkedList {
public:
InlineLinkedList() {}
bool is_empty() const { return !m_head; }
size_t size_slow() const;
void clear();
T* head() const { return m_head; }
T* remove_head();
T* remove_tail();
T* tail() const { return m_tail; }
void prepend(T*);
void append(T*);
void remove(T*);
void append(InlineLinkedList<T>&);
bool contains_slow(T* value) const
{
for (T* node = m_head; node; node = node->next()) {
if (node == value)
return true;
}
return false;
}
private:
T* m_head { nullptr };
T* m_tail { nullptr };
};
template<typename T>
inline size_t InlineLinkedList<T>::size_slow() const
{
size_t size = 0;
for (T* node = m_head; node; node = node->next())
++size;
return size;
}
template<typename T>
inline void InlineLinkedList<T>::clear()
{
m_head = 0;
m_tail = 0;
}
template<typename T>
inline void InlineLinkedList<T>::prepend(T* node)
{
if (!m_head) {
ASSERT(!m_tail);
m_head = node;
m_tail = node;
node->set_prev(0);
node->set_next(0);
return;
}
ASSERT(m_tail);
m_head->set_prev(node);
node->set_next(m_head);
node->set_prev(0);
m_head = node;
}
template<typename T>
inline void InlineLinkedList<T>::append(T* node)
{
if (!m_tail) {
ASSERT(!m_head);
m_head = node;
m_tail = node;
node->set_prev(0);
node->set_next(0);
return;
}
ASSERT(m_head);
m_tail->set_next(node);
node->set_prev(m_tail);
node->set_next(0);
m_tail = node;
}
template<typename T>
inline void InlineLinkedList<T>::remove(T* node)
{
if (node->prev()) {
ASSERT(node != m_head);
node->prev()->set_next(node->next());
} else {
ASSERT(node == m_head);
m_head = node->next();
}
if (node->next()) {
ASSERT(node != m_tail);
node->next()->set_prev(node->prev());
} else {
ASSERT(node == m_tail);
m_tail = node->prev();
}
}
template<typename T>
inline T* InlineLinkedList<T>::remove_head()
{
T* node = head();
if (node)
remove(node);
return node;
}
template<typename T>
inline T* InlineLinkedList<T>::remove_tail()
{
T* node = tail();
if (node)
remove(node);
return node;
}
template<typename T>
inline void InlineLinkedList<T>::append(InlineLinkedList<T>& other)
{
if (!other.head())
return;
if (!head()) {
m_head = other.head();
m_tail = other.tail();
other.clear();
return;
}
ASSERT(tail());
ASSERT(other.head());
T* other_head = other.head();
T* other_tail = other.tail();
other.clear();
ASSERT(!m_tail->next());
m_tail->set_next(other_head);
ASSERT(!other_head->prev());
other_head->set_prev(m_tail);
m_tail = other_tail;
}
}
using AK::InlineLinkedList;
using AK::InlineLinkedListNode;
|