-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathforward_list.hpp
716 lines (598 loc) · 17.9 KB
/
forward_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
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
/**
* @file forward_list.hpp
* @brief
* @date 2021-01-27
* @author Peter
* @copyright
* Peter of [ThinkSpirit Laboratory](http://thinkspirit.org/)
* of [Nanjing University of Information Science & Technology](http://www.nuist.edu.cn/)
* all rights reserved
*/
#ifndef KERBAL_AUTONM_FORWARD_LIST_HPP
#define KERBAL_AUTONM_FORWARD_LIST_HPP
#include <kerbal/container/detail/container_rebind_allocator_overload.hpp>
#include <kerbal/container/detail/single_list_base/single_list_node.hpp>
#include <kerbal/container/detail/forward_list_base/forward_list_iterator.hpp>
#include <kerbal/container/detail/forward_list_base.hpp>
#include <kerbal/compare/sequence_compare.hpp>
#include <kerbal/compatibility/constexpr.hpp>
#include <kerbal/compatibility/move.hpp>
#include <kerbal/compatibility/namespace_std_scope.hpp>
#include <kerbal/compatibility/noexcept.hpp>
#include <kerbal/utility/declval.hpp>
#include <kerbal/utility/noncopyable.hpp>
#if __cplusplus < 201103L
# include <kerbal/macro/macro_concat.hpp>
# include <kerbal/macro/ppexpand.hpp>
#endif
#if __cplusplus >= 201103L
# include <kerbal/utility/forward.hpp>
#endif
namespace kerbal
{
namespace autonm
{
namespace detail
{
template <typename T>
class autonm_sl_node :
public kerbal::container::detail::sl_node<T>
{
private:
typedef kerbal::container::detail::sl_node<T> super;
public:
KERBAL_CONSTEXPR
autonm_sl_node() :
super(kerbal::utility::in_place_t())
{
}
# if __cplusplus >= 201103L
template <typename ... Args>
KERBAL_CONSTEXPR
explicit autonm_sl_node(kerbal::utility::in_place_t in_place, Args && ... args) :
super(in_place, kerbal::utility::forward<Args>(args)...)
{
}
# else
# define EMPTY
# define LEFT_JOIN_COMMA(exp) , exp
# define THEAD_NOT_EMPTY(exp) template <exp>
# define TARGS_DECL(i) typename KERBAL_MACRO_CONCAT(Arg, i)
# define ARGS_DECL(i) const KERBAL_MACRO_CONCAT(Arg, i) & KERBAL_MACRO_CONCAT(arg, i)
# define ARGS_USE(i) KERBAL_MACRO_CONCAT(arg, i)
# define FBODY(i) \
KERBAL_OPT_PPEXPAND_WITH_COMMA_N(THEAD_NOT_EMPTY, EMPTY, TARGS_DECL, i) \
explicit autonm_sl_node( \
kerbal::utility::in_place_t in_place \
KERBAL_OPT_PPEXPAND_WITH_COMMA_N(LEFT_JOIN_COMMA, EMPTY, ARGS_DECL, i) \
) : \
super(in_place KERBAL_OPT_PPEXPAND_WITH_COMMA_N(LEFT_JOIN_COMMA, EMPTY, ARGS_USE, i)) \
{ \
} \
KERBAL_PPEXPAND_N(FBODY, KERBAL_PPEXPAND_EMPTY_SEPARATOR, 0)
KERBAL_PPEXPAND_N(FBODY, KERBAL_PPEXPAND_EMPTY_SEPARATOR, 20)
# undef EMPTY
# undef LEFT_JOIN_COMMA
# undef THEAD_NOT_EMPTY
# undef TARGS_DECL
# undef ARGS_DECL
# undef ARGS_USE
# undef FBODY
# endif
};
template <typename T, typename SemiAllocator>
struct fl_typedef_helper
{
typedef kerbal::autonm::detail::autonm_sl_node<T> auto_node;
typedef SemiAllocator semi_allocator_type;
typedef kerbal::container::detail::container_rebind_allocator_overload<
semi_allocator_type, auto_node
> fl_semi_allocator_overload;
};
} // namespace detail
template <typename T, typename SemiAllocator>
class forward_list :
protected kerbal::autonm::detail::fl_typedef_helper<T, SemiAllocator>::fl_semi_allocator_overload,
protected kerbal::container::detail::fl_type_only<T>,
private kerbal::utility::noncopyable
{
private:
typedef kerbal::autonm::detail::fl_typedef_helper<T, SemiAllocator> fl_typedef_helper;
typedef kerbal::container::detail::fl_type_unrelated fl_type_unrelated;
typedef typename fl_typedef_helper::fl_semi_allocator_overload fl_semi_allocator_overload;
typedef kerbal::container::detail::fl_type_only<T> fl_type_only;
public:
typedef typename fl_typedef_helper::auto_node auto_node;
public:
typedef T value_type;
typedef const value_type const_type;
typedef value_type & reference;
typedef const value_type & const_reference;
typedef value_type * pointer;
typedef const value_type * const_pointer;
# if __cplusplus >= 201103L
typedef value_type && rvalue_reference;
typedef const value_type && const_rvalue_reference;
# endif
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
typedef typename fl_type_only::iterator iterator;
typedef typename fl_type_only::const_iterator const_iterator;
public:
typedef SemiAllocator semi_allocator_type;
private:
typedef typename fl_semi_allocator_overload::rebind_allocator_type node_semi_allocator_type;
typedef typename fl_semi_allocator_overload::rebind_allocator_traits node_semi_allocator_traits;
private:
KERBAL_CONSTEXPR14
node_semi_allocator_type & semi_alloc() KERBAL_NOEXCEPT
{
return this->fl_semi_allocator_overload::alloc();
}
KERBAL_CONSTEXPR
const node_semi_allocator_type & semi_alloc() const KERBAL_NOEXCEPT
{
return this->fl_semi_allocator_overload::alloc();
}
public:
//===================
// construct/copy/destroy
KERBAL_CONSTEXPR20
forward_list()
KERBAL_CONDITIONAL_NOEXCEPT(
fl_type_only::is_nothrow_default_constructible::value
) :
fl_type_only()
{
}
# if __cplusplus >= 201103L
KERBAL_CONSTEXPR20
forward_list(forward_list && src)
KERBAL_CONDITIONAL_NOEXCEPT(
fl_type_only::is_nothrow_move_constructible::value
) :
fl_type_only(kerbal::compatibility::move(src))
{
}
# endif
KERBAL_CONSTEXPR20
~forward_list()
{
this->fl_type_only::k_destroy_using_allocator(this->semi_alloc());
}
public:
//===================
// move assign
# if __cplusplus >= 201103L
KERBAL_CONSTEXPR20
forward_list & operator=(forward_list && src)
KERBAL_CONDITIONAL_NOEXCEPT(
noexcept(
kerbal::utility::declthis<forward_list>()->assign(kerbal::compatibility::move(src))
)
)
{
this->assign(kerbal::compatibility::move(src));
return *this;
}
KERBAL_CONSTEXPR20
void assign(forward_list && src) KERBAL_NOEXCEPT
{
this->k_move_assign(
this->semi_alloc(),
static_cast<fl_type_only &&>(src)
);
}
# endif
//===================
// element access
using fl_type_only::front;
//===================
// iterator
using fl_type_only::before_begin;
using fl_type_only::cbefore_begin;
using fl_type_only::begin;
using fl_type_only::cbegin;
using fl_type_only::end;
using fl_type_only::cend;
using fl_type_only::nth;
using fl_type_only::index_of;
//===================
// lookup
using fl_type_only::find_before;
using fl_type_only::find_before_if;
//===================
// capacity
using fl_type_unrelated::empty;
using fl_type_unrelated::size;
//===================
// insert
KERBAL_CONSTEXPR20
void push_front(auto_node & node) KERBAL_NOEXCEPT
{
fl_type_unrelated::k_hook_node_after(this->basic_before_begin(), &node);
}
KERBAL_CONSTEXPR20
iterator insert_after(const_iterator before_pos, auto_node & node) KERBAL_NOEXCEPT
{
fl_type_unrelated::k_hook_node_after(before_pos, &node);
return (++before_pos).cast_to_mutable();
}
template <typename ForwardIterator>
KERBAL_CONSTEXPR20
typename kerbal::type_traits::enable_if<
kerbal::iterator::is_forward_compatible_iterator<ForwardIterator>::value,
iterator
>::type
insert_after(const_iterator before_pos, ForwardIterator first, ForwardIterator last)
{
if (first == last) {
return before_pos.cast_to_mutable();
}
ForwardIterator it(first);
ForwardIterator next(first); ++next;
while (next != last) {
(*it).next = &*next;
it = next;
++next;
}
fl_type_unrelated::k_hook_node_after(before_pos, &*first, &*it);
return iterator(&*it);
}
/*
template <typename ForwardIterator>
KERBAL_CONSTEXPR20
typename kerbal::type_traits::enable_if<
kerbal::iterator::is_forward_compatible_iterator<ForwardIterator>::value,
typename forward_list<T, SemiAllocator>::iterator
>::type
insert_after(const_iterator before_pos, ForwardIterator first, ForwardIterator last)
{
if (first == last) {
return before_pos.cast_to_mutable();
}
do {
fl_type_unrelated::k_hook_node_after(before_pos, &*first);
++first;
++before_pos;
} while (first != last);
return before_pos.cast_to_mutable();
}
*/
//===================
// erase
KERBAL_CONSTEXPR20
void clear()
{
this->fl_type_only::k_clear_using_allocator(this->semi_alloc());
}
KERBAL_CONSTEXPR20
iterator erase_after(const_iterator before_pos)
{
return fl_type_only::k_erase_after_using_allocator(this->semi_alloc(), before_pos);
}
KERBAL_CONSTEXPR20
iterator erase_after(const_iterator before_first, const_iterator last)
{
return fl_type_only::k_erase_after_using_allocator(this->semi_alloc(), before_first, last);
}
KERBAL_CONSTEXPR20
void pop_front()
{
this->fl_type_only::k_pop_front_using_allocator(this->semi_alloc());
}
//===================
// operation
KERBAL_CONSTEXPR20
void reverse_after(const_iterator before_first, const_iterator last) KERBAL_NOEXCEPT
{
fl_type_unrelated::k_reverse_after(before_first, last);
}
KERBAL_CONSTEXPR20
void reverse() KERBAL_NOEXCEPT
{
this->fl_type_only::reverse();
}
template <typename BinaryPredict>
KERBAL_CONSTEXPR20
void merge(forward_list & other, BinaryPredict cmp)
KERBAL_CONDITIONAL_NOEXCEPT(
noexcept(
kerbal::utility::declthis<forward_list>()->fl_type_only::k_merge(
static_cast<fl_type_only &>(other),
cmp
)
)
)
{
fl_type_only::k_merge(static_cast<fl_type_only &>(other), cmp);
}
KERBAL_CONSTEXPR20
void merge(forward_list & other)
KERBAL_CONDITIONAL_NOEXCEPT(
noexcept(
kerbal::utility::declthis<forward_list>()->fl_type_only::k_merge(
static_cast<fl_type_only &>(other)
)
)
)
{
this->fl_type_only::k_merge(static_cast<fl_type_only &>(other));
}
# if __cplusplus >= 201103L
template <typename BinaryPredict>
KERBAL_CONSTEXPR20
void merge(forward_list && other, BinaryPredict cmp)
KERBAL_CONDITIONAL_NOEXCEPT(
noexcept(
kerbal::utility::declthis<forward_list>()->fl_type_only::k_merge(
static_cast<fl_type_only &&>(other),
cmp
)
)
)
{
fl_type_only::k_merge(static_cast<fl_type_only &&>(other), cmp);
}
KERBAL_CONSTEXPR20
void merge(forward_list && other)
KERBAL_CONDITIONAL_NOEXCEPT(
noexcept(
kerbal::utility::declthis<forward_list>()->fl_type_only::k_merge(
static_cast<fl_type_only &&>(other)
)
)
)
{
this->fl_type_only::k_merge(static_cast<fl_type_only &&>(other));
}
# endif
template <typename BinaryPredict>
KERBAL_CONSTEXPR20
void sort_after(const_iterator before_first, const_iterator last, BinaryPredict cmp)
{
fl_type_only::k_sort_after(before_first, last, cmp);
}
KERBAL_CONSTEXPR20
void sort_after(const_iterator before_first, const_iterator last)
{
fl_type_only::k_sort_after(before_first, last);
}
template <typename BinaryPredict>
KERBAL_CONSTEXPR20
void sort(BinaryPredict cmp)
KERBAL_CONDITIONAL_NOEXCEPT(
noexcept(
kerbal::utility::declthis<forward_list>()->fl_type_only::k_sort(cmp)
)
)
{
this->fl_type_only::k_sort(cmp);
}
KERBAL_CONSTEXPR20
void sort()
KERBAL_CONDITIONAL_NOEXCEPT(
noexcept(
kerbal::utility::declthis<forward_list>()->fl_type_only::k_sort()
)
)
{
this->fl_type_only::k_sort();
}
template <typename UnaryPredicate>
KERBAL_CONSTEXPR20
size_type remove_after_if(const_iterator before_first, const_iterator last, UnaryPredicate predicate)
{
return fl_type_only::k_remove_after_if_using_allocator(this->semi_alloc(), before_first, last, predicate);
}
template <typename UnaryPredicate>
KERBAL_CONSTEXPR20
size_type remove_if(UnaryPredicate predicate)
{
return fl_type_only::k_remove_if_using_allocator(this->semi_alloc(), predicate);
}
KERBAL_CONSTEXPR20
size_type remove_after(const_iterator before_first, const_iterator last, const_reference val)
{
return fl_type_only::k_remove_after_using_allocator(this->semi_alloc(), before_first, last, val);
}
KERBAL_CONSTEXPR20
size_type remove(const_reference val)
{
return fl_type_only::k_remove_using_allocator(this->semi_alloc(), val);
}
template <typename BinaryPredict>
KERBAL_CONSTEXPR20
size_type unique(const_iterator first, const_iterator last, BinaryPredict equal_to)
{
return fl_type_only::k_unique_using_allocator(this->semi_alloc(), first, last, equal_to);
}
KERBAL_CONSTEXPR20
size_type unique(const_iterator first, const_iterator last)
{
return fl_type_only::k_unique_using_allocator(this->semi_alloc(), first, last);
}
template <typename BinaryPredict>
KERBAL_CONSTEXPR20
size_type unique(BinaryPredict equal_to)
{
return fl_type_only::k_unique_using_allocator(this->semi_alloc(), equal_to);
}
KERBAL_CONSTEXPR20
size_type unique()
{
return fl_type_only::k_unique_using_allocator(this->semi_alloc());
}
KERBAL_CONSTEXPR20
void splice_after(
const_iterator before_pos,
forward_list & other
) KERBAL_NOEXCEPT
{
fl_type_unrelated::k_splice_after(
before_pos,
static_cast<fl_type_unrelated &>(other)
);
}
KERBAL_CONSTEXPR20
void splice_after(
const_iterator before_pos,
forward_list & /*other*/,
const_iterator before_opos
) KERBAL_NOEXCEPT
{
fl_type_unrelated::k_splice_after(before_pos, before_opos);
}
KERBAL_CONSTEXPR20
void splice_after(
const_iterator before_pos,
forward_list & /*other*/,
const_iterator before_first, const_iterator last
) KERBAL_NOEXCEPT
{
fl_type_unrelated::k_splice_after(before_pos, before_first, last);
}
# if __cplusplus >= 201103L
KERBAL_CONSTEXPR20
void splice_after(
const_iterator before_pos,
forward_list && other
) KERBAL_NOEXCEPT
{
fl_type_unrelated::k_splice_after(
before_pos,
static_cast<fl_type_unrelated &&>(other)
);
}
KERBAL_CONSTEXPR20
void splice_after(
const_iterator before_pos,
forward_list && /*other*/,
const_iterator before_opos
) KERBAL_NOEXCEPT
{
fl_type_unrelated::k_splice_after(before_pos, before_opos);
}
KERBAL_CONSTEXPR20
void splice_after(
const_iterator before_pos,
forward_list && /*other*/,
const_iterator before_first, const_iterator last
) KERBAL_NOEXCEPT
{
fl_type_unrelated::k_splice_after(before_pos, before_first, last);
}
# endif
KERBAL_CONSTEXPR20
void swap(forward_list & with) KERBAL_NOEXCEPT
{
fl_semi_allocator_overload::k_swap_allocator_if_propagate(
static_cast<fl_semi_allocator_overload &>(*this),
static_cast<fl_semi_allocator_overload &>(with)
);
fl_type_unrelated::k_swap_type_unrelated(
static_cast<fl_type_unrelated &>(*this),
static_cast<fl_type_unrelated &>(with)
);
}
};
template <typename T, typename SemiAllocator>
KERBAL_CONSTEXPR20
bool operator==(
const forward_list<T, SemiAllocator> & lhs,
const forward_list<T, SemiAllocator> & rhs
)
{
return kerbal::compare::sequence_equal_to(
lhs.cbegin(), lhs.cend(),
rhs.cbegin(), rhs.cend()
);
}
template <typename T, typename SemiAllocator>
KERBAL_CONSTEXPR20
bool operator!=(
const forward_list<T, SemiAllocator> & lhs,
const forward_list<T, SemiAllocator> & rhs
)
{
return kerbal::compare::sequence_not_equal_to(
lhs.cbegin(), lhs.cend(),
rhs.cbegin(), rhs.cend()
);
}
template <typename T, typename SemiAllocator>
KERBAL_CONSTEXPR20
bool operator<(
const forward_list<T, SemiAllocator> & lhs,
const forward_list<T, SemiAllocator> & rhs
)
{
return kerbal::compare::sequence_less(
lhs.cbegin(), lhs.cend(),
rhs.cbegin(), rhs.cend()
);
}
template <typename T, typename SemiAllocator>
KERBAL_CONSTEXPR20
bool operator>(
const forward_list<T, SemiAllocator> & lhs,
const forward_list<T, SemiAllocator> & rhs
)
{
return kerbal::compare::sequence_greater(
lhs.cbegin(), lhs.cend(),
rhs.cbegin(), rhs.cend()
);
}
template <typename T, typename SemiAllocator>
KERBAL_CONSTEXPR20
bool operator<=(
const forward_list<T, SemiAllocator> & lhs,
const forward_list<T, SemiAllocator> & rhs
)
{
return kerbal::compare::sequence_less_equal(
lhs.cbegin(), lhs.cend(),
rhs.cbegin(), rhs.cend()
);
}
template <typename T, typename SemiAllocator>
KERBAL_CONSTEXPR20
bool operator>=(
const forward_list<T, SemiAllocator> & lhs,
const forward_list<T, SemiAllocator> & rhs
)
{
return kerbal::compare::sequence_greater_equal(
lhs.cbegin(), lhs.cend(),
rhs.cbegin(), rhs.cend()
);
}
} // namespace autonm
namespace algorithm
{
template <typename T, typename SemiAllocator>
KERBAL_CONSTEXPR20
void swap(
kerbal::autonm::forward_list<T, SemiAllocator> & a,
kerbal::autonm::forward_list<T, SemiAllocator> & b
)
KERBAL_CONDITIONAL_NOEXCEPT(noexcept(a.swap(b)))
{
a.swap(b);
}
} // namespace algorithm
} // namespace kerbal
KERBAL_NAMESPACE_STD_BEGIN
template <typename T, typename SemiAllocator>
KERBAL_CONSTEXPR20
void swap(
kerbal::autonm::forward_list<T, SemiAllocator> & a,
kerbal::autonm::forward_list<T, SemiAllocator> & b
)
KERBAL_CONDITIONAL_NOEXCEPT(noexcept(a.swap(b)))
{
a.swap(b);
}
KERBAL_NAMESPACE_STD_END
#endif // KERBAL_AUTONM_FORWARD_LIST_HPP