-
Notifications
You must be signed in to change notification settings - Fork 0
/
forward_list2.hpp
482 lines (404 loc) · 14.5 KB
/
forward_list2.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
/*
* Copyright (c) 2021-2022 Pavel I. Kryukov
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef FORWARD_LIST_2_HPP
#define FORWARD_LIST_2_HPP
#include <forward_list>
#include <functional>
template<typename T, class Allocator = std::allocator<T>>
class forward_list2
{
using Base = std::forward_list<T, Allocator>;
public:
using value_type = T;
using allocator_type = Allocator;
using size_type = typename Base::size_type;
using difference_type = typename Base::difference_type;
using reference = value_type&;
using const_reference = const value_type&;
using pointer = typename std::allocator_traits<Allocator>::pointer;
using const_pointer = typename std::allocator_traits<Allocator>::const_pointer;
using iterator = typename Base::iterator;
using const_iterator = typename Base::const_iterator;
forward_list2() :
m_list()
{
adjust_last_iterator_on_clear();
}
explicit forward_list2(const Allocator& alloc) :
m_list(alloc)
{
adjust_last_iterator_on_clear();
}
forward_list2(size_type count, const T& value, const Allocator& alloc = Allocator()) :
forward_list2(alloc)
{
insert_to_empty(count, value);
}
template<class InputIt>
forward_list2(InputIt first, InputIt last, const Allocator& alloc = Allocator()) :
forward_list2(alloc)
{
insert_to_empty(first, last);
}
forward_list2(const forward_list2& other) :
forward_list2(std::allocator_traits<allocator_type>::select_on_container_copy_construction(other.get_allocator()))
{
insert_to_empty(other.begin(), other.end());
}
forward_list2(const forward_list2& other, const Allocator& alloc) :
forward_list2(alloc)
{
insert_to_empty(other.begin(), other.end());
}
forward_list2(forward_list2&& other) :
m_list(std::move(other.m_list)), m_last(std::move(other.m_last))
{
other.adjust_last_iterator_on_clear();
}
forward_list2(forward_list2&& other, const Allocator& alloc) :
forward_list2(alloc)
{
m_last = m_list.insert_after(before_begin(),
std::make_move_iterator(other.begin()),
std::make_move_iterator(other.end()));
other.clear();
}
forward_list2(std::initializer_list<T> init, const Allocator& alloc = Allocator()) :
forward_list2(alloc)
{
insert_to_empty(init);
}
~forward_list2() = default;
forward_list2& operator=(const forward_list2& other)
{
m_list = other.m_list;
adjust_last_iterator_linear_time();
return *this;
}
forward_list2& operator=(forward_list2&& other)
#ifdef __cpp_lib_allocator_traits_is_always_equal
noexcept(std::allocator_traits<Allocator>::is_always_equal::value)
#endif
{
m_list = std::move(other.m_list);
m_last = other.m_last;
other.adjust_last_iterator_on_clear();
return *this;
}
forward_list2& operator=(std::initializer_list<T> ilist)
{
assign(std::move(ilist));
return *this;
}
void assign(size_type count, const T& value)
{
m_list.clear();
insert_to_empty(count, value);
}
template<class InputIt>
void assign(InputIt first, InputIt last)
{
m_list.clear();
insert_to_empty(first, last);
}
void assign(std::initializer_list<T> ilist)
{
m_list.clear();
insert_to_empty(ilist);
}
allocator_type get_allocator() const noexcept { return m_list.get_allocator(); }
reference front() { return *begin(); }
const_reference front() const { return *begin(); }
// New!
// reference back() { return *before_end(); }
const_reference back() const { return *before_end(); }
iterator before_begin() noexcept { return m_list.before_begin(); }
const_iterator before_begin() const noexcept { return m_list.before_begin(); }
const_iterator cbefore_begin() const noexcept { return m_list.cbefore_begin(); }
iterator begin() noexcept { return m_list.begin(); }
const_iterator begin() const noexcept { return m_list.begin(); }
const_iterator cbegin() const noexcept { return m_list.cbegin(); }
// New!
// iterator before_end() noexcept { return m_last; }
const_iterator before_end() const noexcept { return m_last; }
const_iterator cbefore_end() const noexcept { return m_last; }
iterator end() noexcept { return m_list.end(); }
const_iterator end() const noexcept { return m_list.end(); }
const_iterator cend() const noexcept { return m_list.cend(); }
[[nodiscard]] bool empty() const noexcept { return m_list.empty(); }
size_type max_size() const noexcept { return m_list.max_size(); }
void clear() noexcept
{
m_list.clear();
adjust_last_iterator_on_clear();
}
iterator insert_after(const_iterator pos, const T& value)
{
auto last_pos = m_list.insert_after(pos, value);
adjust_last_iterator_on_insertion(pos, last_pos);
return last_pos;
}
iterator insert_after(const_iterator pos, T&& value)
{
auto last_pos = m_list.insert_after(pos, std::move(value));
adjust_last_iterator_on_insertion(pos, last_pos);
return last_pos;
}
iterator insert_after(const_iterator pos, size_type count, const T& value)
{
auto last_pos = m_list.insert_after(pos, count, value);
adjust_last_iterator_on_insertion(pos, last_pos);
return last_pos;
}
template<class InputIt>
iterator insert_after(const_iterator pos, InputIt first, InputIt last)
{
auto last_pos = m_list.insert_after(pos, first, last);
adjust_last_iterator_on_insertion(pos, last_pos);
return last_pos;
}
template<class... Args>
iterator emplace_after(const_iterator pos, Args&&... args)
{
auto last_pos = m_list.emplace_after(pos, std::forward<Args>(args)...);
adjust_last_iterator_on_insertion(pos, last_pos);
return last_pos;
}
iterator erase_after(const_iterator pos)
{
auto next = m_list.erase_after(pos);
adjust_last_iterator_on_deletion(pos, next);
return next;
}
iterator erase_after(const_iterator first, const_iterator last)
{
auto next = m_list.erase_after(first, last);
adjust_last_iterator_on_deletion(first, last);
return next;
}
void push_front(const T& value) { insert_after(before_begin(), value); }
void push_front(T&& value) { insert_after(before_begin(), std::move(value)); }
// New!
void push_back(const T& value) { insert_after(before_end(), value); }
void push_back(T&& value) { insert_after(before_end(), std::move(value)); }
template<class... Args>
reference emplace_front(Args&&... args) { return *emplace_after(before_begin(), std::forward<Args>(args)...); }
// New!
template<class... Args>
reference emplace_back(Args&&... args) { return *emplace_after(before_end(), std::forward<Args>(args)...); }
void pop_front() { erase_after(before_begin()); }
void resize(size_type count, const T& value)
{
auto it = before_begin();
for (size_type i = 0; i < count; ++i, ++it) {
if (it == before_end()) {
insert_after(it, count - i, value);
return;
}
}
erase_after(it, end());
}
void resize(size_type count)
{
resize(count, T{});
}
void swap(forward_list2& other) noexcept
{
std::swap(m_list, other.m_list);
std::swap(m_last, other.m_last);
}
void merge(forward_list2& other) { merge(other, std::less<T>()); }
void merge(forward_list2&& other) { merge(other); }
template <class Compare>
void merge(forward_list2&& other, Compare comp) { merge(other, comp); }
template <class Compare>
void merge(forward_list2& other, Compare comp)
{
// Standard does not require this, so do it explicitly
if (std::addressof(other) == this)
return;
m_list.merge(other.m_list, comp);
adjust_last_iterator_on_merge(other.m_last);
other.adjust_last_iterator_on_clear();
}
void splice_after(const_iterator pos, forward_list2& other)
{
adjust_last_iterator_on_insertion(pos, other.m_last);
m_list.splice_after(pos, std::move(other.m_list));
other.adjust_last_iterator_on_clear();
}
void splice_after(const_iterator pos, forward_list2& other, const_iterator it)
{
adjust_last_iterator_on_insertion(pos, std::next(it));
m_list.splice_after(pos, std::move(other.m_list), it);
other.adjust_last_iterator_on_deletion(it, std::next(it));
}
void splice_after(const_iterator pos, forward_list2& other, const_iterator first, const_iterator last)
{
for (; std::next(first) != last; ++pos)
splice_after(pos, std::move(other), first);
}
void splice_after(const_iterator pos, forward_list2&& other) { splice_after(pos, other); }
void splice_after(const_iterator pos, forward_list2&& other, const_iterator it) { splice_after(pos, other, it); }
void splice_after(const_iterator pos, forward_list2&& other, const_iterator first, const_iterator last) { splice_after(pos, other, first, last); }
void remove(const T& value)
{
for (auto it = before_begin(); it != before_end();) {
auto next = std::next(it);
if (*next == value)
erase_after(it);
else
it = next;
}
}
template<typename UnaryPredicate>
void remove_if(UnaryPredicate p)
{
for (auto it = before_begin(); it != before_end();) {
auto next = std::next(it);
if (p(*next))
erase_after(it);
else
it = next;
}
}
void reverse() noexcept
{
m_last = begin();
m_list.reverse();
}
void unique() { return unique(std::equal_to<T>()); }
template<typename BinaryPredicate>
void unique(BinaryPredicate b)
{
for (auto it = begin(); it != before_end();) {
auto next = std::next(it);
if (b(*next, *it))
erase_after(it);
else
it = next;
}
}
void sort() { sort(std::less<T>()); }
template<typename Compare>
void sort(Compare c)
{
// It is compliant to standard since O(N log N) + O(N) => O(N log N)
// But I agree it is not the greatest implementation
m_list.sort(c);
adjust_last_iterator_linear_time();
}
friend bool operator==(const forward_list2& lhs, const forward_list2& rhs)
{
return lhs.m_list == rhs.m_list;
}
#ifdef __cpp_lib_three_way_comparison
friend auto operator<=>(const forward_list2& lhs, const forward_list2& rhs)
{
return lhs.m_list <=> rhs.m_list;
}
#else
friend bool operator!=(const forward_list2& lhs, const forward_list2& rhs)
{
return lhs.m_list != rhs.m_list;
}
friend bool operator<(const forward_list2& lhs, const forward_list2& rhs)
{
return lhs.m_list < rhs.m_list;
}
friend bool operator<=(const forward_list2& lhs, const forward_list2& rhs)
{
return lhs.m_list <= rhs.m_list;
}
friend bool operator>(const forward_list2& lhs, const forward_list2& rhs)
{
return lhs.m_list > rhs.m_list;
}
friend bool operator>=(const forward_list2& lhs, const forward_list2& rhs)
{
return lhs.m_list >= rhs.m_list;
}
#endif
private:
void insert_to_empty(size_type count, const T& value)
{
m_last = m_list.insert_after(m_list.before_begin(), count, value);
}
template<class InputIt>
void insert_to_empty(InputIt first, InputIt last)
{
m_last = m_list.insert_after(m_list.before_begin(), first, last);
}
void insert_to_empty(std::initializer_list<T> init)
{
m_last = m_list.insert_after(m_list.before_begin(), std::move(init));
}
void adjust_last_iterator_on_clear() noexcept
{
m_last = m_list.before_begin();
}
void adjust_last_iterator_on_insertion(const const_iterator& first, const const_iterator& last) noexcept
{
if (first == m_last)
m_last = last;
}
void adjust_last_iterator_on_deletion(const const_iterator& first, const const_iterator& last) noexcept
{
if (last == cend())
m_last = first;
}
void adjust_last_iterator_on_merge(const const_iterator& other_last) noexcept
{
if (std::next(m_last) != cend())
m_last = other_last;
}
void adjust_last_iterator_linear_time() noexcept
{
m_last = m_list.before_begin();
while (std::next(m_last) != cend())
++m_last;
}
Base m_list;
const_iterator m_last;
};
namespace std
{
template<typename T, typename Alloc>
void swap(forward_list2<T, Alloc>& lhs, forward_list2<T, Alloc>& rhs)
noexcept(noexcept(lhs.swap(rhs)))
{
lhs.swap(rhs);
}
#ifdef __cpp_lib_erase_if
template<typename T, typename Alloc, typename U>
auto erase(forward_list2<T, Alloc>& c, const U& value)
{
return c.remove_if([&](auto& x){ return x == value; });
}
template<typename T, typename Alloc, typename Predicate>
auto erase_if(forward_list2<T, Alloc>& c, Predicate p)
{
return c.remove_if(p);
}
#endif
}
#endif // FORWARD_LIST_2_HPP