doxygroups.cc: Move algorithm groups into algorithmfwd.h.
[gcc.git] / libstdc++-v3 / include / bits / stl_vector.h
1 // Vector implementation -*- C++ -*-
2
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
4 // Free Software Foundation, Inc.
5 //
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
10 // any later version.
11
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20 // USA.
21
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
30
31 /*
32 *
33 * Copyright (c) 1994
34 * Hewlett-Packard Company
35 *
36 * Permission to use, copy, modify, distribute and sell this software
37 * and its documentation for any purpose is hereby granted without fee,
38 * provided that the above copyright notice appear in all copies and
39 * that both that copyright notice and this permission notice appear
40 * in supporting documentation. Hewlett-Packard Company makes no
41 * representations about the suitability of this software for any
42 * purpose. It is provided "as is" without express or implied warranty.
43 *
44 *
45 * Copyright (c) 1996
46 * Silicon Graphics Computer Systems, Inc.
47 *
48 * Permission to use, copy, modify, distribute and sell this software
49 * and its documentation for any purpose is hereby granted without fee,
50 * provided that the above copyright notice appear in all copies and
51 * that both that copyright notice and this permission notice appear
52 * in supporting documentation. Silicon Graphics makes no
53 * representations about the suitability of this software for any
54 * purpose. It is provided "as is" without express or implied warranty.
55 */
56
57 /** @file stl_vector.h
58 * This is an internal header file, included by other library headers.
59 * You should not attempt to use it directly.
60 */
61
62 #ifndef _STL_VECTOR_H
63 #define _STL_VECTOR_H 1
64
65 #include <bits/stl_iterator_base_funcs.h>
66 #include <bits/functexcept.h>
67 #include <bits/concept_check.h>
68 #include <initializer_list>
69
70 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D)
71
72 /// See bits/stl_deque.h's _Deque_base for an explanation.
73 template<typename _Tp, typename _Alloc>
74 struct _Vector_base
75 {
76 typedef typename _Alloc::template rebind<_Tp>::other _Tp_alloc_type;
77
78 struct _Vector_impl
79 : public _Tp_alloc_type
80 {
81 typename _Tp_alloc_type::pointer _M_start;
82 typename _Tp_alloc_type::pointer _M_finish;
83 typename _Tp_alloc_type::pointer _M_end_of_storage;
84
85 _Vector_impl()
86 : _Tp_alloc_type(), _M_start(0), _M_finish(0), _M_end_of_storage(0)
87 { }
88
89 _Vector_impl(_Tp_alloc_type const& __a)
90 : _Tp_alloc_type(__a), _M_start(0), _M_finish(0), _M_end_of_storage(0)
91 { }
92 };
93
94 public:
95 typedef _Alloc allocator_type;
96
97 _Tp_alloc_type&
98 _M_get_Tp_allocator()
99 { return *static_cast<_Tp_alloc_type*>(&this->_M_impl); }
100
101 const _Tp_alloc_type&
102 _M_get_Tp_allocator() const
103 { return *static_cast<const _Tp_alloc_type*>(&this->_M_impl); }
104
105 allocator_type
106 get_allocator() const
107 { return allocator_type(_M_get_Tp_allocator()); }
108
109 _Vector_base()
110 : _M_impl() { }
111
112 _Vector_base(const allocator_type& __a)
113 : _M_impl(__a) { }
114
115 _Vector_base(size_t __n, const allocator_type& __a)
116 : _M_impl(__a)
117 {
118 this->_M_impl._M_start = this->_M_allocate(__n);
119 this->_M_impl._M_finish = this->_M_impl._M_start;
120 this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __n;
121 }
122
123 #ifdef __GXX_EXPERIMENTAL_CXX0X__
124 _Vector_base(_Vector_base&& __x)
125 : _M_impl(__x._M_get_Tp_allocator())
126 {
127 this->_M_impl._M_start = __x._M_impl._M_start;
128 this->_M_impl._M_finish = __x._M_impl._M_finish;
129 this->_M_impl._M_end_of_storage = __x._M_impl._M_end_of_storage;
130 __x._M_impl._M_start = 0;
131 __x._M_impl._M_finish = 0;
132 __x._M_impl._M_end_of_storage = 0;
133 }
134 #endif
135
136 ~_Vector_base()
137 { _M_deallocate(this->_M_impl._M_start, this->_M_impl._M_end_of_storage
138 - this->_M_impl._M_start); }
139
140 public:
141 _Vector_impl _M_impl;
142
143 typename _Tp_alloc_type::pointer
144 _M_allocate(size_t __n)
145 { return __n != 0 ? _M_impl.allocate(__n) : 0; }
146
147 void
148 _M_deallocate(typename _Tp_alloc_type::pointer __p, size_t __n)
149 {
150 if (__p)
151 _M_impl.deallocate(__p, __n);
152 }
153 };
154
155
156 /**
157 * @brief A standard container which offers fixed time access to
158 * individual elements in any order.
159 *
160 * @ingroup containers
161 * @ingroup sequences
162 *
163 * Meets the requirements of a <a href="tables.html#65">container</a>, a
164 * <a href="tables.html#66">reversible container</a>, and a
165 * <a href="tables.html#67">sequence</a>, including the
166 * <a href="tables.html#68">optional sequence requirements</a> with the
167 * %exception of @c push_front and @c pop_front.
168 *
169 * In some terminology a %vector can be described as a dynamic
170 * C-style array, it offers fast and efficient access to individual
171 * elements in any order and saves the user from worrying about
172 * memory and size allocation. Subscripting ( @c [] ) access is
173 * also provided as with C-style arrays.
174 */
175 template<typename _Tp, typename _Alloc = std::allocator<_Tp> >
176 class vector : protected _Vector_base<_Tp, _Alloc>
177 {
178 // Concept requirements.
179 typedef typename _Alloc::value_type _Alloc_value_type;
180 __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
181 __glibcxx_class_requires2(_Tp, _Alloc_value_type, _SameTypeConcept)
182
183 typedef _Vector_base<_Tp, _Alloc> _Base;
184 typedef typename _Base::_Tp_alloc_type _Tp_alloc_type;
185
186 public:
187 typedef _Tp value_type;
188 typedef typename _Tp_alloc_type::pointer pointer;
189 typedef typename _Tp_alloc_type::const_pointer const_pointer;
190 typedef typename _Tp_alloc_type::reference reference;
191 typedef typename _Tp_alloc_type::const_reference const_reference;
192 typedef __gnu_cxx::__normal_iterator<pointer, vector> iterator;
193 typedef __gnu_cxx::__normal_iterator<const_pointer, vector>
194 const_iterator;
195 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
196 typedef std::reverse_iterator<iterator> reverse_iterator;
197 typedef size_t size_type;
198 typedef ptrdiff_t difference_type;
199 typedef _Alloc allocator_type;
200
201 protected:
202 using _Base::_M_allocate;
203 using _Base::_M_deallocate;
204 using _Base::_M_impl;
205 using _Base::_M_get_Tp_allocator;
206
207 public:
208 // [23.2.4.1] construct/copy/destroy
209 // (assign() and get_allocator() are also listed in this section)
210 /**
211 * @brief Default constructor creates no elements.
212 */
213 vector()
214 : _Base() { }
215
216 /**
217 * @brief Creates a %vector with no elements.
218 * @param a An allocator object.
219 */
220 explicit
221 vector(const allocator_type& __a)
222 : _Base(__a) { }
223
224 /**
225 * @brief Creates a %vector with copies of an exemplar element.
226 * @param n The number of elements to initially create.
227 * @param value An element to copy.
228 * @param a An allocator.
229 *
230 * This constructor fills the %vector with @a n copies of @a value.
231 */
232 explicit
233 vector(size_type __n, const value_type& __value = value_type(),
234 const allocator_type& __a = allocator_type())
235 : _Base(__n, __a)
236 { _M_fill_initialize(__n, __value); }
237
238 /**
239 * @brief %Vector copy constructor.
240 * @param x A %vector of identical element and allocator types.
241 *
242 * The newly-created %vector uses a copy of the allocation
243 * object used by @a x. All the elements of @a x are copied,
244 * but any extra memory in
245 * @a x (for fast expansion) will not be copied.
246 */
247 vector(const vector& __x)
248 : _Base(__x.size(), __x._M_get_Tp_allocator())
249 { this->_M_impl._M_finish =
250 std::__uninitialized_copy_a(__x.begin(), __x.end(),
251 this->_M_impl._M_start,
252 _M_get_Tp_allocator());
253 }
254
255 #ifdef __GXX_EXPERIMENTAL_CXX0X__
256 /**
257 * @brief %Vector move constructor.
258 * @param x A %vector of identical element and allocator types.
259 *
260 * The newly-created %vector contains the exact contents of @a x.
261 * The contents of @a x are a valid, but unspecified %vector.
262 */
263 vector(vector&& __x)
264 : _Base(std::forward<_Base>(__x)) { }
265
266 /**
267 * @brief Builds a %vector from an initializer list.
268 * @param l An initializer_list.
269 * @param a An allocator.
270 *
271 * Create a %vector consisting of copies of the elements in the
272 * initializer_list @a l.
273 *
274 * This will call the element type's copy constructor N times
275 * (where N is @a l.size()) and do no memory reallocation.
276 */
277 vector(initializer_list<value_type> __l,
278 const allocator_type& __a = allocator_type())
279 : _Base(__a)
280 {
281 _M_range_initialize(__l.begin(), __l.end(),
282 random_access_iterator_tag());
283 }
284 #endif
285
286 /**
287 * @brief Builds a %vector from a range.
288 * @param first An input iterator.
289 * @param last An input iterator.
290 * @param a An allocator.
291 *
292 * Create a %vector consisting of copies of the elements from
293 * [first,last).
294 *
295 * If the iterators are forward, bidirectional, or
296 * random-access, then this will call the elements' copy
297 * constructor N times (where N is distance(first,last)) and do
298 * no memory reallocation. But if only input iterators are
299 * used, then this will do at most 2N calls to the copy
300 * constructor, and logN memory reallocations.
301 */
302 template<typename _InputIterator>
303 vector(_InputIterator __first, _InputIterator __last,
304 const allocator_type& __a = allocator_type())
305 : _Base(__a)
306 {
307 // Check whether it's an integral type. If so, it's not an iterator.
308 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
309 _M_initialize_dispatch(__first, __last, _Integral());
310 }
311
312 /**
313 * The dtor only erases the elements, and note that if the
314 * elements themselves are pointers, the pointed-to memory is
315 * not touched in any way. Managing the pointer is the user's
316 * responsibility.
317 */
318 ~vector()
319 { std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
320 _M_get_Tp_allocator()); }
321
322 /**
323 * @brief %Vector assignment operator.
324 * @param x A %vector of identical element and allocator types.
325 *
326 * All the elements of @a x are copied, but any extra memory in
327 * @a x (for fast expansion) will not be copied. Unlike the
328 * copy constructor, the allocator object is not copied.
329 */
330 vector&
331 operator=(const vector& __x);
332
333 #ifdef __GXX_EXPERIMENTAL_CXX0X__
334 /**
335 * @brief %Vector move assignment operator.
336 * @param x A %vector of identical element and allocator types.
337 *
338 * The contents of @a x are moved into this %vector (without copying).
339 * @a x is a valid, but unspecified %vector.
340 */
341 vector&
342 operator=(vector&& __x)
343 {
344 // NB: DR 675.
345 this->clear();
346 this->swap(__x);
347 return *this;
348 }
349
350 /**
351 * @brief %Vector list assignment operator.
352 * @param l An initializer_list.
353 *
354 * This function fills a %vector with copies of the elements in the
355 * initializer list @a l.
356 *
357 * Note that the assignment completely changes the %vector and
358 * that the resulting %vector's size is the same as the number
359 * of elements assigned. Old data may be lost.
360 */
361 vector&
362 operator=(initializer_list<value_type> __l)
363 {
364 this->assign(__l.begin(), __l.end());
365 return *this;
366 }
367 #endif
368
369 /**
370 * @brief Assigns a given value to a %vector.
371 * @param n Number of elements to be assigned.
372 * @param val Value to be assigned.
373 *
374 * This function fills a %vector with @a n copies of the given
375 * value. Note that the assignment completely changes the
376 * %vector and that the resulting %vector's size is the same as
377 * the number of elements assigned. Old data may be lost.
378 */
379 void
380 assign(size_type __n, const value_type& __val)
381 { _M_fill_assign(__n, __val); }
382
383 /**
384 * @brief Assigns a range to a %vector.
385 * @param first An input iterator.
386 * @param last An input iterator.
387 *
388 * This function fills a %vector with copies of the elements in the
389 * range [first,last).
390 *
391 * Note that the assignment completely changes the %vector and
392 * that the resulting %vector's size is the same as the number
393 * of elements assigned. Old data may be lost.
394 */
395 template<typename _InputIterator>
396 void
397 assign(_InputIterator __first, _InputIterator __last)
398 {
399 // Check whether it's an integral type. If so, it's not an iterator.
400 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
401 _M_assign_dispatch(__first, __last, _Integral());
402 }
403
404 #ifdef __GXX_EXPERIMENTAL_CXX0X__
405 /**
406 * @brief Assigns an initializer list to a %vector.
407 * @param l An initializer_list.
408 *
409 * This function fills a %vector with copies of the elements in the
410 * initializer list @a l.
411 *
412 * Note that the assignment completely changes the %vector and
413 * that the resulting %vector's size is the same as the number
414 * of elements assigned. Old data may be lost.
415 */
416 void
417 assign(initializer_list<value_type> __l)
418 { this->assign(__l.begin(), __l.end()); }
419 #endif
420
421 /// Get a copy of the memory allocation object.
422 using _Base::get_allocator;
423
424 // iterators
425 /**
426 * Returns a read/write iterator that points to the first
427 * element in the %vector. Iteration is done in ordinary
428 * element order.
429 */
430 iterator
431 begin()
432 { return iterator(this->_M_impl._M_start); }
433
434 /**
435 * Returns a read-only (constant) iterator that points to the
436 * first element in the %vector. Iteration is done in ordinary
437 * element order.
438 */
439 const_iterator
440 begin() const
441 { return const_iterator(this->_M_impl._M_start); }
442
443 /**
444 * Returns a read/write iterator that points one past the last
445 * element in the %vector. Iteration is done in ordinary
446 * element order.
447 */
448 iterator
449 end()
450 { return iterator(this->_M_impl._M_finish); }
451
452 /**
453 * Returns a read-only (constant) iterator that points one past
454 * the last element in the %vector. Iteration is done in
455 * ordinary element order.
456 */
457 const_iterator
458 end() const
459 { return const_iterator(this->_M_impl._M_finish); }
460
461 /**
462 * Returns a read/write reverse iterator that points to the
463 * last element in the %vector. Iteration is done in reverse
464 * element order.
465 */
466 reverse_iterator
467 rbegin()
468 { return reverse_iterator(end()); }
469
470 /**
471 * Returns a read-only (constant) reverse iterator that points
472 * to the last element in the %vector. Iteration is done in
473 * reverse element order.
474 */
475 const_reverse_iterator
476 rbegin() const
477 { return const_reverse_iterator(end()); }
478
479 /**
480 * Returns a read/write reverse iterator that points to one
481 * before the first element in the %vector. Iteration is done
482 * in reverse element order.
483 */
484 reverse_iterator
485 rend()
486 { return reverse_iterator(begin()); }
487
488 /**
489 * Returns a read-only (constant) reverse iterator that points
490 * to one before the first element in the %vector. Iteration
491 * is done in reverse element order.
492 */
493 const_reverse_iterator
494 rend() const
495 { return const_reverse_iterator(begin()); }
496
497 #ifdef __GXX_EXPERIMENTAL_CXX0X__
498 /**
499 * Returns a read-only (constant) iterator that points to the
500 * first element in the %vector. Iteration is done in ordinary
501 * element order.
502 */
503 const_iterator
504 cbegin() const
505 { return const_iterator(this->_M_impl._M_start); }
506
507 /**
508 * Returns a read-only (constant) iterator that points one past
509 * the last element in the %vector. Iteration is done in
510 * ordinary element order.
511 */
512 const_iterator
513 cend() const
514 { return const_iterator(this->_M_impl._M_finish); }
515
516 /**
517 * Returns a read-only (constant) reverse iterator that points
518 * to the last element in the %vector. Iteration is done in
519 * reverse element order.
520 */
521 const_reverse_iterator
522 crbegin() const
523 { return const_reverse_iterator(end()); }
524
525 /**
526 * Returns a read-only (constant) reverse iterator that points
527 * to one before the first element in the %vector. Iteration
528 * is done in reverse element order.
529 */
530 const_reverse_iterator
531 crend() const
532 { return const_reverse_iterator(begin()); }
533 #endif
534
535 // [23.2.4.2] capacity
536 /** Returns the number of elements in the %vector. */
537 size_type
538 size() const
539 { return size_type(this->_M_impl._M_finish - this->_M_impl._M_start); }
540
541 /** Returns the size() of the largest possible %vector. */
542 size_type
543 max_size() const
544 { return _M_get_Tp_allocator().max_size(); }
545
546 /**
547 * @brief Resizes the %vector to the specified number of elements.
548 * @param new_size Number of elements the %vector should contain.
549 * @param x Data with which new elements should be populated.
550 *
551 * This function will %resize the %vector to the specified
552 * number of elements. If the number is smaller than the
553 * %vector's current size the %vector is truncated, otherwise
554 * the %vector is extended and new elements are populated with
555 * given data.
556 */
557 void
558 resize(size_type __new_size, value_type __x = value_type())
559 {
560 if (__new_size < size())
561 _M_erase_at_end(this->_M_impl._M_start + __new_size);
562 else
563 insert(end(), __new_size - size(), __x);
564 }
565
566 /**
567 * Returns the total number of elements that the %vector can
568 * hold before needing to allocate more memory.
569 */
570 size_type
571 capacity() const
572 { return size_type(this->_M_impl._M_end_of_storage
573 - this->_M_impl._M_start); }
574
575 /**
576 * Returns true if the %vector is empty. (Thus begin() would
577 * equal end().)
578 */
579 bool
580 empty() const
581 { return begin() == end(); }
582
583 /**
584 * @brief Attempt to preallocate enough memory for specified number of
585 * elements.
586 * @param n Number of elements required.
587 * @throw std::length_error If @a n exceeds @c max_size().
588 *
589 * This function attempts to reserve enough memory for the
590 * %vector to hold the specified number of elements. If the
591 * number requested is more than max_size(), length_error is
592 * thrown.
593 *
594 * The advantage of this function is that if optimal code is a
595 * necessity and the user can determine the number of elements
596 * that will be required, the user can reserve the memory in
597 * %advance, and thus prevent a possible reallocation of memory
598 * and copying of %vector data.
599 */
600 void
601 reserve(size_type __n);
602
603 // element access
604 /**
605 * @brief Subscript access to the data contained in the %vector.
606 * @param n The index of the element for which data should be
607 * accessed.
608 * @return Read/write reference to data.
609 *
610 * This operator allows for easy, array-style, data access.
611 * Note that data access with this operator is unchecked and
612 * out_of_range lookups are not defined. (For checked lookups
613 * see at().)
614 */
615 reference
616 operator[](size_type __n)
617 { return *(this->_M_impl._M_start + __n); }
618
619 /**
620 * @brief Subscript access to the data contained in the %vector.
621 * @param n The index of the element for which data should be
622 * accessed.
623 * @return Read-only (constant) reference to data.
624 *
625 * This operator allows for easy, array-style, data access.
626 * Note that data access with this operator is unchecked and
627 * out_of_range lookups are not defined. (For checked lookups
628 * see at().)
629 */
630 const_reference
631 operator[](size_type __n) const
632 { return *(this->_M_impl._M_start + __n); }
633
634 protected:
635 /// Safety check used only from at().
636 void
637 _M_range_check(size_type __n) const
638 {
639 if (__n >= this->size())
640 __throw_out_of_range(__N("vector::_M_range_check"));
641 }
642
643 public:
644 /**
645 * @brief Provides access to the data contained in the %vector.
646 * @param n The index of the element for which data should be
647 * accessed.
648 * @return Read/write reference to data.
649 * @throw std::out_of_range If @a n is an invalid index.
650 *
651 * This function provides for safer data access. The parameter
652 * is first checked that it is in the range of the vector. The
653 * function throws out_of_range if the check fails.
654 */
655 reference
656 at(size_type __n)
657 {
658 _M_range_check(__n);
659 return (*this)[__n];
660 }
661
662 /**
663 * @brief Provides access to the data contained in the %vector.
664 * @param n The index of the element for which data should be
665 * accessed.
666 * @return Read-only (constant) reference to data.
667 * @throw std::out_of_range If @a n is an invalid index.
668 *
669 * This function provides for safer data access. The parameter
670 * is first checked that it is in the range of the vector. The
671 * function throws out_of_range if the check fails.
672 */
673 const_reference
674 at(size_type __n) const
675 {
676 _M_range_check(__n);
677 return (*this)[__n];
678 }
679
680 /**
681 * Returns a read/write reference to the data at the first
682 * element of the %vector.
683 */
684 reference
685 front()
686 { return *begin(); }
687
688 /**
689 * Returns a read-only (constant) reference to the data at the first
690 * element of the %vector.
691 */
692 const_reference
693 front() const
694 { return *begin(); }
695
696 /**
697 * Returns a read/write reference to the data at the last
698 * element of the %vector.
699 */
700 reference
701 back()
702 { return *(end() - 1); }
703
704 /**
705 * Returns a read-only (constant) reference to the data at the
706 * last element of the %vector.
707 */
708 const_reference
709 back() const
710 { return *(end() - 1); }
711
712 // _GLIBCXX_RESOLVE_LIB_DEFECTS
713 // DR 464. Suggestion for new member functions in standard containers.
714 // data access
715 /**
716 * Returns a pointer such that [data(), data() + size()) is a valid
717 * range. For a non-empty %vector, data() == &front().
718 */
719 pointer
720 data()
721 { return pointer(this->_M_impl._M_start); }
722
723 const_pointer
724 data() const
725 { return const_pointer(this->_M_impl._M_start); }
726
727 // [23.2.4.3] modifiers
728 /**
729 * @brief Add data to the end of the %vector.
730 * @param x Data to be added.
731 *
732 * This is a typical stack operation. The function creates an
733 * element at the end of the %vector and assigns the given data
734 * to it. Due to the nature of a %vector this operation can be
735 * done in constant time if the %vector has preallocated space
736 * available.
737 */
738 void
739 push_back(const value_type& __x)
740 {
741 if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage)
742 {
743 this->_M_impl.construct(this->_M_impl._M_finish, __x);
744 ++this->_M_impl._M_finish;
745 }
746 else
747 _M_insert_aux(end(), __x);
748 }
749
750 #ifdef __GXX_EXPERIMENTAL_CXX0X__
751 void
752 push_back(value_type&& __x)
753 { emplace_back(std::move(__x)); }
754
755 template<typename... _Args>
756 void
757 emplace_back(_Args&&... __args);
758 #endif
759
760 /**
761 * @brief Removes last element.
762 *
763 * This is a typical stack operation. It shrinks the %vector by one.
764 *
765 * Note that no data is returned, and if the last element's
766 * data is needed, it should be retrieved before pop_back() is
767 * called.
768 */
769 void
770 pop_back()
771 {
772 --this->_M_impl._M_finish;
773 this->_M_impl.destroy(this->_M_impl._M_finish);
774 }
775
776 #ifdef __GXX_EXPERIMENTAL_CXX0X__
777 /**
778 * @brief Inserts an object in %vector before specified iterator.
779 * @param position An iterator into the %vector.
780 * @param args Arguments.
781 * @return An iterator that points to the inserted data.
782 *
783 * This function will insert an object of type T constructed
784 * with T(std::forward<Args>(args)...) before the specified location.
785 * Note that this kind of operation could be expensive for a %vector
786 * and if it is frequently used the user should consider using
787 * std::list.
788 */
789 template<typename... _Args>
790 iterator
791 emplace(iterator __position, _Args&&... __args);
792 #endif
793
794 /**
795 * @brief Inserts given value into %vector before specified iterator.
796 * @param position An iterator into the %vector.
797 * @param x Data to be inserted.
798 * @return An iterator that points to the inserted data.
799 *
800 * This function will insert a copy of the given value before
801 * the specified location. Note that this kind of operation
802 * could be expensive for a %vector and if it is frequently
803 * used the user should consider using std::list.
804 */
805 iterator
806 insert(iterator __position, const value_type& __x);
807
808 #ifdef __GXX_EXPERIMENTAL_CXX0X__
809 /**
810 * @brief Inserts given rvalue into %vector before specified iterator.
811 * @param position An iterator into the %vector.
812 * @param x Data to be inserted.
813 * @return An iterator that points to the inserted data.
814 *
815 * This function will insert a copy of the given rvalue before
816 * the specified location. Note that this kind of operation
817 * could be expensive for a %vector and if it is frequently
818 * used the user should consider using std::list.
819 */
820 iterator
821 insert(iterator __position, value_type&& __x)
822 { return emplace(__position, std::move(__x)); }
823
824 /**
825 * @brief Inserts an initializer_list into the %vector.
826 * @param position An iterator into the %vector.
827 * @param l An initializer_list.
828 *
829 * This function will insert copies of the data in the
830 * initializer_list @a l into the %vector before the location
831 * specified by @a position.
832 *
833 * Note that this kind of operation could be expensive for a
834 * %vector and if it is frequently used the user should
835 * consider using std::list.
836 */
837 void
838 insert(iterator __position, initializer_list<value_type> __l)
839 { this->insert(__position, __l.begin(), __l.end()); }
840 #endif
841
842 /**
843 * @brief Inserts a number of copies of given data into the %vector.
844 * @param position An iterator into the %vector.
845 * @param n Number of elements to be inserted.
846 * @param x Data to be inserted.
847 *
848 * This function will insert a specified number of copies of
849 * the given data before the location specified by @a position.
850 *
851 * Note that this kind of operation could be expensive for a
852 * %vector and if it is frequently used the user should
853 * consider using std::list.
854 */
855 void
856 insert(iterator __position, size_type __n, const value_type& __x)
857 { _M_fill_insert(__position, __n, __x); }
858
859 /**
860 * @brief Inserts a range into the %vector.
861 * @param position An iterator into the %vector.
862 * @param first An input iterator.
863 * @param last An input iterator.
864 *
865 * This function will insert copies of the data in the range
866 * [first,last) into the %vector before the location specified
867 * by @a pos.
868 *
869 * Note that this kind of operation could be expensive for a
870 * %vector and if it is frequently used the user should
871 * consider using std::list.
872 */
873 template<typename _InputIterator>
874 void
875 insert(iterator __position, _InputIterator __first,
876 _InputIterator __last)
877 {
878 // Check whether it's an integral type. If so, it's not an iterator.
879 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
880 _M_insert_dispatch(__position, __first, __last, _Integral());
881 }
882
883 /**
884 * @brief Remove element at given position.
885 * @param position Iterator pointing to element to be erased.
886 * @return An iterator pointing to the next element (or end()).
887 *
888 * This function will erase the element at the given position and thus
889 * shorten the %vector by one.
890 *
891 * Note This operation could be expensive and if it is
892 * frequently used the user should consider using std::list.
893 * The user is also cautioned that this function only erases
894 * the element, and that if the element is itself a pointer,
895 * the pointed-to memory is not touched in any way. Managing
896 * the pointer is the user's responsibility.
897 */
898 iterator
899 erase(iterator __position);
900
901 /**
902 * @brief Remove a range of elements.
903 * @param first Iterator pointing to the first element to be erased.
904 * @param last Iterator pointing to one past the last element to be
905 * erased.
906 * @return An iterator pointing to the element pointed to by @a last
907 * prior to erasing (or end()).
908 *
909 * This function will erase the elements in the range [first,last) and
910 * shorten the %vector accordingly.
911 *
912 * Note This operation could be expensive and if it is
913 * frequently used the user should consider using std::list.
914 * The user is also cautioned that this function only erases
915 * the elements, and that if the elements themselves are
916 * pointers, the pointed-to memory is not touched in any way.
917 * Managing the pointer is the user's responsibility.
918 */
919 iterator
920 erase(iterator __first, iterator __last);
921
922 /**
923 * @brief Swaps data with another %vector.
924 * @param x A %vector of the same element and allocator types.
925 *
926 * This exchanges the elements between two vectors in constant time.
927 * (Three pointers, so it should be quite fast.)
928 * Note that the global std::swap() function is specialized such that
929 * std::swap(v1,v2) will feed to this function.
930 */
931 void
932 #ifdef __GXX_EXPERIMENTAL_CXX0X__
933 swap(vector&& __x)
934 #else
935 swap(vector& __x)
936 #endif
937 {
938 std::swap(this->_M_impl._M_start, __x._M_impl._M_start);
939 std::swap(this->_M_impl._M_finish, __x._M_impl._M_finish);
940 std::swap(this->_M_impl._M_end_of_storage,
941 __x._M_impl._M_end_of_storage);
942
943 // _GLIBCXX_RESOLVE_LIB_DEFECTS
944 // 431. Swapping containers with unequal allocators.
945 std::__alloc_swap<_Tp_alloc_type>::_S_do_it(_M_get_Tp_allocator(),
946 __x._M_get_Tp_allocator());
947 }
948
949 /**
950 * Erases all the elements. Note that this function only erases the
951 * elements, and that if the elements themselves are pointers, the
952 * pointed-to memory is not touched in any way. Managing the pointer is
953 * the user's responsibility.
954 */
955 void
956 clear()
957 { _M_erase_at_end(this->_M_impl._M_start); }
958
959 protected:
960 /**
961 * Memory expansion handler. Uses the member allocation function to
962 * obtain @a n bytes of memory, and then copies [first,last) into it.
963 */
964 template<typename _ForwardIterator>
965 pointer
966 _M_allocate_and_copy(size_type __n,
967 _ForwardIterator __first, _ForwardIterator __last)
968 {
969 pointer __result = this->_M_allocate(__n);
970 __try
971 {
972 std::__uninitialized_copy_a(__first, __last, __result,
973 _M_get_Tp_allocator());
974 return __result;
975 }
976 __catch(...)
977 {
978 _M_deallocate(__result, __n);
979 __throw_exception_again;
980 }
981 }
982
983
984 // Internal constructor functions follow.
985
986 // Called by the range constructor to implement [23.1.1]/9
987
988 // _GLIBCXX_RESOLVE_LIB_DEFECTS
989 // 438. Ambiguity in the "do the right thing" clause
990 template<typename _Integer>
991 void
992 _M_initialize_dispatch(_Integer __n, _Integer __value, __true_type)
993 {
994 this->_M_impl._M_start = _M_allocate(static_cast<size_type>(__n));
995 this->_M_impl._M_end_of_storage =
996 this->_M_impl._M_start + static_cast<size_type>(__n);
997 _M_fill_initialize(static_cast<size_type>(__n), __value);
998 }
999
1000 // Called by the range constructor to implement [23.1.1]/9
1001 template<typename _InputIterator>
1002 void
1003 _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
1004 __false_type)
1005 {
1006 typedef typename std::iterator_traits<_InputIterator>::
1007 iterator_category _IterCategory;
1008 _M_range_initialize(__first, __last, _IterCategory());
1009 }
1010
1011 // Called by the second initialize_dispatch above
1012 template<typename _InputIterator>
1013 void
1014 _M_range_initialize(_InputIterator __first,
1015 _InputIterator __last, std::input_iterator_tag)
1016 {
1017 for (; __first != __last; ++__first)
1018 push_back(*__first);
1019 }
1020
1021 // Called by the second initialize_dispatch above
1022 template<typename _ForwardIterator>
1023 void
1024 _M_range_initialize(_ForwardIterator __first,
1025 _ForwardIterator __last, std::forward_iterator_tag)
1026 {
1027 const size_type __n = std::distance(__first, __last);
1028 this->_M_impl._M_start = this->_M_allocate(__n);
1029 this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __n;
1030 this->_M_impl._M_finish =
1031 std::__uninitialized_copy_a(__first, __last,
1032 this->_M_impl._M_start,
1033 _M_get_Tp_allocator());
1034 }
1035
1036 // Called by the first initialize_dispatch above and by the
1037 // vector(n,value,a) constructor.
1038 void
1039 _M_fill_initialize(size_type __n, const value_type& __value)
1040 {
1041 std::__uninitialized_fill_n_a(this->_M_impl._M_start, __n, __value,
1042 _M_get_Tp_allocator());
1043 this->_M_impl._M_finish = this->_M_impl._M_end_of_storage;
1044 }
1045
1046
1047 // Internal assign functions follow. The *_aux functions do the actual
1048 // assignment work for the range versions.
1049
1050 // Called by the range assign to implement [23.1.1]/9
1051
1052 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1053 // 438. Ambiguity in the "do the right thing" clause
1054 template<typename _Integer>
1055 void
1056 _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
1057 { _M_fill_assign(__n, __val); }
1058
1059 // Called by the range assign to implement [23.1.1]/9
1060 template<typename _InputIterator>
1061 void
1062 _M_assign_dispatch(_InputIterator __first, _InputIterator __last,
1063 __false_type)
1064 {
1065 typedef typename std::iterator_traits<_InputIterator>::
1066 iterator_category _IterCategory;
1067 _M_assign_aux(__first, __last, _IterCategory());
1068 }
1069
1070 // Called by the second assign_dispatch above
1071 template<typename _InputIterator>
1072 void
1073 _M_assign_aux(_InputIterator __first, _InputIterator __last,
1074 std::input_iterator_tag);
1075
1076 // Called by the second assign_dispatch above
1077 template<typename _ForwardIterator>
1078 void
1079 _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
1080 std::forward_iterator_tag);
1081
1082 // Called by assign(n,t), and the range assign when it turns out
1083 // to be the same thing.
1084 void
1085 _M_fill_assign(size_type __n, const value_type& __val);
1086
1087
1088 // Internal insert functions follow.
1089
1090 // Called by the range insert to implement [23.1.1]/9
1091
1092 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1093 // 438. Ambiguity in the "do the right thing" clause
1094 template<typename _Integer>
1095 void
1096 _M_insert_dispatch(iterator __pos, _Integer __n, _Integer __val,
1097 __true_type)
1098 { _M_fill_insert(__pos, __n, __val); }
1099
1100 // Called by the range insert to implement [23.1.1]/9
1101 template<typename _InputIterator>
1102 void
1103 _M_insert_dispatch(iterator __pos, _InputIterator __first,
1104 _InputIterator __last, __false_type)
1105 {
1106 typedef typename std::iterator_traits<_InputIterator>::
1107 iterator_category _IterCategory;
1108 _M_range_insert(__pos, __first, __last, _IterCategory());
1109 }
1110
1111 // Called by the second insert_dispatch above
1112 template<typename _InputIterator>
1113 void
1114 _M_range_insert(iterator __pos, _InputIterator __first,
1115 _InputIterator __last, std::input_iterator_tag);
1116
1117 // Called by the second insert_dispatch above
1118 template<typename _ForwardIterator>
1119 void
1120 _M_range_insert(iterator __pos, _ForwardIterator __first,
1121 _ForwardIterator __last, std::forward_iterator_tag);
1122
1123 // Called by insert(p,n,x), and the range insert when it turns out to be
1124 // the same thing.
1125 void
1126 _M_fill_insert(iterator __pos, size_type __n, const value_type& __x);
1127
1128 // Called by insert(p,x)
1129 #ifndef __GXX_EXPERIMENTAL_CXX0X__
1130 void
1131 _M_insert_aux(iterator __position, const value_type& __x);
1132 #else
1133 template<typename... _Args>
1134 void
1135 _M_insert_aux(iterator __position, _Args&&... __args);
1136 #endif
1137
1138 // Called by the latter.
1139 size_type
1140 _M_check_len(size_type __n, const char* __s) const
1141 {
1142 if (max_size() - size() < __n)
1143 __throw_length_error(__N(__s));
1144
1145 const size_type __len = size() + std::max(size(), __n);
1146 return (__len < size() || __len > max_size()) ? max_size() : __len;
1147 }
1148
1149 // Internal erase functions follow.
1150
1151 // Called by erase(q1,q2), clear(), resize(), _M_fill_assign,
1152 // _M_assign_aux.
1153 void
1154 _M_erase_at_end(pointer __pos)
1155 {
1156 std::_Destroy(__pos, this->_M_impl._M_finish, _M_get_Tp_allocator());
1157 this->_M_impl._M_finish = __pos;
1158 }
1159 };
1160
1161
1162 /**
1163 * @brief Vector equality comparison.
1164 * @param x A %vector.
1165 * @param y A %vector of the same type as @a x.
1166 * @return True iff the size and elements of the vectors are equal.
1167 *
1168 * This is an equivalence relation. It is linear in the size of the
1169 * vectors. Vectors are considered equivalent if their sizes are equal,
1170 * and if corresponding elements compare equal.
1171 */
1172 template<typename _Tp, typename _Alloc>
1173 inline bool
1174 operator==(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
1175 { return (__x.size() == __y.size()
1176 && std::equal(__x.begin(), __x.end(), __y.begin())); }
1177
1178 /**
1179 * @brief Vector ordering relation.
1180 * @param x A %vector.
1181 * @param y A %vector of the same type as @a x.
1182 * @return True iff @a x is lexicographically less than @a y.
1183 *
1184 * This is a total ordering relation. It is linear in the size of the
1185 * vectors. The elements must be comparable with @c <.
1186 *
1187 * See std::lexicographical_compare() for how the determination is made.
1188 */
1189 template<typename _Tp, typename _Alloc>
1190 inline bool
1191 operator<(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
1192 { return std::lexicographical_compare(__x.begin(), __x.end(),
1193 __y.begin(), __y.end()); }
1194
1195 /// Based on operator==
1196 template<typename _Tp, typename _Alloc>
1197 inline bool
1198 operator!=(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
1199 { return !(__x == __y); }
1200
1201 /// Based on operator<
1202 template<typename _Tp, typename _Alloc>
1203 inline bool
1204 operator>(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
1205 { return __y < __x; }
1206
1207 /// Based on operator<
1208 template<typename _Tp, typename _Alloc>
1209 inline bool
1210 operator<=(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
1211 { return !(__y < __x); }
1212
1213 /// Based on operator<
1214 template<typename _Tp, typename _Alloc>
1215 inline bool
1216 operator>=(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
1217 { return !(__x < __y); }
1218
1219 /// See std::vector::swap().
1220 template<typename _Tp, typename _Alloc>
1221 inline void
1222 swap(vector<_Tp, _Alloc>& __x, vector<_Tp, _Alloc>& __y)
1223 { __x.swap(__y); }
1224
1225 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1226 template<typename _Tp, typename _Alloc>
1227 inline void
1228 swap(vector<_Tp, _Alloc>&& __x, vector<_Tp, _Alloc>& __y)
1229 { __x.swap(__y); }
1230
1231 template<typename _Tp, typename _Alloc>
1232 inline void
1233 swap(vector<_Tp, _Alloc>& __x, vector<_Tp, _Alloc>&& __y)
1234 { __x.swap(__y); }
1235 #endif
1236
1237 _GLIBCXX_END_NESTED_NAMESPACE
1238
1239 #endif /* _STL_VECTOR_H */