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