]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/bits/stl_vector.h
5e18f6eedce76139c2f175c001e2c4872361eca7
[thirdparty/gcc.git] / libstdc++-v3 / include / bits / stl_vector.h
1 // Vector implementation -*- C++ -*-
2
3 // Copyright (C) 2001-2023 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
10
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
19
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
24
25 /*
26 *
27 * Copyright (c) 1994
28 * Hewlett-Packard Company
29 *
30 * Permission to use, copy, modify, distribute and sell this software
31 * and its documentation for any purpose is hereby granted without fee,
32 * provided that the above copyright notice appear in all copies and
33 * that both that copyright notice and this permission notice appear
34 * in supporting documentation. Hewlett-Packard Company makes no
35 * representations about the suitability of this software for any
36 * purpose. It is provided "as is" without express or implied warranty.
37 *
38 *
39 * Copyright (c) 1996
40 * Silicon Graphics Computer Systems, Inc.
41 *
42 * Permission to use, copy, modify, distribute and sell this software
43 * and its documentation for any purpose is hereby granted without fee,
44 * provided that the above copyright notice appear in all copies and
45 * that both that copyright notice and this permission notice appear
46 * in supporting documentation. Silicon Graphics makes no
47 * representations about the suitability of this software for any
48 * purpose. It is provided "as is" without express or implied warranty.
49 */
50
51 /** @file bits/stl_vector.h
52 * This is an internal header file, included by other library headers.
53 * Do not attempt to use it directly. @headername{vector}
54 */
55
56 #ifndef _STL_VECTOR_H
57 #define _STL_VECTOR_H 1
58
59 #include <bits/stl_iterator_base_funcs.h>
60 #include <bits/functexcept.h>
61 #include <bits/concept_check.h>
62 #if __cplusplus >= 201103L
63 #include <initializer_list>
64 #endif
65 #if __cplusplus >= 202002L
66 # include <compare>
67 #endif
68
69 #include <debug/assertions.h>
70
71 #if _GLIBCXX_SANITIZE_STD_ALLOCATOR && _GLIBCXX_SANITIZE_VECTOR
72 extern "C" void
73 __sanitizer_annotate_contiguous_container(const void*, const void*,
74 const void*, const void*);
75 #endif
76
77 namespace std _GLIBCXX_VISIBILITY(default)
78 {
79 _GLIBCXX_BEGIN_NAMESPACE_VERSION
80 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
81
82 /// See bits/stl_deque.h's _Deque_base for an explanation.
83 template<typename _Tp, typename _Alloc>
84 struct _Vector_base
85 {
86 typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template
87 rebind<_Tp>::other _Tp_alloc_type;
88 typedef typename __gnu_cxx::__alloc_traits<_Tp_alloc_type>::pointer
89 pointer;
90
91 struct _Vector_impl_data
92 {
93 pointer _M_start;
94 pointer _M_finish;
95 pointer _M_end_of_storage;
96
97 _GLIBCXX20_CONSTEXPR
98 _Vector_impl_data() _GLIBCXX_NOEXCEPT
99 : _M_start(), _M_finish(), _M_end_of_storage()
100 { }
101
102 #if __cplusplus >= 201103L
103 _GLIBCXX20_CONSTEXPR
104 _Vector_impl_data(_Vector_impl_data&& __x) noexcept
105 : _M_start(__x._M_start), _M_finish(__x._M_finish),
106 _M_end_of_storage(__x._M_end_of_storage)
107 { __x._M_start = __x._M_finish = __x._M_end_of_storage = pointer(); }
108 #endif
109
110 _GLIBCXX20_CONSTEXPR
111 void
112 _M_copy_data(_Vector_impl_data const& __x) _GLIBCXX_NOEXCEPT
113 {
114 _M_start = __x._M_start;
115 _M_finish = __x._M_finish;
116 _M_end_of_storage = __x._M_end_of_storage;
117 }
118
119 _GLIBCXX20_CONSTEXPR
120 void
121 _M_swap_data(_Vector_impl_data& __x) _GLIBCXX_NOEXCEPT
122 {
123 // Do not use std::swap(_M_start, __x._M_start), etc as it loses
124 // information used by TBAA.
125 _Vector_impl_data __tmp;
126 __tmp._M_copy_data(*this);
127 _M_copy_data(__x);
128 __x._M_copy_data(__tmp);
129 }
130 };
131
132 struct _Vector_impl
133 : public _Tp_alloc_type, public _Vector_impl_data
134 {
135 _GLIBCXX20_CONSTEXPR
136 _Vector_impl() _GLIBCXX_NOEXCEPT_IF(
137 is_nothrow_default_constructible<_Tp_alloc_type>::value)
138 : _Tp_alloc_type()
139 { }
140
141 _GLIBCXX20_CONSTEXPR
142 _Vector_impl(_Tp_alloc_type const& __a) _GLIBCXX_NOEXCEPT
143 : _Tp_alloc_type(__a)
144 { }
145
146 #if __cplusplus >= 201103L
147 // Not defaulted, to enforce noexcept(true) even when
148 // !is_nothrow_move_constructible<_Tp_alloc_type>.
149 _GLIBCXX20_CONSTEXPR
150 _Vector_impl(_Vector_impl&& __x) noexcept
151 : _Tp_alloc_type(std::move(__x)), _Vector_impl_data(std::move(__x))
152 { }
153
154 _GLIBCXX20_CONSTEXPR
155 _Vector_impl(_Tp_alloc_type&& __a) noexcept
156 : _Tp_alloc_type(std::move(__a))
157 { }
158
159 _GLIBCXX20_CONSTEXPR
160 _Vector_impl(_Tp_alloc_type&& __a, _Vector_impl&& __rv) noexcept
161 : _Tp_alloc_type(std::move(__a)), _Vector_impl_data(std::move(__rv))
162 { }
163 #endif
164
165 #if _GLIBCXX_SANITIZE_STD_ALLOCATOR && _GLIBCXX_SANITIZE_VECTOR
166 template<typename = _Tp_alloc_type>
167 struct _Asan
168 {
169 typedef typename __gnu_cxx::__alloc_traits<_Tp_alloc_type>
170 ::size_type size_type;
171
172 static _GLIBCXX20_CONSTEXPR void
173 _S_shrink(_Vector_impl&, size_type) { }
174 static _GLIBCXX20_CONSTEXPR void
175 _S_on_dealloc(_Vector_impl&) { }
176
177 typedef _Vector_impl& _Reinit;
178
179 struct _Grow
180 {
181 _GLIBCXX20_CONSTEXPR _Grow(_Vector_impl&, size_type) { }
182 _GLIBCXX20_CONSTEXPR void _M_grew(size_type) { }
183 };
184 };
185
186 // Enable ASan annotations for memory obtained from std::allocator.
187 template<typename _Up>
188 struct _Asan<allocator<_Up> >
189 {
190 typedef typename __gnu_cxx::__alloc_traits<_Tp_alloc_type>
191 ::size_type size_type;
192
193 // Adjust ASan annotation for [_M_start, _M_end_of_storage) to
194 // mark end of valid region as __curr instead of __prev.
195 static _GLIBCXX20_CONSTEXPR void
196 _S_adjust(_Vector_impl& __impl, pointer __prev, pointer __curr)
197 {
198 #if __cpp_lib_is_constant_evaluated
199 if (std::is_constant_evaluated())
200 return;
201 #endif
202 __sanitizer_annotate_contiguous_container(__impl._M_start,
203 __impl._M_end_of_storage, __prev, __curr);
204 }
205
206 static _GLIBCXX20_CONSTEXPR void
207 _S_grow(_Vector_impl& __impl, size_type __n)
208 { _S_adjust(__impl, __impl._M_finish, __impl._M_finish + __n); }
209
210 static _GLIBCXX20_CONSTEXPR void
211 _S_shrink(_Vector_impl& __impl, size_type __n)
212 { _S_adjust(__impl, __impl._M_finish + __n, __impl._M_finish); }
213
214 static _GLIBCXX20_CONSTEXPR void
215 _S_on_dealloc(_Vector_impl& __impl)
216 {
217 if (__impl._M_start)
218 _S_adjust(__impl, __impl._M_finish, __impl._M_end_of_storage);
219 }
220
221 // Used on reallocation to tell ASan unused capacity is invalid.
222 struct _Reinit
223 {
224 explicit _GLIBCXX20_CONSTEXPR
225 _Reinit(_Vector_impl& __impl) : _M_impl(__impl)
226 {
227 // Mark unused capacity as valid again before deallocating it.
228 _S_on_dealloc(_M_impl);
229 }
230
231 _GLIBCXX20_CONSTEXPR
232 ~_Reinit()
233 {
234 // Mark unused capacity as invalid after reallocation.
235 if (_M_impl._M_start)
236 _S_adjust(_M_impl, _M_impl._M_end_of_storage,
237 _M_impl._M_finish);
238 }
239
240 _Vector_impl& _M_impl;
241
242 #if __cplusplus >= 201103L
243 _Reinit(const _Reinit&) = delete;
244 _Reinit& operator=(const _Reinit&) = delete;
245 #endif
246 };
247
248 // Tell ASan when unused capacity is initialized to be valid.
249 struct _Grow
250 {
251 _GLIBCXX20_CONSTEXPR
252 _Grow(_Vector_impl& __impl, size_type __n)
253 : _M_impl(__impl), _M_n(__n)
254 { _S_grow(_M_impl, __n); }
255
256 _GLIBCXX20_CONSTEXPR
257 ~_Grow() { if (_M_n) _S_shrink(_M_impl, _M_n); }
258
259 _GLIBCXX20_CONSTEXPR
260 void _M_grew(size_type __n) { _M_n -= __n; }
261
262 #if __cplusplus >= 201103L
263 _Grow(const _Grow&) = delete;
264 _Grow& operator=(const _Grow&) = delete;
265 #endif
266 private:
267 _Vector_impl& _M_impl;
268 size_type _M_n;
269 };
270 };
271
272 #define _GLIBCXX_ASAN_ANNOTATE_REINIT \
273 typename _Base::_Vector_impl::template _Asan<>::_Reinit const \
274 __attribute__((__unused__)) __reinit_guard(this->_M_impl)
275 #define _GLIBCXX_ASAN_ANNOTATE_GROW(n) \
276 typename _Base::_Vector_impl::template _Asan<>::_Grow \
277 __attribute__((__unused__)) __grow_guard(this->_M_impl, (n))
278 #define _GLIBCXX_ASAN_ANNOTATE_GREW(n) __grow_guard._M_grew(n)
279 #define _GLIBCXX_ASAN_ANNOTATE_SHRINK(n) \
280 _Base::_Vector_impl::template _Asan<>::_S_shrink(this->_M_impl, n)
281 #define _GLIBCXX_ASAN_ANNOTATE_BEFORE_DEALLOC \
282 _Base::_Vector_impl::template _Asan<>::_S_on_dealloc(this->_M_impl)
283 #else // ! (_GLIBCXX_SANITIZE_STD_ALLOCATOR && _GLIBCXX_SANITIZE_VECTOR)
284 #define _GLIBCXX_ASAN_ANNOTATE_REINIT
285 #define _GLIBCXX_ASAN_ANNOTATE_GROW(n)
286 #define _GLIBCXX_ASAN_ANNOTATE_GREW(n)
287 #define _GLIBCXX_ASAN_ANNOTATE_SHRINK(n)
288 #define _GLIBCXX_ASAN_ANNOTATE_BEFORE_DEALLOC
289 #endif // _GLIBCXX_SANITIZE_STD_ALLOCATOR && _GLIBCXX_SANITIZE_VECTOR
290 };
291
292 public:
293 typedef _Alloc allocator_type;
294
295 _GLIBCXX20_CONSTEXPR
296 _Tp_alloc_type&
297 _M_get_Tp_allocator() _GLIBCXX_NOEXCEPT
298 { return this->_M_impl; }
299
300 _GLIBCXX20_CONSTEXPR
301 const _Tp_alloc_type&
302 _M_get_Tp_allocator() const _GLIBCXX_NOEXCEPT
303 { return this->_M_impl; }
304
305 _GLIBCXX20_CONSTEXPR
306 allocator_type
307 get_allocator() const _GLIBCXX_NOEXCEPT
308 { return allocator_type(_M_get_Tp_allocator()); }
309
310 #if __cplusplus >= 201103L
311 _Vector_base() = default;
312 #else
313 _Vector_base() { }
314 #endif
315
316 _GLIBCXX20_CONSTEXPR
317 _Vector_base(const allocator_type& __a) _GLIBCXX_NOEXCEPT
318 : _M_impl(__a) { }
319
320 // Kept for ABI compatibility.
321 #if !_GLIBCXX_INLINE_VERSION
322 _GLIBCXX20_CONSTEXPR
323 _Vector_base(size_t __n)
324 : _M_impl()
325 { _M_create_storage(__n); }
326 #endif
327
328 _GLIBCXX20_CONSTEXPR
329 _Vector_base(size_t __n, const allocator_type& __a)
330 : _M_impl(__a)
331 { _M_create_storage(__n); }
332
333 #if __cplusplus >= 201103L
334 _Vector_base(_Vector_base&&) = default;
335
336 // Kept for ABI compatibility.
337 # if !_GLIBCXX_INLINE_VERSION
338 _GLIBCXX20_CONSTEXPR
339 _Vector_base(_Tp_alloc_type&& __a) noexcept
340 : _M_impl(std::move(__a)) { }
341
342 _GLIBCXX20_CONSTEXPR
343 _Vector_base(_Vector_base&& __x, const allocator_type& __a)
344 : _M_impl(__a)
345 {
346 if (__x.get_allocator() == __a)
347 this->_M_impl._M_swap_data(__x._M_impl);
348 else
349 {
350 size_t __n = __x._M_impl._M_finish - __x._M_impl._M_start;
351 _M_create_storage(__n);
352 }
353 }
354 # endif
355
356 _GLIBCXX20_CONSTEXPR
357 _Vector_base(const allocator_type& __a, _Vector_base&& __x)
358 : _M_impl(_Tp_alloc_type(__a), std::move(__x._M_impl))
359 { }
360 #endif
361
362 _GLIBCXX20_CONSTEXPR
363 ~_Vector_base() _GLIBCXX_NOEXCEPT
364 {
365 _M_deallocate(_M_impl._M_start,
366 _M_impl._M_end_of_storage - _M_impl._M_start);
367 }
368
369 public:
370 _Vector_impl _M_impl;
371
372 _GLIBCXX20_CONSTEXPR
373 pointer
374 _M_allocate(size_t __n)
375 {
376 typedef __gnu_cxx::__alloc_traits<_Tp_alloc_type> _Tr;
377 return __n != 0 ? _Tr::allocate(_M_impl, __n) : pointer();
378 }
379
380 _GLIBCXX20_CONSTEXPR
381 void
382 _M_deallocate(pointer __p, size_t __n)
383 {
384 typedef __gnu_cxx::__alloc_traits<_Tp_alloc_type> _Tr;
385 if (__p)
386 _Tr::deallocate(_M_impl, __p, __n);
387 }
388
389 protected:
390
391 _GLIBCXX20_CONSTEXPR
392 void
393 _M_create_storage(size_t __n)
394 {
395 this->_M_impl._M_start = this->_M_allocate(__n);
396 this->_M_impl._M_finish = this->_M_impl._M_start;
397 this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __n;
398 }
399 };
400
401 /**
402 * @brief A standard container which offers fixed time access to
403 * individual elements in any order.
404 *
405 * @ingroup sequences
406 * @headerfile vector
407 * @since C++98
408 *
409 * @tparam _Tp Type of element.
410 * @tparam _Alloc Allocator type, defaults to allocator<_Tp>.
411 *
412 * Meets the requirements of a <a href="tables.html#65">container</a>, a
413 * <a href="tables.html#66">reversible container</a>, and a
414 * <a href="tables.html#67">sequence</a>, including the
415 * <a href="tables.html#68">optional sequence requirements</a> with the
416 * %exception of @c push_front and @c pop_front.
417 *
418 * In some terminology a %vector can be described as a dynamic
419 * C-style array, it offers fast and efficient access to individual
420 * elements in any order and saves the user from worrying about
421 * memory and size allocation. Subscripting ( @c [] ) access is
422 * also provided as with C-style arrays.
423 */
424 template<typename _Tp, typename _Alloc = std::allocator<_Tp> >
425 class vector : protected _Vector_base<_Tp, _Alloc>
426 {
427 #ifdef _GLIBCXX_CONCEPT_CHECKS
428 // Concept requirements.
429 typedef typename _Alloc::value_type _Alloc_value_type;
430 # if __cplusplus < 201103L
431 __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
432 # endif
433 __glibcxx_class_requires2(_Tp, _Alloc_value_type, _SameTypeConcept)
434 #endif
435
436 #if __cplusplus >= 201103L
437 static_assert(is_same<typename remove_cv<_Tp>::type, _Tp>::value,
438 "std::vector must have a non-const, non-volatile value_type");
439 # if __cplusplus > 201703L || defined __STRICT_ANSI__
440 static_assert(is_same<typename _Alloc::value_type, _Tp>::value,
441 "std::vector must have the same value_type as its allocator");
442 # endif
443 #endif
444
445 typedef _Vector_base<_Tp, _Alloc> _Base;
446 typedef typename _Base::_Tp_alloc_type _Tp_alloc_type;
447 typedef __gnu_cxx::__alloc_traits<_Tp_alloc_type> _Alloc_traits;
448
449 public:
450 typedef _Tp value_type;
451 typedef typename _Base::pointer pointer;
452 typedef typename _Alloc_traits::const_pointer const_pointer;
453 typedef typename _Alloc_traits::reference reference;
454 typedef typename _Alloc_traits::const_reference const_reference;
455 typedef __gnu_cxx::__normal_iterator<pointer, vector> iterator;
456 typedef __gnu_cxx::__normal_iterator<const_pointer, vector>
457 const_iterator;
458 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
459 typedef std::reverse_iterator<iterator> reverse_iterator;
460 typedef size_t size_type;
461 typedef ptrdiff_t difference_type;
462 typedef _Alloc allocator_type;
463
464 private:
465 #if __cplusplus >= 201103L
466 static constexpr bool
467 _S_nothrow_relocate(true_type)
468 {
469 return noexcept(std::__relocate_a(std::declval<pointer>(),
470 std::declval<pointer>(),
471 std::declval<pointer>(),
472 std::declval<_Tp_alloc_type&>()));
473 }
474
475 static constexpr bool
476 _S_nothrow_relocate(false_type)
477 { return false; }
478
479 static constexpr bool
480 _S_use_relocate()
481 {
482 // Instantiating std::__relocate_a might cause an error outside the
483 // immediate context (in __relocate_object_a's noexcept-specifier),
484 // so only do it if we know the type can be move-inserted into *this.
485 return _S_nothrow_relocate(__is_move_insertable<_Tp_alloc_type>{});
486 }
487
488 static pointer
489 _S_do_relocate(pointer __first, pointer __last, pointer __result,
490 _Tp_alloc_type& __alloc, true_type) noexcept
491 {
492 return std::__relocate_a(__first, __last, __result, __alloc);
493 }
494
495 static pointer
496 _S_do_relocate(pointer, pointer, pointer __result,
497 _Tp_alloc_type&, false_type) noexcept
498 { return __result; }
499
500 static _GLIBCXX20_CONSTEXPR pointer
501 _S_relocate(pointer __first, pointer __last, pointer __result,
502 _Tp_alloc_type& __alloc) noexcept
503 {
504 #if __cpp_if_constexpr
505 // All callers have already checked _S_use_relocate() so just do it.
506 return std::__relocate_a(__first, __last, __result, __alloc);
507 #else
508 using __do_it = __bool_constant<_S_use_relocate()>;
509 return _S_do_relocate(__first, __last, __result, __alloc, __do_it{});
510 #endif
511 }
512 #endif // C++11
513
514 protected:
515 using _Base::_M_allocate;
516 using _Base::_M_deallocate;
517 using _Base::_M_impl;
518 using _Base::_M_get_Tp_allocator;
519
520 public:
521 // [23.2.4.1] construct/copy/destroy
522 // (assign() and get_allocator() are also listed in this section)
523
524 /**
525 * @brief Creates a %vector with no elements.
526 */
527 #if __cplusplus >= 201103L
528 vector() = default;
529 #else
530 vector() { }
531 #endif
532
533 /**
534 * @brief Creates a %vector with no elements.
535 * @param __a An allocator object.
536 */
537 explicit
538 _GLIBCXX20_CONSTEXPR
539 vector(const allocator_type& __a) _GLIBCXX_NOEXCEPT
540 : _Base(__a) { }
541
542 #if __cplusplus >= 201103L
543 /**
544 * @brief Creates a %vector with default constructed elements.
545 * @param __n The number of elements to initially create.
546 * @param __a An allocator.
547 *
548 * This constructor fills the %vector with @a __n default
549 * constructed elements.
550 */
551 explicit
552 _GLIBCXX20_CONSTEXPR
553 vector(size_type __n, const allocator_type& __a = allocator_type())
554 : _Base(_S_check_init_len(__n, __a), __a)
555 { _M_default_initialize(__n); }
556
557 /**
558 * @brief Creates a %vector with copies of an exemplar element.
559 * @param __n The number of elements to initially create.
560 * @param __value An element to copy.
561 * @param __a An allocator.
562 *
563 * This constructor fills the %vector with @a __n copies of @a __value.
564 */
565 _GLIBCXX20_CONSTEXPR
566 vector(size_type __n, const value_type& __value,
567 const allocator_type& __a = allocator_type())
568 : _Base(_S_check_init_len(__n, __a), __a)
569 { _M_fill_initialize(__n, __value); }
570 #else
571 /**
572 * @brief Creates a %vector with copies of an exemplar element.
573 * @param __n The number of elements to initially create.
574 * @param __value An element to copy.
575 * @param __a An allocator.
576 *
577 * This constructor fills the %vector with @a __n copies of @a __value.
578 */
579 explicit
580 vector(size_type __n, const value_type& __value = value_type(),
581 const allocator_type& __a = allocator_type())
582 : _Base(_S_check_init_len(__n, __a), __a)
583 { _M_fill_initialize(__n, __value); }
584 #endif
585
586 /**
587 * @brief %Vector copy constructor.
588 * @param __x A %vector of identical element and allocator types.
589 *
590 * All the elements of @a __x are copied, but any unused capacity in
591 * @a __x will not be copied
592 * (i.e. capacity() == size() in the new %vector).
593 *
594 * The newly-created %vector uses a copy of the allocator object used
595 * by @a __x (unless the allocator traits dictate a different object).
596 */
597 _GLIBCXX20_CONSTEXPR
598 vector(const vector& __x)
599 : _Base(__x.size(),
600 _Alloc_traits::_S_select_on_copy(__x._M_get_Tp_allocator()))
601 {
602 this->_M_impl._M_finish =
603 std::__uninitialized_copy_a(__x.begin(), __x.end(),
604 this->_M_impl._M_start,
605 _M_get_Tp_allocator());
606 }
607
608 #if __cplusplus >= 201103L
609 /**
610 * @brief %Vector move constructor.
611 *
612 * The newly-created %vector contains the exact contents of the
613 * moved instance.
614 * The contents of the moved instance are a valid, but unspecified
615 * %vector.
616 */
617 vector(vector&&) noexcept = default;
618
619 /// Copy constructor with alternative allocator
620 _GLIBCXX20_CONSTEXPR
621 vector(const vector& __x, const __type_identity_t<allocator_type>& __a)
622 : _Base(__x.size(), __a)
623 {
624 this->_M_impl._M_finish =
625 std::__uninitialized_copy_a(__x.begin(), __x.end(),
626 this->_M_impl._M_start,
627 _M_get_Tp_allocator());
628 }
629
630 private:
631 _GLIBCXX20_CONSTEXPR
632 vector(vector&& __rv, const allocator_type& __m, true_type) noexcept
633 : _Base(__m, std::move(__rv))
634 { }
635
636 _GLIBCXX20_CONSTEXPR
637 vector(vector&& __rv, const allocator_type& __m, false_type)
638 : _Base(__m)
639 {
640 if (__rv.get_allocator() == __m)
641 this->_M_impl._M_swap_data(__rv._M_impl);
642 else if (!__rv.empty())
643 {
644 this->_M_create_storage(__rv.size());
645 this->_M_impl._M_finish =
646 std::__uninitialized_move_a(__rv.begin(), __rv.end(),
647 this->_M_impl._M_start,
648 _M_get_Tp_allocator());
649 __rv.clear();
650 }
651 }
652
653 public:
654 /// Move constructor with alternative allocator
655 _GLIBCXX20_CONSTEXPR
656 vector(vector&& __rv, const __type_identity_t<allocator_type>& __m)
657 noexcept( noexcept(
658 vector(std::declval<vector&&>(), std::declval<const allocator_type&>(),
659 std::declval<typename _Alloc_traits::is_always_equal>())) )
660 : vector(std::move(__rv), __m, typename _Alloc_traits::is_always_equal{})
661 { }
662
663 /**
664 * @brief Builds a %vector from an initializer list.
665 * @param __l An initializer_list.
666 * @param __a An allocator.
667 *
668 * Create a %vector consisting of copies of the elements in the
669 * initializer_list @a __l.
670 *
671 * This will call the element type's copy constructor N times
672 * (where N is @a __l.size()) and do no memory reallocation.
673 */
674 _GLIBCXX20_CONSTEXPR
675 vector(initializer_list<value_type> __l,
676 const allocator_type& __a = allocator_type())
677 : _Base(__a)
678 {
679 _M_range_initialize(__l.begin(), __l.end(),
680 random_access_iterator_tag());
681 }
682 #endif
683
684 /**
685 * @brief Builds a %vector from a range.
686 * @param __first An input iterator.
687 * @param __last An input iterator.
688 * @param __a An allocator.
689 *
690 * Create a %vector consisting of copies of the elements from
691 * [first,last).
692 *
693 * If the iterators are forward, bidirectional, or
694 * random-access, then this will call the elements' copy
695 * constructor N times (where N is distance(first,last)) and do
696 * no memory reallocation. But if only input iterators are
697 * used, then this will do at most 2N calls to the copy
698 * constructor, and logN memory reallocations.
699 */
700 #if __cplusplus >= 201103L
701 template<typename _InputIterator,
702 typename = std::_RequireInputIter<_InputIterator>>
703 _GLIBCXX20_CONSTEXPR
704 vector(_InputIterator __first, _InputIterator __last,
705 const allocator_type& __a = allocator_type())
706 : _Base(__a)
707 {
708 _M_range_initialize(__first, __last,
709 std::__iterator_category(__first));
710 }
711 #else
712 template<typename _InputIterator>
713 vector(_InputIterator __first, _InputIterator __last,
714 const allocator_type& __a = allocator_type())
715 : _Base(__a)
716 {
717 // Check whether it's an integral type. If so, it's not an iterator.
718 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
719 _M_initialize_dispatch(__first, __last, _Integral());
720 }
721 #endif
722
723 /**
724 * The dtor only erases the elements, and note that if the
725 * elements themselves are pointers, the pointed-to memory is
726 * not touched in any way. Managing the pointer is the user's
727 * responsibility.
728 */
729 _GLIBCXX20_CONSTEXPR
730 ~vector() _GLIBCXX_NOEXCEPT
731 {
732 std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
733 _M_get_Tp_allocator());
734 _GLIBCXX_ASAN_ANNOTATE_BEFORE_DEALLOC;
735 }
736
737 /**
738 * @brief %Vector assignment operator.
739 * @param __x A %vector of identical element and allocator types.
740 *
741 * All the elements of @a __x are copied, but any unused capacity in
742 * @a __x will not be copied.
743 *
744 * Whether the allocator is copied depends on the allocator traits.
745 */
746 _GLIBCXX20_CONSTEXPR
747 vector&
748 operator=(const vector& __x);
749
750 #if __cplusplus >= 201103L
751 /**
752 * @brief %Vector move assignment operator.
753 * @param __x A %vector of identical element and allocator types.
754 *
755 * The contents of @a __x are moved into this %vector (without copying,
756 * if the allocators permit it).
757 * Afterwards @a __x is a valid, but unspecified %vector.
758 *
759 * Whether the allocator is moved depends on the allocator traits.
760 */
761 _GLIBCXX20_CONSTEXPR
762 vector&
763 operator=(vector&& __x) noexcept(_Alloc_traits::_S_nothrow_move())
764 {
765 constexpr bool __move_storage =
766 _Alloc_traits::_S_propagate_on_move_assign()
767 || _Alloc_traits::_S_always_equal();
768 _M_move_assign(std::move(__x), __bool_constant<__move_storage>());
769 return *this;
770 }
771
772 /**
773 * @brief %Vector list assignment operator.
774 * @param __l An initializer_list.
775 *
776 * This function fills a %vector with copies of the elements in the
777 * initializer list @a __l.
778 *
779 * Note that the assignment completely changes the %vector and
780 * that the resulting %vector's size is the same as the number
781 * of elements assigned.
782 */
783 _GLIBCXX20_CONSTEXPR
784 vector&
785 operator=(initializer_list<value_type> __l)
786 {
787 this->_M_assign_aux(__l.begin(), __l.end(),
788 random_access_iterator_tag());
789 return *this;
790 }
791 #endif
792
793 /**
794 * @brief Assigns a given value to a %vector.
795 * @param __n Number of elements to be assigned.
796 * @param __val Value to be assigned.
797 *
798 * This function fills a %vector with @a __n copies of the given
799 * value. Note that the assignment completely changes the
800 * %vector and that the resulting %vector's size is the same as
801 * the number of elements assigned.
802 */
803 _GLIBCXX20_CONSTEXPR
804 void
805 assign(size_type __n, const value_type& __val)
806 { _M_fill_assign(__n, __val); }
807
808 /**
809 * @brief Assigns a range to a %vector.
810 * @param __first An input iterator.
811 * @param __last An input iterator.
812 *
813 * This function fills a %vector with copies of the elements in the
814 * range [__first,__last).
815 *
816 * Note that the assignment completely changes the %vector and
817 * that the resulting %vector's size is the same as the number
818 * of elements assigned.
819 */
820 #if __cplusplus >= 201103L
821 template<typename _InputIterator,
822 typename = std::_RequireInputIter<_InputIterator>>
823 _GLIBCXX20_CONSTEXPR
824 void
825 assign(_InputIterator __first, _InputIterator __last)
826 { _M_assign_aux(__first, __last, std::__iterator_category(__first)); }
827 #else
828 template<typename _InputIterator>
829 void
830 assign(_InputIterator __first, _InputIterator __last)
831 {
832 // Check whether it's an integral type. If so, it's not an iterator.
833 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
834 _M_assign_dispatch(__first, __last, _Integral());
835 }
836 #endif
837
838 #if __cplusplus >= 201103L
839 /**
840 * @brief Assigns an initializer list to a %vector.
841 * @param __l An initializer_list.
842 *
843 * This function fills a %vector with copies of the elements in the
844 * initializer list @a __l.
845 *
846 * Note that the assignment completely changes the %vector and
847 * that the resulting %vector's size is the same as the number
848 * of elements assigned.
849 */
850 _GLIBCXX20_CONSTEXPR
851 void
852 assign(initializer_list<value_type> __l)
853 {
854 this->_M_assign_aux(__l.begin(), __l.end(),
855 random_access_iterator_tag());
856 }
857 #endif
858
859 /// Get a copy of the memory allocation object.
860 using _Base::get_allocator;
861
862 // iterators
863 /**
864 * Returns a read/write iterator that points to the first
865 * element in the %vector. Iteration is done in ordinary
866 * element order.
867 */
868 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
869 iterator
870 begin() _GLIBCXX_NOEXCEPT
871 { return iterator(this->_M_impl._M_start); }
872
873 /**
874 * Returns a read-only (constant) iterator that points to the
875 * first element in the %vector. Iteration is done in ordinary
876 * element order.
877 */
878 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
879 const_iterator
880 begin() const _GLIBCXX_NOEXCEPT
881 { return const_iterator(this->_M_impl._M_start); }
882
883 /**
884 * Returns a read/write iterator that points one past the last
885 * element in the %vector. Iteration is done in ordinary
886 * element order.
887 */
888 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
889 iterator
890 end() _GLIBCXX_NOEXCEPT
891 { return iterator(this->_M_impl._M_finish); }
892
893 /**
894 * Returns a read-only (constant) iterator that points one past
895 * the last element in the %vector. Iteration is done in
896 * ordinary element order.
897 */
898 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
899 const_iterator
900 end() const _GLIBCXX_NOEXCEPT
901 { return const_iterator(this->_M_impl._M_finish); }
902
903 /**
904 * Returns a read/write reverse iterator that points to the
905 * last element in the %vector. Iteration is done in reverse
906 * element order.
907 */
908 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
909 reverse_iterator
910 rbegin() _GLIBCXX_NOEXCEPT
911 { return reverse_iterator(end()); }
912
913 /**
914 * Returns a read-only (constant) reverse iterator that points
915 * to the last element in the %vector. Iteration is done in
916 * reverse element order.
917 */
918 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
919 const_reverse_iterator
920 rbegin() const _GLIBCXX_NOEXCEPT
921 { return const_reverse_iterator(end()); }
922
923 /**
924 * Returns a read/write reverse iterator that points to one
925 * before the first element in the %vector. Iteration is done
926 * in reverse element order.
927 */
928 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
929 reverse_iterator
930 rend() _GLIBCXX_NOEXCEPT
931 { return reverse_iterator(begin()); }
932
933 /**
934 * Returns a read-only (constant) reverse iterator that points
935 * to one before the first element in the %vector. Iteration
936 * is done in reverse element order.
937 */
938 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
939 const_reverse_iterator
940 rend() const _GLIBCXX_NOEXCEPT
941 { return const_reverse_iterator(begin()); }
942
943 #if __cplusplus >= 201103L
944 /**
945 * Returns a read-only (constant) iterator that points to the
946 * first element in the %vector. Iteration is done in ordinary
947 * element order.
948 */
949 [[__nodiscard__]] _GLIBCXX20_CONSTEXPR
950 const_iterator
951 cbegin() const noexcept
952 { return const_iterator(this->_M_impl._M_start); }
953
954 /**
955 * Returns a read-only (constant) iterator that points one past
956 * the last element in the %vector. Iteration is done in
957 * ordinary element order.
958 */
959 [[__nodiscard__]] _GLIBCXX20_CONSTEXPR
960 const_iterator
961 cend() const noexcept
962 { return const_iterator(this->_M_impl._M_finish); }
963
964 /**
965 * Returns a read-only (constant) reverse iterator that points
966 * to the last element in the %vector. Iteration is done in
967 * reverse element order.
968 */
969 [[__nodiscard__]] _GLIBCXX20_CONSTEXPR
970 const_reverse_iterator
971 crbegin() const noexcept
972 { return const_reverse_iterator(end()); }
973
974 /**
975 * Returns a read-only (constant) reverse iterator that points
976 * to one before the first element in the %vector. Iteration
977 * is done in reverse element order.
978 */
979 [[__nodiscard__]] _GLIBCXX20_CONSTEXPR
980 const_reverse_iterator
981 crend() const noexcept
982 { return const_reverse_iterator(begin()); }
983 #endif
984
985 // [23.2.4.2] capacity
986 /** Returns the number of elements in the %vector. */
987 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
988 size_type
989 size() const _GLIBCXX_NOEXCEPT
990 { return size_type(this->_M_impl._M_finish - this->_M_impl._M_start); }
991
992 /** Returns the size() of the largest possible %vector. */
993 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
994 size_type
995 max_size() const _GLIBCXX_NOEXCEPT
996 { return _S_max_size(_M_get_Tp_allocator()); }
997
998 #if __cplusplus >= 201103L
999 /**
1000 * @brief Resizes the %vector to the specified number of elements.
1001 * @param __new_size Number of elements the %vector should contain.
1002 *
1003 * This function will %resize the %vector to the specified
1004 * number of elements. If the number is smaller than the
1005 * %vector's current size the %vector is truncated, otherwise
1006 * default constructed elements are appended.
1007 */
1008 _GLIBCXX20_CONSTEXPR
1009 void
1010 resize(size_type __new_size)
1011 {
1012 if (__new_size > size())
1013 _M_default_append(__new_size - size());
1014 else if (__new_size < size())
1015 _M_erase_at_end(this->_M_impl._M_start + __new_size);
1016 }
1017
1018 /**
1019 * @brief Resizes the %vector to the specified number of elements.
1020 * @param __new_size Number of elements the %vector should contain.
1021 * @param __x Data with which new elements should be populated.
1022 *
1023 * This function will %resize the %vector to the specified
1024 * number of elements. If the number is smaller than the
1025 * %vector's current size the %vector is truncated, otherwise
1026 * the %vector is extended and new elements are populated with
1027 * given data.
1028 */
1029 _GLIBCXX20_CONSTEXPR
1030 void
1031 resize(size_type __new_size, const value_type& __x)
1032 {
1033 if (__new_size > size())
1034 _M_fill_insert(end(), __new_size - size(), __x);
1035 else if (__new_size < size())
1036 _M_erase_at_end(this->_M_impl._M_start + __new_size);
1037 }
1038 #else
1039 /**
1040 * @brief Resizes the %vector to the specified number of elements.
1041 * @param __new_size Number of elements the %vector should contain.
1042 * @param __x Data with which new elements should be populated.
1043 *
1044 * This function will %resize the %vector to the specified
1045 * number of elements. If the number is smaller than the
1046 * %vector's current size the %vector is truncated, otherwise
1047 * the %vector is extended and new elements are populated with
1048 * given data.
1049 */
1050 _GLIBCXX20_CONSTEXPR
1051 void
1052 resize(size_type __new_size, value_type __x = value_type())
1053 {
1054 if (__new_size > size())
1055 _M_fill_insert(end(), __new_size - size(), __x);
1056 else if (__new_size < size())
1057 _M_erase_at_end(this->_M_impl._M_start + __new_size);
1058 }
1059 #endif
1060
1061 #if __cplusplus >= 201103L
1062 /** A non-binding request to reduce capacity() to size(). */
1063 _GLIBCXX20_CONSTEXPR
1064 void
1065 shrink_to_fit()
1066 { _M_shrink_to_fit(); }
1067 #endif
1068
1069 /**
1070 * Returns the total number of elements that the %vector can
1071 * hold before needing to allocate more memory.
1072 */
1073 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1074 size_type
1075 capacity() const _GLIBCXX_NOEXCEPT
1076 {
1077 return size_type(this->_M_impl._M_end_of_storage
1078 - this->_M_impl._M_start);
1079 }
1080
1081 /**
1082 * Returns true if the %vector is empty. (Thus begin() would
1083 * equal end().)
1084 */
1085 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1086 bool
1087 empty() const _GLIBCXX_NOEXCEPT
1088 { return begin() == end(); }
1089
1090 /**
1091 * @brief Attempt to preallocate enough memory for specified number of
1092 * elements.
1093 * @param __n Number of elements required.
1094 * @throw std::length_error If @a n exceeds @c max_size().
1095 *
1096 * This function attempts to reserve enough memory for the
1097 * %vector to hold the specified number of elements. If the
1098 * number requested is more than max_size(), length_error is
1099 * thrown.
1100 *
1101 * The advantage of this function is that if optimal code is a
1102 * necessity and the user can determine the number of elements
1103 * that will be required, the user can reserve the memory in
1104 * %advance, and thus prevent a possible reallocation of memory
1105 * and copying of %vector data.
1106 */
1107 _GLIBCXX20_CONSTEXPR
1108 void
1109 reserve(size_type __n);
1110
1111 // element access
1112 /**
1113 * @brief Subscript access to the data contained in the %vector.
1114 * @param __n The index of the element for which data should be
1115 * accessed.
1116 * @return Read/write reference to data.
1117 *
1118 * This operator allows for easy, array-style, data access.
1119 * Note that data access with this operator is unchecked and
1120 * out_of_range lookups are not defined. (For checked lookups
1121 * see at().)
1122 */
1123 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1124 reference
1125 operator[](size_type __n) _GLIBCXX_NOEXCEPT
1126 {
1127 __glibcxx_requires_subscript(__n);
1128 return *(this->_M_impl._M_start + __n);
1129 }
1130
1131 /**
1132 * @brief Subscript access to the data contained in the %vector.
1133 * @param __n The index of the element for which data should be
1134 * accessed.
1135 * @return Read-only (constant) reference to data.
1136 *
1137 * This operator allows for easy, array-style, data access.
1138 * Note that data access with this operator is unchecked and
1139 * out_of_range lookups are not defined. (For checked lookups
1140 * see at().)
1141 */
1142 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1143 const_reference
1144 operator[](size_type __n) const _GLIBCXX_NOEXCEPT
1145 {
1146 __glibcxx_requires_subscript(__n);
1147 return *(this->_M_impl._M_start + __n);
1148 }
1149
1150 protected:
1151 /// Safety check used only from at().
1152 _GLIBCXX20_CONSTEXPR
1153 void
1154 _M_range_check(size_type __n) const
1155 {
1156 if (__n >= this->size())
1157 __throw_out_of_range_fmt(__N("vector::_M_range_check: __n "
1158 "(which is %zu) >= this->size() "
1159 "(which is %zu)"),
1160 __n, this->size());
1161 }
1162
1163 public:
1164 /**
1165 * @brief Provides access to the data contained in the %vector.
1166 * @param __n The index of the element for which data should be
1167 * accessed.
1168 * @return Read/write reference to data.
1169 * @throw std::out_of_range If @a __n is an invalid index.
1170 *
1171 * This function provides for safer data access. The parameter
1172 * is first checked that it is in the range of the vector. The
1173 * function throws out_of_range if the check fails.
1174 */
1175 _GLIBCXX20_CONSTEXPR
1176 reference
1177 at(size_type __n)
1178 {
1179 _M_range_check(__n);
1180 return (*this)[__n];
1181 }
1182
1183 /**
1184 * @brief Provides access to the data contained in the %vector.
1185 * @param __n The index of the element for which data should be
1186 * accessed.
1187 * @return Read-only (constant) reference to data.
1188 * @throw std::out_of_range If @a __n is an invalid index.
1189 *
1190 * This function provides for safer data access. The parameter
1191 * is first checked that it is in the range of the vector. The
1192 * function throws out_of_range if the check fails.
1193 */
1194 _GLIBCXX20_CONSTEXPR
1195 const_reference
1196 at(size_type __n) const
1197 {
1198 _M_range_check(__n);
1199 return (*this)[__n];
1200 }
1201
1202 /**
1203 * Returns a read/write reference to the data at the first
1204 * element of the %vector.
1205 */
1206 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1207 reference
1208 front() _GLIBCXX_NOEXCEPT
1209 {
1210 __glibcxx_requires_nonempty();
1211 return *begin();
1212 }
1213
1214 /**
1215 * Returns a read-only (constant) reference to the data at the first
1216 * element of the %vector.
1217 */
1218 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1219 const_reference
1220 front() const _GLIBCXX_NOEXCEPT
1221 {
1222 __glibcxx_requires_nonempty();
1223 return *begin();
1224 }
1225
1226 /**
1227 * Returns a read/write reference to the data at the last
1228 * element of the %vector.
1229 */
1230 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1231 reference
1232 back() _GLIBCXX_NOEXCEPT
1233 {
1234 __glibcxx_requires_nonempty();
1235 return *(end() - 1);
1236 }
1237
1238 /**
1239 * Returns a read-only (constant) reference to the data at the
1240 * last element of the %vector.
1241 */
1242 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1243 const_reference
1244 back() const _GLIBCXX_NOEXCEPT
1245 {
1246 __glibcxx_requires_nonempty();
1247 return *(end() - 1);
1248 }
1249
1250 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1251 // DR 464. Suggestion for new member functions in standard containers.
1252 // data access
1253 /**
1254 * Returns a pointer such that [data(), data() + size()) is a valid
1255 * range. For a non-empty %vector, data() == &front().
1256 */
1257 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1258 _Tp*
1259 data() _GLIBCXX_NOEXCEPT
1260 { return _M_data_ptr(this->_M_impl._M_start); }
1261
1262 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1263 const _Tp*
1264 data() const _GLIBCXX_NOEXCEPT
1265 { return _M_data_ptr(this->_M_impl._M_start); }
1266
1267 // [23.2.4.3] modifiers
1268 /**
1269 * @brief Add data to the end of the %vector.
1270 * @param __x Data to be added.
1271 *
1272 * This is a typical stack operation. The function creates an
1273 * element at the end of the %vector and assigns the given data
1274 * to it. Due to the nature of a %vector this operation can be
1275 * done in constant time if the %vector has preallocated space
1276 * available.
1277 */
1278 _GLIBCXX20_CONSTEXPR
1279 void
1280 push_back(const value_type& __x)
1281 {
1282 if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage)
1283 {
1284 _GLIBCXX_ASAN_ANNOTATE_GROW(1);
1285 _Alloc_traits::construct(this->_M_impl, this->_M_impl._M_finish,
1286 __x);
1287 ++this->_M_impl._M_finish;
1288 _GLIBCXX_ASAN_ANNOTATE_GREW(1);
1289 }
1290 else
1291 _M_realloc_insert(end(), __x);
1292 }
1293
1294 #if __cplusplus >= 201103L
1295 _GLIBCXX20_CONSTEXPR
1296 void
1297 push_back(value_type&& __x)
1298 { emplace_back(std::move(__x)); }
1299
1300 template<typename... _Args>
1301 #if __cplusplus > 201402L
1302 _GLIBCXX20_CONSTEXPR
1303 reference
1304 #else
1305 void
1306 #endif
1307 emplace_back(_Args&&... __args);
1308 #endif
1309
1310 /**
1311 * @brief Removes last element.
1312 *
1313 * This is a typical stack operation. It shrinks the %vector by one.
1314 *
1315 * Note that no data is returned, and if the last element's
1316 * data is needed, it should be retrieved before pop_back() is
1317 * called.
1318 */
1319 _GLIBCXX20_CONSTEXPR
1320 void
1321 pop_back() _GLIBCXX_NOEXCEPT
1322 {
1323 __glibcxx_requires_nonempty();
1324 --this->_M_impl._M_finish;
1325 _Alloc_traits::destroy(this->_M_impl, this->_M_impl._M_finish);
1326 _GLIBCXX_ASAN_ANNOTATE_SHRINK(1);
1327 }
1328
1329 #if __cplusplus >= 201103L
1330 /**
1331 * @brief Inserts an object in %vector before specified iterator.
1332 * @param __position A const_iterator into the %vector.
1333 * @param __args Arguments.
1334 * @return An iterator that points to the inserted data.
1335 *
1336 * This function will insert an object of type T constructed
1337 * with T(std::forward<Args>(args)...) before the specified location.
1338 * Note that this kind of operation could be expensive for a %vector
1339 * and if it is frequently used the user should consider using
1340 * std::list.
1341 */
1342 template<typename... _Args>
1343 _GLIBCXX20_CONSTEXPR
1344 iterator
1345 emplace(const_iterator __position, _Args&&... __args)
1346 { return _M_emplace_aux(__position, std::forward<_Args>(__args)...); }
1347
1348 /**
1349 * @brief Inserts given value into %vector before specified iterator.
1350 * @param __position A const_iterator into the %vector.
1351 * @param __x Data to be inserted.
1352 * @return An iterator that points to the inserted data.
1353 *
1354 * This function will insert a copy of the given value before
1355 * the specified location. Note that this kind of operation
1356 * could be expensive for a %vector and if it is frequently
1357 * used the user should consider using std::list.
1358 */
1359 _GLIBCXX20_CONSTEXPR
1360 iterator
1361 insert(const_iterator __position, const value_type& __x);
1362 #else
1363 /**
1364 * @brief Inserts given value into %vector before specified iterator.
1365 * @param __position An iterator into the %vector.
1366 * @param __x Data to be inserted.
1367 * @return An iterator that points to the inserted data.
1368 *
1369 * This function will insert a copy of the given value before
1370 * the specified location. Note that this kind of operation
1371 * could be expensive for a %vector and if it is frequently
1372 * used the user should consider using std::list.
1373 */
1374 iterator
1375 insert(iterator __position, const value_type& __x);
1376 #endif
1377
1378 #if __cplusplus >= 201103L
1379 /**
1380 * @brief Inserts given rvalue into %vector before specified iterator.
1381 * @param __position A const_iterator into the %vector.
1382 * @param __x Data to be inserted.
1383 * @return An iterator that points to the inserted data.
1384 *
1385 * This function will insert a copy of the given rvalue before
1386 * the specified location. Note that this kind of operation
1387 * could be expensive for a %vector and if it is frequently
1388 * used the user should consider using std::list.
1389 */
1390 _GLIBCXX20_CONSTEXPR
1391 iterator
1392 insert(const_iterator __position, value_type&& __x)
1393 { return _M_insert_rval(__position, std::move(__x)); }
1394
1395 /**
1396 * @brief Inserts an initializer_list into the %vector.
1397 * @param __position An iterator into the %vector.
1398 * @param __l An initializer_list.
1399 *
1400 * This function will insert copies of the data in the
1401 * initializer_list @a l into the %vector before the location
1402 * specified by @a position.
1403 *
1404 * Note that this kind of operation could be expensive for a
1405 * %vector and if it is frequently used the user should
1406 * consider using std::list.
1407 */
1408 _GLIBCXX20_CONSTEXPR
1409 iterator
1410 insert(const_iterator __position, initializer_list<value_type> __l)
1411 {
1412 auto __offset = __position - cbegin();
1413 _M_range_insert(begin() + __offset, __l.begin(), __l.end(),
1414 std::random_access_iterator_tag());
1415 return begin() + __offset;
1416 }
1417 #endif
1418
1419 #if __cplusplus >= 201103L
1420 /**
1421 * @brief Inserts a number of copies of given data into the %vector.
1422 * @param __position A const_iterator into the %vector.
1423 * @param __n Number of elements to be inserted.
1424 * @param __x Data to be inserted.
1425 * @return An iterator that points to the inserted data.
1426 *
1427 * This function will insert a specified number of copies of
1428 * the given data before the location specified by @a position.
1429 *
1430 * Note that this kind of operation could be expensive for a
1431 * %vector and if it is frequently used the user should
1432 * consider using std::list.
1433 */
1434 _GLIBCXX20_CONSTEXPR
1435 iterator
1436 insert(const_iterator __position, size_type __n, const value_type& __x)
1437 {
1438 difference_type __offset = __position - cbegin();
1439 _M_fill_insert(begin() + __offset, __n, __x);
1440 return begin() + __offset;
1441 }
1442 #else
1443 /**
1444 * @brief Inserts a number of copies of given data into the %vector.
1445 * @param __position An iterator into the %vector.
1446 * @param __n Number of elements to be inserted.
1447 * @param __x Data to be inserted.
1448 *
1449 * This function will insert a specified number of copies of
1450 * the given data before the location specified by @a position.
1451 *
1452 * Note that this kind of operation could be expensive for a
1453 * %vector and if it is frequently used the user should
1454 * consider using std::list.
1455 */
1456 void
1457 insert(iterator __position, size_type __n, const value_type& __x)
1458 { _M_fill_insert(__position, __n, __x); }
1459 #endif
1460
1461 #if __cplusplus >= 201103L
1462 /**
1463 * @brief Inserts a range into the %vector.
1464 * @param __position A const_iterator into the %vector.
1465 * @param __first An input iterator.
1466 * @param __last An input iterator.
1467 * @return An iterator that points to the inserted data.
1468 *
1469 * This function will insert copies of the data in the range
1470 * [__first,__last) into the %vector before the location specified
1471 * by @a pos.
1472 *
1473 * Note that this kind of operation could be expensive for a
1474 * %vector and if it is frequently used the user should
1475 * consider using std::list.
1476 */
1477 template<typename _InputIterator,
1478 typename = std::_RequireInputIter<_InputIterator>>
1479 _GLIBCXX20_CONSTEXPR
1480 iterator
1481 insert(const_iterator __position, _InputIterator __first,
1482 _InputIterator __last)
1483 {
1484 difference_type __offset = __position - cbegin();
1485 _M_range_insert(begin() + __offset, __first, __last,
1486 std::__iterator_category(__first));
1487 return begin() + __offset;
1488 }
1489 #else
1490 /**
1491 * @brief Inserts a range into the %vector.
1492 * @param __position An iterator into the %vector.
1493 * @param __first An input iterator.
1494 * @param __last An input iterator.
1495 *
1496 * This function will insert copies of the data in the range
1497 * [__first,__last) into the %vector before the location specified
1498 * by @a pos.
1499 *
1500 * Note that this kind of operation could be expensive for a
1501 * %vector and if it is frequently used the user should
1502 * consider using std::list.
1503 */
1504 template<typename _InputIterator>
1505 void
1506 insert(iterator __position, _InputIterator __first,
1507 _InputIterator __last)
1508 {
1509 // Check whether it's an integral type. If so, it's not an iterator.
1510 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
1511 _M_insert_dispatch(__position, __first, __last, _Integral());
1512 }
1513 #endif
1514
1515 /**
1516 * @brief Remove element at given position.
1517 * @param __position Iterator pointing to element to be erased.
1518 * @return An iterator pointing to the next element (or end()).
1519 *
1520 * This function will erase the element at the given position and thus
1521 * shorten the %vector by one.
1522 *
1523 * Note This operation could be expensive and if it is
1524 * frequently used the user should consider using std::list.
1525 * The user is also cautioned that this function only erases
1526 * the element, and that if the element is itself a pointer,
1527 * the pointed-to memory is not touched in any way. Managing
1528 * the pointer is the user's responsibility.
1529 */
1530 _GLIBCXX20_CONSTEXPR
1531 iterator
1532 #if __cplusplus >= 201103L
1533 erase(const_iterator __position)
1534 { return _M_erase(begin() + (__position - cbegin())); }
1535 #else
1536 erase(iterator __position)
1537 { return _M_erase(__position); }
1538 #endif
1539
1540 /**
1541 * @brief Remove a range of elements.
1542 * @param __first Iterator pointing to the first element to be erased.
1543 * @param __last Iterator pointing to one past the last element to be
1544 * erased.
1545 * @return An iterator pointing to the element pointed to by @a __last
1546 * prior to erasing (or end()).
1547 *
1548 * This function will erase the elements in the range
1549 * [__first,__last) and shorten the %vector accordingly.
1550 *
1551 * Note This operation could be expensive and if it is
1552 * frequently used the user should consider using std::list.
1553 * The user is also cautioned that this function only erases
1554 * the elements, and that if the elements themselves are
1555 * pointers, the pointed-to memory is not touched in any way.
1556 * Managing the pointer is the user's responsibility.
1557 */
1558 _GLIBCXX20_CONSTEXPR
1559 iterator
1560 #if __cplusplus >= 201103L
1561 erase(const_iterator __first, const_iterator __last)
1562 {
1563 const auto __beg = begin();
1564 const auto __cbeg = cbegin();
1565 return _M_erase(__beg + (__first - __cbeg), __beg + (__last - __cbeg));
1566 }
1567 #else
1568 erase(iterator __first, iterator __last)
1569 { return _M_erase(__first, __last); }
1570 #endif
1571
1572 /**
1573 * @brief Swaps data with another %vector.
1574 * @param __x A %vector of the same element and allocator types.
1575 *
1576 * This exchanges the elements between two vectors in constant time.
1577 * (Three pointers, so it should be quite fast.)
1578 * Note that the global std::swap() function is specialized such that
1579 * std::swap(v1,v2) will feed to this function.
1580 *
1581 * Whether the allocators are swapped depends on the allocator traits.
1582 */
1583 _GLIBCXX20_CONSTEXPR
1584 void
1585 swap(vector& __x) _GLIBCXX_NOEXCEPT
1586 {
1587 #if __cplusplus >= 201103L
1588 __glibcxx_assert(_Alloc_traits::propagate_on_container_swap::value
1589 || _M_get_Tp_allocator() == __x._M_get_Tp_allocator());
1590 #endif
1591 this->_M_impl._M_swap_data(__x._M_impl);
1592 _Alloc_traits::_S_on_swap(_M_get_Tp_allocator(),
1593 __x._M_get_Tp_allocator());
1594 }
1595
1596 /**
1597 * Erases all the elements. Note that this function only erases the
1598 * elements, and that if the elements themselves are pointers, the
1599 * pointed-to memory is not touched in any way. Managing the pointer is
1600 * the user's responsibility.
1601 */
1602 _GLIBCXX20_CONSTEXPR
1603 void
1604 clear() _GLIBCXX_NOEXCEPT
1605 { _M_erase_at_end(this->_M_impl._M_start); }
1606
1607 protected:
1608 /**
1609 * Memory expansion handler. Uses the member allocation function to
1610 * obtain @a n bytes of memory, and then copies [first,last) into it.
1611 */
1612 template<typename _ForwardIterator>
1613 _GLIBCXX20_CONSTEXPR
1614 pointer
1615 _M_allocate_and_copy(size_type __n,
1616 _ForwardIterator __first, _ForwardIterator __last)
1617 {
1618 pointer __result = this->_M_allocate(__n);
1619 __try
1620 {
1621 std::__uninitialized_copy_a(__first, __last, __result,
1622 _M_get_Tp_allocator());
1623 return __result;
1624 }
1625 __catch(...)
1626 {
1627 _M_deallocate(__result, __n);
1628 __throw_exception_again;
1629 }
1630 }
1631
1632
1633 // Internal constructor functions follow.
1634
1635 // Called by the range constructor to implement [23.1.1]/9
1636
1637 #if __cplusplus < 201103L
1638 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1639 // 438. Ambiguity in the "do the right thing" clause
1640 template<typename _Integer>
1641 void
1642 _M_initialize_dispatch(_Integer __n, _Integer __value, __true_type)
1643 {
1644 this->_M_impl._M_start = _M_allocate(_S_check_init_len(
1645 static_cast<size_type>(__n), _M_get_Tp_allocator()));
1646 this->_M_impl._M_end_of_storage =
1647 this->_M_impl._M_start + static_cast<size_type>(__n);
1648 _M_fill_initialize(static_cast<size_type>(__n), __value);
1649 }
1650
1651 // Called by the range constructor to implement [23.1.1]/9
1652 template<typename _InputIterator>
1653 void
1654 _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
1655 __false_type)
1656 {
1657 _M_range_initialize(__first, __last,
1658 std::__iterator_category(__first));
1659 }
1660 #endif
1661
1662 // Called by the second initialize_dispatch above
1663 template<typename _InputIterator>
1664 _GLIBCXX20_CONSTEXPR
1665 void
1666 _M_range_initialize(_InputIterator __first, _InputIterator __last,
1667 std::input_iterator_tag)
1668 {
1669 __try {
1670 for (; __first != __last; ++__first)
1671 #if __cplusplus >= 201103L
1672 emplace_back(*__first);
1673 #else
1674 push_back(*__first);
1675 #endif
1676 } __catch(...) {
1677 clear();
1678 __throw_exception_again;
1679 }
1680 }
1681
1682 // Called by the second initialize_dispatch above
1683 template<typename _ForwardIterator>
1684 _GLIBCXX20_CONSTEXPR
1685 void
1686 _M_range_initialize(_ForwardIterator __first, _ForwardIterator __last,
1687 std::forward_iterator_tag)
1688 {
1689 const size_type __n = std::distance(__first, __last);
1690 this->_M_impl._M_start
1691 = this->_M_allocate(_S_check_init_len(__n, _M_get_Tp_allocator()));
1692 this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __n;
1693 this->_M_impl._M_finish =
1694 std::__uninitialized_copy_a(__first, __last,
1695 this->_M_impl._M_start,
1696 _M_get_Tp_allocator());
1697 }
1698
1699 // Called by the first initialize_dispatch above and by the
1700 // vector(n,value,a) constructor.
1701 _GLIBCXX20_CONSTEXPR
1702 void
1703 _M_fill_initialize(size_type __n, const value_type& __value)
1704 {
1705 this->_M_impl._M_finish =
1706 std::__uninitialized_fill_n_a(this->_M_impl._M_start, __n, __value,
1707 _M_get_Tp_allocator());
1708 }
1709
1710 #if __cplusplus >= 201103L
1711 // Called by the vector(n) constructor.
1712 _GLIBCXX20_CONSTEXPR
1713 void
1714 _M_default_initialize(size_type __n)
1715 {
1716 this->_M_impl._M_finish =
1717 std::__uninitialized_default_n_a(this->_M_impl._M_start, __n,
1718 _M_get_Tp_allocator());
1719 }
1720 #endif
1721
1722 // Internal assign functions follow. The *_aux functions do the actual
1723 // assignment work for the range versions.
1724
1725 // Called by the range assign to implement [23.1.1]/9
1726
1727 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1728 // 438. Ambiguity in the "do the right thing" clause
1729 template<typename _Integer>
1730 _GLIBCXX20_CONSTEXPR
1731 void
1732 _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
1733 { _M_fill_assign(__n, __val); }
1734
1735 // Called by the range assign to implement [23.1.1]/9
1736 template<typename _InputIterator>
1737 _GLIBCXX20_CONSTEXPR
1738 void
1739 _M_assign_dispatch(_InputIterator __first, _InputIterator __last,
1740 __false_type)
1741 { _M_assign_aux(__first, __last, std::__iterator_category(__first)); }
1742
1743 // Called by the second assign_dispatch above
1744 template<typename _InputIterator>
1745 _GLIBCXX20_CONSTEXPR
1746 void
1747 _M_assign_aux(_InputIterator __first, _InputIterator __last,
1748 std::input_iterator_tag);
1749
1750 // Called by the second assign_dispatch above
1751 template<typename _ForwardIterator>
1752 _GLIBCXX20_CONSTEXPR
1753 void
1754 _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
1755 std::forward_iterator_tag);
1756
1757 // Called by assign(n,t), and the range assign when it turns out
1758 // to be the same thing.
1759 _GLIBCXX20_CONSTEXPR
1760 void
1761 _M_fill_assign(size_type __n, const value_type& __val);
1762
1763 // Internal insert functions follow.
1764
1765 // Called by the range insert to implement [23.1.1]/9
1766
1767 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1768 // 438. Ambiguity in the "do the right thing" clause
1769 template<typename _Integer>
1770 _GLIBCXX20_CONSTEXPR
1771 void
1772 _M_insert_dispatch(iterator __pos, _Integer __n, _Integer __val,
1773 __true_type)
1774 { _M_fill_insert(__pos, __n, __val); }
1775
1776 // Called by the range insert to implement [23.1.1]/9
1777 template<typename _InputIterator>
1778 _GLIBCXX20_CONSTEXPR
1779 void
1780 _M_insert_dispatch(iterator __pos, _InputIterator __first,
1781 _InputIterator __last, __false_type)
1782 {
1783 _M_range_insert(__pos, __first, __last,
1784 std::__iterator_category(__first));
1785 }
1786
1787 // Called by the second insert_dispatch above
1788 template<typename _InputIterator>
1789 _GLIBCXX20_CONSTEXPR
1790 void
1791 _M_range_insert(iterator __pos, _InputIterator __first,
1792 _InputIterator __last, std::input_iterator_tag);
1793
1794 // Called by the second insert_dispatch above
1795 template<typename _ForwardIterator>
1796 _GLIBCXX20_CONSTEXPR
1797 void
1798 _M_range_insert(iterator __pos, _ForwardIterator __first,
1799 _ForwardIterator __last, std::forward_iterator_tag);
1800
1801 // Called by insert(p,n,x), and the range insert when it turns out to be
1802 // the same thing.
1803 _GLIBCXX20_CONSTEXPR
1804 void
1805 _M_fill_insert(iterator __pos, size_type __n, const value_type& __x);
1806
1807 #if __cplusplus >= 201103L
1808 // Called by resize(n).
1809 _GLIBCXX20_CONSTEXPR
1810 void
1811 _M_default_append(size_type __n);
1812
1813 _GLIBCXX20_CONSTEXPR
1814 bool
1815 _M_shrink_to_fit();
1816 #endif
1817
1818 #if __cplusplus < 201103L
1819 // Called by insert(p,x)
1820 void
1821 _M_insert_aux(iterator __position, const value_type& __x);
1822
1823 void
1824 _M_realloc_insert(iterator __position, const value_type& __x);
1825 #else
1826 // A value_type object constructed with _Alloc_traits::construct()
1827 // and destroyed with _Alloc_traits::destroy().
1828 struct _Temporary_value
1829 {
1830 template<typename... _Args>
1831 _GLIBCXX20_CONSTEXPR explicit
1832 _Temporary_value(vector* __vec, _Args&&... __args) : _M_this(__vec)
1833 {
1834 _Alloc_traits::construct(_M_this->_M_impl, _M_ptr(),
1835 std::forward<_Args>(__args)...);
1836 }
1837
1838 _GLIBCXX20_CONSTEXPR
1839 ~_Temporary_value()
1840 { _Alloc_traits::destroy(_M_this->_M_impl, _M_ptr()); }
1841
1842 _GLIBCXX20_CONSTEXPR value_type&
1843 _M_val() noexcept { return _M_storage._M_val; }
1844
1845 private:
1846 _GLIBCXX20_CONSTEXPR _Tp*
1847 _M_ptr() noexcept { return std::__addressof(_M_storage._M_val); }
1848
1849 union _Storage
1850 {
1851 constexpr _Storage() : _M_byte() { }
1852 _GLIBCXX20_CONSTEXPR ~_Storage() { }
1853 _Storage& operator=(const _Storage&) = delete;
1854 unsigned char _M_byte;
1855 _Tp _M_val;
1856 };
1857
1858 vector* _M_this;
1859 _Storage _M_storage;
1860 };
1861
1862 // Called by insert(p,x) and other functions when insertion needs to
1863 // reallocate or move existing elements. _Arg is either _Tp& or _Tp.
1864 template<typename _Arg>
1865 _GLIBCXX20_CONSTEXPR
1866 void
1867 _M_insert_aux(iterator __position, _Arg&& __arg);
1868
1869 template<typename... _Args>
1870 _GLIBCXX20_CONSTEXPR
1871 void
1872 _M_realloc_insert(iterator __position, _Args&&... __args);
1873
1874 // Either move-construct at the end, or forward to _M_insert_aux.
1875 _GLIBCXX20_CONSTEXPR
1876 iterator
1877 _M_insert_rval(const_iterator __position, value_type&& __v);
1878
1879 // Try to emplace at the end, otherwise forward to _M_insert_aux.
1880 template<typename... _Args>
1881 _GLIBCXX20_CONSTEXPR
1882 iterator
1883 _M_emplace_aux(const_iterator __position, _Args&&... __args);
1884
1885 // Emplacing an rvalue of the correct type can use _M_insert_rval.
1886 _GLIBCXX20_CONSTEXPR
1887 iterator
1888 _M_emplace_aux(const_iterator __position, value_type&& __v)
1889 { return _M_insert_rval(__position, std::move(__v)); }
1890 #endif
1891
1892 // Called by _M_fill_insert, _M_insert_aux etc.
1893 _GLIBCXX20_CONSTEXPR
1894 size_type
1895 _M_check_len(size_type __n, const char* __s) const
1896 {
1897 if (max_size() - size() < __n)
1898 __throw_length_error(__N(__s));
1899
1900 const size_type __len = size() + (std::max)(size(), __n);
1901 return (__len < size() || __len > max_size()) ? max_size() : __len;
1902 }
1903
1904 // Called by constructors to check initial size.
1905 static _GLIBCXX20_CONSTEXPR size_type
1906 _S_check_init_len(size_type __n, const allocator_type& __a)
1907 {
1908 if (__n > _S_max_size(_Tp_alloc_type(__a)))
1909 __throw_length_error(
1910 __N("cannot create std::vector larger than max_size()"));
1911 return __n;
1912 }
1913
1914 static _GLIBCXX20_CONSTEXPR size_type
1915 _S_max_size(const _Tp_alloc_type& __a) _GLIBCXX_NOEXCEPT
1916 {
1917 // std::distance(begin(), end()) cannot be greater than PTRDIFF_MAX,
1918 // and realistically we can't store more than PTRDIFF_MAX/sizeof(T)
1919 // (even if std::allocator_traits::max_size says we can).
1920 const size_t __diffmax
1921 = __gnu_cxx::__numeric_traits<ptrdiff_t>::__max / sizeof(_Tp);
1922 const size_t __allocmax = _Alloc_traits::max_size(__a);
1923 return (std::min)(__diffmax, __allocmax);
1924 }
1925
1926 // Internal erase functions follow.
1927
1928 // Called by erase(q1,q2), clear(), resize(), _M_fill_assign,
1929 // _M_assign_aux.
1930 _GLIBCXX20_CONSTEXPR
1931 void
1932 _M_erase_at_end(pointer __pos) _GLIBCXX_NOEXCEPT
1933 {
1934 if (size_type __n = this->_M_impl._M_finish - __pos)
1935 {
1936 std::_Destroy(__pos, this->_M_impl._M_finish,
1937 _M_get_Tp_allocator());
1938 this->_M_impl._M_finish = __pos;
1939 _GLIBCXX_ASAN_ANNOTATE_SHRINK(__n);
1940 }
1941 }
1942
1943 _GLIBCXX20_CONSTEXPR
1944 iterator
1945 _M_erase(iterator __position);
1946
1947 _GLIBCXX20_CONSTEXPR
1948 iterator
1949 _M_erase(iterator __first, iterator __last);
1950
1951 #if __cplusplus >= 201103L
1952 private:
1953 // Constant-time move assignment when source object's memory can be
1954 // moved, either because the source's allocator will move too
1955 // or because the allocators are equal.
1956 _GLIBCXX20_CONSTEXPR
1957 void
1958 _M_move_assign(vector&& __x, true_type) noexcept
1959 {
1960 vector __tmp(get_allocator());
1961 this->_M_impl._M_swap_data(__x._M_impl);
1962 __tmp._M_impl._M_swap_data(__x._M_impl);
1963 std::__alloc_on_move(_M_get_Tp_allocator(), __x._M_get_Tp_allocator());
1964 }
1965
1966 // Do move assignment when it might not be possible to move source
1967 // object's memory, resulting in a linear-time operation.
1968 _GLIBCXX20_CONSTEXPR
1969 void
1970 _M_move_assign(vector&& __x, false_type)
1971 {
1972 if (__x._M_get_Tp_allocator() == this->_M_get_Tp_allocator())
1973 _M_move_assign(std::move(__x), true_type());
1974 else
1975 {
1976 // The rvalue's allocator cannot be moved and is not equal,
1977 // so we need to individually move each element.
1978 this->_M_assign_aux(std::make_move_iterator(__x.begin()),
1979 std::make_move_iterator(__x.end()),
1980 std::random_access_iterator_tag());
1981 __x.clear();
1982 }
1983 }
1984 #endif
1985
1986 template<typename _Up>
1987 _GLIBCXX20_CONSTEXPR
1988 _Up*
1989 _M_data_ptr(_Up* __ptr) const _GLIBCXX_NOEXCEPT
1990 { return __ptr; }
1991
1992 #if __cplusplus >= 201103L
1993 template<typename _Ptr>
1994 _GLIBCXX20_CONSTEXPR
1995 typename std::pointer_traits<_Ptr>::element_type*
1996 _M_data_ptr(_Ptr __ptr) const
1997 { return empty() ? nullptr : std::__to_address(__ptr); }
1998 #else
1999 template<typename _Up>
2000 _Up*
2001 _M_data_ptr(_Up* __ptr) _GLIBCXX_NOEXCEPT
2002 { return __ptr; }
2003
2004 template<typename _Ptr>
2005 value_type*
2006 _M_data_ptr(_Ptr __ptr)
2007 { return empty() ? (value_type*)0 : __ptr.operator->(); }
2008
2009 template<typename _Ptr>
2010 const value_type*
2011 _M_data_ptr(_Ptr __ptr) const
2012 { return empty() ? (const value_type*)0 : __ptr.operator->(); }
2013 #endif
2014 };
2015
2016 #if __cpp_deduction_guides >= 201606
2017 template<typename _InputIterator, typename _ValT
2018 = typename iterator_traits<_InputIterator>::value_type,
2019 typename _Allocator = allocator<_ValT>,
2020 typename = _RequireInputIter<_InputIterator>,
2021 typename = _RequireAllocator<_Allocator>>
2022 vector(_InputIterator, _InputIterator, _Allocator = _Allocator())
2023 -> vector<_ValT, _Allocator>;
2024 #endif
2025
2026 /**
2027 * @brief Vector equality comparison.
2028 * @param __x A %vector.
2029 * @param __y A %vector of the same type as @a __x.
2030 * @return True iff the size and elements of the vectors are equal.
2031 *
2032 * This is an equivalence relation. It is linear in the size of the
2033 * vectors. Vectors are considered equivalent if their sizes are equal,
2034 * and if corresponding elements compare equal.
2035 */
2036 template<typename _Tp, typename _Alloc>
2037 _GLIBCXX20_CONSTEXPR
2038 inline bool
2039 operator==(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
2040 { return (__x.size() == __y.size()
2041 && std::equal(__x.begin(), __x.end(), __y.begin())); }
2042
2043 #if __cpp_lib_three_way_comparison
2044 /**
2045 * @brief Vector ordering relation.
2046 * @param __x A `vector`.
2047 * @param __y A `vector` of the same type as `__x`.
2048 * @return A value indicating whether `__x` is less than, equal to,
2049 * greater than, or incomparable with `__y`.
2050 *
2051 * See `std::lexicographical_compare_three_way()` for how the determination
2052 * is made. This operator is used to synthesize relational operators like
2053 * `<` and `>=` etc.
2054 */
2055 template<typename _Tp, typename _Alloc>
2056 _GLIBCXX20_CONSTEXPR
2057 inline __detail::__synth3way_t<_Tp>
2058 operator<=>(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
2059 {
2060 return std::lexicographical_compare_three_way(__x.begin(), __x.end(),
2061 __y.begin(), __y.end(),
2062 __detail::__synth3way);
2063 }
2064 #else
2065 /**
2066 * @brief Vector ordering relation.
2067 * @param __x A %vector.
2068 * @param __y A %vector of the same type as @a __x.
2069 * @return True iff @a __x is lexicographically less than @a __y.
2070 *
2071 * This is a total ordering relation. It is linear in the size of the
2072 * vectors. The elements must be comparable with @c <.
2073 *
2074 * See std::lexicographical_compare() for how the determination is made.
2075 */
2076 template<typename _Tp, typename _Alloc>
2077 inline bool
2078 operator<(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
2079 { return std::lexicographical_compare(__x.begin(), __x.end(),
2080 __y.begin(), __y.end()); }
2081
2082 /// Based on operator==
2083 template<typename _Tp, typename _Alloc>
2084 inline bool
2085 operator!=(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
2086 { return !(__x == __y); }
2087
2088 /// Based on operator<
2089 template<typename _Tp, typename _Alloc>
2090 inline bool
2091 operator>(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
2092 { return __y < __x; }
2093
2094 /// Based on operator<
2095 template<typename _Tp, typename _Alloc>
2096 inline bool
2097 operator<=(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
2098 { return !(__y < __x); }
2099
2100 /// Based on operator<
2101 template<typename _Tp, typename _Alloc>
2102 inline bool
2103 operator>=(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
2104 { return !(__x < __y); }
2105 #endif // three-way comparison
2106
2107 /// See std::vector::swap().
2108 template<typename _Tp, typename _Alloc>
2109 _GLIBCXX20_CONSTEXPR
2110 inline void
2111 swap(vector<_Tp, _Alloc>& __x, vector<_Tp, _Alloc>& __y)
2112 _GLIBCXX_NOEXCEPT_IF(noexcept(__x.swap(__y)))
2113 { __x.swap(__y); }
2114
2115 _GLIBCXX_END_NAMESPACE_CONTAINER
2116
2117 #if __cplusplus >= 201703L
2118 namespace __detail::__variant
2119 {
2120 template<typename> struct _Never_valueless_alt; // see <variant>
2121
2122 // Provide the strong exception-safety guarantee when emplacing a
2123 // vector into a variant, but only if move assignment cannot throw.
2124 template<typename _Tp, typename _Alloc>
2125 struct _Never_valueless_alt<_GLIBCXX_STD_C::vector<_Tp, _Alloc>>
2126 : std::is_nothrow_move_assignable<_GLIBCXX_STD_C::vector<_Tp, _Alloc>>
2127 { };
2128 } // namespace __detail::__variant
2129 #endif // C++17
2130
2131 _GLIBCXX_END_NAMESPACE_VERSION
2132 } // namespace std
2133
2134 #endif /* _STL_VECTOR_H */