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