-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
vector
3959 lines (3290 loc) · 157 KB
/
vector
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
// vector standard header
// Copyright (c) Microsoft Corporation.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#ifndef _VECTOR_
#define _VECTOR_
#include <yvals_core.h>
#if _STL_COMPILER_PREPROCESSOR
#include <__msvc_bit_utils.hpp>
#include <__msvc_sanitizer_annotate_container.hpp>
#include <xmemory>
#if _HAS_CXX17
#include <xpolymorphic_allocator.h>
#endif // _HAS_CXX17
#if _HAS_CXX23
#include <__msvc_formatter.hpp>
#endif // _HAS_CXX23
#pragma pack(push, _CRT_PACKING)
#pragma warning(push, _STL_WARNING_LEVEL)
#pragma warning(disable : _STL_DISABLED_WARNINGS)
_STL_DISABLE_CLANG_WARNINGS
#pragma push_macro("new")
#undef new
_STD_BEGIN
template <class _Myvec>
class _Vector_const_iterator : public _Iterator_base {
public:
#if _HAS_CXX20
using iterator_concept = contiguous_iterator_tag;
#endif // _HAS_CXX20
using iterator_category = random_access_iterator_tag;
using value_type = typename _Myvec::value_type;
using difference_type = typename _Myvec::difference_type;
using pointer = typename _Myvec::const_pointer;
using reference = const value_type&;
using _Tptr = typename _Myvec::pointer;
_CONSTEXPR20 _Vector_const_iterator() noexcept : _Ptr() {}
_CONSTEXPR20 _Vector_const_iterator(_Tptr _Parg, const _Container_base* _Pvector) noexcept : _Ptr(_Parg) {
this->_Adopt(_Pvector);
}
_NODISCARD _CONSTEXPR20 reference operator*() const noexcept {
#if _ITERATOR_DEBUG_LEVEL != 0
const auto _Mycont = static_cast<const _Myvec*>(this->_Getcont());
_STL_VERIFY(_Ptr, "can't dereference value-initialized vector iterator");
_STL_VERIFY(_Mycont, "can't dereference invalidated vector iterator");
_STL_VERIFY(
_Mycont->_Myfirst <= _Ptr && _Ptr < _Mycont->_Mylast, "can't dereference out of range vector iterator");
#endif // _ITERATOR_DEBUG_LEVEL != 0
return *_Ptr;
}
_NODISCARD _CONSTEXPR20 pointer operator->() const noexcept {
#if _ITERATOR_DEBUG_LEVEL != 0
const auto _Mycont = static_cast<const _Myvec*>(this->_Getcont());
_STL_VERIFY(_Ptr, "can't dereference value-initialized vector iterator");
_STL_VERIFY(_Mycont, "can't dereference invalidated vector iterator");
_STL_VERIFY(
_Mycont->_Myfirst <= _Ptr && _Ptr < _Mycont->_Mylast, "can't dereference out of range vector iterator");
#endif // _ITERATOR_DEBUG_LEVEL != 0
return _Ptr;
}
_CONSTEXPR20 _Vector_const_iterator& operator++() noexcept {
#if _ITERATOR_DEBUG_LEVEL != 0
const auto _Mycont = static_cast<const _Myvec*>(this->_Getcont());
_STL_VERIFY(_Ptr, "can't increment value-initialized vector iterator");
_STL_VERIFY(_Mycont, "can't increment invalidated vector iterator");
_STL_VERIFY(_Ptr < _Mycont->_Mylast, "can't increment vector iterator past end");
#endif // _ITERATOR_DEBUG_LEVEL != 0
++_Ptr;
return *this;
}
_CONSTEXPR20 _Vector_const_iterator operator++(int) noexcept {
_Vector_const_iterator _Tmp = *this;
++*this;
return _Tmp;
}
_CONSTEXPR20 _Vector_const_iterator& operator--() noexcept {
#if _ITERATOR_DEBUG_LEVEL != 0
const auto _Mycont = static_cast<const _Myvec*>(this->_Getcont());
_STL_VERIFY(_Ptr, "can't decrement value-initialized vector iterator");
_STL_VERIFY(_Mycont, "can't decrement invalidated vector iterator");
_STL_VERIFY(_Mycont->_Myfirst < _Ptr, "can't decrement vector iterator before begin");
#endif // _ITERATOR_DEBUG_LEVEL != 0
--_Ptr;
return *this;
}
_CONSTEXPR20 _Vector_const_iterator operator--(int) noexcept {
_Vector_const_iterator _Tmp = *this;
--*this;
return _Tmp;
}
_CONSTEXPR20 void _Verify_offset(const difference_type _Off) const noexcept {
#if _ITERATOR_DEBUG_LEVEL == 0
(void) _Off;
#else // ^^^ _ITERATOR_DEBUG_LEVEL == 0 / _ITERATOR_DEBUG_LEVEL != 0 vvv
const auto _Mycont = static_cast<const _Myvec*>(this->_Getcont());
_STL_VERIFY(_Off == 0 || _Ptr, "cannot seek value-initialized vector iterator");
_STL_VERIFY(_Off == 0 || _Mycont, "cannot seek invalidated vector iterator");
if (_Off < 0) {
_STL_VERIFY(_Off >= _Mycont->_Myfirst - _Ptr, "cannot seek vector iterator before begin");
}
if (_Off > 0) {
_STL_VERIFY(_Off <= _Mycont->_Mylast - _Ptr, "cannot seek vector iterator after end");
}
#endif // ^^^ _ITERATOR_DEBUG_LEVEL != 0 ^^^
}
_CONSTEXPR20 _Vector_const_iterator& operator+=(const difference_type _Off) noexcept {
_Verify_offset(_Off);
_Ptr += _Off;
return *this;
}
_NODISCARD _CONSTEXPR20 _Vector_const_iterator operator+(const difference_type _Off) const noexcept {
_Vector_const_iterator _Tmp = *this;
_Tmp += _Off;
return _Tmp;
}
_NODISCARD friend _CONSTEXPR20 _Vector_const_iterator operator+(
const difference_type _Off, _Vector_const_iterator _Next) noexcept {
_Next += _Off;
return _Next;
}
_CONSTEXPR20 _Vector_const_iterator& operator-=(const difference_type _Off) noexcept {
return *this += -_Off;
}
_NODISCARD _CONSTEXPR20 _Vector_const_iterator operator-(const difference_type _Off) const noexcept {
_Vector_const_iterator _Tmp = *this;
_Tmp -= _Off;
return _Tmp;
}
_NODISCARD _CONSTEXPR20 difference_type operator-(const _Vector_const_iterator& _Right) const noexcept {
_Compat(_Right);
return static_cast<difference_type>(_Ptr - _Right._Ptr);
}
_NODISCARD _CONSTEXPR20 reference operator[](const difference_type _Off) const noexcept {
return *(*this + _Off);
}
_NODISCARD _CONSTEXPR20 bool operator==(const _Vector_const_iterator& _Right) const noexcept {
_Compat(_Right);
return _Ptr == _Right._Ptr;
}
#if _HAS_CXX20
_NODISCARD constexpr strong_ordering operator<=>(const _Vector_const_iterator& _Right) const noexcept {
_Compat(_Right);
return _STD _Unfancy_maybe_null(_Ptr) <=> _STD _Unfancy_maybe_null(_Right._Ptr);
}
#else // ^^^ _HAS_CXX20 / !_HAS_CXX20 vvv
_NODISCARD bool operator!=(const _Vector_const_iterator& _Right) const noexcept {
return !(*this == _Right);
}
_NODISCARD bool operator<(const _Vector_const_iterator& _Right) const noexcept {
_Compat(_Right);
return _Ptr < _Right._Ptr;
}
_NODISCARD bool operator>(const _Vector_const_iterator& _Right) const noexcept {
return _Right < *this;
}
_NODISCARD bool operator<=(const _Vector_const_iterator& _Right) const noexcept {
return !(_Right < *this);
}
_NODISCARD bool operator>=(const _Vector_const_iterator& _Right) const noexcept {
return !(*this < _Right);
}
#endif // ^^^ !_HAS_CXX20 ^^^
_CONSTEXPR20 void _Compat(const _Vector_const_iterator& _Right) const noexcept {
// test for compatible iterator pair
#if _ITERATOR_DEBUG_LEVEL == 0
(void) _Right;
#else // ^^^ _ITERATOR_DEBUG_LEVEL == 0 / _ITERATOR_DEBUG_LEVEL != 0 vvv
_STL_VERIFY(this->_Getcont() == _Right._Getcont(), "vector iterators incompatible");
#endif // ^^^ _ITERATOR_DEBUG_LEVEL != 0 ^^^
}
#if _ITERATOR_DEBUG_LEVEL != 0
friend _CONSTEXPR20 void _Verify_range(
const _Vector_const_iterator& _First, const _Vector_const_iterator& _Last) noexcept {
_STL_VERIFY(_First._Getcont() == _Last._Getcont(), "vector iterators in range are from different containers");
_STL_VERIFY(_First._Ptr <= _Last._Ptr, "vector iterator range transposed");
}
#endif // _ITERATOR_DEBUG_LEVEL != 0
using _Prevent_inheriting_unwrap = _Vector_const_iterator;
_NODISCARD _CONSTEXPR20 const value_type* _Unwrapped() const noexcept {
return _STD _Unfancy_maybe_null(_Ptr);
}
_CONSTEXPR20 void _Seek_to(const value_type* _It) noexcept {
_Ptr = _STD _Refancy_maybe_null<_Tptr>(const_cast<value_type*>(_It));
}
_Tptr _Ptr; // pointer to element in vector
};
#if _HAS_CXX20
template <class _Myvec>
struct pointer_traits<_Vector_const_iterator<_Myvec>> {
using pointer = _Vector_const_iterator<_Myvec>;
using element_type = const pointer::value_type;
using difference_type = pointer::difference_type;
_NODISCARD static constexpr element_type* to_address(const pointer _Iter) noexcept {
#if _ITERATOR_DEBUG_LEVEL != 0
// A value-initialized iterator is in the domain of to_address. An invalidated end iterator for a vector with
// capacity() of 0 is not. This function cannot distinguish those two cases, so it incorrectly does not diagnose
// the latter. In practice, this isn't a significant problem since to_address returns nullptr for such an
// iterator.
const auto _Mycont = static_cast<const _Myvec*>(_Iter._Getcont());
if (_Mycont) {
_STL_VERIFY(_Mycont->_Myfirst <= _Iter._Ptr && _Iter._Ptr <= _Mycont->_Mylast,
"can't convert out-of-range vector iterator to pointer");
} else {
_STL_VERIFY(!_Iter._Ptr, "can't convert invalid vector iterator to pointer");
}
#endif // _ITERATOR_DEBUG_LEVEL != 0
return _STD to_address(_Iter._Ptr);
}
};
#endif // _HAS_CXX20
template <class _Myvec>
class _Vector_iterator : public _Vector_const_iterator<_Myvec> {
public:
using _Mybase = _Vector_const_iterator<_Myvec>;
#if _HAS_CXX20
using iterator_concept = contiguous_iterator_tag;
#endif // _HAS_CXX20
using iterator_category = random_access_iterator_tag;
using value_type = typename _Myvec::value_type;
using difference_type = typename _Myvec::difference_type;
using pointer = typename _Myvec::pointer;
using reference = value_type&;
using _Mybase::_Mybase;
_NODISCARD _CONSTEXPR20 reference operator*() const noexcept {
return const_cast<reference>(_Mybase::operator*());
}
_NODISCARD _CONSTEXPR20 pointer operator->() const noexcept {
#if _ITERATOR_DEBUG_LEVEL != 0
const auto _Mycont = static_cast<const _Myvec*>(this->_Getcont());
_STL_VERIFY(this->_Ptr, "can't dereference value-initialized vector iterator");
_STL_VERIFY(_Mycont, "can't dereference invalidated vector iterator");
_STL_VERIFY(_Mycont->_Myfirst <= this->_Ptr && this->_Ptr < _Mycont->_Mylast,
"can't dereference out of range vector iterator");
#endif // _ITERATOR_DEBUG_LEVEL != 0
return this->_Ptr;
}
_CONSTEXPR20 _Vector_iterator& operator++() noexcept {
_Mybase::operator++();
return *this;
}
_CONSTEXPR20 _Vector_iterator operator++(int) noexcept {
_Vector_iterator _Tmp = *this;
_Mybase::operator++();
return _Tmp;
}
_CONSTEXPR20 _Vector_iterator& operator--() noexcept {
_Mybase::operator--();
return *this;
}
_CONSTEXPR20 _Vector_iterator operator--(int) noexcept {
_Vector_iterator _Tmp = *this;
_Mybase::operator--();
return _Tmp;
}
_CONSTEXPR20 _Vector_iterator& operator+=(const difference_type _Off) noexcept {
_Mybase::operator+=(_Off);
return *this;
}
_NODISCARD _CONSTEXPR20 _Vector_iterator operator+(const difference_type _Off) const noexcept {
_Vector_iterator _Tmp = *this;
_Tmp += _Off;
return _Tmp;
}
_NODISCARD friend _CONSTEXPR20 _Vector_iterator operator+(
const difference_type _Off, _Vector_iterator _Next) noexcept {
_Next += _Off;
return _Next;
}
_CONSTEXPR20 _Vector_iterator& operator-=(const difference_type _Off) noexcept {
_Mybase::operator-=(_Off);
return *this;
}
using _Mybase::operator-;
_NODISCARD _CONSTEXPR20 _Vector_iterator operator-(const difference_type _Off) const noexcept {
_Vector_iterator _Tmp = *this;
_Tmp -= _Off;
return _Tmp;
}
_NODISCARD _CONSTEXPR20 reference operator[](const difference_type _Off) const noexcept {
return const_cast<reference>(_Mybase::operator[](_Off));
}
using _Prevent_inheriting_unwrap = _Vector_iterator;
_NODISCARD _CONSTEXPR20 value_type* _Unwrapped() const noexcept {
return _STD _Unfancy_maybe_null(this->_Ptr);
}
};
#if _HAS_CXX20
template <class _Myvec>
struct pointer_traits<_Vector_iterator<_Myvec>> {
using pointer = _Vector_iterator<_Myvec>;
using element_type = pointer::value_type;
using difference_type = pointer::difference_type;
_NODISCARD static constexpr element_type* to_address(const pointer _Iter) noexcept {
#if _ITERATOR_DEBUG_LEVEL != 0
// A value-initialized iterator is in the domain of to_address. An invalidated end iterator for a vector with
// capacity() of 0 is not. This function cannot distinguish those two cases, so it incorrectly does not diagnose
// the latter. In practice, this isn't a significant problem since to_address returns nullptr for such an
// iterator.
const auto _Mycont = static_cast<const _Myvec*>(_Iter._Getcont());
if (_Mycont) {
_STL_VERIFY(_Mycont->_Myfirst <= _Iter._Ptr && _Iter._Ptr <= _Mycont->_Mylast,
"can't convert out-of-range vector iterator to pointer");
} else {
_STL_VERIFY(!_Iter._Ptr, "can't convert invalid vector iterator to pointer");
}
#endif // _ITERATOR_DEBUG_LEVEL != 0
return _STD to_address(_Iter._Ptr);
}
};
#endif // _HAS_CXX20
template <class _Value_type, class _Size_type, class _Difference_type, class _Pointer, class _Const_pointer>
struct _Vec_iter_types {
using value_type = _Value_type;
using size_type = _Size_type;
using difference_type = _Difference_type;
using pointer = _Pointer;
using const_pointer = _Const_pointer;
};
struct _Value_init_tag { // tag to request value-initialization
explicit _Value_init_tag() = default;
};
template <class _Val_types>
class _Vector_val : public _Container_base {
public:
using value_type = typename _Val_types::value_type;
using size_type = typename _Val_types::size_type;
using difference_type = typename _Val_types::difference_type;
using pointer = typename _Val_types::pointer;
using const_pointer = typename _Val_types::const_pointer;
using reference = value_type&;
using const_reference = const value_type&;
_CONSTEXPR20 _Vector_val() noexcept : _Myfirst(), _Mylast(), _Myend() {}
_CONSTEXPR20 _Vector_val(pointer _First, pointer _Last, pointer _End) noexcept
: _Myfirst(_First), _Mylast(_Last), _Myend(_End) {}
_CONSTEXPR20 void _Swap_val(_Vector_val& _Right) noexcept {
this->_Swap_proxy_and_iterators(_Right);
swap(_Myfirst, _Right._Myfirst); // intentional ADL
swap(_Mylast, _Right._Mylast); // intentional ADL
swap(_Myend, _Right._Myend); // intentional ADL
}
_CONSTEXPR20 void _Take_contents(_Vector_val& _Right) noexcept {
this->_Swap_proxy_and_iterators(_Right);
_Myfirst = _Right._Myfirst;
_Mylast = _Right._Mylast;
_Myend = _Right._Myend;
_Right._Myfirst = nullptr;
_Right._Mylast = nullptr;
_Right._Myend = nullptr;
}
pointer _Myfirst; // pointer to beginning of array
pointer _Mylast; // pointer to current end of sequence
pointer _Myend; // pointer to end of array
};
_EXPORT_STD template <class _Ty, class _Alloc = allocator<_Ty>>
class vector { // varying size array of values
private:
template <class>
friend class _Vb_val;
friend _Tidy_guard<vector>;
using _Alty = _Rebind_alloc_t<_Alloc, _Ty>;
using _Alty_traits = allocator_traits<_Alty>;
public:
static_assert(!_ENFORCE_MATCHING_ALLOCATORS || is_same_v<_Ty, typename _Alloc::value_type>,
_MISMATCHED_ALLOCATOR_MESSAGE("vector<T, Allocator>", "T"));
static_assert(is_object_v<_Ty>, "The C++ Standard forbids containers of non-object types "
"because of [container.requirements].");
using value_type = _Ty;
using allocator_type = _Alloc;
using pointer = typename _Alty_traits::pointer;
using const_pointer = typename _Alty_traits::const_pointer;
using reference = _Ty&;
using const_reference = const _Ty&;
using size_type = typename _Alty_traits::size_type;
using difference_type = typename _Alty_traits::difference_type;
private:
#ifdef _INSERT_VECTOR_ANNOTATION
_CONSTEXPR20 void _Create_annotation() const noexcept {
// Annotates the shadow memory of the valid range
auto& _My_data = _Mypair._Myval2;
_Apply_annotation(_My_data._Myfirst, _My_data._Myend, _My_data._Myend, _My_data._Mylast);
}
_CONSTEXPR20 void _Remove_annotation() const noexcept {
// Removes the shadow memory annotation of the range
auto& _My_data = _Mypair._Myval2;
_Apply_annotation(_My_data._Myfirst, _My_data._Myend, _My_data._Mylast, _My_data._Myend);
}
_CONSTEXPR20 void _Modify_annotation(const difference_type _Count) const noexcept {
// Extends/shrinks the annotated range by _Count
if (_Count == 0) { // nothing to do
// This also avoids calling _Apply_annotation() with null pointers
// when the vector has zero capacity, see GH-2464.
return;
}
auto& _My_data = _Mypair._Myval2;
_Apply_annotation(_My_data._Myfirst, _My_data._Myend, _My_data._Mylast, _My_data._Mylast + _Count);
}
static _CONSTEXPR20 void _Apply_annotation(
pointer _First_, pointer _End_, pointer _Old_last_, pointer _New_last_) noexcept {
_STL_INTERNAL_CHECK(_First_ != nullptr);
_STL_INTERNAL_CHECK(_End_ != nullptr);
_STL_INTERNAL_CHECK(_Old_last_ != nullptr);
_STL_INTERNAL_CHECK(_New_last_ != nullptr);
#if _HAS_CXX20
if (_STD is_constant_evaluated()) {
return;
}
#endif // _HAS_CXX20
if (!_Asan_vector_should_annotate) {
return;
}
const void* const _First = _STD _Unfancy(_First_);
const void* const _End = _STD _Unfancy(_End_);
const void* const _Old_last = _STD _Unfancy(_Old_last_);
const void* const _New_last = _STD _Unfancy(_New_last_);
if constexpr ((_Container_allocation_minimum_asan_alignment<vector>) >= _Asan_granularity) {
// old state:
// [_First, _Old_last) valid
// [_Old_last, _End) poison
// new state:
// [_First, _New_last) valid
// [_New_last, asan_aligned_after(_End)) poison
_CSTD __sanitizer_annotate_contiguous_container(
_First, _STD _Get_asan_aligned_after(_End), _Old_last, _New_last);
} else {
const auto _Aligned = _STD _Get_asan_aligned_first_end(_First, _End);
if (_Aligned._First == _Aligned._End) {
// The buffer does not end at least one shadow memory section; nothing to do.
return;
}
const void* const _Old_fixed = _Aligned._Clamp_to_end(_Old_last);
const void* const _New_fixed = _Aligned._Clamp_to_end(_New_last);
// old state:
// [_Aligned._First, _Old_fixed) valid
// [_Old_fixed, _Aligned._End) poison
// [_Aligned._End, _End) valid
// new state:
// [_Aligned._First, _New_fixed) valid
// [_New_fixed, _Aligned._End) poison
// [_Aligned._End, _End) valid
_CSTD __sanitizer_annotate_contiguous_container(_Aligned._First, _Aligned._End, _Old_fixed, _New_fixed);
}
}
class _NODISCARD _Asan_extend_guard {
public:
_Asan_extend_guard(const _Asan_extend_guard&) = delete;
_Asan_extend_guard& operator=(const _Asan_extend_guard&) = delete;
constexpr explicit _Asan_extend_guard(vector* _Myvec_, size_type _Target_size_) noexcept
: _Myvec(_Myvec_), _Target_size(_Target_size_) {
_STL_INTERNAL_CHECK(_Myvec != nullptr);
auto& _My_data = _Myvec->_Mypair._Myval2;
_Apply_annotation(_My_data._Myfirst, _My_data._Myend, _My_data._Mylast, _My_data._Myfirst + _Target_size);
}
_CONSTEXPR20 ~_Asan_extend_guard() {
if (!_Myvec) { // Operation succeeded, no modification to the shadow memory required.
return;
}
// Shrinks the shadow memory to the current size of the vector.
auto& _My_data = _Myvec->_Mypair._Myval2;
_Apply_annotation(_My_data._Myfirst, _My_data._Myend, _My_data._Myfirst + _Target_size, _My_data._Mylast);
}
_CONSTEXPR20 void _Release() noexcept {
_Myvec = nullptr;
}
private:
vector* _Myvec;
size_type _Target_size;
};
class _NODISCARD _Asan_create_guard {
public:
_Asan_create_guard(const _Asan_create_guard&) = delete;
_Asan_create_guard& operator=(const _Asan_create_guard&) = delete;
constexpr explicit _Asan_create_guard(const vector& _Myvec_) noexcept : _Myvec(_Myvec_) {}
_CONSTEXPR20 ~_Asan_create_guard() {
_Myvec._Create_annotation();
}
private:
const vector& _Myvec;
};
#define _ASAN_VECTOR_MODIFY(n) _Modify_annotation((n))
#define _ASAN_VECTOR_REMOVE _Remove_annotation()
#define _ASAN_VECTOR_CREATE _Create_annotation()
#define _ASAN_VECTOR_CREATE_GUARD _Asan_create_guard _Annotator(*this)
#define _ASAN_VECTOR_EXTEND_GUARD(n) _Asan_extend_guard _Annotator(this, (n))
#define _ASAN_VECTOR_RELEASE_GUARD _Annotator._Release()
#else // ^^^ _INSERT_VECTOR_ANNOTATION / !_INSERT_VECTOR_ANNOTATION vvv
#define _ASAN_VECTOR_MODIFY(n)
#define _ASAN_VECTOR_REMOVE
#define _ASAN_VECTOR_CREATE
#define _ASAN_VECTOR_CREATE_GUARD
#define _ASAN_VECTOR_EXTEND_GUARD(n)
#define _ASAN_VECTOR_RELEASE_GUARD
#endif // ^^^ !_INSERT_VECTOR_ANNOTATION ^^^
using _Scary_val = _Vector_val<conditional_t<_Is_simple_alloc_v<_Alty>, _Simple_types<_Ty>,
_Vec_iter_types<_Ty, size_type, difference_type, pointer, const_pointer>>>;
struct _NODISCARD _Reallocation_guard {
_Alloc& _Al;
pointer _New_begin;
size_type _New_capacity;
pointer _Constructed_first;
pointer _Constructed_last;
_Reallocation_guard& operator=(const _Reallocation_guard&) = delete;
_Reallocation_guard& operator=(_Reallocation_guard&&) = delete;
_CONSTEXPR20 ~_Reallocation_guard() noexcept {
if (_New_begin != nullptr) {
_STD _Destroy_range(_Constructed_first, _Constructed_last, _Al);
_Al.deallocate(_New_begin, _New_capacity);
}
}
};
struct _NODISCARD _Simple_reallocation_guard {
_Alloc& _Al;
pointer _New_begin;
size_type _New_capacity;
_Simple_reallocation_guard& operator=(const _Simple_reallocation_guard&) = delete;
_Simple_reallocation_guard& operator=(_Simple_reallocation_guard&&) = delete;
_CONSTEXPR20 ~_Simple_reallocation_guard() noexcept {
if (_New_begin != nullptr) {
_Al.deallocate(_New_begin, _New_capacity);
}
}
};
struct _NODISCARD _Vaporization_guard { // vaporize the detached piece
vector* _Target;
pointer _Vaporized_first;
pointer _Vaporized_last;
pointer _Destroyed_first;
_Vaporization_guard& operator=(const _Vaporization_guard&) = delete;
_Vaporization_guard& operator=(_Vaporization_guard&&) = delete;
~_Vaporization_guard() noexcept {
if (_Target != nullptr) {
auto& _Al = _Target->_Getal();
auto& _Mylast = _Target->_Mypair._Myval2._Mylast;
_Target->_Orphan_range(_Vaporized_first, _Vaporized_last);
_STD _Destroy_range(_Destroyed_first, _Mylast, _Al);
_Mylast = _Vaporized_first;
}
}
};
public:
using iterator = _Vector_iterator<_Scary_val>;
using const_iterator = _Vector_const_iterator<_Scary_val>;
using reverse_iterator = _STD reverse_iterator<iterator>;
using const_reverse_iterator = _STD reverse_iterator<const_iterator>;
_CONSTEXPR20 vector() noexcept(is_nothrow_default_constructible_v<_Alty>) : _Mypair(_Zero_then_variadic_args_t{}) {
_Mypair._Myval2._Alloc_proxy(_GET_PROXY_ALLOCATOR(_Alty, _Getal()));
}
_CONSTEXPR20 explicit vector(const _Alloc& _Al) noexcept : _Mypair(_One_then_variadic_args_t{}, _Al) {
_Mypair._Myval2._Alloc_proxy(_GET_PROXY_ALLOCATOR(_Alty, _Getal()));
}
_CONSTEXPR20 explicit vector(_CRT_GUARDOVERFLOW const size_type _Count, const _Alloc& _Al = _Alloc())
: _Mypair(_One_then_variadic_args_t{}, _Al) {
_Construct_n(_Count);
}
#if _HAS_CXX17
template <class _Alloc2 = _Alloc, enable_if_t<_Is_allocator<_Alloc2>::value, int> = 0>
#endif // _HAS_CXX17
_CONSTEXPR20 vector(_CRT_GUARDOVERFLOW const size_type _Count, const _Ty& _Val, const _Alloc& _Al = _Alloc())
: _Mypair(_One_then_variadic_args_t{}, _Al) {
_Construct_n(_Count, _Val);
}
template <class _Iter, enable_if_t<_Is_iterator_v<_Iter>, int> = 0>
_CONSTEXPR20 vector(_Iter _First, _Iter _Last, const _Alloc& _Al = _Alloc())
: _Mypair(_One_then_variadic_args_t{}, _Al) {
_STD _Adl_verify_range(_First, _Last);
auto _UFirst = _STD _Get_unwrapped(_First);
auto _ULast = _STD _Get_unwrapped(_Last);
if constexpr (_Is_cpp17_fwd_iter_v<_Iter>) {
const auto _Length = static_cast<size_t>(_STD distance(_UFirst, _ULast));
const auto _Count = _STD _Convert_size<size_type>(_Length);
_Construct_n(_Count, _STD move(_UFirst), _STD move(_ULast));
#if _HAS_CXX20
} else if constexpr (forward_iterator<_Iter>) {
const auto _Length = _STD _To_unsigned_like(_RANGES distance(_UFirst, _ULast));
const auto _Count = _STD _Convert_size<size_type>(_Length);
_Construct_n(_Count, _STD move(_UFirst), _STD move(_ULast));
#endif // _HAS_CXX20
} else {
auto&& _Alproxy = _GET_PROXY_ALLOCATOR(_Alty, _Getal());
_Container_proxy_ptr<_Alty> _Proxy(_Alproxy, _Mypair._Myval2);
_Tidy_guard<vector> _Guard{this};
_Append_uncounted_range(_STD move(_UFirst), _STD move(_ULast));
_Guard._Target = nullptr;
_Proxy._Release();
}
}
_CONSTEXPR20 vector(initializer_list<_Ty> _Ilist, const _Alloc& _Al = _Alloc())
: _Mypair(_One_then_variadic_args_t{}, _Al) {
_Construct_n(_Convert_size<size_type>(_Ilist.size()), _Ilist.begin(), _Ilist.end());
}
#if _HAS_CXX23
template <_Container_compatible_range<_Ty> _Rng>
constexpr vector(from_range_t, _Rng&& _Range, const _Alloc& _Al = _Alloc())
: _Mypair(_One_then_variadic_args_t{}, _Al) {
if constexpr (_RANGES sized_range<_Rng> || _RANGES forward_range<_Rng>) {
const auto _Length = _To_unsigned_like(_RANGES distance(_Range));
const auto _Count = _Convert_size<size_type>(_Length);
_Construct_n(_Count, _RANGES _Ubegin(_Range), _RANGES _Uend(_Range));
} else {
auto&& _Alproxy = _GET_PROXY_ALLOCATOR(_Alty, _Getal());
_Container_proxy_ptr<_Alty> _Proxy(_Alproxy, _Mypair._Myval2);
_Tidy_guard<vector> _Guard{this};
_Append_uncounted_range(_RANGES _Ubegin(_Range), _RANGES _Uend(_Range));
_Guard._Target = nullptr;
_Proxy._Release();
}
}
#endif // _HAS_CXX23
_CONSTEXPR20 vector(const vector& _Right)
: _Mypair(_One_then_variadic_args_t{}, _Alty_traits::select_on_container_copy_construction(_Right._Getal())) {
const auto& _Right_data = _Right._Mypair._Myval2;
const auto _Count = static_cast<size_type>(_Right_data._Mylast - _Right_data._Myfirst);
_Construct_n(_Count, _Right_data._Myfirst, _Right_data._Mylast);
}
_CONSTEXPR20 vector(const vector& _Right, const _Identity_t<_Alloc>& _Al)
: _Mypair(_One_then_variadic_args_t{}, _Al) {
const auto& _Right_data = _Right._Mypair._Myval2;
const auto _Count = static_cast<size_type>(_Right_data._Mylast - _Right_data._Myfirst);
_Construct_n(_Count, _Right_data._Myfirst, _Right_data._Mylast);
}
_CONSTEXPR20 vector(vector&& _Right) noexcept
: _Mypair(_One_then_variadic_args_t{}, _STD move(_Right._Getal()),
_STD exchange(_Right._Mypair._Myval2._Myfirst, nullptr),
_STD exchange(_Right._Mypair._Myval2._Mylast, nullptr),
_STD exchange(_Right._Mypair._Myval2._Myend, nullptr)) {
_Mypair._Myval2._Alloc_proxy(_GET_PROXY_ALLOCATOR(_Alty, _Getal()));
_Mypair._Myval2._Swap_proxy_and_iterators(_Right._Mypair._Myval2);
}
_CONSTEXPR20 vector(vector&& _Right, const _Identity_t<_Alloc>& _Al_)
noexcept(_Alty_traits::is_always_equal::value) // strengthened
: _Mypair(_One_then_variadic_args_t{}, _Al_) {
_Alty& _Al = _Getal();
auto&& _Alproxy = _GET_PROXY_ALLOCATOR(_Alty, _Al);
auto& _My_data = _Mypair._Myval2;
auto& _Right_data = _Right._Mypair._Myval2;
_Container_proxy_ptr<_Alty> _Proxy(_Alproxy, _My_data);
if constexpr (!_Alty_traits::is_always_equal::value) {
if (_Al != _Right._Getal()) {
const auto _Count = static_cast<size_type>(_Right_data._Mylast - _Right_data._Myfirst);
if (_Count != 0) {
_Buy_raw(_Count);
_Tidy_guard<vector> _Guard{this};
_My_data._Mylast =
_Uninitialized_move(_Right_data._Myfirst, _Right_data._Mylast, _My_data._Myfirst, _Al);
_ASAN_VECTOR_CREATE;
_Guard._Target = nullptr;
}
_Proxy._Release();
return;
}
}
_My_data._Take_contents(_Right_data);
_Proxy._Release();
}
_CONSTEXPR20 vector& operator=(vector&& _Right)
noexcept(_Choose_pocma_v<_Alty> != _Pocma_values::_No_propagate_allocators) {
if (this == _STD addressof(_Right)) {
return *this;
}
_Alty& _Al = _Getal();
_Alty& _Right_al = _Right._Getal();
constexpr auto _Pocma_val = _Choose_pocma_v<_Alty>;
if constexpr (_Pocma_val == _Pocma_values::_No_propagate_allocators) {
if (_Al != _Right_al) {
_Move_assign_unequal_alloc(_Right);
return *this;
}
}
_Tidy();
#if _ITERATOR_DEBUG_LEVEL != 0
if constexpr (_Pocma_val == _Pocma_values::_Propagate_allocators) {
if (_Al != _Right_al) {
// intentionally slams into noexcept on OOM, TRANSITION, VSO-466800
_Mypair._Myval2._Reload_proxy(_GET_PROXY_ALLOCATOR(_Alty, _Al), _GET_PROXY_ALLOCATOR(_Alty, _Right_al));
}
}
#endif // _ITERATOR_DEBUG_LEVEL != 0
_Pocma(_Al, _Right_al);
_Mypair._Myval2._Take_contents(_Right._Mypair._Myval2);
return *this;
}
_CONSTEXPR20 ~vector() noexcept {
_Tidy();
#if _ITERATOR_DEBUG_LEVEL != 0
auto&& _Alproxy = _GET_PROXY_ALLOCATOR(_Alty, _Getal());
_Delete_plain_internal(_Alproxy, _STD exchange(_Mypair._Myval2._Myproxy, nullptr));
#endif // _ITERATOR_DEBUG_LEVEL != 0
}
private:
template <class... _Valty>
_CONSTEXPR20 _Ty& _Emplace_one_at_back(_Valty&&... _Val) {
// insert by perfectly forwarding into element at end, provide strong guarantee
auto& _My_data = _Mypair._Myval2;
pointer& _Mylast = _My_data._Mylast;
if (_Mylast != _My_data._Myend) {
return _Emplace_back_with_unused_capacity(_STD forward<_Valty>(_Val)...);
}
return *_Emplace_reallocate(_Mylast, _STD forward<_Valty>(_Val)...);
}
template <class... _Valty>
_CONSTEXPR20 _Ty& _Emplace_back_with_unused_capacity(_Valty&&... _Val) {
// insert by perfectly forwarding into element at end, provide strong guarantee
auto& _My_data = _Mypair._Myval2;
pointer& _Mylast = _My_data._Mylast;
_STL_INTERNAL_CHECK(_Mylast != _My_data._Myend); // check that we have unused capacity
if constexpr (conjunction_v<is_nothrow_constructible<_Ty, _Valty...>,
_Uses_default_construct<_Alloc, _Ty*, _Valty...>>) {
_ASAN_VECTOR_MODIFY(1);
_STD _Construct_in_place(*_Mylast, _STD forward<_Valty>(_Val)...);
} else {
_ASAN_VECTOR_EXTEND_GUARD(static_cast<size_type>(_Mylast - _My_data._Myfirst) + 1);
_Alty_traits::construct(_Getal(), _Unfancy(_Mylast), _STD forward<_Valty>(_Val)...);
_ASAN_VECTOR_RELEASE_GUARD;
}
_Orphan_range(_Mylast, _Mylast);
_Ty& _Result = *_Mylast;
++_Mylast;
return _Result;
}
template <class... _Valty>
_CONSTEXPR20 pointer _Emplace_reallocate(const pointer _Whereptr, _Valty&&... _Val) {
// reallocate and insert by perfectly forwarding _Val at _Whereptr
_Alty& _Al = _Getal();
auto& _My_data = _Mypair._Myval2;
pointer& _Myfirst = _My_data._Myfirst;
pointer& _Mylast = _My_data._Mylast;
_STL_INTERNAL_CHECK(_Mylast == _My_data._Myend); // check that we have no unused capacity
const auto _Whereoff = static_cast<size_type>(_Whereptr - _Myfirst);
const auto _Oldsize = static_cast<size_type>(_Mylast - _Myfirst);
if (_Oldsize == max_size()) {
_Xlength();
}
const size_type _Newsize = _Oldsize + 1;
size_type _Newcapacity = _Calculate_growth(_Newsize);
const pointer _Newvec = _STD _Allocate_at_least_helper(_Al, _Newcapacity);
const pointer _Constructed_last = _Newvec + _Whereoff + 1;
_Reallocation_guard _Guard{_Al, _Newvec, _Newcapacity, _Constructed_last, _Constructed_last};
auto& _Constructed_first = _Guard._Constructed_first;
_Alty_traits::construct(_Al, _STD _Unfancy(_Newvec + _Whereoff), _STD forward<_Valty>(_Val)...);
_Constructed_first = _Newvec + _Whereoff;
if (_Whereptr == _Mylast) { // at back, provide strong guarantee
if constexpr (is_nothrow_move_constructible_v<_Ty> || !is_copy_constructible_v<_Ty>) {
_STD _Uninitialized_move(_Myfirst, _Mylast, _Newvec, _Al);
} else {
_STD _Uninitialized_copy(_Myfirst, _Mylast, _Newvec, _Al);
}
} else { // provide basic guarantee
_STD _Uninitialized_move(_Myfirst, _Whereptr, _Newvec, _Al);
_Constructed_first = _Newvec;
_STD _Uninitialized_move(_Whereptr, _Mylast, _Newvec + _Whereoff + 1, _Al);
}
_Guard._New_begin = nullptr;
_Change_array(_Newvec, _Newsize, _Newcapacity);
return _Newvec + _Whereoff;
}
public:
template <class... _Valty>
_CONSTEXPR20 _CONTAINER_EMPLACE_RETURN emplace_back(_Valty&&... _Val) {
// insert by perfectly forwarding into element at end, provide strong guarantee
_Ty& _Result = _Emplace_one_at_back(_STD forward<_Valty>(_Val)...);
#if _HAS_CXX17
return _Result;
#else // ^^^ _HAS_CXX17 / !_HAS_CXX17 vvv
(void) _Result;
#endif // ^^^ !_HAS_CXX17 ^^^
}
_CONSTEXPR20 void push_back(const _Ty& _Val) { // insert element at end, provide strong guarantee
_Emplace_one_at_back(_Val);
}
_CONSTEXPR20 void push_back(_Ty&& _Val) {
// insert by moving into element at end, provide strong guarantee
_Emplace_one_at_back(_STD move(_Val));
}
private:
template <class _Iter, class _Sent>
_CONSTEXPR20 void _Append_uncounted_range(_Iter _First, const _Sent _Last) {
// insert range [_First, _Last) at end
// For one-at-back, provide strong guarantee.
// Otherwise, provide basic guarantee (despite N4950 [vector.modifiers]/2).
// Performance note: except for one-at-back, _Emplace_one_at_back()'s strong guarantee is unnecessary here.
for (; _First != _Last; ++_First) {
_Emplace_one_at_back(*_First);
}
}
#if _HAS_CXX23
template <class _Iter>
_CONSTEXPR20 void _Append_counted_range(_Iter _First, const size_type _Count) {
// insert counted range _First + [0, _Count) at end
auto& _Al = _Getal();
auto& _My_data = _Mypair._Myval2;
pointer& _Mylast = _My_data._Mylast;
const pointer _Oldfirst = _My_data._Myfirst;
const pointer _Oldlast = _Mylast;
const auto _Unused_capacity = static_cast<size_type>(_My_data._Myend - _Oldlast);
if (_Count == 0) { // nothing to do, avoid invalidating iterators
} else if (_Count > _Unused_capacity) { // reallocate
const auto _Oldsize = static_cast<size_type>(_Oldlast - _Oldfirst);
if (_Count > max_size() - _Oldsize) {
_Xlength();
}
const size_type _Newsize = _Oldsize + _Count;
size_type _Newcapacity = _Calculate_growth(_Newsize);
const pointer _Newvec = _Allocate_at_least_helper(_Al, _Newcapacity);
const pointer _Constructed_last = _Newvec + _Oldsize + _Count;
_Reallocation_guard _Guard{_Al, _Newvec, _Newcapacity, _Constructed_last, _Constructed_last};
auto& _Constructed_first = _Guard._Constructed_first;
_Uninitialized_copy_n(_STD move(_First), _Count, _Newvec + _Oldsize, _Al);
_Constructed_first = _Newvec + _Oldsize;
if (_Count == 1) { // one at back, provide strong guarantee
if constexpr (is_nothrow_move_constructible_v<_Ty> || !is_copy_constructible_v<_Ty>) {
_Uninitialized_move(_Oldfirst, _Oldlast, _Newvec, _Al);
} else {
_Uninitialized_copy(_Oldfirst, _Oldlast, _Newvec, _Al);
}
} else { // provide basic guarantee
_Uninitialized_move(_Oldfirst, _Oldlast, _Newvec, _Al);
}
_Guard._New_begin = nullptr;
_Change_array(_Newvec, _Newsize, _Newcapacity);
} else { // Provide the strong guarantee.
// Performance note: except for one-at-back, the strong guarantee is unnecessary here.
_ASAN_VECTOR_EXTEND_GUARD(static_cast<size_type>(_Oldlast - _Oldfirst) + _Count);
_Mylast = _Uninitialized_copy_n(_STD move(_First), _Count, _Oldlast, _Al);
_ASAN_VECTOR_RELEASE_GUARD;
_Orphan_range(_Oldlast, _Oldlast);
}
}
#endif // _HAS_CXX23
public:
#if _HAS_CXX23
template <_Container_compatible_range<_Ty> _Rng>
constexpr void append_range(_Rng&& _Range) {
if constexpr (_RANGES forward_range<_Rng> || _RANGES sized_range<_Rng>) {