-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdelist.h
338 lines (271 loc) · 8.92 KB
/
delist.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
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/**
* delist header
* written by Shuangquan Li, [email protected]
* created on 2018.8.22
*/
#ifndef __DELIST_H__
#define __DELIST_H__
#include <functional>
#include <initializer_list>
#include <iostream>
#include <memory>
#include <utility>
template <typename T>
class __delist_node {
public:
typedef T value_type;
typedef std::shared_ptr<__delist_node> node_ptr;
value_type __val;
node_ptr __link1, __link2;
public:
/// Self loop as default. Which one is link1 and which one is link2 could be arbitrary.
__delist_node(const value_type& val = value_type(),
const node_ptr& link1 = nullptr, const node_ptr& link2 = nullptr):
__val(val), __link1(link1), __link2(link2) {}
~__delist_node() {}
node_ptr& link_not_to(const node_ptr& p) {
return __link1 == p ? __link2 : __link1;
}
node_ptr& link_to(const node_ptr& p) {
return __link1 == p ? __link1 : __link2;
}
void clear_links() {
__link1 = __link2 = nullptr;
}
void both_link_to(const node_ptr& p) {
__link1 = __link2 = p;
}
};
template <typename T>
class __delist_const_iterator {
typedef __delist_node<T> node_type;
typedef typename node_type::node_ptr node_ptr;
public:
typedef T value_type;
typedef T& reference;
typedef const T& const_reference;
typedef T* pointer;
typedef const T* const_pointer;
typedef std::bidirectional_iterator_tag iterator_category;
protected:
node_ptr __cur;
node_ptr __next;
public:
__delist_const_iterator(const node_ptr& cur = nullptr, const node_ptr& next = nullptr): __cur(cur), __next(next) {}
__delist_const_iterator(const __delist_const_iterator& rhs): __cur(rhs.__cur), __next(rhs.__next) {}
__delist_const_iterator& operator ++ () {
node_ptr nn = __next->link_not_to(__cur);
__cur = __next;
__next = nn;
return *this;
}
__delist_const_iterator operator ++ (int) {
node_ptr ret(*this);
node_ptr nn = __next->link_not_to(__cur);
__cur = __next;
__next = nn;
return ret;
}
__delist_const_iterator& operator -- () {
node_ptr pre = __cur->link_not_to(__next);
__next = __cur;
__cur = pre;
return *this;
}
__delist_const_iterator operator -- (int) {
node_ptr ret(*this);
node_ptr pre = __cur->link_not_to(__next);
__next = __cur;
__cur = pre;
return ret;
}
const_reference operator * () const {
return __cur->__val;
}
const_pointer operator -> () const {
return &(__cur->__val);
}
bool operator == (const __delist_const_iterator& rhs) const {
return __cur == rhs.__cur;
}
bool operator != (const __delist_const_iterator& rhs) const {
return __cur != rhs.__cur;
}
__delist_const_iterator& operator = (const __delist_const_iterator& rhs) {
__cur = rhs.__cur;
__next = rhs.__next;
return *this;
}
};
template <typename T>
class __delist_iterator: public __delist_const_iterator<T> {
typedef __delist_const_iterator<T> base_type;
typedef __delist_node<T> node_type;
typedef typename node_type::node_ptr node_ptr;
public:
typedef T value_type;
typedef T& reference;
typedef const T& const_reference;
typedef T* pointer;
typedef const T* const_pointer;
typedef std::bidirectional_iterator_tag iterator_category;
public:
__delist_iterator(const node_ptr& cur = nullptr, const node_ptr& next = nullptr): base_type(cur, next) {}
__delist_iterator(const __delist_iterator& rhs): base_type(rhs.__cur, rhs.__next) {}
reference operator * () {
return base_type::__cur->__val;
}
pointer operator -> () {
return &(base_type::__cur->__val);
}
};
template <typename T>
class delist {
typedef __delist_node<T> node_type;
typedef typename node_type::node_ptr node_ptr;
public:
typedef T value_type;
typedef T& reference;
typedef const T& const_reference;
typedef T* pointer;
typedef const T* const_pointer;
typedef __delist_iterator<value_type> iterator;
typedef __delist_const_iterator<value_type> const_iterator;
typedef size_t size_type;
private:
node_ptr __phead; /// link1 point to next(the first one), link2 previous(last one).
size_type __size;
private:
node_ptr make_new_node(const value_type& v = value_type(),
const node_ptr& l1 = nullptr, const node_ptr& l2 = nullptr) {
return std::make_shared<node_type>(v, l1, l2);
}
public:
delist(): __phead(make_new_node()), __size(0) {
__phead->both_link_to(__phead);
}
delist(std::initializer_list<value_type> init): __phead(make_new_node()), __size(0) {
__phead->both_link_to(__phead);
for (const value_type& i : init) {
push_back(i);
}
}
~delist() {
clear();
__phead->clear_links();
__phead = nullptr;
}
void clear() {
node_ptr pre = __phead;
node_ptr it = __phead->__link1;
while (it != __phead) {
node_ptr nxt = it->link_not_to(pre);
it->clear_links();
pre = it;
it = nxt;
}
__phead->both_link_to(__phead); // keep __phead self loop when clear
__size = 0;
}
delist& reverse() {
std::swap(__phead->__link1, __phead->__link2);
return *this;
}
size_type size() const { return __size; }
bool empty() const { return __size == 0; }
iterator end() {
return iterator(__phead, __phead->__link1);
}
iterator begin() {
node_ptr first = __phead->__link1;
node_ptr second = first->link_not_to(__phead);
return iterator(first, second);
}
const_iterator cend() const {
return const_iterator(__phead, __phead->__link1);
}
const_iterator cbegin() const {
node_ptr first = __phead->__link1;
node_ptr second = first->link_not_to(__phead);
return const_iterator(first, second);
}
void push_back(const value_type& v) {
node_ptr last = __phead->__link2;
node_ptr new_node = make_new_node(v, __phead, last);
last->link_to(__phead) = new_node;
__phead->__link2 = new_node;
++__size;
}
void push_front(const value_type& v) {
reverse();
push_back(v);
reverse();
}
reference front() {
return __phead->__link1->__val;
}
const_reference front() const {
return __phead->__link1->__val;
}
reference back() {
return __phead->__link2->__val;
}
const_reference back() const {
return __phead->__link2->__val;
}
value_type pop_back() {
if (__size <= 0) {
throw std::runtime_error("delist is empty");
}
node_ptr last = __phead->__link2;
value_type ret = last->__val;
node_ptr new_last = last->link_not_to(__phead);
new_last->link_to(last) = __phead;
__phead->__link2 = new_last;
--__size;
return ret;
}
value_type pop_front() {
reverse();
value_type ret = pop_back();
reverse();
return ret;
}
/// Extend rhs into this on the end, then empty rhs. No node copied, but all transferred, preform in O(1) time.
delist& extend(delist& rhs) {
if (!rhs.__size) return *this;
if (*this == rhs) return *this;
node_ptr last = __phead->__link2;
node_ptr rhs_first = rhs.__phead->__link1;
last->link_to(__phead) = rhs_first;
rhs_first->link_to(rhs.__phead) = last;
node_ptr rhs_last = rhs.__phead->__link2;
rhs_last->link_to(rhs.__phead) = __phead;
__phead->__link2 = rhs_last;
rhs.__phead->both_link_to(rhs.__phead);
__size += rhs.__size;
rhs.__size = 0;
return *this;
}
bool operator == (const delist& rhs) const {
return __phead == rhs.__phead;
}
bool operator != (const delist& rhs) const {
return !operator==(rhs);
}
};
template <typename T>
std::ostream& operator << (std::ostream& os, const delist<T>& dl) {
os << "[";
for (typename delist<T>::const_iterator it = dl.cbegin(), e = dl.cend(); it != e; ++it) {
if (it == dl.cbegin()) {
os << *it;
continue;
}
os << ", " << *it;
}
os << "]" << std::flush;
return os;
}
/* eof */
#endif