* fold-const.c (fold_unary): Use build1 instead of copy_node.
[official-gcc.git] / libstdc++-v3 / include / bits / stl_vector.h
blob3495d9312ae8059884b57186f32ef23b08930fee
1 // Vector implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005 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 _VECTOR_H
62 #define _VECTOR_H 1
64 #include <bits/stl_iterator_base_funcs.h>
65 #include <bits/functexcept.h>
66 #include <bits/concept_check.h>
68 namespace _GLIBCXX_STD
70 /**
71 * @if maint
72 * See bits/stl_deque.h's _Deque_base for an explanation.
73 * @endif
75 template<typename _Tp, typename _Alloc>
76 struct _Vector_base
78 struct _Vector_impl
79 : public _Alloc
81 _Tp* _M_start;
82 _Tp* _M_finish;
83 _Tp* _M_end_of_storage;
84 _Vector_impl(_Alloc const& __a)
85 : _Alloc(__a), _M_start(0), _M_finish(0), _M_end_of_storage(0)
86 { }
89 public:
90 typedef _Alloc allocator_type;
92 allocator_type
93 get_allocator() const
94 { return *static_cast<const _Alloc*>(&this->_M_impl); }
96 _Vector_base(const allocator_type& __a)
97 : _M_impl(__a)
98 { }
100 _Vector_base(size_t __n, const allocator_type& __a)
101 : _M_impl(__a)
103 this->_M_impl._M_start = this->_M_allocate(__n);
104 this->_M_impl._M_finish = this->_M_impl._M_start;
105 this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __n;
108 ~_Vector_base()
109 { _M_deallocate(this->_M_impl._M_start, this->_M_impl._M_end_of_storage
110 - this->_M_impl._M_start); }
112 public:
113 _Vector_impl _M_impl;
115 _Tp*
116 _M_allocate(size_t __n)
117 { return _M_impl.allocate(__n); }
119 void
120 _M_deallocate(_Tp* __p, size_t __n)
122 if (__p)
123 _M_impl.deallocate(__p, __n);
129 * @brief A standard container which offers fixed time access to
130 * individual elements in any order.
132 * @ingroup Containers
133 * @ingroup Sequences
135 * Meets the requirements of a <a href="tables.html#65">container</a>, a
136 * <a href="tables.html#66">reversible container</a>, and a
137 * <a href="tables.html#67">sequence</a>, including the
138 * <a href="tables.html#68">optional sequence requirements</a> with the
139 * %exception of @c push_front and @c pop_front.
141 * In some terminology a %vector can be described as a dynamic
142 * C-style array, it offers fast and efficient access to individual
143 * elements in any order and saves the user from worrying about
144 * memory and size allocation. Subscripting ( @c [] ) access is
145 * also provided as with C-style arrays.
147 template<typename _Tp, typename _Alloc = allocator<_Tp> >
148 class vector : protected _Vector_base<_Tp, _Alloc>
150 // Concept requirements.
151 __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
153 typedef _Vector_base<_Tp, _Alloc> _Base;
154 typedef vector<_Tp, _Alloc> vector_type;
156 public:
157 typedef _Tp value_type;
158 typedef typename _Alloc::pointer pointer;
159 typedef typename _Alloc::const_pointer const_pointer;
160 typedef typename _Alloc::reference reference;
161 typedef typename _Alloc::const_reference const_reference;
162 typedef __gnu_cxx::__normal_iterator<pointer, vector_type> iterator;
163 typedef __gnu_cxx::__normal_iterator<const_pointer, vector_type>
164 const_iterator;
165 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
166 typedef std::reverse_iterator<iterator> reverse_iterator;
167 typedef size_t size_type;
168 typedef ptrdiff_t difference_type;
169 typedef typename _Base::allocator_type allocator_type;
171 protected:
172 /** @if maint
173 * These two functions and three data members are all from the
174 * base class. They should be pretty self-explanatory, as
175 * %vector uses a simple contiguous allocation scheme. @endif
177 using _Base::_M_allocate;
178 using _Base::_M_deallocate;
179 using _Base::_M_impl;
181 public:
182 // [23.2.4.1] construct/copy/destroy
183 // (assign() and get_allocator() are also listed in this section)
185 * @brief Default constructor creates no elements.
187 explicit
188 vector(const allocator_type& __a = allocator_type())
189 : _Base(__a)
193 * @brief Create a %vector with copies of an exemplar element.
194 * @param n The number of elements to initially create.
195 * @param value An element to copy.
197 * This constructor fills the %vector with @a n copies of @a value.
199 vector(size_type __n, const value_type& __value,
200 const allocator_type& __a = allocator_type())
201 : _Base(__n, __a)
203 std::__uninitialized_fill_n_a(this->_M_impl._M_start, __n, __value,
204 this->get_allocator());
205 this->_M_impl._M_finish = this->_M_impl._M_start + __n;
209 * @brief Create a %vector with default elements.
210 * @param n The number of elements to initially create.
212 * This constructor fills the %vector with @a n copies of a
213 * default-constructed element.
215 explicit
216 vector(size_type __n)
217 : _Base(__n, allocator_type())
219 std::__uninitialized_fill_n_a(this->_M_impl._M_start, __n, value_type(),
220 this->get_allocator());
221 this->_M_impl._M_finish = this->_M_impl._M_start + __n;
225 * @brief %Vector copy constructor.
226 * @param x A %vector of identical element and allocator types.
228 * The newly-created %vector uses a copy of the allocation
229 * object used by @a x. All the elements of @a x are copied,
230 * but any extra memory in
231 * @a x (for fast expansion) will not be copied.
233 vector(const vector& __x)
234 : _Base(__x.size(), __x.get_allocator())
235 { this->_M_impl._M_finish =
236 std::__uninitialized_copy_a(__x.begin(), __x.end(),
237 this->_M_impl._M_start,
238 this->get_allocator());
242 * @brief Builds a %vector from a range.
243 * @param first An input iterator.
244 * @param last An input iterator.
246 * Create a %vector consisting of copies of the elements from
247 * [first,last).
249 * If the iterators are forward, bidirectional, or
250 * random-access, then this will call the elements' copy
251 * constructor N times (where N is distance(first,last)) and do
252 * no memory reallocation. But if only input iterators are
253 * used, then this will do at most 2N calls to the copy
254 * constructor, and logN memory reallocations.
256 template<typename _InputIterator>
257 vector(_InputIterator __first, _InputIterator __last,
258 const allocator_type& __a = allocator_type())
259 : _Base(__a)
261 // Check whether it's an integral type. If so, it's not an iterator.
262 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
263 _M_initialize_dispatch(__first, __last, _Integral());
267 * The dtor only erases the elements, and note that if the
268 * elements themselves are pointers, the pointed-to memory is
269 * not touched in any way. Managing the pointer is the user's
270 * responsibilty.
272 ~vector()
273 { std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
274 this->get_allocator());
278 * @brief %Vector assignment operator.
279 * @param x A %vector of identical element and allocator types.
281 * All the elements of @a x are copied, but any extra memory in
282 * @a x (for fast expansion) will not be copied. Unlike the
283 * copy constructor, the allocator object is not copied.
285 vector&
286 operator=(const vector& __x);
289 * @brief Assigns a given value to a %vector.
290 * @param n Number of elements to be assigned.
291 * @param val Value to be assigned.
293 * This function fills a %vector with @a n copies of the given
294 * value. Note that the assignment completely changes the
295 * %vector and that the resulting %vector's size is the same as
296 * the number of elements assigned. Old data may be lost.
298 void
299 assign(size_type __n, const value_type& __val)
300 { _M_fill_assign(__n, __val); }
303 * @brief Assigns a range to a %vector.
304 * @param first An input iterator.
305 * @param last An input iterator.
307 * This function fills a %vector with copies of the elements in the
308 * range [first,last).
310 * Note that the assignment completely changes the %vector and
311 * that the resulting %vector's size is the same as the number
312 * of elements assigned. Old data may be lost.
314 template<typename _InputIterator>
315 void
316 assign(_InputIterator __first, _InputIterator __last)
318 // Check whether it's an integral type. If so, it's not an iterator.
319 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
320 _M_assign_dispatch(__first, __last, _Integral());
323 /// Get a copy of the memory allocation object.
324 using _Base::get_allocator;
326 // iterators
328 * Returns a read/write iterator that points to the first
329 * element in the %vector. Iteration is done in ordinary
330 * element order.
332 iterator
333 begin()
334 { return iterator (this->_M_impl._M_start); }
337 * Returns a read-only (constant) iterator that points to the
338 * first element in the %vector. Iteration is done in ordinary
339 * element order.
341 const_iterator
342 begin() const
343 { return const_iterator (this->_M_impl._M_start); }
346 * Returns a read/write iterator that points one past the last
347 * element in the %vector. Iteration is done in ordinary
348 * element order.
350 iterator
351 end()
352 { return iterator (this->_M_impl._M_finish); }
355 * Returns a read-only (constant) iterator that points one past
356 * the last element in the %vector. Iteration is done in
357 * ordinary element order.
359 const_iterator
360 end() const
361 { return const_iterator (this->_M_impl._M_finish); }
364 * Returns a read/write reverse iterator that points to the
365 * last element in the %vector. Iteration is done in reverse
366 * element order.
368 reverse_iterator
369 rbegin()
370 { return reverse_iterator(end()); }
373 * Returns a read-only (constant) reverse iterator that points
374 * to the last element in the %vector. Iteration is done in
375 * reverse element order.
377 const_reverse_iterator
378 rbegin() const
379 { return const_reverse_iterator(end()); }
382 * Returns a read/write reverse iterator that points to one
383 * before the first element in the %vector. Iteration is done
384 * in reverse element order.
386 reverse_iterator
387 rend()
388 { return reverse_iterator(begin()); }
391 * Returns a read-only (constant) reverse iterator that points
392 * to one before the first element in the %vector. Iteration
393 * is done in reverse element order.
395 const_reverse_iterator
396 rend() const
397 { return const_reverse_iterator(begin()); }
399 // [23.2.4.2] capacity
400 /** Returns the number of elements in the %vector. */
401 size_type
402 size() const
403 { return size_type(end() - begin()); }
405 /** Returns the size() of the largest possible %vector. */
406 size_type
407 max_size() const
408 { return size_type(-1) / sizeof(value_type); }
411 * @brief Resizes the %vector to the specified number of elements.
412 * @param new_size Number of elements the %vector should contain.
413 * @param x Data with which new elements should be populated.
415 * This function will %resize the %vector to the specified
416 * number of elements. If the number is smaller than the
417 * %vector's current size the %vector is truncated, otherwise
418 * the %vector is extended and new elements are populated with
419 * given data.
421 void
422 resize(size_type __new_size, const value_type& __x)
424 if (__new_size < size())
425 erase(begin() + __new_size, end());
426 else
427 insert(end(), __new_size - size(), __x);
431 * @brief Resizes the %vector to the specified number of elements.
432 * @param new_size Number of elements the %vector should contain.
434 * This function will resize the %vector to the specified
435 * number of elements. If the number is smaller than the
436 * %vector's current size the %vector is truncated, otherwise
437 * the %vector is extended and new elements are
438 * default-constructed.
440 void
441 resize(size_type __new_size)
442 { resize(__new_size, value_type()); }
445 * Returns the total number of elements that the %vector can
446 * hold before needing to allocate more memory.
448 size_type
449 capacity() const
450 { return size_type(const_iterator(this->_M_impl._M_end_of_storage)
451 - begin()); }
454 * Returns true if the %vector is empty. (Thus begin() would
455 * equal end().)
457 bool
458 empty() const
459 { return begin() == end(); }
462 * @brief Attempt to preallocate enough memory for specified number of
463 * elements.
464 * @param n Number of elements required.
465 * @throw std::length_error If @a n exceeds @c max_size().
467 * This function attempts to reserve enough memory for the
468 * %vector to hold the specified number of elements. If the
469 * number requested is more than max_size(), length_error is
470 * thrown.
472 * The advantage of this function is that if optimal code is a
473 * necessity and the user can determine the number of elements
474 * that will be required, the user can reserve the memory in
475 * %advance, and thus prevent a possible reallocation of memory
476 * and copying of %vector data.
478 void
479 reserve(size_type __n);
481 // element access
483 * @brief Subscript access to the data contained in the %vector.
484 * @param n The index of the element for which data should be
485 * accessed.
486 * @return Read/write reference to data.
488 * This operator allows for easy, array-style, data access.
489 * Note that data access with this operator is unchecked and
490 * out_of_range lookups are not defined. (For checked lookups
491 * see at().)
493 reference
494 operator[](size_type __n)
495 { return *(begin() + __n); }
498 * @brief Subscript access to the data contained in the %vector.
499 * @param n The index of the element for which data should be
500 * accessed.
501 * @return Read-only (constant) reference to data.
503 * This operator allows for easy, array-style, data access.
504 * Note that data access with this operator is unchecked and
505 * out_of_range lookups are not defined. (For checked lookups
506 * see at().)
508 const_reference
509 operator[](size_type __n) const
510 { return *(begin() + __n); }
512 protected:
513 /// @if maint Safety check used only from at(). @endif
514 void
515 _M_range_check(size_type __n) const
517 if (__n >= this->size())
518 __throw_out_of_range(__N("vector::_M_range_check"));
521 public:
523 * @brief Provides access to the data contained in the %vector.
524 * @param n The index of the element for which data should be
525 * accessed.
526 * @return Read/write reference to data.
527 * @throw std::out_of_range If @a n is an invalid index.
529 * This function provides for safer data access. The parameter
530 * is first checked that it is in the range of the vector. The
531 * function throws out_of_range if the check fails.
533 reference
534 at(size_type __n)
536 _M_range_check(__n);
537 return (*this)[__n];
541 * @brief Provides access to the data contained in the %vector.
542 * @param n The index of the element for which data should be
543 * accessed.
544 * @return Read-only (constant) reference to data.
545 * @throw std::out_of_range If @a n is an invalid index.
547 * This function provides for safer data access. The parameter
548 * is first checked that it is in the range of the vector. The
549 * function throws out_of_range if the check fails.
551 const_reference
552 at(size_type __n) const
554 _M_range_check(__n);
555 return (*this)[__n];
559 * Returns a read/write reference to the data at the first
560 * element of the %vector.
562 reference
563 front()
564 { return *begin(); }
567 * Returns a read-only (constant) reference to the data at the first
568 * element of the %vector.
570 const_reference
571 front() const
572 { return *begin(); }
575 * Returns a read/write reference to the data at the last
576 * element of the %vector.
578 reference
579 back()
580 { return *(end() - 1); }
583 * Returns a read-only (constant) reference to the data at the
584 * last element of the %vector.
586 const_reference
587 back() const
588 { return *(end() - 1); }
590 // [23.2.4.3] modifiers
592 * @brief Add data to the end of the %vector.
593 * @param x Data to be added.
595 * This is a typical stack operation. The function creates an
596 * element at the end of the %vector and assigns the given data
597 * to it. Due to the nature of a %vector this operation can be
598 * done in constant time if the %vector has preallocated space
599 * available.
601 void
602 push_back(const value_type& __x)
604 if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage)
606 this->_M_impl.construct(this->_M_impl._M_finish, __x);
607 ++this->_M_impl._M_finish;
609 else
610 _M_insert_aux(end(), __x);
614 * @brief Removes last element.
616 * This is a typical stack operation. It shrinks the %vector by one.
618 * Note that no data is returned, and if the last element's
619 * data is needed, it should be retrieved before pop_back() is
620 * called.
622 void
623 pop_back()
625 --this->_M_impl._M_finish;
626 this->_M_impl.destroy(this->_M_impl._M_finish);
630 * @brief Inserts given value into %vector before specified iterator.
631 * @param position An iterator into the %vector.
632 * @param x Data to be inserted.
633 * @return An iterator that points to the inserted data.
635 * This function will insert a copy of the given value before
636 * the specified location. Note that this kind of operation
637 * could be expensive for a %vector and if it is frequently
638 * used the user should consider using std::list.
640 iterator
641 insert(iterator __position, const value_type& __x);
644 * @brief Inserts a number of copies of given data into the %vector.
645 * @param position An iterator into the %vector.
646 * @param n Number of elements to be inserted.
647 * @param x Data to be inserted.
649 * This function will insert a specified number of copies of
650 * the given data before the location specified by @a position.
652 * Note that this kind of operation could be expensive for a
653 * %vector and if it is frequently used the user should
654 * consider using std::list.
656 void
657 insert(iterator __position, size_type __n, const value_type& __x)
658 { _M_fill_insert(__position, __n, __x); }
661 * @brief Inserts a range into the %vector.
662 * @param position An iterator into the %vector.
663 * @param first An input iterator.
664 * @param last An input iterator.
666 * This function will insert copies of the data in the range
667 * [first,last) into the %vector before the location specified
668 * by @a pos.
670 * Note that this kind of operation could be expensive for a
671 * %vector and if it is frequently used the user should
672 * consider using std::list.
674 template<typename _InputIterator>
675 void
676 insert(iterator __position, _InputIterator __first,
677 _InputIterator __last)
679 // Check whether it's an integral type. If so, it's not an iterator.
680 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
681 _M_insert_dispatch(__position, __first, __last, _Integral());
685 * @brief Remove element at given position.
686 * @param position Iterator pointing to element to be erased.
687 * @return An iterator pointing to the next element (or end()).
689 * This function will erase the element at the given position and thus
690 * shorten the %vector by one.
692 * Note This operation could be expensive and if it is
693 * frequently used the user should consider using std::list.
694 * The user is also cautioned that this function only erases
695 * the element, and that if the element is itself a pointer,
696 * the pointed-to memory is not touched in any way. Managing
697 * the pointer is the user's responsibilty.
699 iterator
700 erase(iterator __position);
703 * @brief Remove a range of elements.
704 * @param first Iterator pointing to the first element to be erased.
705 * @param last Iterator pointing to one past the last element to be
706 * erased.
707 * @return An iterator pointing to the element pointed to by @a last
708 * prior to erasing (or end()).
710 * This function will erase the elements in the range [first,last) and
711 * shorten the %vector accordingly.
713 * Note This operation could be expensive and if it is
714 * frequently used the user should consider using std::list.
715 * The user is also cautioned that this function only erases
716 * the elements, and that if the elements themselves are
717 * pointers, the pointed-to memory is not touched in any way.
718 * Managing the pointer is the user's responsibilty.
720 iterator
721 erase(iterator __first, iterator __last);
724 * @brief Swaps data with another %vector.
725 * @param x A %vector of the same element and allocator types.
727 * This exchanges the elements between two vectors in constant time.
728 * (Three pointers, so it should be quite fast.)
729 * Note that the global std::swap() function is specialized such that
730 * std::swap(v1,v2) will feed to this function.
732 void
733 swap(vector& __x)
735 std::swap(this->_M_impl._M_start, __x._M_impl._M_start);
736 std::swap(this->_M_impl._M_finish, __x._M_impl._M_finish);
737 std::swap(this->_M_impl._M_end_of_storage,
738 __x._M_impl._M_end_of_storage);
742 * Erases all the elements. Note that this function only erases the
743 * elements, and that if the elements themselves are pointers, the
744 * pointed-to memory is not touched in any way. Managing the pointer is
745 * the user's responsibilty.
747 void
748 clear()
749 { erase(begin(), end()); }
751 protected:
753 * @if maint
754 * Memory expansion handler. Uses the member allocation function to
755 * obtain @a n bytes of memory, and then copies [first,last) into it.
756 * @endif
758 template<typename _ForwardIterator>
759 pointer
760 _M_allocate_and_copy(size_type __n,
761 _ForwardIterator __first, _ForwardIterator __last)
763 pointer __result = this->_M_allocate(__n);
766 std::__uninitialized_copy_a(__first, __last, __result,
767 this->get_allocator());
768 return __result;
770 catch(...)
772 _M_deallocate(__result, __n);
773 __throw_exception_again;
778 // Internal constructor functions follow.
780 // Called by the range constructor to implement [23.1.1]/9
781 template<typename _Integer>
782 void
783 _M_initialize_dispatch(_Integer __n, _Integer __value, __true_type)
785 this->_M_impl._M_start = _M_allocate(__n);
786 this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __n;
787 std::__uninitialized_fill_n_a(this->_M_impl._M_start, __n, __value,
788 this->get_allocator());
789 this->_M_impl._M_finish = this->_M_impl._M_end_of_storage;
792 // Called by the range constructor to implement [23.1.1]/9
793 template<typename _InputIterator>
794 void
795 _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
796 __false_type)
798 typedef typename iterator_traits<_InputIterator>::iterator_category
799 _IterCategory;
800 _M_range_initialize(__first, __last, _IterCategory());
803 // Called by the second initialize_dispatch above
804 template<typename _InputIterator>
805 void
806 _M_range_initialize(_InputIterator __first,
807 _InputIterator __last, input_iterator_tag)
809 for (; __first != __last; ++__first)
810 push_back(*__first);
813 // Called by the second initialize_dispatch above
814 template<typename _ForwardIterator>
815 void
816 _M_range_initialize(_ForwardIterator __first,
817 _ForwardIterator __last, forward_iterator_tag)
819 const size_type __n = std::distance(__first, __last);
820 this->_M_impl._M_start = this->_M_allocate(__n);
821 this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __n;
822 this->_M_impl._M_finish =
823 std::__uninitialized_copy_a(__first, __last,
824 this->_M_impl._M_start,
825 this->get_allocator());
829 // Internal assign functions follow. The *_aux functions do the actual
830 // assignment work for the range versions.
832 // Called by the range assign to implement [23.1.1]/9
833 template<typename _Integer>
834 void
835 _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
837 _M_fill_assign(static_cast<size_type>(__n),
838 static_cast<value_type>(__val));
841 // Called by the range assign to implement [23.1.1]/9
842 template<typename _InputIterator>
843 void
844 _M_assign_dispatch(_InputIterator __first, _InputIterator __last,
845 __false_type)
847 typedef typename iterator_traits<_InputIterator>::iterator_category
848 _IterCategory;
849 _M_assign_aux(__first, __last, _IterCategory());
852 // Called by the second assign_dispatch above
853 template<typename _InputIterator>
854 void
855 _M_assign_aux(_InputIterator __first, _InputIterator __last,
856 input_iterator_tag);
858 // Called by the second assign_dispatch above
859 template<typename _ForwardIterator>
860 void
861 _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
862 forward_iterator_tag);
864 // Called by assign(n,t), and the range assign when it turns out
865 // to be the same thing.
866 void
867 _M_fill_assign(size_type __n, const value_type& __val);
870 // Internal insert functions follow.
872 // Called by the range insert to implement [23.1.1]/9
873 template<typename _Integer>
874 void
875 _M_insert_dispatch(iterator __pos, _Integer __n, _Integer __val,
876 __true_type)
878 _M_fill_insert(__pos, static_cast<size_type>(__n),
879 static_cast<value_type>(__val));
882 // Called by the range insert to implement [23.1.1]/9
883 template<typename _InputIterator>
884 void
885 _M_insert_dispatch(iterator __pos, _InputIterator __first,
886 _InputIterator __last, __false_type)
888 typedef typename iterator_traits<_InputIterator>::iterator_category
889 _IterCategory;
890 _M_range_insert(__pos, __first, __last, _IterCategory());
893 // Called by the second insert_dispatch above
894 template<typename _InputIterator>
895 void
896 _M_range_insert(iterator __pos, _InputIterator __first,
897 _InputIterator __last, input_iterator_tag);
899 // Called by the second insert_dispatch above
900 template<typename _ForwardIterator>
901 void
902 _M_range_insert(iterator __pos, _ForwardIterator __first,
903 _ForwardIterator __last, forward_iterator_tag);
905 // Called by insert(p,n,x), and the range insert when it turns out to be
906 // the same thing.
907 void
908 _M_fill_insert(iterator __pos, size_type __n, const value_type& __x);
910 // Called by insert(p,x)
911 void
912 _M_insert_aux(iterator __position, const value_type& __x);
917 * @brief Vector equality comparison.
918 * @param x A %vector.
919 * @param y A %vector of the same type as @a x.
920 * @return True iff the size and elements of the vectors are equal.
922 * This is an equivalence relation. It is linear in the size of the
923 * vectors. Vectors are considered equivalent if their sizes are equal,
924 * and if corresponding elements compare equal.
926 template<typename _Tp, typename _Alloc>
927 inline bool
928 operator==(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
929 { return (__x.size() == __y.size()
930 && std::equal(__x.begin(), __x.end(), __y.begin())); }
933 * @brief Vector ordering relation.
934 * @param x A %vector.
935 * @param y A %vector of the same type as @a x.
936 * @return True iff @a x is lexicographically less than @a y.
938 * This is a total ordering relation. It is linear in the size of the
939 * vectors. The elements must be comparable with @c <.
941 * See std::lexicographical_compare() for how the determination is made.
943 template<typename _Tp, typename _Alloc>
944 inline bool
945 operator<(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
946 { return std::lexicographical_compare(__x.begin(), __x.end(),
947 __y.begin(), __y.end()); }
949 /// Based on operator==
950 template<typename _Tp, typename _Alloc>
951 inline bool
952 operator!=(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
953 { return !(__x == __y); }
955 /// Based on operator<
956 template<typename _Tp, typename _Alloc>
957 inline bool
958 operator>(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
959 { return __y < __x; }
961 /// Based on operator<
962 template<typename _Tp, typename _Alloc>
963 inline bool
964 operator<=(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
965 { return !(__y < __x); }
967 /// Based on operator<
968 template<typename _Tp, typename _Alloc>
969 inline bool
970 operator>=(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
971 { return !(__x < __y); }
973 /// See std::vector::swap().
974 template<typename _Tp, typename _Alloc>
975 inline void
976 swap(vector<_Tp, _Alloc>& __x, vector<_Tp, _Alloc>& __y)
977 { __x.swap(__y); }
978 } // namespace std
980 #endif /* _VECTOR_H */