1 // Vector implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
33 * Hewlett-Packard Company
35 * Permission to use, copy, modify, distribute and sell this software
36 * and its documentation for any purpose is hereby granted without fee,
37 * provided that the above copyright notice appear in all copies and
38 * that both that copyright notice and this permission notice appear
39 * in supporting documentation. Hewlett-Packard Company makes no
40 * representations about the suitability of this software for any
41 * purpose. It is provided "as is" without express or implied warranty.
45 * Silicon Graphics Computer Systems, Inc.
47 * Permission to use, copy, modify, distribute and sell this software
48 * and its documentation for any purpose is hereby granted without fee,
49 * provided that the above copyright notice appear in all copies and
50 * that both that copyright notice and this permission notice appear
51 * in supporting documentation. Silicon Graphics makes no
52 * representations about the suitability of this software for any
53 * purpose. It is provided "as is" without express or implied warranty.
56 /** @file stl_vector.h
57 * This is an internal header file, included by other library headers.
58 * You should not attempt to use it directly.
61 #ifndef __GLIBCPP_INTERNAL_VECTOR_H
62 #define __GLIBCPP_INTERNAL_VECTOR_H
64 #include <bits/stl_iterator_base_funcs.h>
65 #include <bits/functexcept.h>
66 #include <bits/concept_check.h>
68 // Since this entire file is within namespace std, there's no reason to
69 // waste two spaces along the left column. Thus the leading indentation is
70 // slightly violated from here on.
74 /// @if maint Primary default version. @endif
77 * See bits/stl_deque.h's _Deque_alloc_base for an explanation.
80 template <class _Tp
, class _Allocator
, bool _IsStatic
>
81 class _Vector_alloc_base
84 typedef typename _Alloc_traits
<_Tp
, _Allocator
>::allocator_type
88 get_allocator() const { return _M_data_allocator
; }
90 _Vector_alloc_base(const allocator_type
& __a
)
91 : _M_data_allocator(__a
), _M_start(0), _M_finish(0), _M_end_of_storage(0)
95 allocator_type _M_data_allocator
;
98 _Tp
* _M_end_of_storage
;
101 _M_allocate(size_t __n
) { return _M_data_allocator
.allocate(__n
); }
104 _M_deallocate(_Tp
* __p
, size_t __n
)
105 { if (__p
) _M_data_allocator
.deallocate(__p
, __n
); }
108 /// @if maint Specialization for instanceless allocators. @endif
109 template <class _Tp
, class _Allocator
>
110 class _Vector_alloc_base
<_Tp
, _Allocator
, true>
113 typedef typename _Alloc_traits
<_Tp
, _Allocator
>::allocator_type
117 get_allocator() const { return allocator_type(); }
119 _Vector_alloc_base(const allocator_type
&)
120 : _M_start(0), _M_finish(0), _M_end_of_storage(0)
126 _Tp
* _M_end_of_storage
;
128 typedef typename _Alloc_traits
<_Tp
, _Allocator
>::_Alloc_type _Alloc_type
;
131 _M_allocate(size_t __n
) { return _Alloc_type::allocate(__n
); }
134 _M_deallocate(_Tp
* __p
, size_t __n
) { _Alloc_type::deallocate(__p
, __n
);}
140 * See bits/stl_deque.h's _Deque_base for an explanation.
143 template <class _Tp
, class _Alloc
>
145 : public _Vector_alloc_base
<_Tp
, _Alloc
,
146 _Alloc_traits
<_Tp
, _Alloc
>::_S_instanceless
>
149 typedef _Vector_alloc_base
<_Tp
, _Alloc
,
150 _Alloc_traits
<_Tp
, _Alloc
>::_S_instanceless
>
152 typedef typename
_Base::allocator_type allocator_type
;
154 _Vector_base(const allocator_type
& __a
)
156 _Vector_base(size_t __n
, const allocator_type
& __a
)
159 _M_start
= _M_allocate(__n
);
160 _M_finish
= _M_start
;
161 _M_end_of_storage
= _M_start
+ __n
;
164 ~_Vector_base() { _M_deallocate(_M_start
, _M_end_of_storage
- _M_start
); }
169 * @brief A standard container which offers fixed time access to individual
170 * elements in any order.
172 * @ingroup Containers
175 * Meets the requirements of a <a href="tables.html#65">container</a>, a
176 * <a href="tables.html#66">reversible container</a>, and a
177 * <a href="tables.html#67">sequence</a>, including the
178 * <a href="tables.html#68">optional sequence requirements</a> with the
179 * %exception of @c push_front and @c pop_front.
181 * In some terminology a %vector can be described as a dynamic C-style array,
182 * it offers fast and efficient access to individual elements in any order
183 * and saves the user from worrying about memory and size allocation.
184 * Subscripting ( @c [] ) access is also provided as with C-style arrays.
186 template <class _Tp
, class _Alloc
= allocator
<_Tp
> >
187 class vector
: protected _Vector_base
<_Tp
, _Alloc
>
189 // concept requirements
190 __glibcpp_class_requires(_Tp
, _SGIAssignableConcept
)
192 typedef _Vector_base
<_Tp
, _Alloc
> _Base
;
193 typedef vector
<_Tp
, _Alloc
> vector_type
;
196 typedef _Tp value_type
;
197 typedef value_type
* pointer
;
198 typedef const value_type
* const_pointer
;
199 typedef __gnu_cxx::__normal_iterator
<pointer
, vector_type
> iterator
;
200 typedef __gnu_cxx::__normal_iterator
<const_pointer
, vector_type
>
202 typedef reverse_iterator
<const_iterator
> const_reverse_iterator
;
203 typedef reverse_iterator
<iterator
> reverse_iterator
;
204 typedef value_type
& reference
;
205 typedef const value_type
& const_reference
;
206 typedef size_t size_type
;
207 typedef ptrdiff_t difference_type
;
208 typedef typename
_Base::allocator_type allocator_type
;
212 * These two functions and three data members are all from the top-most
213 * base class, which varies depending on the type of %allocator. They
214 * should be pretty self-explanatory, as %vector uses a simple contiguous
218 using _Base::_M_allocate
;
219 using _Base::_M_deallocate
;
220 using _Base::_M_start
;
221 using _Base::_M_finish
;
222 using _Base::_M_end_of_storage
;
225 // [23.2.4.1] construct/copy/destroy
226 // (assign() and get_allocator() are also listed in this section)
228 * @brief Default constructor creates no elements.
231 vector(const allocator_type
& __a
= allocator_type())
235 * @brief Create a %vector with copies of an exemplar element.
236 * @param n The number of elements to initially create.
237 * @param value An element to copy.
239 * This constructor fills the %vector with @a n copies of @a value.
241 vector(size_type __n
, const value_type
& __value
,
242 const allocator_type
& __a
= allocator_type())
244 { _M_finish
= uninitialized_fill_n(_M_start
, __n
, __value
); }
247 * @brief Create a %vector with default elements.
248 * @param n The number of elements to initially create.
250 * This constructor fills the %vector with @a n copies of a
251 * default-constructed element.
254 vector(size_type __n
)
255 : _Base(__n
, allocator_type())
256 { _M_finish
= uninitialized_fill_n(_M_start
, __n
, _Tp()); }
259 * @brief %Vector copy constructor.
260 * @param x A %vector of identical element and allocator types.
262 * The newly-created %vector uses a copy of the allocation object used
263 * by @a x. All the elements of @a x are copied, but any extra memory in
264 * @a x (for fast expansion) will not be copied.
266 vector(const vector
& __x
)
267 : _Base(__x
.size(), __x
.get_allocator())
268 { _M_finish
= uninitialized_copy(__x
.begin(), __x
.end(), _M_start
); }
271 * @brief Builds a %vector from a range.
272 * @param first An input iterator.
273 * @param last An input iterator.
275 * Creats a %vector consisting of copies of the elements from [first,last).
277 * If the iterators are forward, bidirectional, or random-access, then
278 * this will call the elements' copy constructor N times (where N is
279 * distance(first,last)) and do no memory reallocation. But if only
280 * input iterators are used, then this will do at most 2N calls to the
281 * copy constructor, and logN memory reallocations.
283 template <class _InputIterator
>
284 vector(_InputIterator __first
, _InputIterator __last
,
285 const allocator_type
& __a
= allocator_type())
288 // Check whether it's an integral type. If so, it's not an iterator.
289 typedef typename _Is_integer
<_InputIterator
>::_Integral _Integral
;
290 _M_initialize_dispatch(__first
, __last
, _Integral());
294 * The dtor only erases the elements, and note that if the elements
295 * themselves are pointers, the pointed-to memory is not touched in any
296 * way. Managing the pointer is the user's responsibilty.
298 ~vector() { _Destroy(_M_start
, _M_finish
); }
301 * @brief %Vector assignment operator.
302 * @param x A %vector of identical element and allocator types.
304 * All the elements of @a x are copied, but any extra memory in @a x (for
305 * fast expansion) will not be copied. Unlike the copy constructor, the
306 * allocator object is not copied.
309 operator=(const vector
& __x
);
312 * @brief Assigns a given value to a %vector.
313 * @param n Number of elements to be assigned.
314 * @param val Value to be assigned.
316 * This function fills a %vector with @a n copies of the given value.
317 * Note that the assignment completely changes the %vector and that the
318 * resulting %vector's size is the same as the number of elements assigned.
319 * Old data may be lost.
322 assign(size_type __n
, const value_type
& __val
) { _M_fill_assign(__n
, __val
); }
325 * @brief Assigns a range to a %vector.
326 * @param first An input iterator.
327 * @param last An input iterator.
329 * This function fills a %vector with copies of the elements in the
330 * range [first,last).
332 * Note that the assignment completely changes the %vector and that the
333 * resulting %vector's size is the same as the number of elements assigned.
334 * Old data may be lost.
336 template<class _InputIterator
>
338 assign(_InputIterator __first
, _InputIterator __last
)
340 // Check whether it's an integral type. If so, it's not an iterator.
341 typedef typename _Is_integer
<_InputIterator
>::_Integral _Integral
;
342 _M_assign_dispatch(__first
, __last
, _Integral());
345 /// Get a copy of the memory allocation object.
347 get_allocator() const { return _Base::get_allocator(); }
351 * Returns a read/write iterator that points to the first element in the
352 * %vector. Iteration is done in ordinary element order.
355 begin() { return iterator (_M_start
); }
358 * Returns a read-only (constant) iterator that points to the first element
359 * in the %vector. Iteration is done in ordinary element order.
362 begin() const { return const_iterator (_M_start
); }
365 * Returns a read/write iterator that points one past the last element in
366 * the %vector. Iteration is done in ordinary element order.
369 end() { return iterator (_M_finish
); }
372 * Returns a read-only (constant) iterator that points one past the last
373 * element in the %vector. Iteration is done in ordinary element order.
376 end() const { return const_iterator (_M_finish
); }
379 * Returns a read/write reverse iterator that points to the last element in
380 * the %vector. Iteration is done in reverse element order.
383 rbegin() { return reverse_iterator(end()); }
386 * Returns a read-only (constant) reverse iterator that points to the last
387 * element in the %vector. Iteration is done in reverse element order.
389 const_reverse_iterator
390 rbegin() const { return const_reverse_iterator(end()); }
393 * Returns a read/write reverse iterator that points to one before the
394 * first element in the %vector. Iteration is done in reverse element
398 rend() { return reverse_iterator(begin()); }
401 * Returns a read-only (constant) reverse iterator that points to one
402 * before the first element in the %vector. Iteration is done in reverse
405 const_reverse_iterator
406 rend() const { return const_reverse_iterator(begin()); }
408 // [23.2.4.2] capacity
409 /** Returns the number of elements in the %vector. */
411 size() const { return size_type(end() - begin()); }
413 /** Returns the size() of the largest possible %vector. */
415 max_size() const { return size_type(-1) / sizeof(value_type
); }
418 * @brief Resizes the %vector to the specified number of elements.
419 * @param new_size Number of elements the %vector should contain.
420 * @param x Data with which new elements should be populated.
422 * This function will %resize the %vector to the specified number of
423 * elements. If the number is smaller than the %vector's current size the
424 * %vector is truncated, otherwise the %vector is extended and new elements
425 * are populated with given data.
428 resize(size_type __new_size
, const value_type
& __x
)
430 if (__new_size
< size())
431 erase(begin() + __new_size
, end());
433 insert(end(), __new_size
- size(), __x
);
437 * @brief Resizes the %vector to the specified number of elements.
438 * @param new_size Number of elements the %vector should contain.
440 * This function will resize the %vector to the specified number of
441 * elements. If the number is smaller than the %vector's current size the
442 * %vector is truncated, otherwise the %vector is extended and new elements
443 * are default-constructed.
446 resize(size_type __new_size
) { resize(__new_size
, value_type()); }
449 * Returns the total number of elements that the %vector can hold before
450 * needing to allocate more memory.
454 { return size_type(const_iterator(_M_end_of_storage
) - begin()); }
457 * Returns true if the %vector is empty. (Thus begin() would equal end().)
460 empty() const { return begin() == end(); }
463 * @brief Attempt to preallocate enough memory for specified number of
465 * @param n Number of elements required.
466 * @throw std::length_error If @a n exceeds @c max_size().
468 * This function attempts to reserve enough memory for the %vector to hold
469 * the specified number of elements. If the number requested is more than
470 * max_size(), length_error is thrown.
472 * The advantage of this function is that if optimal code is a necessity
473 * and the user can determine the number of elements that will be required,
474 * the user can reserve the memory in %advance, and thus prevent a possible
475 * reallocation of memory and copying of %vector data.
478 reserve(size_type __n
) // FIXME should be out of class
480 if (capacity() < __n
)
482 const size_type __old_size
= size();
483 pointer __tmp
= _M_allocate_and_copy(__n
, _M_start
, _M_finish
);
484 _Destroy(_M_start
, _M_finish
);
485 _M_deallocate(_M_start
, _M_end_of_storage
- _M_start
);
487 _M_finish
= __tmp
+ __old_size
;
488 _M_end_of_storage
= _M_start
+ __n
;
494 * @brief Subscript access to the data contained in the %vector.
495 * @param n The index of the element for which data should be accessed.
496 * @return Read/write reference to data.
498 * This operator allows for easy, array-style, data access.
499 * Note that data access with this operator is unchecked and out_of_range
500 * lookups are not defined. (For checked lookups see at().)
503 operator[](size_type __n
) { return *(begin() + __n
); }
504 // XXX do we need to convert to normal_iterator first?
507 * @brief Subscript access to the data contained in the %vector.
508 * @param n The index of the element for which data should be accessed.
509 * @return Read-only (constant) reference to data.
511 * This operator allows for easy, array-style, data access.
512 * Note that data access with this operator is unchecked and out_of_range
513 * lookups are not defined. (For checked lookups see at().)
516 operator[](size_type __n
) const { return *(begin() + __n
); }
519 /// @if maint Safety check used only from at(). @endif
521 _M_range_check(size_type __n
) const
523 if (__n
>= this->size())
524 __throw_out_of_range("vector [] access out of range");
529 * @brief Provides access to the data contained in the %vector.
530 * @param n The index of the element for which data should be accessed.
531 * @return Read/write reference to data.
532 * @throw std::out_of_range If @a n is an invalid index.
534 * This function provides for safer data access. The parameter is first
535 * checked that it is in the range of the vector. The function throws
536 * out_of_range if the check fails.
539 at(size_type __n
) { _M_range_check(__n
); return (*this)[__n
]; }
542 * @brief Provides access to the data contained in the %vector.
543 * @param n The index of the element for which data should be accessed.
544 * @return Read-only (constant) reference to data.
545 * @throw std::out_of_range If @a n is an invalid index.
547 * This function provides for safer data access. The parameter is first
548 * checked that it is in the range of the vector. The function throws
549 * out_of_range if the check fails.
552 at(size_type __n
) const { _M_range_check(__n
); return (*this)[__n
]; }
555 * Returns a read/write reference to the data at the first element of the
559 front() { return *begin(); }
560 // XXX do we need to convert to normal_iterator first?
563 * Returns a read-only (constant) reference to the data at the first
564 * element of the %vector.
567 front() const { return *begin(); }
570 * Returns a read/write reference to the data at the last element of the
574 back() { return *(end() - 1); }
577 * Returns a read-only (constant) reference to the data at the last
578 * element of the %vector.
581 back() const { return *(end() - 1); }
583 // [23.2.4.3] modifiers
585 * @brief Add data to the end of the %vector.
586 * @param x Data to be added.
588 * This is a typical stack operation. The function creates an element at
589 * the end of the %vector and assigns the given data to it.
590 * Due to the nature of a %vector this operation can be done in constant
591 * time if the %vector has preallocated space available.
594 push_back(const value_type
& __x
)
596 if (_M_finish
!= _M_end_of_storage
) {
597 _Construct(_M_finish
, __x
);
601 _M_insert_aux(end(), __x
);
605 * @brief Removes last element.
607 * This is a typical stack operation. It shrinks the %vector by one.
609 * Note that no data is returned, and if the last element's data is
610 * needed, it should be retrieved before pop_back() is called.
620 * @brief Inserts given value into %vector before specified iterator.
621 * @param position An iterator into the %vector.
622 * @param x Data to be inserted.
623 * @return An iterator that points to the inserted data.
625 * This function will insert a copy of the given value before the specified
627 * Note that this kind of operation could be expensive for a %vector and if
628 * it is frequently used the user should consider using std::list.
631 insert(iterator __position
, const value_type
& __x
)
633 size_type __n
= __position
- begin();
634 if (_M_finish
!= _M_end_of_storage
&& __position
== end())
636 _Construct(_M_finish
, __x
);
640 _M_insert_aux(__position
, __x
);
641 return begin() + __n
;
644 #ifdef _GLIBCPP_DEPRECATED
646 * @brief Inserts an element into the %vector.
647 * @param position An iterator into the %vector.
648 * @return An iterator that points to the inserted element.
650 * This function will insert a default-constructed element before the
651 * specified location. You should consider using
652 * insert(position,value_type()) instead.
653 * Note that this kind of operation could be expensive for a vector and if
654 * it is frequently used the user should consider using std::list.
656 * @note This was deprecated in 3.2 and will be removed in 3.3. You must
657 * define @c _GLIBCPP_DEPRECATED to make this visible in 3.2; see
661 insert(iterator __position
)
662 { return insert(__position
, value_type()); }
666 * @brief Inserts a number of copies of given data into the %vector.
667 * @param position An iterator into the %vector.
668 * @param n Number of elements to be inserted.
669 * @param x Data to be inserted.
671 * This function will insert a specified number of copies of the given data
672 * before the location specified by @a position.
674 * Note that this kind of operation could be expensive for a %vector and if
675 * it is frequently used the user should consider using std::list.
678 insert (iterator __pos
, size_type __n
, const value_type
& __x
)
679 { _M_fill_insert(__pos
, __n
, __x
); }
682 * @brief Inserts a range into the %vector.
683 * @param pos An iterator into the %vector.
684 * @param first An input iterator.
685 * @param last An input iterator.
687 * This function will insert copies of the data in the range [first,last)
688 * into the %vector before the location specified by @a pos.
690 * Note that this kind of operation could be expensive for a %vector and if
691 * it is frequently used the user should consider using std::list.
693 template<class _InputIterator
>
695 insert(iterator __pos
, _InputIterator __first
, _InputIterator __last
)
697 // Check whether it's an integral type. If so, it's not an iterator.
698 typedef typename _Is_integer
<_InputIterator
>::_Integral _Integral
;
699 _M_insert_dispatch(__pos
, __first
, __last
, _Integral());
703 * @brief Remove element at given position.
704 * @param position Iterator pointing to element to be erased.
705 * @return An iterator pointing to the next element (or end()).
707 * This function will erase the element at the given position and thus
708 * shorten the %vector by one.
710 * Note This operation could be expensive and if it is frequently used the
711 * user should consider using std::list. The user is also cautioned that
712 * this function only erases the element, and that if the element is itself
713 * a pointer, the pointed-to memory is not touched in any way. Managing
714 * the pointer is the user's responsibilty.
717 erase(iterator __position
)
719 if (__position
+ 1 != end())
720 copy(__position
+ 1, end(), __position
);
727 * @brief Remove a range of elements.
728 * @param first Iterator pointing to the first element to be erased.
729 * @param last Iterator pointing to one past the last element to be erased.
730 * @return An iterator pointing to the element pointed to by @a last
731 * prior to erasing (or end()).
733 * This function will erase the elements in the range [first,last) and
734 * shorten the %vector accordingly.
736 * Note This operation could be expensive and if it is frequently used the
737 * user should consider using std::list. The user is also cautioned that
738 * this function only erases the elements, and that if the elements
739 * themselves are pointers, the pointed-to memory is not touched in any
740 * way. Managing the pointer is the user's responsibilty.
743 erase(iterator __first
, iterator __last
)
745 iterator
__i(copy(__last
, end(), __first
));
746 _Destroy(__i
, end());
747 _M_finish
= _M_finish
- (__last
- __first
);
752 * @brief Swaps data with another %vector.
753 * @param x A %vector of the same element and allocator types.
755 * This exchanges the elements between two vectors in constant time.
756 * (Three pointers, so it should be quite fast.)
757 * Note that the global std::swap() function is specialized such that
758 * std::swap(v1,v2) will feed to this function.
763 std::swap(_M_start
, __x
._M_start
);
764 std::swap(_M_finish
, __x
._M_finish
);
765 std::swap(_M_end_of_storage
, __x
._M_end_of_storage
);
769 * Erases all the elements. Note that this function only erases the
770 * elements, and that if the elements themselves are pointers, the
771 * pointed-to memory is not touched in any way. Managing the pointer is
772 * the user's responsibilty.
775 clear() { erase(begin(), end()); }
780 * Memory expansion handler. Uses the member allocation function to
781 * obtain @a n bytes of memory, and then copies [first,last) into it.
784 template <class _ForwardIterator
>
786 _M_allocate_and_copy(size_type __n
,
787 _ForwardIterator __first
, _ForwardIterator __last
)
789 pointer __result
= _M_allocate(__n
);
792 uninitialized_copy(__first
, __last
, __result
);
797 _M_deallocate(__result
, __n
);
798 __throw_exception_again
;
803 // Internal constructor functions follow.
805 // called by the range constructor to implement [23.1.1]/9
806 template<class _Integer
>
808 _M_initialize_dispatch(_Integer __n
, _Integer __value
, __true_type
)
810 _M_start
= _M_allocate(__n
);
811 _M_end_of_storage
= _M_start
+ __n
;
812 _M_finish
= uninitialized_fill_n(_M_start
, __n
, __value
);
815 // called by the range constructor to implement [23.1.1]/9
816 template<class _InputIter
>
818 _M_initialize_dispatch(_InputIter __first
, _InputIter __last
, __false_type
)
820 typedef typename iterator_traits
<_InputIter
>::iterator_category
822 _M_range_initialize(__first
, __last
, _IterCategory());
825 // called by the second initialize_dispatch above
826 template <class _InputIterator
>
828 _M_range_initialize(_InputIterator __first
,
829 _InputIterator __last
, input_iterator_tag
)
831 for ( ; __first
!= __last
; ++__first
)
835 // called by the second initialize_dispatch above
836 template <class _ForwardIterator
>
837 void _M_range_initialize(_ForwardIterator __first
,
838 _ForwardIterator __last
, forward_iterator_tag
)
840 size_type __n
= distance(__first
, __last
);
841 _M_start
= _M_allocate(__n
);
842 _M_end_of_storage
= _M_start
+ __n
;
843 _M_finish
= uninitialized_copy(__first
, __last
, _M_start
);
847 // Internal assign functions follow. The *_aux functions do the actual
848 // assignment work for the range versions.
850 // called by the range assign to implement [23.1.1]/9
851 template<class _Integer
>
853 _M_assign_dispatch(_Integer __n
, _Integer __val
, __true_type
)
855 _M_fill_assign(static_cast<size_type
>(__n
),
856 static_cast<value_type
>(__val
));
859 // called by the range assign to implement [23.1.1]/9
860 template<class _InputIter
>
862 _M_assign_dispatch(_InputIter __first
, _InputIter __last
, __false_type
)
864 typedef typename iterator_traits
<_InputIter
>::iterator_category
866 _M_assign_aux(__first
, __last
, _IterCategory());
869 // called by the second assign_dispatch above
870 template <class _InputIterator
>
872 _M_assign_aux(_InputIterator __first
, _InputIterator __last
,
875 // called by the second assign_dispatch above
876 template <class _ForwardIterator
>
878 _M_assign_aux(_ForwardIterator __first
, _ForwardIterator __last
,
879 forward_iterator_tag
);
881 // Called by assign(n,t), and the range assign when it turns out to be the
884 _M_fill_assign(size_type __n
, const value_type
& __val
);
887 // Internal insert functions follow.
889 // called by the range insert to implement [23.1.1]/9
890 template<class _Integer
>
892 _M_insert_dispatch(iterator __pos
, _Integer __n
, _Integer __val
,
895 _M_fill_insert(__pos
, static_cast<size_type
>(__n
),
896 static_cast<value_type
>(__val
));
899 // called by the range insert to implement [23.1.1]/9
900 template<class _InputIterator
>
902 _M_insert_dispatch(iterator __pos
, _InputIterator __first
,
903 _InputIterator __last
, __false_type
)
905 typedef typename iterator_traits
<_InputIterator
>::iterator_category
907 _M_range_insert(__pos
, __first
, __last
, _IterCategory());
910 // called by the second insert_dispatch above
911 template <class _InputIterator
>
913 _M_range_insert(iterator __pos
,
914 _InputIterator __first
, _InputIterator __last
,
917 // called by the second insert_dispatch above
918 template <class _ForwardIterator
>
920 _M_range_insert(iterator __pos
,
921 _ForwardIterator __first
, _ForwardIterator __last
,
922 forward_iterator_tag
);
924 // Called by insert(p,n,x), and the range insert when it turns out to be
927 _M_fill_insert (iterator __pos
, size_type __n
, const value_type
& __x
);
929 // called by insert(p,x)
931 _M_insert_aux(iterator __position
, const value_type
& __x
);
933 #ifdef _GLIBCPP_DEPRECATED
934 // unused now (same situation as in deque)
935 void _M_insert_aux(iterator __position
);
941 * @brief Vector equality comparison.
942 * @param x A %vector.
943 * @param y A %vector of the same type as @a x.
944 * @return True iff the size and elements of the vectors are equal.
946 * This is an equivalence relation. It is linear in the size of the
947 * vectors. Vectors are considered equivalent if their sizes are equal,
948 * and if corresponding elements compare equal.
950 template <class _Tp
, class _Alloc
>
952 operator==(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
954 return __x
.size() == __y
.size() &&
955 equal(__x
.begin(), __x
.end(), __y
.begin());
959 * @brief Vector ordering relation.
960 * @param x A %vector.
961 * @param y A %vector of the same type as @a x.
962 * @return True iff @a x is lexographically less than @a y.
964 * This is a total ordering relation. It is linear in the size of the
965 * vectors. The elements must be comparable with @c <.
967 * See std::lexographical_compare() for how the determination is made.
969 template <class _Tp
, class _Alloc
>
971 operator<(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
973 return lexicographical_compare(__x
.begin(), __x
.end(),
974 __y
.begin(), __y
.end());
977 /// Based on operator==
978 template <class _Tp
, class _Alloc
>
980 operator!=(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
) {
981 return !(__x
== __y
);
984 /// Based on operator<
985 template <class _Tp
, class _Alloc
>
987 operator>(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
) {
991 /// Based on operator<
992 template <class _Tp
, class _Alloc
>
994 operator<=(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
) {
998 /// Based on operator<
999 template <class _Tp
, class _Alloc
>
1001 operator>=(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
) {
1002 return !(__x
< __y
);
1005 /// See std::vector::swap().
1006 template <class _Tp
, class _Alloc
>
1007 inline void swap(vector
<_Tp
, _Alloc
>& __x
, vector
<_Tp
, _Alloc
>& __y
)
1013 template <class _Tp
, class _Alloc
>
1015 vector
<_Tp
,_Alloc
>::operator=(const vector
<_Tp
,_Alloc
>& __x
)
1018 const size_type __xlen
= __x
.size();
1019 if (__xlen
> capacity()) {
1020 pointer __tmp
= _M_allocate_and_copy(__xlen
, __x
.begin(), __x
.end());
1021 _Destroy(_M_start
, _M_finish
);
1022 _M_deallocate(_M_start
, _M_end_of_storage
- _M_start
);
1024 _M_end_of_storage
= _M_start
+ __xlen
;
1026 else if (size() >= __xlen
) {
1027 iterator
__i(copy(__x
.begin(), __x
.end(), begin()));
1028 _Destroy(__i
, end());
1031 copy(__x
.begin(), __x
.begin() + size(), _M_start
);
1032 uninitialized_copy(__x
.begin() + size(), __x
.end(), _M_finish
);
1034 _M_finish
= _M_start
+ __xlen
;
1039 template <class _Tp
, class _Alloc
>
1041 vector
<_Tp
, _Alloc
>::_M_fill_assign(size_t __n
, const value_type
& __val
)
1043 if (__n
> capacity()) {
1044 vector
__tmp(__n
, __val
, get_allocator());
1047 else if (__n
> size()) {
1048 fill(begin(), end(), __val
);
1049 _M_finish
= uninitialized_fill_n(_M_finish
, __n
- size(), __val
);
1052 erase(fill_n(begin(), __n
, __val
), end());
1055 template <class _Tp
, class _Alloc
> template <class _InputIter
>
1056 void vector
<_Tp
, _Alloc
>::_M_assign_aux(_InputIter __first
, _InputIter __last
,
1057 input_iterator_tag
) {
1058 iterator
__cur(begin());
1059 for ( ; __first
!= __last
&& __cur
!= end(); ++__cur
, ++__first
)
1061 if (__first
== __last
)
1062 erase(__cur
, end());
1064 insert(end(), __first
, __last
);
1067 template <class _Tp
, class _Alloc
> template <class _ForwardIter
>
1069 vector
<_Tp
, _Alloc
>::_M_assign_aux(_ForwardIter __first
, _ForwardIter __last
,
1070 forward_iterator_tag
) {
1071 size_type __len
= distance(__first
, __last
);
1073 if (__len
> capacity()) {
1074 pointer
__tmp(_M_allocate_and_copy(__len
, __first
, __last
));
1075 _Destroy(_M_start
, _M_finish
);
1076 _M_deallocate(_M_start
, _M_end_of_storage
- _M_start
);
1078 _M_end_of_storage
= _M_finish
= _M_start
+ __len
;
1080 else if (size() >= __len
) {
1081 iterator
__new_finish(copy(__first
, __last
, _M_start
));
1082 _Destroy(__new_finish
, end());
1083 _M_finish
= __new_finish
.base();
1086 _ForwardIter __mid
= __first
;
1087 advance(__mid
, size());
1088 copy(__first
, __mid
, _M_start
);
1089 _M_finish
= uninitialized_copy(__mid
, __last
, _M_finish
);
1093 template <class _Tp
, class _Alloc
>
1095 vector
<_Tp
, _Alloc
>::_M_insert_aux(iterator __position
, const _Tp
& __x
)
1097 if (_M_finish
!= _M_end_of_storage
) {
1098 _Construct(_M_finish
, *(_M_finish
- 1));
1101 copy_backward(__position
, iterator(_M_finish
- 2), iterator(_M_finish
- 1));
1102 *__position
= __x_copy
;
1105 const size_type __old_size
= size();
1106 const size_type __len
= __old_size
!= 0 ? 2 * __old_size
: 1;
1107 iterator
__new_start(_M_allocate(__len
));
1108 iterator
__new_finish(__new_start
);
1110 __new_finish
= uninitialized_copy(iterator(_M_start
), __position
,
1112 _Construct(__new_finish
.base(), __x
);
1114 __new_finish
= uninitialized_copy(__position
, iterator(_M_finish
),
1119 _Destroy(__new_start
,__new_finish
);
1120 _M_deallocate(__new_start
.base(),__len
);
1121 __throw_exception_again
;
1123 _Destroy(begin(), end());
1124 _M_deallocate(_M_start
, _M_end_of_storage
- _M_start
);
1125 _M_start
= __new_start
.base();
1126 _M_finish
= __new_finish
.base();
1127 _M_end_of_storage
= __new_start
.base() + __len
;
1131 #ifdef _GLIBCPP_DEPRECATED
1132 template <class _Tp
, class _Alloc
>
1134 vector
<_Tp
, _Alloc
>::_M_insert_aux(iterator __position
)
1136 if (_M_finish
!= _M_end_of_storage
) {
1137 _Construct(_M_finish
, *(_M_finish
- 1));
1139 copy_backward(__position
, iterator(_M_finish
- 2),
1140 iterator(_M_finish
- 1));
1141 *__position
= _Tp();
1144 const size_type __old_size
= size();
1145 const size_type __len
= __old_size
!= 0 ? 2 * __old_size
: 1;
1146 pointer __new_start
= _M_allocate(__len
);
1147 pointer __new_finish
= __new_start
;
1149 __new_finish
= uninitialized_copy(iterator(_M_start
), __position
,
1151 _Construct(__new_finish
);
1153 __new_finish
= uninitialized_copy(__position
, iterator(_M_finish
),
1158 _Destroy(__new_start
,__new_finish
);
1159 _M_deallocate(__new_start
,__len
);
1160 __throw_exception_again
;
1162 _Destroy(begin(), end());
1163 _M_deallocate(_M_start
, _M_end_of_storage
- _M_start
);
1164 _M_start
= __new_start
;
1165 _M_finish
= __new_finish
;
1166 _M_end_of_storage
= __new_start
+ __len
;
1171 template <class _Tp
, class _Alloc
>
1172 void vector
<_Tp
, _Alloc
>::_M_fill_insert(iterator __position
, size_type __n
,
1176 if (size_type(_M_end_of_storage
- _M_finish
) >= __n
) {
1178 const size_type __elems_after
= end() - __position
;
1179 iterator
__old_finish(_M_finish
);
1180 if (__elems_after
> __n
) {
1181 uninitialized_copy(_M_finish
- __n
, _M_finish
, _M_finish
);
1183 copy_backward(__position
, __old_finish
- __n
, __old_finish
);
1184 fill(__position
, __position
+ __n
, __x_copy
);
1187 uninitialized_fill_n(_M_finish
, __n
- __elems_after
, __x_copy
);
1188 _M_finish
+= __n
- __elems_after
;
1189 uninitialized_copy(__position
, __old_finish
, _M_finish
);
1190 _M_finish
+= __elems_after
;
1191 fill(__position
, __old_finish
, __x_copy
);
1195 const size_type __old_size
= size();
1196 const size_type __len
= __old_size
+ max(__old_size
, __n
);
1197 iterator
__new_start(_M_allocate(__len
));
1198 iterator
__new_finish(__new_start
);
1200 __new_finish
= uninitialized_copy(begin(), __position
, __new_start
);
1201 __new_finish
= uninitialized_fill_n(__new_finish
, __n
, __x
);
1203 = uninitialized_copy(__position
, end(), __new_finish
);
1207 _Destroy(__new_start
,__new_finish
);
1208 _M_deallocate(__new_start
.base(),__len
);
1209 __throw_exception_again
;
1211 _Destroy(_M_start
, _M_finish
);
1212 _M_deallocate(_M_start
, _M_end_of_storage
- _M_start
);
1213 _M_start
= __new_start
.base();
1214 _M_finish
= __new_finish
.base();
1215 _M_end_of_storage
= __new_start
.base() + __len
;
1220 template <class _Tp
, class _Alloc
> template <class _InputIterator
>
1222 vector
<_Tp
, _Alloc
>::_M_range_insert(iterator __pos
,
1223 _InputIterator __first
,
1224 _InputIterator __last
,
1227 for ( ; __first
!= __last
; ++__first
) {
1228 __pos
= insert(__pos
, *__first
);
1233 template <class _Tp
, class _Alloc
> template <class _ForwardIterator
>
1235 vector
<_Tp
, _Alloc
>::_M_range_insert(iterator __position
,
1236 _ForwardIterator __first
,
1237 _ForwardIterator __last
,
1238 forward_iterator_tag
)
1240 if (__first
!= __last
) {
1241 size_type __n
= distance(__first
, __last
);
1242 if (size_type(_M_end_of_storage
- _M_finish
) >= __n
) {
1243 const size_type __elems_after
= end() - __position
;
1244 iterator
__old_finish(_M_finish
);
1245 if (__elems_after
> __n
) {
1246 uninitialized_copy(_M_finish
- __n
, _M_finish
, _M_finish
);
1248 copy_backward(__position
, __old_finish
- __n
, __old_finish
);
1249 copy(__first
, __last
, __position
);
1252 _ForwardIterator __mid
= __first
;
1253 advance(__mid
, __elems_after
);
1254 uninitialized_copy(__mid
, __last
, _M_finish
);
1255 _M_finish
+= __n
- __elems_after
;
1256 uninitialized_copy(__position
, __old_finish
, _M_finish
);
1257 _M_finish
+= __elems_after
;
1258 copy(__first
, __mid
, __position
);
1262 const size_type __old_size
= size();
1263 const size_type __len
= __old_size
+ max(__old_size
, __n
);
1264 iterator
__new_start(_M_allocate(__len
));
1265 iterator
__new_finish(__new_start
);
1267 __new_finish
= uninitialized_copy(iterator(_M_start
),
1268 __position
, __new_start
);
1269 __new_finish
= uninitialized_copy(__first
, __last
, __new_finish
);
1271 = uninitialized_copy(__position
, iterator(_M_finish
), __new_finish
);
1275 _Destroy(__new_start
,__new_finish
);
1276 _M_deallocate(__new_start
.base(), __len
);
1277 __throw_exception_again
;
1279 _Destroy(_M_start
, _M_finish
);
1280 _M_deallocate(_M_start
, _M_end_of_storage
- _M_start
);
1281 _M_start
= __new_start
.base();
1282 _M_finish
= __new_finish
.base();
1283 _M_end_of_storage
= __new_start
.base() + __len
;
1290 #endif /* __GLIBCPP_INTERNAL_VECTOR_H */