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