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