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