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