-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplf_stack.hpp
1584 lines (1293 loc) · 53.5 KB
/
plf_stack.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
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) 2020, 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_STACK_H
#define PLF_STACK_H
// Compiler-specific defines used by stack:
#if defined(_MSC_VER)
#define PLF_STACK_FORCE_INLINE __forceinline
#if _MSC_VER >= 1900
#define PLF_STACK_ALIGNMENT_SUPPORT
#define PLF_STACK_NOEXCEPT noexcept
#define PLF_STACK_NOEXCEPT_SWAP(the_allocator) noexcept(std::allocator_traits<the_allocator>::propagate_on_container_swap::value || std::allocator_traits<the_allocator>::is_always_equal::value)
#define PLF_STACK_NOEXCEPT_MOVE_ASSIGNMENT(the_allocator) noexcept(std::allocator_traits<the_allocator>::propagate_on_container_move_assignment::value || std::allocator_traits<the_allocator>::is_always_equal::value)
#else
#define PLF_STACK_NOEXCEPT throw()
#define PLF_STACK_NOEXCEPT_SWAP(the_allocator)
#define PLF_STACK_NOEXCEPT_MOVE_ASSIGNMENT(the_allocator) throw()
#endif
#if _MSC_VER >= 1600
#define PLF_STACK_MOVE_SEMANTICS_SUPPORT
#endif
#if _MSC_VER >= 1700
#define PLF_STACK_TYPE_TRAITS_SUPPORT
#define PLF_STACK_ALLOCATOR_TRAITS_SUPPORT
#endif
#if _MSC_VER >= 1800
#define PLF_STACK_VARIADICS_SUPPORT // Variadics, in this context, means both variadic templates and variadic macros are supported
#define PLF_STACK_INITIALIZER_LIST_SUPPORT
#endif
#if defined(_MSVC_LANG) && (_MSVC_LANG >= 201703L)
#define PLF_STACK_CONSTEXPR constexpr
#define PLF_STACK_CONSTEXPR_SUPPORT
#else
#define PLF_STACK_CONSTEXPR
#endif
#if defined(_MSVC_LANG) && (_MSVC_LANG > 201703L)
#define PLF_STACK_CPP20_SUPPORT
#endif
#elif defined(__cplusplus) && __cplusplus >= 201103L // C++11 support, at least
#define PLF_STACK_FORCE_INLINE // note: GCC creates faster code without forcing inline
#define PLF_STACK_MOVE_SEMANTICS_SUPPORT
#if defined(__GNUC__) && defined(__GNUC_MINOR__) && !defined(__clang__) // If compiler is GCC/G++
#if (__GNUC__ == 4 && __GNUC_MINOR__ >= 3) || __GNUC__ > 4 // 4.2 and below do not support variadic templates
#define PLF_STACK_VARIADICS_SUPPORT
#endif
#if (__GNUC__ == 4 && __GNUC_MINOR__ >= 4) || __GNUC__ > 4 // 4.3 and below do not support initializer lists
#define PLF_STACK_INITIALIZER_LIST_SUPPORT
#endif
#if (__GNUC__ == 4 && __GNUC_MINOR__ < 6) || __GNUC__ < 4
#define PLF_STACK_NOEXCEPT throw()
#define PLF_STACK_NOEXCEPT_MOVE_ASSIGNMENT(the_allocator)
#define PLF_STACK_NOEXCEPT_SWAP(the_allocator)
#elif __GNUC__ < 6
#define PLF_STACK_NOEXCEPT noexcept
#define PLF_STACK_NOEXCEPT_MOVE_ASSIGNMENT(the_allocator) noexcept
#define PLF_STACK_NOEXCEPT_SWAP(the_allocator) noexcept
#else // C++17 support
#define PLF_STACK_NOEXCEPT noexcept
#define PLF_STACK_NOEXCEPT_MOVE_ASSIGNMENT(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_STACK_NOEXCEPT_SWAP(the_allocator) noexcept(std::allocator_traits<the_allocator>::propagate_on_container_swap::value || std::allocator_traits<the_allocator>::is_always_equal::value)
#endif
#if (__GNUC__ == 4 && __GNUC_MINOR__ >= 7) || __GNUC__ > 4
#define PLF_STACK_ALLOCATOR_TRAITS_SUPPORT
#endif
#if (__GNUC__ == 4 && __GNUC_MINOR__ >= 8) || __GNUC__ > 4
#define PLF_STACK_ALIGNMENT_SUPPORT
#endif
#if __GNUC__ >= 5 // GCC v4.9 and below do not support std::is_trivially_copyable
#define PLF_STACK_TYPE_TRAITS_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
#if __GLIBCXX__ >= 20080606 // libstdc++ 4.2 and below do not support variadic templates
#define PLF_STACK_VARIADICS_SUPPORT
#endif
#if __GLIBCXX__ >= 20090421 // libstdc++ 4.3 and below do not support initializer lists
#define PLF_STACK_INITIALIZER_LIST_SUPPORT
#endif
#if __GLIBCXX__ >= 20160111
#define PLF_STACK_ALLOCATOR_TRAITS_SUPPORT
#define PLF_STACK_NOEXCEPT noexcept
#define PLF_STACK_NOEXCEPT_MOVE_ASSIGNMENT(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_STACK_NOEXCEPT_SWAP(the_allocator) noexcept(std::allocator_traits<the_allocator>::propagate_on_container_swap::value || std::allocator_traits<the_allocator>::is_always_equal::value)
#elif __GLIBCXX__ >= 20120322
#define PLF_STACK_ALLOCATOR_TRAITS_SUPPORT
#define PLF_STACK_NOEXCEPT noexcept
#define PLF_STACK_NOEXCEPT_MOVE_ASSIGNMENT(the_allocator) noexcept
#define PLF_STACK_NOEXCEPT_SWAP(the_allocator) noexcept
#else
#define PLF_STACK_NOEXCEPT throw()
#define PLF_STACK_NOEXCEPT_MOVE_ASSIGNMENT(the_allocator)
#define PLF_STACK_NOEXCEPT_SWAP(the_allocator)
#endif
#if __GLIBCXX__ >= 20130322
#define PLF_STACK_ALIGNMENT_SUPPORT
#endif
#if __GLIBCXX__ >= 20150422 // libstdc++ v4.9 and below do not support std::is_trivially_copyable
#define PLF_STACK_TYPE_TRAITS_SUPPORT
#endif
#elif (defined(_LIBCPP_CXX03_LANG) || defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) || defined(_LIBCPP_HAS_NO_VARIADICS)) // Special case for checking C++11 support with libCPP
#define PLF_STACK_NOEXCEPT throw()
#define PLF_STACK_NOEXCEPT_MOVE_ASSIGNMENT(the_allocator)
#define PLF_STACK_NOEXCEPT_SWAP(the_allocator)
#else // Assume type traits and initializer support for other compilers and standard libraries
#define PLF_STACK_VARIADICS_SUPPORT
#define PLF_STACK_TYPE_TRAITS_SUPPORT
#define PLF_STACK_MOVE_SEMANTICS_SUPPORT
#define PLF_STACK_ALLOCATOR_TRAITS_SUPPORT
#define PLF_STACK_ALIGNMENT_SUPPORT
#define PLF_STACK_INITIALIZER_LIST_SUPPORT
#define PLF_STACK_NOEXCEPT noexcept
#define PLF_STACK_NOEXCEPT_MOVE_ASSIGNMENT(the_allocator) noexcept(std::allocator_traits<the_allocator>::is_always_equal::value)
#define PLF_STACK_NOEXCEPT_SWAP(the_allocator) noexcept
#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-GNU/cland compilers
#define PLF_STACK_CONSTEXPR constexpr
#define PLF_STACK_CONSTEXPR_SUPPORT
#else
#define PLF_STACK_CONSTEXPR
#endif
#if __cplusplus > 201703L && ((defined(__clang__) && (__clang_major__ >= 10)) || (defined(__GNUC__) && __GNUC__ >= 10) || (!defined(__clang__) && !defined(__GNUC__))) // assume correct C++20 implementation for other compilers
#define PLF_STACK_CPP20_SUPPORT
#endif
#else
#define PLF_STACK_FORCE_INLINE
#define PLF_STACK_NOEXCEPT throw()
#define PLF_STACK_NOEXCEPT_SWAP(the_allocator)
#define PLF_STACK_NOEXCEPT_MOVE_ASSIGNMENT(the_allocator)
#define PLF_STACK_CONSTEXPR
#endif
#ifdef PLF_STACK_ALLOCATOR_TRAITS_SUPPORT
#ifdef PLF_STACK_VARIADICS_SUPPORT
#define PLF_STACK_CONSTRUCT(the_allocator, allocator_instance, location, ...) std::allocator_traits<the_allocator>::construct(allocator_instance, location, __VA_ARGS__)
#else
#define PLF_STACK_CONSTRUCT(the_allocator, allocator_instance, location, data) std::allocator_traits<the_allocator>::construct(allocator_instance, location, data)
#endif
#define PLF_STACK_DESTROY(the_allocator, allocator_instance, location) std::allocator_traits<the_allocator>::destroy(allocator_instance, location)
#define PLF_STACK_ALLOCATE(the_allocator, allocator_instance, size, hint) std::allocator_traits<the_allocator>::allocate(allocator_instance, size, hint)
#define PLF_STACK_ALLOCATE_INITIALIZATION(the_allocator, size, hint) std::allocator_traits<the_allocator>::allocate(*this, size, hint)
#define PLF_STACK_DEALLOCATE(the_allocator, allocator_instance, location, size) std::allocator_traits<the_allocator>::deallocate(allocator_instance, location, size)
#else
#ifdef PLF_STACK_VARIADICS_SUPPORT
#define PLF_STACK_CONSTRUCT(the_allocator, allocator_instance, location, ...) allocator_instance.construct(location, __VA_ARGS__)
#else
#define PLF_STACK_CONSTRUCT(the_allocator, allocator_instance, location, data) allocator_instance.construct(location, data)
#endif
#define PLF_STACK_DESTROY(the_allocator, allocator_instance, location) allocator_instance.destroy(location)
#define PLF_STACK_ALLOCATE(the_allocator, allocator_instance, size, hint) allocator_instance.allocate(size, hint)
#define PLF_STACK_ALLOCATE_INITIALIZATION(the_allocator, size, hint) the_allocator::allocate(size, hint)
#define PLF_STACK_DEALLOCATE(the_allocator, allocator_instance, location, size) allocator_instance.deallocate(location, size)
#endif
#include <cstring> // memset, memcpy
#include <cassert> // assert
#include <limits> // std::numeric_limits
#include <memory> // std::uninitialized_copy, std::allocator
#ifdef PLF_STACK_MOVE_SEMANTICS_SUPPORT
#include <utility> // std::move
#endif
#ifdef PLF_STACK_TYPE_TRAITS_SUPPORT
#include <cstddef> // offsetof, used in blank()
#include <type_traits> // std::is_trivially_destructible
#endif
namespace plf
{
template <class element_type, class element_allocator_type = std::allocator<element_type> > class stack : private element_allocator_type // Empty base class optimisation - inheriting allocator functions
{
public:
// Standard container typedefs:
typedef element_type value_type;
typedef element_allocator_type allocator_type;
#ifdef PLF_STACK_ALLOCATOR_TRAITS_SUPPORT
typedef typename std::allocator_traits<element_allocator_type>::size_type size_type;
typedef element_type & reference;
typedef const element_type & const_reference;
typedef typename std::allocator_traits<element_allocator_type>::pointer pointer;
typedef typename std::allocator_traits<element_allocator_type>::const_pointer const_pointer;
#else
typedef typename element_allocator_type::size_type size_type;
typedef typename element_allocator_type::reference reference;
typedef typename element_allocator_type::const_reference const_reference;
typedef typename element_allocator_type::pointer pointer;
typedef typename element_allocator_type::const_pointer const_pointer;
#endif
private:
struct group; // Forward declaration for typedefs below
#ifdef PLF_STACK_ALLOCATOR_TRAITS_SUPPORT
typedef typename std::allocator_traits<element_allocator_type>::template rebind_alloc<group> group_allocator_type;
typedef typename std::allocator_traits<group_allocator_type>::pointer group_pointer_type;
typedef typename std::allocator_traits<element_allocator_type>::pointer element_pointer_type;
#else
typedef typename element_allocator_type::template rebind<group>::other group_allocator_type;
typedef typename group_allocator_type::pointer group_pointer_type;
typedef typename element_allocator_type::pointer element_pointer_type;
#endif
struct group : private element_allocator_type
{
const element_pointer_type elements;
group_pointer_type next_group, previous_group;
const element_pointer_type end; // End is the actual end element of the group, not one-past the end element as it is in list
#ifdef PLF_STACK_VARIADICS_SUPPORT
group(const size_type elements_per_group, group_pointer_type const previous = NULL) :
elements(PLF_STACK_ALLOCATE_INITIALIZATION(element_allocator_type, elements_per_group, (previous == NULL) ? 0 : previous->elements)),
next_group(NULL),
previous_group(previous),
end(elements + elements_per_group - 1)
{}
#else
// This is a hack around the fact that element_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 size_type elements_per_group, group_pointer_type const previous = NULL) PLF_STACK_NOEXCEPT:
elements(NULL),
next_group(reinterpret_cast<group_pointer_type>(elements_per_group)),
previous_group(previous),
end(NULL)
{}
// 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) :
element_allocator_type(source),
elements(PLF_STACK_ALLOCATE_INITIALIZATION(element_allocator_type, reinterpret_cast<size_type>(source.next_group), (source.previous_group == NULL) ? 0 : source.previous_group->elements)),
next_group(NULL),
previous_group(source.previous_group),
end(elements + reinterpret_cast<size_type>(source.next_group) - 1)
{}
#endif
~group() PLF_STACK_NOEXCEPT
{
// Null check not necessary (for empty group and copied group as above) as delete will ignore.
PLF_STACK_DEALLOCATE(element_allocator_type, (*this), elements, static_cast<size_type>((end - elements) + 1)); // Size is calculated from end and elements
}
};
group_pointer_type current_group, first_group;
element_pointer_type top_element, start_element, end_element;
size_type total_number_of_elements, min_elements_per_group;
struct ebco_pair : group_allocator_type // Packaging the group allocator with the least-used member variable, for empty-base-class optimization
{
size_type max_elements_per_group;
explicit ebco_pair(const size_type max_elements) PLF_STACK_NOEXCEPT: max_elements_per_group(max_elements) {};
} group_allocator_pair;
public:
// Default constructor:
stack() PLF_STACK_NOEXCEPT :
element_allocator_type(element_allocator_type()),
current_group(NULL),
first_group(NULL),
top_element(NULL),
start_element(NULL),
end_element(NULL),
total_number_of_elements(0),
min_elements_per_group((sizeof(element_type) * 8 > (sizeof(*this) + sizeof(group)) * 2) ? 8 : (((sizeof(*this) + sizeof(group)) * 2) / sizeof(element_type)) + 1),
group_allocator_pair(std::numeric_limits<size_type>::max() / 2)
{
assert(min_elements_per_group > 2);
assert(min_elements_per_group <= group_allocator_pair.max_elements_per_group);
}
// Allocator-extended constructor:
explicit stack(const element_allocator_type &alloc) :
element_allocator_type(alloc),
current_group(NULL),
first_group(NULL),
top_element(NULL),
start_element(NULL),
end_element(NULL),
total_number_of_elements(0),
min_elements_per_group((sizeof(element_type) * 8 > (sizeof(*this) + sizeof(group)) * 2) ? 8 : (((sizeof(*this) + sizeof(group)) * 2) / sizeof(element_type)) + 1),
group_allocator_pair(std::numeric_limits<size_type>::max() / 2)
{
assert(min_elements_per_group > 2);
assert(min_elements_per_group <= group_allocator_pair.max_elements_per_group);
}
// Constructor with minimum & maximum group size parameters:
stack(const size_type min_allocation_amount, const size_type max_allocation_amount = (std::numeric_limits<size_type>::max() / 2)) PLF_STACK_NOEXCEPT:
element_allocator_type(element_allocator_type()),
current_group(NULL),
first_group(NULL),
top_element(NULL),
start_element(NULL),
end_element(NULL),
total_number_of_elements(0),
min_elements_per_group(min_allocation_amount),
group_allocator_pair(max_allocation_amount)
{
assert(min_elements_per_group > 2);
assert(min_elements_per_group <= group_allocator_pair.max_elements_per_group);
assert(group_allocator_pair.max_elements_per_group <= std::numeric_limits<size_type>::max() / 2);
}
// Allocator-extended constructor with minimum & maximum group size parameters:
stack(const size_type min_allocation_amount, const size_type max_allocation_amount, const element_allocator_type &alloc) :
element_allocator_type(alloc),
current_group(NULL),
first_group(NULL),
top_element(NULL),
start_element(NULL),
end_element(NULL),
total_number_of_elements(0),
min_elements_per_group(min_allocation_amount),
group_allocator_pair(max_allocation_amount)
{
assert(min_elements_per_group > 2);
assert(min_elements_per_group <= group_allocator_pair.max_elements_per_group);
assert(group_allocator_pair.max_elements_per_group <= std::numeric_limits<size_type>::max() / 2); // Must be half of what the allocator can allocate, otherwise could result in overflow, because at the point where we might allocate a max group of that size, the previous groups will have a total size equal to it, as each group doubles the previous capacity of the stack.
}
private:
void copy_from_source(const stack &source)
{
assert(&source != this);
if (total_number_of_elements == 0)
{
return;
}
group_pointer_type current_copy_group = source.first_group;
const group_pointer_type end_copy_group = source.current_group;
if (total_number_of_elements <= group_allocator_pair.max_elements_per_group) // most common case
{
min_elements_per_group = total_number_of_elements;
initialize();
min_elements_per_group = source.min_elements_per_group;
// Copy groups to this stack:
while (current_copy_group != end_copy_group)
{
std::uninitialized_copy(current_copy_group->elements, current_copy_group->end + 1, top_element);
top_element += (current_copy_group->end + 1) - current_copy_group->elements;
current_copy_group = current_copy_group->next_group;
}
// Handle special case of last group:
std::uninitialized_copy(source.start_element, source.top_element + 1, top_element);
end_element = (top_element += (source.top_element - source.start_element)); // This should make top_element == the last "pushed" element, rather than the one past it
}
else // uncommon edge case, so not optimising:
{
min_elements_per_group = group_allocator_pair.max_elements_per_group;
total_number_of_elements = 0;
initialize();
while (current_copy_group != end_copy_group)
{
for (element_pointer_type element_to_copy = current_copy_group->elements; element_to_copy != current_copy_group->end + 1; ++element_to_copy)
{
push(*element_to_copy);
}
current_copy_group = current_copy_group->next_group;
}
// Handle special case of last group:
for (element_pointer_type element_to_copy = source.start_element; element_to_copy != source.top_element + 1; ++element_to_copy)
{
push(*element_to_copy);
}
min_elements_per_group = source.min_elements_per_group;
}
}
public:
// Copy constructor:
stack(const stack &source) :
element_allocator_type(source),
current_group(NULL),
first_group(NULL),
top_element(NULL),
start_element(NULL),
end_element(NULL),
total_number_of_elements(source.total_number_of_elements),
min_elements_per_group(source.min_elements_per_group),
group_allocator_pair(source.group_allocator_pair.max_elements_per_group)
{
copy_from_source(source);
}
// Allocator-extended copy constructor:
stack(const stack &source, const allocator_type &alloc) :
element_allocator_type(alloc),
current_group(NULL),
first_group(NULL),
top_element(NULL),
start_element(NULL),
end_element(NULL),
total_number_of_elements(source.total_number_of_elements),
min_elements_per_group(source.min_elements_per_group),
group_allocator_pair(source.group_allocator_pair.max_elements_per_group)
{
copy_from_source(source);
}
#ifdef PLF_STACK_MOVE_SEMANTICS_SUPPORT
// move constructor
stack(stack &&source) PLF_STACK_NOEXCEPT:
element_allocator_type(source),
current_group(std::move(source.current_group)),
first_group(std::move(source.first_group)),
top_element(std::move(source.top_element)),
start_element(std::move(source.start_element)),
end_element(std::move(source.end_element)),
total_number_of_elements(source.total_number_of_elements),
min_elements_per_group(source.min_elements_per_group),
group_allocator_pair(source.group_allocator_pair.max_elements_per_group)
{
source.blank();
}
// allocator-extended move constructor
stack(stack &&source, const allocator_type &alloc) :
element_allocator_type(alloc),
current_group(std::move(source.current_group)),
first_group(std::move(source.first_group)),
top_element(std::move(source.top_element)),
start_element(std::move(source.start_element)),
end_element(std::move(source.end_element)),
total_number_of_elements(source.total_number_of_elements),
min_elements_per_group(source.min_elements_per_group),
group_allocator_pair(source.group_allocator_pair.max_elements_per_group)
{
source.blank();
}
#endif
~stack() PLF_STACK_NOEXCEPT
{
destroy_all_data();
}
private:
void destroy_all_data() PLF_STACK_NOEXCEPT
{
#ifdef PLF_STACK_TYPE_TRAITS_SUPPORT
if PLF_STACK_CONSTEXPR(!std::is_trivially_destructible<element_type>::value) // Avoid iteration for trivially-destructible types eg. POD, structs, classes with empty destructor.
#endif // If compiler doesn't support traits, iterate regardless - trivial destructors will not be called, hopefully compiler will optimise this loop out for POD types
{
if (total_number_of_elements != 0)
{
while (first_group != current_group)
{
const element_pointer_type past_end = first_group->end + 1;
for (element_pointer_type element_pointer = first_group->elements; element_pointer != past_end; ++element_pointer)
{
PLF_STACK_DESTROY(element_allocator_type, (*this), element_pointer);
}
const group_pointer_type next_group = first_group->next_group;
PLF_STACK_DESTROY(group_allocator_type, group_allocator_pair, first_group);
PLF_STACK_DEALLOCATE(group_allocator_type, group_allocator_pair, first_group, 1);
first_group = next_group;
}
// Special case for current group:
const element_pointer_type past_end = top_element + 1;
for (element_pointer_type element_pointer = start_element; element_pointer != past_end; ++element_pointer)
{
PLF_STACK_DESTROY(element_allocator_type, (*this), element_pointer);
}
first_group = first_group->next_group;
PLF_STACK_DESTROY(group_allocator_type, group_allocator_pair, current_group);
PLF_STACK_DEALLOCATE(group_allocator_type, group_allocator_pair, current_group, 1);
}
}
total_number_of_elements = 0;
while (first_group != NULL)
{
current_group = first_group;
first_group = first_group->next_group;
PLF_STACK_DESTROY(group_allocator_type, group_allocator_pair, current_group);
PLF_STACK_DEALLOCATE(group_allocator_type, group_allocator_pair, current_group, 1);
}
}
void initialize()
{
first_group = current_group = PLF_STACK_ALLOCATE(group_allocator_type, group_allocator_pair, 1, 0);
// Initialize:
try
{
#ifdef PLF_STACK_VARIADICS_SUPPORT
PLF_STACK_CONSTRUCT(group_allocator_type, group_allocator_pair, first_group, min_elements_per_group);
#else
PLF_STACK_CONSTRUCT(group_allocator_type, group_allocator_pair, first_group, group(min_elements_per_group));
#endif
}
catch (...)
{
PLF_STACK_DEALLOCATE(group_allocator_type, group_allocator_pair, first_group, 1);
first_group = current_group = NULL;
throw;
}
top_element = start_element = first_group->elements;
end_element = first_group->end;
}
public:
void push(const element_type &element)
{
switch ((top_element == NULL) + (top_element == end_element))
{
case 0:
{
#ifdef PLF_STACK_TYPE_TRAITS_SUPPORT
if PLF_STACK_CONSTEXPR(std::is_nothrow_copy_constructible<element_type>::value)
{
PLF_STACK_CONSTRUCT(element_allocator_type, (*this), ++top_element, element);
}
else
#endif
{
try
{
PLF_STACK_CONSTRUCT(element_allocator_type, (*this), ++top_element, element);
}
catch (...)
{
--top_element;
throw;
}
}
++total_number_of_elements;
return;
}
case 1:
{
if (current_group->next_group == NULL)
{
current_group->next_group = PLF_STACK_ALLOCATE(group_allocator_type, group_allocator_pair, 1, current_group);
try
{
#ifdef PLF_STACK_VARIADICS_SUPPORT
PLF_STACK_CONSTRUCT(group_allocator_type, group_allocator_pair, current_group->next_group, (total_number_of_elements < group_allocator_pair.max_elements_per_group) ? total_number_of_elements : group_allocator_pair.max_elements_per_group, current_group);
#else
PLF_STACK_CONSTRUCT(group_allocator_type, group_allocator_pair, current_group->next_group, group((total_number_of_elements < group_allocator_pair.max_elements_per_group) ? total_number_of_elements : group_allocator_pair.max_elements_per_group, current_group));
#endif
}
catch (...)
{
PLF_STACK_DEALLOCATE(group_allocator_type, group_allocator_pair, current_group->next_group, 1);
current_group->next_group = NULL;
throw;
}
}
current_group = current_group->next_group;
start_element = top_element = current_group->elements;
#ifdef PLF_STACK_TYPE_TRAITS_SUPPORT
if PLF_STACK_CONSTEXPR(std::is_nothrow_copy_constructible<element_type>::value)
{
PLF_STACK_CONSTRUCT(element_allocator_type, (*this), top_element, element);
}
else
#endif
{
try
{
PLF_STACK_CONSTRUCT(element_allocator_type, (*this), top_element, element);
}
catch (...)
{
current_group = current_group->previous_group;
start_element = current_group->elements;
top_element = current_group->end;
throw;
}
}
end_element = current_group->end;
++total_number_of_elements;
return;
}
case 2: // ie. empty stack, must initialize
{
initialize();
#ifdef PLF_STACK_TYPE_TRAITS_SUPPORT
if PLF_STACK_CONSTEXPR(std::is_nothrow_copy_constructible<element_type>::value)
{
PLF_STACK_CONSTRUCT(element_allocator_type, (*this), top_element, element);
}
else
#endif
{
try
{
PLF_STACK_CONSTRUCT(element_allocator_type, (*this), top_element, element);
}
catch (...)
{
clear();
throw;
}
}
++total_number_of_elements;
return;
}
}
}
#ifdef PLF_STACK_MOVE_SEMANTICS_SUPPORT
// Note: the reason for code duplication from non-move push, as opposed to using std::forward for both, was because most compilers didn't actually create as-optimal code in that strategy. Also C++03 compatibility.
void push(element_type &&element)
{
switch ((top_element == NULL) + (top_element == end_element))
{
case 0:
{
#ifdef PLF_STACK_TYPE_TRAITS_SUPPORT
if PLF_STACK_CONSTEXPR(std::is_nothrow_move_constructible<element_type>::value)
{
PLF_STACK_CONSTRUCT(element_allocator_type, (*this), ++top_element, std::move(element));
}
else
#endif
{
try
{
PLF_STACK_CONSTRUCT(element_allocator_type, (*this), ++top_element, std::move(element));
}
catch (...)
{
--top_element;
throw;
}
}
++total_number_of_elements;
return;
}
case 1:
{
if (current_group->next_group == NULL)
{
current_group->next_group = PLF_STACK_ALLOCATE(group_allocator_type, group_allocator_pair, 1, current_group);
try
{
#ifdef PLF_STACK_VARIADICS_SUPPORT
PLF_STACK_CONSTRUCT(group_allocator_type, group_allocator_pair, current_group->next_group, (total_number_of_elements < group_allocator_pair.max_elements_per_group) ? total_number_of_elements : group_allocator_pair.max_elements_per_group, current_group);
#else
PLF_STACK_CONSTRUCT(group_allocator_type, group_allocator_pair, current_group->next_group, group((total_number_of_elements < group_allocator_pair.max_elements_per_group) ? total_number_of_elements : group_allocator_pair.max_elements_per_group, current_group));
#endif
}
catch (...)
{
PLF_STACK_DEALLOCATE(group_allocator_type, group_allocator_pair, current_group->next_group, 1);
current_group->next_group = NULL;
throw;
}
}
current_group = current_group->next_group;
start_element = top_element = current_group->elements;
#ifdef PLF_STACK_TYPE_TRAITS_SUPPORT
if PLF_STACK_CONSTEXPR(std::is_nothrow_move_constructible<element_type>::value)
{
PLF_STACK_CONSTRUCT(element_allocator_type, (*this), top_element, std::move(element));
}
else
#endif
{
try
{
PLF_STACK_CONSTRUCT(element_allocator_type, (*this), top_element, std::move(element));
}
catch (...)
{
current_group = current_group->previous_group;
start_element = current_group->elements;
top_element = current_group->end;
throw;
}
}
end_element = current_group->end;
++total_number_of_elements;
return;
}
case 2: // ie. empty stack, must initialize
{
initialize();
#ifdef PLF_STACK_TYPE_TRAITS_SUPPORT
if PLF_STACK_CONSTEXPR(std::is_nothrow_move_constructible<element_type>::value)
{
PLF_STACK_CONSTRUCT(element_allocator_type, (*this), top_element, std::move(element));
}
else
#endif
{
try
{
PLF_STACK_CONSTRUCT(element_allocator_type, (*this), top_element, std::move(element));
}
catch (...)
{
clear();
throw;
}
}
++total_number_of_elements;
return;
}
}
}
#endif
#ifdef PLF_STACK_VARIADICS_SUPPORT
template<typename... arguments>
void emplace(arguments &&... parameters)
{
switch ((top_element == NULL) + (top_element == end_element))
{
case 0:
{
#ifdef PLF_STACK_TYPE_TRAITS_SUPPORT
if PLF_STACK_CONSTEXPR(std::is_nothrow_constructible<element_type, arguments ...>::value)
{
PLF_STACK_CONSTRUCT(element_allocator_type, (*this), ++top_element, std::forward<arguments>(parameters)...);
}
else
#endif
{
try
{
PLF_STACK_CONSTRUCT(element_allocator_type, (*this), ++top_element, std::forward<arguments>(parameters)...);
}
catch (...)
{
--top_element;
throw;
}
}
++total_number_of_elements;
return;
}
case 1:
{
if (current_group->next_group == NULL)
{
current_group->next_group = PLF_STACK_ALLOCATE(group_allocator_type, group_allocator_pair, 1, current_group);
try
{
PLF_STACK_CONSTRUCT(group_allocator_type, group_allocator_pair, current_group->next_group, (total_number_of_elements < group_allocator_pair.max_elements_per_group) ? total_number_of_elements : group_allocator_pair.max_elements_per_group, current_group);
}
catch (...)
{
PLF_STACK_DEALLOCATE(group_allocator_type, group_allocator_pair, current_group->next_group, 1);
current_group->next_group = NULL;
throw;
}
}
current_group = current_group->next_group;
start_element = top_element = current_group->elements;
#ifdef PLF_STACK_TYPE_TRAITS_SUPPORT
if PLF_STACK_CONSTEXPR(std::is_nothrow_constructible<element_type, arguments ...>::value)
{
PLF_STACK_CONSTRUCT(element_allocator_type, (*this), top_element, std::forward<arguments>(parameters)...);
}
else
#endif
{
try
{
PLF_STACK_CONSTRUCT(element_allocator_type, (*this), top_element, std::forward<arguments>(parameters)...);
}
catch (...)
{
current_group = current_group->previous_group;
start_element = current_group->elements;
top_element = current_group->end;
throw;
}
}
end_element = current_group->end;
++total_number_of_elements;
return;
}
case 2: // ie. empty stack, must initialize
{
initialize();
#ifdef PLF_STACK_TYPE_TRAITS_SUPPORT
if PLF_STACK_CONSTEXPR(std::is_nothrow_constructible<element_type, arguments ...>::value)
{
PLF_STACK_CONSTRUCT(element_allocator_type, (*this), top_element, std::forward<arguments>(parameters)...);
}
else
#endif
{
try
{
PLF_STACK_CONSTRUCT(element_allocator_type, (*this), top_element, std::forward<arguments>(parameters)...);
}
catch (...)
{
clear();
throw;
}
}
++total_number_of_elements;
return;
}
}
}
#endif
inline PLF_STACK_FORCE_INLINE reference top() const // Exception may occur if stack is empty in release mode
{
assert(!empty());
return *top_element;
}
void pop() // Exception will occur if stack is empty
{
assert(!empty());
#ifdef PLF_STACK_TYPE_TRAITS_SUPPORT
if PLF_STACK_CONSTEXPR(!std::is_trivially_destructible<element_type>::value)
#endif
{
PLF_STACK_DESTROY(element_allocator_type, (*this), top_element);
}
// ie. if total_number_of_elements != 0 after decrement, or we were not already at the start of a non-first group
if (total_number_of_elements-- == 1 || top_element != start_element) // If total_number_of_elements is now 0 after decrement, this essentially moves top_element back to it's initial position (start_element - 1). But otherwise, this is just a regular pop
{
--top_element;
}
else
{ // ie. is start element, but not first group in stack (if it were, total_number_of_elements would be 0 after decrement)
current_group = current_group->previous_group;
start_element = current_group->elements;
end_element = top_element = current_group->end;
}
}
inline stack & operator = (const stack &source)
{
assert(&source != this);
destroy_all_data();
stack temp(source);
#ifdef PLF_STACK_MOVE_SEMANTICS_SUPPORT
*this = std::move(temp); // Avoid generating 2nd temporary
#else
swap(temp);
#endif
return *this;
}
#ifdef PLF_STACK_MOVE_SEMANTICS_SUPPORT
// Move assignment
stack & operator = (stack &&source) PLF_STACK_NOEXCEPT_MOVE_ASSIGNMENT(allocator_type)
{
assert(&source != this);
destroy_all_data();
#ifdef PLF_STACK_TYPE_TRAITS_SUPPORT
if PLF_STACK_CONSTEXPR(std::is_trivial<group_pointer_type>::value && std::is_trivial<element_pointer_type>::value)
{
std::memcpy(static_cast<void *>(this), &source, sizeof(stack));
}
else
#endif
{
current_group = std::move(source.current_group);
first_group = std::move(source.first_group);
top_element = std::move(source.top_element);
start_element = std::move(source.start_element);
end_element = std::move(source.end_element);