1 // Vector implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
4 // 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 2, 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 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
34 * Hewlett-Packard Company
36 * Permission to use, copy, modify, distribute and sell this software
37 * and its documentation for any purpose is hereby granted without fee,
38 * provided that the above copyright notice appear in all copies and
39 * that both that copyright notice and this permission notice appear
40 * in supporting documentation. Hewlett-Packard Company makes no
41 * representations about the suitability of this software for any
42 * purpose. It is provided "as is" without express or implied warranty.
46 * Silicon Graphics Computer Systems, Inc.
48 * Permission to use, copy, modify, distribute and sell this software
49 * and its documentation for any purpose is hereby granted without fee,
50 * provided that the above copyright notice appear in all copies and
51 * that both that copyright notice and this permission notice appear
52 * in supporting documentation. Silicon Graphics makes no
53 * representations about the suitability of this software for any
54 * purpose. It is provided "as is" without express or implied warranty.
57 /** @file stl_vector.h
58 * This is an internal header file, included by other library headers.
59 * You should not attempt to use it directly.
63 #define _STL_VECTOR_H 1
65 #include <bits/stl_iterator_base_funcs.h>
66 #include <bits/functexcept.h>
67 #include <bits/concept_check.h>
68 #include <initializer_list>
70 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D)
72 /// See bits/stl_deque.h's _Deque_base for an explanation.
73 template<typename _Tp, typename _Alloc>
76 typedef typename _Alloc::template rebind<_Tp>::other _Tp_alloc_type;
79 : public _Tp_alloc_type
81 typename _Tp_alloc_type::pointer _M_start;
82 typename _Tp_alloc_type::pointer _M_finish;
83 typename _Tp_alloc_type::pointer _M_end_of_storage;
86 : _Tp_alloc_type(), _M_start(0), _M_finish(0), _M_end_of_storage(0)
89 _Vector_impl(_Tp_alloc_type const& __a)
90 : _Tp_alloc_type(__a), _M_start(0), _M_finish(0), _M_end_of_storage(0)
95 typedef _Alloc allocator_type;
99 { return *static_cast<_Tp_alloc_type*>(&this->_M_impl); }
101 const _Tp_alloc_type&
102 _M_get_Tp_allocator() const
103 { return *static_cast<const _Tp_alloc_type*>(&this->_M_impl); }
106 get_allocator() const
107 { return allocator_type(_M_get_Tp_allocator()); }
112 _Vector_base(const allocator_type& __a)
115 _Vector_base(size_t __n, const allocator_type& __a)
118 this->_M_impl._M_start = this->_M_allocate(__n);
119 this->_M_impl._M_finish = this->_M_impl._M_start;
120 this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __n;
123 #ifdef __GXX_EXPERIMENTAL_CXX0X__
124 _Vector_base(_Vector_base&& __x)
125 : _M_impl(__x._M_get_Tp_allocator())
127 this->_M_impl._M_start = __x._M_impl._M_start;
128 this->_M_impl._M_finish = __x._M_impl._M_finish;
129 this->_M_impl._M_end_of_storage = __x._M_impl._M_end_of_storage;
130 __x._M_impl._M_start = 0;
131 __x._M_impl._M_finish = 0;
132 __x._M_impl._M_end_of_storage = 0;
137 { _M_deallocate(this->_M_impl._M_start, this->_M_impl._M_end_of_storage
138 - this->_M_impl._M_start); }
141 _Vector_impl _M_impl;
143 typename _Tp_alloc_type::pointer
144 _M_allocate(size_t __n)
145 { return __n != 0 ? _M_impl.allocate(__n) : 0; }
148 _M_deallocate(typename _Tp_alloc_type::pointer __p, size_t __n)
151 _M_impl.deallocate(__p, __n);
157 * @brief A standard container which offers fixed time access to
158 * individual elements in any order.
160 * @ingroup Containers
163 * Meets the requirements of a <a href="tables.html#65">container</a>, a
164 * <a href="tables.html#66">reversible container</a>, and a
165 * <a href="tables.html#67">sequence</a>, including the
166 * <a href="tables.html#68">optional sequence requirements</a> with the
167 * %exception of @c push_front and @c pop_front.
169 * In some terminology a %vector can be described as a dynamic
170 * C-style array, it offers fast and efficient access to individual
171 * elements in any order and saves the user from worrying about
172 * memory and size allocation. Subscripting ( @c [] ) access is
173 * also provided as with C-style arrays.
175 template<typename _Tp, typename _Alloc = std::allocator<_Tp> >
176 class vector : protected _Vector_base<_Tp, _Alloc>
178 // Concept requirements.
179 typedef typename _Alloc::value_type _Alloc_value_type;
180 __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
181 __glibcxx_class_requires2(_Tp, _Alloc_value_type, _SameTypeConcept)
183 typedef _Vector_base<_Tp, _Alloc> _Base;
184 typedef vector<_Tp, _Alloc> vector_type;
185 typedef typename _Base::_Tp_alloc_type _Tp_alloc_type;
188 typedef _Tp value_type;
189 typedef typename _Tp_alloc_type::pointer pointer;
190 typedef typename _Tp_alloc_type::const_pointer const_pointer;
191 typedef typename _Tp_alloc_type::reference reference;
192 typedef typename _Tp_alloc_type::const_reference const_reference;
193 typedef __gnu_cxx::__normal_iterator<pointer, vector_type> iterator;
194 typedef __gnu_cxx::__normal_iterator<const_pointer, vector_type>
196 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
197 typedef std::reverse_iterator<iterator> reverse_iterator;
198 typedef size_t size_type;
199 typedef ptrdiff_t difference_type;
200 typedef _Alloc allocator_type;
203 using _Base::_M_allocate;
204 using _Base::_M_deallocate;
205 using _Base::_M_impl;
206 using _Base::_M_get_Tp_allocator;
209 // [23.2.4.1] construct/copy/destroy
210 // (assign() and get_allocator() are also listed in this section)
212 * @brief Default constructor creates no elements.
218 * @brief Creates a %vector with no elements.
219 * @param a An allocator object.
222 vector(const allocator_type& __a)
226 * @brief Creates a %vector with copies of an exemplar element.
227 * @param n The number of elements to initially create.
228 * @param value An element to copy.
229 * @param a An allocator.
231 * This constructor fills the %vector with @a n copies of @a value.
234 vector(size_type __n, const value_type& __value = value_type(),
235 const allocator_type& __a = allocator_type())
237 { _M_fill_initialize(__n, __value); }
240 * @brief %Vector copy constructor.
241 * @param x A %vector of identical element and allocator types.
243 * The newly-created %vector uses a copy of the allocation
244 * object used by @a x. All the elements of @a x are copied,
245 * but any extra memory in
246 * @a x (for fast expansion) will not be copied.
248 vector(const vector& __x)
249 : _Base(__x.size(), __x._M_get_Tp_allocator())
250 { this->_M_impl._M_finish =
251 std::__uninitialized_copy_a(__x.begin(), __x.end(),
252 this->_M_impl._M_start,
253 _M_get_Tp_allocator());
256 #ifdef __GXX_EXPERIMENTAL_CXX0X__
258 * @brief %Vector move constructor.
259 * @param x A %vector of identical element and allocator types.
261 * The newly-created %vector contains the exact contents of @a x.
262 * The contents of @a x are a valid, but unspecified %vector.
265 : _Base(std::forward<_Base>(__x)) { }
268 * @brief Builds a %vector from an initializer list.
269 * @param l An initializer_list.
270 * @param a An allocator.
272 * Create a %vector consisting of copies of the elements in the
273 * initializer_list @a l.
275 * This will call the element type's copy constructor N times
276 * (where N is @a l.size()) and do no memory reallocation.
278 vector(initializer_list<value_type> __l,
279 const allocator_type& __a = allocator_type())
282 _M_range_initialize(__l.begin(), __l.end(),
283 random_access_iterator_tag());
288 * @brief Builds a %vector from a range.
289 * @param first An input iterator.
290 * @param last An input iterator.
291 * @param a An allocator.
293 * Create a %vector consisting of copies of the elements from
296 * If the iterators are forward, bidirectional, or
297 * random-access, then this will call the elements' copy
298 * constructor N times (where N is distance(first,last)) and do
299 * no memory reallocation. But if only input iterators are
300 * used, then this will do at most 2N calls to the copy
301 * constructor, and logN memory reallocations.
303 template<typename _InputIterator>
304 vector(_InputIterator __first, _InputIterator __last,
305 const allocator_type& __a = allocator_type())
308 // Check whether it's an integral type. If so, it's not an iterator.
309 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
310 _M_initialize_dispatch(__first, __last, _Integral());
314 * The dtor only erases the elements, and note that if the
315 * elements themselves are pointers, the pointed-to memory is
316 * not touched in any way. Managing the pointer is the user's
320 { std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
321 _M_get_Tp_allocator()); }
324 * @brief %Vector assignment operator.
325 * @param x A %vector of identical element and allocator types.
327 * All the elements of @a x are copied, but any extra memory in
328 * @a x (for fast expansion) will not be copied. Unlike the
329 * copy constructor, the allocator object is not copied.
332 operator=(const vector& __x);
334 #ifdef __GXX_EXPERIMENTAL_CXX0X__
336 * @brief %Vector move assignment operator.
337 * @param x A %vector of identical element and allocator types.
339 * The contents of @a x are moved into this %vector (without copying).
340 * @a x is a valid, but unspecified %vector.
343 operator=(vector&& __x)
352 * @brief %Vector list assignment operator.
353 * @param l An initializer_list.
355 * This function fills a %vector with copies of the elements in the
356 * initializer list @a l.
358 * Note that the assignment completely changes the %vector and
359 * that the resulting %vector's size is the same as the number
360 * of elements assigned. Old data may be lost.
363 operator=(initializer_list<value_type> __l)
365 this->assign(__l.begin(), __l.end());
371 * @brief Assigns a given value to a %vector.
372 * @param n Number of elements to be assigned.
373 * @param val Value to be assigned.
375 * This function fills a %vector with @a n copies of the given
376 * value. Note that the assignment completely changes the
377 * %vector and that the resulting %vector's size is the same as
378 * the number of elements assigned. Old data may be lost.
381 assign(size_type __n, const value_type& __val)
382 { _M_fill_assign(__n, __val); }
385 * @brief Assigns a range to a %vector.
386 * @param first An input iterator.
387 * @param last An input iterator.
389 * This function fills a %vector with copies of the elements in the
390 * range [first,last).
392 * Note that the assignment completely changes the %vector and
393 * that the resulting %vector's size is the same as the number
394 * of elements assigned. Old data may be lost.
396 template<typename _InputIterator>
398 assign(_InputIterator __first, _InputIterator __last)
400 // Check whether it's an integral type. If so, it's not an iterator.
401 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
402 _M_assign_dispatch(__first, __last, _Integral());
405 #ifdef __GXX_EXPERIMENTAL_CXX0X__
407 * @brief Assigns an initializer list to a %vector.
408 * @param l An initializer_list.
410 * This function fills a %vector with copies of the elements in the
411 * initializer list @a l.
413 * Note that the assignment completely changes the %vector and
414 * that the resulting %vector's size is the same as the number
415 * of elements assigned. Old data may be lost.
418 assign(initializer_list<value_type> __l)
419 { this->assign(__l.begin(), __l.end()); }
422 /// Get a copy of the memory allocation object.
423 using _Base::get_allocator;
427 * Returns a read/write iterator that points to the first
428 * element in the %vector. Iteration is done in ordinary
433 { return iterator(this->_M_impl._M_start); }
436 * Returns a read-only (constant) iterator that points to the
437 * first element in the %vector. Iteration is done in ordinary
442 { return const_iterator(this->_M_impl._M_start); }
445 * Returns a read/write iterator that points one past the last
446 * element in the %vector. Iteration is done in ordinary
451 { return iterator(this->_M_impl._M_finish); }
454 * Returns a read-only (constant) iterator that points one past
455 * the last element in the %vector. Iteration is done in
456 * ordinary element order.
460 { return const_iterator(this->_M_impl._M_finish); }
463 * Returns a read/write reverse iterator that points to the
464 * last element in the %vector. Iteration is done in reverse
469 { return reverse_iterator(end()); }
472 * Returns a read-only (constant) reverse iterator that points
473 * to the last element in the %vector. Iteration is done in
474 * reverse element order.
476 const_reverse_iterator
478 { return const_reverse_iterator(end()); }
481 * Returns a read/write reverse iterator that points to one
482 * before the first element in the %vector. Iteration is done
483 * in reverse element order.
487 { return reverse_iterator(begin()); }
490 * Returns a read-only (constant) reverse iterator that points
491 * to one before the first element in the %vector. Iteration
492 * is done in reverse element order.
494 const_reverse_iterator
496 { return const_reverse_iterator(begin()); }
498 #ifdef __GXX_EXPERIMENTAL_CXX0X__
500 * Returns a read-only (constant) iterator that points to the
501 * first element in the %vector. Iteration is done in ordinary
506 { return const_iterator(this->_M_impl._M_start); }
509 * Returns a read-only (constant) iterator that points one past
510 * the last element in the %vector. Iteration is done in
511 * ordinary element order.
515 { return const_iterator(this->_M_impl._M_finish); }
518 * Returns a read-only (constant) reverse iterator that points
519 * to the last element in the %vector. Iteration is done in
520 * reverse element order.
522 const_reverse_iterator
524 { return const_reverse_iterator(end()); }
527 * Returns a read-only (constant) reverse iterator that points
528 * to one before the first element in the %vector. Iteration
529 * is done in reverse element order.
531 const_reverse_iterator
533 { return const_reverse_iterator(begin()); }
536 // [23.2.4.2] capacity
537 /** Returns the number of elements in the %vector. */
540 { return size_type(this->_M_impl._M_finish - this->_M_impl._M_start); }
542 /** Returns the size() of the largest possible %vector. */
545 { return _M_get_Tp_allocator().max_size(); }
548 * @brief Resizes the %vector to the specified number of elements.
549 * @param new_size Number of elements the %vector should contain.
550 * @param x Data with which new elements should be populated.
552 * This function will %resize the %vector to the specified
553 * number of elements. If the number is smaller than the
554 * %vector's current size the %vector is truncated, otherwise
555 * the %vector is extended and new elements are populated with
559 resize(size_type __new_size, value_type __x = value_type())
561 if (__new_size < size())
562 _M_erase_at_end(this->_M_impl._M_start + __new_size);
564 insert(end(), __new_size - size(), __x);
568 * Returns the total number of elements that the %vector can
569 * hold before needing to allocate more memory.
573 { return size_type(this->_M_impl._M_end_of_storage
574 - this->_M_impl._M_start); }
577 * Returns true if the %vector is empty. (Thus begin() would
582 { return begin() == end(); }
585 * @brief Attempt to preallocate enough memory for specified number of
587 * @param n Number of elements required.
588 * @throw std::length_error If @a n exceeds @c max_size().
590 * This function attempts to reserve enough memory for the
591 * %vector to hold the specified number of elements. If the
592 * number requested is more than max_size(), length_error is
595 * The advantage of this function is that if optimal code is a
596 * necessity and the user can determine the number of elements
597 * that will be required, the user can reserve the memory in
598 * %advance, and thus prevent a possible reallocation of memory
599 * and copying of %vector data.
602 reserve(size_type __n);
606 * @brief Subscript access to the data contained in the %vector.
607 * @param n The index of the element for which data should be
609 * @return Read/write reference to data.
611 * This operator allows for easy, array-style, data access.
612 * Note that data access with this operator is unchecked and
613 * out_of_range lookups are not defined. (For checked lookups
617 operator[](size_type __n)
618 { return *(this->_M_impl._M_start + __n); }
621 * @brief Subscript access to the data contained in the %vector.
622 * @param n The index of the element for which data should be
624 * @return Read-only (constant) reference to data.
626 * This operator allows for easy, array-style, data access.
627 * Note that data access with this operator is unchecked and
628 * out_of_range lookups are not defined. (For checked lookups
632 operator[](size_type __n) const
633 { return *(this->_M_impl._M_start + __n); }
636 /// Safety check used only from at().
638 _M_range_check(size_type __n) const
640 if (__n >= this->size())
641 __throw_out_of_range(__N("vector::_M_range_check"));
646 * @brief Provides access to the data contained in the %vector.
647 * @param n The index of the element for which data should be
649 * @return Read/write reference to data.
650 * @throw std::out_of_range If @a n is an invalid index.
652 * This function provides for safer data access. The parameter
653 * is first checked that it is in the range of the vector. The
654 * function throws out_of_range if the check fails.
664 * @brief Provides access to the data contained in the %vector.
665 * @param n The index of the element for which data should be
667 * @return Read-only (constant) reference to data.
668 * @throw std::out_of_range If @a n is an invalid index.
670 * This function provides for safer data access. The parameter
671 * is first checked that it is in the range of the vector. The
672 * function throws out_of_range if the check fails.
675 at(size_type __n) const
682 * Returns a read/write reference to the data at the first
683 * element of the %vector.
690 * Returns a read-only (constant) reference to the data at the first
691 * element of the %vector.
698 * Returns a read/write reference to the data at the last
699 * element of the %vector.
703 { return *(end() - 1); }
706 * Returns a read-only (constant) reference to the data at the
707 * last element of the %vector.
711 { return *(end() - 1); }
713 // _GLIBCXX_RESOLVE_LIB_DEFECTS
714 // DR 464. Suggestion for new member functions in standard containers.
717 * Returns a pointer such that [data(), data() + size()) is a valid
718 * range. For a non-empty %vector, data() == &front().
722 { return pointer(this->_M_impl._M_start); }
726 { return const_pointer(this->_M_impl._M_start); }
728 // [23.2.4.3] modifiers
730 * @brief Add data to the end of the %vector.
731 * @param x Data to be added.
733 * This is a typical stack operation. The function creates an
734 * element at the end of the %vector and assigns the given data
735 * to it. Due to the nature of a %vector this operation can be
736 * done in constant time if the %vector has preallocated space
740 push_back(const value_type& __x)
742 if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage)
744 this->_M_impl.construct(this->_M_impl._M_finish, __x);
745 ++this->_M_impl._M_finish;
748 _M_insert_aux(end(), __x);
751 #ifdef __GXX_EXPERIMENTAL_CXX0X__
753 push_back(value_type&& __x)
754 { emplace_back(std::move(__x)); }
756 template<typename... _Args>
758 emplace_back(_Args&&... __args);
762 * @brief Removes last element.
764 * This is a typical stack operation. It shrinks the %vector by one.
766 * Note that no data is returned, and if the last element's
767 * data is needed, it should be retrieved before pop_back() is
773 --this->_M_impl._M_finish;
774 this->_M_impl.destroy(this->_M_impl._M_finish);
777 #ifdef __GXX_EXPERIMENTAL_CXX0X__
779 * @brief Inserts an object in %vector before specified iterator.
780 * @param position An iterator into the %vector.
781 * @param args Arguments.
782 * @return An iterator that points to the inserted data.
784 * This function will insert an object of type T constructed
785 * with T(std::forward<Args>(args)...) before the specified location.
786 * Note that this kind of operation could be expensive for a %vector
787 * and if it is frequently used the user should consider using
790 template<typename... _Args>
792 emplace(iterator __position, _Args&&... __args);
796 * @brief Inserts given value into %vector before specified iterator.
797 * @param position An iterator into the %vector.
798 * @param x Data to be inserted.
799 * @return An iterator that points to the inserted data.
801 * This function will insert a copy of the given value before
802 * the specified location. Note that this kind of operation
803 * could be expensive for a %vector and if it is frequently
804 * used the user should consider using std::list.
807 insert(iterator __position, const value_type& __x);
809 #ifdef __GXX_EXPERIMENTAL_CXX0X__
811 * @brief Inserts given rvalue into %vector before specified iterator.
812 * @param position An iterator into the %vector.
813 * @param x Data to be inserted.
814 * @return An iterator that points to the inserted data.
816 * This function will insert a copy of the given rvalue before
817 * the specified location. Note that this kind of operation
818 * could be expensive for a %vector and if it is frequently
819 * used the user should consider using std::list.
822 insert(iterator __position, value_type&& __x)
823 { return emplace(__position, std::move(__x)); }
826 * @brief Inserts an initializer_list into the %vector.
827 * @param position An iterator into the %vector.
828 * @param l An initializer_list.
830 * This function will insert copies of the data in the
831 * initializer_list @a l into the %vector before the location
832 * specified by @a position.
834 * Note that this kind of operation could be expensive for a
835 * %vector and if it is frequently used the user should
836 * consider using std::list.
839 insert(iterator __position, initializer_list<value_type> __l)
840 { this->insert(__position, __l.begin(), __l.end()); }
844 * @brief Inserts a number of copies of given data into the %vector.
845 * @param position An iterator into the %vector.
846 * @param n Number of elements to be inserted.
847 * @param x Data to be inserted.
849 * This function will insert a specified number of copies of
850 * the given data before the location specified by @a position.
852 * Note that this kind of operation could be expensive for a
853 * %vector and if it is frequently used the user should
854 * consider using std::list.
857 insert(iterator __position, size_type __n, const value_type& __x)
858 { _M_fill_insert(__position, __n, __x); }
861 * @brief Inserts a range into the %vector.
862 * @param position An iterator into the %vector.
863 * @param first An input iterator.
864 * @param last An input iterator.
866 * This function will insert copies of the data in the range
867 * [first,last) into the %vector before the location specified
870 * Note that this kind of operation could be expensive for a
871 * %vector and if it is frequently used the user should
872 * consider using std::list.
874 template<typename _InputIterator>
876 insert(iterator __position, _InputIterator __first,
877 _InputIterator __last)
879 // Check whether it's an integral type. If so, it's not an iterator.
880 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
881 _M_insert_dispatch(__position, __first, __last, _Integral());
885 * @brief Remove element at given position.
886 * @param position Iterator pointing to element to be erased.
887 * @return An iterator pointing to the next element (or end()).
889 * This function will erase the element at the given position and thus
890 * shorten the %vector by one.
892 * Note This operation could be expensive and if it is
893 * frequently used the user should consider using std::list.
894 * The user is also cautioned that this function only erases
895 * the element, and that if the element is itself a pointer,
896 * the pointed-to memory is not touched in any way. Managing
897 * the pointer is the user's responsibility.
900 erase(iterator __position);
903 * @brief Remove a range of elements.
904 * @param first Iterator pointing to the first element to be erased.
905 * @param last Iterator pointing to one past the last element to be
907 * @return An iterator pointing to the element pointed to by @a last
908 * prior to erasing (or end()).
910 * This function will erase the elements in the range [first,last) and
911 * shorten the %vector accordingly.
913 * Note This operation could be expensive and if it is
914 * frequently used the user should consider using std::list.
915 * The user is also cautioned that this function only erases
916 * the elements, and that if the elements themselves are
917 * pointers, the pointed-to memory is not touched in any way.
918 * Managing the pointer is the user's responsibility.
921 erase(iterator __first, iterator __last);
924 * @brief Swaps data with another %vector.
925 * @param x A %vector of the same element and allocator types.
927 * This exchanges the elements between two vectors in constant time.
928 * (Three pointers, so it should be quite fast.)
929 * Note that the global std::swap() function is specialized such that
930 * std::swap(v1,v2) will feed to this function.
933 #ifdef __GXX_EXPERIMENTAL_CXX0X__
939 std::swap(this->_M_impl._M_start, __x._M_impl._M_start);
940 std::swap(this->_M_impl._M_finish, __x._M_impl._M_finish);
941 std::swap(this->_M_impl._M_end_of_storage,
942 __x._M_impl._M_end_of_storage);
944 // _GLIBCXX_RESOLVE_LIB_DEFECTS
945 // 431. Swapping containers with unequal allocators.
946 std::__alloc_swap<_Tp_alloc_type>::_S_do_it(_M_get_Tp_allocator(),
947 __x._M_get_Tp_allocator());
951 * Erases all the elements. Note that this function only erases the
952 * elements, and that if the elements themselves are pointers, the
953 * pointed-to memory is not touched in any way. Managing the pointer is
954 * the user's responsibility.
958 { _M_erase_at_end(this->_M_impl._M_start); }
962 * Memory expansion handler. Uses the member allocation function to
963 * obtain @a n bytes of memory, and then copies [first,last) into it.
965 template<typename _ForwardIterator>
967 _M_allocate_and_copy(size_type __n,
968 _ForwardIterator __first, _ForwardIterator __last)
970 pointer __result = this->_M_allocate(__n);
973 std::__uninitialized_copy_a(__first, __last, __result,
974 _M_get_Tp_allocator());
979 _M_deallocate(__result, __n);
980 __throw_exception_again;
985 // Internal constructor functions follow.
987 // Called by the range constructor to implement [23.1.1]/9
989 // _GLIBCXX_RESOLVE_LIB_DEFECTS
990 // 438. Ambiguity in the "do the right thing" clause
991 template<typename _Integer>
993 _M_initialize_dispatch(_Integer __n, _Integer __value, __true_type)
995 this->_M_impl._M_start = _M_allocate(static_cast<size_type>(__n));
996 this->_M_impl._M_end_of_storage =
997 this->_M_impl._M_start + static_cast<size_type>(__n);
998 _M_fill_initialize(static_cast<size_type>(__n), __value);
1001 // Called by the range constructor to implement [23.1.1]/9
1002 template<typename _InputIterator>
1004 _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
1007 typedef typename std::iterator_traits<_InputIterator>::
1008 iterator_category _IterCategory;
1009 _M_range_initialize(__first, __last, _IterCategory());
1012 // Called by the second initialize_dispatch above
1013 template<typename _InputIterator>
1015 _M_range_initialize(_InputIterator __first,
1016 _InputIterator __last, std::input_iterator_tag)
1018 for (; __first != __last; ++__first)
1019 push_back(*__first);
1022 // Called by the second initialize_dispatch above
1023 template<typename _ForwardIterator>
1025 _M_range_initialize(_ForwardIterator __first,
1026 _ForwardIterator __last, std::forward_iterator_tag)
1028 const size_type __n = std::distance(__first, __last);
1029 this->_M_impl._M_start = this->_M_allocate(__n);
1030 this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __n;
1031 this->_M_impl._M_finish =
1032 std::__uninitialized_copy_a(__first, __last,
1033 this->_M_impl._M_start,
1034 _M_get_Tp_allocator());
1037 // Called by the first initialize_dispatch above and by the
1038 // vector(n,value,a) constructor.
1040 _M_fill_initialize(size_type __n, const value_type& __value)
1042 std::__uninitialized_fill_n_a(this->_M_impl._M_start, __n, __value,
1043 _M_get_Tp_allocator());
1044 this->_M_impl._M_finish = this->_M_impl._M_end_of_storage;
1048 // Internal assign functions follow. The *_aux functions do the actual
1049 // assignment work for the range versions.
1051 // Called by the range assign to implement [23.1.1]/9
1053 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1054 // 438. Ambiguity in the "do the right thing" clause
1055 template<typename _Integer>
1057 _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
1058 { _M_fill_assign(__n, __val); }
1060 // Called by the range assign to implement [23.1.1]/9
1061 template<typename _InputIterator>
1063 _M_assign_dispatch(_InputIterator __first, _InputIterator __last,
1066 typedef typename std::iterator_traits<_InputIterator>::
1067 iterator_category _IterCategory;
1068 _M_assign_aux(__first, __last, _IterCategory());
1071 // Called by the second assign_dispatch above
1072 template<typename _InputIterator>
1074 _M_assign_aux(_InputIterator __first, _InputIterator __last,
1075 std::input_iterator_tag);
1077 // Called by the second assign_dispatch above
1078 template<typename _ForwardIterator>
1080 _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
1081 std::forward_iterator_tag);
1083 // Called by assign(n,t), and the range assign when it turns out
1084 // to be the same thing.
1086 _M_fill_assign(size_type __n, const value_type& __val);
1089 // Internal insert functions follow.
1091 // Called by the range insert to implement [23.1.1]/9
1093 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1094 // 438. Ambiguity in the "do the right thing" clause
1095 template<typename _Integer>
1097 _M_insert_dispatch(iterator __pos, _Integer __n, _Integer __val,
1099 { _M_fill_insert(__pos, __n, __val); }
1101 // Called by the range insert to implement [23.1.1]/9
1102 template<typename _InputIterator>
1104 _M_insert_dispatch(iterator __pos, _InputIterator __first,
1105 _InputIterator __last, __false_type)
1107 typedef typename std::iterator_traits<_InputIterator>::
1108 iterator_category _IterCategory;
1109 _M_range_insert(__pos, __first, __last, _IterCategory());
1112 // Called by the second insert_dispatch above
1113 template<typename _InputIterator>
1115 _M_range_insert(iterator __pos, _InputIterator __first,
1116 _InputIterator __last, std::input_iterator_tag);
1118 // Called by the second insert_dispatch above
1119 template<typename _ForwardIterator>
1121 _M_range_insert(iterator __pos, _ForwardIterator __first,
1122 _ForwardIterator __last, std::forward_iterator_tag);
1124 // Called by insert(p,n,x), and the range insert when it turns out to be
1127 _M_fill_insert(iterator __pos, size_type __n, const value_type& __x);
1129 // Called by insert(p,x)
1130 #ifndef __GXX_EXPERIMENTAL_CXX0X__
1132 _M_insert_aux(iterator __position, const value_type& __x);
1134 template<typename... _Args>
1136 _M_insert_aux(iterator __position, _Args&&... __args);
1139 // Called by the latter.
1141 _M_check_len(size_type __n, const char* __s) const
1143 if (max_size() - size() < __n)
1144 __throw_length_error(__N(__s));
1146 const size_type __len = size() + std::max(size(), __n);
1147 return (__len < size() || __len > max_size()) ? max_size() : __len;
1150 // Internal erase functions follow.
1152 // Called by erase(q1,q2), clear(), resize(), _M_fill_assign,
1155 _M_erase_at_end(pointer __pos)
1157 std::_Destroy(__pos, this->_M_impl._M_finish, _M_get_Tp_allocator());
1158 this->_M_impl._M_finish = __pos;
1164 * @brief Vector equality comparison.
1165 * @param x A %vector.
1166 * @param y A %vector of the same type as @a x.
1167 * @return True iff the size and elements of the vectors are equal.
1169 * This is an equivalence relation. It is linear in the size of the
1170 * vectors. Vectors are considered equivalent if their sizes are equal,
1171 * and if corresponding elements compare equal.
1173 template<typename _Tp, typename _Alloc>
1175 operator==(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
1176 { return (__x.size() == __y.size()
1177 && std::equal(__x.begin(), __x.end(), __y.begin())); }
1180 * @brief Vector ordering relation.
1181 * @param x A %vector.
1182 * @param y A %vector of the same type as @a x.
1183 * @return True iff @a x is lexicographically less than @a y.
1185 * This is a total ordering relation. It is linear in the size of the
1186 * vectors. The elements must be comparable with @c <.
1188 * See std::lexicographical_compare() for how the determination is made.
1190 template<typename _Tp, typename _Alloc>
1192 operator<(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
1193 { return std::lexicographical_compare(__x.begin(), __x.end(),
1194 __y.begin(), __y.end()); }
1196 /// Based on operator==
1197 template<typename _Tp, typename _Alloc>
1199 operator!=(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
1200 { return !(__x == __y); }
1202 /// Based on operator<
1203 template<typename _Tp, typename _Alloc>
1205 operator>(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
1206 { return __y < __x; }
1208 /// Based on operator<
1209 template<typename _Tp, typename _Alloc>
1211 operator<=(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
1212 { return !(__y < __x); }
1214 /// Based on operator<
1215 template<typename _Tp, typename _Alloc>
1217 operator>=(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
1218 { return !(__x < __y); }
1220 /// See std::vector::swap().
1221 template<typename _Tp, typename _Alloc>
1223 swap(vector<_Tp, _Alloc>& __x, vector<_Tp, _Alloc>& __y)
1226 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1227 template<typename _Tp, typename _Alloc>
1229 swap(vector<_Tp, _Alloc>&& __x, vector<_Tp, _Alloc>& __y)
1232 template<typename _Tp, typename _Alloc>
1234 swap(vector<_Tp, _Alloc>& __x, vector<_Tp, _Alloc>&& __y)
1238 _GLIBCXX_END_NESTED_NAMESPACE
1240 #endif /* _STL_VECTOR_H */