-
Notifications
You must be signed in to change notification settings - Fork 1
/
intrusive_list.h
259 lines (250 loc) · 6.15 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
#ifndef INTRUSIVE_LIST_H
#define INTRUSIVE_LIST_H
#include <utility>
#include <iterator>
#include <cstddef>
template<typename T, std::pair<T*, T*> T::* link_ptr>
struct intrusive_list_member_link {
std::pair<T*, T*>* operator()(T* ptr) {
return &(ptr->*link_ptr);
}
};
template<typename T, typename link_T, std::pair<T*, T*> T::* link_ptr = nullptr>
class intrusive_list {
using link_f = std::conditional_t<link_ptr == nullptr, link_T, intrusive_list_member_link<T, link_ptr>>;
public:
typedef T value_type;
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef T& reference;
typedef const T& const_reference;
typedef T* pointer;
typedef const T* const_pointer;
class const_iterator {
public:
typedef std::bidirectional_iterator_tag iterator_category;
typedef typename intrusive_list::value_type value_type;
typedef typename intrusive_list::difference_type difference_type;
typedef typename intrusive_list::const_pointer pointer;
typedef typename intrusive_list::const_reference reference;
private:
typedef const_iterator this_t;
pointer ptr = nullptr;
public:
const_iterator() = default;
explicit const_iterator(pointer ptr) : ptr(ptr) {}
explicit const_iterator(reference v) : ptr(&v) {}
reference operator*() const {
return *ptr;
}
pointer operator->() const {
return ptr;
}
this_t& operator++() {
ptr = link_f()(ptr)->second;
return *this;
}
this_t& operator++(int) {
auto r = *this;
ptr = link_f()(ptr)->second;
return r;
}
this_t& operator--() {
ptr = link_f()(ptr)->first;
return *this;
}
this_t& operator--(int) {
auto r = *this;
ptr = link_f()(ptr)->first;
return r;
}
bool operator==(const this_t& rhs) const {
return ptr == rhs.ptr;
}
bool operator!=(const this_t& rhs) const {
return !(*this == rhs);
}
};
class iterator {
public:
typedef std::bidirectional_iterator_tag iterator_category;
typedef typename intrusive_list::value_type value_type;
typedef typename intrusive_list::difference_type difference_type;
typedef typename intrusive_list::pointer pointer;
typedef typename intrusive_list::reference reference;
private:
typedef iterator this_t;
pointer ptr = nullptr;
public:
iterator() = default;
explicit iterator(pointer ptr) : ptr(ptr) {}
explicit iterator(reference v) : ptr(&v) {}
reference operator*() const {
return *ptr;
}
pointer operator->() const {
return ptr;
}
this_t& operator++() {
ptr = link_f()(ptr)->second;
return *this;
}
this_t operator++(int) {
auto r = *this;
ptr = link_f()(ptr)->second;
return r;
}
this_t& operator--() {
ptr = link_f()(ptr)->first;
return *this;
}
this_t& operator--(int) {
auto r = *this;
ptr = link_f()(ptr)->first;
return r;
}
bool operator==(const this_t& rhs) const {
return ptr == rhs.ptr;
}
bool operator!=(const this_t& rhs) const {
return !(*this == rhs);
}
};
typedef std::reverse_iterator<iterator> reverse_iterator;
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
private:
std::pair<T*, T*> header = { &*end(), &*end() };
pointer ptr_begin() const {
return header.second;
}
pointer ptr_end() const {
uint8_t* p = (uint8_t*)&header;
p -= (intptr_t)link_f()((T*)nullptr);
return (T*)p;
}
pointer ptr_back() const {
return header.first;
}
public:
intrusive_list() = default;
intrusive_list(const intrusive_list&) = delete;
intrusive_list(intrusive_list&& n) noexcept {
*this = std::move(n);
}
intrusive_list&operator=(const intrusive_list& n) = delete;
intrusive_list&operator=(intrusive_list&& n) noexcept {
if (n.empty()) clear();
else {
header = n.header;
n.clear();
link_f()(ptr_begin())->first = ptr_end();
link_f()(ptr_begin())->second = ptr_end();
}
return *this;
}
reference front() {
return *ptr_begin();
}
const_reference front() const {
return *ptr_begin();
}
reference back() {
return *(--end());
}
const_reference back() const {
return *(--end());
}
iterator begin() {
return iterator(ptr_begin());
}
const_iterator begin() const {
return iterator(ptr_begin());
}
const_iterator cbegin() const {
return iterator(ptr_begin());
}
iterator end() {
return iterator(ptr_end());
}
const_iterator end() const {
return const_iterator(ptr_end());
}
const_iterator cend() const {
return const_iterator(ptr_end());
}
reverse_iterator rbegin() {
return reverse_iterator(end());
}
const_reverse_iterator rbegin() const {
return reverse_iterator(end());
}
const_reverse_iterator crbegin() const {
return reverse_iterator(end());
}
reverse_iterator rend() {
return reverse_iterator(begin());
}
const_reverse_iterator rend() const {
return reverse_iterator(begin());
}
const_reverse_iterator crend() const {
return reverse_iterator(begin());
}
bool empty() const {
return ptr_begin() == ptr_end();
}
size_type size() const = delete;
constexpr size_type max_size() const = delete;
void clear() {
header = { ptr_end(), ptr_end() };
}
iterator insert(iterator pos, reference v) {
pointer before = &*pos;
pointer after = link_f()(before)->first;
link_f()(before)->first = &v;
link_f()(after)->second = &v;
link_f()(&v)->first = after;
link_f()(&v)->second = before;
return iterator(v);
}
static iterator erase(iterator pos) {
pointer ptr = &*pos;
pointer prev = link_f()(ptr)->first;
pointer next = link_f()(ptr)->second;
link_f()(prev)->second = next;
link_f()(next)->first = prev;
return iterator(*next);
}
void push_back(reference v) {
insert(end(), v);
}
void pop_back() {
erase(back());
}
void push_front(reference v) {
insert(begin(), v);
}
void pop_front() {
erase(iterator(front()));
}
void swap(intrusive_list& n) noexcept {
if (empty()) {
*this = std::move(n);
} else if (n.empty()) {
n = std::move(*this);
} else {
std::swap(header, n.header);
link_f()(n.ptr_begin())->first = n.ptr_end();
link_f()(n.ptr_begin())->second = n.ptr_end();
link_f()(ptr_begin())->first = ptr_end();
link_f()(ptr_begin())->second = ptr_end();
}
}
iterator iterator_to(reference v) {
return iterator(v);
}
static iterator s_iterator_to(reference v) {
return iterator(v);
}
};
#endif