1 // Vector implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
4 // 2011 Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
29 * Hewlett-Packard Company
31 * Permission to use, copy, modify, distribute and sell this software
32 * and its documentation for any purpose is hereby granted without fee,
33 * provided that the above copyright notice appear in all copies and
34 * that both that copyright notice and this permission notice appear
35 * in supporting documentation. Hewlett-Packard Company makes no
36 * representations about the suitability of this software for any
37 * purpose. It is provided "as is" without express or implied warranty.
41 * Silicon Graphics Computer Systems, Inc.
43 * Permission to use, copy, modify, distribute and sell this software
44 * and its documentation for any purpose is hereby granted without fee,
45 * provided that the above copyright notice appear in all copies and
46 * that both that copyright notice and this permission notice appear
47 * in supporting documentation. Silicon Graphics makes no
48 * representations about the suitability of this software for any
49 * purpose. It is provided "as is" without express or implied warranty.
52 /** @file bits/stl_vector.h
53 * This is an internal header file, included by other library headers.
54 * Do not attempt to use it directly. @headername{vector}
58 #define _STL_VECTOR_H 1
60 #include <bits/stl_iterator_base_funcs.h>
61 #include <bits/functexcept.h>
62 #include <bits/concept_check.h>
63 #ifdef __GXX_EXPERIMENTAL_CXX0X__
64 #include <initializer_list>
67 namespace std _GLIBCXX_VISIBILITY(default)
69 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
71 /// See bits/stl_deque.h's _Deque_base for an explanation.
72 template<typename _Tp, typename _Alloc>
75 typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template
76 rebind<_Tp>::other _Tp_alloc_type;
77 typedef typename __gnu_cxx::__alloc_traits<_Tp_alloc_type>::pointer
81 : public _Tp_alloc_type
85 pointer _M_end_of_storage;
88 : _Tp_alloc_type(), _M_start(0), _M_finish(0), _M_end_of_storage(0)
91 _Vector_impl(_Tp_alloc_type const& __a)
92 : _Tp_alloc_type(__a), _M_start(0), _M_finish(0), _M_end_of_storage(0)
95 #ifdef __GXX_EXPERIMENTAL_CXX0X__
96 _Vector_impl(_Tp_alloc_type&& __a)
97 : _Tp_alloc_type(std::move(__a)),
98 _M_start(0), _M_finish(0), _M_end_of_storage(0)
102 void _M_swap_data(_Vector_impl& __x)
104 std::swap(_M_start, __x._M_start);
105 std::swap(_M_finish, __x._M_finish);
106 std::swap(_M_end_of_storage, __x._M_end_of_storage);
111 typedef _Alloc allocator_type;
114 _M_get_Tp_allocator() _GLIBCXX_NOEXCEPT
115 { return *static_cast<_Tp_alloc_type*>(&this->_M_impl); }
117 const _Tp_alloc_type&
118 _M_get_Tp_allocator() const _GLIBCXX_NOEXCEPT
119 { return *static_cast<const _Tp_alloc_type*>(&this->_M_impl); }
122 get_allocator() const _GLIBCXX_NOEXCEPT
123 { return allocator_type(_M_get_Tp_allocator()); }
128 _Vector_base(const allocator_type& __a)
131 _Vector_base(size_t __n)
133 { _M_create_storage(__n); }
135 _Vector_base(size_t __n, const allocator_type& __a)
137 { _M_create_storage(__n); }
139 #ifdef __GXX_EXPERIMENTAL_CXX0X__
140 _Vector_base(_Tp_alloc_type&& __a)
141 : _M_impl(std::move(__a)) { }
143 _Vector_base(_Vector_base&& __x)
144 : _M_impl(std::move(__x._M_get_Tp_allocator()))
145 { this->_M_impl._M_swap_data(__x._M_impl); }
147 _Vector_base(_Vector_base&& __x, const allocator_type& __a)
150 if (__x.get_allocator() == __a)
151 this->_M_impl._M_swap_data(__x._M_impl);
154 size_t __n = __x._M_impl._M_finish - __x._M_impl._M_start;
155 _M_create_storage(__n);
161 { _M_deallocate(this->_M_impl._M_start, this->_M_impl._M_end_of_storage
162 - this->_M_impl._M_start); }
165 _Vector_impl _M_impl;
168 _M_allocate(size_t __n)
169 { return __n != 0 ? _M_impl.allocate(__n) : 0; }
172 _M_deallocate(pointer __p, size_t __n)
175 _M_impl.deallocate(__p, __n);
180 _M_create_storage(size_t __n)
182 this->_M_impl._M_start = this->_M_allocate(__n);
183 this->_M_impl._M_finish = this->_M_impl._M_start;
184 this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __n;
190 * @brief A standard container which offers fixed time access to
191 * individual elements in any order.
195 * Meets the requirements of a <a href="tables.html#65">container</a>, a
196 * <a href="tables.html#66">reversible container</a>, and a
197 * <a href="tables.html#67">sequence</a>, including the
198 * <a href="tables.html#68">optional sequence requirements</a> with the
199 * %exception of @c push_front and @c pop_front.
201 * In some terminology a %vector can be described as a dynamic
202 * C-style array, it offers fast and efficient access to individual
203 * elements in any order and saves the user from worrying about
204 * memory and size allocation. Subscripting ( @c [] ) access is
205 * also provided as with C-style arrays.
207 template<typename _Tp, typename _Alloc = std::allocator<_Tp> >
208 class vector : protected _Vector_base<_Tp, _Alloc>
210 // Concept requirements.
211 typedef typename _Alloc::value_type _Alloc_value_type;
212 __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
213 __glibcxx_class_requires2(_Tp, _Alloc_value_type, _SameTypeConcept)
215 typedef _Vector_base<_Tp, _Alloc> _Base;
216 typedef typename _Base::_Tp_alloc_type _Tp_alloc_type;
219 typedef _Tp value_type;
220 typedef typename _Base::pointer pointer;
221 typedef __gnu_cxx::__alloc_traits<_Tp_alloc_type> _Alloc_traits;
222 typedef typename _Alloc_traits::const_pointer const_pointer;
223 typedef typename _Alloc_traits::reference reference;
224 typedef typename _Alloc_traits::const_reference const_reference;
225 typedef __gnu_cxx::__normal_iterator<pointer, vector> iterator;
226 typedef __gnu_cxx::__normal_iterator<const_pointer, vector>
228 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
229 typedef std::reverse_iterator<iterator> reverse_iterator;
230 typedef size_t size_type;
231 typedef ptrdiff_t difference_type;
232 typedef _Alloc allocator_type;
235 using _Base::_M_allocate;
236 using _Base::_M_deallocate;
237 using _Base::_M_impl;
238 using _Base::_M_get_Tp_allocator;
241 // [23.2.4.1] construct/copy/destroy
242 // (assign() and get_allocator() are also listed in this section)
244 * @brief Default constructor creates no elements.
250 * @brief Creates a %vector with no elements.
251 * @param __a An allocator object.
254 vector(const allocator_type& __a)
257 #ifdef __GXX_EXPERIMENTAL_CXX0X__
259 * @brief Creates a %vector with default constructed elements.
260 * @param __n The number of elements to initially create.
262 * This constructor fills the %vector with @a __n default
263 * constructed elements.
266 vector(size_type __n)
268 { _M_default_initialize(__n); }
271 * @brief Creates a %vector with copies of an exemplar element.
272 * @param __n The number of elements to initially create.
273 * @param __value An element to copy.
274 * @param __a An allocator.
276 * This constructor fills the %vector with @a __n copies of @a __value.
278 vector(size_type __n, const value_type& __value,
279 const allocator_type& __a = allocator_type())
281 { _M_fill_initialize(__n, __value); }
284 * @brief Creates a %vector with copies of an exemplar element.
285 * @param __n The number of elements to initially create.
286 * @param __value An element to copy.
287 * @param __a An allocator.
289 * This constructor fills the %vector with @a __n copies of @a __value.
292 vector(size_type __n, const value_type& __value = value_type(),
293 const allocator_type& __a = allocator_type())
295 { _M_fill_initialize(__n, __value); }
299 * @brief %Vector copy constructor.
300 * @param __x A %vector of identical element and allocator types.
302 * The newly-created %vector uses a copy of the allocation
303 * object used by @a __x. All the elements of @a __x are copied,
304 * but any extra memory in
305 * @a __x (for fast expansion) will not be copied.
307 vector(const vector& __x)
309 _Alloc_traits::_S_select_on_copy(__x._M_get_Tp_allocator()))
310 { this->_M_impl._M_finish =
311 std::__uninitialized_copy_a(__x.begin(), __x.end(),
312 this->_M_impl._M_start,
313 _M_get_Tp_allocator());
316 #ifdef __GXX_EXPERIMENTAL_CXX0X__
318 * @brief %Vector move constructor.
319 * @param __x A %vector of identical element and allocator types.
321 * The newly-created %vector contains the exact contents of @a __x.
322 * The contents of @a __x are a valid, but unspecified %vector.
324 vector(vector&& __x) noexcept
325 : _Base(std::move(__x)) { }
327 /// Copy constructor with alternative allocator
328 vector(const vector& __x, const allocator_type& __a)
329 : _Base(__x.size(), __a)
330 { this->_M_impl._M_finish =
331 std::__uninitialized_copy_a(__x.begin(), __x.end(),
332 this->_M_impl._M_start,
333 _M_get_Tp_allocator());
336 /// Move constructor with alternative allocator
337 vector(vector&& __rv, const allocator_type& __m)
338 : _Base(std::move(__rv), __m)
340 if (__rv.get_allocator() != __m)
342 this->_M_impl._M_finish =
343 std::__uninitialized_move_a(__rv.begin(), __rv.end(),
344 this->_M_impl._M_start,
345 _M_get_Tp_allocator());
351 * @brief Builds a %vector from an initializer list.
352 * @param __l An initializer_list.
353 * @param __a An allocator.
355 * Create a %vector consisting of copies of the elements in the
356 * initializer_list @a __l.
358 * This will call the element type's copy constructor N times
359 * (where N is @a __l.size()) and do no memory reallocation.
361 vector(initializer_list<value_type> __l,
362 const allocator_type& __a = allocator_type())
365 _M_range_initialize(__l.begin(), __l.end(),
366 random_access_iterator_tag());
371 * @brief Builds a %vector from a range.
372 * @param __first An input iterator.
373 * @param __last An input iterator.
374 * @param __a An allocator.
376 * Create a %vector consisting of copies of the elements from
379 * If the iterators are forward, bidirectional, or
380 * random-access, then this will call the elements' copy
381 * constructor N times (where N is distance(first,last)) and do
382 * no memory reallocation. But if only input iterators are
383 * used, then this will do at most 2N calls to the copy
384 * constructor, and logN memory reallocations.
386 template<typename _InputIterator>
387 vector(_InputIterator __first, _InputIterator __last,
388 const allocator_type& __a = allocator_type())
391 // Check whether it's an integral type. If so, it's not an iterator.
392 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
393 _M_initialize_dispatch(__first, __last, _Integral());
397 * The dtor only erases the elements, and note that if the
398 * elements themselves are pointers, the pointed-to memory is
399 * not touched in any way. Managing the pointer is the user's
402 ~vector() _GLIBCXX_NOEXCEPT
403 { std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
404 _M_get_Tp_allocator()); }
407 * @brief %Vector assignment operator.
408 * @param __x A %vector of identical element and allocator types.
410 * All the elements of @a __x are copied, but any extra memory in
411 * @a __x (for fast expansion) will not be copied. Unlike the
412 * copy constructor, the allocator object is not copied.
415 operator=(const vector& __x);
417 #ifdef __GXX_EXPERIMENTAL_CXX0X__
419 * @brief %Vector move assignment operator.
420 * @param __x A %vector of identical element and allocator types.
422 * The contents of @a __x are moved into this %vector (without copying).
423 * @a __x is a valid, but unspecified %vector.
426 operator=(vector&& __x) noexcept(_Alloc_traits::_S_nothrow_move())
428 if (_Alloc_traits::_S_propagate_on_move_assign())
430 // We're moving the rvalue's allocator so can move the data too.
431 const vector __tmp(std::move(*this)); // discard existing data
432 this->_M_impl._M_swap_data(__x._M_impl);
433 std::__alloc_on_move(_M_get_Tp_allocator(),
434 __x._M_get_Tp_allocator());
436 else if (_Alloc_traits::_S_always_equal()
437 || __x._M_get_Tp_allocator() == this->_M_get_Tp_allocator())
439 // The rvalue's allocator can free our storage and vice versa,
440 // so can swap the data storage after destroying our contents.
442 this->_M_impl._M_swap_data(__x._M_impl);
446 // The rvalue's allocator cannot be moved, or is not equal,
447 // so we need to individually move each element.
448 this->assign(std::__make_move_if_noexcept_iterator(__x.begin()),
449 std::__make_move_if_noexcept_iterator(__x.end()));
456 * @brief %Vector list assignment operator.
457 * @param __l An initializer_list.
459 * This function fills a %vector with copies of the elements in the
460 * initializer list @a __l.
462 * Note that the assignment completely changes the %vector and
463 * that the resulting %vector's size is the same as the number
464 * of elements assigned. Old data may be lost.
467 operator=(initializer_list<value_type> __l)
469 this->assign(__l.begin(), __l.end());
475 * @brief Assigns a given value to a %vector.
476 * @param __n Number of elements to be assigned.
477 * @param __val Value to be assigned.
479 * This function fills a %vector with @a __n copies of the given
480 * value. Note that the assignment completely changes the
481 * %vector and that the resulting %vector's size is the same as
482 * the number of elements assigned. Old data may be lost.
485 assign(size_type __n, const value_type& __val)
486 { _M_fill_assign(__n, __val); }
489 * @brief Assigns a range to a %vector.
490 * @param __first An input iterator.
491 * @param __last An input iterator.
493 * This function fills a %vector with copies of the elements in the
494 * range [__first,__last).
496 * Note that the assignment completely changes the %vector and
497 * that the resulting %vector's size is the same as the number
498 * of elements assigned. Old data may be lost.
500 template<typename _InputIterator>
502 assign(_InputIterator __first, _InputIterator __last)
504 // Check whether it's an integral type. If so, it's not an iterator.
505 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
506 _M_assign_dispatch(__first, __last, _Integral());
509 #ifdef __GXX_EXPERIMENTAL_CXX0X__
511 * @brief Assigns an initializer list to a %vector.
512 * @param __l An initializer_list.
514 * This function fills a %vector with copies of the elements in the
515 * initializer list @a __l.
517 * Note that the assignment completely changes the %vector and
518 * that the resulting %vector's size is the same as the number
519 * of elements assigned. Old data may be lost.
522 assign(initializer_list<value_type> __l)
523 { this->assign(__l.begin(), __l.end()); }
526 /// Get a copy of the memory allocation object.
527 using _Base::get_allocator;
531 * Returns a read/write iterator that points to the first
532 * element in the %vector. Iteration is done in ordinary
536 begin() _GLIBCXX_NOEXCEPT
537 { return iterator(this->_M_impl._M_start); }
540 * Returns a read-only (constant) iterator that points to the
541 * first element in the %vector. Iteration is done in ordinary
545 begin() const _GLIBCXX_NOEXCEPT
546 { return const_iterator(this->_M_impl._M_start); }
549 * Returns a read/write iterator that points one past the last
550 * element in the %vector. Iteration is done in ordinary
554 end() _GLIBCXX_NOEXCEPT
555 { return iterator(this->_M_impl._M_finish); }
558 * Returns a read-only (constant) iterator that points one past
559 * the last element in the %vector. Iteration is done in
560 * ordinary element order.
563 end() const _GLIBCXX_NOEXCEPT
564 { return const_iterator(this->_M_impl._M_finish); }
567 * Returns a read/write reverse iterator that points to the
568 * last element in the %vector. Iteration is done in reverse
572 rbegin() _GLIBCXX_NOEXCEPT
573 { return reverse_iterator(end()); }
576 * Returns a read-only (constant) reverse iterator that points
577 * to the last element in the %vector. Iteration is done in
578 * reverse element order.
580 const_reverse_iterator
581 rbegin() const _GLIBCXX_NOEXCEPT
582 { return const_reverse_iterator(end()); }
585 * Returns a read/write reverse iterator that points to one
586 * before the first element in the %vector. Iteration is done
587 * in reverse element order.
590 rend() _GLIBCXX_NOEXCEPT
591 { return reverse_iterator(begin()); }
594 * Returns a read-only (constant) reverse iterator that points
595 * to one before the first element in the %vector. Iteration
596 * is done in reverse element order.
598 const_reverse_iterator
599 rend() const _GLIBCXX_NOEXCEPT
600 { return const_reverse_iterator(begin()); }
602 #ifdef __GXX_EXPERIMENTAL_CXX0X__
604 * Returns a read-only (constant) iterator that points to the
605 * first element in the %vector. Iteration is done in ordinary
609 cbegin() const noexcept
610 { return const_iterator(this->_M_impl._M_start); }
613 * Returns a read-only (constant) iterator that points one past
614 * the last element in the %vector. Iteration is done in
615 * ordinary element order.
618 cend() const noexcept
619 { return const_iterator(this->_M_impl._M_finish); }
622 * Returns a read-only (constant) reverse iterator that points
623 * to the last element in the %vector. Iteration is done in
624 * reverse element order.
626 const_reverse_iterator
627 crbegin() const noexcept
628 { return const_reverse_iterator(end()); }
631 * Returns a read-only (constant) reverse iterator that points
632 * to one before the first element in the %vector. Iteration
633 * is done in reverse element order.
635 const_reverse_iterator
636 crend() const noexcept
637 { return const_reverse_iterator(begin()); }
640 // [23.2.4.2] capacity
641 /** Returns the number of elements in the %vector. */
643 size() const _GLIBCXX_NOEXCEPT
644 { return size_type(this->_M_impl._M_finish - this->_M_impl._M_start); }
646 /** Returns the size() of the largest possible %vector. */
648 max_size() const _GLIBCXX_NOEXCEPT
649 { return _Alloc_traits::max_size(_M_get_Tp_allocator()); }
651 #ifdef __GXX_EXPERIMENTAL_CXX0X__
653 * @brief Resizes the %vector to the specified number of elements.
654 * @param __new_size Number of elements the %vector should contain.
656 * This function will %resize the %vector to the specified
657 * number of elements. If the number is smaller than the
658 * %vector's current size the %vector is truncated, otherwise
659 * default constructed elements are appended.
662 resize(size_type __new_size)
664 if (__new_size > size())
665 _M_default_append(__new_size - size());
666 else if (__new_size < size())
667 _M_erase_at_end(this->_M_impl._M_start + __new_size);
671 * @brief Resizes the %vector to the specified number of elements.
672 * @param __new_size Number of elements the %vector should contain.
673 * @param __x Data with which new elements should be populated.
675 * This function will %resize the %vector to the specified
676 * number of elements. If the number is smaller than the
677 * %vector's current size the %vector is truncated, otherwise
678 * the %vector is extended and new elements are populated with
682 resize(size_type __new_size, const value_type& __x)
684 if (__new_size > size())
685 insert(end(), __new_size - size(), __x);
686 else if (__new_size < size())
687 _M_erase_at_end(this->_M_impl._M_start + __new_size);
691 * @brief Resizes the %vector to the specified number of elements.
692 * @param __new_size Number of elements the %vector should contain.
693 * @param __x Data with which new elements should be populated.
695 * This function will %resize the %vector to the specified
696 * number of elements. If the number is smaller than the
697 * %vector's current size the %vector is truncated, otherwise
698 * the %vector is extended and new elements are populated with
702 resize(size_type __new_size, value_type __x = value_type())
704 if (__new_size > size())
705 insert(end(), __new_size - size(), __x);
706 else if (__new_size < size())
707 _M_erase_at_end(this->_M_impl._M_start + __new_size);
711 #ifdef __GXX_EXPERIMENTAL_CXX0X__
712 /** A non-binding request to reduce capacity() to size(). */
715 { _M_shrink_to_fit(); }
719 * Returns the total number of elements that the %vector can
720 * hold before needing to allocate more memory.
723 capacity() const _GLIBCXX_NOEXCEPT
724 { return size_type(this->_M_impl._M_end_of_storage
725 - this->_M_impl._M_start); }
728 * Returns true if the %vector is empty. (Thus begin() would
732 empty() const _GLIBCXX_NOEXCEPT
733 { return begin() == end(); }
736 * @brief Attempt to preallocate enough memory for specified number of
738 * @param __n Number of elements required.
739 * @throw std::length_error If @a n exceeds @c max_size().
741 * This function attempts to reserve enough memory for the
742 * %vector to hold the specified number of elements. If the
743 * number requested is more than max_size(), length_error is
746 * The advantage of this function is that if optimal code is a
747 * necessity and the user can determine the number of elements
748 * that will be required, the user can reserve the memory in
749 * %advance, and thus prevent a possible reallocation of memory
750 * and copying of %vector data.
753 reserve(size_type __n);
757 * @brief Subscript access to the data contained in the %vector.
758 * @param __n The index of the element for which data should be
760 * @return Read/write reference to data.
762 * This operator allows for easy, array-style, data access.
763 * Note that data access with this operator is unchecked and
764 * out_of_range lookups are not defined. (For checked lookups
768 operator[](size_type __n)
769 { return *(this->_M_impl._M_start + __n); }
772 * @brief Subscript access to the data contained in the %vector.
773 * @param __n The index of the element for which data should be
775 * @return Read-only (constant) reference to data.
777 * This operator allows for easy, array-style, data access.
778 * Note that data access with this operator is unchecked and
779 * out_of_range lookups are not defined. (For checked lookups
783 operator[](size_type __n) const
784 { return *(this->_M_impl._M_start + __n); }
787 /// Safety check used only from at().
789 _M_range_check(size_type __n) const
791 if (__n >= this->size())
792 __throw_out_of_range(__N("vector::_M_range_check"));
797 * @brief Provides access to the data contained in the %vector.
798 * @param __n The index of the element for which data should be
800 * @return Read/write reference to data.
801 * @throw std::out_of_range If @a __n is an invalid index.
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.
815 * @brief Provides access to the data contained in the %vector.
816 * @param __n The index of the element for which data should be
818 * @return Read-only (constant) reference to data.
819 * @throw std::out_of_range If @a __n is an invalid index.
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.
826 at(size_type __n) const
833 * Returns a read/write reference to the data at the first
834 * element of the %vector.
841 * Returns a read-only (constant) reference to the data at the first
842 * element of the %vector.
849 * Returns a read/write reference to the data at the last
850 * element of the %vector.
854 { return *(end() - 1); }
857 * Returns a read-only (constant) reference to the data at the
858 * last element of the %vector.
862 { return *(end() - 1); }
864 // _GLIBCXX_RESOLVE_LIB_DEFECTS
865 // DR 464. Suggestion for new member functions in standard containers.
868 * Returns a pointer such that [data(), data() + size()) is a valid
869 * range. For a non-empty %vector, data() == &front().
871 #ifdef __GXX_EXPERIMENTAL_CXX0X__
876 data() _GLIBCXX_NOEXCEPT
877 { return std::__addressof(front()); }
879 #ifdef __GXX_EXPERIMENTAL_CXX0X__
884 data() const _GLIBCXX_NOEXCEPT
885 { return std::__addressof(front()); }
887 // [23.2.4.3] modifiers
889 * @brief Add data to the end of the %vector.
890 * @param __x Data to be added.
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
899 push_back(const value_type& __x)
901 if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage)
903 _Alloc_traits::construct(this->_M_impl, this->_M_impl._M_finish,
905 ++this->_M_impl._M_finish;
908 #ifdef __GXX_EXPERIMENTAL_CXX0X__
909 _M_emplace_back_aux(__x);
911 _M_insert_aux(end(), __x);
915 #ifdef __GXX_EXPERIMENTAL_CXX0X__
917 push_back(value_type&& __x)
918 { emplace_back(std::move(__x)); }
920 template<typename... _Args>
922 emplace_back(_Args&&... __args);
926 * @brief Removes last element.
928 * This is a typical stack operation. It shrinks the %vector by one.
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
937 --this->_M_impl._M_finish;
938 _Alloc_traits::destroy(this->_M_impl, this->_M_impl._M_finish);
941 #ifdef __GXX_EXPERIMENTAL_CXX0X__
943 * @brief Inserts an object in %vector before specified iterator.
944 * @param __position An iterator into the %vector.
945 * @param __args Arguments.
946 * @return An iterator that points to the inserted data.
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
954 template<typename... _Args>
956 emplace(iterator __position, _Args&&... __args);
960 * @brief Inserts given value into %vector before specified iterator.
961 * @param __position An iterator into the %vector.
962 * @param __x Data to be inserted.
963 * @return An iterator that points to the inserted data.
965 * This function will insert a copy of the given value before
966 * the specified location. Note that this kind of operation
967 * could be expensive for a %vector and if it is frequently
968 * used the user should consider using std::list.
971 insert(iterator __position, const value_type& __x);
973 #ifdef __GXX_EXPERIMENTAL_CXX0X__
975 * @brief Inserts given rvalue into %vector before specified iterator.
976 * @param __position An iterator into the %vector.
977 * @param __x Data to be inserted.
978 * @return An iterator that points to the inserted data.
980 * This function will insert a copy of the given rvalue before
981 * the specified location. Note that this kind of operation
982 * could be expensive for a %vector and if it is frequently
983 * used the user should consider using std::list.
986 insert(iterator __position, value_type&& __x)
987 { return emplace(__position, std::move(__x)); }
990 * @brief Inserts an initializer_list into the %vector.
991 * @param __position An iterator into the %vector.
992 * @param __l An initializer_list.
994 * This function will insert copies of the data in the
995 * initializer_list @a l into the %vector before the location
996 * specified by @a position.
998 * Note that this kind of operation could be expensive for a
999 * %vector and if it is frequently used the user should
1000 * consider using std::list.
1003 insert(iterator __position, initializer_list<value_type> __l)
1004 { this->insert(__position, __l.begin(), __l.end()); }
1008 * @brief Inserts a number of copies of given data into the %vector.
1009 * @param __position An iterator into the %vector.
1010 * @param __n Number of elements to be inserted.
1011 * @param __x Data to be inserted.
1013 * This function will insert a specified number of copies of
1014 * the given data before the location specified by @a position.
1016 * Note that this kind of operation could be expensive for a
1017 * %vector and if it is frequently used the user should
1018 * consider using std::list.
1021 insert(iterator __position, size_type __n, const value_type& __x)
1022 { _M_fill_insert(__position, __n, __x); }
1025 * @brief Inserts a range into the %vector.
1026 * @param __position An iterator into the %vector.
1027 * @param __first An input iterator.
1028 * @param __last An input iterator.
1030 * This function will insert copies of the data in the range
1031 * [__first,__last) into the %vector before the location specified
1034 * Note that this kind of operation could be expensive for a
1035 * %vector and if it is frequently used the user should
1036 * consider using std::list.
1038 template<typename _InputIterator>
1040 insert(iterator __position, _InputIterator __first,
1041 _InputIterator __last)
1043 // Check whether it's an integral type. If so, it's not an iterator.
1044 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
1045 _M_insert_dispatch(__position, __first, __last, _Integral());
1049 * @brief Remove element at given position.
1050 * @param __position Iterator pointing to element to be erased.
1051 * @return An iterator pointing to the next element (or end()).
1053 * This function will erase the element at the given position and thus
1054 * shorten the %vector by one.
1056 * Note This operation could be expensive and if it is
1057 * frequently used the user should consider using std::list.
1058 * The user is also cautioned that this function only erases
1059 * the element, and that if the element is itself a pointer,
1060 * the pointed-to memory is not touched in any way. Managing
1061 * the pointer is the user's responsibility.
1064 erase(iterator __position);
1067 * @brief Remove a range of elements.
1068 * @param __first Iterator pointing to the first element to be erased.
1069 * @param __last Iterator pointing to one past the last element to be
1071 * @return An iterator pointing to the element pointed to by @a __last
1072 * prior to erasing (or end()).
1074 * This function will erase the elements in the range
1075 * [__first,__last) and shorten the %vector accordingly.
1077 * Note This operation could be expensive and if it is
1078 * frequently used the user should consider using std::list.
1079 * The user is also cautioned that this function only erases
1080 * the elements, and that if the elements themselves are
1081 * pointers, the pointed-to memory is not touched in any way.
1082 * Managing the pointer is the user's responsibility.
1085 erase(iterator __first, iterator __last);
1088 * @brief Swaps data with another %vector.
1089 * @param __x A %vector of the same element and allocator types.
1091 * This exchanges the elements between two vectors in constant time.
1092 * (Three pointers, so it should be quite fast.)
1093 * Note that the global std::swap() function is specialized such that
1094 * std::swap(v1,v2) will feed to this function.
1098 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1099 noexcept(_Alloc_traits::_S_nothrow_swap())
1102 this->_M_impl._M_swap_data(__x._M_impl);
1103 _Alloc_traits::_S_on_swap(_M_get_Tp_allocator(),
1104 __x._M_get_Tp_allocator());
1108 * Erases all the elements. Note that this function only erases the
1109 * elements, and that if the elements themselves are pointers, the
1110 * pointed-to memory is not touched in any way. Managing the pointer is
1111 * the user's responsibility.
1114 clear() _GLIBCXX_NOEXCEPT
1115 { _M_erase_at_end(this->_M_impl._M_start); }
1119 * Memory expansion handler. Uses the member allocation function to
1120 * obtain @a n bytes of memory, and then copies [first,last) into it.
1122 template<typename _ForwardIterator>
1124 _M_allocate_and_copy(size_type __n,
1125 _ForwardIterator __first, _ForwardIterator __last)
1127 pointer __result = this->_M_allocate(__n);
1130 std::__uninitialized_copy_a(__first, __last, __result,
1131 _M_get_Tp_allocator());
1136 _M_deallocate(__result, __n);
1137 __throw_exception_again;
1142 // Internal constructor functions follow.
1144 // Called by the range constructor to implement [23.1.1]/9
1146 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1147 // 438. Ambiguity in the "do the right thing" clause
1148 template<typename _Integer>
1150 _M_initialize_dispatch(_Integer __n, _Integer __value, __true_type)
1152 this->_M_impl._M_start = _M_allocate(static_cast<size_type>(__n));
1153 this->_M_impl._M_end_of_storage =
1154 this->_M_impl._M_start + static_cast<size_type>(__n);
1155 _M_fill_initialize(static_cast<size_type>(__n), __value);
1158 // Called by the range constructor to implement [23.1.1]/9
1159 template<typename _InputIterator>
1161 _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
1164 typedef typename std::iterator_traits<_InputIterator>::
1165 iterator_category _IterCategory;
1166 _M_range_initialize(__first, __last, _IterCategory());
1169 // Called by the second initialize_dispatch above
1170 template<typename _InputIterator>
1172 _M_range_initialize(_InputIterator __first,
1173 _InputIterator __last, std::input_iterator_tag)
1175 for (; __first != __last; ++__first)
1176 push_back(*__first);
1179 // Called by the second initialize_dispatch above
1180 template<typename _ForwardIterator>
1182 _M_range_initialize(_ForwardIterator __first,
1183 _ForwardIterator __last, std::forward_iterator_tag)
1185 const size_type __n = std::distance(__first, __last);
1186 this->_M_impl._M_start = this->_M_allocate(__n);
1187 this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __n;
1188 this->_M_impl._M_finish =
1189 std::__uninitialized_copy_a(__first, __last,
1190 this->_M_impl._M_start,
1191 _M_get_Tp_allocator());
1194 // Called by the first initialize_dispatch above and by the
1195 // vector(n,value,a) constructor.
1197 _M_fill_initialize(size_type __n, const value_type& __value)
1199 std::__uninitialized_fill_n_a(this->_M_impl._M_start, __n, __value,
1200 _M_get_Tp_allocator());
1201 this->_M_impl._M_finish = this->_M_impl._M_end_of_storage;
1204 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1205 // Called by the vector(n) constructor.
1207 _M_default_initialize(size_type __n)
1209 std::__uninitialized_default_n_a(this->_M_impl._M_start, __n,
1210 _M_get_Tp_allocator());
1211 this->_M_impl._M_finish = this->_M_impl._M_end_of_storage;
1215 // Internal assign functions follow. The *_aux functions do the actual
1216 // assignment work for the range versions.
1218 // Called by the range assign to implement [23.1.1]/9
1220 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1221 // 438. Ambiguity in the "do the right thing" clause
1222 template<typename _Integer>
1224 _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
1225 { _M_fill_assign(__n, __val); }
1227 // Called by the range assign to implement [23.1.1]/9
1228 template<typename _InputIterator>
1230 _M_assign_dispatch(_InputIterator __first, _InputIterator __last,
1233 typedef typename std::iterator_traits<_InputIterator>::
1234 iterator_category _IterCategory;
1235 _M_assign_aux(__first, __last, _IterCategory());
1238 // Called by the second assign_dispatch above
1239 template<typename _InputIterator>
1241 _M_assign_aux(_InputIterator __first, _InputIterator __last,
1242 std::input_iterator_tag);
1244 // Called by the second assign_dispatch above
1245 template<typename _ForwardIterator>
1247 _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
1248 std::forward_iterator_tag);
1250 // Called by assign(n,t), and the range assign when it turns out
1251 // to be the same thing.
1253 _M_fill_assign(size_type __n, const value_type& __val);
1256 // Internal insert functions follow.
1258 // Called by the range insert to implement [23.1.1]/9
1260 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1261 // 438. Ambiguity in the "do the right thing" clause
1262 template<typename _Integer>
1264 _M_insert_dispatch(iterator __pos, _Integer __n, _Integer __val,
1266 { _M_fill_insert(__pos, __n, __val); }
1268 // Called by the range insert to implement [23.1.1]/9
1269 template<typename _InputIterator>
1271 _M_insert_dispatch(iterator __pos, _InputIterator __first,
1272 _InputIterator __last, __false_type)
1274 typedef typename std::iterator_traits<_InputIterator>::
1275 iterator_category _IterCategory;
1276 _M_range_insert(__pos, __first, __last, _IterCategory());
1279 // Called by the second insert_dispatch above
1280 template<typename _InputIterator>
1282 _M_range_insert(iterator __pos, _InputIterator __first,
1283 _InputIterator __last, std::input_iterator_tag);
1285 // Called by the second insert_dispatch above
1286 template<typename _ForwardIterator>
1288 _M_range_insert(iterator __pos, _ForwardIterator __first,
1289 _ForwardIterator __last, std::forward_iterator_tag);
1291 // Called by insert(p,n,x), and the range insert when it turns out to be
1294 _M_fill_insert(iterator __pos, size_type __n, const value_type& __x);
1296 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1297 // Called by resize(n).
1299 _M_default_append(size_type __n);
1305 // Called by insert(p,x)
1306 #ifndef __GXX_EXPERIMENTAL_CXX0X__
1308 _M_insert_aux(iterator __position, const value_type& __x);
1310 template<typename... _Args>
1312 _M_insert_aux(iterator __position, _Args&&... __args);
1314 template<typename... _Args>
1316 _M_emplace_back_aux(_Args&&... __args);
1319 // Called by the latter.
1321 _M_check_len(size_type __n, const char* __s) const
1323 if (max_size() - size() < __n)
1324 __throw_length_error(__N(__s));
1326 const size_type __len = size() + std::max(size(), __n);
1327 return (__len < size() || __len > max_size()) ? max_size() : __len;
1330 // Internal erase functions follow.
1332 // Called by erase(q1,q2), clear(), resize(), _M_fill_assign,
1335 _M_erase_at_end(pointer __pos)
1337 std::_Destroy(__pos, this->_M_impl._M_finish, _M_get_Tp_allocator());
1338 this->_M_impl._M_finish = __pos;
1344 * @brief Vector equality comparison.
1345 * @param __x A %vector.
1346 * @param __y A %vector of the same type as @a __x.
1347 * @return True iff the size and elements of the vectors are equal.
1349 * This is an equivalence relation. It is linear in the size of the
1350 * vectors. Vectors are considered equivalent if their sizes are equal,
1351 * and if corresponding elements compare equal.
1353 template<typename _Tp, typename _Alloc>
1355 operator==(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
1356 { return (__x.size() == __y.size()
1357 && std::equal(__x.begin(), __x.end(), __y.begin())); }
1360 * @brief Vector ordering relation.
1361 * @param __x A %vector.
1362 * @param __y A %vector of the same type as @a __x.
1363 * @return True iff @a __x is lexicographically less than @a __y.
1365 * This is a total ordering relation. It is linear in the size of the
1366 * vectors. The elements must be comparable with @c <.
1368 * See std::lexicographical_compare() for how the determination is made.
1370 template<typename _Tp, typename _Alloc>
1372 operator<(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
1373 { return std::lexicographical_compare(__x.begin(), __x.end(),
1374 __y.begin(), __y.end()); }
1376 /// Based on operator==
1377 template<typename _Tp, typename _Alloc>
1379 operator!=(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
1380 { return !(__x == __y); }
1382 /// Based on operator<
1383 template<typename _Tp, typename _Alloc>
1385 operator>(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
1386 { return __y < __x; }
1388 /// Based on operator<
1389 template<typename _Tp, typename _Alloc>
1391 operator<=(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
1392 { return !(__y < __x); }
1394 /// Based on operator<
1395 template<typename _Tp, typename _Alloc>
1397 operator>=(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
1398 { return !(__x < __y); }
1400 /// See std::vector::swap().
1401 template<typename _Tp, typename _Alloc>
1403 swap(vector<_Tp, _Alloc>& __x, vector<_Tp, _Alloc>& __y)
1406 _GLIBCXX_END_NAMESPACE_CONTAINER
1409 #endif /* _STL_VECTOR_H */