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