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