1 // Vector implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007
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>
69 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std
, _GLIBCXX_STD_D
)
73 * See bits/stl_deque.h's _Deque_base for an explanation.
76 template<typename _Tp
, typename _Alloc
>
79 typedef typename
_Alloc::template rebind
<_Tp
>::other _Tp_alloc_type
;
82 : public _Tp_alloc_type
86 _Tp
* _M_end_of_storage
;
89 : _Tp_alloc_type(), _M_start(0), _M_finish(0), _M_end_of_storage(0)
92 _Vector_impl(_Tp_alloc_type
const& __a
)
93 : _Tp_alloc_type(__a
), _M_start(0), _M_finish(0), _M_end_of_storage(0)
98 typedef _Alloc allocator_type
;
101 _M_get_Tp_allocator()
102 { return *static_cast<_Tp_alloc_type
*>(&this->_M_impl
); }
104 const _Tp_alloc_type
&
105 _M_get_Tp_allocator() const
106 { return *static_cast<const _Tp_alloc_type
*>(&this->_M_impl
); }
109 get_allocator() const
110 { return allocator_type(_M_get_Tp_allocator()); }
115 _Vector_base(const allocator_type
& __a
)
118 _Vector_base(size_t __n
, const allocator_type
& __a
)
121 this->_M_impl
._M_start
= this->_M_allocate(__n
);
122 this->_M_impl
._M_finish
= this->_M_impl
._M_start
;
123 this->_M_impl
._M_end_of_storage
= this->_M_impl
._M_start
+ __n
;
126 #ifdef __GXX_EXPERIMENTAL_CXX0X__
127 _Vector_base(_Vector_base
&& __x
)
128 : _M_impl(__x
._M_get_Tp_allocator())
130 this->_M_impl
._M_start
= __x
._M_impl
._M_start
;
131 this->_M_impl
._M_finish
= __x
._M_impl
._M_finish
;
132 this->_M_impl
._M_end_of_storage
= __x
._M_impl
._M_end_of_storage
;
133 __x
._M_impl
._M_start
= 0;
134 __x
._M_impl
._M_finish
= 0;
135 __x
._M_impl
._M_end_of_storage
= 0;
140 { _M_deallocate(this->_M_impl
._M_start
, this->_M_impl
._M_end_of_storage
141 - this->_M_impl
._M_start
); }
144 _Vector_impl _M_impl
;
147 _M_allocate(size_t __n
)
148 { return __n
!= 0 ? _M_impl
.allocate(__n
) : 0; }
151 _M_deallocate(_Tp
* __p
, size_t __n
)
154 _M_impl
.deallocate(__p
, __n
);
160 * @brief A standard container which offers fixed time access to
161 * individual elements in any order.
163 * @ingroup Containers
166 * Meets the requirements of a <a href="tables.html#65">container</a>, a
167 * <a href="tables.html#66">reversible container</a>, and a
168 * <a href="tables.html#67">sequence</a>, including the
169 * <a href="tables.html#68">optional sequence requirements</a> with the
170 * %exception of @c push_front and @c pop_front.
172 * In some terminology a %vector can be described as a dynamic
173 * C-style array, it offers fast and efficient access to individual
174 * elements in any order and saves the user from worrying about
175 * memory and size allocation. Subscripting ( @c [] ) access is
176 * also provided as with C-style arrays.
178 template<typename _Tp
, typename _Alloc
= std::allocator
<_Tp
> >
179 class vector
: protected _Vector_base
<_Tp
, _Alloc
>
181 // Concept requirements.
182 typedef typename
_Alloc::value_type _Alloc_value_type
;
183 __glibcxx_class_requires(_Tp
, _SGIAssignableConcept
)
184 __glibcxx_class_requires2(_Tp
, _Alloc_value_type
, _SameTypeConcept
)
186 typedef _Vector_base
<_Tp
, _Alloc
> _Base
;
187 typedef vector
<_Tp
, _Alloc
> vector_type
;
188 typedef typename
_Base::_Tp_alloc_type _Tp_alloc_type
;
191 typedef _Tp value_type
;
192 typedef typename
_Tp_alloc_type::pointer pointer
;
193 typedef typename
_Tp_alloc_type::const_pointer const_pointer
;
194 typedef typename
_Tp_alloc_type::reference reference
;
195 typedef typename
_Tp_alloc_type::const_reference const_reference
;
196 typedef __gnu_cxx::__normal_iterator
<pointer
, vector_type
> iterator
;
197 typedef __gnu_cxx::__normal_iterator
<const_pointer
, vector_type
>
199 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
200 typedef std::reverse_iterator
<iterator
> reverse_iterator
;
201 typedef size_t size_type
;
202 typedef ptrdiff_t difference_type
;
203 typedef _Alloc allocator_type
;
206 using _Base::_M_allocate
;
207 using _Base::_M_deallocate
;
208 using _Base::_M_impl
;
209 using _Base::_M_get_Tp_allocator
;
212 // [23.2.4.1] construct/copy/destroy
213 // (assign() and get_allocator() are also listed in this section)
215 * @brief Default constructor creates no elements.
221 * @brief Creates a %vector with no elements.
222 * @param a An allocator object.
225 vector(const allocator_type
& __a
)
229 * @brief Creates a %vector with copies of an exemplar element.
230 * @param n The number of elements to initially create.
231 * @param value An element to copy.
232 * @param a An allocator.
234 * This constructor fills the %vector with @a n copies of @a value.
237 vector(size_type __n
, const value_type
& __value
= value_type(),
238 const allocator_type
& __a
= allocator_type())
240 { _M_fill_initialize(__n
, __value
); }
243 * @brief %Vector copy constructor.
244 * @param x A %vector of identical element and allocator types.
246 * The newly-created %vector uses a copy of the allocation
247 * object used by @a x. All the elements of @a x are copied,
248 * but any extra memory in
249 * @a x (for fast expansion) will not be copied.
251 vector(const vector
& __x
)
252 : _Base(__x
.size(), __x
._M_get_Tp_allocator())
253 { this->_M_impl
._M_finish
=
254 std::__uninitialized_copy_a(__x
.begin(), __x
.end(),
255 this->_M_impl
._M_start
,
256 _M_get_Tp_allocator());
259 #ifdef __GXX_EXPERIMENTAL_CXX0X__
261 * @brief %Vector move constructor.
262 * @param x A %vector of identical element and allocator types.
264 * The newly-created %vector contains the exact contents of @a x.
265 * The contents of @a x are a valid, but unspecified %vector.
268 : _Base(std::forward
<_Base
>(__x
)) { }
272 * @brief Builds a %vector from a range.
273 * @param first An input iterator.
274 * @param last An input iterator.
275 * @param a An allocator.
277 * Create a %vector consisting of copies of the elements from
280 * If the iterators are forward, bidirectional, or
281 * random-access, then this will call the elements' copy
282 * constructor N times (where N is distance(first,last)) and do
283 * no memory reallocation. But if only input iterators are
284 * used, then this will do at most 2N calls to the copy
285 * constructor, and logN memory reallocations.
287 template<typename _InputIterator
>
288 vector(_InputIterator __first
, _InputIterator __last
,
289 const allocator_type
& __a
= allocator_type())
292 // Check whether it's an integral type. If so, it's not an iterator.
293 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
294 _M_initialize_dispatch(__first
, __last
, _Integral());
298 * The dtor only erases the elements, and note that if the
299 * elements themselves are pointers, the pointed-to memory is
300 * not touched in any way. Managing the pointer is the user's
304 { std::_Destroy(this->_M_impl
._M_start
, this->_M_impl
._M_finish
,
305 _M_get_Tp_allocator()); }
308 * @brief %Vector assignment operator.
309 * @param x A %vector of identical element and allocator types.
311 * All the elements of @a x are copied, but any extra memory in
312 * @a x (for fast expansion) will not be copied. Unlike the
313 * copy constructor, the allocator object is not copied.
316 operator=(const vector
& __x
);
318 #ifdef __GXX_EXPERIMENTAL_CXX0X__
320 * @brief %Vector move assignment operator.
321 * @param x A %vector of identical element and allocator types.
323 * The contents of @a x are moved into this %vector (without copying).
324 * @a x is a valid, but unspecified %vector.
327 operator=(vector
&& __x
)
337 * @brief Assigns a given value to a %vector.
338 * @param n Number of elements to be assigned.
339 * @param val Value to be assigned.
341 * This function fills a %vector with @a n copies of the given
342 * value. Note that the assignment completely changes the
343 * %vector and that the resulting %vector's size is the same as
344 * the number of elements assigned. Old data may be lost.
347 assign(size_type __n
, const value_type
& __val
)
348 { _M_fill_assign(__n
, __val
); }
351 * @brief Assigns a range to a %vector.
352 * @param first An input iterator.
353 * @param last An input iterator.
355 * This function fills a %vector with copies of the elements in the
356 * range [first,last).
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.
362 template<typename _InputIterator
>
364 assign(_InputIterator __first
, _InputIterator __last
)
366 // Check whether it's an integral type. If so, it's not an iterator.
367 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
368 _M_assign_dispatch(__first
, __last
, _Integral());
371 /// Get a copy of the memory allocation object.
372 using _Base::get_allocator
;
376 * Returns a read/write iterator that points to the first
377 * element in the %vector. Iteration is done in ordinary
382 { return iterator(this->_M_impl
._M_start
); }
385 * Returns a read-only (constant) iterator that points to the
386 * first element in the %vector. Iteration is done in ordinary
391 { return const_iterator(this->_M_impl
._M_start
); }
394 * Returns a read/write iterator that points one past the last
395 * element in the %vector. Iteration is done in ordinary
400 { return iterator(this->_M_impl
._M_finish
); }
403 * Returns a read-only (constant) iterator that points one past
404 * the last element in the %vector. Iteration is done in
405 * ordinary element order.
409 { return const_iterator(this->_M_impl
._M_finish
); }
412 * Returns a read/write reverse iterator that points to the
413 * last element in the %vector. Iteration is done in reverse
418 { return reverse_iterator(end()); }
421 * Returns a read-only (constant) reverse iterator that points
422 * to the last element in the %vector. Iteration is done in
423 * reverse element order.
425 const_reverse_iterator
427 { return const_reverse_iterator(end()); }
430 * Returns a read/write reverse iterator that points to one
431 * before the first element in the %vector. Iteration is done
432 * in reverse element order.
436 { return reverse_iterator(begin()); }
439 * Returns a read-only (constant) reverse iterator that points
440 * to one before the first element in the %vector. Iteration
441 * is done in reverse element order.
443 const_reverse_iterator
445 { return const_reverse_iterator(begin()); }
447 #ifdef __GXX_EXPERIMENTAL_CXX0X__
449 * Returns a read-only (constant) iterator that points to the
450 * first element in the %vector. Iteration is done in ordinary
455 { return const_iterator(this->_M_impl
._M_start
); }
458 * Returns a read-only (constant) iterator that points one past
459 * the last element in the %vector. Iteration is done in
460 * ordinary element order.
464 { return const_iterator(this->_M_impl
._M_finish
); }
467 * Returns a read-only (constant) reverse iterator that points
468 * to the last element in the %vector. Iteration is done in
469 * reverse element order.
471 const_reverse_iterator
473 { return const_reverse_iterator(end()); }
476 * Returns a read-only (constant) reverse iterator that points
477 * to one before the first element in the %vector. Iteration
478 * is done in reverse element order.
480 const_reverse_iterator
482 { return const_reverse_iterator(begin()); }
485 // [23.2.4.2] capacity
486 /** Returns the number of elements in the %vector. */
489 { return size_type(this->_M_impl
._M_finish
- this->_M_impl
._M_start
); }
491 /** Returns the size() of the largest possible %vector. */
494 { return _M_get_Tp_allocator().max_size(); }
497 * @brief Resizes the %vector to the specified number of elements.
498 * @param new_size Number of elements the %vector should contain.
499 * @param x Data with which new elements should be populated.
501 * This function will %resize the %vector to the specified
502 * number of elements. If the number is smaller than the
503 * %vector's current size the %vector is truncated, otherwise
504 * the %vector is extended and new elements are populated with
508 resize(size_type __new_size
, value_type __x
= value_type())
510 if (__new_size
< size())
511 _M_erase_at_end(this->_M_impl
._M_start
+ __new_size
);
513 insert(end(), __new_size
- size(), __x
);
517 * Returns the total number of elements that the %vector can
518 * hold before needing to allocate more memory.
522 { return size_type(this->_M_impl
._M_end_of_storage
523 - this->_M_impl
._M_start
); }
526 * Returns true if the %vector is empty. (Thus begin() would
531 { return begin() == end(); }
534 * @brief Attempt to preallocate enough memory for specified number of
536 * @param n Number of elements required.
537 * @throw std::length_error If @a n exceeds @c max_size().
539 * This function attempts to reserve enough memory for the
540 * %vector to hold the specified number of elements. If the
541 * number requested is more than max_size(), length_error is
544 * The advantage of this function is that if optimal code is a
545 * necessity and the user can determine the number of elements
546 * that will be required, the user can reserve the memory in
547 * %advance, and thus prevent a possible reallocation of memory
548 * and copying of %vector data.
551 reserve(size_type __n
);
555 * @brief Subscript access to the data contained in the %vector.
556 * @param n The index of the element for which data should be
558 * @return Read/write reference to data.
560 * This operator allows for easy, array-style, data access.
561 * Note that data access with this operator is unchecked and
562 * out_of_range lookups are not defined. (For checked lookups
566 operator[](size_type __n
)
567 { return *(this->_M_impl
._M_start
+ __n
); }
570 * @brief Subscript access to the data contained in the %vector.
571 * @param n The index of the element for which data should be
573 * @return Read-only (constant) reference to data.
575 * This operator allows for easy, array-style, data access.
576 * Note that data access with this operator is unchecked and
577 * out_of_range lookups are not defined. (For checked lookups
581 operator[](size_type __n
) const
582 { return *(this->_M_impl
._M_start
+ __n
); }
585 /// @if maint Safety check used only from at(). @endif
587 _M_range_check(size_type __n
) const
589 if (__n
>= this->size())
590 __throw_out_of_range(__N("vector::_M_range_check"));
595 * @brief Provides access to the data contained in the %vector.
596 * @param n The index of the element for which data should be
598 * @return Read/write reference to data.
599 * @throw std::out_of_range If @a n is an invalid index.
601 * This function provides for safer data access. The parameter
602 * is first checked that it is in the range of the vector. The
603 * function throws out_of_range if the check fails.
613 * @brief Provides access to the data contained in the %vector.
614 * @param n The index of the element for which data should be
616 * @return Read-only (constant) reference to data.
617 * @throw std::out_of_range If @a n is an invalid index.
619 * This function provides for safer data access. The parameter
620 * is first checked that it is in the range of the vector. The
621 * function throws out_of_range if the check fails.
624 at(size_type __n
) const
631 * Returns a read/write reference to the data at the first
632 * element of the %vector.
639 * Returns a read-only (constant) reference to the data at the first
640 * element of the %vector.
647 * Returns a read/write reference to the data at the last
648 * element of the %vector.
652 { return *(end() - 1); }
655 * Returns a read-only (constant) reference to the data at the
656 * last element of the %vector.
660 { return *(end() - 1); }
662 // _GLIBCXX_RESOLVE_LIB_DEFECTS
663 // DR 464. Suggestion for new member functions in standard containers.
666 * Returns a pointer such that [data(), data() + size()) is a valid
667 * range. For a non-empty %vector, data() == &front().
671 { return pointer(this->_M_impl
._M_start
); }
675 { return const_pointer(this->_M_impl
._M_start
); }
677 // [23.2.4.3] modifiers
679 * @brief Add data to the end of the %vector.
680 * @param x Data to be added.
682 * This is a typical stack operation. The function creates an
683 * element at the end of the %vector and assigns the given data
684 * to it. Due to the nature of a %vector this operation can be
685 * done in constant time if the %vector has preallocated space
688 #ifndef __GXX_EXPERIMENTAL_CXX0X__
690 push_back(const value_type
& __x
)
692 if (this->_M_impl
._M_finish
!= this->_M_impl
._M_end_of_storage
)
694 this->_M_impl
.construct(this->_M_impl
._M_finish
, __x
);
695 ++this->_M_impl
._M_finish
;
698 _M_insert_aux(end(), __x
);
701 template<typename
... _Args
>
703 push_back(_Args
&&... __args
)
705 if (this->_M_impl
._M_finish
!= this->_M_impl
._M_end_of_storage
)
707 this->_M_impl
.construct(this->_M_impl
._M_finish
,
708 std::forward
<_Args
>(__args
)...);
709 ++this->_M_impl
._M_finish
;
712 _M_insert_aux(end(), std::forward
<_Args
>(__args
)...);
717 * @brief Removes last element.
719 * This is a typical stack operation. It shrinks the %vector by one.
721 * Note that no data is returned, and if the last element's
722 * data is needed, it should be retrieved before pop_back() is
728 --this->_M_impl
._M_finish
;
729 this->_M_impl
.destroy(this->_M_impl
._M_finish
);
732 #ifdef __GXX_EXPERIMENTAL_CXX0X__
734 * @brief Inserts an object in %vector before specified iterator.
735 * @param position An iterator into the %vector.
736 * @param args Arguments.
737 * @return An iterator that points to the inserted data.
739 * This function will insert an object of type T constructed
740 * with T(std::forward<Args>(args)...) before the specified location.
741 * Note that this kind of operation could be expensive for a %vector
742 * and if it is frequently used the user should consider using
745 template<typename
... _Args
>
747 emplace(iterator __position
, _Args
&&... __args
);
751 * @brief Inserts given value into %vector before specified iterator.
752 * @param position An iterator into the %vector.
753 * @param x Data to be inserted.
754 * @return An iterator that points to the inserted data.
756 * This function will insert a copy of the given value before
757 * the specified location. Note that this kind of operation
758 * could be expensive for a %vector and if it is frequently
759 * used the user should consider using std::list.
762 insert(iterator __position
, const value_type
& __x
);
764 #ifdef __GXX_EXPERIMENTAL_CXX0X__
766 * @brief Inserts given rvalue into %vector before specified iterator.
767 * @param position An iterator into the %vector.
768 * @param x Data to be inserted.
769 * @return An iterator that points to the inserted data.
771 * This function will insert a copy of the given rvalue before
772 * the specified location. Note that this kind of operation
773 * could be expensive for a %vector and if it is frequently
774 * used the user should consider using std::list.
777 insert(iterator __position
, value_type
&& __x
)
778 { return emplace(__position
, std::move(__x
)); }
782 * @brief Inserts a number of copies of given data into the %vector.
783 * @param position An iterator into the %vector.
784 * @param n Number of elements to be inserted.
785 * @param x Data to be inserted.
787 * This function will insert a specified number of copies of
788 * the given data before the location specified by @a position.
790 * Note that this kind of operation could be expensive for a
791 * %vector and if it is frequently used the user should
792 * consider using std::list.
795 insert(iterator __position
, size_type __n
, const value_type
& __x
)
796 { _M_fill_insert(__position
, __n
, __x
); }
799 * @brief Inserts a range into the %vector.
800 * @param position An iterator into the %vector.
801 * @param first An input iterator.
802 * @param last An input iterator.
804 * This function will insert copies of the data in the range
805 * [first,last) into the %vector before the location specified
808 * Note that this kind of operation could be expensive for a
809 * %vector and if it is frequently used the user should
810 * consider using std::list.
812 template<typename _InputIterator
>
814 insert(iterator __position
, _InputIterator __first
,
815 _InputIterator __last
)
817 // Check whether it's an integral type. If so, it's not an iterator.
818 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
819 _M_insert_dispatch(__position
, __first
, __last
, _Integral());
823 * @brief Remove element at given position.
824 * @param position Iterator pointing to element to be erased.
825 * @return An iterator pointing to the next element (or end()).
827 * This function will erase the element at the given position and thus
828 * shorten the %vector by one.
830 * Note This operation could be expensive and if it is
831 * frequently used the user should consider using std::list.
832 * The user is also cautioned that this function only erases
833 * the element, and that if the element is itself a pointer,
834 * the pointed-to memory is not touched in any way. Managing
835 * the pointer is the user's responsibilty.
838 erase(iterator __position
);
841 * @brief Remove a range of elements.
842 * @param first Iterator pointing to the first element to be erased.
843 * @param last Iterator pointing to one past the last element to be
845 * @return An iterator pointing to the element pointed to by @a last
846 * prior to erasing (or end()).
848 * This function will erase the elements in the range [first,last) and
849 * shorten the %vector accordingly.
851 * Note This operation could be expensive and if it is
852 * frequently used the user should consider using std::list.
853 * The user is also cautioned that this function only erases
854 * the elements, and that if the elements themselves are
855 * pointers, the pointed-to memory is not touched in any way.
856 * Managing the pointer is the user's responsibilty.
859 erase(iterator __first
, iterator __last
);
862 * @brief Swaps data with another %vector.
863 * @param x A %vector of the same element and allocator types.
865 * This exchanges the elements between two vectors in constant time.
866 * (Three pointers, so it should be quite fast.)
867 * Note that the global std::swap() function is specialized such that
868 * std::swap(v1,v2) will feed to this function.
871 #ifdef __GXX_EXPERIMENTAL_CXX0X__
877 std::swap(this->_M_impl
._M_start
, __x
._M_impl
._M_start
);
878 std::swap(this->_M_impl
._M_finish
, __x
._M_impl
._M_finish
);
879 std::swap(this->_M_impl
._M_end_of_storage
,
880 __x
._M_impl
._M_end_of_storage
);
882 // _GLIBCXX_RESOLVE_LIB_DEFECTS
883 // 431. Swapping containers with unequal allocators.
884 std::__alloc_swap
<_Tp_alloc_type
>::_S_do_it(_M_get_Tp_allocator(),
885 __x
._M_get_Tp_allocator());
889 * Erases all the elements. Note that this function only erases the
890 * elements, and that if the elements themselves are pointers, the
891 * pointed-to memory is not touched in any way. Managing the pointer is
892 * the user's responsibilty.
896 { _M_erase_at_end(this->_M_impl
._M_start
); }
901 * Memory expansion handler. Uses the member allocation function to
902 * obtain @a n bytes of memory, and then copies [first,last) into it.
905 template<typename _ForwardIterator
>
907 _M_allocate_and_copy(size_type __n
,
908 _ForwardIterator __first
, _ForwardIterator __last
)
910 pointer __result
= this->_M_allocate(__n
);
913 std::__uninitialized_copy_a(__first
, __last
, __result
,
914 _M_get_Tp_allocator());
919 _M_deallocate(__result
, __n
);
920 __throw_exception_again
;
925 // Internal constructor functions follow.
927 // Called by the range constructor to implement [23.1.1]/9
929 // _GLIBCXX_RESOLVE_LIB_DEFECTS
930 // 438. Ambiguity in the "do the right thing" clause
931 template<typename _Integer
>
933 _M_initialize_dispatch(_Integer __n
, _Integer __value
, __true_type
)
935 this->_M_impl
._M_start
= _M_allocate(static_cast<size_type
>(__n
));
936 this->_M_impl
._M_end_of_storage
=
937 this->_M_impl
._M_start
+ static_cast<size_type
>(__n
);
938 _M_fill_initialize(static_cast<size_type
>(__n
), __value
);
941 // Called by the range constructor to implement [23.1.1]/9
942 template<typename _InputIterator
>
944 _M_initialize_dispatch(_InputIterator __first
, _InputIterator __last
,
947 typedef typename
std::iterator_traits
<_InputIterator
>::
948 iterator_category _IterCategory
;
949 _M_range_initialize(__first
, __last
, _IterCategory());
952 // Called by the second initialize_dispatch above
953 template<typename _InputIterator
>
955 _M_range_initialize(_InputIterator __first
,
956 _InputIterator __last
, std::input_iterator_tag
)
958 for (; __first
!= __last
; ++__first
)
962 // Called by the second initialize_dispatch above
963 template<typename _ForwardIterator
>
965 _M_range_initialize(_ForwardIterator __first
,
966 _ForwardIterator __last
, std::forward_iterator_tag
)
968 const size_type __n
= std::distance(__first
, __last
);
969 this->_M_impl
._M_start
= this->_M_allocate(__n
);
970 this->_M_impl
._M_end_of_storage
= this->_M_impl
._M_start
+ __n
;
971 this->_M_impl
._M_finish
=
972 std::__uninitialized_copy_a(__first
, __last
,
973 this->_M_impl
._M_start
,
974 _M_get_Tp_allocator());
977 // Called by the first initialize_dispatch above and by the
978 // vector(n,value,a) constructor.
980 _M_fill_initialize(size_type __n
, const value_type
& __value
)
982 std::__uninitialized_fill_n_a(this->_M_impl
._M_start
, __n
, __value
,
983 _M_get_Tp_allocator());
984 this->_M_impl
._M_finish
= this->_M_impl
._M_end_of_storage
;
988 // Internal assign functions follow. The *_aux functions do the actual
989 // assignment work for the range versions.
991 // Called by the range assign to implement [23.1.1]/9
993 // _GLIBCXX_RESOLVE_LIB_DEFECTS
994 // 438. Ambiguity in the "do the right thing" clause
995 template<typename _Integer
>
997 _M_assign_dispatch(_Integer __n
, _Integer __val
, __true_type
)
998 { _M_fill_assign(__n
, __val
); }
1000 // Called by the range assign to implement [23.1.1]/9
1001 template<typename _InputIterator
>
1003 _M_assign_dispatch(_InputIterator __first
, _InputIterator __last
,
1006 typedef typename
std::iterator_traits
<_InputIterator
>::
1007 iterator_category _IterCategory
;
1008 _M_assign_aux(__first
, __last
, _IterCategory());
1011 // Called by the second assign_dispatch above
1012 template<typename _InputIterator
>
1014 _M_assign_aux(_InputIterator __first
, _InputIterator __last
,
1015 std::input_iterator_tag
);
1017 // Called by the second assign_dispatch above
1018 template<typename _ForwardIterator
>
1020 _M_assign_aux(_ForwardIterator __first
, _ForwardIterator __last
,
1021 std::forward_iterator_tag
);
1023 // Called by assign(n,t), and the range assign when it turns out
1024 // to be the same thing.
1026 _M_fill_assign(size_type __n
, const value_type
& __val
);
1029 // Internal insert functions follow.
1031 // Called by the range insert to implement [23.1.1]/9
1033 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1034 // 438. Ambiguity in the "do the right thing" clause
1035 template<typename _Integer
>
1037 _M_insert_dispatch(iterator __pos
, _Integer __n
, _Integer __val
,
1039 { _M_fill_insert(__pos
, __n
, __val
); }
1041 // Called by the range insert to implement [23.1.1]/9
1042 template<typename _InputIterator
>
1044 _M_insert_dispatch(iterator __pos
, _InputIterator __first
,
1045 _InputIterator __last
, __false_type
)
1047 typedef typename
std::iterator_traits
<_InputIterator
>::
1048 iterator_category _IterCategory
;
1049 _M_range_insert(__pos
, __first
, __last
, _IterCategory());
1052 // Called by the second insert_dispatch above
1053 template<typename _InputIterator
>
1055 _M_range_insert(iterator __pos
, _InputIterator __first
,
1056 _InputIterator __last
, std::input_iterator_tag
);
1058 // Called by the second insert_dispatch above
1059 template<typename _ForwardIterator
>
1061 _M_range_insert(iterator __pos
, _ForwardIterator __first
,
1062 _ForwardIterator __last
, std::forward_iterator_tag
);
1064 // Called by insert(p,n,x), and the range insert when it turns out to be
1067 _M_fill_insert(iterator __pos
, size_type __n
, const value_type
& __x
);
1069 // Called by insert(p,x)
1070 #ifndef __GXX_EXPERIMENTAL_CXX0X__
1072 _M_insert_aux(iterator __position
, const value_type
& __x
);
1074 template<typename
... _Args
>
1076 _M_insert_aux(iterator __position
, _Args
&&... __args
);
1079 // Called by the latter.
1081 _M_check_len(size_type __n
, const char* __s
) const
1083 if (max_size() - size() < __n
)
1084 __throw_length_error(__N(__s
));
1086 const size_type __len
= size() + std::max(size(), __n
);
1087 return (__len
< size() || __len
> max_size()) ? max_size() : __len
;
1090 // Internal erase functions follow.
1092 // Called by erase(q1,q2), clear(), resize(), _M_fill_assign,
1095 _M_erase_at_end(pointer __pos
)
1097 std::_Destroy(__pos
, this->_M_impl
._M_finish
, _M_get_Tp_allocator());
1098 this->_M_impl
._M_finish
= __pos
;
1104 * @brief Vector equality comparison.
1105 * @param x A %vector.
1106 * @param y A %vector of the same type as @a x.
1107 * @return True iff the size and elements of the vectors are equal.
1109 * This is an equivalence relation. It is linear in the size of the
1110 * vectors. Vectors are considered equivalent if their sizes are equal,
1111 * and if corresponding elements compare equal.
1113 template<typename _Tp
, typename _Alloc
>
1115 operator==(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
1116 { return (__x
.size() == __y
.size()
1117 && std::equal(__x
.begin(), __x
.end(), __y
.begin())); }
1120 * @brief Vector ordering relation.
1121 * @param x A %vector.
1122 * @param y A %vector of the same type as @a x.
1123 * @return True iff @a x is lexicographically less than @a y.
1125 * This is a total ordering relation. It is linear in the size of the
1126 * vectors. The elements must be comparable with @c <.
1128 * See std::lexicographical_compare() for how the determination is made.
1130 template<typename _Tp
, typename _Alloc
>
1132 operator<(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
1133 { return std::lexicographical_compare(__x
.begin(), __x
.end(),
1134 __y
.begin(), __y
.end()); }
1136 /// Based on operator==
1137 template<typename _Tp
, typename _Alloc
>
1139 operator!=(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
1140 { return !(__x
== __y
); }
1142 /// Based on operator<
1143 template<typename _Tp
, typename _Alloc
>
1145 operator>(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
1146 { return __y
< __x
; }
1148 /// Based on operator<
1149 template<typename _Tp
, typename _Alloc
>
1151 operator<=(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
1152 { return !(__y
< __x
); }
1154 /// Based on operator<
1155 template<typename _Tp
, typename _Alloc
>
1157 operator>=(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
1158 { return !(__x
< __y
); }
1160 /// See std::vector::swap().
1161 template<typename _Tp
, typename _Alloc
>
1163 swap(vector
<_Tp
, _Alloc
>& __x
, vector
<_Tp
, _Alloc
>& __y
)
1166 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1167 template<typename _Tp
, typename _Alloc
>
1169 swap(vector
<_Tp
, _Alloc
>&& __x
, vector
<_Tp
, _Alloc
>& __y
)
1172 template<typename _Tp
, typename _Alloc
>
1174 swap(vector
<_Tp
, _Alloc
>& __x
, vector
<_Tp
, _Alloc
>&& __y
)
1178 _GLIBCXX_END_NESTED_NAMESPACE
1180 #endif /* _STL_VECTOR_H */