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 __normal_iterator
<pointer
, vector_type
> iterator
;
179 typedef __normal_iterator
<const_pointer
, vector_type
> const_iterator
;
180 typedef value_type
& reference
;
181 typedef const value_type
& const_reference
;
182 typedef size_t size_type
;
183 typedef ptrdiff_t difference_type
;
185 typedef typename
_Base::allocator_type allocator_type
;
186 allocator_type
get_allocator() const { return _Base::get_allocator(); }
188 typedef reverse_iterator
<const_iterator
> const_reverse_iterator
;
189 typedef reverse_iterator
<iterator
> reverse_iterator
;
192 using _Base::_M_allocate
;
193 using _Base::_M_deallocate
;
194 using _Base::_M_start
;
195 using _Base::_M_finish
;
196 using _Base::_M_end_of_storage
;
199 void _M_insert_aux(iterator __position
, const _Tp
& __x
);
200 void _M_insert_aux(iterator __position
);
204 * Returns a read/write iterator that points to the first element in the
205 * vector. Iteration is done in ordinary element order.
207 iterator
begin() { return iterator (_M_start
); }
210 * Returns a read-only (constant) iterator that points to the first element
211 * in the vector. Iteration is done in ordinary element order.
213 const_iterator
begin() const
214 { return const_iterator (_M_start
); }
217 * Returns a read/write iterator that points one past the last element in
218 * the vector. Iteration is done in ordinary element order.
220 iterator
end() { return iterator (_M_finish
); }
223 * Returns a read-only (constant) iterator that points one past the last
224 * element in the vector. Iteration is done in ordinary element order.
226 const_iterator
end() const { return const_iterator (_M_finish
); }
229 * Returns a read/write reverse iterator that points to the last element in
230 * the vector. Iteration is done in reverse element order.
232 reverse_iterator
rbegin()
233 { return reverse_iterator(end()); }
236 * Returns a read-only (constant) reverse iterator that points to the last
237 * element in the vector. Iteration is done in reverse element order.
239 const_reverse_iterator
rbegin() const
240 { return const_reverse_iterator(end()); }
243 * Returns a read/write reverse iterator that points to one before the
244 * first element in the vector. Iteration is done in reverse element
247 reverse_iterator
rend()
248 { return reverse_iterator(begin()); }
251 * Returns a read-only (constant) reverse iterator that points to one
252 * before the first element in the vector. Iteration is done in reverse
255 const_reverse_iterator
rend() const
256 { return const_reverse_iterator(begin()); }
258 /** Returns the number of elements in the vector. */
259 size_type
size() const
260 { return size_type(end() - begin()); }
262 /** Returns the size of the largest possible vector. */
263 size_type
max_size() const
264 { return size_type(-1) / sizeof(_Tp
); }
267 * Returns the amount of memory that has been alocated for the current
270 size_type
capacity() const
271 { return size_type(const_iterator(_M_end_of_storage
) - begin()); }
274 * Returns true if the vector is empty. (Thus begin() would equal end().)
277 { return begin() == end(); }
280 * @brief Subscript access to the data contained in the vector.
281 * @param n The element for which data should be accessed.
282 * @return Read/write reference to data.
284 * This operator allows for easy, array-style, data access.
285 * Note that data access with this operator is unchecked and out_of_range
286 * lookups are not defined. (For checked lookups see at().)
288 reference
operator[](size_type __n
) { return *(begin() + __n
); }
291 * @brief Subscript access to the data contained in the vector.
292 * @param n The element for which data should be accessed.
293 * @return Read-only (constant) reference to data.
295 * This operator allows for easy, array-style, data access.
296 * Note that data access with this operator is unchecked and out_of_range
297 * lookups are not defined. (For checked lookups see at().)
299 const_reference
operator[](size_type __n
) const { return *(begin() + __n
); }
301 void _M_range_check(size_type __n
) const {
302 if (__n
>= this->size())
303 __throw_out_of_range("vector");
307 * @brief Provides access to the data contained in the vector.
308 * @param n The element for which data should be accessed.
309 * @return Read/write reference to data.
311 * This function provides for safer data access. The parameter is first
312 * checked that it is in the range of the vector. The function throws
313 * out_of_range if the check fails.
315 reference
at(size_type __n
)
316 { _M_range_check(__n
); return (*this)[__n
]; }
319 * @brief Provides access to the data contained in the vector.
320 * @param n The element for which data should be accessed.
321 * @return Read-only (constant) reference to data.
323 * This function provides for safer data access. The parameter is first
324 * checked that it is in the range of the vector. The function throws
325 * out_of_range if the check fails.
327 const_reference
at(size_type __n
) const
328 { _M_range_check(__n
); return (*this)[__n
]; }
331 explicit vector(const allocator_type
& __a
= allocator_type())
334 vector(size_type __n
, const _Tp
& __value
,
335 const allocator_type
& __a
= allocator_type())
337 { _M_finish
= uninitialized_fill_n(_M_start
, __n
, __value
); }
339 explicit vector(size_type __n
)
340 : _Base(__n
, allocator_type())
341 { _M_finish
= uninitialized_fill_n(_M_start
, __n
, _Tp()); }
343 vector(const vector
<_Tp
, _Alloc
>& __x
)
344 : _Base(__x
.size(), __x
.get_allocator())
345 { _M_finish
= uninitialized_copy(__x
.begin(), __x
.end(), _M_start
); }
347 // Check whether it's an integral type. If so, it's not an iterator.
348 template <class _InputIterator
>
349 vector(_InputIterator __first
, _InputIterator __last
,
350 const allocator_type
& __a
= allocator_type())
353 typedef typename _Is_integer
<_InputIterator
>::_Integral _Integral
;
354 _M_initialize_aux(__first
, __last
, _Integral());
357 template <class _Integer
>
358 void _M_initialize_aux(_Integer __n
, _Integer __value
, __true_type
)
360 _M_start
= _M_allocate(__n
);
361 _M_end_of_storage
= _M_start
+ __n
;
362 _M_finish
= uninitialized_fill_n(_M_start
, __n
, __value
);
365 template<class _InputIterator
>
367 _M_initialize_aux(_InputIterator __first
, _InputIterator __last
, __false_type
)
369 typedef typename iterator_traits
<_InputIterator
>::iterator_category _IterCategory
;
370 _M_range_initialize(__first
, __last
, _IterCategory());
374 { _Destroy(_M_start
, _M_finish
); }
376 vector
<_Tp
, _Alloc
>& operator=(const vector
<_Tp
, _Alloc
>& __x
);
379 * @brief Attempt to preallocate enough memory for specified number of
381 * @param n Number of elements required
383 * This function attempts to reserve enough memory for the vector to hold
384 * the specified number of elements. If the number requested is more than
385 * max_size() length_error is thrown.
387 * The advantage of this function is that if optimal code is a necessity
388 * and the user can determine the number of elements that will be required
389 * the user can reserve the memory and thus prevent a possible
390 * reallocation of memory and copy of vector data.
392 void reserve(size_type __n
) {
393 if (capacity() < __n
) {
394 const size_type __old_size
= size();
395 pointer __tmp
= _M_allocate_and_copy(__n
, _M_start
, _M_finish
);
396 _Destroy(_M_start
, _M_finish
);
397 _M_deallocate(_M_start
, _M_end_of_storage
- _M_start
);
399 _M_finish
= __tmp
+ __old_size
;
400 _M_end_of_storage
= _M_start
+ __n
;
404 // assign(), a generalized assignment member function. Two
405 // versions: one that takes a count, and one that takes a range.
406 // The range version is a member template, so we dispatch on whether
407 // or not the type is an integer.
410 * @brief Assigns a given value or range to a vector.
411 * @param n Number of elements to be assigned.
412 * @param val Value to be assigned.
414 * This function can be used to assign a range to a vector or fill it
415 * with a specified number of copies of the given value.
416 * Note that the assignment completely changes the vector and that the
417 * resulting vector's size is the same as the number of elements assigned.
418 * Old data may be lost.
420 void assign(size_type __n
, const _Tp
& __val
) { _M_fill_assign(__n
, __val
); }
421 void _M_fill_assign(size_type __n
, const _Tp
& __val
);
423 template<class _InputIterator
>
425 assign(_InputIterator __first
, _InputIterator __last
)
427 typedef typename _Is_integer
<_InputIterator
>::_Integral _Integral
;
428 _M_assign_dispatch(__first
, __last
, _Integral());
431 template<class _Integer
>
433 _M_assign_dispatch(_Integer __n
, _Integer __val
, __true_type
)
434 { _M_fill_assign((size_type
) __n
, (_Tp
) __val
); }
436 template<class _InputIter
>
438 _M_assign_dispatch(_InputIter __first
, _InputIter __last
, __false_type
)
440 typedef typename iterator_traits
<_InputIter
>::iterator_category _IterCategory
;
441 _M_assign_aux(__first
, __last
, _IterCategory());
444 template <class _InputIterator
>
445 void _M_assign_aux(_InputIterator __first
, _InputIterator __last
,
448 template <class _ForwardIterator
>
449 void _M_assign_aux(_ForwardIterator __first
, _ForwardIterator __last
,
450 forward_iterator_tag
);
453 * Returns a read/write reference to the data at the first element of the
456 reference
front() { return *begin(); }
459 * Returns a read-only (constant) reference to the data at the first
460 * element of the vector.
462 const_reference
front() const { return *begin(); }
465 * Returns a read/write reference to the data at the last element of the
468 reference
back() { return *(end() - 1); }
471 * Returns a read-only (constant) reference to the data at the first
472 * element of the vector.
474 const_reference
back() const { return *(end() - 1); }
477 * @brief Add data to the end of the vector.
478 * @param x Data to be added.
480 * This is a typical stack operation. The function creates an element at
481 * the end of the vector and assigns the given data to it.
482 * Due to the nature of a vector this operation can be done in constant
483 * time if the vector has preallocated space available.
486 push_back(const _Tp
& __x
)
488 if (_M_finish
!= _M_end_of_storage
) {
489 _Construct(_M_finish
, __x
);
493 _M_insert_aux(end(), __x
);
496 #ifdef _GLIBCPP_DEPRECATED
498 * Add an element to the end of the vector. The element is
499 * default-constructed.
501 * @note You must define _GLIBCPP_DEPRECATED to make this visible; see
507 if (_M_finish
!= _M_end_of_storage
) {
508 _Construct(_M_finish
);
512 _M_insert_aux(end());
517 swap(vector
<_Tp
, _Alloc
>& __x
)
519 std::swap(_M_start
, __x
._M_start
);
520 std::swap(_M_finish
, __x
._M_finish
);
521 std::swap(_M_end_of_storage
, __x
._M_end_of_storage
);
525 * @brief Inserts given value into vector at specified element.
526 * @param position An iterator that points to the element where data
527 * should be inserted.
528 * @param x Data to be inserted.
529 * @return An iterator that points to the inserted data.
531 * This function will insert the given value into the specified location.
532 * Note that this kind of operation could be expensive for a vector and if
533 * it is frequently used the user should consider using std::list.
536 insert(iterator __position
, const _Tp
& __x
)
538 size_type __n
= __position
- begin();
539 if (_M_finish
!= _M_end_of_storage
&& __position
== end()) {
540 _Construct(_M_finish
, __x
);
544 _M_insert_aux(iterator(__position
), __x
);
545 return begin() + __n
;
549 * @brief Inserts an empty element into the vector.
550 * @param position An iterator that points to the element where empty
551 * element should be inserted.
552 * @param x Data to be inserted.
553 * @return An iterator that points to the inserted element.
555 * This function will insert an empty element into the specified location.
556 * Note that this kind of operation could be expensive for a vector and if
557 * it is frequently used the user should consider using std::list.
560 insert(iterator __position
)
562 size_type __n
= __position
- begin();
563 if (_M_finish
!= _M_end_of_storage
&& __position
== end()) {
564 _Construct(_M_finish
);
568 _M_insert_aux(iterator(__position
));
569 return begin() + __n
;
572 // Check whether it's an integral type. If so, it's not an iterator.
573 template<class _InputIterator
>
575 insert(iterator __pos
, _InputIterator __first
, _InputIterator __last
)
577 typedef typename _Is_integer
<_InputIterator
>::_Integral _Integral
;
578 _M_insert_dispatch(__pos
, __first
, __last
, _Integral());
581 template <class _Integer
>
583 _M_insert_dispatch(iterator __pos
, _Integer __n
, _Integer __val
, __true_type
)
584 { _M_fill_insert(__pos
, static_cast<size_type
>(__n
), static_cast<_Tp
>(__val
)); }
586 template<class _InputIterator
>
588 _M_insert_dispatch(iterator __pos
,
589 _InputIterator __first
, _InputIterator __last
,
592 typedef typename iterator_traits
<_InputIterator
>::iterator_category _IterCategory
;
593 _M_range_insert(__pos
, __first
, __last
, _IterCategory());
597 * @brief Inserts a number of copies of given data into the vector.
598 * @param position An iterator that points to the element where data
599 * should be inserted.
600 * @param n Amount of elements to be inserted.
601 * @param x Data to be inserted.
603 * This function will insert a specified number of copies of the given data
604 * into the specified location.
606 * Note that this kind of operation could be expensive for a vector and if
607 * it is frequently used the user should consider using std::list.
609 void insert (iterator __pos
, size_type __n
, const _Tp
& __x
)
610 { _M_fill_insert(__pos
, __n
, __x
); }
612 void _M_fill_insert (iterator __pos
, size_type __n
, const _Tp
& __x
);
615 * @brief Removes last element from vector.
617 * This is a typical stack operation. It allows us to shrink the vector by
620 * Note that no data is returned and if last element's data is needed it
621 * should be retrieved before pop_back() is called.
629 * @brief Remove element at given position
630 * @param position Iterator pointing to element to be erased.
631 * @return Doc Me! (Iterator pointing to new element at old location?)
633 * This function will erase the element at the given position and thus
634 * shorten the vector by one.
636 * Note This operation could be expensive and if it is frequently used the
637 * user should consider using std::list. The user is also cautioned that
638 * this function only erases the element, and that if the element is itself
639 * a pointer, the pointed-to memory is not touched in any way. Managing
640 * the pointer is the user's responsibilty.
642 iterator
erase(iterator __position
) {
643 if (__position
+ 1 != end())
644 copy(__position
+ 1, end(), __position
);
651 * @brief Remove a range of elements from a vector.
652 * @param first Iterator pointing to the first element to be erased.
653 * @param last Iterator pointing to the last element to be erased.
654 * @return Doc Me! (Iterator pointing to new element at old location?)
656 * This function will erase the elements in the given range and shorten the
657 * vector accordingly.
659 * Note This operation could be expensive and if it is frequently used the
660 * user should consider using std::list. The user is also cautioned that
661 * this function only erases the elements, and that if the elements
662 * themselves are pointers, the pointed-to memory is not touched in any
663 * way. Managing the pointer is the user's responsibilty.
665 iterator
erase(iterator __first
, iterator __last
) {
666 iterator
__i(copy(__last
, end(), __first
));
667 _Destroy(__i
, end());
668 _M_finish
= _M_finish
- (__last
- __first
);
673 * @brief Resizes the vector to the specified number of elements.
674 * @param new_size Number of elements the vector should contain.
675 * @param x Data with which new elements should be populated.
677 * This function will resize the vector to the specified number of
678 * elements. If the number is smaller than the vector's current size the
679 * vector is truncated, otherwise the vector is extended and new elements
680 * are populated with given data.
682 void resize(size_type __new_size
, const _Tp
& __x
) {
683 if (__new_size
< size())
684 erase(begin() + __new_size
, end());
686 insert(end(), __new_size
- size(), __x
);
690 * @brief Resizes the vector to the specified number of elements.
691 * @param new_size Number of elements the vector should contain.
693 * This function will resize the vector to the specified number of
694 * elements. If the number is smaller than the vector's current size the
695 * vector is truncated, otherwise the vector is extended and new elements
696 * are left uninitialized.
698 void resize(size_type __new_size
) { resize(__new_size
, _Tp()); }
701 * Erases all elements in vector. Note that this function only erases the
702 * elements, and that if the elements themselves are pointers, the
703 * pointed-to memory is not touched in any way. Managing the pointer is
704 * the user's responsibilty.
706 void clear() { erase(begin(), end()); }
710 template <class _ForwardIterator
>
711 pointer
_M_allocate_and_copy(size_type __n
, _ForwardIterator __first
,
712 _ForwardIterator __last
)
714 pointer __result
= _M_allocate(__n
);
716 uninitialized_copy(__first
, __last
, __result
);
721 _M_deallocate(__result
, __n
);
722 __throw_exception_again
;
726 template <class _InputIterator
>
727 void _M_range_initialize(_InputIterator __first
,
728 _InputIterator __last
, input_iterator_tag
)
730 for ( ; __first
!= __last
; ++__first
)
734 // This function is only called by the constructor.
735 template <class _ForwardIterator
>
736 void _M_range_initialize(_ForwardIterator __first
,
737 _ForwardIterator __last
, forward_iterator_tag
)
739 size_type __n
= distance(__first
, __last
);
740 _M_start
= _M_allocate(__n
);
741 _M_end_of_storage
= _M_start
+ __n
;
742 _M_finish
= uninitialized_copy(__first
, __last
, _M_start
);
745 template <class _InputIterator
>
746 void _M_range_insert(iterator __pos
,
747 _InputIterator __first
, _InputIterator __last
,
750 template <class _ForwardIterator
>
751 void _M_range_insert(iterator __pos
,
752 _ForwardIterator __first
, _ForwardIterator __last
,
753 forward_iterator_tag
);
756 template <class _Tp
, class _Alloc
>
758 operator==(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
760 return __x
.size() == __y
.size() &&
761 equal(__x
.begin(), __x
.end(), __y
.begin());
764 template <class _Tp
, class _Alloc
>
766 operator<(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
768 return lexicographical_compare(__x
.begin(), __x
.end(),
769 __y
.begin(), __y
.end());
772 template <class _Tp
, class _Alloc
>
773 inline void swap(vector
<_Tp
, _Alloc
>& __x
, vector
<_Tp
, _Alloc
>& __y
)
778 template <class _Tp
, class _Alloc
>
780 operator!=(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
) {
781 return !(__x
== __y
);
784 template <class _Tp
, class _Alloc
>
786 operator>(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
) {
790 template <class _Tp
, class _Alloc
>
792 operator<=(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
) {
796 template <class _Tp
, class _Alloc
>
798 operator>=(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
) {
802 template <class _Tp
, class _Alloc
>
804 vector
<_Tp
,_Alloc
>::operator=(const vector
<_Tp
, _Alloc
>& __x
)
807 const size_type __xlen
= __x
.size();
808 if (__xlen
> capacity()) {
809 pointer __tmp
= _M_allocate_and_copy(__xlen
, __x
.begin(), __x
.end());
810 _Destroy(_M_start
, _M_finish
);
811 _M_deallocate(_M_start
, _M_end_of_storage
- _M_start
);
813 _M_end_of_storage
= _M_start
+ __xlen
;
815 else if (size() >= __xlen
) {
816 iterator
__i(copy(__x
.begin(), __x
.end(), begin()));
817 _Destroy(__i
, end());
820 copy(__x
.begin(), __x
.begin() + size(), _M_start
);
821 uninitialized_copy(__x
.begin() + size(), __x
.end(), _M_finish
);
823 _M_finish
= _M_start
+ __xlen
;
828 template <class _Tp
, class _Alloc
>
829 void vector
<_Tp
, _Alloc
>::_M_fill_assign(size_t __n
, const value_type
& __val
)
831 if (__n
> capacity()) {
832 vector
<_Tp
, _Alloc
> __tmp(__n
, __val
, get_allocator());
835 else if (__n
> size()) {
836 fill(begin(), end(), __val
);
837 _M_finish
= uninitialized_fill_n(_M_finish
, __n
- size(), __val
);
840 erase(fill_n(begin(), __n
, __val
), end());
843 template <class _Tp
, class _Alloc
> template <class _InputIter
>
844 void vector
<_Tp
, _Alloc
>::_M_assign_aux(_InputIter __first
, _InputIter __last
,
845 input_iterator_tag
) {
846 iterator
__cur(begin());
847 for ( ; __first
!= __last
&& __cur
!= end(); ++__cur
, ++__first
)
849 if (__first
== __last
)
852 insert(end(), __first
, __last
);
855 template <class _Tp
, class _Alloc
> template <class _ForwardIter
>
857 vector
<_Tp
, _Alloc
>::_M_assign_aux(_ForwardIter __first
, _ForwardIter __last
,
858 forward_iterator_tag
) {
859 size_type __len
= distance(__first
, __last
);
861 if (__len
> capacity()) {
862 pointer
__tmp(_M_allocate_and_copy(__len
, __first
, __last
));
863 _Destroy(_M_start
, _M_finish
);
864 _M_deallocate(_M_start
, _M_end_of_storage
- _M_start
);
866 _M_end_of_storage
= _M_finish
= _M_start
+ __len
;
868 else if (size() >= __len
) {
869 iterator
__new_finish(copy(__first
, __last
, _M_start
));
870 _Destroy(__new_finish
, end());
871 _M_finish
= __new_finish
.base();
874 _ForwardIter __mid
= __first
;
875 advance(__mid
, size());
876 copy(__first
, __mid
, _M_start
);
877 _M_finish
= uninitialized_copy(__mid
, __last
, _M_finish
);
881 template <class _Tp
, class _Alloc
>
883 vector
<_Tp
, _Alloc
>::_M_insert_aux(iterator __position
, const _Tp
& __x
)
885 if (_M_finish
!= _M_end_of_storage
) {
886 _Construct(_M_finish
, *(_M_finish
- 1));
889 copy_backward(__position
, iterator(_M_finish
- 2), iterator(_M_finish
- 1));
890 *__position
= __x_copy
;
893 const size_type __old_size
= size();
894 const size_type __len
= __old_size
!= 0 ? 2 * __old_size
: 1;
895 iterator
__new_start(_M_allocate(__len
));
896 iterator
__new_finish(__new_start
);
898 __new_finish
= uninitialized_copy(iterator(_M_start
), __position
,
900 _Construct(__new_finish
.base(), __x
);
902 __new_finish
= uninitialized_copy(__position
, iterator(_M_finish
),
907 _Destroy(__new_start
,__new_finish
);
908 _M_deallocate(__new_start
.base(),__len
);
909 __throw_exception_again
;
911 _Destroy(begin(), end());
912 _M_deallocate(_M_start
, _M_end_of_storage
- _M_start
);
913 _M_start
= __new_start
.base();
914 _M_finish
= __new_finish
.base();
915 _M_end_of_storage
= __new_start
.base() + __len
;
919 template <class _Tp
, class _Alloc
>
921 vector
<_Tp
, _Alloc
>::_M_insert_aux(iterator __position
)
923 if (_M_finish
!= _M_end_of_storage
) {
924 _Construct(_M_finish
, *(_M_finish
- 1));
926 copy_backward(__position
, iterator(_M_finish
- 2),
927 iterator(_M_finish
- 1));
931 const size_type __old_size
= size();
932 const size_type __len
= __old_size
!= 0 ? 2 * __old_size
: 1;
933 pointer __new_start
= _M_allocate(__len
);
934 pointer __new_finish
= __new_start
;
936 __new_finish
= uninitialized_copy(iterator(_M_start
), __position
,
938 _Construct(__new_finish
);
940 __new_finish
= uninitialized_copy(__position
, iterator(_M_finish
),
945 _Destroy(__new_start
,__new_finish
);
946 _M_deallocate(__new_start
,__len
);
947 __throw_exception_again
;
949 _Destroy(begin(), end());
950 _M_deallocate(_M_start
, _M_end_of_storage
- _M_start
);
951 _M_start
= __new_start
;
952 _M_finish
= __new_finish
;
953 _M_end_of_storage
= __new_start
+ __len
;
957 template <class _Tp
, class _Alloc
>
958 void vector
<_Tp
, _Alloc
>::_M_fill_insert(iterator __position
, size_type __n
,
962 if (size_type(_M_end_of_storage
- _M_finish
) >= __n
) {
964 const size_type __elems_after
= end() - __position
;
965 iterator
__old_finish(_M_finish
);
966 if (__elems_after
> __n
) {
967 uninitialized_copy(_M_finish
- __n
, _M_finish
, _M_finish
);
969 copy_backward(__position
, __old_finish
- __n
, __old_finish
);
970 fill(__position
, __position
+ __n
, __x_copy
);
973 uninitialized_fill_n(_M_finish
, __n
- __elems_after
, __x_copy
);
974 _M_finish
+= __n
- __elems_after
;
975 uninitialized_copy(__position
, __old_finish
, _M_finish
);
976 _M_finish
+= __elems_after
;
977 fill(__position
, __old_finish
, __x_copy
);
981 const size_type __old_size
= size();
982 const size_type __len
= __old_size
+ max(__old_size
, __n
);
983 iterator
__new_start(_M_allocate(__len
));
984 iterator
__new_finish(__new_start
);
986 __new_finish
= uninitialized_copy(begin(), __position
, __new_start
);
987 __new_finish
= uninitialized_fill_n(__new_finish
, __n
, __x
);
989 = uninitialized_copy(__position
, end(), __new_finish
);
993 _Destroy(__new_start
,__new_finish
);
994 _M_deallocate(__new_start
.base(),__len
);
995 __throw_exception_again
;
997 _Destroy(_M_start
, _M_finish
);
998 _M_deallocate(_M_start
, _M_end_of_storage
- _M_start
);
999 _M_start
= __new_start
.base();
1000 _M_finish
= __new_finish
.base();
1001 _M_end_of_storage
= __new_start
.base() + __len
;
1006 template <class _Tp
, class _Alloc
> template <class _InputIterator
>
1008 vector
<_Tp
, _Alloc
>::_M_range_insert(iterator __pos
,
1009 _InputIterator __first
,
1010 _InputIterator __last
,
1013 for ( ; __first
!= __last
; ++__first
) {
1014 __pos
= insert(__pos
, *__first
);
1019 template <class _Tp
, class _Alloc
> template <class _ForwardIterator
>
1021 vector
<_Tp
, _Alloc
>::_M_range_insert(iterator __position
,
1022 _ForwardIterator __first
,
1023 _ForwardIterator __last
,
1024 forward_iterator_tag
)
1026 if (__first
!= __last
) {
1027 size_type __n
= distance(__first
, __last
);
1028 if (size_type(_M_end_of_storage
- _M_finish
) >= __n
) {
1029 const size_type __elems_after
= end() - __position
;
1030 iterator
__old_finish(_M_finish
);
1031 if (__elems_after
> __n
) {
1032 uninitialized_copy(_M_finish
- __n
, _M_finish
, _M_finish
);
1034 copy_backward(__position
, __old_finish
- __n
, __old_finish
);
1035 copy(__first
, __last
, __position
);
1038 _ForwardIterator __mid
= __first
;
1039 advance(__mid
, __elems_after
);
1040 uninitialized_copy(__mid
, __last
, _M_finish
);
1041 _M_finish
+= __n
- __elems_after
;
1042 uninitialized_copy(__position
, __old_finish
, _M_finish
);
1043 _M_finish
+= __elems_after
;
1044 copy(__first
, __mid
, __position
);
1048 const size_type __old_size
= size();
1049 const size_type __len
= __old_size
+ max(__old_size
, __n
);
1050 iterator
__new_start(_M_allocate(__len
));
1051 iterator
__new_finish(__new_start
);
1053 __new_finish
= uninitialized_copy(iterator(_M_start
),
1054 __position
, __new_start
);
1055 __new_finish
= uninitialized_copy(__first
, __last
, __new_finish
);
1057 = uninitialized_copy(__position
, iterator(_M_finish
), __new_finish
);
1061 _Destroy(__new_start
,__new_finish
);
1062 _M_deallocate(__new_start
.base(), __len
);
1063 __throw_exception_again
;
1065 _Destroy(_M_start
, _M_finish
);
1066 _M_deallocate(_M_start
, _M_end_of_storage
- _M_start
);
1067 _M_start
= __new_start
.base();
1068 _M_finish
= __new_finish
.base();
1069 _M_end_of_storage
= __new_start
.base() + __len
;
1076 #endif /* __GLIBCPP_INTERNAL_VECTOR_H */