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