abi.txt: New file.
[official-gcc.git] / libstdc++-v3 / include / bits / stl_vector.h
blob5f82d5d60953d32213f0ddc6d59266081a762c00
1 // Vector implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002 Free Software Foundation, Inc.
4 //
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)
9 // any later version.
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,
19 // USA.
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.
32 * Copyright (c) 1994
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.
44 * Copyright (c) 1996
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.
71 namespace std
74 /// @if maint Primary default version. @endif
75 /**
76 * @if maint
77 * See bits/stl_deque.h's _Deque_alloc_base for an explanation.
78 * @endif
80 template <typename _Tp, typename _Allocator, bool _IsStatic>
81 class _Vector_alloc_base
83 public:
84 typedef typename _Alloc_traits<_Tp, _Allocator>::allocator_type
85 allocator_type;
87 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)
94 protected:
95 allocator_type _M_data_allocator;
96 _Tp* _M_start;
97 _Tp* _M_finish;
98 _Tp* _M_end_of_storage;
100 _Tp*
101 _M_allocate(size_t __n) { return _M_data_allocator.allocate(__n); }
103 void
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 <typename _Tp, typename _Allocator>
110 class _Vector_alloc_base<_Tp, _Allocator, true>
112 public:
113 typedef typename _Alloc_traits<_Tp, _Allocator>::allocator_type
114 allocator_type;
116 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)
123 protected:
124 _Tp* _M_start;
125 _Tp* _M_finish;
126 _Tp* _M_end_of_storage;
128 typedef typename _Alloc_traits<_Tp, _Allocator>::_Alloc_type _Alloc_type;
130 _Tp*
131 _M_allocate(size_t __n) { return _Alloc_type::allocate(__n); }
133 void
134 _M_deallocate(_Tp* __p, size_t __n) { _Alloc_type::deallocate(__p, __n);}
139 * @if maint
140 * See bits/stl_deque.h's _Deque_base for an explanation.
141 * @endif
143 template <typename _Tp, typename _Alloc>
144 struct _Vector_base
145 : public _Vector_alloc_base<_Tp, _Alloc,
146 _Alloc_traits<_Tp, _Alloc>::_S_instanceless>
148 public:
149 typedef _Vector_alloc_base<_Tp, _Alloc,
150 _Alloc_traits<_Tp, _Alloc>::_S_instanceless>
151 _Base;
152 typedef typename _Base::allocator_type allocator_type;
154 _Vector_base(const allocator_type& __a)
155 : _Base(__a) {}
156 _Vector_base(size_t __n, const allocator_type& __a)
157 : _Base(__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
173 * @ingroup Sequences
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 <typename _Tp, typename _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;
195 public:
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>
201 const_iterator;
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;
210 protected:
211 /** @if maint
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
215 * allocation scheme.
216 * @endif
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;
224 public:
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.
230 explicit
231 vector(const allocator_type& __a = allocator_type())
232 : _Base(__a) {}
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())
243 : _Base(__n, __a)
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.
253 explicit
254 vector(size_type __n)
255 : _Base(__n, allocator_type())
256 { _M_finish = uninitialized_fill_n(_M_start, __n, value_type()); }
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 * Create 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 <typename _InputIterator>
284 vector(_InputIterator __first, _InputIterator __last,
285 const allocator_type& __a = allocator_type())
286 : _Base(__a)
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.
308 vector&
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.
321 void
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<typename _InputIterator>
337 void
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.
346 allocator_type
347 get_allocator() const { return _Base::get_allocator(); }
349 // iterators
351 * Returns a read/write iterator that points to the first element in the
352 * %vector. Iteration is done in ordinary element order.
354 iterator
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.
361 const_iterator
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.
368 iterator
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.
375 const_iterator
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.
382 reverse_iterator
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
395 * order.
397 reverse_iterator
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
403 * element order.
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. */
410 size_type
411 size() const { return size_type(end() - begin()); }
413 /** Returns the size() of the largest possible %vector. */
414 size_type
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.
427 void
428 resize(size_type __new_size, const value_type& __x)
430 if (__new_size < size())
431 erase(begin() + __new_size, end());
432 else
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.
445 void
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.
452 size_type
453 capacity() const
454 { return size_type(const_iterator(_M_end_of_storage) - begin()); }
457 * Returns true if the %vector is empty. (Thus begin() would equal end().)
459 bool
460 empty() const { return begin() == end(); }
463 * @brief Attempt to preallocate enough memory for specified number of
464 * elements.
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.
477 void
478 reserve(size_type __n);
480 // element access
482 * @brief Subscript access to the data contained in the %vector.
483 * @param n The index of the element for which data should be accessed.
484 * @return Read/write reference to data.
486 * This operator allows for easy, array-style, data access.
487 * Note that data access with this operator is unchecked and out_of_range
488 * lookups are not defined. (For checked lookups see at().)
490 reference
491 operator[](size_type __n) { return *(begin() + __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-only (constant) 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().)
502 const_reference
503 operator[](size_type __n) const { return *(begin() + __n); }
505 protected:
506 /// @if maint Safety check used only from at(). @endif
507 void
508 _M_range_check(size_type __n) const
510 if (__n >= this->size())
511 __throw_out_of_range("vector [] access out of range");
514 public:
516 * @brief Provides access to the data contained in the %vector.
517 * @param n The index of the element for which data should be accessed.
518 * @return Read/write reference to data.
519 * @throw std::out_of_range If @a n is an invalid index.
521 * This function provides for safer data access. The parameter is first
522 * checked that it is in the range of the vector. The function throws
523 * out_of_range if the check fails.
525 reference
526 at(size_type __n) { _M_range_check(__n); return (*this)[__n]; }
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-only (constant) 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.
538 const_reference
539 at(size_type __n) const { _M_range_check(__n); return (*this)[__n]; }
542 * Returns a read/write reference to the data at the first element of the
543 * %vector.
545 reference
546 front() { return *begin(); }
549 * Returns a read-only (constant) reference to the data at the first
550 * element of the %vector.
552 const_reference
553 front() const { return *begin(); }
556 * Returns a read/write reference to the data at the last element of the
557 * %vector.
559 reference
560 back() { return *(end() - 1); }
563 * Returns a read-only (constant) reference to the data at the last
564 * element of the %vector.
566 const_reference
567 back() const { return *(end() - 1); }
569 // [23.2.4.3] modifiers
571 * @brief Add data to the end of the %vector.
572 * @param x Data to be added.
574 * This is a typical stack operation. The function creates an element at
575 * the end of the %vector and assigns the given data to it.
576 * Due to the nature of a %vector this operation can be done in constant
577 * time if the %vector has preallocated space available.
579 void
580 push_back(const value_type& __x)
582 if (_M_finish != _M_end_of_storage)
584 _Construct(_M_finish, __x);
585 ++_M_finish;
587 else
588 _M_insert_aux(end(), __x);
592 * @brief Removes last element.
594 * This is a typical stack operation. It shrinks the %vector by one.
596 * Note that no data is returned, and if the last element's data is
597 * needed, it should be retrieved before pop_back() is called.
599 void
600 pop_back()
602 --_M_finish;
603 _Destroy(_M_finish);
607 * @brief Inserts given value into %vector before specified iterator.
608 * @param position An iterator into the %vector.
609 * @param x Data to be inserted.
610 * @return An iterator that points to the inserted data.
612 * This function will insert a copy of the given value before the specified
613 * location.
614 * Note that this kind of operation could be expensive for a %vector and if
615 * it is frequently used the user should consider using std::list.
617 iterator
618 insert(iterator __position, const value_type& __x);
620 #ifdef _GLIBCPP_DEPRECATED
622 * @brief Inserts an element into the %vector.
623 * @param position An iterator into the %vector.
624 * @return An iterator that points to the inserted element.
626 * This function will insert a default-constructed element before the
627 * specified location. You should consider using
628 * insert(position,value_type()) instead.
629 * Note that this kind of operation could be expensive for a vector and if
630 * it is frequently used the user should consider using std::list.
632 * @note This was deprecated in 3.2 and will be removed in 3.4. You must
633 * define @c _GLIBCPP_DEPRECATED to make this visible in 3.2; see
634 * c++config.h.
636 iterator
637 insert(iterator __position)
638 { return insert(__position, value_type()); }
639 #endif
642 * @brief Inserts a number of copies of given data into the %vector.
643 * @param position An iterator into the %vector.
644 * @param n Number of elements to be inserted.
645 * @param x Data to be inserted.
647 * This function will insert a specified number of copies of the given data
648 * before the location specified by @a position.
650 * Note that this kind of operation could be expensive for a %vector and if
651 * it is frequently used the user should consider using std::list.
653 void
654 insert (iterator __pos, size_type __n, const value_type& __x)
655 { _M_fill_insert(__pos, __n, __x); }
658 * @brief Inserts a range into the %vector.
659 * @param pos An iterator into the %vector.
660 * @param first An input iterator.
661 * @param last An input iterator.
663 * This function will insert copies of the data in the range [first,last)
664 * into the %vector before the location specified by @a pos.
666 * Note that this kind of operation could be expensive for a %vector and if
667 * it is frequently used the user should consider using std::list.
669 template<typename _InputIterator>
670 void
671 insert(iterator __pos, _InputIterator __first, _InputIterator __last)
673 // Check whether it's an integral type. If so, it's not an iterator.
674 typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
675 _M_insert_dispatch(__pos, __first, __last, _Integral());
679 * @brief Remove element at given position.
680 * @param position Iterator pointing to element to be erased.
681 * @return An iterator pointing to the next element (or end()).
683 * This function will erase the element at the given position and thus
684 * shorten the %vector by one.
686 * Note This operation could be expensive and if it is frequently used the
687 * user should consider using std::list. The user is also cautioned that
688 * this function only erases the element, and that if the element is itself
689 * a pointer, the pointed-to memory is not touched in any way. Managing
690 * the pointer is the user's responsibilty.
692 iterator
693 erase(iterator __position);
696 * @brief Remove a range of elements.
697 * @param first Iterator pointing to the first element to be erased.
698 * @param last Iterator pointing to one past the last element to be erased.
699 * @return An iterator pointing to the element pointed to by @a last
700 * prior to erasing (or end()).
702 * This function will erase the elements in the range [first,last) and
703 * shorten the %vector accordingly.
705 * Note This operation could be expensive and if it is frequently used the
706 * user should consider using std::list. The user is also cautioned that
707 * this function only erases the elements, and that if the elements
708 * themselves are pointers, the pointed-to memory is not touched in any
709 * way. Managing the pointer is the user's responsibilty.
711 iterator
712 erase(iterator __first, iterator __last);
715 * @brief Swaps data with another %vector.
716 * @param x A %vector of the same element and allocator types.
718 * This exchanges the elements between two vectors in constant time.
719 * (Three pointers, so it should be quite fast.)
720 * Note that the global std::swap() function is specialized such that
721 * std::swap(v1,v2) will feed to this function.
723 void
724 swap(vector& __x)
726 std::swap(_M_start, __x._M_start);
727 std::swap(_M_finish, __x._M_finish);
728 std::swap(_M_end_of_storage, __x._M_end_of_storage);
732 * Erases all the elements. Note that this function only erases the
733 * elements, and that if the elements themselves are pointers, the
734 * pointed-to memory is not touched in any way. Managing the pointer is
735 * the user's responsibilty.
737 void
738 clear() { erase(begin(), end()); }
740 protected:
742 * @if maint
743 * Memory expansion handler. Uses the member allocation function to
744 * obtain @a n bytes of memory, and then copies [first,last) into it.
745 * @endif
747 template <typename _ForwardIterator>
748 pointer
749 _M_allocate_and_copy(size_type __n,
750 _ForwardIterator __first, _ForwardIterator __last)
752 pointer __result = _M_allocate(__n);
755 uninitialized_copy(__first, __last, __result);
756 return __result;
758 catch(...)
760 _M_deallocate(__result, __n);
761 __throw_exception_again;
766 // Internal constructor functions follow.
768 // called by the range constructor to implement [23.1.1]/9
769 template<typename _Integer>
770 void
771 _M_initialize_dispatch(_Integer __n, _Integer __value, __true_type)
773 _M_start = _M_allocate(__n);
774 _M_end_of_storage = _M_start + __n;
775 _M_finish = uninitialized_fill_n(_M_start, __n, __value);
778 // called by the range constructor to implement [23.1.1]/9
779 template<typename _InputIter>
780 void
781 _M_initialize_dispatch(_InputIter __first, _InputIter __last, __false_type)
783 typedef typename iterator_traits<_InputIter>::iterator_category
784 _IterCategory;
785 _M_range_initialize(__first, __last, _IterCategory());
788 // called by the second initialize_dispatch above
789 template <typename _InputIterator>
790 void
791 _M_range_initialize(_InputIterator __first,
792 _InputIterator __last, input_iterator_tag)
794 for ( ; __first != __last; ++__first)
795 push_back(*__first);
798 // called by the second initialize_dispatch above
799 template <typename _ForwardIterator>
800 void _M_range_initialize(_ForwardIterator __first,
801 _ForwardIterator __last, forward_iterator_tag)
803 size_type __n = distance(__first, __last);
804 _M_start = _M_allocate(__n);
805 _M_end_of_storage = _M_start + __n;
806 _M_finish = uninitialized_copy(__first, __last, _M_start);
810 // Internal assign functions follow. The *_aux functions do the actual
811 // assignment work for the range versions.
813 // called by the range assign to implement [23.1.1]/9
814 template<typename _Integer>
815 void
816 _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
818 _M_fill_assign(static_cast<size_type>(__n),
819 static_cast<value_type>(__val));
822 // called by the range assign to implement [23.1.1]/9
823 template<typename _InputIter>
824 void
825 _M_assign_dispatch(_InputIter __first, _InputIter __last, __false_type)
827 typedef typename iterator_traits<_InputIter>::iterator_category
828 _IterCategory;
829 _M_assign_aux(__first, __last, _IterCategory());
832 // called by the second assign_dispatch above
833 template <typename _InputIterator>
834 void
835 _M_assign_aux(_InputIterator __first, _InputIterator __last,
836 input_iterator_tag);
838 // called by the second assign_dispatch above
839 template <typename _ForwardIterator>
840 void
841 _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
842 forward_iterator_tag);
844 // Called by assign(n,t), and the range assign when it turns out to be the
845 // same thing.
846 void
847 _M_fill_assign(size_type __n, const value_type& __val);
850 // Internal insert functions follow.
852 // called by the range insert to implement [23.1.1]/9
853 template<typename _Integer>
854 void
855 _M_insert_dispatch(iterator __pos, _Integer __n, _Integer __val,
856 __true_type)
858 _M_fill_insert(__pos, static_cast<size_type>(__n),
859 static_cast<value_type>(__val));
862 // called by the range insert to implement [23.1.1]/9
863 template<typename _InputIterator>
864 void
865 _M_insert_dispatch(iterator __pos, _InputIterator __first,
866 _InputIterator __last, __false_type)
868 typedef typename iterator_traits<_InputIterator>::iterator_category
869 _IterCategory;
870 _M_range_insert(__pos, __first, __last, _IterCategory());
873 // called by the second insert_dispatch above
874 template <typename _InputIterator>
875 void
876 _M_range_insert(iterator __pos,
877 _InputIterator __first, _InputIterator __last,
878 input_iterator_tag);
880 // called by the second insert_dispatch above
881 template <typename _ForwardIterator>
882 void
883 _M_range_insert(iterator __pos,
884 _ForwardIterator __first, _ForwardIterator __last,
885 forward_iterator_tag);
887 // Called by insert(p,n,x), and the range insert when it turns out to be
888 // the same thing.
889 void
890 _M_fill_insert (iterator __pos, size_type __n, const value_type& __x);
892 // called by insert(p,x)
893 void
894 _M_insert_aux(iterator __position, const value_type& __x);
896 #ifdef _GLIBCPP_DEPRECATED
897 // unused now (same situation as in deque)
898 void _M_insert_aux(iterator __position);
899 #endif
904 * @brief Vector equality comparison.
905 * @param x A %vector.
906 * @param y A %vector of the same type as @a x.
907 * @return True iff the size and elements of the vectors are equal.
909 * This is an equivalence relation. It is linear in the size of the
910 * vectors. Vectors are considered equivalent if their sizes are equal,
911 * and if corresponding elements compare equal.
913 template <typename _Tp, typename _Alloc>
914 inline bool
915 operator==(const vector<_Tp,_Alloc>& __x, const vector<_Tp,_Alloc>& __y)
917 return __x.size() == __y.size() &&
918 equal(__x.begin(), __x.end(), __y.begin());
922 * @brief Vector ordering relation.
923 * @param x A %vector.
924 * @param y A %vector of the same type as @a x.
925 * @return True iff @a x is lexographically less than @a y.
927 * This is a total ordering relation. It is linear in the size of the
928 * vectors. The elements must be comparable with @c <.
930 * See std::lexographical_compare() for how the determination is made.
932 template <typename _Tp, typename _Alloc>
933 inline bool
934 operator<(const vector<_Tp,_Alloc>& __x, const vector<_Tp,_Alloc>& __y)
936 return lexicographical_compare(__x.begin(), __x.end(),
937 __y.begin(), __y.end());
940 /// Based on operator==
941 template <typename _Tp, typename _Alloc>
942 inline bool
943 operator!=(const vector<_Tp,_Alloc>& __x, const vector<_Tp,_Alloc>& __y)
944 { return !(__x == __y); }
946 /// Based on operator<
947 template <typename _Tp, typename _Alloc>
948 inline bool
949 operator>(const vector<_Tp,_Alloc>& __x, const vector<_Tp,_Alloc>& __y)
950 { return __y < __x; }
952 /// Based on operator<
953 template <typename _Tp, typename _Alloc>
954 inline bool
955 operator<=(const vector<_Tp,_Alloc>& __x, const vector<_Tp,_Alloc>& __y)
956 { return !(__y < __x); }
958 /// Based on operator<
959 template <typename _Tp, typename _Alloc>
960 inline bool
961 operator>=(const vector<_Tp,_Alloc>& __x, const vector<_Tp,_Alloc>& __y)
962 { return !(__x < __y); }
964 /// See std::vector::swap().
965 template <typename _Tp, typename _Alloc>
966 inline void
967 swap(vector<_Tp,_Alloc>& __x, vector<_Tp,_Alloc>& __y)
968 { __x.swap(__y); }
970 } // namespace std
972 #endif /* __GLIBCPP_INTERNAL_VECTOR_H */