-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintrusive_list.h
More file actions
201 lines (166 loc) · 4.46 KB
/
intrusive_list.h
File metadata and controls
201 lines (166 loc) · 4.46 KB
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
#ifndef INTRUSIVE_LIST_H
#define INTRUSIVE_LIST_H
#include <memory>
#include <type_traits>
namespace details {
struct default_tag {};
template <typename Tag = default_tag>
struct intrusive_list_node {
intrusive_list_node* prev;
intrusive_list_node* next;
intrusive_list_node() : prev(this), next(this) {}
intrusive_list_node(intrusive_list_node const&) = delete;
intrusive_list_node(intrusive_list_node&& other) {
if (!other.empty()) {
next = other.next;
prev = other.prev;
update();
other.quite_remove();
} else {
quite_remove();
}
}
intrusive_list_node& operator=(intrusive_list_node const& other) = delete;
intrusive_list_node& operator=(intrusive_list_node&& other) noexcept {
if (&other != this) {
intrusive_list_node(std::move(other)).swap(*this);
}
return *this;
}
~intrusive_list_node() {
remove();
}
bool empty() {
return next == this && prev == this;
}
void swap(intrusive_list_node& other) {
using std::swap;
if (empty() && other.empty()) {
return;
} else if (empty() || other.empty()) {
intrusive_list_node& empt = (empty()) ? *this : other;
intrusive_list_node& not_empt = (!empty()) ? *this : other;
empt.next = not_empt.next;
empt.prev = not_empt.prev;
empt.update();
not_empt.quite_remove();
} else {
swap(prev, other.prev);
swap(next, other.next);
update();
other.update();
}
}
void quite_remove() {
prev = this;
next = this;
}
void remove() {
if (!empty()) {
next->prev = prev;
prev->next = next;
}
quite_remove();
}
void update() {
next->prev = this;
prev->next = this;
}
};
} // namespace details
template <typename T, typename Tag = details::default_tag>
class intrusive_list {
using node_t = details::intrusive_list_node<Tag>;
node_t end_;
template <bool Const>
class iterator_t {
friend class intrusive_list;
node_t* ptr;
public:
using iterator_category = std::bidirectional_iterator_tag;
using difference_type = std::ptrdiff_t;
using value_type = T;
using pointer = typename std::conditional<Const, const value_type*, value_type*>::type;
using reference = typename std::conditional<Const, const value_type&, value_type&>::type;
iterator_t() : ptr(nullptr) {}
iterator_t(node_t* node) : ptr(node) {}
iterator_t(const iterator_t&) = default;
iterator_t& operator=(const iterator_t&) = default;
template <bool Const_, class = std::enable_if_t<Const && !Const_>>
iterator_t(const iterator_t<Const_>& other) : ptr(other.ptr) {}
template <bool Const_>
bool operator==(iterator_t<Const_> const& other) const {
return ptr == other.ptr;
}
template <bool Const_>
bool operator!=(iterator_t<Const_> const& other) const {
return ptr != other.ptr;
}
typename iterator_t::reference operator*() const {
return *static_cast<T*>(ptr);
}
typename iterator_t::pointer operator->() const {
return static_cast<T*>(ptr);
}
iterator_t operator++() {
ptr = ptr->next;
return iterator_t(ptr);
}
iterator_t operator++(int) {
node_t* copy = ptr;
++(*this);
return iterator_t(copy);
}
iterator_t operator--() {
ptr = ptr->prev;
return iterator_t(ptr);
}
iterator_t operator--(int) {
node_t* copy = ptr;
--(*this);
return iterator_t(copy);
}
};
public:
using iterator = iterator_t<false>;
using const_iterator = iterator_t<true>;
intrusive_list() = default;
~intrusive_list() {
iterator it = begin();
while (it != end()) {
erase(it++);
}
}
iterator begin() noexcept {
return iterator(end_.next);
}
iterator end() noexcept {
return iterator(&end_);
}
const_iterator cbegin() const noexcept {
return iterator(end_.next);
}
const_iterator cend() const noexcept {
return iterator(end_);
}
iterator insert(const_iterator it, T const& value) noexcept {
node_t* node = const_cast<node_t*>(static_cast<node_t const*>(&value));
node->remove();
node->prev = it.ptr->prev;
node->next = it.ptr;
node->update();
return iterator(node);
}
iterator push_back(T const& value) noexcept {
return insert(end(), value);
}
iterator erase(iterator it) noexcept {
iterator del = it++;
del.ptr->remove();
return it;
}
bool empty() const noexcept {
return &end_ == end_.next;
}
};
#endif