-
Notifications
You must be signed in to change notification settings - Fork 7
/
deque.h
2789 lines (2272 loc) · 111 KB
/
deque.h
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) Electronic Arts Inc. All rights reserved.
/////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
// deque design
//
// A deque (pronounced "deck") is a double-ended queue, though this is partially
// of a misnomer. A deque does indeed let you add and remove values from both ends
// of the container, but it's not usually used for such a thing and instead is used
// as a more flexible version of a vector. It provides operator[] (random access)
// and can insert items anywhere and not just at the front and back.
//
// While you can implement a double-ended queue via a doubly-linked list, deque is
// instead implemented as a list of arrays. The benefit of this is that memory usage
// is lower and that random access can be had with decent efficiency.
//
// Our implementation of deque is just like every other implementation of deque,
// as the C++ standard all but dictates that you make it work this way. Below
// we have a depiction of an array (or vector) of 48 items, with each node being
// a '+' character and extra capacity being a '-' character. What we have is one
// contiguous block of memory:
//
// ++++++++++++++++++++++++++++++++++++++++++++++++-----------------
// 0 47
//
// With a deque, the same array of 48 items would be implemented as multiple smaller
// arrays of contiguous memory, each of fixed size. We will call these "sub-arrays."
// In the case here, we have six arrays of 8 nodes:
//
// ++++++++ ++++++++ ++++++++ ++++++++ ++++++++ ++++++++
//
// With an vector, item [0] is the first item and item [47] is the last item. With a
// deque, item [0] is usually not the first item and neither is item [47]. There is
// extra capacity on both the front side and the back side of the deque. So a deque
// (of 24 items) actually looks like this:
//
// -------- -----+++ ++++++++ ++++++++ +++++--- --------
// 0 23
//
// To insert items at the front, you move into the capacity on the left, and to insert
// items at the back, you append items on the right. As you can see, inserting an item
// at the front doesn't require allocating new memory nor does it require moving any
// items in the container. It merely involves moving the pointer to the [0] item to
// the left by one node.
//
// We keep track of these sub-arrays by having an array of pointers, with each array
// entry pointing to each of the sub-arrays. We could alternatively use a linked
// list of pointers, but it turns out we can implement our deque::operator[] more
// efficiently if we use an array of pointers instead of a list of pointers.
//
// To implement deque::iterator, we could keep a struct which is essentially this:
// struct iterator {
// int subArrayIndex;
// int subArrayOffset;
// }
//
// In practice, we implement iterators a little differently, but in reality our
// implementation isn't much different from the above. It turns out that it's most
// simple if we also manage the location of item [0] and item [end] by using these
// same iterators.
//
// To consider: Implement the deque as a circular deque instead of a linear one.
// This would use a similar subarray layout but iterators would
// wrap around when they reached the end of the subarray pointer list.
//
//////////////////////////////////////////////////////////////////////////////
#ifndef EASTL_DEQUE_H
#define EASTL_DEQUE_H
#include <eastl/internal/config.h>
#include <eastl/allocator.h>
#include <eastl/algorithm.h>
#include <eastl/type_traits.h>
#include <eastl/iterator.h>
#include <eastl/memory.h>
#include <eastl/initializer_list.h>
EA_DISABLE_ALL_VC_WARNINGS()
#include <new>
#include <stddef.h>
EA_RESTORE_ALL_VC_WARNINGS()
#if EASTL_EXCEPTIONS_ENABLED
EA_DISABLE_ALL_VC_WARNINGS()
#include <stdexcept> // std::out_of_range, std::length_error.
EA_RESTORE_ALL_VC_WARNINGS()
#endif
// 4267 - 'argument' : conversion from 'size_t' to 'const uint32_t', possible loss of data. This is a bogus warning resulting from a bug in VC++.
// 4345 - Behavior change: an object of POD type constructed with an initializer of the form () will be default-initialized
// 4480 - nonstandard extension used: specifying underlying type for enum
// 4530 - C++ exception handler used, but unwind semantics are not enabled. Specify /EHsc
// 4571 - catch(...) semantics changed since Visual C++ 7.1; structured exceptions (SEH) are no longer caught.
EA_DISABLE_VC_WARNING(4267 4345 4480 4530 4571);
#if EASTL_EXCEPTIONS_ENABLED
// 4703 - potentially uninitialized local pointer variable used. VC++ is mistakenly analyzing the possibility of uninitialized variables, though it's not easy for it to do so.
// 4701 - potentially uninitialized local variable used.
EA_DISABLE_VC_WARNING(4703 4701)
#endif
#if defined(EASTL_PRAGMA_ONCE_SUPPORTED)
#pragma once // Some compilers (e.g. VC++) benefit significantly from using this. We've measured 3-4% build speed improvements in apps as a result.
#endif
namespace eastl
{
/// EASTL_DEQUE_DEFAULT_NAME
///
/// Defines a default container name in the absence of a user-provided name.
///
#ifndef EASTL_DEQUE_DEFAULT_NAME
#define EASTL_DEQUE_DEFAULT_NAME EASTL_DEFAULT_NAME_PREFIX " deque" // Unless the user overrides something, this is "EASTL deque".
#endif
/// EASTL_DEQUE_DEFAULT_ALLOCATOR
///
#ifndef EASTL_DEQUE_DEFAULT_ALLOCATOR
#define EASTL_DEQUE_DEFAULT_ALLOCATOR allocator_type(EASTL_DEQUE_DEFAULT_NAME)
#endif
/// DEQUE_DEFAULT_SUBARRAY_SIZE
///
/// Defines the default number of items in a subarray.
/// Note that the user has the option of specifying the subarray size
/// in the deque template declaration.
///
#if !defined(__GNUC__) || (__GNUC__ >= 3) // GCC 2.x can't handle the declaration below.
#define DEQUE_DEFAULT_SUBARRAY_SIZE(T) ((sizeof(T) <= 4) ? 64 : ((sizeof(T) <= 8) ? 32 : ((sizeof(T) <= 16) ? 16 : ((sizeof(T) <= 32) ? 8 : 4))))
#else
#define DEQUE_DEFAULT_SUBARRAY_SIZE(T) 16
#endif
/// DequeIterator
///
/// The DequeIterator provides both const and non-const iterators for deque.
/// It also is used for the tracking of the begin and end for the deque.
///
template <typename T, typename Pointer, typename Reference, unsigned kDequeSubarraySize>
struct DequeIterator
{
typedef DequeIterator<T, Pointer, Reference, kDequeSubarraySize> this_type;
typedef DequeIterator<T, T*, T&, kDequeSubarraySize> iterator;
typedef DequeIterator<T, const T*, const T&, kDequeSubarraySize> const_iterator;
typedef ptrdiff_t difference_type;
typedef EASTL_ITC_NS::random_access_iterator_tag iterator_category;
typedef T value_type;
typedef T* pointer;
typedef T& reference;
public:
DequeIterator();
DequeIterator(const iterator& x);
pointer operator->() const;
reference operator*() const;
this_type& operator++();
this_type operator++(int);
this_type& operator--();
this_type operator--(int);
this_type& operator+=(difference_type n);
this_type& operator-=(difference_type n);
this_type operator+(difference_type n) const;
this_type operator-(difference_type n) const;
protected:
template <typename, typename, typename, unsigned>
friend struct DequeIterator;
template <typename, typename, unsigned>
friend struct DequeBase;
template <typename, typename, unsigned>
friend class deque;
template <typename U, typename PointerA, typename ReferenceA, typename PointerB, typename ReferenceB, unsigned kDequeSubarraySizeU>
friend bool operator==(const DequeIterator<U, PointerA, ReferenceA, kDequeSubarraySizeU>&,
const DequeIterator<U, PointerB, ReferenceB, kDequeSubarraySizeU>&);
template <typename U, typename PointerA, typename ReferenceA, typename PointerB, typename ReferenceB, unsigned kDequeSubarraySizeU>
friend bool operator!=(const DequeIterator<U, PointerA, ReferenceA, kDequeSubarraySizeU>&,
const DequeIterator<U, PointerB, ReferenceB, kDequeSubarraySizeU>&);
template <typename U, typename PointerU, typename ReferenceU, unsigned kDequeSubarraySizeU>
friend bool operator!=(const DequeIterator<U, PointerU, ReferenceU, kDequeSubarraySizeU>& a,
const DequeIterator<U, PointerU, ReferenceU, kDequeSubarraySizeU>& b);
template <typename U, typename PointerA, typename ReferenceA, typename PointerB, typename ReferenceB, unsigned kDequeSubarraySizeU>
friend bool operator< (const DequeIterator<U, PointerA, ReferenceA, kDequeSubarraySizeU>&,
const DequeIterator<U, PointerB, ReferenceB, kDequeSubarraySizeU>&);
template <typename U, typename PointerA, typename ReferenceA, typename PointerB, typename ReferenceB, unsigned kDequeSubarraySizeU>
friend bool operator> (const DequeIterator<U, PointerA, ReferenceA, kDequeSubarraySizeU>&,
const DequeIterator<U, PointerB, ReferenceB, kDequeSubarraySizeU>&);
template <typename U, typename PointerA, typename ReferenceA, typename PointerB, typename ReferenceB, unsigned kDequeSubarraySizeU>
friend bool operator<=(const DequeIterator<U, PointerA, ReferenceA, kDequeSubarraySizeU>&,
const DequeIterator<U, PointerB, ReferenceB, kDequeSubarraySizeU>&);
template <typename U, typename PointerA, typename ReferenceA, typename PointerB, typename ReferenceB, unsigned kDequeSubarraySizeU>
friend bool operator>=(const DequeIterator<U, PointerA, ReferenceA, kDequeSubarraySizeU>&,
const DequeIterator<U, PointerB, ReferenceB, kDequeSubarraySizeU>&);
template <typename U, typename PointerA, typename ReferenceA, typename PointerB, typename ReferenceB, unsigned kDequeSubarraySizeU>
friend typename DequeIterator<U, PointerA, ReferenceA, kDequeSubarraySizeU>::difference_type
operator-(const DequeIterator<U, PointerA, ReferenceA, kDequeSubarraySizeU>& a,
const DequeIterator<U, PointerB, ReferenceB, kDequeSubarraySizeU>& b);
protected:
T* mpCurrent; // Where we currently point. Declared first because it's used most often.
T* mpBegin; // The beginning of the current subarray.
T* mpEnd; // The end of the current subarray. To consider: remove this member, as it is always equal to 'mpBegin + kDequeSubarraySize'. Given that deque subarrays usually consist of hundreds of bytes, this isn't a massive win. Also, now that we are implementing a zero-allocation new deque policy, mpEnd may in fact not be equal to 'mpBegin + kDequeSubarraySize'.
T** mpCurrentArrayPtr; // Pointer to current subarray. We could alternatively implement this as a list node iterator if the deque used a linked list.
struct Increment {};
struct Decrement {};
struct FromConst {};
DequeIterator(T** pCurrentArrayPtr, T* pCurrent);
DequeIterator(const const_iterator& x, FromConst) : mpCurrent(x.mpCurrent), mpBegin(x.mpBegin), mpEnd(x.mpEnd), mpCurrentArrayPtr(x.mpCurrentArrayPtr){}
DequeIterator(const iterator& x, Increment);
DequeIterator(const iterator& x, Decrement);
this_type copy(const iterator& first, const iterator& last, true_type); // true means that value_type has the type_trait is_trivially_copyable,
this_type copy(const iterator& first, const iterator& last, false_type); // false means it does not.
void copyBackward(const iterator& first, const iterator& last, true_type); // true means that value_type has the type_trait is_trivially_copyable,
void copyBackward(const iterator& first, const iterator& last, false_type); // false means it does not.
void SetSubarray(T** pCurrentArrayPtr);
};
/// DequeBase
///
/// The DequeBase implements memory allocation for deque.
/// See VectorBase (class vector) for an explanation of why we
/// create this separate base class.
///
template <typename T, typename Allocator, unsigned kDequeSubarraySize>
struct DequeBase
{
typedef T value_type;
typedef Allocator allocator_type;
typedef eastl_size_t size_type; // See config.h for the definition of eastl_size_t, which defaults to size_t.
typedef ptrdiff_t difference_type;
typedef DequeIterator<T, T*, T&, kDequeSubarraySize> iterator;
typedef DequeIterator<T, const T*, const T&, kDequeSubarraySize> const_iterator;
static const size_type npos = (size_type)-1; /// 'npos' means non-valid position or simply non-position.
static const size_type kMaxSize = (size_type)-2; /// -1 is reserved for 'npos'. It also happens to be slightly beneficial that kMaxSize is a value less than -1, as it helps us deal with potential integer wraparound issues.
enum
{
kMinPtrArraySize = 8, /// A new empty deque has a ptrArraySize of 0, but any allocated ptrArrays use this min size.
kSubarraySize = kDequeSubarraySize ///
//kNodeSize = kDequeSubarraySize * sizeof(T) /// Disabled because it prevents the ability to do this: struct X{ eastl::deque<X, EASTLAllocatorType, 16> mDequeOfSelf; };
};
protected:
enum Side /// Defines the side of the deque: front or back.
{
kSideFront, /// Identifies the front side of the deque.
kSideBack /// Identifies the back side of the deque.
};
T** mpPtrArray; // Array of pointers to subarrays.
size_type mnPtrArraySize; // Possibly we should store this as T** mpArrayEnd.
iterator mItBegin; // Where within the subarrays is our beginning.
iterator mItEnd; // Where within the subarrays is our end.
allocator_type mAllocator; // To do: Use base class optimization to make this go away.
public:
DequeBase(const allocator_type& allocator);
DequeBase(size_type n);
DequeBase(size_type n, const allocator_type& allocator);
~DequeBase();
const allocator_type& getAllocator() const EASTL_NOEXCEPT;
allocator_type& getAllocator() EASTL_NOEXCEPT;
void setAllocator(const allocator_type& allocator);
protected:
T* DoAllocateSubarray();
void DoFreeSubarray(T* p);
void DoFreeSubarrays(T** pBegin, T** pEnd);
T** DoAllocatePtrArray(size_type n);
void DoFreePtrArray(T** p, size_t n);
iterator DoReallocSubarray(size_type nAdditionalCapacity, Side allocationSide);
void DoReallocPtrArray(size_type nAdditionalCapacity, Side allocationSide);
void DoInit(size_type n);
}; // DequeBase
/// deque
///
/// Implements a conventional C++ double-ended queue. The implementation used here
/// is very much like any other deque implementations you may have seen, as it
/// follows the standard algorithm for deque design.
///
/// Note:
/// As of this writing, deque does not support zero-allocation initial emptiness.
/// A newly created deque with zero elements will still allocate a subarray
/// pointer set. We are looking for efficient and clean ways to get around this,
/// but current efforts have resulted in less efficient and more fragile code.
/// The logic of this class doesn't lend itself to a clean implementation.
/// It turns out that deques are one of the least likely classes you'd want this
/// behaviour in, so until this functionality becomes very important to somebody,
/// we will leave it as-is. It can probably be solved by adding some extra code to
/// the Do* functions and adding good comments explaining the situation.
///
template <typename T, typename Allocator = EASTLAllocatorType, unsigned kDequeSubarraySize = DEQUE_DEFAULT_SUBARRAY_SIZE(T)>
class deque : public DequeBase<T, Allocator, kDequeSubarraySize>
{
public:
typedef DequeBase<T, Allocator, kDequeSubarraySize> base_type;
typedef deque<T, Allocator, kDequeSubarraySize> this_type;
typedef T value_type;
typedef T* pointer;
typedef const T* const_pointer;
typedef T& reference;
typedef const T& const_reference;
typedef DequeIterator<T, T*, T&, kDequeSubarraySize> iterator;
typedef DequeIterator<T, const T*, const T&, kDequeSubarraySize> const_iterator;
typedef eastl::reverse_iterator<iterator> reverse_iterator;
typedef eastl::reverse_iterator<const_iterator> const_reverse_iterator;
typedef typename base_type::size_type size_type;
typedef typename base_type::difference_type difference_type;
typedef typename base_type::allocator_type allocator_type;
using base_type::npos;
#if EA_IS_ENABLED(EASTL_DEPRECATIONS_FOR_2024_APRIL)
static_assert(!is_const<value_type>::value, "deque<T>::value_type must be non-const.");
static_assert(!is_volatile<value_type>::value, "deque<T>::value_type must be non-volatile.");
#endif
protected:
using base_type::kSideFront;
using base_type::kSideBack;
using base_type::mpPtrArray;
using base_type::mnPtrArraySize;
using base_type::mItBegin;
using base_type::mItEnd;
using base_type::mAllocator;
using base_type::DoAllocateSubarray;
using base_type::DoFreeSubarray;
using base_type::DoFreeSubarrays;
using base_type::DoAllocatePtrArray;
using base_type::DoFreePtrArray;
using base_type::DoReallocSubarray;
using base_type::DoReallocPtrArray;
public:
deque();
explicit deque(const allocator_type& allocator);
explicit deque(size_type n, const allocator_type& allocator = EASTL_DEQUE_DEFAULT_ALLOCATOR);
deque(size_type n, const value_type& value, const allocator_type& allocator = EASTL_DEQUE_DEFAULT_ALLOCATOR);
deque(const this_type& x);
deque(this_type&& x);
deque(this_type&& x, const allocator_type& allocator);
deque(std::initializer_list<value_type> ilist, const allocator_type& allocator = EASTL_DEQUE_DEFAULT_ALLOCATOR);
// note: this has pre-C++11 semantics:
// this constructor is equivalent to the constructor deque(static_cast<size_type>(first), static_cast<value_type>(last)) if InputIterator is an integral type.
template <typename InputIterator>
deque(InputIterator first, InputIterator last); // allocator arg removed because VC7.1 fails on the default arg. To do: Make a second version of this function without a default arg.
~deque();
this_type& operator=(const this_type& x);
this_type& operator=(std::initializer_list<value_type> ilist);
this_type& operator=(this_type&& x);
void swap(this_type& x);
void assign(size_type n, const value_type& value);
void assign(std::initializer_list<value_type> ilist);
template <typename InputIterator> // It turns out that the C++ std::deque<int, int> specifies a two argument
void assign(InputIterator first, InputIterator last); // version of assign that takes (int size, int value). These are not
// iterators, so we need to do a template compiler trick to do the right thing.
iterator begin() EASTL_NOEXCEPT;
const_iterator begin() const EASTL_NOEXCEPT;
const_iterator cbegin() const EASTL_NOEXCEPT;
iterator end() EASTL_NOEXCEPT;
const_iterator end() const EASTL_NOEXCEPT;
const_iterator cend() const EASTL_NOEXCEPT;
reverse_iterator rbegin() EASTL_NOEXCEPT;
const_reverse_iterator rbegin() const EASTL_NOEXCEPT;
const_reverse_iterator crbegin() const EASTL_NOEXCEPT;
reverse_iterator rend() EASTL_NOEXCEPT;
const_reverse_iterator rend() const EASTL_NOEXCEPT;
const_reverse_iterator crend() const EASTL_NOEXCEPT;
bool empty() const EASTL_NOEXCEPT;
size_type size() const EASTL_NOEXCEPT;
void resize(size_type n, const value_type& value);
void resize(size_type n);
void shrink_to_fit();
void setCapacity(size_type n = base_type::npos);
reference operator[](size_type n);
const_reference operator[](size_type n) const;
reference at(size_type n);
const_reference at(size_type n) const;
reference front();
const_reference front() const;
reference back();
const_reference back() const;
void pushFront(const value_type& value);
reference pushFront();
void pushFront(value_type&& value);
void pushBack(const value_type& value);
reference pushBack();
void pushBack(value_type&& value);
void popFront();
void popBack();
template<class... Args>
iterator emplace(const_iterator position, Args&&... args);
template<class... Args>
void emplace_front(Args&&... args);
template<class... Args>
void emplace_back(Args&&... args);
iterator insert(const_iterator position, const value_type& value);
iterator insert(const_iterator position, value_type&& value);
iterator insert(const_iterator position, size_type n, const value_type& value);
iterator insert(const_iterator position, std::initializer_list<value_type> ilist);
// note: this has pre-C++11 semantics:
// this function is equivalent to insert(const_iterator position, static_cast<size_type>(first), static_cast<value_type>(last)) if InputIterator is an integral type.
// ie. same as insert(const_iterator position, size_type n, const value_type& value)
template <typename InputIterator>
iterator insert(const_iterator position, InputIterator first, InputIterator last);
iterator erase(const_iterator position);
iterator erase(const_iterator first, const_iterator last);
reverse_iterator erase(reverse_iterator position);
reverse_iterator erase(reverse_iterator first, reverse_iterator last);
void clear();
//void reset_lose_memory(); // Disabled until it can be implemented efficiently and cleanly. // This is a unilateral reset to an initially empty state. No destructors are called, no deallocation occurs.
bool validate() const;
int validateIterator(const_iterator i) const;
protected:
template <typename Integer>
void DoInit(Integer n, Integer value, true_type);
template <typename InputIterator>
void DoInit(InputIterator first, InputIterator last, false_type);
template <typename InputIterator>
void DoInitFromIterator(InputIterator first, InputIterator last, EASTL_ITC_NS::input_iterator_tag);
template <typename ForwardIterator>
void DoInitFromIterator(ForwardIterator first, ForwardIterator last, EASTL_ITC_NS::forward_iterator_tag);
void DoFillInit(const value_type& value);
template <typename Integer>
void DoAssign(Integer n, Integer value, true_type);
template <typename InputIterator>
void DoAssign(InputIterator first, InputIterator last, false_type);
void DoAssignValues(size_type n, const value_type& value);
template <typename Integer>
iterator DoInsert(const const_iterator& position, Integer n, Integer value, true_type);
template <typename InputIterator>
iterator DoInsert(const const_iterator& position, const InputIterator& first, const InputIterator& last, false_type);
template <typename InputIterator>
iterator DoInsertFromIterator(const_iterator position, const InputIterator& first, const InputIterator& last, EASTL_ITC_NS::input_iterator_tag);
template <typename ForwardIterator>
iterator DoInsertFromIterator(const_iterator position, const ForwardIterator& first, const ForwardIterator& last, EASTL_ITC_NS::forward_iterator_tag);
iterator DoInsertValues(const_iterator position, size_type n, const value_type& value);
void DoSwap(this_type& x);
}; // class deque
///////////////////////////////////////////////////////////////////////
// DequeBase
///////////////////////////////////////////////////////////////////////
template <typename T, typename Allocator, unsigned kDequeSubarraySize>
DequeBase<T, Allocator, kDequeSubarraySize>::DequeBase(const allocator_type& allocator)
: mpPtrArray(NULL),
mnPtrArraySize(0),
mItBegin(),
mItEnd(),
mAllocator(allocator)
{
// It is assumed here that the deque subclass will init us when/as needed.
}
template <typename T, typename Allocator, unsigned kDequeSubarraySize>
DequeBase<T, Allocator, kDequeSubarraySize>::DequeBase(size_type n)
: mpPtrArray(NULL),
mnPtrArraySize(0),
mItBegin(),
mItEnd(),
mAllocator(EASTL_DEQUE_DEFAULT_NAME)
{
// It's important to note that DoInit creates space for elements and assigns
// mItBegin/mItEnd to point to them, but these elements are not constructed.
// You need to immediately follow this constructor with code that constructs the values.
DoInit(n);
}
template <typename T, typename Allocator, unsigned kDequeSubarraySize>
DequeBase<T, Allocator, kDequeSubarraySize>::DequeBase(size_type n, const allocator_type& allocator)
: mpPtrArray(NULL),
mnPtrArraySize(0),
mItBegin(),
mItEnd(),
mAllocator(allocator)
{
// It's important to note that DoInit creates space for elements and assigns
// mItBegin/mItEnd to point to them, but these elements are not constructed.
// You need to immediately follow this constructor with code that constructs the values.
DoInit(n);
}
template <typename T, typename Allocator, unsigned kDequeSubarraySize>
DequeBase<T, Allocator, kDequeSubarraySize>::~DequeBase()
{
if(mpPtrArray)
{
DoFreeSubarrays(mItBegin.mpCurrentArrayPtr, mItEnd.mpCurrentArrayPtr + 1);
DoFreePtrArray(mpPtrArray, mnPtrArraySize);
mpPtrArray = nullptr;
}
}
template <typename T, typename Allocator, unsigned kDequeSubarraySize>
const typename DequeBase<T, Allocator, kDequeSubarraySize>::allocator_type&
DequeBase<T, Allocator, kDequeSubarraySize>::getAllocator() const EASTL_NOEXCEPT
{
return mAllocator;
}
template <typename T, typename Allocator, unsigned kDequeSubarraySize>
typename DequeBase<T, Allocator, kDequeSubarraySize>::allocator_type&
DequeBase<T, Allocator, kDequeSubarraySize>::getAllocator() EASTL_NOEXCEPT
{
return mAllocator;
}
template <typename T, typename Allocator, unsigned kDequeSubarraySize>
void DequeBase<T, Allocator, kDequeSubarraySize>::setAllocator(const allocator_type& allocator)
{
// The only time you can set an allocator is with an empty unused container, such as right after construction.
if(EASTL_LIKELY(mAllocator != allocator))
{
if(EASTL_LIKELY(mpPtrArray && (mItBegin.mpCurrentArrayPtr == mItEnd.mpCurrentArrayPtr))) // If we are empty and so can safely deallocate the existing memory... We could also test for empty(), but that's a more expensive calculation and more involved clearing, though it would be more flexible.
{
DoFreeSubarrays(mItBegin.mpCurrentArrayPtr, mItEnd.mpCurrentArrayPtr + 1);
DoFreePtrArray(mpPtrArray, mnPtrArraySize);
mAllocator = allocator;
DoInit(0);
}
else
{
EASTL_FAIL_MSG("DequeBase::setAllocator -- atempt to change allocator after allocating elements.");
}
}
}
template <typename T, typename Allocator, unsigned kDequeSubarraySize>
T* DequeBase<T, Allocator, kDequeSubarraySize>::DoAllocateSubarray()
{
T* p = (T*)allocate_memory(mAllocator, kDequeSubarraySize * sizeof(T), EASTL_ALIGN_OF(T), 0);
EASTL_ASSERT_MSG(p != nullptr, "the behaviour of eastl::allocators that return nullptr is not defined.");
#if EASTL_DEBUG
memset((void*)p, 0, kDequeSubarraySize * sizeof(T));
#endif
return (T*)p;
}
template <typename T, typename Allocator, unsigned kDequeSubarraySize>
void DequeBase<T, Allocator, kDequeSubarraySize>::DoFreeSubarray(T* p)
{
if(p)
EASTLFree(mAllocator, p, kDequeSubarraySize * sizeof(T));
}
template <typename T, typename Allocator, unsigned kDequeSubarraySize>
void DequeBase<T, Allocator, kDequeSubarraySize>::DoFreeSubarrays(T** pBegin, T** pEnd)
{
while(pBegin < pEnd)
DoFreeSubarray(*pBegin++);
}
template <typename T, typename Allocator, unsigned kDequeSubarraySize>
T** DequeBase<T, Allocator, kDequeSubarraySize>::DoAllocatePtrArray(size_type n)
{
#if EASTL_ASSERT_ENABLED
if(EASTL_UNLIKELY(n >= 0x80000000))
EASTL_FAIL_MSG("deque::DoAllocatePtrArray -- improbably large request.");
#endif
T** pp = (T**)allocate_memory(mAllocator, n * sizeof(T*), EASTL_ALIGN_OF(T), 0);
EASTL_ASSERT_MSG(pp != nullptr, "the behaviour of eastl::allocators that return nullptr is not defined.");
#if EASTL_DEBUG
memset((void*)pp, 0, n * sizeof(T*));
#endif
return pp;
}
template <typename T, typename Allocator, unsigned kDequeSubarraySize>
void DequeBase<T, Allocator, kDequeSubarraySize>::DoFreePtrArray(T** pp, size_t n)
{
if(pp)
EASTLFree(mAllocator, pp, n * sizeof(T*));
}
template <typename T, typename Allocator, unsigned kDequeSubarraySize>
typename DequeBase<T, Allocator, kDequeSubarraySize>::iterator
DequeBase<T, Allocator, kDequeSubarraySize>::DoReallocSubarray(size_type nAdditionalCapacity, Side allocationSide)
{
// nAdditionalCapacity refers to the amount of additional space we need to be
// able to store in this deque. Typically this function is called as part of
// an insert or append operation. This is the function that makes sure there
// is enough capacity for the new elements to be copied into the deque.
// The new capacity here is always at the front or back of the deque.
// This function returns an iterator to that points to the new begin or
// the new end of the deque space, depending on allocationSide.
if(allocationSide == kSideFront)
{
// There might be some free space (nCurrentAdditionalCapacity) at the front of the existing subarray.
const size_type nCurrentAdditionalCapacity = (size_type)(mItBegin.mpCurrent - mItBegin.mpBegin);
if(EASTL_UNLIKELY(nCurrentAdditionalCapacity < nAdditionalCapacity)) // If we need to grow downward into a new subarray...
{
const difference_type nSubarrayIncrease = (difference_type)(((nAdditionalCapacity - nCurrentAdditionalCapacity) + kDequeSubarraySize - 1) / kDequeSubarraySize);
difference_type i;
if(nSubarrayIncrease > (mItBegin.mpCurrentArrayPtr - mpPtrArray)) // If there are not enough pointers in front of the current (first) one...
DoReallocPtrArray((size_type)(nSubarrayIncrease - (mItBegin.mpCurrentArrayPtr - mpPtrArray)), kSideFront);
#if EASTL_EXCEPTIONS_ENABLED
try
{
#endif
for(i = 1; i <= nSubarrayIncrease; ++i)
mItBegin.mpCurrentArrayPtr[-i] = DoAllocateSubarray();
#if EASTL_EXCEPTIONS_ENABLED
}
catch(...)
{
for(difference_type j = 1; j < i; ++j)
DoFreeSubarray(mItBegin.mpCurrentArrayPtr[-j]);
throw;
}
#endif
}
return mItBegin - (difference_type)nAdditionalCapacity;
}
else // else kSideBack
{
const size_type nCurrentAdditionalCapacity = (size_type)((mItEnd.mpEnd - 1) - mItEnd.mpCurrent);
if(EASTL_UNLIKELY(nCurrentAdditionalCapacity < nAdditionalCapacity)) // If we need to grow forward into a new subarray...
{
const difference_type nSubarrayIncrease = (difference_type)(((nAdditionalCapacity - nCurrentAdditionalCapacity) + kDequeSubarraySize - 1) / kDequeSubarraySize);
difference_type i;
if(nSubarrayIncrease > ((mpPtrArray + mnPtrArraySize) - mItEnd.mpCurrentArrayPtr) - 1) // If there are not enough pointers after the current (last) one...
DoReallocPtrArray((size_type)(nSubarrayIncrease - (((mpPtrArray + mnPtrArraySize) - mItEnd.mpCurrentArrayPtr) - 1)), kSideBack);
#if EASTL_EXCEPTIONS_ENABLED
try
{
#endif
for(i = 1; i <= nSubarrayIncrease; ++i)
mItEnd.mpCurrentArrayPtr[i] = DoAllocateSubarray();
#if EASTL_EXCEPTIONS_ENABLED
}
catch(...)
{
for(difference_type j = 1; j < i; ++j)
DoFreeSubarray(mItEnd.mpCurrentArrayPtr[j]);
throw;
}
#endif
}
return mItEnd + (difference_type)nAdditionalCapacity;
}
}
template <typename T, typename Allocator, unsigned kDequeSubarraySize>
void DequeBase<T, Allocator, kDequeSubarraySize>::DoReallocPtrArray(size_type nAdditionalCapacity, Side allocationSide)
{
// This function is not called unless the capacity is known to require a resize.
//
// We have an array of pointers (mpPtrArray), of which a segment of them are in use and
// at either end of the array are zero or more unused pointers. This function is being
// called because we need to extend the capacity on either side of this array by
// nAdditionalCapacity pointers. However, it's possible that if the user is continually
// using pushBack and popFront then the pointer array will continue to be extended
// on the back side and unused on the front side. So while we are doing this resizing
// here we also take the opportunity to recenter the pointers and thus be balanced.
// It man turn out that we don't even need to reallocate the pointer array in order
// to increase capacity on one side, as simply moving the pointers to the center may
// be enough to open up the requires space.
//
// Balanced pointer array Unbalanced pointer array (unused space at front, no free space at back)
// ----++++++++++++---- ---------+++++++++++
const size_type nUnusedPtrCountAtFront = (size_type)(mItBegin.mpCurrentArrayPtr - mpPtrArray);
const size_type nUsedPtrCount = (size_type)(mItEnd.mpCurrentArrayPtr - mItBegin.mpCurrentArrayPtr) + 1;
const size_type nUsedPtrSpace = nUsedPtrCount * sizeof(void*);
const size_type nUnusedPtrCountAtBack = (mnPtrArraySize - nUnusedPtrCountAtFront) - nUsedPtrCount;
value_type** pPtrArrayBegin;
if((allocationSide == kSideBack) && (nAdditionalCapacity <= nUnusedPtrCountAtFront)) // If we can take advantage of unused pointers at the front without doing any reallocation...
{
if(nAdditionalCapacity < (nUnusedPtrCountAtFront / 2)) // Possibly use more space than required, if there's a lot of extra space.
nAdditionalCapacity = (nUnusedPtrCountAtFront / 2);
pPtrArrayBegin = mpPtrArray + (nUnusedPtrCountAtFront - nAdditionalCapacity);
memmove(pPtrArrayBegin, mItBegin.mpCurrentArrayPtr, nUsedPtrSpace);
#if EASTL_DEBUG
memset(pPtrArrayBegin + nUsedPtrCount, 0, (size_t)(mpPtrArray + mnPtrArraySize) - (size_t)(pPtrArrayBegin + nUsedPtrCount));
#endif
}
else if((allocationSide == kSideFront) && (nAdditionalCapacity <= nUnusedPtrCountAtBack)) // If we can take advantage of unused pointers at the back without doing any reallocation...
{
if(nAdditionalCapacity < (nUnusedPtrCountAtBack / 2)) // Possibly use more space than required, if there's a lot of extra space.
nAdditionalCapacity = (nUnusedPtrCountAtBack / 2);
pPtrArrayBegin = mItBegin.mpCurrentArrayPtr + nAdditionalCapacity;
memmove(pPtrArrayBegin, mItBegin.mpCurrentArrayPtr, nUsedPtrSpace);
#if EASTL_DEBUG
memset(mpPtrArray, 0, (size_t)((uintptr_t)pPtrArrayBegin - (uintptr_t)mpPtrArray));
#endif
}
else
{
// In this case we will have to do a reallocation.
const size_type nNewPtrArraySize = mnPtrArraySize + eastl::maxAlt(mnPtrArraySize, nAdditionalCapacity) + 2; // Allocate extra capacity.
value_type** const pNewPtrArray = DoAllocatePtrArray(nNewPtrArraySize);
pPtrArrayBegin = pNewPtrArray + (mItBegin.mpCurrentArrayPtr - mpPtrArray) + ((allocationSide == kSideFront) ? nAdditionalCapacity : 0);
// The following is equivalent to: eastl::copy(mItBegin.mpCurrentArrayPtr, mItEnd.mpCurrentArrayPtr + 1, pPtrArrayBegin);
// It's OK to use memcpy instead of memmove because the destination is guaranteed to non-overlap the source.
if(mpPtrArray) // Could also say: 'if(mItBegin.mpCurrentArrayPtr)'
memcpy(pPtrArrayBegin, mItBegin.mpCurrentArrayPtr, nUsedPtrSpace);
DoFreePtrArray(mpPtrArray, mnPtrArraySize);
mpPtrArray = pNewPtrArray;
mnPtrArraySize = nNewPtrArraySize;
}
// We need to reset the begin and end iterators, as code that calls this expects them to *not* be invalidated.
mItBegin.SetSubarray(pPtrArrayBegin);
mItEnd.SetSubarray((pPtrArrayBegin + nUsedPtrCount) - 1);
}
template <typename T, typename Allocator, unsigned kDequeSubarraySize>
void DequeBase<T, Allocator, kDequeSubarraySize>::DoInit(size_type n)
{
// This code is disabled because it doesn't currently work properly.
// We are trying to make it so that a deque can have a zero allocation
// initial empty state, but we (OK, I) am having a hard time making
// this elegant and efficient.
//if(n)
//{
const size_type nNewPtrArraySize = (size_type)((n / kDequeSubarraySize) + 1); // Always have at least one, even if n is zero.
const size_type kMinPtrArraySize_ = kMinPtrArraySize;
mnPtrArraySize = eastl::maxAlt(kMinPtrArraySize_, (nNewPtrArraySize + 2));
mpPtrArray = DoAllocatePtrArray(mnPtrArraySize);
value_type** const pPtrArrayBegin = (mpPtrArray + ((mnPtrArraySize - nNewPtrArraySize) / 2)); // Try to place it in the middle.
value_type** const pPtrArrayEnd = pPtrArrayBegin + nNewPtrArraySize;
value_type** pPtrArrayCurrent = pPtrArrayBegin;
#if EASTL_EXCEPTIONS_ENABLED
try
{
try
{
#endif
while(pPtrArrayCurrent < pPtrArrayEnd)
*pPtrArrayCurrent++ = DoAllocateSubarray();
#if EASTL_EXCEPTIONS_ENABLED
}
catch(...)
{
DoFreeSubarrays(pPtrArrayBegin, pPtrArrayCurrent);
throw;
}
}
catch(...)
{
DoFreePtrArray(mpPtrArray, mnPtrArraySize);
mpPtrArray = NULL;
mnPtrArraySize = 0;
throw;
}
#endif
mItBegin.SetSubarray(pPtrArrayBegin);
mItBegin.mpCurrent = mItBegin.mpBegin;
mItEnd.SetSubarray(pPtrArrayEnd - 1);
mItEnd.mpCurrent = mItEnd.mpBegin + (difference_type)(n % kDequeSubarraySize);
//}
//else // Else we do a zero-allocation initialization.
//{
// mpPtrArray = NULL;
// mnPtrArraySize = 0;
//
// mItBegin.mpCurrentArrayPtr = NULL;
// mItBegin.mpBegin = NULL;
// mItBegin.mpEnd = NULL; // We intentionally create a situation whereby the subarray that has no capacity.
// mItBegin.mpCurrent = NULL;
//
// mItEnd = mItBegin;
//}
}
///////////////////////////////////////////////////////////////////////
// DequeIterator
///////////////////////////////////////////////////////////////////////
template <typename T, typename Pointer, typename Reference, unsigned kDequeSubarraySize>
DequeIterator<T, Pointer, Reference, kDequeSubarraySize>::DequeIterator()
: mpCurrent(NULL), mpBegin(NULL), mpEnd(NULL), mpCurrentArrayPtr(NULL)
{
// Empty
}
template <typename T, typename Pointer, typename Reference, unsigned kDequeSubarraySize>
DequeIterator<T, Pointer, Reference, kDequeSubarraySize>::DequeIterator(T** pCurrentArrayPtr, T* pCurrent)
: mpCurrent(pCurrent), mpBegin(*pCurrentArrayPtr), mpEnd(pCurrent + kDequeSubarraySize), mpCurrentArrayPtr(pCurrentArrayPtr)
{
// Empty
}
template <typename T, typename Pointer, typename Reference, unsigned kDequeSubarraySize>
DequeIterator<T, Pointer, Reference, kDequeSubarraySize>::DequeIterator(const iterator& x)
: mpCurrent(x.mpCurrent), mpBegin(x.mpBegin), mpEnd(x.mpEnd), mpCurrentArrayPtr(x.mpCurrentArrayPtr)
{
// Empty
}
template <typename T, typename Pointer, typename Reference, unsigned kDequeSubarraySize>
DequeIterator<T, Pointer, Reference, kDequeSubarraySize>::DequeIterator(const iterator& x, Increment)
: mpCurrent(x.mpCurrent), mpBegin(x.mpBegin), mpEnd(x.mpEnd), mpCurrentArrayPtr(x.mpCurrentArrayPtr)
{
operator++();
}
template <typename T, typename Pointer, typename Reference, unsigned kDequeSubarraySize>
DequeIterator<T, Pointer, Reference, kDequeSubarraySize>::DequeIterator(const iterator& x, Decrement)
: mpCurrent(x.mpCurrent), mpBegin(x.mpBegin), mpEnd(x.mpEnd), mpCurrentArrayPtr(x.mpCurrentArrayPtr)
{
operator--();
}
template <typename T, typename Pointer, typename Reference, unsigned kDequeSubarraySize>
typename DequeIterator<T, Pointer, Reference, kDequeSubarraySize>::pointer
DequeIterator<T, Pointer, Reference, kDequeSubarraySize>::operator->() const
{
return mpCurrent;
}
template <typename T, typename Pointer, typename Reference, unsigned kDequeSubarraySize>
typename DequeIterator<T, Pointer, Reference, kDequeSubarraySize>::reference
DequeIterator<T, Pointer, Reference, kDequeSubarraySize>::operator*() const
{
return *mpCurrent;
}
template <typename T, typename Pointer, typename Reference, unsigned kDequeSubarraySize>
typename DequeIterator<T, Pointer, Reference, kDequeSubarraySize>::this_type&
DequeIterator<T, Pointer, Reference, kDequeSubarraySize>::operator++()
{
if(EASTL_UNLIKELY(++mpCurrent == mpEnd))
{
mpBegin = *++mpCurrentArrayPtr;
mpEnd = mpBegin + kDequeSubarraySize;
mpCurrent = mpBegin;
}
return *this;
}
template <typename T, typename Pointer, typename Reference, unsigned kDequeSubarraySize>
typename DequeIterator<T, Pointer, Reference, kDequeSubarraySize>::this_type
DequeIterator<T, Pointer, Reference, kDequeSubarraySize>::operator++(int)
{
const this_type temp(*this);
operator++();
return temp;
}
template <typename T, typename Pointer, typename Reference, unsigned kDequeSubarraySize>
typename DequeIterator<T, Pointer, Reference, kDequeSubarraySize>::this_type&
DequeIterator<T, Pointer, Reference, kDequeSubarraySize>::operator--()
{
if(EASTL_UNLIKELY(mpCurrent == mpBegin))
{
mpBegin = *--mpCurrentArrayPtr;
mpEnd = mpBegin + kDequeSubarraySize;
mpCurrent = mpEnd; // fall through...
}
--mpCurrent;
return *this;
}
template <typename T, typename Pointer, typename Reference, unsigned kDequeSubarraySize>
typename DequeIterator<T, Pointer, Reference, kDequeSubarraySize>::this_type
DequeIterator<T, Pointer, Reference, kDequeSubarraySize>::operator--(int)