-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathintrusive_list.h
265 lines (233 loc) · 5.53 KB
/
intrusive_list.h
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#pragma once
#include <iterator>
namespace intrusive
{
struct default_tag;
template<typename Tag = default_tag>
struct list_element
{
list_element *prev = nullptr;
list_element *next = nullptr;
list_element() noexcept {}
list_element(list_element *prev, list_element *next) noexcept : prev(prev), next(next) {}
list_element(list_element &&other) noexcept
{
other.insert(this);
other.unlink();
}
list_element &operator=(list_element &&other) noexcept
{
other.insert(this);
other.unlink();
return *this;
}
void unlink() noexcept
{
if (next != nullptr)
next->prev = prev;
if (prev != nullptr)
prev->next = next;
prev = next = nullptr;
}
void insert(list_element *other) noexcept
{
other->prev = prev;
other->next = this;
if (prev != nullptr)
prev->next = other;
prev = other;
}
bool is_connected() const noexcept
{
return prev != nullptr && next != nullptr;
}
~list_element()
{
unlink();
}
};
template<typename T, typename Tag = default_tag>
class list
{
private:
list_element<Tag> head_, tail_;
list_element<Tag> *head = &head_;
list_element<Tag> *tail = &tail_;
template<typename ItT>
class iterator_ex
{
friend class list;
protected:
list_element<Tag> *data;
explicit iterator_ex(list_element<Tag> *data) noexcept : data(data) {}
public:
using value_type = ItT;
using difference_type = std::ptrdiff_t;
using pointer = ItT *;
using reference = ItT &;
using iterator_category = std::bidirectional_iterator_tag;
iterator_ex() = default;
reference operator*() const noexcept
{
return static_cast<reference>(*data);
}
pointer operator->() const noexcept
{
return static_cast<pointer>(data);
}
bool operator==(iterator_ex other) const noexcept
{
return data == other.data;
}
bool operator!=(iterator_ex other) const noexcept
{
return data != other.data;
}
iterator_ex &operator--() noexcept
{
data = data->prev;
return *this;
}
iterator_ex &operator++() noexcept
{
data = data->next;
return *this;
}
iterator_ex operator--(int) noexcept
{
iterator_ex res(data);
operator--();
return res;
}
iterator_ex operator++(int) noexcept
{
iterator_ex res(data);
operator++();
return res;
}
operator iterator_ex<const ItT>() const noexcept
{
return iterator_ex<const ItT>(data);
}
};
public:
using iterator = iterator_ex<T>;
using const_iterator = iterator_ex<const T>;
static_assert(std::is_convertible_v<T &, list_element<Tag> &>,
"value type is not convertible to list_element");
list() noexcept : head_{nullptr, &tail_}, tail_{&head_, nullptr} {}
list(list const &) = delete;
list(list &&other) noexcept : list()
{
splice(end(), other, other.begin(), other.end());
}
~list() = default;
list &operator=(list const &) = delete;
list &operator=(list &&other) noexcept
{
clear();
splice(end(), other, other.begin(), other.end());
return *this;
}
void clear() noexcept
{
auto at = head->next;
while (at != tail)
{
auto save = at->next;
at->prev = at->next = nullptr;
at = save;
}
head->next = tail;
tail->prev = head;
}
void push_back(T &el) noexcept
{
insert(end(), el);
}
void pop_back() noexcept
{
back().unlink();
}
T &back() noexcept
{
return *--end();
}
T const &back() const noexcept
{
return *--end();
}
void push_front(T &el) noexcept
{
insert(begin(), el);
}
void pop_front() noexcept
{
front().unlink();
}
T &front() noexcept
{
return *begin();
}
T const &front() const noexcept
{
return *begin();
}
bool empty() const noexcept
{
return head->next == tail;
}
iterator begin() noexcept
{
return iterator(head->next);
}
const_iterator begin() const noexcept
{
return const_iterator(head->next);
}
iterator end() noexcept
{
return iterator(tail);
}
const_iterator end() const noexcept
{
return const_iterator(tail);
}
iterator get_iterator(T &el) noexcept
{
return iterator(&el);
}
const_iterator get_iterator(const T &el) const noexcept
{
return const_iterator(&el);
}
iterator insert(const_iterator pos, T &el) noexcept
{
list_element<Tag> *at = pos.data;
at->insert(&el);
return iterator(&el);
}
iterator erase(const_iterator pos) noexcept
{
list_element<Tag> *at = pos.data;
auto saved = at->next;
at->unlink();
return iterator(saved);
}
void splice(const_iterator pos, list &other, const_iterator first, const_iterator last) noexcept
{
list_element<Tag> *at = pos.data;
list_element<Tag> *begin = first.data;
list_element<Tag> *end = last.data;
if (begin == end)
return;
end = end->prev;
begin->prev->next = end->next;
end->next->prev = begin->prev;
begin->prev = at->prev;
end->next = at;
at->prev->next = begin;
at->prev = end;
}
};
} // namespace intrusive