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>
71 // The vector base class serves two purposes. First, its constructor
72 // and destructor allocate (but don't initialize) storage. This makes
73 // exception safety easier. Second, the base class encapsulates all of
74 // the differences between SGI-style allocators and standard-conforming
77 // Base class for ordinary allocators.
78 template <class _Tp
, class _Allocator
, bool _IsStatic
>
79 class _Vector_alloc_base
{
81 typedef typename _Alloc_traits
<_Tp
, _Allocator
>::allocator_type
83 allocator_type
get_allocator() const { return _M_data_allocator
; }
85 _Vector_alloc_base(const allocator_type
& __a
)
86 : _M_data_allocator(__a
), _M_start(0), _M_finish(0), _M_end_of_storage(0)
90 allocator_type _M_data_allocator
;
93 _Tp
* _M_end_of_storage
;
95 _Tp
* _M_allocate(size_t __n
)
96 { return _M_data_allocator
.allocate(__n
); }
97 void _M_deallocate(_Tp
* __p
, size_t __n
)
98 { if (__p
) _M_data_allocator
.deallocate(__p
, __n
); }
101 // Specialization for allocators that have the property that we don't
102 // actually have to store an allocator object.
103 template <class _Tp
, class _Allocator
>
104 class _Vector_alloc_base
<_Tp
, _Allocator
, true> {
106 typedef typename _Alloc_traits
<_Tp
, _Allocator
>::allocator_type
108 allocator_type
get_allocator() const { return allocator_type(); }
110 _Vector_alloc_base(const allocator_type
&)
111 : _M_start(0), _M_finish(0), _M_end_of_storage(0)
117 _Tp
* _M_end_of_storage
;
119 typedef typename _Alloc_traits
<_Tp
, _Allocator
>::_Alloc_type _Alloc_type
;
120 _Tp
* _M_allocate(size_t __n
)
121 { return _Alloc_type::allocate(__n
); }
122 void _M_deallocate(_Tp
* __p
, size_t __n
)
123 { _Alloc_type::deallocate(__p
, __n
);}
126 template <class _Tp
, class _Alloc
>
128 : public _Vector_alloc_base
<_Tp
, _Alloc
,
129 _Alloc_traits
<_Tp
, _Alloc
>::_S_instanceless
>
131 typedef _Vector_alloc_base
<_Tp
, _Alloc
,
132 _Alloc_traits
<_Tp
, _Alloc
>::_S_instanceless
>
134 typedef typename
_Base::allocator_type allocator_type
;
136 _Vector_base(const allocator_type
& __a
) : _Base(__a
) {}
137 _Vector_base(size_t __n
, const allocator_type
& __a
) : _Base(__a
) {
138 _M_start
= _M_allocate(__n
);
139 _M_finish
= _M_start
;
140 _M_end_of_storage
= _M_start
+ __n
;
143 ~_Vector_base() { _M_deallocate(_M_start
, _M_end_of_storage
- _M_start
); }
148 * @brief A standard container which offers fixed time access to individual
149 * elements in any order.
151 * @ingroup Containers
154 * Meets the requirements of a <a href="tables.html#65">container</a>, a
155 * <a href="tables.html#66">reversible container</a>, and a
156 * <a href="tables.html#67">sequence</a>, including the
157 * <a href="tables.html#68">optional sequence requirements</a> with the
158 * %exception of @c push_front and @c pop_front.
160 * In some terminology a vector can be described as a dynamic C-style array,
161 * it offers fast and efficient access to individual elements in any order
162 * and saves the user from worrying about memory and size allocation.
163 * Subscripting ( [] ) access is also provided as with C-style arrays.
165 template <class _Tp
, class _Alloc
= allocator
<_Tp
> >
166 class vector
: protected _Vector_base
<_Tp
, _Alloc
>
168 // concept requirements
169 __glibcpp_class_requires(_Tp
, _SGIAssignableConcept
)
172 typedef _Vector_base
<_Tp
, _Alloc
> _Base
;
173 typedef vector
<_Tp
, _Alloc
> vector_type
;
175 typedef _Tp value_type
;
176 typedef value_type
* pointer
;
177 typedef const value_type
* const_pointer
;
178 typedef __gnu_cxx::__normal_iterator
<pointer
, vector_type
> iterator
;
179 typedef __gnu_cxx::__normal_iterator
<const_pointer
, vector_type
>
181 typedef value_type
& reference
;
182 typedef const value_type
& const_reference
;
183 typedef size_t size_type
;
184 typedef ptrdiff_t difference_type
;
186 typedef typename
_Base::allocator_type allocator_type
;
187 allocator_type
get_allocator() const { return _Base::get_allocator(); }
189 typedef reverse_iterator
<const_iterator
> const_reverse_iterator
;
190 typedef reverse_iterator
<iterator
> reverse_iterator
;
193 using _Base::_M_allocate
;
194 using _Base::_M_deallocate
;
195 using _Base::_M_start
;
196 using _Base::_M_finish
;
197 using _Base::_M_end_of_storage
;
200 void _M_insert_aux(iterator __position
, const _Tp
& __x
);
201 void _M_insert_aux(iterator __position
);
205 * Returns a read/write iterator that points to the first element in the
206 * vector. Iteration is done in ordinary element order.
208 iterator
begin() { return iterator (_M_start
); }
211 * Returns a read-only (constant) iterator that points to the first element
212 * in the vector. Iteration is done in ordinary element order.
214 const_iterator
begin() const
215 { return const_iterator (_M_start
); }
218 * Returns a read/write iterator that points one past the last element in
219 * the vector. Iteration is done in ordinary element order.
221 iterator
end() { return iterator (_M_finish
); }
224 * Returns a read-only (constant) iterator that points one past the last
225 * element in the vector. Iteration is done in ordinary element order.
227 const_iterator
end() const { return const_iterator (_M_finish
); }
230 * Returns a read/write reverse iterator that points to the last element in
231 * the vector. Iteration is done in reverse element order.
233 reverse_iterator
rbegin()
234 { return reverse_iterator(end()); }
237 * Returns a read-only (constant) reverse iterator that points to the last
238 * element in the vector. Iteration is done in reverse element order.
240 const_reverse_iterator
rbegin() const
241 { return const_reverse_iterator(end()); }
244 * Returns a read/write reverse iterator that points to one before the
245 * first element in the vector. Iteration is done in reverse element
248 reverse_iterator
rend()
249 { return reverse_iterator(begin()); }
252 * Returns a read-only (constant) reverse iterator that points to one
253 * before the first element in the vector. Iteration is done in reverse
256 const_reverse_iterator
rend() const
257 { return const_reverse_iterator(begin()); }
259 /** Returns the number of elements in the vector. */
260 size_type
size() const
261 { return size_type(end() - begin()); }
263 /** Returns the size of the largest possible vector. */
264 size_type
max_size() const
265 { return size_type(-1) / sizeof(_Tp
); }
268 * Returns the amount of memory that has been alocated for the current
271 size_type
capacity() const
272 { return size_type(const_iterator(_M_end_of_storage
) - begin()); }
275 * Returns true if the vector is empty. (Thus begin() would equal end().)
278 { return begin() == end(); }
281 * @brief Subscript access to the data contained in the vector.
282 * @param n The element for which data should be accessed.
283 * @return Read/write reference to data.
285 * This operator allows for easy, array-style, data access.
286 * Note that data access with this operator is unchecked and out_of_range
287 * lookups are not defined. (For checked lookups see at().)
289 reference
operator[](size_type __n
) { return *(begin() + __n
); }
292 * @brief Subscript access to the data contained in the vector.
293 * @param n The element for which data should be accessed.
294 * @return Read-only (constant) reference to data.
296 * This operator allows for easy, array-style, data access.
297 * Note that data access with this operator is unchecked and out_of_range
298 * lookups are not defined. (For checked lookups see at().)
300 const_reference
operator[](size_type __n
) const { return *(begin() + __n
); }
302 void _M_range_check(size_type __n
) const {
303 if (__n
>= this->size())
304 __throw_out_of_range("vector");
308 * @brief Provides access to the data contained in the vector.
309 * @param n The element for which data should be accessed.
310 * @return Read/write reference to data.
312 * This function provides for safer data access. The parameter is first
313 * checked that it is in the range of the vector. The function throws
314 * out_of_range if the check fails.
316 reference
at(size_type __n
)
317 { _M_range_check(__n
); return (*this)[__n
]; }
320 * @brief Provides access to the data contained in the vector.
321 * @param n The element for which data should be accessed.
322 * @return Read-only (constant) reference to data.
324 * This function provides for safer data access. The parameter is first
325 * checked that it is in the range of the vector. The function throws
326 * out_of_range if the check fails.
328 const_reference
at(size_type __n
) const
329 { _M_range_check(__n
); return (*this)[__n
]; }
332 explicit vector(const allocator_type
& __a
= allocator_type())
335 vector(size_type __n
, const _Tp
& __value
,
336 const allocator_type
& __a
= allocator_type())
338 { _M_finish
= uninitialized_fill_n(_M_start
, __n
, __value
); }
340 explicit vector(size_type __n
)
341 : _Base(__n
, allocator_type())
342 { _M_finish
= uninitialized_fill_n(_M_start
, __n
, _Tp()); }
344 vector(const vector
<_Tp
, _Alloc
>& __x
)
345 : _Base(__x
.size(), __x
.get_allocator())
346 { _M_finish
= uninitialized_copy(__x
.begin(), __x
.end(), _M_start
); }
348 // Check whether it's an integral type. If so, it's not an iterator.
349 template <class _InputIterator
>
350 vector(_InputIterator __first
, _InputIterator __last
,
351 const allocator_type
& __a
= allocator_type())
354 typedef typename _Is_integer
<_InputIterator
>::_Integral _Integral
;
355 _M_initialize_aux(__first
, __last
, _Integral());
358 template <class _Integer
>
359 void _M_initialize_aux(_Integer __n
, _Integer __value
, __true_type
)
361 _M_start
= _M_allocate(__n
);
362 _M_end_of_storage
= _M_start
+ __n
;
363 _M_finish
= uninitialized_fill_n(_M_start
, __n
, __value
);
366 template<class _InputIterator
>
368 _M_initialize_aux(_InputIterator __first
, _InputIterator __last
, __false_type
)
370 typedef typename iterator_traits
<_InputIterator
>::iterator_category _IterCategory
;
371 _M_range_initialize(__first
, __last
, _IterCategory());
375 { _Destroy(_M_start
, _M_finish
); }
377 vector
<_Tp
, _Alloc
>& operator=(const vector
<_Tp
, _Alloc
>& __x
);
380 * @brief Attempt to preallocate enough memory for specified number of
382 * @param n Number of elements required
384 * This function attempts to reserve enough memory for the vector to hold
385 * the specified number of elements. If the number requested is more than
386 * max_size() length_error is thrown.
388 * The advantage of this function is that if optimal code is a necessity
389 * and the user can determine the number of elements that will be required
390 * the user can reserve the memory and thus prevent a possible
391 * reallocation of memory and copy of vector data.
393 void reserve(size_type __n
) {
394 if (capacity() < __n
) {
395 const size_type __old_size
= size();
396 pointer __tmp
= _M_allocate_and_copy(__n
, _M_start
, _M_finish
);
397 _Destroy(_M_start
, _M_finish
);
398 _M_deallocate(_M_start
, _M_end_of_storage
- _M_start
);
400 _M_finish
= __tmp
+ __old_size
;
401 _M_end_of_storage
= _M_start
+ __n
;
405 // assign(), a generalized assignment member function. Two
406 // versions: one that takes a count, and one that takes a range.
407 // The range version is a member template, so we dispatch on whether
408 // or not the type is an integer.
411 * @brief Assigns a given value or range to a vector.
412 * @param n Number of elements to be assigned.
413 * @param val Value to be assigned.
415 * This function can be used to assign a range to a vector or fill it
416 * with a specified number of copies of the given value.
417 * Note that the assignment completely changes the vector and that the
418 * resulting vector's size is the same as the number of elements assigned.
419 * Old data may be lost.
421 void assign(size_type __n
, const _Tp
& __val
) { _M_fill_assign(__n
, __val
); }
422 void _M_fill_assign(size_type __n
, const _Tp
& __val
);
424 template<class _InputIterator
>
426 assign(_InputIterator __first
, _InputIterator __last
)
428 typedef typename _Is_integer
<_InputIterator
>::_Integral _Integral
;
429 _M_assign_dispatch(__first
, __last
, _Integral());
432 template<class _Integer
>
434 _M_assign_dispatch(_Integer __n
, _Integer __val
, __true_type
)
435 { _M_fill_assign((size_type
) __n
, (_Tp
) __val
); }
437 template<class _InputIter
>
439 _M_assign_dispatch(_InputIter __first
, _InputIter __last
, __false_type
)
441 typedef typename iterator_traits
<_InputIter
>::iterator_category _IterCategory
;
442 _M_assign_aux(__first
, __last
, _IterCategory());
445 template <class _InputIterator
>
447 _M_assign_aux(_InputIterator __first
, _InputIterator __last
,
450 template <class _ForwardIterator
>
452 _M_assign_aux(_ForwardIterator __first
, _ForwardIterator __last
,
453 forward_iterator_tag
);
456 * Returns a read/write reference to the data at the first element of the
459 reference
front() { return *begin(); }
462 * Returns a read-only (constant) reference to the data at the first
463 * element of the vector.
465 const_reference
front() const { return *begin(); }
468 * Returns a read/write reference to the data at the last element of the
471 reference
back() { return *(end() - 1); }
474 * Returns a read-only (constant) reference to the data at the first
475 * element of the vector.
477 const_reference
back() const { return *(end() - 1); }
480 * @brief Add data to the end of the vector.
481 * @param x Data to be added.
483 * This is a typical stack operation. The function creates an element at
484 * the end of the vector and assigns the given data to it.
485 * Due to the nature of a vector this operation can be done in constant
486 * time if the vector has preallocated space available.
489 push_back(const _Tp
& __x
)
491 if (_M_finish
!= _M_end_of_storage
) {
492 _Construct(_M_finish
, __x
);
496 _M_insert_aux(end(), __x
);
499 #ifdef _GLIBCPP_DEPRECATED
501 * Add an element to the end of the vector. The element is
502 * default-constructed.
504 * @note You must define _GLIBCPP_DEPRECATED to make this visible; see
510 if (_M_finish
!= _M_end_of_storage
) {
511 _Construct(_M_finish
);
515 _M_insert_aux(end());
520 swap(vector
<_Tp
, _Alloc
>& __x
)
522 std::swap(_M_start
, __x
._M_start
);
523 std::swap(_M_finish
, __x
._M_finish
);
524 std::swap(_M_end_of_storage
, __x
._M_end_of_storage
);
528 * @brief Inserts given value into vector at specified element.
529 * @param position An iterator that points to the element where data
530 * should be inserted.
531 * @param x Data to be inserted.
532 * @return An iterator that points to the inserted data.
534 * This function will insert the given value into the specified location.
535 * Note that this kind of operation could be expensive for a vector and if
536 * it is frequently used the user should consider using std::list.
539 insert(iterator __position
, const _Tp
& __x
)
541 size_type __n
= __position
- begin();
542 if (_M_finish
!= _M_end_of_storage
&& __position
== end()) {
543 _Construct(_M_finish
, __x
);
547 _M_insert_aux(iterator(__position
), __x
);
548 return begin() + __n
;
552 * @brief Inserts an empty element into the vector.
553 * @param position An iterator that points to the element where empty
554 * element should be inserted.
555 * @param x Data to be inserted.
556 * @return An iterator that points to the inserted element.
558 * This function will insert an empty element into the specified location.
559 * Note that this kind of operation could be expensive for a vector and if
560 * it is frequently used the user should consider using std::list.
563 insert(iterator __position
)
565 size_type __n
= __position
- begin();
566 if (_M_finish
!= _M_end_of_storage
&& __position
== end()) {
567 _Construct(_M_finish
);
571 _M_insert_aux(iterator(__position
));
572 return begin() + __n
;
575 // Check whether it's an integral type. If so, it's not an iterator.
576 template<class _InputIterator
>
578 insert(iterator __pos
, _InputIterator __first
, _InputIterator __last
)
580 typedef typename _Is_integer
<_InputIterator
>::_Integral _Integral
;
581 _M_insert_dispatch(__pos
, __first
, __last
, _Integral());
584 template <class _Integer
>
586 _M_insert_dispatch(iterator __pos
, _Integer __n
, _Integer __val
, __true_type
)
587 { _M_fill_insert(__pos
, static_cast<size_type
>(__n
), static_cast<_Tp
>(__val
)); }
589 template<class _InputIterator
>
591 _M_insert_dispatch(iterator __pos
,
592 _InputIterator __first
, _InputIterator __last
,
595 typedef typename iterator_traits
<_InputIterator
>::iterator_category _IterCategory
;
596 _M_range_insert(__pos
, __first
, __last
, _IterCategory());
600 * @brief Inserts a number of copies of given data into the vector.
601 * @param position An iterator that points to the element where data
602 * should be inserted.
603 * @param n Amount of elements to be inserted.
604 * @param x Data to be inserted.
606 * This function will insert a specified number of copies of the given data
607 * into the specified location.
609 * Note that this kind of operation could be expensive for a vector and if
610 * it is frequently used the user should consider using std::list.
612 void insert (iterator __pos
, size_type __n
, const _Tp
& __x
)
613 { _M_fill_insert(__pos
, __n
, __x
); }
615 void _M_fill_insert (iterator __pos
, size_type __n
, const _Tp
& __x
);
618 * @brief Removes last element from vector.
620 * This is a typical stack operation. It allows us to shrink the vector by
623 * Note that no data is returned and if last element's data is needed it
624 * should be retrieved before pop_back() is called.
632 * @brief Remove element at given position
633 * @param position Iterator pointing to element to be erased.
634 * @return Doc Me! (Iterator pointing to new element at old location?)
636 * This function will erase the element at the given position and thus
637 * shorten the vector by one.
639 * Note This operation could be expensive and if it is frequently used the
640 * user should consider using std::list. The user is also cautioned that
641 * this function only erases the element, and that if the element is itself
642 * a pointer, the pointed-to memory is not touched in any way. Managing
643 * the pointer is the user's responsibilty.
645 iterator
erase(iterator __position
) {
646 if (__position
+ 1 != end())
647 copy(__position
+ 1, end(), __position
);
654 * @brief Remove a range of elements from a vector.
655 * @param first Iterator pointing to the first element to be erased.
656 * @param last Iterator pointing to the last element to be erased.
657 * @return Doc Me! (Iterator pointing to new element at old location?)
659 * This function will erase the elements in the given range and shorten the
660 * vector accordingly.
662 * Note This operation could be expensive and if it is frequently used the
663 * user should consider using std::list. The user is also cautioned that
664 * this function only erases the elements, and that if the elements
665 * themselves are pointers, the pointed-to memory is not touched in any
666 * way. Managing the pointer is the user's responsibilty.
668 iterator
erase(iterator __first
, iterator __last
) {
669 iterator
__i(copy(__last
, end(), __first
));
670 _Destroy(__i
, end());
671 _M_finish
= _M_finish
- (__last
- __first
);
676 * @brief Resizes the vector to the specified number of elements.
677 * @param new_size Number of elements the vector should contain.
678 * @param x Data with which new elements should be populated.
680 * This function will resize the vector to the specified number of
681 * elements. If the number is smaller than the vector's current size the
682 * vector is truncated, otherwise the vector is extended and new elements
683 * are populated with given data.
685 void resize(size_type __new_size
, const _Tp
& __x
) {
686 if (__new_size
< size())
687 erase(begin() + __new_size
, end());
689 insert(end(), __new_size
- size(), __x
);
693 * @brief Resizes the vector to the specified number of elements.
694 * @param new_size Number of elements the vector should contain.
696 * This function will resize the vector to the specified number of
697 * elements. If the number is smaller than the vector's current size the
698 * vector is truncated, otherwise the vector is extended and new elements
699 * are left uninitialized.
701 void resize(size_type __new_size
) { resize(__new_size
, _Tp()); }
704 * Erases all elements in vector. Note that this function only erases the
705 * elements, and that if the elements themselves are pointers, the
706 * pointed-to memory is not touched in any way. Managing the pointer is
707 * the user's responsibilty.
709 void clear() { erase(begin(), end()); }
713 template <class _ForwardIterator
>
714 pointer
_M_allocate_and_copy(size_type __n
, _ForwardIterator __first
,
715 _ForwardIterator __last
)
717 pointer __result
= _M_allocate(__n
);
719 uninitialized_copy(__first
, __last
, __result
);
724 _M_deallocate(__result
, __n
);
725 __throw_exception_again
;
729 template <class _InputIterator
>
730 void _M_range_initialize(_InputIterator __first
,
731 _InputIterator __last
, input_iterator_tag
)
733 for ( ; __first
!= __last
; ++__first
)
737 // This function is only called by the constructor.
738 template <class _ForwardIterator
>
739 void _M_range_initialize(_ForwardIterator __first
,
740 _ForwardIterator __last
, forward_iterator_tag
)
742 size_type __n
= distance(__first
, __last
);
743 _M_start
= _M_allocate(__n
);
744 _M_end_of_storage
= _M_start
+ __n
;
745 _M_finish
= uninitialized_copy(__first
, __last
, _M_start
);
748 template <class _InputIterator
>
749 void _M_range_insert(iterator __pos
,
750 _InputIterator __first
, _InputIterator __last
,
753 template <class _ForwardIterator
>
754 void _M_range_insert(iterator __pos
,
755 _ForwardIterator __first
, _ForwardIterator __last
,
756 forward_iterator_tag
);
759 template <class _Tp
, class _Alloc
>
761 operator==(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
763 return __x
.size() == __y
.size() &&
764 equal(__x
.begin(), __x
.end(), __y
.begin());
767 template <class _Tp
, class _Alloc
>
769 operator<(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
771 return lexicographical_compare(__x
.begin(), __x
.end(),
772 __y
.begin(), __y
.end());
775 template <class _Tp
, class _Alloc
>
776 inline void swap(vector
<_Tp
, _Alloc
>& __x
, vector
<_Tp
, _Alloc
>& __y
)
781 template <class _Tp
, class _Alloc
>
783 operator!=(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
) {
784 return !(__x
== __y
);
787 template <class _Tp
, class _Alloc
>
789 operator>(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
) {
793 template <class _Tp
, class _Alloc
>
795 operator<=(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
) {
799 template <class _Tp
, class _Alloc
>
801 operator>=(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
) {
805 template <class _Tp
, class _Alloc
>
807 vector
<_Tp
,_Alloc
>::operator=(const vector
<_Tp
, _Alloc
>& __x
)
810 const size_type __xlen
= __x
.size();
811 if (__xlen
> capacity()) {
812 pointer __tmp
= _M_allocate_and_copy(__xlen
, __x
.begin(), __x
.end());
813 _Destroy(_M_start
, _M_finish
);
814 _M_deallocate(_M_start
, _M_end_of_storage
- _M_start
);
816 _M_end_of_storage
= _M_start
+ __xlen
;
818 else if (size() >= __xlen
) {
819 iterator
__i(copy(__x
.begin(), __x
.end(), begin()));
820 _Destroy(__i
, end());
823 copy(__x
.begin(), __x
.begin() + size(), _M_start
);
824 uninitialized_copy(__x
.begin() + size(), __x
.end(), _M_finish
);
826 _M_finish
= _M_start
+ __xlen
;
831 template <class _Tp
, class _Alloc
>
832 void vector
<_Tp
, _Alloc
>::_M_fill_assign(size_t __n
, const value_type
& __val
)
834 if (__n
> capacity()) {
835 vector
<_Tp
, _Alloc
> __tmp(__n
, __val
, get_allocator());
838 else if (__n
> size()) {
839 fill(begin(), end(), __val
);
840 _M_finish
= uninitialized_fill_n(_M_finish
, __n
- size(), __val
);
843 erase(fill_n(begin(), __n
, __val
), end());
846 template <class _Tp
, class _Alloc
> template <class _InputIter
>
847 void vector
<_Tp
, _Alloc
>::_M_assign_aux(_InputIter __first
, _InputIter __last
,
848 input_iterator_tag
) {
849 iterator
__cur(begin());
850 for ( ; __first
!= __last
&& __cur
!= end(); ++__cur
, ++__first
)
852 if (__first
== __last
)
855 insert(end(), __first
, __last
);
858 template <class _Tp
, class _Alloc
> template <class _ForwardIter
>
860 vector
<_Tp
, _Alloc
>::_M_assign_aux(_ForwardIter __first
, _ForwardIter __last
,
861 forward_iterator_tag
) {
862 size_type __len
= distance(__first
, __last
);
864 if (__len
> capacity()) {
865 pointer
__tmp(_M_allocate_and_copy(__len
, __first
, __last
));
866 _Destroy(_M_start
, _M_finish
);
867 _M_deallocate(_M_start
, _M_end_of_storage
- _M_start
);
869 _M_end_of_storage
= _M_finish
= _M_start
+ __len
;
871 else if (size() >= __len
) {
872 iterator
__new_finish(copy(__first
, __last
, _M_start
));
873 _Destroy(__new_finish
, end());
874 _M_finish
= __new_finish
.base();
877 _ForwardIter __mid
= __first
;
878 advance(__mid
, size());
879 copy(__first
, __mid
, _M_start
);
880 _M_finish
= uninitialized_copy(__mid
, __last
, _M_finish
);
884 template <class _Tp
, class _Alloc
>
886 vector
<_Tp
, _Alloc
>::_M_insert_aux(iterator __position
, const _Tp
& __x
)
888 if (_M_finish
!= _M_end_of_storage
) {
889 _Construct(_M_finish
, *(_M_finish
- 1));
892 copy_backward(__position
, iterator(_M_finish
- 2), iterator(_M_finish
- 1));
893 *__position
= __x_copy
;
896 const size_type __old_size
= size();
897 const size_type __len
= __old_size
!= 0 ? 2 * __old_size
: 1;
898 iterator
__new_start(_M_allocate(__len
));
899 iterator
__new_finish(__new_start
);
901 __new_finish
= uninitialized_copy(iterator(_M_start
), __position
,
903 _Construct(__new_finish
.base(), __x
);
905 __new_finish
= uninitialized_copy(__position
, iterator(_M_finish
),
910 _Destroy(__new_start
,__new_finish
);
911 _M_deallocate(__new_start
.base(),__len
);
912 __throw_exception_again
;
914 _Destroy(begin(), end());
915 _M_deallocate(_M_start
, _M_end_of_storage
- _M_start
);
916 _M_start
= __new_start
.base();
917 _M_finish
= __new_finish
.base();
918 _M_end_of_storage
= __new_start
.base() + __len
;
922 template <class _Tp
, class _Alloc
>
924 vector
<_Tp
, _Alloc
>::_M_insert_aux(iterator __position
)
926 if (_M_finish
!= _M_end_of_storage
) {
927 _Construct(_M_finish
, *(_M_finish
- 1));
929 copy_backward(__position
, iterator(_M_finish
- 2),
930 iterator(_M_finish
- 1));
934 const size_type __old_size
= size();
935 const size_type __len
= __old_size
!= 0 ? 2 * __old_size
: 1;
936 pointer __new_start
= _M_allocate(__len
);
937 pointer __new_finish
= __new_start
;
939 __new_finish
= uninitialized_copy(iterator(_M_start
), __position
,
941 _Construct(__new_finish
);
943 __new_finish
= uninitialized_copy(__position
, iterator(_M_finish
),
948 _Destroy(__new_start
,__new_finish
);
949 _M_deallocate(__new_start
,__len
);
950 __throw_exception_again
;
952 _Destroy(begin(), end());
953 _M_deallocate(_M_start
, _M_end_of_storage
- _M_start
);
954 _M_start
= __new_start
;
955 _M_finish
= __new_finish
;
956 _M_end_of_storage
= __new_start
+ __len
;
960 template <class _Tp
, class _Alloc
>
961 void vector
<_Tp
, _Alloc
>::_M_fill_insert(iterator __position
, size_type __n
,
965 if (size_type(_M_end_of_storage
- _M_finish
) >= __n
) {
967 const size_type __elems_after
= end() - __position
;
968 iterator
__old_finish(_M_finish
);
969 if (__elems_after
> __n
) {
970 uninitialized_copy(_M_finish
- __n
, _M_finish
, _M_finish
);
972 copy_backward(__position
, __old_finish
- __n
, __old_finish
);
973 fill(__position
, __position
+ __n
, __x_copy
);
976 uninitialized_fill_n(_M_finish
, __n
- __elems_after
, __x_copy
);
977 _M_finish
+= __n
- __elems_after
;
978 uninitialized_copy(__position
, __old_finish
, _M_finish
);
979 _M_finish
+= __elems_after
;
980 fill(__position
, __old_finish
, __x_copy
);
984 const size_type __old_size
= size();
985 const size_type __len
= __old_size
+ max(__old_size
, __n
);
986 iterator
__new_start(_M_allocate(__len
));
987 iterator
__new_finish(__new_start
);
989 __new_finish
= uninitialized_copy(begin(), __position
, __new_start
);
990 __new_finish
= uninitialized_fill_n(__new_finish
, __n
, __x
);
992 = uninitialized_copy(__position
, end(), __new_finish
);
996 _Destroy(__new_start
,__new_finish
);
997 _M_deallocate(__new_start
.base(),__len
);
998 __throw_exception_again
;
1000 _Destroy(_M_start
, _M_finish
);
1001 _M_deallocate(_M_start
, _M_end_of_storage
- _M_start
);
1002 _M_start
= __new_start
.base();
1003 _M_finish
= __new_finish
.base();
1004 _M_end_of_storage
= __new_start
.base() + __len
;
1009 template <class _Tp
, class _Alloc
> template <class _InputIterator
>
1011 vector
<_Tp
, _Alloc
>::_M_range_insert(iterator __pos
,
1012 _InputIterator __first
,
1013 _InputIterator __last
,
1016 for ( ; __first
!= __last
; ++__first
) {
1017 __pos
= insert(__pos
, *__first
);
1022 template <class _Tp
, class _Alloc
> template <class _ForwardIterator
>
1024 vector
<_Tp
, _Alloc
>::_M_range_insert(iterator __position
,
1025 _ForwardIterator __first
,
1026 _ForwardIterator __last
,
1027 forward_iterator_tag
)
1029 if (__first
!= __last
) {
1030 size_type __n
= distance(__first
, __last
);
1031 if (size_type(_M_end_of_storage
- _M_finish
) >= __n
) {
1032 const size_type __elems_after
= end() - __position
;
1033 iterator
__old_finish(_M_finish
);
1034 if (__elems_after
> __n
) {
1035 uninitialized_copy(_M_finish
- __n
, _M_finish
, _M_finish
);
1037 copy_backward(__position
, __old_finish
- __n
, __old_finish
);
1038 copy(__first
, __last
, __position
);
1041 _ForwardIterator __mid
= __first
;
1042 advance(__mid
, __elems_after
);
1043 uninitialized_copy(__mid
, __last
, _M_finish
);
1044 _M_finish
+= __n
- __elems_after
;
1045 uninitialized_copy(__position
, __old_finish
, _M_finish
);
1046 _M_finish
+= __elems_after
;
1047 copy(__first
, __mid
, __position
);
1051 const size_type __old_size
= size();
1052 const size_type __len
= __old_size
+ max(__old_size
, __n
);
1053 iterator
__new_start(_M_allocate(__len
));
1054 iterator
__new_finish(__new_start
);
1056 __new_finish
= uninitialized_copy(iterator(_M_start
),
1057 __position
, __new_start
);
1058 __new_finish
= uninitialized_copy(__first
, __last
, __new_finish
);
1060 = uninitialized_copy(__position
, iterator(_M_finish
), __new_finish
);
1064 _Destroy(__new_start
,__new_finish
);
1065 _M_deallocate(__new_start
.base(), __len
);
1066 __throw_exception_again
;
1068 _Destroy(_M_start
, _M_finish
);
1069 _M_deallocate(_M_start
, _M_end_of_storage
- _M_start
);
1070 _M_start
= __new_start
.base();
1071 _M_finish
= __new_finish
.base();
1072 _M_end_of_storage
= __new_start
.base() + __len
;
1079 #endif /* __GLIBCPP_INTERNAL_VECTOR_H */