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