-
Notifications
You must be signed in to change notification settings - Fork 7
/
vector.h
2097 lines (1687 loc) · 74.5 KB
/
vector.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.
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// This file implements a vector (array-like container), much like the C++
// std::vector class.
// The primary distinctions between this vector and std::vector are:
// - vector has a couple extension functions that increase performance.
// - vector can contain objects with alignment requirements. std::vector
// cannot do so without a bit of tedious non-portable effort.
// - vector supports debug memory naming natively.
// - vector is easier to read, debug, and visualize.
// - vector is savvy to an environment that doesn't have exception handling,
// as is sometimes the case with console or embedded environments.
// - vector has less deeply nested function calls and allows the user to
// enable forced inlining in debug builds in order to reduce bloat.
// - vector<bool> is a vector of boolean values and not a bit vector.
// - vector guarantees that memory is contiguous and that vector::iterator
// is nothing more than a pointer to T.
// - vector has an explicit data() method for obtaining a pointer to storage
// which is safe to call even if the block is empty. This avoids the
// common &v[0], &v.front(), and &*v.begin() constructs that trigger false
// asserts in STL debugging modes.
// - vector data is guaranteed to be contiguous.
// - vector has a setCapacity() function which frees excess capacity.
// The only way to do this with std::vector is via the cryptic non-obvious
// trick of using: vector<SomeClass>(x).swap(x);
///////////////////////////////////////////////////////////////////////////////
#ifndef EASTL_VECTOR_H
#define EASTL_VECTOR_H
#include <eastl/internal/config.h>
#include <eastl/allocator.h>
#include <eastl/type_traits.h>
#include <eastl/iterator.h>
#include <eastl/algorithm.h>
#include <eastl/initializer_list.h>
#include <eastl/memory.h>
#include <eastl/bonus/compressed_pair.h>
EA_DISABLE_ALL_VC_WARNINGS()
#include <new>
#include <stddef.h>
#if EASTL_EXCEPTIONS_ENABLED
#include <stdexcept> // std::out_of_range, std::length_error.
#endif
EA_RESTORE_ALL_VC_WARNINGS()
// 4530 - C++ exception handler used, but unwind semantics are not enabled. Specify /EHsc
// 4480 - nonstandard extension used: specifying underlying type for enum
// 4571 - catch(...) semantics changed since Visual C++ 7.1; structured exceptions (SEH) are no longer caught.
EA_DISABLE_VC_WARNING(4530 4480 4571);
// 4345 - Behavior change: an object of POD type constructed with an initializer of the form () will be default-initialized
// 4244 - Argument: conversion from 'int' to 'const eastl::vector<T>::value_type', possible loss of data
// 4127 - Conditional expression is constant
EA_DISABLE_VC_WARNING(4345 4244 4127);
#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
#if EASTL_NOMINMAX
#ifdef min
#undef min
#endif
#ifdef max
#undef max
#endif
#endif
namespace eastl
{
/// EASTL_VECTOR_DEFAULT_NAME
///
/// Defines a default container name in the absence of a user-provided name.
///
#ifndef EASTL_VECTOR_DEFAULT_NAME
#define EASTL_VECTOR_DEFAULT_NAME EASTL_DEFAULT_NAME_PREFIX " vector" // Unless the user overrides something, this is "EASTL vector".
#endif
/// EASTL_VECTOR_DEFAULT_ALLOCATOR
///
#ifndef EASTL_VECTOR_DEFAULT_ALLOCATOR
#define EASTL_VECTOR_DEFAULT_ALLOCATOR allocator_type(EASTL_VECTOR_DEFAULT_NAME)
#endif
/// VectorBase
///
/// The reason we have a VectorBase class is that it makes exception handling
/// simpler to implement because memory allocation is implemented entirely
/// in this class. If a user creates a vector which needs to allocate
/// memory in the constructor, VectorBase handles it. If an exception is thrown
/// by the allocator then the exception throw jumps back to the user code and
/// no try/catch code need be written in the vector or VectorBase constructor.
/// If an exception is thrown in the vector (not VectorBase) constructor, the
/// destructor for VectorBase will be called automatically (and free the allocated
/// memory) before the execution jumps back to the user code.
/// However, if the vector class were to handle both allocation and initialization
/// then it would have no choice but to implement an explicit try/catch statement
/// for all pathways that allocate memory. This increases code size and decreases
/// performance and makes the code a little harder read and maintain.
///
/// The C++ standard (15.2 paragraph 2) states:
/// "An object that is partially constructed or partially destroyed will
/// have destructors executed for all its fully constructed subobjects,
/// that is, for subobjects for which the constructor has been completed
/// execution and the destructor has not yet begun execution."
///
/// The C++ standard (15.3 paragraph 11) states:
/// "The fully constructed base classes and members of an object shall
/// be destroyed before entering the handler of a function-try-block
/// of a constructor or destructor for that block."
///
template <typename T, typename Allocator>
struct VectorBase
{
typedef Allocator allocator_type;
typedef eastl_size_t size_type;
typedef ptrdiff_t difference_type;
#if defined(_MSC_VER) && (_MSC_VER >= 1400) && (_MSC_VER <= 1600) && !EASTL_STD_CPP_ONLY // _MSC_VER of 1400 means VS2005, 1600 means VS2010. VS2012 generates errors with usage of enum:size_type.
enum : size_type { // Use Microsoft enum language extension, allowing for smaller debug symbols than using a static const. Users have been affected by this.
npos = (size_type)-1,
kMaxSize = (size_type)-2
};
#else
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.
#endif
size_type GetNewCapacity(size_type currentCapacity);
protected:
T* mpBegin;
T* mpEnd;
eastl::compressed_pair<T*, allocator_type> mCapacityAllocator;
T*& internalCapacityPtr() EASTL_NOEXCEPT { return mCapacityAllocator.first(); }
T* const& internalCapacityPtr() const EASTL_NOEXCEPT { return mCapacityAllocator.first(); }
allocator_type& internalAllocator() EASTL_NOEXCEPT { return mCapacityAllocator.second(); }
const allocator_type& internalAllocator() const EASTL_NOEXCEPT { return mCapacityAllocator.second(); }
public:
VectorBase();
VectorBase(const allocator_type& allocator);
VectorBase(size_type n, const allocator_type& allocator);
~VectorBase();
const allocator_type& getAllocator() const EASTL_NOEXCEPT;
allocator_type& getAllocator() EASTL_NOEXCEPT;
void setAllocator(const allocator_type& allocator);
protected:
T* DoAllocate(size_type n);
void DoFree(T* p, size_type n);
}; // VectorBase
/// vector
///
/// Implements a dynamic array.
///
template <typename T, typename Allocator = EASTLAllocatorType>
class vector : public VectorBase<T, Allocator>
{
typedef VectorBase<T, Allocator> base_type;
typedef vector<T, Allocator> this_type;
protected:
using base_type::mpBegin;
using base_type::mpEnd;
using base_type::mCapacityAllocator;
using base_type::DoAllocate;
using base_type::DoFree;
using base_type::internalCapacityPtr;
using base_type::internalAllocator;
public:
typedef T value_type;
typedef T* pointer;
typedef const T* const_pointer;
typedef T& reference;
typedef const T& const_reference; // Maintainer note: We want to leave iterator defined as T* -- at least in release builds -- as this gives some algorithms an advantage that optimizers cannot get around.
typedef T* iterator; // Note: iterator is simply T* right now, but this will likely change in the future, at least for debug builds.
typedef const T* const_iterator; // Do not write code that relies on iterator being T*. The reason it will
typedef eastl::reverse_iterator<iterator> reverse_iterator; // change in the future is that a debugging iterator system will be created.
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;
using base_type::GetNewCapacity;
#if EA_IS_ENABLED(EASTL_DEPRECATIONS_FOR_2024_APRIL)
static_assert(!is_const<value_type>::value, "vector<T> value_type must be non-const.");
static_assert(!is_volatile<value_type>::value, "vector<T> value_type must be non-volatile.");
#endif
public:
vector() EASTL_NOEXCEPT_IF(EASTL_NOEXCEPT_EXPR(EASTL_VECTOR_DEFAULT_ALLOCATOR));
explicit vector(const allocator_type& allocator) EASTL_NOEXCEPT;
explicit vector(size_type n, const allocator_type& allocator = EASTL_VECTOR_DEFAULT_ALLOCATOR);
vector(size_type n, const value_type& value, const allocator_type& allocator = EASTL_VECTOR_DEFAULT_ALLOCATOR);
vector(const this_type& x);
vector(const this_type& x, const allocator_type& allocator);
vector(this_type&& x) EASTL_NOEXCEPT;
vector(this_type&& x, const allocator_type& allocator);
vector(std::initializer_list<value_type> ilist, const allocator_type& allocator = EASTL_VECTOR_DEFAULT_ALLOCATOR);
// note: this has pre-C++11 semantics:
// this constructor is equivalent to the constructor vector(static_cast<size_type>(first), static_cast<value_type>(last), allocator) if InputIterator is an integral type.
template <typename InputIterator>
vector(InputIterator first, InputIterator last, const allocator_type& allocator = EASTL_VECTOR_DEFAULT_ALLOCATOR);
~vector();
this_type& operator=(const this_type& x);
this_type& operator=(std::initializer_list<value_type> ilist);
this_type& operator=(this_type&& x); // TODO(c++17): noexcept(allocator_traits<Allocator>::propagate_on_container_move_assignment::value || allocator_traits<Allocator>::is_always_equal::value)
void swap(this_type& x); // TODO(c++17): noexcept(allocator_traits<Allocator>::propagate_on_container_move_assignment::value || allocator_traits<Allocator>::is_always_equal::value)
void assign(size_type n, const value_type& value);
template <typename InputIterator>
void assign(InputIterator first, InputIterator last);
void assign(std::initializer_list<value_type> ilist);
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;
size_type capacity() const EASTL_NOEXCEPT;
void resize(size_type n, const value_type& value);
void resize(size_type n);
void reserve(size_type n);
void setCapacity(size_type n = base_type::npos); // Revises the capacity to the user-specified value. Resizes the container to match the capacity if the requested capacity n is less than the current size. If n == npos then the capacity is reallocated (if necessary) such that capacity == size.
void shrink_to_fit(); // C++11 function which is the same as setCapacity().
pointer data() EASTL_NOEXCEPT;
const_pointer data() const EASTL_NOEXCEPT;
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 pushBack(const value_type& value);
reference pushBack();
void* pushBackUninitialized();
void pushBack(value_type&& value);
void popBack();
template<class... Args>
iterator emplace(const_iterator position, Args&&... args);
template<class... Args>
reference emplace_back(Args&&... args);
iterator insert(const_iterator position, const value_type& value);
iterator insert(const_iterator position, size_type n, const value_type& value);
iterator insert(const_iterator position, 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_first(const T& value);
iterator erase_first_unsorted(const T& value); // Same as erase, except it doesn't preserve order, but is faster because it simply copies the last item in the vector over the erased position.
reverse_iterator erase_last(const T& value);
reverse_iterator erase_last_unsorted(const T& value); // Same as erase, except it doesn't preserve order, but is faster because it simply copies the last item in the vector over the erased position.
iterator erase(const_iterator position);
iterator erase(const_iterator first, const_iterator last);
iterator erase_unsorted(const_iterator position); // Same as erase, except it doesn't preserve order, but is faster because it simply copies the last item in the vector over the erased position.
reverse_iterator erase(const_reverse_iterator position);
reverse_iterator erase(const_reverse_iterator first, const_reverse_iterator last);
reverse_iterator erase_unsorted(const_reverse_iterator position);
void clear() EASTL_NOEXCEPT;
void reset_lose_memory() EASTL_NOEXCEPT; // This is a unilateral reset to an initially empty state. No destructors are called, no deallocation occurs.
bool validate() const EASTL_NOEXCEPT;
int validateIterator(const_iterator i) const EASTL_NOEXCEPT;
protected:
// These functions do the real work of maintaining the vector. You will notice
// that many of them have the same name but are specialized on iterator_tag
// (iterator categories). This is because in these cases there is an optimized
// implementation that can be had for some cases relative to others. Functions
// which aren't referenced are neither compiled nor linked into the application.
template <bool bMove> struct should_move_or_copy_tag{};
using should_copy_tag = should_move_or_copy_tag<false>;
using should_move_tag = should_move_or_copy_tag<true>;
template <typename ForwardIterator> // Allocates a pointer of array count n and copy-constructs it with [first,last).
pointer DoRealloc(size_type n, ForwardIterator first, ForwardIterator last, should_copy_tag);
template <typename ForwardIterator> // Allocates a pointer of array count n and copy-constructs it with [first,last).
pointer DoRealloc(size_type n, ForwardIterator first, ForwardIterator last, should_move_tag);
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);
template <typename Integer, bool bMove>
void DoAssign(Integer n, Integer value, true_type);
template <typename InputIterator, bool bMove>
void DoAssign(InputIterator first, InputIterator last, false_type);
void DoAssignValues(size_type n, const value_type& value);
template <typename InputIterator, bool bMove>
void DoAssignFromIterator(InputIterator first, InputIterator last, EASTL_ITC_NS::input_iterator_tag);
template <typename RandomAccessIterator, bool bMove>
void DoAssignFromIterator(RandomAccessIterator first, RandomAccessIterator last, EASTL_ITC_NS::random_access_iterator_tag);
template <typename Integer>
void DoInsert(const_iterator position, Integer n, Integer value, true_type);
template <typename InputIterator>
void DoInsert(const_iterator position, InputIterator first, InputIterator last, false_type);
template <typename InputIterator>
void DoInsertFromIterator(const_iterator position, InputIterator first, InputIterator last, EASTL_ITC_NS::input_iterator_tag);
template <typename BidirectionalIterator>
void DoInsertFromIterator(const_iterator position, BidirectionalIterator first, BidirectionalIterator last, EASTL_ITC_NS::bidirectional_iterator_tag);
void DoInsertValues(const_iterator position, size_type n, const value_type& value);
void DoInsertValuesEnd(size_type n); // Default constructs n values
void DoInsertValuesEnd(size_type n, const value_type& value);
template<typename... Args>
void DoInsertValue(const_iterator position, Args&&... args);
template<typename... Args>
void DoInsertValueEnd(Args&&... args);
void DoClearCapacity();
void DoGrow(size_type n);
void DoSwap(this_type& x);
}; // class vector
///////////////////////////////////////////////////////////////////////
// VectorBase
///////////////////////////////////////////////////////////////////////
template <typename T, typename Allocator>
inline VectorBase<T, Allocator>::VectorBase()
: mpBegin(NULL),
mpEnd(NULL),
mCapacityAllocator(NULL, allocator_type(EASTL_VECTOR_DEFAULT_NAME))
{
}
template <typename T, typename Allocator>
inline VectorBase<T, Allocator>::VectorBase(const allocator_type& allocator)
: mpBegin(NULL),
mpEnd(NULL),
mCapacityAllocator(NULL, allocator)
{
}
template <typename T, typename Allocator>
inline VectorBase<T, Allocator>::VectorBase(size_type n, const allocator_type& allocator)
: mCapacityAllocator(allocator)
{
mpBegin = DoAllocate(n);
mpEnd = mpBegin;
internalCapacityPtr() = mpBegin + n;
}
template <typename T, typename Allocator>
inline VectorBase<T, Allocator>::~VectorBase()
{
if(mpBegin)
EASTLFree(internalAllocator(), mpBegin, (internalCapacityPtr() - mpBegin) * sizeof(T));
}
template <typename T, typename Allocator>
inline const typename VectorBase<T, Allocator>::allocator_type&
VectorBase<T, Allocator>::getAllocator() const EASTL_NOEXCEPT
{
return internalAllocator();
}
template <typename T, typename Allocator>
inline typename VectorBase<T, Allocator>::allocator_type&
VectorBase<T, Allocator>::getAllocator() EASTL_NOEXCEPT
{
return internalAllocator();
}
template <typename T, typename Allocator>
inline void VectorBase<T, Allocator>::setAllocator(const allocator_type& allocator)
{
internalAllocator() = allocator;
}
template <typename T, typename Allocator>
inline T* VectorBase<T, Allocator>::DoAllocate(size_type n)
{
#if EASTL_ASSERT_ENABLED
if(EASTL_UNLIKELY(n >= 0x80000000))
EASTL_FAIL_MSG("vector::DoAllocate -- improbably large request.");
#endif
// If n is zero, then we allocate no memory and just return nullptr.
// This is fine, as our default ctor initializes with NULL pointers.
if(EASTL_LIKELY(n))
{
auto* p = (T*)allocate_memory(internalAllocator(), n * sizeof(T), EASTL_ALIGN_OF(T), 0);
EASTL_ASSERT_MSG(p != nullptr, "the behaviour of eastl::allocators that return nullptr is not defined.");
return p;
}
else
{
return nullptr;
}
}
template <typename T, typename Allocator>
inline void VectorBase<T, Allocator>::DoFree(T* p, size_type n)
{
if(p)
EASTLFree(internalAllocator(), p, n * sizeof(T));
}
template <typename T, typename Allocator>
inline typename VectorBase<T, Allocator>::size_type
VectorBase<T, Allocator>::GetNewCapacity(size_type currentCapacity)
{
// This needs to return a value of at least currentCapacity and at least 1.
return (currentCapacity > 0) ? (size_type)(1.618f * (float)currentCapacity) : 1;
}
///////////////////////////////////////////////////////////////////////
// vector
///////////////////////////////////////////////////////////////////////
template <typename T, typename Allocator>
inline vector<T, Allocator>::vector() EASTL_NOEXCEPT_IF(EASTL_NOEXCEPT_EXPR(EASTL_VECTOR_DEFAULT_ALLOCATOR))
: base_type()
{
// Empty
}
template <typename T, typename Allocator>
inline vector<T, Allocator>::vector(const allocator_type& allocator) EASTL_NOEXCEPT
: base_type(allocator)
{
// Empty
}
template <typename T, typename Allocator>
inline vector<T, Allocator>::vector(size_type n, const allocator_type& allocator)
: base_type(n, allocator)
{
eastl::uninitialized_value_construct_n(mpBegin, n);
mpEnd = mpBegin + n;
}
template <typename T, typename Allocator>
inline vector<T, Allocator>::vector(size_type n, const value_type& value, const allocator_type& allocator)
: base_type(n, allocator)
{
eastl::uninitializedFillNPtr(mpBegin, n, value);
mpEnd = mpBegin + n;
}
template <typename T, typename Allocator>
inline vector<T, Allocator>::vector(const this_type& x)
: base_type(x.size(), x.internalAllocator())
{
mpEnd = eastl::uninitializedCopyPtr(x.mpBegin, x.mpEnd, mpBegin);
}
template <typename T, typename Allocator>
inline vector<T, Allocator>::vector(const this_type& x, const allocator_type& allocator)
: base_type(x.size(), allocator)
{
mpEnd = eastl::uninitializedCopyPtr(x.mpBegin, x.mpEnd, mpBegin);
}
template <typename T, typename Allocator>
inline vector<T, Allocator>::vector(this_type&& x) EASTL_NOEXCEPT
: base_type(eastl::move(x.internalAllocator())) // vector requires move-construction of allocator in this case.
{
DoSwap(x);
}
template <typename T, typename Allocator>
inline vector<T, Allocator>::vector(this_type&& x, const allocator_type& allocator)
: base_type(allocator)
{
if (internalAllocator() == x.internalAllocator()) // If allocators are equivalent...
DoSwap(x);
else
{
this_type temp(eastl::move(*this)); // move construct so we don't require the use of copy-ctors that prevent the use of move-only types.
temp.swap(x);
}
}
template <typename T, typename Allocator>
inline vector<T, Allocator>::vector(std::initializer_list<value_type> ilist, const allocator_type& allocator)
: base_type(allocator)
{
DoInit(ilist.begin(), ilist.end(), false_type());
}
template <typename T, typename Allocator>
template <typename InputIterator>
inline vector<T, Allocator>::vector(InputIterator first, InputIterator last, const allocator_type& allocator)
: base_type(allocator)
{
DoInit(first, last, is_integral<InputIterator>());
}
template <typename T, typename Allocator>
inline vector<T, Allocator>::~vector()
{
// Call destructor for the values. Parent class will free the memory.
eastl::destruct(mpBegin, mpEnd);
}
template <typename T, typename Allocator>
typename vector<T, Allocator>::this_type&
vector<T, Allocator>::operator=(const this_type& x)
{
if(this != &x) // If not assigning to self...
{
// If (EASTL_ALLOCATOR_COPY_ENABLED == 1) and the current contents are allocated by an
// allocator that's unequal to x's allocator, we need to reallocate our elements with
// our current allocator and reallocate it with x's allocator. If the allocators are
// equal then we can use a more optimal algorithm that doesn't reallocate our elements
// but instead can copy them in place.
#if EASTL_ALLOCATOR_COPY_ENABLED
bool bSlowerPathwayRequired = (internalAllocator() != x.internalAllocator());
#else
bool bSlowerPathwayRequired = false;
#endif
if(bSlowerPathwayRequired)
{
DoClearCapacity(); // Must clear the capacity instead of clear because setCapacity frees our memory, unlike clear.
#if EASTL_ALLOCATOR_COPY_ENABLED
internalAllocator() = x.internalAllocator();
#endif
}
DoAssign<const_iterator, false>(x.begin(), x.end(), eastl::false_type());
}
return *this;
}
template <typename T, typename Allocator>
typename vector<T, Allocator>::this_type&
vector<T, Allocator>::operator=(std::initializer_list<value_type> ilist)
{
typedef typename std::initializer_list<value_type>::iterator InputIterator;
typedef typename eastl::iterator_traits<InputIterator>::iterator_category IC;
DoAssignFromIterator<InputIterator, false>(ilist.begin(), ilist.end(), IC()); // initializer_list has const elements and so we can't move from them.
return *this;
}
template <typename T, typename Allocator>
typename vector<T, Allocator>::this_type&
vector<T, Allocator>::operator=(this_type&& x)
{
if(this != &x)
{
DoClearCapacity(); // To consider: Are we really required to clear here? x is going away soon and will clear itself in its dtor.
swap(x); // member swap handles the case that x has a different allocator than our allocator by doing a copy.
}
return *this;
}
template <typename T, typename Allocator>
inline void vector<T, Allocator>::assign(size_type n, const value_type& value)
{
DoAssignValues(n, value);
}
template <typename T, typename Allocator>
template <typename InputIterator>
inline void vector<T, Allocator>::assign(InputIterator first, InputIterator last)
{
// It turns out that the C++ std::vector<int, int> specifies a two argument
// 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.
DoAssign<InputIterator, false>(first, last, is_integral<InputIterator>());
}
template <typename T, typename Allocator>
inline void vector<T, Allocator>::assign(std::initializer_list<value_type> ilist)
{
typedef typename std::initializer_list<value_type>::iterator InputIterator;
typedef typename eastl::iterator_traits<InputIterator>::iterator_category IC;
DoAssignFromIterator<InputIterator, false>(ilist.begin(), ilist.end(), IC()); // initializer_list has const elements and so we can't move from them.
}
template <typename T, typename Allocator>
inline typename vector<T, Allocator>::iterator
vector<T, Allocator>::begin() EASTL_NOEXCEPT
{
return mpBegin;
}
template <typename T, typename Allocator>
inline typename vector<T, Allocator>::const_iterator
vector<T, Allocator>::begin() const EASTL_NOEXCEPT
{
return mpBegin;
}
template <typename T, typename Allocator>
inline typename vector<T, Allocator>::const_iterator
vector<T, Allocator>::cbegin() const EASTL_NOEXCEPT
{
return mpBegin;
}
template <typename T, typename Allocator>
inline typename vector<T, Allocator>::iterator
vector<T, Allocator>::end() EASTL_NOEXCEPT
{
return mpEnd;
}
template <typename T, typename Allocator>
inline typename vector<T, Allocator>::const_iterator
vector<T, Allocator>::end() const EASTL_NOEXCEPT
{
return mpEnd;
}
template <typename T, typename Allocator>
inline typename vector<T, Allocator>::const_iterator
vector<T, Allocator>::cend() const EASTL_NOEXCEPT
{
return mpEnd;
}
template <typename T, typename Allocator>
inline typename vector<T, Allocator>::reverse_iterator
vector<T, Allocator>::rbegin() EASTL_NOEXCEPT
{
return reverse_iterator(mpEnd);
}
template <typename T, typename Allocator>
inline typename vector<T, Allocator>::const_reverse_iterator
vector<T, Allocator>::rbegin() const EASTL_NOEXCEPT
{
return const_reverse_iterator(mpEnd);
}
template <typename T, typename Allocator>
inline typename vector<T, Allocator>::const_reverse_iterator
vector<T, Allocator>::crbegin() const EASTL_NOEXCEPT
{
return const_reverse_iterator(mpEnd);
}
template <typename T, typename Allocator>
inline typename vector<T, Allocator>::reverse_iterator
vector<T, Allocator>::rend() EASTL_NOEXCEPT
{
return reverse_iterator(mpBegin);
}
template <typename T, typename Allocator>
inline typename vector<T, Allocator>::const_reverse_iterator
vector<T, Allocator>::rend() const EASTL_NOEXCEPT
{
return const_reverse_iterator(mpBegin);
}
template <typename T, typename Allocator>
inline typename vector<T, Allocator>::const_reverse_iterator
vector<T, Allocator>::crend() const EASTL_NOEXCEPT
{
return const_reverse_iterator(mpBegin);
}
template <typename T, typename Allocator>
bool vector<T, Allocator>::empty() const EASTL_NOEXCEPT
{
return (mpBegin == mpEnd);
}
template <typename T, typename Allocator>
inline typename vector<T, Allocator>::size_type
vector<T, Allocator>::size() const EASTL_NOEXCEPT
{
return (size_type)(mpEnd - mpBegin);
}
template <typename T, typename Allocator>
inline typename vector<T, Allocator>::size_type
vector<T, Allocator>::capacity() const EASTL_NOEXCEPT
{
return (size_type)(internalCapacityPtr() - mpBegin);
}
template <typename T, typename Allocator>
inline void vector<T, Allocator>::resize(size_type n, const value_type& value)
{
if(n > (size_type)(mpEnd - mpBegin)) // We expect that more often than not, resizes will be upsizes.
DoInsertValuesEnd(n - ((size_type)(mpEnd - mpBegin)), value);
else
{
eastl::destruct(mpBegin + n, mpEnd);
mpEnd = mpBegin + n;
}
}
template <typename T, typename Allocator>
inline void vector<T, Allocator>::resize(size_type n)
{
// Alternative implementation:
// resize(n, value_type());
if(n > (size_type)(mpEnd - mpBegin)) // We expect that more often than not, resizes will be upsizes.
DoInsertValuesEnd(n - ((size_type)(mpEnd - mpBegin)));
else
{
eastl::destruct(mpBegin + n, mpEnd);
mpEnd = mpBegin + n;
}
}
template <typename T, typename Allocator>
void vector<T, Allocator>::reserve(size_type n)
{
// If the user wants to reduce the reserved memory, there is the setCapacity function.
if(n > size_type(internalCapacityPtr() - mpBegin)) // If n > capacity ...
DoGrow(n);
}
template <typename T, typename Allocator>
void vector<T, Allocator>::setCapacity(size_type n)
{
if((n == npos) || (n <= (size_type)(mpEnd - mpBegin))) // If new capacity <= size...
{
if(n == 0) // Very often n will be 0, and clear will be faster than resize and use less stack space.
clear();
else if(n < (size_type)(mpEnd - mpBegin))
resize(n);
shrink_to_fit();
}
else // Else new capacity > size.
{
pointer const pNewData = DoRealloc(n, mpBegin, mpEnd, should_move_tag());
eastl::destruct(mpBegin, mpEnd);
DoFree(mpBegin, (size_type)(internalCapacityPtr() - mpBegin));
const ptrdiff_t nPrevSize = mpEnd - mpBegin;
mpBegin = pNewData;
mpEnd = pNewData + nPrevSize;
internalCapacityPtr() = mpBegin + n;
}
}
template <typename T, typename Allocator>
inline void vector<T, Allocator>::shrink_to_fit()
{
// This is the simplest way to accomplish this, and it is as efficient as any other.
this_type temp = this_type(move_iterator<iterator>(begin()), move_iterator<iterator>(end()), internalAllocator());
// Call DoSwap() rather than swap() as we know our allocators match and we don't want to invoke the code path
// handling non matching allocators as it imposes additional restrictions on the type of T to be copyable
DoSwap(temp);
}
template <typename T, typename Allocator>
inline typename vector<T, Allocator>::pointer
vector<T, Allocator>::data() EASTL_NOEXCEPT
{
return mpBegin;
}
template <typename T, typename Allocator>
inline typename vector<T, Allocator>::const_pointer
vector<T, Allocator>::data() const EASTL_NOEXCEPT
{
return mpBegin;
}
template <typename T, typename Allocator>
inline typename vector<T, Allocator>::reference
vector<T, Allocator>::operator[](size_type n)
{
#if EASTL_ASSERT_ENABLED && EASTL_EMPTY_REFERENCE_ASSERT_ENABLED
if (EASTL_UNLIKELY(n >= (static_cast<size_type>(mpEnd - mpBegin))))
EASTL_FAIL_MSG("vector::operator[] -- out of range");
#elif EASTL_ASSERT_ENABLED
// We allow the user to use a reference to v[0] of an empty container. But this was merely grandfathered in and ideally we shouldn't allow such access to [0].
if (EASTL_UNLIKELY((n != 0) && (n >= (static_cast<size_type>(mpEnd - mpBegin)))))
EASTL_FAIL_MSG("vector::operator[] -- out of range");
#endif
return *(mpBegin + n);
}
template <typename T, typename Allocator>
inline typename vector<T, Allocator>::const_reference
vector<T, Allocator>::operator[](size_type n) const
{
#if EASTL_ASSERT_ENABLED && EASTL_EMPTY_REFERENCE_ASSERT_ENABLED
if (EASTL_UNLIKELY(n >= (static_cast<size_type>(mpEnd - mpBegin))))
EASTL_FAIL_MSG("vector::operator[] -- out of range");
#elif EASTL_ASSERT_ENABLED
// We allow the user to use a reference to v[0] of an empty container. But this was merely grandfathered in and ideally we shouldn't allow such access to [0].
if (EASTL_UNLIKELY((n != 0) && (n >= (static_cast<size_type>(mpEnd - mpBegin)))))
EASTL_FAIL_MSG("vector::operator[] -- out of range");
#endif
return *(mpBegin + n);
}
template <typename T, typename Allocator>
inline typename vector<T, Allocator>::reference
vector<T, Allocator>::at(size_type n)
{
// The difference between at() and operator[] is it signals
// the requested position is out of range by throwing an
// out_of_range exception.
#if EASTL_EXCEPTIONS_ENABLED
if(EASTL_UNLIKELY(n >= (static_cast<size_type>(mpEnd - mpBegin))))
throw std::out_of_range("vector::at -- out of range");
#elif EASTL_ASSERT_ENABLED
if(EASTL_UNLIKELY(n >= (static_cast<size_type>(mpEnd - mpBegin))))
EASTL_FAIL_MSG("vector::at -- out of range");
#endif
return *(mpBegin + n);
}
template <typename T, typename Allocator>
inline typename vector<T, Allocator>::const_reference
vector<T, Allocator>::at(size_type n) const
{
#if EASTL_EXCEPTIONS_ENABLED
if(EASTL_UNLIKELY(n >= (static_cast<size_type>(mpEnd - mpBegin))))
throw std::out_of_range("vector::at -- out of range");
#elif EASTL_ASSERT_ENABLED
if(EASTL_UNLIKELY(n >= (static_cast<size_type>(mpEnd - mpBegin))))
EASTL_FAIL_MSG("vector::at -- out of range");
#endif
return *(mpBegin + n);
}
template <typename T, typename Allocator>
inline typename vector<T, Allocator>::reference
vector<T, Allocator>::front()
{
#if EASTL_ASSERT_ENABLED && EASTL_EMPTY_REFERENCE_ASSERT_ENABLED
if (EASTL_UNLIKELY((mpBegin == nullptr) || (mpEnd <= mpBegin))) // We don't allow the user to reference an empty container.
EASTL_FAIL_MSG("vector::front -- empty vector");
#else
// We allow the user to reference an empty container.
#endif
return *mpBegin;
}
template <typename T, typename Allocator>
inline typename vector<T, Allocator>::const_reference
vector<T, Allocator>::front() const
{
#if EASTL_ASSERT_ENABLED && EASTL_EMPTY_REFERENCE_ASSERT_ENABLED
if (EASTL_UNLIKELY((mpBegin == nullptr) || (mpEnd <= mpBegin))) // We don't allow the user to reference an empty container.
EASTL_FAIL_MSG("vector::front -- empty vector");
#else
// We allow the user to reference an empty container.
#endif