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