forked from mattreecebentley/plf_list
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplf_list.h
More file actions
3930 lines (3025 loc) · 124 KB
/
plf_list.h
File metadata and controls
3930 lines (3025 loc) · 124 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
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
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2024, Matthew Bentley (mattreecebentley@gmail.com) www.plflib.org
// zLib license (https://www.zlib.net/zlib_license.html):
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgement in the product documentation would be
// appreciated but is not required.
// 2. Altered source versions must be plainly marked as such, and must not be
// misrepresented as being the original software.
// 3. This notice may not be removed or altered from any source distribution.
#ifndef PLF_LIST_H
#define PLF_LIST_H
// Compiler-specific defines:
// Define default cases before possibly redefining:
#define PLF_NOEXCEPT throw()
#define PLF_NOEXCEPT_ALLOCATOR
#define PLF_CONSTEXPR
#define PLF_CONSTFUNC
#define PLF_EXCEPTIONS_SUPPORT
#if ((defined(__clang__) || defined(__GNUC__)) && !defined(__EXCEPTIONS)) || (defined(_MSC_VER) && !defined(_CPPUNWIND))
#undef PLF_EXCEPTIONS_SUPPORT
#include <exception> // std::terminate
#endif
#if defined(_MSC_VER) && !defined(__clang__) && !defined(__GNUC__)
// Suppress incorrect (unfixed MSVC bug) warnings re: constant expressions in constexpr-if statements
#pragma warning ( push )
#pragma warning ( disable : 4127 )
#if _MSC_VER >= 1600
#define PLF_MOVE_SEMANTICS_SUPPORT
#endif
#if _MSC_VER >= 1700
#define PLF_TYPE_TRAITS_SUPPORT
#define PLF_ALLOCATOR_TRAITS_SUPPORT
#endif
#if _MSC_VER >= 1800
#define PLF_VARIADICS_SUPPORT // Variadics, in this context, means both variadic templates and variadic macros are supported
#define PLF_DEFAULT_TEMPLATE_ARGUMENT_SUPPORT
#define PLF_INITIALIZER_LIST_SUPPORT
#endif
#if _MSC_VER >= 1900
#define PLF_ALIGNMENT_SUPPORT
#undef PLF_NOEXCEPT
#undef PLF_NOEXCEPT_ALLOCATOR
#define PLF_NOEXCEPT noexcept
#define PLF_NOEXCEPT_ALLOCATOR noexcept(noexcept(allocator_type()))
#define PLF_IS_ALWAYS_EQUAL_SUPPORT
#endif
#if defined(_MSVC_LANG) && (_MSVC_LANG >= 201703L)
#undef PLF_CONSTEXPR
#define PLF_CONSTEXPR constexpr
#endif
#if defined(_MSVC_LANG) && (_MSVC_LANG >= 202002L) && _MSC_VER >= 1929
#define PLF_CPP20_SUPPORT
#undef PLF_CONSTFUNC
#define PLF_CONSTFUNC constexpr
#endif
#elif defined(__cplusplus) && __cplusplus >= 201103L // C++11 support, at least
#if defined(__GNUC__) && defined(__GNUC_MINOR__) && !defined(__clang__) // If compiler is GCC/G++
#if (__GNUC__ == 4 && __GNUC_MINOR__ >= 3) || __GNUC__ > 4
#define PLF_MOVE_SEMANTICS_SUPPORT
#define PLF_VARIADICS_SUPPORT
#endif
#if (__GNUC__ == 4 && __GNUC_MINOR__ >= 4) || __GNUC__ > 4
#define PLF_DEFAULT_TEMPLATE_ARGUMENT_SUPPORT
#define PLF_INITIALIZER_LIST_SUPPORT
#endif
#if (__GNUC__ == 4 && __GNUC_MINOR__ >= 6) || __GNUC__ > 4
#undef PLF_NOEXCEPT
#undef PLF_NOEXCEPT_ALLOCATOR
#define PLF_NOEXCEPT noexcept
#define PLF_NOEXCEPT_ALLOCATOR noexcept(noexcept(allocator_type()))
#endif
#if (__GNUC__ == 4 && __GNUC_MINOR__ >= 7) || __GNUC__ > 4
#define PLF_ALLOCATOR_TRAITS_SUPPORT
#endif
#if (__GNUC__ == 4 && __GNUC_MINOR__ >= 8) || __GNUC__ > 4
#define PLF_ALIGNMENT_SUPPORT
#endif
#if __GNUC__ >= 5 // GCC v4.9 and below do not support std::is_trivially_copyable
#define PLF_TYPE_TRAITS_SUPPORT
#endif
#if __GNUC__ > 6
#define PLF_IS_ALWAYS_EQUAL_SUPPORT
#endif
#elif defined(__clang__) && !defined(__GLIBCXX__) && !defined(_LIBCPP_CXX03_LANG) && __clang_major__ >= 3
#define PLF_DEFAULT_TEMPLATE_ARGUMENT_SUPPORT
#define PLF_ALLOCATOR_TRAITS_SUPPORT
#define PLF_TYPE_TRAITS_SUPPORT
#if __has_feature(cxx_alignas) && __has_feature(cxx_alignof)
#define PLF_ALIGNMENT_SUPPORT
#endif
#if __has_feature(cxx_noexcept)
#undef PLF_NOEXCEPT
#undef PLF_NOEXCEPT_ALLOCATOR
#define PLF_NOEXCEPT noexcept
#define PLF_NOEXCEPT_ALLOCATOR noexcept(noexcept(allocator_type()))
#define PLF_IS_ALWAYS_EQUAL_SUPPORT
#endif
#if __has_feature(cxx_rvalue_references) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
#define PLF_MOVE_SEMANTICS_SUPPORT
#endif
#if __has_feature(cxx_variadic_templates) && !defined(_LIBCPP_HAS_NO_VARIADICS)
#define PLF_VARIADICS_SUPPORT
#endif
#if (__clang_major__ == 3 && __clang_minor__ >= 1) || __clang_major__ > 3
#define PLF_INITIALIZER_LIST_SUPPORT
#endif
#elif defined(__GLIBCXX__) // Using another compiler type with libstdc++ - we are assuming full c++11 compliance for compiler - which may not be true
#define PLF_DEFAULT_TEMPLATE_ARGUMENT_SUPPORT
#if __GLIBCXX__ >= 20080606
#define PLF_MOVE_SEMANTICS_SUPPORT
#define PLF_VARIADICS_SUPPORT
#endif
#if __GLIBCXX__ >= 20090421
#define PLF_INITIALIZER_LIST_SUPPORT
#endif
#if __GLIBCXX__ >= 20120322
#define PLF_ALLOCATOR_TRAITS_SUPPORT
#undef PLF_NOEXCEPT
#undef PLF_NOEXCEPT_ALLOCATOR
#define PLF_NOEXCEPT noexcept
#define PLF_NOEXCEPT_ALLOCATOR noexcept(noexcept(allocator_type()))
#endif
#if __GLIBCXX__ >= 20130322
#define PLF_ALIGNMENT_SUPPORT
#endif
#if __GLIBCXX__ >= 20150422 // libstdc++ v4.9 and below do not support std::is_trivially_copyable
#define PLF_TYPE_TRAITS_SUPPORT
#endif
#if __GLIBCXX__ >= 20160111
#define PLF_IS_ALWAYS_EQUAL_SUPPORT
#endif
#elif defined(_LIBCPP_CXX03_LANG) || defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) // Special case for checking C++11 support with libCPP
#if !defined(_LIBCPP_HAS_NO_VARIADICS)
#define PLF_VARIADICS_SUPPORT
#endif
#else // Assume type traits and initializer support for other compilers and standard library implementations
#define PLF_DEFAULT_TEMPLATE_ARGUMENT_SUPPORT
#define PLF_MOVE_SEMANTICS_SUPPORT
#define PLF_VARIADICS_SUPPORT
#define PLF_TYPE_TRAITS_SUPPORT
#define PLF_ALLOCATOR_TRAITS_SUPPORT
#define PLF_ALIGNMENT_SUPPORT
#define PLF_INITIALIZER_LIST_SUPPORT
#undef PLF_NOEXCEPT
#undef PLF_NOEXCEPT_ALLOCATOR
#define PLF_NOEXCEPT noexcept
#define PLF_NOEXCEPT_ALLOCATOR noexcept(noexcept(allocator_type()))
#define PLF_IS_ALWAYS_EQUAL_SUPPORT
#endif
#if __cplusplus >= 201703L && ((defined(__clang__) && ((__clang_major__ == 3 && __clang_minor__ == 9) || __clang_major__ > 3)) || (defined(__GNUC__) && __GNUC__ >= 7) || (!defined(__clang__) && !defined(__GNUC__))) // assume correct C++17 implementation for non-gcc/clang compilers
#undef PLF_CONSTEXPR
#define PLF_CONSTEXPR constexpr
#endif
#if __cplusplus > 201704L && ((((defined(__clang__) && !defined(__APPLE_CC__) && __clang_major__ >= 14) || (defined(__GNUC__) && (__GNUC__ > 11 || (__GNUC__ == 11 && __GNUC_MINOR__ > 0)))) && ((defined(_LIBCPP_VERSION) && _LIBCPP_VERSION >= 14) || (defined(__GLIBCXX__) && __GLIBCXX__ >= 201806L))) || (!defined(__clang__) && !defined(__GNUC__)))
#define PLF_CPP20_SUPPORT
#undef PLF_CONSTFUNC
#define PLF_CONSTFUNC constexpr
#endif
#endif
#if defined(PLF_IS_ALWAYS_EQUAL_SUPPORT) && defined(PLF_MOVE_SEMANTICS_SUPPORT) && defined(PLF_ALLOCATOR_TRAITS_SUPPORT) && (__cplusplus >= 201703L || (defined(_MSVC_LANG) && (_MSVC_LANG >= 201703L)))
#define PLF_NOEXCEPT_MOVE_ASSIGN(the_allocator) noexcept(std::allocator_traits<the_allocator>::propagate_on_container_move_assignment::value || std::allocator_traits<the_allocator>::is_always_equal::value)
#define PLF_NOEXCEPT_SWAP(the_allocator) noexcept(std::allocator_traits<the_allocator>::propagate_on_container_swap::value || std::allocator_traits<the_allocator>::is_always_equal::value)
#else
#define PLF_NOEXCEPT_MOVE_ASSIGN(the_allocator)
#define PLF_NOEXCEPT_SWAP(the_allocator)
#endif
#undef PLF_IS_ALWAYS_EQUAL_SUPPORT
#ifdef PLF_ALLOCATOR_TRAITS_SUPPORT
#ifdef PLF_VARIADICS_SUPPORT
#define PLF_CONSTRUCT(the_allocator, allocator_instance, location, ...) std::allocator_traits<the_allocator>::construct(allocator_instance, location, __VA_ARGS__)
#else
#define PLF_CONSTRUCT(the_allocator, allocator_instance, location, data) std::allocator_traits<the_allocator>::construct(allocator_instance, location, data)
#endif
#define PLF_DESTROY(the_allocator, allocator_instance, location) std::allocator_traits<the_allocator>::destroy(allocator_instance, location)
#define PLF_ALLOCATE(the_allocator, allocator_instance, size, hint) std::allocator_traits<the_allocator>::allocate(allocator_instance, size, hint)
#define PLF_DEALLOCATE(the_allocator, allocator_instance, location, size) std::allocator_traits<the_allocator>::deallocate(allocator_instance, location, size)
#else
#ifdef PLF_VARIADICS_SUPPORT
#define PLF_CONSTRUCT(the_allocator, allocator_instance, location, ...) (allocator_instance).construct(location, __VA_ARGS__)
#else
#define PLF_CONSTRUCT(the_allocator, allocator_instance, location, data) (allocator_instance).construct(location, data)
#endif
#define PLF_DESTROY(the_allocator, allocator_instance, location) (allocator_instance).destroy(location)
#define PLF_ALLOCATE(the_allocator, allocator_instance, size, hint) (allocator_instance).allocate(size, hint)
#define PLF_DEALLOCATE(the_allocator, allocator_instance, location, size) (allocator_instance).deallocate(location, size)
#endif
#ifdef PLF_VARIADICS_SUPPORT
#define PLF_CONSTRUCT_NODE(location, next, prev, element) PLF_CONSTRUCT(node_allocator_type, node_allocator_pair, location, next, prev, element)
#else
#define PLF_CONSTRUCT_NODE(location, next, prev, element) PLF_CONSTRUCT(node_allocator_type, node_allocator_pair, location, node(next, prev, element))
#endif
#include <cstring> // memmove, memcpy
#include <cassert> // assert
#include <limits> // std::numeric_limits
#include <memory> // std::uninitialized_copy, std::allocator, std::to_address
#include <iterator> // std::bidirectional_iterator_tag, iterator_traits, std::move_iterator, std::distance for range insert
#include <stdexcept> // std::length_error
#include <utility> // std::move, std::swap
#if !defined(PLF_SORT_FUNCTION) || defined(PLF_CPP20_SUPPORT)
#include <algorithm> // std::sort, lexicographical_three_way_compare (C++20)
#endif
#ifdef PLF_TYPE_TRAITS_SUPPORT
#include <type_traits> // std::is_trivially_destructible, etc
#endif
#ifdef PLF_INITIALIZER_LIST_SUPPORT
#include <initializer_list>
#endif
#ifdef PLF_CPP20_SUPPORT
#include <concepts>
#include <compare> // std::strong_ordering
#include <ranges>
namespace plf
{
// For getting std:: overload for reverse_iterator to match plf::list iterators specifically (see bottom of header):
template <class T>
concept list_iterator_concept = requires { typename T::list_iterator_tag; };
#ifndef PLF_FROM_RANGE
#define PLF_FROM_RANGE
// Until such point as standard libraries include std::ranges::from_range_t, including this so the rangesv3 constructor overloads will work unambiguously:
namespace ranges
{
struct from_range_t {};
constexpr from_range_t from_range;
}
#endif
}
#endif
namespace plf
{
#ifndef PLF_TOOLS
#define PLF_TOOLS
// std:: tool replacements for C++03/98/11 support:
template <bool condition, class T = void>
struct enable_if
{
typedef T type;
};
template <class T>
struct enable_if<false, T>
{};
template <bool flag, class is_true, class is_false> struct conditional;
template <class is_true, class is_false> struct conditional<true, is_true, is_false>
{
typedef is_true type;
};
template <class is_true, class is_false> struct conditional<false, is_true, is_false>
{
typedef is_false type;
};
template <class element_type>
struct less
{
bool operator() (const element_type &a, const element_type &b) const PLF_NOEXCEPT
{
return a < b;
}
};
template<class element_type>
struct equal_to
{
const element_type &value;
explicit equal_to(const element_type &store_value) PLF_NOEXCEPT:
value(store_value)
{}
bool operator() (const element_type &compare_value) const PLF_NOEXCEPT
{
return value == compare_value;
}
};
// To enable conversion to void * when allocator supplies non-raw pointers:
template <class source_pointer_type>
static PLF_CONSTFUNC void * void_cast(const source_pointer_type source_pointer) PLF_NOEXCEPT
{
#ifdef PLF_CPP20_SUPPORT
return static_cast<void *>(std::to_address(source_pointer));
#else
return static_cast<void *>(&*source_pointer);
#endif
}
#ifdef PLF_MOVE_SEMANTICS_SUPPORT
template <class iterator_type>
static PLF_CONSTFUNC std::move_iterator<iterator_type> make_move_iterator(iterator_type it)
{
return std::move_iterator<iterator_type>(std::move(it));
}
#endif
enum priority { performance = 1, memory_use = 4};
#endif
template <class element_type, class allocator_type = std::allocator<element_type> > class list : private allocator_type
{
public:
// Standard container typedefs:
typedef element_type value_type;
typedef unsigned short group_size_type;
#ifdef PLF_ALLOCATOR_TRAITS_SUPPORT
typedef typename std::allocator_traits<allocator_type>::size_type size_type;
typedef typename std::allocator_traits<allocator_type>::difference_type difference_type;
typedef element_type & reference;
typedef const element_type & const_reference;
typedef typename std::allocator_traits<allocator_type>::pointer pointer;
typedef typename std::allocator_traits<allocator_type>::const_pointer const_pointer;
#else
typedef typename allocator_type::size_type size_type;
typedef typename allocator_type::difference_type difference_type;
typedef typename allocator_type::reference reference;
typedef typename allocator_type::const_reference const_reference;
typedef typename allocator_type::pointer pointer;
typedef typename allocator_type::const_pointer const_pointer;
#endif
// Iterator declarations:
template <bool is_const> class list_iterator;
typedef list_iterator<false> iterator;
typedef list_iterator<true> const_iterator;
friend class list_iterator<false>; // Using 'iterator' typedef name here is illegal under C++03
friend class list_iterator<true>;
template <bool is_const> class list_reverse_iterator;
typedef list_reverse_iterator<false> reverse_iterator;
typedef list_reverse_iterator<true> const_reverse_iterator;
friend class list_reverse_iterator<false>;
friend class list_reverse_iterator<true>;
private:
struct group; // forward declarations for typedefs below
struct node;
#ifdef PLF_ALLOCATOR_TRAITS_SUPPORT // >= C++11
typedef typename std::allocator_traits<allocator_type>::template rebind_alloc<group> group_allocator_type;
typedef typename std::allocator_traits<allocator_type>::template rebind_alloc<node> node_allocator_type;
typedef typename std::allocator_traits<group_allocator_type>::pointer group_pointer_type;
typedef typename std::allocator_traits<node_allocator_type>::pointer node_pointer_type;
typedef typename std::allocator_traits<allocator_type>::template rebind_alloc<node_pointer_type> node_pointer_allocator_type;
#else
typedef typename allocator_type::template rebind<group>::other group_allocator_type;
typedef typename allocator_type::template rebind<node>::other node_allocator_type;
typedef typename group_allocator_type::pointer group_pointer_type;
typedef typename node_allocator_type::pointer node_pointer_type;
typedef typename allocator_type::template rebind<node_pointer_type>::other node_pointer_allocator_type;
#endif
struct node_base
{
node_pointer_type next, previous;
node_base() PLF_NOEXCEPT
{}
node_base(const node_pointer_type &n, const node_pointer_type &p) PLF_NOEXCEPT:
next(n),
previous(p)
{}
#ifdef PLF_MOVE_SEMANTICS_SUPPORT
node_base(node_pointer_type &&n, node_pointer_type &&p) PLF_NOEXCEPT:
next(std::move(n)),
previous(std::move(p))
{}
#endif
};
struct node : public node_base
{
element_type element;
node(const node_pointer_type next, const node_pointer_type previous, const element_type &source):
node_base(next, previous),
element(source)
{}
#ifdef PLF_MOVE_SEMANTICS_SUPPORT
node(node_pointer_type &&next, node_pointer_type &&previous, element_type &&source) PLF_NOEXCEPT:
node_base(std::move(next), std::move(previous)),
element(std::move(source))
{}
#endif
#ifdef PLF_VARIADICS_SUPPORT
template<typename... arguments>
node(node_pointer_type const next, node_pointer_type const previous, arguments&&... parameters):
node_base(next, previous),
element(std::forward<arguments>(parameters) ...)
{}
#endif
};
struct group : public node_allocator_type // Node memory block + metadata
{
node_pointer_type nodes;
node_pointer_type free_list_head;
node_pointer_type beyond_end;
group_size_type number_of_elements;
group() PLF_NOEXCEPT:
nodes(NULL),
free_list_head(NULL),
beyond_end(NULL),
number_of_elements(0)
{}
#if defined(PLF_VARIADICS_SUPPORT) || defined(PLF_MOVE_SEMANTICS_SUPPORT)
group(const group_size_type group_size, node_pointer_type const previous = NULL):
nodes(PLF_ALLOCATE(node_allocator_type, *this, group_size, previous)),
free_list_head(NULL),
beyond_end(nodes + group_size),
number_of_elements(0)
{}
#else
// This is a hack around the fact that allocator_type::construct only supports copy construction in C++03 and copy elision does not occur on the vast majority of compilers in this circumstance. And to avoid running out of memory (and performance loss) from allocating the same block twice, we're allocating in this constructor and moving data in the copy constructor.
group(const group_size_type group_size, node_pointer_type const previous = NULL) PLF_NOEXCEPT:
nodes(NULL),
free_list_head(previous),
beyond_end(NULL),
number_of_elements(group_size)
{}
// Not a real copy constructor ie. actually a move constructor. Only used for allocator.construct in C++03 for reasons stated above:
group(const group &source):
node_allocator_type(source),
nodes(PLF_ALLOCATE(node_allocator_type, *this, source.number_of_elements, source.free_list_head)),
free_list_head(NULL),
beyond_end(nodes + source.number_of_elements),
number_of_elements(0)
{}
#endif
group & operator = (const group &source) PLF_NOEXCEPT // Actually a move operator, used by c++03 in group_vector's remove, expand_capacity and append functions
{
nodes = source.nodes;
free_list_head = source.free_list_head;
beyond_end = source.beyond_end;
number_of_elements = source.number_of_elements;
return *this;
}
#ifdef PLF_MOVE_SEMANTICS_SUPPORT
group(group &&source) PLF_NOEXCEPT:
node_allocator_type(source),
nodes(std::move(source.nodes)),
free_list_head(std::move(source.free_list_head)),
beyond_end(std::move(source.beyond_end)),
number_of_elements(source.number_of_elements)
{
source.nodes = NULL;
source.beyond_end = NULL;
}
group & operator = (group &&source) PLF_NOEXCEPT
{
nodes = std::move(source.nodes);
free_list_head = std::move(source.free_list_head);
beyond_end = std::move(source.beyond_end);
number_of_elements = std::move(source.number_of_elements);
source.nodes = NULL;
source.beyond_end = NULL;
return *this;
}
#endif
~group() PLF_NOEXCEPT
{
PLF_DEALLOCATE(node_allocator_type, *this, nodes, static_cast<size_type>(beyond_end - nodes));
}
};
class group_vector : private allocator_type // Simple vector of groups + associated functions
{
public:
group_pointer_type last_endpoint_group, block_pointer, last_searched_group; // last_endpoint_group is the last -active- group in the block. Other -inactive- (previously used, now empty of elements) groups may be stored after this group for future usage (to reduce deallocation/reallocation of nodes). block_pointer + size - 1 == the last group in the block, regardless of whether or not the group is active.
size_type size;
struct ebco_pair2 : node_pointer_allocator_type // empty-base-class optimisation
{
size_type capacity; // Total element capacity of all initialized groups
ebco_pair2(const size_type number_of_elements, const allocator_type &alloc) PLF_NOEXCEPT:
node_pointer_allocator_type(alloc),
capacity(number_of_elements)
{};
} node_pointer_allocator_pair;
struct ebco_pair : group_allocator_type
{
size_type capacity; // Total number of groups
ebco_pair(const size_type number_of_groups, const allocator_type &alloc) PLF_NOEXCEPT:
group_allocator_type(alloc),
capacity(number_of_groups)
{};
} group_allocator_pair;
group_vector(const allocator_type &alloc) PLF_NOEXCEPT:
allocator_type(alloc),
last_endpoint_group(NULL),
block_pointer(NULL),
last_searched_group(NULL),
size(0),
node_pointer_allocator_pair(0, alloc),
group_allocator_pair(0, alloc)
{}
void blank() PLF_NOEXCEPT
{
#ifdef PLF_TYPE_TRAITS_SUPPORT
if PLF_CONSTEXPR (std::is_standard_layout<group_vector>::value && std::allocator_traits<allocator_type>::is_always_equal::value && std::is_trivial<group_pointer_type>::value)
{
std::memset(static_cast<void *>(this), 0, sizeof(group_vector));
}
else
#endif
{
last_endpoint_group = NULL;
block_pointer = NULL;
last_searched_group = NULL;
size = 0;
node_pointer_allocator_pair.capacity = 0;
group_allocator_pair.capacity = 0;
}
}
#ifdef PLF_MOVE_SEMANTICS_SUPPORT
group_vector(group_vector &&source) PLF_NOEXCEPT:
allocator_type(source),
last_endpoint_group(std::move(source.last_endpoint_group)),
block_pointer(std::move(source.block_pointer)),
last_searched_group(std::move(source.last_searched_group)),
size(source.size),
node_pointer_allocator_pair(source.node_pointer_allocator_pair.capacity, source),
group_allocator_pair(source.group_allocator_pair.capacity, source)
{
source.blank();
}
group_vector & operator = (group_vector &&source) PLF_NOEXCEPT
{
#if defined(PLF_TYPE_TRAITS_SUPPORT) && defined(PLF_ALLOCATOR_TRAITS_SUPPORT)
if PLF_CONSTEXPR ((std::is_trivially_copyable<allocator_type>::value || std::allocator_traits<allocator_type>::is_always_equal::value) && std::is_trivial<group_pointer_type>::value)
{
std::memcpy(static_cast<void *>(this), &source, sizeof(group_vector));
}
else
#endif
{
last_endpoint_group = std::move(source.last_endpoint_group);
block_pointer = std::move(source.block_pointer);
last_searched_group = std::move(source.last_searched_group);
size = source.size;
node_pointer_allocator_pair.capacity = source.node_pointer_allocator_pair.capacity;
group_allocator_pair.capacity = source.group_allocator_pair.capacity;
#ifdef PLF_ALLOCATOR_TRAITS_SUPPORT
if PLF_CONSTEXPR(std::allocator_traits<allocator_type>::propagate_on_container_move_assignment::value)
#endif
{
static_cast<allocator_type &>(*this) = std::move(static_cast<allocator_type &>(source));
// Reconstruct rebinds:
static_cast<node_pointer_allocator_type &>(node_pointer_allocator_pair) = node_allocator_type(*this);
static_cast<group_allocator_type &>(group_allocator_pair) = group_allocator_type(*this);
}
}
source.blank();
return *this;
}
#endif
void destroy_all_data(const node_pointer_type last_endpoint_node) PLF_NOEXCEPT
{
if (block_pointer == NULL) return;
#ifdef PLF_TYPE_TRAITS_SUPPORT
if PLF_CONSTEXPR (!std::is_trivially_destructible<element_type>::value || !std::is_trivially_destructible<node_pointer_type>::value)
#endif
{
if (last_endpoint_node != NULL) clear(last_endpoint_node);
}
const group_pointer_type end_group = block_pointer + size;
for (group_pointer_type current_group = block_pointer; current_group != end_group; ++current_group)
{
PLF_DESTROY(group_allocator_type, group_allocator_pair, current_group);
}
PLF_DEALLOCATE(group_allocator_type, group_allocator_pair, block_pointer, group_allocator_pair.capacity);
blank();
}
void clear(const node_pointer_type last_endpoint_node) PLF_NOEXCEPT
{
for (group_pointer_type current_group = block_pointer; current_group != last_endpoint_group; ++current_group)
{
#ifdef PLF_TYPE_TRAITS_SUPPORT
if PLF_CONSTEXPR (!std::is_trivially_destructible<element_type>::value || !std::is_trivially_destructible<node_pointer_type>::value)
#endif
{
const node_pointer_type end = current_group->beyond_end;
if ((end - current_group->nodes) != current_group->number_of_elements) // If there are erased nodes present in the group
{
for (node_pointer_type current_node = current_group->nodes; current_node != end; ++current_node)
{
#ifdef PLF_TYPE_TRAITS_SUPPORT
if PLF_CONSTEXPR (!std::is_trivially_destructible<element_type>::value)
#endif
{
if (current_node->next != NULL) // ie. is not part of free list
{
PLF_DESTROY(allocator_type, *this, &(current_node->element));
}
}
#ifdef PLF_TYPE_TRAITS_SUPPORT
if PLF_CONSTEXPR (!std::is_trivially_destructible<node_pointer_type>::value)
#endif
{
PLF_DESTROY(node_pointer_allocator_type, node_pointer_allocator_pair, &(current_node->next));
PLF_DESTROY(node_pointer_allocator_type, node_pointer_allocator_pair, &(current_node->previous));
}
}
}
else
{
for (node_pointer_type current_node = current_group->nodes; current_node != end; ++current_node)
{
#ifdef PLF_TYPE_TRAITS_SUPPORT
if PLF_CONSTEXPR (!std::is_trivially_destructible<element_type>::value)
#endif
{
PLF_DESTROY(allocator_type, *this, &(current_node->element));
}
#ifdef PLF_TYPE_TRAITS_SUPPORT
if PLF_CONSTEXPR (!std::is_trivially_destructible<node_pointer_type>::value)
#endif
{
PLF_DESTROY(node_pointer_allocator_type, node_pointer_allocator_pair, &(current_node->next));
PLF_DESTROY(node_pointer_allocator_type, node_pointer_allocator_pair, &(current_node->previous));
}
}
}
}
current_group->free_list_head = NULL;
current_group->number_of_elements = 0;
}
#ifdef PLF_TYPE_TRAITS_SUPPORT
if PLF_CONSTEXPR (!std::is_trivially_destructible<element_type>::value || !std::is_trivially_destructible<node_pointer_type>::value)
#endif
{
if ((last_endpoint_node - last_endpoint_group->nodes) != last_endpoint_group->number_of_elements) // If there are erased nodes present in the group
{
for (node_pointer_type current_node = last_endpoint_group->nodes; current_node != last_endpoint_node; ++current_node)
{
#ifdef PLF_TYPE_TRAITS_SUPPORT
if PLF_CONSTEXPR (!std::is_trivially_destructible<element_type>::value)
#endif
{
if (current_node->next != NULL) // is not part of free list ie. element has not already had it's destructor called
{
PLF_DESTROY(allocator_type, *this, &(current_node->element));
}
}
#ifdef PLF_TYPE_TRAITS_SUPPORT
if PLF_CONSTEXPR (!std::is_trivially_destructible<node_pointer_type>::value)
#endif
{
PLF_DESTROY(node_pointer_allocator_type, node_pointer_allocator_pair, &(current_node->next));
PLF_DESTROY(node_pointer_allocator_type, node_pointer_allocator_pair, &(current_node->previous));
}
}
}
else
{
for (node_pointer_type current_node = last_endpoint_group->nodes; current_node != last_endpoint_node; ++current_node)
{
#ifdef PLF_TYPE_TRAITS_SUPPORT
if PLF_CONSTEXPR (!std::is_trivially_destructible<element_type>::value)
#endif
{
PLF_DESTROY(allocator_type, *this, &(current_node->element));
}
#ifdef PLF_TYPE_TRAITS_SUPPORT
if PLF_CONSTEXPR (!std::is_trivially_destructible<node_pointer_type>::value)
#endif
{
PLF_DESTROY(node_pointer_allocator_type, node_pointer_allocator_pair, &(current_node->next));
PLF_DESTROY(node_pointer_allocator_type, node_pointer_allocator_pair, &(current_node->previous));
}
}
}
}
last_endpoint_group->free_list_head = NULL;
last_endpoint_group->number_of_elements = 0;
last_searched_group = last_endpoint_group = block_pointer;
}
void expand_capacity(const size_type new_capacity) // used by add_new and append
{
group_pointer_type const old_block = block_pointer;
block_pointer = PLF_ALLOCATE(group_allocator_type, group_allocator_pair, new_capacity, 0);
#ifdef PLF_TYPE_TRAITS_SUPPORT
if PLF_CONSTEXPR (std::is_trivially_copyable<node_pointer_type>::value && std::is_trivially_destructible<node_pointer_type>::value)
{
std::memcpy(plf::void_cast(block_pointer), plf::void_cast(old_block), sizeof(group) * size);
}
#ifdef PLF_MOVE_SEMANTICS_SUPPORT
else if PLF_CONSTEXPR (std::is_move_constructible<node_pointer_type>::value)
{
std::uninitialized_copy(plf::make_move_iterator(old_block), plf::make_move_iterator(old_block + size), block_pointer);
}
#endif
else
#endif
{
// If allocator supplies non-trivial pointers it becomes necessary to destroy the group. uninitialized_copy will not work in this context as the copy constructor for "group" is overriden in C++03/98. The = operator for "group" has been overriden to make the following work:
const group_pointer_type beyond_end = old_block + size;
group_pointer_type current_new_group = block_pointer;
for (group_pointer_type current_group = old_block; current_group != beyond_end; ++current_group)
{
*current_new_group++ = *current_group;
current_group->nodes = NULL;
current_group->beyond_end = NULL;
PLF_DESTROY(group_allocator_type, group_allocator_pair, current_group);
}
}
last_searched_group = block_pointer + (last_searched_group - old_block); // correct pointer post-reallocation
PLF_DEALLOCATE(group_allocator_type, group_allocator_pair, old_block, group_allocator_pair.capacity);
group_allocator_pair.capacity = new_capacity;
}
void add_new(const group_size_type group_size)
{
if (group_allocator_pair.capacity == size) expand_capacity(group_allocator_pair.capacity * 2);
last_endpoint_group = block_pointer + size - 1;
#ifdef PLF_VARIADICS_SUPPORT
PLF_CONSTRUCT(group_allocator_type, group_allocator_pair, last_endpoint_group + 1, group_size, last_endpoint_group->nodes);
#else
PLF_CONSTRUCT(group_allocator_type, group_allocator_pair, last_endpoint_group + 1, group(group_size, last_endpoint_group->nodes));
#endif
++last_endpoint_group; // Doing this here instead of pre-construct to avoid need for a try-catch block
node_pointer_allocator_pair.capacity += group_size;
++size;
}
void initialize(const group_size_type first_group_capacity) // For adding first group *only* when group vector is completely empty and block_pointer is NULL
{
last_endpoint_group = block_pointer = last_searched_group = PLF_ALLOCATE(group_allocator_type, group_allocator_pair, 1, 0);
group_allocator_pair.capacity = 1;
#ifdef PLF_VARIADICS_SUPPORT
PLF_CONSTRUCT(group_allocator_type, group_allocator_pair, last_endpoint_group, first_group_capacity);
#else
PLF_CONSTRUCT(group_allocator_type, group_allocator_pair, last_endpoint_group, group(first_group_capacity));
#endif
size = 1; // Doing these here instead of pre-construct to avoid need for a try-catch block
node_pointer_allocator_pair.capacity = first_group_capacity;
}
#ifdef PLF_CPP20_SUPPORT
#define PLF_TO_ADDRESS(pointer) std::to_address(pointer)
#else
#define PLF_TO_ADDRESS(pointer) &*(pointer)
#endif
void remove(group_pointer_type const group_to_erase) PLF_NOEXCEPT
{
if (last_searched_group >= group_to_erase && last_searched_group != block_pointer) --last_searched_group;
node_pointer_allocator_pair.capacity -= static_cast<size_type>(group_to_erase->beyond_end - group_to_erase->nodes);
PLF_DESTROY(group_allocator_type, group_allocator_pair, group_to_erase);
#ifdef PLF_TYPE_TRAITS_SUPPORT
if PLF_CONSTEXPR (std::is_trivially_copyable<node_pointer_type>::value && std::is_trivially_destructible<node_pointer_type>::value)
{
std::memmove(plf::void_cast(group_to_erase), plf::void_cast(group_to_erase + 1), sizeof(group) * (--size - static_cast<size_type>(PLF_TO_ADDRESS(group_to_erase) - PLF_TO_ADDRESS(block_pointer))));
}
#ifdef PLF_MOVE_SEMANTICS_SUPPORT
else if PLF_CONSTEXPR (std::is_move_constructible<node_pointer_type>::value)
{
std::move(group_to_erase + 1, block_pointer + size--, group_to_erase);
}
#endif
else
#endif
{
group_pointer_type back = block_pointer + size--;
std::copy(group_to_erase + 1, back--, group_to_erase);
back->nodes = NULL;
back->beyond_end = NULL;
PLF_DESTROY(group_allocator_type, group_allocator_pair, back);
}
}
void move_to_back(group_pointer_type const group_to_erase)
{
if (last_searched_group >= group_to_erase && last_searched_group != block_pointer) --last_searched_group;
group *temp_group = PLF_ALLOCATE(group_allocator_type, group_allocator_pair, 1, NULL);
#ifdef PLF_TYPE_TRAITS_SUPPORT
if PLF_CONSTEXPR (std::is_trivially_copyable<node_pointer_type>::value && std::is_trivially_destructible<node_pointer_type>::value)
{
std::memcpy(plf::void_cast(temp_group), plf::void_cast(group_to_erase), sizeof(group));
std::memmove(plf::void_cast(group_to_erase), plf::void_cast(group_to_erase + 1), sizeof(group) * ((size - 1) - static_cast<size_type>(PLF_TO_ADDRESS(group_to_erase) - PLF_TO_ADDRESS(block_pointer))));
std::memcpy(plf::void_cast(block_pointer + size - 1), plf::void_cast(temp_group), sizeof(group));
}
#ifdef PLF_MOVE_SEMANTICS_SUPPORT
else if PLF_CONSTEXPR (std::is_move_constructible<node_pointer_type>::value)
{
PLF_CONSTRUCT(group_allocator_type, group_allocator_pair, temp_group, std::move(*group_to_erase));
std::move(group_to_erase + 1, block_pointer + size, group_to_erase);
*(block_pointer + size - 1) = std::move(*temp_group);
if PLF_CONSTEXPR (!std::is_trivially_destructible<node_pointer_type>::value)
{
PLF_DESTROY(group_allocator_type, group_allocator_pair, temp_group);
}
}
#endif
else
#endif
{
PLF_CONSTRUCT(group_allocator_type, group_allocator_pair, temp_group, group());
*temp_group = *group_to_erase;
std::copy(group_to_erase + 1, block_pointer + size, group_to_erase);
*(block_pointer + --size) = *temp_group;
temp_group->nodes = NULL;
PLF_DESTROY(group_allocator_type, group_allocator_pair, temp_group);
}
PLF_DEALLOCATE(group_allocator_type, group_allocator_pair, temp_group, 1);
}
group_pointer_type get_nearest_freelist_group(const node_pointer_type location_node) PLF_NOEXCEPT // In working implementation this cannot throw
{
const group_pointer_type beyond_end_group = last_endpoint_group + 1;
group_pointer_type left = last_searched_group - 1, right = last_searched_group + 1, freelist_group = NULL;
bool right_not_beyond_back = right < beyond_end_group;
bool left_not_beyond_front = left >= block_pointer;
if (location_node >= last_searched_group->nodes && location_node < last_searched_group->beyond_end) // ie. location is within last_search_group
{
if (last_searched_group->free_list_head != NULL) return last_searched_group; // if last_searched_group has previously-erased nodes
// Else: search outwards using loop below this if/else block
}
else // search for the node group which location_node is located within, using last_searched_group as a starting point and searching left and right. Try and find the closest node group with reusable erased-element locations along the way:
{
group_pointer_type closest_freelist_left = (last_searched_group->free_list_head == NULL) ? NULL : last_searched_group;
group_pointer_type closest_freelist_right = closest_freelist_left;
while (true)
{
if (right_not_beyond_back)
{
if (location_node < right->beyond_end && location_node >= right->nodes) // location_node's group is found
{
last_searched_group = right;
if (right->free_list_head != NULL) return right; // group has erased nodes, reuse them:
difference_type left_distance;
if (closest_freelist_right != NULL)
{