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