-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
vector
3480 lines (2878 loc) · 135 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
#pragma once
#ifndef _VECTOR_
#define _VECTOR_
#include <yvals_core.h>
#if _STL_COMPILER_PREPROCESSOR
#include <xmemory>
#if _HAS_CXX17
#include <xpolymorphic_allocator.h>
#endif // _HAS_CXX17
#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:
#ifdef __cpp_lib_concepts
using iterator_concept = contiguous_iterator_tag;
#endif // __cpp_lib_concepts
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->_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->_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(_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->_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 ^^^ // vvv _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");
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;
}
_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 _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 _Unfancy(_Ptr) <=> _Unfancy(_Right._Ptr);
}
#else // ^^^ _HAS_CXX20 ^^^ / vvv !_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 ^^^ // vvv _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 _Unfancy(_Ptr);
}
_CONSTEXPR20 void _Seek_to(const value_type* _It) noexcept {
_Ptr = _Refancy<_Tptr>(const_cast<value_type*>(_It));
}
_Tptr _Ptr; // pointer to element in vector
};
template <class _Myvec>
_NODISCARD _CONSTEXPR20 _Vector_const_iterator<_Myvec> operator+(
typename _Vector_const_iterator<_Myvec>::difference_type _Off, _Vector_const_iterator<_Myvec> _Next) noexcept {
_Next += _Off;
return _Next;
}
#if _HAS_CXX20
template <class _Myvec>
struct pointer_traits<_Vector_const_iterator<_Myvec>> {
using pointer = _Vector_const_iterator<_Myvec>;
using element_type = const typename pointer::value_type;
using difference_type = typename 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>;
#ifdef __cpp_lib_concepts
using iterator_concept = contiguous_iterator_tag;
#endif // __cpp_lib_concepts
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->_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;
}
_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 _Unfancy(this->_Ptr);
}
};
template <class _Myvec>
_NODISCARD _CONSTEXPR20 _Vector_iterator<_Myvec> operator+(
typename _Vector_iterator<_Myvec>::difference_type _Off, _Vector_iterator<_Myvec> _Next) noexcept {
_Next += _Off;
return _Next;
}
#if _HAS_CXX20
template <class _Myvec>
struct pointer_traits<_Vector_iterator<_Myvec>> {
using pointer = _Vector_iterator<_Myvec>;
using element_type = typename pointer::value_type;
using difference_type = typename 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,
class _Reference, class _Const_reference>
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_adl(_Myfirst, _Right._Myfirst);
_Swap_adl(_Mylast, _Right._Mylast);
_Swap_adl(_Myend, _Right._Myend);
}
_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
};
template <class _Ptrty>
constexpr auto _Unfancy_maybe_null(_Ptrty _Ptr) noexcept {
// converts from a (potentially null) fancy pointer to a plain pointer
return _Ptr ? _STD addressof(*_Ptr) : nullptr;
}
template <class _Ty>
constexpr _Ty* _Unfancy_maybe_null(_Ty* _Ptr) noexcept { // do nothing for plain pointers
return _Ptr;
}
#if !defined(_M_CEE_PURE) && !defined(_DISABLE_VECTOR_ANNOTATION)
#if defined(__SANITIZE_ADDRESS__)
#define _ACTIVATE_VECTOR_ANNOTATION
#define _INSERT_VECTOR_ANNOTATION
#elif defined(__clang__) && defined(__has_feature) // ^^^ __SANITIZE_ADDRESS__ ^^^ // vvv __clang__ vvv
#if __has_feature(address_sanitizer)
#define _ACTIVATE_VECTOR_ANNOTATION
#define _INSERT_VECTOR_ANNOTATION
#pragma comment(linker, "/INFERASANLIBS")
#endif // __has_feature(address_sanitizer)
#elif defined(_ANNOTATE_VECTOR) // ^^^ __clang__ ^^^ // vvv _ANNOTATE_VECTOR vvv
#define _INSERT_VECTOR_ANNOTATION
#endif // _ANNOTATE_VECTOR
#endif // !_M_CEE_PURE && !_DISABLE_VECTOR_ANNOTATION
#ifdef _ACTIVATE_VECTOR_ANNOTATION
#pragma comment(lib, "stl_asan")
#pragma detect_mismatch("annotate_vector", "1")
#endif // _ACTIVATE_VECTOR_ANNOTATION
#ifdef _INSERT_VECTOR_ANNOTATION
extern "C" {
void __cdecl __sanitizer_annotate_contiguous_container(
const void* _First, const void* _End, const void* _Old_last, const void* _New_last) noexcept;
extern const bool _Asan_vector_should_annotate;
}
#if defined(_M_ARM64EC)
#pragma comment(linker, \
"/alternatename:#__sanitizer_annotate_contiguous_container=#__sanitizer_annotate_contiguous_container_default")
#pragma comment(linker, \
"/alternatename:__sanitizer_annotate_contiguous_container=__sanitizer_annotate_contiguous_container_default")
#pragma comment(linker, "/alternatename:#_Asan_vector_should_annotate=#_Asan_vector_should_annotate_default")
#pragma comment(linker, "/alternatename:_Asan_vector_should_annotate=_Asan_vector_should_annotate_default")
#elif defined(_M_HYBRID)
#pragma comment(linker, \
"/alternatename:#__sanitizer_annotate_contiguous_container=#__sanitizer_annotate_contiguous_container_default")
#pragma comment(linker, \
"/alternatename:___sanitizer_annotate_contiguous_container=___sanitizer_annotate_contiguous_container_default")
#pragma comment(linker, "/alternatename:#_Asan_vector_should_annotate=#_Asan_vector_should_annotate_default")
#pragma comment(linker, "/alternatename:__Asan_vector_should_annotate=__Asan_vector_should_annotate_default")
#elif defined(_M_IX86)
#pragma comment(linker, \
"/alternatename:___sanitizer_annotate_contiguous_container=___sanitizer_annotate_contiguous_container_default")
#pragma comment(linker, "/alternatename:__Asan_vector_should_annotate=__Asan_vector_should_annotate_default")
#elif defined(_M_X64) || defined(_M_ARM) || defined(_M_ARM64)
#pragma comment(linker, \
"/alternatename:__sanitizer_annotate_contiguous_container=__sanitizer_annotate_contiguous_container_default")
#pragma comment(linker, "/alternatename:_Asan_vector_should_annotate=_Asan_vector_should_annotate_default")
#else // ^^^ known architecture / unknown architecture vvv
#error Unknown architecture
#endif // ^^^ unknown architecture ^^^
template <class _Vec, class = void>
_INLINE_VAR constexpr bool _Has_minimum_allocation_alignment = alignof(typename _Vec::value_type) >= _Asan_granularity;
template <class _Vec>
_INLINE_VAR constexpr bool
_Has_minimum_allocation_alignment<_Vec, void_t<decltype(_Vec::allocator_type::_Minimum_allocation_alignment)>> =
_Vec::allocator_type::_Minimum_allocation_alignment >= _Asan_granularity;
#else // ^^^ _INSERT_VECTOR_ANNOTATION ^^^ // vvv !_INSERT_VECTOR_ANNOTATION vvv
#pragma detect_mismatch("annotate_vector", "0")
#endif // !_INSERT_VECTOR_ANNOTATION
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"));
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);
}
_NODISCARD static const void* _Get_aligned_first(const void* _First, const void* _End) noexcept {
const auto _CFirst = reinterpret_cast<const char*>(_First);
const auto _CEnd = reinterpret_cast<const char*>(_End);
const size_t _Capacity = static_cast<size_t>(_CEnd - _CFirst);
if (_Capacity >= _Asan_granularity) {
// We are guaranteed to have sufficient space to find an aligned address.
return reinterpret_cast<const void*>(
(reinterpret_cast<uintptr_t>(_CFirst) + (_Asan_granularity - 1)) & ~(_Asan_granularity - 1));
}
uintptr_t _Alignment_offset = reinterpret_cast<uintptr_t>(_CFirst) & (_Asan_granularity - 1);
if (_Alignment_offset != 0) {
_Alignment_offset = _Asan_granularity - _Alignment_offset;
}
if (_Capacity > _Alignment_offset) {
return _CFirst + _Alignment_offset;
}
return nullptr;
}
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 auto _First = reinterpret_cast<const char*>(_Unfancy(_First_));
const auto _End = reinterpret_cast<const char*>(_Unfancy(_End_));
const auto _Old_last = reinterpret_cast<const char*>(_Unfancy(_Old_last_));
const auto _New_last = reinterpret_cast<const char*>(_Unfancy(_New_last_));
if constexpr (_Has_minimum_allocation_alignment<vector>) {
__sanitizer_annotate_contiguous_container(_First, _End, _Old_last, _New_last);
} else {
const void* const _Aligned_first = _Get_aligned_first(_First, _End);
if (!_Aligned_first) {
// There is no aligned address within the underlying buffer; nothing to do.
return;
}
const void* const _Aligned_old_last = _Old_last < _Aligned_first ? _Aligned_first : _Old_last;
const void* const _Aligned_new_last = _New_last < _Aligned_first ? _Aligned_first : _New_last;
const void* const _Aligned_end = _End < _Aligned_first ? _Aligned_first : _End;
__sanitizer_annotate_contiguous_container(
_Aligned_first, _Aligned_end, _Aligned_old_last, _Aligned_new_last);
}
}
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 ^^^ // vvv !_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, _Ty&, const _Ty&>>>;
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);
}
_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) {
_Adl_verify_range(_First, _Last);
auto _UFirst = _Get_unwrapped(_First);
auto _ULast = _Get_unwrapped(_Last);
if constexpr (_Is_fwd_iter_v<_Iter>) {
const auto _Count = _Convert_size<size_type>(static_cast<size_t>(_STD distance(_UFirst, _ULast)));
_Construct_n(_Count, _STD move(_UFirst), _STD move(_ULast));
} else {
auto&& _Alproxy = _GET_PROXY_ALLOCATOR(_Alty, _Getal());
_Container_proxy_ptr<_Alty> _Proxy(_Alproxy, _Mypair._Myval2);
_Tidy_guard<vector> _Guard{this};
for (; _UFirst != _ULast; ++_UFirst) {
// performance note: _Emplace_one_at_back's strong guarantee is unnecessary here.
_Emplace_one_at_back(*_UFirst);
}
_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());
}
_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);
_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;
const size_type _Newcapacity = _Calculate_growth(_Newsize);
const pointer _Newvec = _Al.allocate(_Newcapacity);
const pointer _Constructed_last = _Newvec + _Whereoff + 1;
pointer _Constructed_first = _Constructed_last;
_TRY_BEGIN
_Alty_traits::construct(_Al, _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>) {
_Uninitialized_move(_Myfirst, _Mylast, _Newvec, _Al);
} else {
_Uninitialized_copy(_Myfirst, _Mylast, _Newvec, _Al);
}
} else { // provide basic guarantee
_Uninitialized_move(_Myfirst, _Whereptr, _Newvec, _Al);
_Constructed_first = _Newvec;
_Uninitialized_move(_Whereptr, _Mylast, _Newvec + _Whereoff + 1, _Al);
}
_CATCH_ALL
_Destroy_range(_Constructed_first, _Constructed_last, _Al);
_Al.deallocate(_Newvec, _Newcapacity);
_RERAISE;
_CATCH_END
_Change_array(_Newvec, _Newsize, _Newcapacity);
return _Newvec + _Whereoff;
}
public:
template <class... _Valty>
_CONSTEXPR20 decltype(auto) 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 ^^^ // vvv !_HAS_CXX17 vvv
(void) _Result;
#endif // _HAS_CXX17
}
_CONSTEXPR20 void push_back(const _Ty& _Val) { // insert element at end, provide strong guarantee
emplace_back(_Val);
}
_CONSTEXPR20 void push_back(_Ty&& _Val) {
// insert by moving into element at end, provide strong guarantee
emplace_back(_STD move(_Val));
}
template <class... _Valty>
_CONSTEXPR20 iterator emplace(const_iterator _Where, _Valty&&... _Val) {
// insert by perfectly forwarding _Val at _Where
const pointer _Whereptr = _Where._Ptr;
auto& _My_data = _Mypair._Myval2;
const pointer _Oldlast = _My_data._Mylast;
#if _ITERATOR_DEBUG_LEVEL == 2
_STL_VERIFY(
_Where._Getcont() == _STD addressof(_My_data) && _Whereptr >= _My_data._Myfirst && _Oldlast >= _Whereptr,
"vector emplace iterator outside range");
#endif // _ITERATOR_DEBUG_LEVEL == 2
if (_Oldlast != _My_data._Myend) {
if (_Whereptr == _Oldlast) { // at back, provide strong guarantee
_Emplace_back_with_unused_capacity(_STD forward<_Valty>(_Val)...);
} else {
auto& _Al = _Getal();
_Alloc_temporary2<_Alty> _Obj(_Al, _STD forward<_Valty>(_Val)...); // handle aliasing
// after constructing _Obj, provide basic guarantee
_Orphan_range(_Whereptr, _Oldlast);
_ASAN_VECTOR_EXTEND_GUARD(static_cast<size_type>(_Oldlast - _My_data._Myfirst) + 1);
_Alty_traits::construct(_Al, _Unfancy(_Oldlast), _STD move(_Oldlast[-1]));
_ASAN_VECTOR_RELEASE_GUARD;
++_My_data._Mylast;
_Move_backward_unchecked(_Whereptr, _Oldlast - 1, _Oldlast);
*_Whereptr = _STD move(_Obj._Get_value());
}
return _Make_iterator(_Whereptr);
}
return _Make_iterator(_Emplace_reallocate(_Whereptr, _STD forward<_Valty>(_Val)...));
}
_CONSTEXPR20 iterator insert(const_iterator _Where, const _Ty& _Val) { // insert _Val at _Where
return emplace(_Where, _Val);
}
_CONSTEXPR20 iterator insert(const_iterator _Where, _Ty&& _Val) { // insert by moving _Val at _Where
return emplace(_Where, _STD move(_Val));
}
_CONSTEXPR20 iterator insert(const_iterator _Where, _CRT_GUARDOVERFLOW const size_type _Count, const _Ty& _Val) {
// insert _Count * _Val at _Where
const pointer _Whereptr = _Where._Ptr;
auto& _Al = _Getal();
auto& _My_data = _Mypair._Myval2;
pointer& _Mylast = _My_data._Mylast;
const pointer _Oldfirst = _My_data._Myfirst;
const pointer _Oldlast = _Mylast;
#if _ITERATOR_DEBUG_LEVEL == 2
_STL_VERIFY(_Where._Getcont() == _STD addressof(_My_data) && _Whereptr >= _Oldfirst && _Oldlast >= _Whereptr,
"vector insert iterator outside range");
#endif // _ITERATOR_DEBUG_LEVEL == 2
const auto _Whereoff = static_cast<size_type>(_Whereptr - _Oldfirst);
const auto _Unused_capacity = static_cast<size_type>(_My_data._Myend - _Oldlast);
const bool _One_at_back = _Count == 1 && _Whereptr == _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;
const size_type _Newcapacity = _Calculate_growth(_Newsize);
const pointer _Newvec = _Al.allocate(_Newcapacity);
const pointer _Constructed_last = _Newvec + _Whereoff + _Count;
pointer _Constructed_first = _Constructed_last;
_TRY_BEGIN
_Uninitialized_fill_n(_Newvec + _Whereoff, _Count, _Val, _Al);
_Constructed_first = _Newvec + _Whereoff;
if (_One_at_back) { // provide strong guarantee