forked from parallel101/stl1weekend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathList.hpp
600 lines (497 loc) · 15.3 KB
/
List.hpp
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
#pragma once
#include <cstddef>
#include <iterator>
#include <memory>
#include <limits>
#include <algorithm>
#include <utility>
#include <initializer_list>
#include "_Common.hpp"
template <class T>
struct ListBaseNode {
ListBaseNode *m_next;
ListBaseNode *m_prev;
inline T &value();
inline T const &value() const;
};
template <class T>
struct ListValueNode : ListBaseNode<T> {
union {
T m_value;
};
};
template <class T>
inline T &ListBaseNode<T>::value() {
return static_cast<ListValueNode<T> &>(*this).m_value;
}
template <class T>
inline T const &ListBaseNode<T>::value() const {
return static_cast<ListValueNode<T> const &>(*this).m_value;
}
template <class T, class Alloc = std::allocator<T>>
struct List {
using value_type = T;
using allocator_type = Alloc;
using size_type = std::size_t;
using difference_type = std::ptrdiff_t;
using pointer = T *;
using const_pointer = T const *;
using reference = T &;
using const_reference = T const &;
private:
using ListNode = ListBaseNode<T>;
using AllocNode = typename std::allocator_traits<Alloc>::template rebind_alloc<ListValueNode<T>>;
ListNode m_dummy;
std::size_t m_size;
[[no_unique_address]] Alloc m_alloc;
ListNode *newNode() {
AllocNode allocNode{m_alloc};
return std::allocator_traits<AllocNode>::allocate(allocNode, 1);
}
void deleteNode(ListNode *node) noexcept {
AllocNode allocNode{m_alloc};
std::allocator_traits<AllocNode>::deallocate(allocNode, static_cast<ListValueNode<T> *>(node));
}
public:
List() noexcept {
m_size = 0;
m_dummy.m_prev = m_dummy.m_next = &m_dummy;
}
explicit List(Alloc const &alloc) noexcept : m_alloc(alloc) {
m_size = 0;
m_dummy.m_prev = m_dummy.m_next = &m_dummy;
}
List(List &&that) noexcept : m_alloc(std::move(that.m_alloc)) {
_uninit_move_assign(std::move(that));
}
List(List &&that, Alloc const &alloc) noexcept : m_alloc(alloc) {
_uninit_move_assign(std::move(that));
}
List &operator=(List &&that) {
m_alloc = std::move(that.m_alloc);
clear();
_uninit_move_assign(std::move(that));
}
private:
void _uninit_move_assign(List &&that) {
auto prev = that.m_dummy.m_prev;
auto next = that.m_dummy.m_next;
prev->m_next = &m_dummy;
next->m_prev = &m_dummy;
m_dummy = that.m_dummy;
that.m_dummy.m_prev = that.m_dummy.m_next = &that.m_dummy;
m_size = that.m_size;
that.m_size = 0;
}
public:
List(List const &that) : m_alloc(that.m_alloc) {
_uninit_assign(that.cbegin(), that.cend());
}
List(List const &that, Alloc const &alloc) : m_alloc(alloc) {
_uninit_assign(that.cbegin(), that.cend());
}
List &operator=(List const &that) {
assign(that.cbegin(), that.cend());
}
bool empty() noexcept {
return m_dummy.m_prev == m_dummy.m_next;
}
T &front() noexcept {
return m_dummy.m_next->value();
}
T &back() noexcept {
return m_dummy.m_prev->value();
}
T const &front() const noexcept {
return m_dummy.m_next->value();
}
T const &back() const noexcept {
return m_dummy.m_prev->value();
}
explicit List(size_t n, Alloc const &alloc = Alloc()) : m_alloc(alloc) {
_uninit_assign(n);
}
explicit List(size_t n, T const &val, Alloc const &alloc = Alloc()) : m_alloc(alloc) {
_uninit_assign(n, val);
}
// input_iterator = *it it++ ++it it!=it it==it
// output_iterator = *it=val it++ ++it it!=it it==it
// forward_iterator = *it *it=val it++ ++it it!=it it==it
// bidirectional_iterator = *it *it=val it++ ++it it-- --it it!=it it==it
// random_access_iterator = *it *it=val it[n] it[n]=val it++ ++it it-- --it it+=n it-=n it+n it-n it!=it it==it
template <std::input_iterator InputIt>
List(InputIt first, InputIt last, Alloc const &alloc = Alloc()) : m_alloc(alloc) {
_uninit_assign(first, last);
}
List(std::initializer_list<T> ilist, Alloc const &alloc = Alloc())
: List(ilist.begin(), ilist.end(), alloc) {}
List &operator=(std::initializer_list<T> ilist) {
assign(ilist);
}
private:
template <std::input_iterator InputIt>
void _uninit_assign(InputIt first, InputIt last) {
m_size = 0;
ListNode *prev = &m_dummy;
while (first != last) {
ListNode *node = newNode();
prev->m_next = node;
node->m_prev = prev;
std::construct_at(&node->value(), *first);
prev = node;
++first;
++m_size;
}
m_dummy.m_prev = prev;
prev->m_next = &m_dummy;
}
void _uninit_assign(size_t n, T const &val) {
ListNode *prev = &m_dummy;
while (n) {
ListNode *node = newNode();
prev->m_next = node;
node->m_prev = prev;
std::construct_at(&node->value(), val);
prev = node;
--n;
}
m_dummy.m_prev = prev;
prev->m_next = &m_dummy;
m_size = n;
}
void _uninit_assign(size_t n) {
ListNode *prev = &m_dummy;
while (n) {
ListNode *node = newNode();
prev->m_next = node;
node->m_prev = prev;
std::construct_at(&node->value());
prev = node;
--n;
}
m_dummy.m_prev = prev;
prev->m_next = &m_dummy;
m_size = n;
}
public:
std::size_t size() const noexcept {
return m_size;
}
static constexpr std::size_t max_size() noexcept {
return std::numeric_limits<std::size_t>::max();
}
template <std::input_iterator InputIt>
void assign(InputIt first, InputIt last) {
clear();
_uninit_assign(first, last);
}
void assign(std::initializer_list<T> ilist) {
clear();
_uninit_assign(ilist.begin(), ilist.end());
}
void assign(size_t n, T const &val) {
clear();
_uninit_assign(n, val);
}
void push_back(T const &val) {
emplace_back(val);
}
void push_back(T &&val) {
emplace_back(std::move(val));
}
void push_front(T const &val) {
emplace_front(val);
}
void push_front(T &&val) { // don't repeat yourself (DRY)
emplace_front(std::move(val));
}
template <class ...Args>
T &emplace_back(Args &&...args) {
ListNode *node = newNode();
ListNode *prev = m_dummy.m_prev;
prev->m_next = node;
node->m_prev = prev;
node->m_next = &m_dummy;
std::construct_at(&node->value(), std::forward<Args>(args)...);
m_dummy.m_prev = node;
++m_size;
return node->value();
}
template <class ...Args>
T &emplace_front(Args &&...args) {
ListNode *node = newNode();
ListNode *next = m_dummy.m_next;
next->m_prev = node;
node->m_next = next;
node->m_prev = &m_dummy;
std::construct_at(&node->value(), std::forward<Args>(args)...);
m_dummy.m_next = node;
++m_size;
return node->value();
}
/* template <std::invocable<T &> Visitor> */
/* void foreach(Visitor visit) { */
/* ListNode *curr = m_dummy.m_next; */
/* while (curr != &m_dummy) { */
/* visit(curr->value()); */
/* curr = curr->m_next; */
/* } */
/* } */
/* template <std::invocable<T const &> Visitor> */
/* void foreach(Visitor visit) const { */
/* ListNode *curr = m_dummy.m_next; */
/* while (curr != &m_dummy) { */
/* visit(curr->value()); */
/* curr = curr->m_next; */
/* } */
/* } */
~List() noexcept {
clear();
}
void clear() noexcept {
ListNode *curr = m_dummy.m_next;
while (curr != &m_dummy) {
std::destroy_at(&curr->value());
auto next = curr->m_next;
deleteNode(curr);
curr = next;
}
m_dummy.m_prev = m_dummy.m_next = &m_dummy;
m_size = 0;
}
struct iterator {
using iterator_category = std::bidirectional_iterator_tag;
using value_type = T;
using difference_type = ptrdiff_t;
using pointer = T *;
using reference = T &;
private:
ListNode *m_curr;
friend List;
explicit iterator(ListNode *curr) noexcept
: m_curr(curr) {}
public:
iterator() = default;
iterator &operator++() noexcept { // ++iterator
m_curr = m_curr->m_next;
return *this;
}
iterator operator++(int) noexcept { // iterator++
auto tmp = *this;
++*this;
return tmp;
}
iterator &operator--() noexcept { // --iterator
m_curr = m_curr->m_prev;
return *this;
}
iterator operator--(int) noexcept { // iterator--
auto tmp = *this;
++*this;
return tmp;
}
T &operator*() const noexcept {
return m_curr->value();
}
bool operator!=(iterator const &that) const noexcept {
return m_curr != that.m_curr;
}
bool operator==(iterator const &that) const noexcept {
return !(*this != that);
}
};
struct const_iterator {
using iterator_category = std::bidirectional_iterator_tag;
using value_type = T;
using difference_type = ptrdiff_t;
using pointer = T const *;
using reference = T const &;
private:
ListNode const *m_curr;
friend List;
explicit const_iterator(ListNode const *curr) noexcept
: m_curr(curr) {}
public:
const_iterator() = default;
const_iterator(iterator that) noexcept : m_curr(that.m_curr) {
}
explicit operator iterator() noexcept {
return iterator{const_cast<ListNode *>(m_curr)};
}
const_iterator &operator++() noexcept { // ++iterator
m_curr = m_curr->m_next;
return *this;
}
const_iterator operator++(int) noexcept { // iterator++
auto tmp = *this;
++*this;
return tmp;
}
const_iterator &operator--() noexcept { // --iterator
m_curr = m_curr->m_prev;
return *this;
}
const_iterator operator--(int) noexcept { // iterator--
auto tmp = *this;
++*this;
return tmp;
}
T const &operator*() const noexcept {
return m_curr->value();
}
bool operator!=(const_iterator const &that) const noexcept {
return m_curr != that.m_curr;
}
bool operator==(const_iterator const &that) const noexcept {
return !(*this != that);
}
};
iterator begin() noexcept {
return iterator{m_dummy.m_next};
}
iterator end() noexcept {
return iterator{&m_dummy};
}
const_iterator cbegin() const noexcept {
return const_iterator{m_dummy.m_next};
}
const_iterator cend() const noexcept {
return const_iterator{&m_dummy};
}
const_iterator begin() const noexcept {
return cbegin();
}
const_iterator end() const noexcept {
return cend();
}
using reverse_iterator = std::reverse_iterator<iterator>;
using reverse_const_iterator = std::reverse_iterator<const_iterator>;
reverse_iterator rbegin() noexcept {
return std::make_reverse_iterator(end());
}
reverse_iterator rend() noexcept {
return std::make_reverse_iterator(begin());
}
reverse_const_iterator crbegin() const noexcept {
return std::make_reverse_iterator(cend());
}
reverse_const_iterator crend() const noexcept {
return std::make_reverse_iterator(cbegin());
}
reverse_const_iterator rbegin() const noexcept {
return crbegin();
}
reverse_const_iterator rend() const noexcept {
return crend();
}
iterator erase(const_iterator pos) noexcept {
ListNode *node = const_cast<ListNode *>(pos.m_curr);
auto next = node->m_next;
auto prev = node->m_prev;
prev->m_next = next;
next->m_prev = prev;
std::destroy_at(&node->value());
deleteNode(node);
--m_size;
return iterator{next};
}
iterator erase(const_iterator first, const_iterator last) noexcept {
while (first != last) {
first = erase(first);
}
return iterator(first);
}
void pop_front() noexcept {
erase(begin());
}
void pop_back() noexcept {
erase(std::prev(end()));
}
std::size_t remove(T const &val) noexcept {
auto first = begin();
auto last = begin();
std::size_t count = 0;
while (first != last) {
if (*first == val) {
first = erase(first);
++count;
} else {
++first;
}
}
return count;
}
template <class Pred>
std::size_t remove_if(Pred &&pred) noexcept {
auto first = begin();
auto last = begin();
std::size_t count = 0;
while (first != last) {
if (pred(*first)) {
first = erase(first);
++count;
} else {
++first;
}
}
return count;
}
template <class ...Args>
iterator emplace(const_iterator pos, Args &&...args) {
ListNode *curr = newNode();
ListNode *next = const_cast<ListNode *>(pos.m_curr);
ListNode *prev = next->m_prev;
curr->m_prev = prev;
prev->m_next = curr;
curr->m_next = next;
next->m_prev = curr;
std::construct_at(&curr->value(), std::forward<Args>(args)...);
++m_size;
return iterator{curr};
}
iterator insert(const_iterator pos, const T &val) {
return emplace(pos, val);
}
iterator insert(const_iterator pos, T &&val) {
return emplace(pos, std::move(val));
}
iterator insert(const_iterator pos, std::size_t n, T const &val) {
auto orig = pos;
bool had_orig = false;
while (n) {
pos = emplace(pos, val);
if (!had_orig) {
had_orig = true;
orig = pos;
}
++pos;
--n;
}
return iterator(orig);
}
template <std::input_iterator InputIt>
iterator insert(const_iterator pos, InputIt first, InputIt last) {
auto orig = pos;
bool had_orig = false;
while (first != last) {
pos = emplace(pos, *first);
if (!had_orig) {
had_orig = true;
orig = pos;
}
++pos;
++first;
}
return iterator(orig);
}
iterator insert(const_iterator pos, std::initializer_list<T> ilist) {
return insert(pos, ilist.begin(), ilist.end());
}
void splice(const_iterator pos, List &&that) {
insert(pos, std::make_move_iterator(that.begin()), std::make_move_iterator(that.end()));
}
Alloc get_allocator() const noexcept {
return m_alloc;
}
_LIBPENGCXX_DEFINE_COMPARISON(List);
};