-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcircular_list.h
More file actions
297 lines (268 loc) · 8.16 KB
/
circular_list.h
File metadata and controls
297 lines (268 loc) · 8.16 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
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/// cpp-circular-list - A barebones, header-only C++ implementation of
/// `circular_list` with STL-like iterator support.
///
/// Author: Gurtej Kanwar (https://github.com/gkanwar)
///
/// To the extent possible under law, the author(s) have dedicated all copyright
/// and related and neighboring rights to this software to the public domain
/// worldwide. This software is distributed without any warranty. You should
/// have received a copy of the CC0 Public Domain Dedication along with this
/// software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
#ifndef CIRCULAR_LIST
#define CIRCULAR_LIST
#include <cstddef>
#include <iterator>
#include <vector>
namespace ext_std {
template<typename T>
struct circular_list {
struct node {
int fwd;
int bwd;
T value;
bool free;
bool is_head;
};
template<bool Const = false>
struct generic_iterator {
using iterator_category = std::bidirectional_iterator_tag;
using difference_type = std::ptrdiff_t;
using value_type = T;
using pointer = typename std::conditional<Const, T const*, T *>::type;
using reference = typename std::conditional<Const, T const&, T &>::type;
using list_pointer = typename std::conditional<Const, circular_list const*, circular_list *>::type;
using node_pointer = typename std::conditional<Const, node const*, node *>::type;
list_pointer list;
int node;
int branch;
generic_iterator(list_pointer list, int node)
: list(list), node(node), branch(0) {}
generic_iterator(list_pointer list, int node, int branch)
: list(list), node(node), branch(branch) {}
template<bool _Const = Const>
typename std::enable_if<_Const, node_pointer>::type
_get_node() const {
return &list->all_nodes[node];
}
template<bool _Const = Const>
typename std::enable_if<!_Const, node_pointer>::type
_get_node() {
return &list->all_nodes[node];
}
template<bool _Const = Const>
typename std::enable_if<_Const, reference>::type
operator*() const {
assert(!_get_node()->free);
return _get_node()->value;
}
template<bool _Const = Const>
typename std::enable_if<!_Const, reference>::type
operator*() {
assert(!_get_node()->free);
return _get_node()->value;
}
template<bool _Const = Const>
typename std::enable_if<_Const, pointer>::type
operator->() const {
assert(!_get_node()->free);
return &_get_node()->value;
}
template<bool _Const = Const>
typename std::enable_if<!_Const, pointer>::type
operator->() {
assert(!_get_node()->free);
return &_get_node()->value;
}
generic_iterator& operator++() {
assert(!_get_node()->free);
assert(!list->all_nodes[_get_node()->fwd].free);
node = _get_node()->fwd;
if (_get_node()->is_head) {
branch++;
}
return *this;
}
generic_iterator operator++(int) {
auto tmp = *this;
++(*this);
return tmp;
}
generic_iterator& operator--() {
assert(!_get_node()->free);
assert(!list->all_nodes[_get_node()->bwd].free);
if (_get_node()->is_head) {
branch--;
}
node = _get_node()->bwd;
return *this;
}
generic_iterator operator--(int) {
auto tmp = *this;
--(*this);
return tmp;
}
generic_iterator operator+(difference_type n) const {
return std::next(*this, n);
}
generic_iterator operator-(difference_type n) const {
return std::prev(*this, n);
}
friend bool operator==(const generic_iterator& l, const generic_iterator& r) {
// -1 (empty iterator) does not distinguish branches
return l.list == r.list && l.node == r.node && (l.node < 0 || l.branch == r.branch);
}
friend bool operator!=(const generic_iterator& l, const generic_iterator& r) {
return !(l == r);
}
};
using iterator = generic_iterator<false>;
using const_iterator = generic_iterator<true>;
int head;
std::vector<node> all_nodes;
circular_list() : head(-1), all_nodes() {}
circular_list(const std::vector<T>& values) : all_nodes(values.size()) {
size_t n_values = values.size();
for (int i = 0; i < n_values; ++i) {
all_nodes[i].value = values[i];
all_nodes[i].fwd = (i+1) % values.size();
all_nodes[i].bwd = (i-1+values.size()) % values.size();
all_nodes[i].free = false;
all_nodes[i].is_head = (i == 0);
}
head = 0;
}
iterator begin() {
return iterator(this, head, 0);
}
const_iterator begin() const {
return const_iterator(this, head, 0);
}
iterator end() {
return iterator(this, head, 1);
}
const_iterator end() const {
return const_iterator(this, head, 1);
}
const_iterator cbegin() const {
return const_iterator(this, head, 0);
}
const_iterator cend() const {
return const_iterator(this, head, 1);
}
bool empty() const {
return head < 0;
}
size_t size() const {
return std::distance(cbegin(), cend());
}
bool _list_is_consistent() const {
for (auto it = cbegin(); it != cend(); ++it) {
if (it == cbegin() && !it._get_node()->is_head ||
it != cbegin() && it._get_node()->is_head) {
return false;
}
if (it._get_node()->free) {
return false;
}
if (it._get_node()->fwd != std::next(it).node) {
return false;
}
if (it._get_node()->bwd != std::prev(it).node) {
return false;
}
}
if (!empty() && head != cbegin().node) {
return false;
}
if (empty() && head >= 0) {
return false;
}
return true;
}
iterator erase(iterator first_it, iterator last_it) {
assert(last_it.branch == first_it.branch || last_it.branch == first_it.branch + 1);
int before_first = all_nodes[first_it.node].bwd;
// free nodes in [first, last)
int cur_branch = first_it.branch;
bool beheaded = false;
for (int cur = first_it.node;
cur != last_it.node || cur_branch != last_it.branch;
cur = all_nodes[cur].fwd) {
node& cur_node = all_nodes[cur];
if (cur == head) {
beheaded = true;
}
cur_node.free = true;
cur_node.is_head = false;
if (cur_node.fwd == head) {
cur_branch++;
}
}
// close the loop again (we may be connecting free'd nodes, but that is ok)
all_nodes[last_it.node].bwd = before_first;
all_nodes[before_first].fwd = last_it.node;
if (beheaded) {
if (all_nodes[last_it.node].free) {
head = -1;
}
else {
head = last_it.node;
all_nodes[last_it.node].is_head = true;
}
}
assert(_list_is_consistent());
if (!all_nodes[last_it.node].free) {
return last_it;
}
else {
return end();
}
}
// insert values BEFORE pos
void insert(iterator pos, const std::vector<T>& values) {
int last = all_nodes[pos.node].bwd;
int next_free = 0;
for (int i = 0; i < values.size(); ++i) {
while(!all_nodes[next_free].free) {
++next_free;
if (next_free == all_nodes.size()) {
all_nodes.resize(
all_nodes.size() + values.size() - i,
node{ .free = true, .is_head = false });
}
}
node& new_node = all_nodes[next_free];
new_node.free = false;
new_node.value = values[i];
new_node.bwd = last;
all_nodes[last].fwd = next_free;
last = next_free;
// special case: inserting before head
if (all_nodes[pos.node].is_head && pos.branch == 0) {
all_nodes[pos.node].is_head = false;
new_node.is_head = true;
head = next_free;
}
}
all_nodes[last].fwd = pos.node;
all_nodes[pos.node].bwd = last;
assert(_list_is_consistent());
}
const T& operator[](size_t ind) const {
return *std::next(begin(), ind);
}
T& operator[](size_t ind) {
return *std::next(begin(), ind);
}
};
template<typename T>
bool operator==(const circular_list<T>& l, const circular_list<T>& r) {
auto l_it = l.cbegin();
auto r_it = r.cbegin();
for (; l_it != l.cend() && r_it != r.cend(); ++l_it, ++r_it) {
if (*l_it != *r_it) return false;
}
return l_it == l.cend() && r_it == r.cend();
}
} // namespace ext_std
#endif