]>
Commit | Line | Data |
---|---|---|
42526146 PE |
1 | // Vector implementation -*- C++ -*- |
2 | ||
99dee823 | 3 | // Copyright (C) 2001-2021 Free Software Foundation, Inc. |
42526146 PE |
4 | // |
5 | // This file is part of the GNU ISO C++ Library. This library is free | |
6 | // software; you can redistribute it and/or modify it under the | |
7 | // terms of the GNU General Public License as published by the | |
748086b7 | 8 | // Free Software Foundation; either version 3, or (at your option) |
42526146 PE |
9 | // any later version. |
10 | ||
11 | // This library is distributed in the hope that it will be useful, | |
12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | // GNU General Public License for more details. | |
15 | ||
748086b7 JJ |
16 | // Under Section 7 of GPL version 3, you are granted additional |
17 | // permissions described in the GCC Runtime Library Exception, version | |
18 | // 3.1, as published by the Free Software Foundation. | |
42526146 | 19 | |
748086b7 JJ |
20 | // You should have received a copy of the GNU General Public License and |
21 | // a copy of the GCC Runtime Library Exception along with this program; | |
22 | // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see | |
23 | // <http://www.gnu.org/licenses/>. | |
42526146 | 24 | |
725dc051 BK |
25 | /* |
26 | * | |
27 | * Copyright (c) 1994 | |
28 | * Hewlett-Packard Company | |
29 | * | |
30 | * Permission to use, copy, modify, distribute and sell this software | |
31 | * and its documentation for any purpose is hereby granted without fee, | |
32 | * provided that the above copyright notice appear in all copies and | |
33 | * that both that copyright notice and this permission notice appear | |
34 | * in supporting documentation. Hewlett-Packard Company makes no | |
35 | * representations about the suitability of this software for any | |
36 | * purpose. It is provided "as is" without express or implied warranty. | |
37 | * | |
38 | * | |
39 | * Copyright (c) 1996 | |
40 | * Silicon Graphics Computer Systems, Inc. | |
41 | * | |
42 | * Permission to use, copy, modify, distribute and sell this software | |
43 | * and its documentation for any purpose is hereby granted without fee, | |
44 | * provided that the above copyright notice appear in all copies and | |
45 | * that both that copyright notice and this permission notice appear | |
46 | * in supporting documentation. Silicon Graphics makes no | |
47 | * representations about the suitability of this software for any | |
48 | * purpose. It is provided "as is" without express or implied warranty. | |
49 | */ | |
50 | ||
f910786b | 51 | /** @file bits/stl_vector.h |
729e3d3f | 52 | * This is an internal header file, included by other library headers. |
f910786b | 53 | * Do not attempt to use it directly. @headername{vector} |
725dc051 BK |
54 | */ |
55 | ||
046d30f4 PC |
56 | #ifndef _STL_VECTOR_H |
57 | #define _STL_VECTOR_H 1 | |
725dc051 | 58 | |
30a20a1e | 59 | #include <bits/stl_iterator_base_funcs.h> |
e2c09482 | 60 | #include <bits/functexcept.h> |
30a20a1e | 61 | #include <bits/concept_check.h> |
734f5023 | 62 | #if __cplusplus >= 201103L |
988499f4 | 63 | #include <initializer_list> |
a7d5d7e2 | 64 | #endif |
bd2420f8 JW |
65 | #if __cplusplus > 201703L |
66 | # include <compare> | |
67 | #endif | |
725dc051 | 68 | |
bd2ee798 FD |
69 | #include <debug/assertions.h> |
70 | ||
8c7331c5 JW |
71 | #if _GLIBCXX_SANITIZE_STD_ALLOCATOR && _GLIBCXX_SANITIZE_VECTOR |
72 | extern "C" void | |
73 | __sanitizer_annotate_contiguous_container(const void*, const void*, | |
74 | const void*, const void*); | |
75 | #endif | |
76 | ||
12ffa228 BK |
77 | namespace std _GLIBCXX_VISIBILITY(default) |
78 | { | |
4a15d842 | 79 | _GLIBCXX_BEGIN_NAMESPACE_VERSION |
12ffa228 | 80 | _GLIBCXX_BEGIN_NAMESPACE_CONTAINER |
3cbc7af0 | 81 | |
4312e020 | 82 | /// See bits/stl_deque.h's _Deque_base for an explanation. |
af5fb6ab | 83 | template<typename _Tp, typename _Alloc> |
3971a4d2 | 84 | struct _Vector_base |
83144cfc | 85 | { |
73f05031 | 86 | typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template |
d7e16fc5 | 87 | rebind<_Tp>::other _Tp_alloc_type; |
bd8485dc JW |
88 | typedef typename __gnu_cxx::__alloc_traits<_Tp_alloc_type>::pointer |
89 | pointer; | |
4fd20a8f | 90 | |
e6cad987 | 91 | struct _Vector_impl_data |
874e7baa | 92 | { |
bd8485dc JW |
93 | pointer _M_start; |
94 | pointer _M_finish; | |
95 | pointer _M_end_of_storage; | |
78b36b70 | 96 | |
1ae8edf5 | 97 | _GLIBCXX20_CONSTEXPR |
e6cad987 FD |
98 | _Vector_impl_data() _GLIBCXX_NOEXCEPT |
99 | : _M_start(), _M_finish(), _M_end_of_storage() | |
03f9ea44 | 100 | { } |
6f59ea25 | 101 | |
734f5023 | 102 | #if __cplusplus >= 201103L |
1ae8edf5 | 103 | _GLIBCXX20_CONSTEXPR |
e6cad987 FD |
104 | _Vector_impl_data(_Vector_impl_data&& __x) noexcept |
105 | : _M_start(__x._M_start), _M_finish(__x._M_finish), | |
106 | _M_end_of_storage(__x._M_end_of_storage) | |
107 | { __x._M_start = __x._M_finish = __x._M_end_of_storage = pointer(); } | |
6f59ea25 | 108 | #endif |
bd8485dc | 109 | |
1ae8edf5 | 110 | _GLIBCXX20_CONSTEXPR |
e98edc20 MG |
111 | void |
112 | _M_copy_data(_Vector_impl_data const& __x) _GLIBCXX_NOEXCEPT | |
113 | { | |
114 | _M_start = __x._M_start; | |
115 | _M_finish = __x._M_finish; | |
116 | _M_end_of_storage = __x._M_end_of_storage; | |
117 | } | |
118 | ||
1ae8edf5 | 119 | _GLIBCXX20_CONSTEXPR |
e6cad987 FD |
120 | void |
121 | _M_swap_data(_Vector_impl_data& __x) _GLIBCXX_NOEXCEPT | |
bd8485dc | 122 | { |
e98edc20 MG |
123 | // Do not use std::swap(_M_start, __x._M_start), etc as it loses |
124 | // information used by TBAA. | |
125 | _Vector_impl_data __tmp; | |
126 | __tmp._M_copy_data(*this); | |
127 | _M_copy_data(__x); | |
128 | __x._M_copy_data(__tmp); | |
bd8485dc | 129 | } |
e6cad987 FD |
130 | }; |
131 | ||
132 | struct _Vector_impl | |
133 | : public _Tp_alloc_type, public _Vector_impl_data | |
134 | { | |
1ae8edf5 | 135 | _GLIBCXX20_CONSTEXPR |
0321d9fa JW |
136 | _Vector_impl() _GLIBCXX_NOEXCEPT_IF( |
137 | is_nothrow_default_constructible<_Tp_alloc_type>::value) | |
e6cad987 FD |
138 | : _Tp_alloc_type() |
139 | { } | |
140 | ||
1ae8edf5 | 141 | _GLIBCXX20_CONSTEXPR |
e6cad987 FD |
142 | _Vector_impl(_Tp_alloc_type const& __a) _GLIBCXX_NOEXCEPT |
143 | : _Tp_alloc_type(__a) | |
144 | { } | |
145 | ||
146 | #if __cplusplus >= 201103L | |
147 | // Not defaulted, to enforce noexcept(true) even when | |
148 | // !is_nothrow_move_constructible<_Tp_alloc_type>. | |
1ae8edf5 | 149 | _GLIBCXX20_CONSTEXPR |
e6cad987 FD |
150 | _Vector_impl(_Vector_impl&& __x) noexcept |
151 | : _Tp_alloc_type(std::move(__x)), _Vector_impl_data(std::move(__x)) | |
152 | { } | |
153 | ||
1ae8edf5 | 154 | _GLIBCXX20_CONSTEXPR |
e6cad987 FD |
155 | _Vector_impl(_Tp_alloc_type&& __a) noexcept |
156 | : _Tp_alloc_type(std::move(__a)) | |
157 | { } | |
158 | ||
1ae8edf5 | 159 | _GLIBCXX20_CONSTEXPR |
e6cad987 FD |
160 | _Vector_impl(_Tp_alloc_type&& __a, _Vector_impl&& __rv) noexcept |
161 | : _Tp_alloc_type(std::move(__a)), _Vector_impl_data(std::move(__rv)) | |
162 | { } | |
163 | #endif | |
8c7331c5 JW |
164 | |
165 | #if _GLIBCXX_SANITIZE_STD_ALLOCATOR && _GLIBCXX_SANITIZE_VECTOR | |
166 | template<typename = _Tp_alloc_type> | |
167 | struct _Asan | |
168 | { | |
169 | typedef typename __gnu_cxx::__alloc_traits<_Tp_alloc_type> | |
170 | ::size_type size_type; | |
171 | ||
1ae8edf5 JW |
172 | static _GLIBCXX20_CONSTEXPR void |
173 | _S_shrink(_Vector_impl&, size_type) { } | |
174 | static _GLIBCXX20_CONSTEXPR void | |
175 | _S_on_dealloc(_Vector_impl&) { } | |
8c7331c5 JW |
176 | |
177 | typedef _Vector_impl& _Reinit; | |
178 | ||
179 | struct _Grow | |
180 | { | |
1ae8edf5 JW |
181 | _GLIBCXX20_CONSTEXPR _Grow(_Vector_impl&, size_type) { } |
182 | _GLIBCXX20_CONSTEXPR void _M_grew(size_type) { } | |
8c7331c5 JW |
183 | }; |
184 | }; | |
185 | ||
186 | // Enable ASan annotations for memory obtained from std::allocator. | |
187 | template<typename _Up> | |
188 | struct _Asan<allocator<_Up> > | |
189 | { | |
190 | typedef typename __gnu_cxx::__alloc_traits<_Tp_alloc_type> | |
191 | ::size_type size_type; | |
192 | ||
193 | // Adjust ASan annotation for [_M_start, _M_end_of_storage) to | |
194 | // mark end of valid region as __curr instead of __prev. | |
1ae8edf5 | 195 | static _GLIBCXX20_CONSTEXPR void |
8c7331c5 JW |
196 | _S_adjust(_Vector_impl& __impl, pointer __prev, pointer __curr) |
197 | { | |
1ae8edf5 JW |
198 | #if __has_builtin(__builtin_is_constant_evaluated) |
199 | if (!__builtin_is_constant_evaluated()) | |
200 | #endif | |
8c7331c5 JW |
201 | __sanitizer_annotate_contiguous_container(__impl._M_start, |
202 | __impl._M_end_of_storage, __prev, __curr); | |
203 | } | |
204 | ||
1ae8edf5 | 205 | static _GLIBCXX20_CONSTEXPR void |
8c7331c5 JW |
206 | _S_grow(_Vector_impl& __impl, size_type __n) |
207 | { _S_adjust(__impl, __impl._M_finish, __impl._M_finish + __n); } | |
208 | ||
1ae8edf5 | 209 | static _GLIBCXX20_CONSTEXPR void |
8c7331c5 JW |
210 | _S_shrink(_Vector_impl& __impl, size_type __n) |
211 | { _S_adjust(__impl, __impl._M_finish + __n, __impl._M_finish); } | |
212 | ||
1ae8edf5 | 213 | static _GLIBCXX20_CONSTEXPR void |
8c7331c5 JW |
214 | _S_on_dealloc(_Vector_impl& __impl) |
215 | { | |
216 | if (__impl._M_start) | |
217 | _S_adjust(__impl, __impl._M_finish, __impl._M_end_of_storage); | |
218 | } | |
219 | ||
220 | // Used on reallocation to tell ASan unused capacity is invalid. | |
221 | struct _Reinit | |
222 | { | |
1ae8edf5 JW |
223 | explicit _GLIBCXX20_CONSTEXPR |
224 | _Reinit(_Vector_impl& __impl) : _M_impl(__impl) | |
8c7331c5 JW |
225 | { |
226 | // Mark unused capacity as valid again before deallocating it. | |
227 | _S_on_dealloc(_M_impl); | |
228 | } | |
229 | ||
1ae8edf5 | 230 | _GLIBCXX20_CONSTEXPR |
8c7331c5 JW |
231 | ~_Reinit() |
232 | { | |
233 | // Mark unused capacity as invalid after reallocation. | |
234 | if (_M_impl._M_start) | |
235 | _S_adjust(_M_impl, _M_impl._M_end_of_storage, | |
236 | _M_impl._M_finish); | |
237 | } | |
238 | ||
239 | _Vector_impl& _M_impl; | |
240 | ||
241 | #if __cplusplus >= 201103L | |
242 | _Reinit(const _Reinit&) = delete; | |
243 | _Reinit& operator=(const _Reinit&) = delete; | |
244 | #endif | |
245 | }; | |
246 | ||
247 | // Tell ASan when unused capacity is initialized to be valid. | |
248 | struct _Grow | |
249 | { | |
1ae8edf5 | 250 | _GLIBCXX20_CONSTEXPR |
8c7331c5 JW |
251 | _Grow(_Vector_impl& __impl, size_type __n) |
252 | : _M_impl(__impl), _M_n(__n) | |
253 | { _S_grow(_M_impl, __n); } | |
254 | ||
1ae8edf5 | 255 | _GLIBCXX20_CONSTEXPR |
8c7331c5 JW |
256 | ~_Grow() { if (_M_n) _S_shrink(_M_impl, _M_n); } |
257 | ||
1ae8edf5 | 258 | _GLIBCXX20_CONSTEXPR |
8c7331c5 JW |
259 | void _M_grew(size_type __n) { _M_n -= __n; } |
260 | ||
261 | #if __cplusplus >= 201103L | |
262 | _Grow(const _Grow&) = delete; | |
263 | _Grow& operator=(const _Grow&) = delete; | |
264 | #endif | |
265 | private: | |
266 | _Vector_impl& _M_impl; | |
267 | size_type _M_n; | |
268 | }; | |
269 | }; | |
270 | ||
271 | #define _GLIBCXX_ASAN_ANNOTATE_REINIT \ | |
272 | typename _Base::_Vector_impl::template _Asan<>::_Reinit const \ | |
273 | __attribute__((__unused__)) __reinit_guard(this->_M_impl) | |
274 | #define _GLIBCXX_ASAN_ANNOTATE_GROW(n) \ | |
275 | typename _Base::_Vector_impl::template _Asan<>::_Grow \ | |
276 | __attribute__((__unused__)) __grow_guard(this->_M_impl, (n)) | |
277 | #define _GLIBCXX_ASAN_ANNOTATE_GREW(n) __grow_guard._M_grew(n) | |
278 | #define _GLIBCXX_ASAN_ANNOTATE_SHRINK(n) \ | |
279 | _Base::_Vector_impl::template _Asan<>::_S_shrink(this->_M_impl, n) | |
280 | #define _GLIBCXX_ASAN_ANNOTATE_BEFORE_DEALLOC \ | |
281 | _Base::_Vector_impl::template _Asan<>::_S_on_dealloc(this->_M_impl) | |
282 | #else // ! (_GLIBCXX_SANITIZE_STD_ALLOCATOR && _GLIBCXX_SANITIZE_VECTOR) | |
283 | #define _GLIBCXX_ASAN_ANNOTATE_REINIT | |
284 | #define _GLIBCXX_ASAN_ANNOTATE_GROW(n) | |
285 | #define _GLIBCXX_ASAN_ANNOTATE_GREW(n) | |
286 | #define _GLIBCXX_ASAN_ANNOTATE_SHRINK(n) | |
287 | #define _GLIBCXX_ASAN_ANNOTATE_BEFORE_DEALLOC | |
288 | #endif // _GLIBCXX_SANITIZE_STD_ALLOCATOR && _GLIBCXX_SANITIZE_VECTOR | |
03f9ea44 | 289 | }; |
fe62dd04 | 290 | |
af5fb6ab | 291 | public: |
8a1d8dd9 MA |
292 | typedef _Alloc allocator_type; |
293 | ||
1ae8edf5 | 294 | _GLIBCXX20_CONSTEXPR |
8d46ce60 | 295 | _Tp_alloc_type& |
d3677132 | 296 | _M_get_Tp_allocator() _GLIBCXX_NOEXCEPT |
e6cad987 | 297 | { return this->_M_impl; } |
8d46ce60 | 298 | |
1ae8edf5 | 299 | _GLIBCXX20_CONSTEXPR |
8d46ce60 | 300 | const _Tp_alloc_type& |
d3677132 | 301 | _M_get_Tp_allocator() const _GLIBCXX_NOEXCEPT |
e6cad987 | 302 | { return this->_M_impl; } |
4fd20a8f | 303 | |
1ae8edf5 | 304 | _GLIBCXX20_CONSTEXPR |
8a1d8dd9 | 305 | allocator_type |
d3677132 | 306 | get_allocator() const _GLIBCXX_NOEXCEPT |
31905f34 | 307 | { return allocator_type(_M_get_Tp_allocator()); } |
af5fb6ab | 308 | |
e6cad987 FD |
309 | #if __cplusplus >= 201103L |
310 | _Vector_base() = default; | |
311 | #else | |
312 | _Vector_base() { } | |
313 | #endif | |
78b36b70 | 314 | |
1ae8edf5 | 315 | _GLIBCXX20_CONSTEXPR |
757b1644 | 316 | _Vector_base(const allocator_type& __a) _GLIBCXX_NOEXCEPT |
78b36b70 | 317 | : _M_impl(__a) { } |
ed6814f7 | 318 | |
e6cad987 FD |
319 | // Kept for ABI compatibility. |
320 | #if !_GLIBCXX_INLINE_VERSION | |
1ae8edf5 | 321 | _GLIBCXX20_CONSTEXPR |
dc2cf706 PC |
322 | _Vector_base(size_t __n) |
323 | : _M_impl() | |
bd8485dc | 324 | { _M_create_storage(__n); } |
e6cad987 | 325 | #endif |
dc2cf706 | 326 | |
1ae8edf5 | 327 | _GLIBCXX20_CONSTEXPR |
af5fb6ab | 328 | _Vector_base(size_t __n, const allocator_type& __a) |
874e7baa | 329 | : _M_impl(__a) |
bd8485dc | 330 | { _M_create_storage(__n); } |
ed6814f7 | 331 | |
734f5023 | 332 | #if __cplusplus >= 201103L |
e6cad987 FD |
333 | _Vector_base(_Vector_base&&) = default; |
334 | ||
335 | // Kept for ABI compatibility. | |
336 | # if !_GLIBCXX_INLINE_VERSION | |
1ae8edf5 | 337 | _GLIBCXX20_CONSTEXPR |
757b1644 | 338 | _Vector_base(_Tp_alloc_type&& __a) noexcept |
bd8485dc JW |
339 | : _M_impl(std::move(__a)) { } |
340 | ||
1ae8edf5 | 341 | _GLIBCXX20_CONSTEXPR |
bd8485dc JW |
342 | _Vector_base(_Vector_base&& __x, const allocator_type& __a) |
343 | : _M_impl(__a) | |
053cc380 | 344 | { |
bd8485dc JW |
345 | if (__x.get_allocator() == __a) |
346 | this->_M_impl._M_swap_data(__x._M_impl); | |
347 | else | |
348 | { | |
349 | size_t __n = __x._M_impl._M_finish - __x._M_impl._M_start; | |
350 | _M_create_storage(__n); | |
351 | } | |
053cc380 | 352 | } |
e6cad987 FD |
353 | # endif |
354 | ||
1ae8edf5 | 355 | _GLIBCXX20_CONSTEXPR |
e6cad987 FD |
356 | _Vector_base(const allocator_type& __a, _Vector_base&& __x) |
357 | : _M_impl(_Tp_alloc_type(__a), std::move(__x._M_impl)) | |
358 | { } | |
053cc380 PC |
359 | #endif |
360 | ||
1ae8edf5 | 361 | _GLIBCXX20_CONSTEXPR |
757b1644 | 362 | ~_Vector_base() _GLIBCXX_NOEXCEPT |
8c7331c5 JW |
363 | { |
364 | _M_deallocate(_M_impl._M_start, | |
365 | _M_impl._M_end_of_storage - _M_impl._M_start); | |
366 | } | |
8a1d8dd9 MA |
367 | |
368 | public: | |
03f9ea44 | 369 | _Vector_impl _M_impl; |
ed6814f7 | 370 | |
1ae8edf5 | 371 | _GLIBCXX20_CONSTEXPR |
bd8485dc | 372 | pointer |
874e7baa | 373 | _M_allocate(size_t __n) |
20067423 JW |
374 | { |
375 | typedef __gnu_cxx::__alloc_traits<_Tp_alloc_type> _Tr; | |
5b99e0a0 | 376 | return __n != 0 ? _Tr::allocate(_M_impl, __n) : pointer(); |
20067423 | 377 | } |
ed6814f7 | 378 | |
1ae8edf5 | 379 | _GLIBCXX20_CONSTEXPR |
8a1d8dd9 | 380 | void |
bd8485dc | 381 | _M_deallocate(pointer __p, size_t __n) |
368b7a30 | 382 | { |
20067423 | 383 | typedef __gnu_cxx::__alloc_traits<_Tp_alloc_type> _Tr; |
368b7a30 | 384 | if (__p) |
20067423 | 385 | _Tr::deallocate(_M_impl, __p, __n); |
874e7baa | 386 | } |
bd8485dc | 387 | |
e6cad987 | 388 | protected: |
1ae8edf5 | 389 | _GLIBCXX20_CONSTEXPR |
bd8485dc JW |
390 | void |
391 | _M_create_storage(size_t __n) | |
392 | { | |
393 | this->_M_impl._M_start = this->_M_allocate(__n); | |
394 | this->_M_impl._M_finish = this->_M_impl._M_start; | |
395 | this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __n; | |
396 | } | |
af5fb6ab | 397 | }; |
ed6814f7 | 398 | |
3971a4d2 | 399 | /** |
e135a038 BK |
400 | * @brief A standard container which offers fixed time access to |
401 | * individual elements in any order. | |
ad2a4e2b | 402 | * |
aac2878e | 403 | * @ingroup sequences |
ad2a4e2b | 404 | * |
d632488a BK |
405 | * @tparam _Tp Type of element. |
406 | * @tparam _Alloc Allocator type, defaults to allocator<_Tp>. | |
407 | * | |
3971a4d2 PE |
408 | * Meets the requirements of a <a href="tables.html#65">container</a>, a |
409 | * <a href="tables.html#66">reversible container</a>, and a | |
410 | * <a href="tables.html#67">sequence</a>, including the | |
411 | * <a href="tables.html#68">optional sequence requirements</a> with the | |
412 | * %exception of @c push_front and @c pop_front. | |
ad2a4e2b | 413 | * |
e135a038 BK |
414 | * In some terminology a %vector can be described as a dynamic |
415 | * C-style array, it offers fast and efficient access to individual | |
416 | * elements in any order and saves the user from worrying about | |
417 | * memory and size allocation. Subscripting ( @c [] ) access is | |
418 | * also provided as with C-style arrays. | |
ad2a4e2b | 419 | */ |
6323b34e | 420 | template<typename _Tp, typename _Alloc = std::allocator<_Tp> > |
3971a4d2 | 421 | class vector : protected _Vector_base<_Tp, _Alloc> |
af5fb6ab | 422 | { |
fe62dd04 | 423 | #ifdef _GLIBCXX_CONCEPT_CHECKS |
af5fb6ab | 424 | // Concept requirements. |
d7e16fc5 | 425 | typedef typename _Alloc::value_type _Alloc_value_type; |
fe62dd04 | 426 | # if __cplusplus < 201103L |
3d7c150e | 427 | __glibcxx_class_requires(_Tp, _SGIAssignableConcept) |
fe62dd04 | 428 | # endif |
4fd20a8f | 429 | __glibcxx_class_requires2(_Tp, _Alloc_value_type, _SameTypeConcept) |
fe62dd04 FD |
430 | #endif |
431 | ||
866e4d38 JW |
432 | #if __cplusplus >= 201103L |
433 | static_assert(is_same<typename remove_cv<_Tp>::type, _Tp>::value, | |
434 | "std::vector must have a non-const, non-volatile value_type"); | |
ebaf3659 | 435 | # if __cplusplus > 201703L || defined __STRICT_ANSI__ |
866e4d38 JW |
436 | static_assert(is_same<typename _Alloc::value_type, _Tp>::value, |
437 | "std::vector must have the same value_type as its allocator"); | |
438 | # endif | |
439 | #endif | |
440 | ||
d7e16fc5 FD |
441 | typedef _Vector_base<_Tp, _Alloc> _Base; |
442 | typedef typename _Base::_Tp_alloc_type _Tp_alloc_type; | |
443 | typedef __gnu_cxx::__alloc_traits<_Tp_alloc_type> _Alloc_traits; | |
ed6814f7 | 444 | |
af5fb6ab | 445 | public: |
d7e16fc5 FD |
446 | typedef _Tp value_type; |
447 | typedef typename _Base::pointer pointer; | |
448 | typedef typename _Alloc_traits::const_pointer const_pointer; | |
449 | typedef typename _Alloc_traits::reference reference; | |
450 | typedef typename _Alloc_traits::const_reference const_reference; | |
37d5c6ba BK |
451 | typedef __gnu_cxx::__normal_iterator<pointer, vector> iterator; |
452 | typedef __gnu_cxx::__normal_iterator<const_pointer, vector> | |
af5fb6ab | 453 | const_iterator; |
d7e16fc5 FD |
454 | typedef std::reverse_iterator<const_iterator> const_reverse_iterator; |
455 | typedef std::reverse_iterator<iterator> reverse_iterator; | |
456 | typedef size_t size_type; | |
457 | typedef ptrdiff_t difference_type; | |
458 | typedef _Alloc allocator_type; | |
ed6814f7 | 459 | |
0f317ef7 MG |
460 | private: |
461 | #if __cplusplus >= 201103L | |
e658669f | 462 | static constexpr bool |
258bd1d6 | 463 | _S_nothrow_relocate(true_type) |
e658669f JW |
464 | { |
465 | return noexcept(std::__relocate_a(std::declval<pointer>(), | |
466 | std::declval<pointer>(), | |
467 | std::declval<pointer>(), | |
468 | std::declval<_Tp_alloc_type&>())); | |
469 | } | |
258bd1d6 JW |
470 | |
471 | static constexpr bool | |
472 | _S_nothrow_relocate(false_type) | |
473 | { return false; } | |
474 | ||
475 | static constexpr bool | |
476 | _S_use_relocate() | |
477 | { | |
478 | // Instantiating std::__relocate_a might cause an error outside the | |
479 | // immediate context (in __relocate_object_a's noexcept-specifier), | |
480 | // so only do it if we know the type can be move-inserted into *this. | |
481 | return _S_nothrow_relocate(__is_move_insertable<_Tp_alloc_type>{}); | |
482 | } | |
483 | ||
1ae8edf5 | 484 | static _GLIBCXX20_CONSTEXPR pointer |
258bd1d6 JW |
485 | _S_do_relocate(pointer __first, pointer __last, pointer __result, |
486 | _Tp_alloc_type& __alloc, true_type) noexcept | |
487 | { | |
488 | return std::__relocate_a(__first, __last, __result, __alloc); | |
489 | } | |
490 | ||
1ae8edf5 | 491 | static _GLIBCXX20_CONSTEXPR pointer |
258bd1d6 JW |
492 | _S_do_relocate(pointer, pointer, pointer __result, |
493 | _Tp_alloc_type&, false_type) noexcept | |
494 | { return __result; } | |
495 | ||
1ae8edf5 | 496 | static _GLIBCXX20_CONSTEXPR pointer |
258bd1d6 JW |
497 | _S_relocate(pointer __first, pointer __last, pointer __result, |
498 | _Tp_alloc_type& __alloc) noexcept | |
499 | { | |
500 | using __do_it = __bool_constant<_S_use_relocate()>; | |
501 | return _S_do_relocate(__first, __last, __result, __alloc, __do_it{}); | |
502 | } | |
503 | #endif // C++11 | |
0f317ef7 | 504 | |
af5fb6ab | 505 | protected: |
af5fb6ab BK |
506 | using _Base::_M_allocate; |
507 | using _Base::_M_deallocate; | |
03f9ea44 | 508 | using _Base::_M_impl; |
4fd20a8f | 509 | using _Base::_M_get_Tp_allocator; |
ed6814f7 | 510 | |
af5fb6ab BK |
511 | public: |
512 | // [23.2.4.1] construct/copy/destroy | |
513 | // (assign() and get_allocator() are also listed in this section) | |
c3cdd71f JW |
514 | |
515 | /** | |
516 | * @brief Creates a %vector with no elements. | |
517 | */ | |
d9dcda6f | 518 | #if __cplusplus >= 201103L |
e6cad987 FD |
519 | vector() = default; |
520 | #else | |
521 | vector() { } | |
d9dcda6f | 522 | #endif |
c3cdd71f | 523 | |
78b36b70 PC |
524 | /** |
525 | * @brief Creates a %vector with no elements. | |
93c66bc6 | 526 | * @param __a An allocator object. |
78b36b70 | 527 | */ |
af5fb6ab | 528 | explicit |
1ae8edf5 | 529 | _GLIBCXX20_CONSTEXPR |
c3cdd71f | 530 | vector(const allocator_type& __a) _GLIBCXX_NOEXCEPT |
78b36b70 | 531 | : _Base(__a) { } |
ed6814f7 | 532 | |
734f5023 | 533 | #if __cplusplus >= 201103L |
dc2cf706 PC |
534 | /** |
535 | * @brief Creates a %vector with default constructed elements. | |
93c66bc6 | 536 | * @param __n The number of elements to initially create. |
d720a22b | 537 | * @param __a An allocator. |
dc2cf706 | 538 | * |
93c66bc6 | 539 | * This constructor fills the %vector with @a __n default |
dc2cf706 PC |
540 | * constructed elements. |
541 | */ | |
542 | explicit | |
1ae8edf5 | 543 | _GLIBCXX20_CONSTEXPR |
d720a22b | 544 | vector(size_type __n, const allocator_type& __a = allocator_type()) |
af55b3af | 545 | : _Base(_S_check_init_len(__n, __a), __a) |
dc2cf706 PC |
546 | { _M_default_initialize(__n); } |
547 | ||
548 | /** | |
549 | * @brief Creates a %vector with copies of an exemplar element. | |
93c66bc6 BK |
550 | * @param __n The number of elements to initially create. |
551 | * @param __value An element to copy. | |
552 | * @param __a An allocator. | |
dc2cf706 | 553 | * |
93c66bc6 | 554 | * This constructor fills the %vector with @a __n copies of @a __value. |
dc2cf706 | 555 | */ |
1ae8edf5 | 556 | _GLIBCXX20_CONSTEXPR |
dc2cf706 PC |
557 | vector(size_type __n, const value_type& __value, |
558 | const allocator_type& __a = allocator_type()) | |
af55b3af | 559 | : _Base(_S_check_init_len(__n, __a), __a) |
dc2cf706 PC |
560 | { _M_fill_initialize(__n, __value); } |
561 | #else | |
af5fb6ab | 562 | /** |
78b36b70 | 563 | * @brief Creates a %vector with copies of an exemplar element. |
93c66bc6 BK |
564 | * @param __n The number of elements to initially create. |
565 | * @param __value An element to copy. | |
566 | * @param __a An allocator. | |
ed6814f7 | 567 | * |
93c66bc6 | 568 | * This constructor fills the %vector with @a __n copies of @a __value. |
af5fb6ab | 569 | */ |
2fecaef4 PC |
570 | explicit |
571 | vector(size_type __n, const value_type& __value = value_type(), | |
af5fb6ab | 572 | const allocator_type& __a = allocator_type()) |
af55b3af | 573 | : _Base(_S_check_init_len(__n, __a), __a) |
f4c5578f | 574 | { _M_fill_initialize(__n, __value); } |
dc2cf706 | 575 | #endif |
ed6814f7 | 576 | |
af5fb6ab BK |
577 | /** |
578 | * @brief %Vector copy constructor. | |
93c66bc6 | 579 | * @param __x A %vector of identical element and allocator types. |
ed6814f7 | 580 | * |
0a2bf188 JW |
581 | * All the elements of @a __x are copied, but any unused capacity in |
582 | * @a __x will not be copied | |
583 | * (i.e. capacity() == size() in the new %vector). | |
584 | * | |
585 | * The newly-created %vector uses a copy of the allocator object used | |
586 | * by @a __x (unless the allocator traits dictate a different object). | |
af5fb6ab | 587 | */ |
1ae8edf5 | 588 | _GLIBCXX20_CONSTEXPR |
af5fb6ab | 589 | vector(const vector& __x) |
bd8485dc | 590 | : _Base(__x.size(), |
d7e16fc5 FD |
591 | _Alloc_traits::_S_select_on_copy(__x._M_get_Tp_allocator())) |
592 | { | |
593 | this->_M_impl._M_finish = | |
1985f1cd MA |
594 | std::__uninitialized_copy_a(__x.begin(), __x.end(), |
595 | this->_M_impl._M_start, | |
4fd20a8f | 596 | _M_get_Tp_allocator()); |
1985f1cd | 597 | } |
ed6814f7 | 598 | |
734f5023 | 599 | #if __cplusplus >= 201103L |
78b36b70 PC |
600 | /** |
601 | * @brief %Vector move constructor. | |
78b36b70 | 602 | * |
e6cad987 FD |
603 | * The newly-created %vector contains the exact contents of the |
604 | * moved instance. | |
605 | * The contents of the moved instance are a valid, but unspecified | |
606 | * %vector. | |
78b36b70 | 607 | */ |
e6cad987 | 608 | vector(vector&&) noexcept = default; |
988499f4 | 609 | |
bd8485dc | 610 | /// Copy constructor with alternative allocator |
1ae8edf5 | 611 | _GLIBCXX20_CONSTEXPR |
22d34a2a | 612 | vector(const vector& __x, const __type_identity_t<allocator_type>& __a) |
bd8485dc | 613 | : _Base(__x.size(), __a) |
d7e16fc5 FD |
614 | { |
615 | this->_M_impl._M_finish = | |
bd8485dc JW |
616 | std::__uninitialized_copy_a(__x.begin(), __x.end(), |
617 | this->_M_impl._M_start, | |
618 | _M_get_Tp_allocator()); | |
619 | } | |
620 | ||
e6cad987 | 621 | private: |
1ae8edf5 | 622 | _GLIBCXX20_CONSTEXPR |
e6cad987 FD |
623 | vector(vector&& __rv, const allocator_type& __m, true_type) noexcept |
624 | : _Base(__m, std::move(__rv)) | |
625 | { } | |
626 | ||
1ae8edf5 | 627 | _GLIBCXX20_CONSTEXPR |
e6cad987 FD |
628 | vector(vector&& __rv, const allocator_type& __m, false_type) |
629 | : _Base(__m) | |
bd8485dc | 630 | { |
e6cad987 FD |
631 | if (__rv.get_allocator() == __m) |
632 | this->_M_impl._M_swap_data(__rv._M_impl); | |
633 | else if (!__rv.empty()) | |
bd8485dc | 634 | { |
e6cad987 | 635 | this->_M_create_storage(__rv.size()); |
bd8485dc JW |
636 | this->_M_impl._M_finish = |
637 | std::__uninitialized_move_a(__rv.begin(), __rv.end(), | |
638 | this->_M_impl._M_start, | |
639 | _M_get_Tp_allocator()); | |
640 | __rv.clear(); | |
641 | } | |
642 | } | |
643 | ||
e6cad987 FD |
644 | public: |
645 | /// Move constructor with alternative allocator | |
1ae8edf5 | 646 | _GLIBCXX20_CONSTEXPR |
22d34a2a | 647 | vector(vector&& __rv, const __type_identity_t<allocator_type>& __m) |
e6cad987 FD |
648 | noexcept( noexcept( |
649 | vector(std::declval<vector&&>(), std::declval<const allocator_type&>(), | |
650 | std::declval<typename _Alloc_traits::is_always_equal>())) ) | |
651 | : vector(std::move(__rv), __m, typename _Alloc_traits::is_always_equal{}) | |
652 | { } | |
653 | ||
988499f4 JM |
654 | /** |
655 | * @brief Builds a %vector from an initializer list. | |
93c66bc6 BK |
656 | * @param __l An initializer_list. |
657 | * @param __a An allocator. | |
988499f4 JM |
658 | * |
659 | * Create a %vector consisting of copies of the elements in the | |
93c66bc6 | 660 | * initializer_list @a __l. |
988499f4 JM |
661 | * |
662 | * This will call the element type's copy constructor N times | |
93c66bc6 | 663 | * (where N is @a __l.size()) and do no memory reallocation. |
988499f4 | 664 | */ |
1ae8edf5 | 665 | _GLIBCXX20_CONSTEXPR |
988499f4 JM |
666 | vector(initializer_list<value_type> __l, |
667 | const allocator_type& __a = allocator_type()) | |
b798df05 PC |
668 | : _Base(__a) |
669 | { | |
670 | _M_range_initialize(__l.begin(), __l.end(), | |
671 | random_access_iterator_tag()); | |
672 | } | |
78b36b70 PC |
673 | #endif |
674 | ||
af5fb6ab BK |
675 | /** |
676 | * @brief Builds a %vector from a range. | |
93c66bc6 BK |
677 | * @param __first An input iterator. |
678 | * @param __last An input iterator. | |
679 | * @param __a An allocator. | |
ed6814f7 | 680 | * |
af5fb6ab BK |
681 | * Create a %vector consisting of copies of the elements from |
682 | * [first,last). | |
683 | * | |
e135a038 BK |
684 | * If the iterators are forward, bidirectional, or |
685 | * random-access, then this will call the elements' copy | |
686 | * constructor N times (where N is distance(first,last)) and do | |
687 | * no memory reallocation. But if only input iterators are | |
688 | * used, then this will do at most 2N calls to the copy | |
689 | * constructor, and logN memory reallocations. | |
af5fb6ab | 690 | */ |
734f5023 | 691 | #if __cplusplus >= 201103L |
2203cb90 PC |
692 | template<typename _InputIterator, |
693 | typename = std::_RequireInputIter<_InputIterator>> | |
1ae8edf5 | 694 | _GLIBCXX20_CONSTEXPR |
d7e16fc5 | 695 | vector(_InputIterator __first, _InputIterator __last, |
2203cb90 PC |
696 | const allocator_type& __a = allocator_type()) |
697 | : _Base(__a) | |
e6cad987 FD |
698 | { |
699 | _M_range_initialize(__first, __last, | |
700 | std::__iterator_category(__first)); | |
701 | } | |
2203cb90 | 702 | #else |
af5fb6ab | 703 | template<typename _InputIterator> |
d7e16fc5 | 704 | vector(_InputIterator __first, _InputIterator __last, |
af5fb6ab BK |
705 | const allocator_type& __a = allocator_type()) |
706 | : _Base(__a) | |
d7e16fc5 | 707 | { |
af5fb6ab | 708 | // Check whether it's an integral type. If so, it's not an iterator. |
c0736a9d | 709 | typedef typename std::__is_integer<_InputIterator>::__type _Integral; |
af5fb6ab BK |
710 | _M_initialize_dispatch(__first, __last, _Integral()); |
711 | } | |
2203cb90 | 712 | #endif |
ed6814f7 | 713 | |
af5fb6ab | 714 | /** |
e135a038 BK |
715 | * The dtor only erases the elements, and note that if the |
716 | * elements themselves are pointers, the pointed-to memory is | |
717 | * not touched in any way. Managing the pointer is the user's | |
28dac70a | 718 | * responsibility. |
af5fb6ab | 719 | */ |
1ae8edf5 | 720 | _GLIBCXX20_CONSTEXPR |
6f59ea25 | 721 | ~vector() _GLIBCXX_NOEXCEPT |
8c7331c5 JW |
722 | { |
723 | std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish, | |
724 | _M_get_Tp_allocator()); | |
725 | _GLIBCXX_ASAN_ANNOTATE_BEFORE_DEALLOC; | |
726 | } | |
ed6814f7 | 727 | |
af5fb6ab BK |
728 | /** |
729 | * @brief %Vector assignment operator. | |
93c66bc6 | 730 | * @param __x A %vector of identical element and allocator types. |
ed6814f7 | 731 | * |
0a2bf188 JW |
732 | * All the elements of @a __x are copied, but any unused capacity in |
733 | * @a __x will not be copied. | |
734 | * | |
735 | * Whether the allocator is copied depends on the allocator traits. | |
af5fb6ab | 736 | */ |
1ae8edf5 | 737 | _GLIBCXX20_CONSTEXPR |
af5fb6ab BK |
738 | vector& |
739 | operator=(const vector& __x); | |
ed6814f7 | 740 | |
734f5023 | 741 | #if __cplusplus >= 201103L |
78b36b70 PC |
742 | /** |
743 | * @brief %Vector move assignment operator. | |
93c66bc6 | 744 | * @param __x A %vector of identical element and allocator types. |
78b36b70 | 745 | * |
ea2c1a6d JW |
746 | * The contents of @a __x are moved into this %vector (without copying, |
747 | * if the allocators permit it). | |
0a2bf188 JW |
748 | * Afterwards @a __x is a valid, but unspecified %vector. |
749 | * | |
750 | * Whether the allocator is moved depends on the allocator traits. | |
78b36b70 | 751 | */ |
1ae8edf5 | 752 | _GLIBCXX20_CONSTEXPR |
78b36b70 | 753 | vector& |
bd8485dc | 754 | operator=(vector&& __x) noexcept(_Alloc_traits::_S_nothrow_move()) |
cbc6c888 | 755 | { |
d7e16fc5 FD |
756 | constexpr bool __move_storage = |
757 | _Alloc_traits::_S_propagate_on_move_assign() | |
758 | || _Alloc_traits::_S_always_equal(); | |
759 | _M_move_assign(std::move(__x), __bool_constant<__move_storage>()); | |
78b36b70 PC |
760 | return *this; |
761 | } | |
988499f4 JM |
762 | |
763 | /** | |
764 | * @brief %Vector list assignment operator. | |
93c66bc6 | 765 | * @param __l An initializer_list. |
988499f4 JM |
766 | * |
767 | * This function fills a %vector with copies of the elements in the | |
93c66bc6 | 768 | * initializer list @a __l. |
988499f4 JM |
769 | * |
770 | * Note that the assignment completely changes the %vector and | |
771 | * that the resulting %vector's size is the same as the number | |
0a2bf188 | 772 | * of elements assigned. |
988499f4 | 773 | */ |
1ae8edf5 | 774 | _GLIBCXX20_CONSTEXPR |
988499f4 JM |
775 | vector& |
776 | operator=(initializer_list<value_type> __l) | |
777 | { | |
d7e16fc5 FD |
778 | this->_M_assign_aux(__l.begin(), __l.end(), |
779 | random_access_iterator_tag()); | |
988499f4 JM |
780 | return *this; |
781 | } | |
78b36b70 PC |
782 | #endif |
783 | ||
af5fb6ab BK |
784 | /** |
785 | * @brief Assigns a given value to a %vector. | |
93c66bc6 BK |
786 | * @param __n Number of elements to be assigned. |
787 | * @param __val Value to be assigned. | |
af5fb6ab | 788 | * |
93c66bc6 | 789 | * This function fills a %vector with @a __n copies of the given |
af5fb6ab BK |
790 | * value. Note that the assignment completely changes the |
791 | * %vector and that the resulting %vector's size is the same as | |
0a2bf188 | 792 | * the number of elements assigned. |
af5fb6ab | 793 | */ |
1ae8edf5 | 794 | _GLIBCXX20_CONSTEXPR |
af5fb6ab | 795 | void |
ed6814f7 | 796 | assign(size_type __n, const value_type& __val) |
af5fb6ab | 797 | { _M_fill_assign(__n, __val); } |
ed6814f7 | 798 | |
af5fb6ab BK |
799 | /** |
800 | * @brief Assigns a range to a %vector. | |
93c66bc6 BK |
801 | * @param __first An input iterator. |
802 | * @param __last An input iterator. | |
af5fb6ab BK |
803 | * |
804 | * This function fills a %vector with copies of the elements in the | |
93c66bc6 | 805 | * range [__first,__last). |
af5fb6ab BK |
806 | * |
807 | * Note that the assignment completely changes the %vector and | |
808 | * that the resulting %vector's size is the same as the number | |
0a2bf188 | 809 | * of elements assigned. |
af5fb6ab | 810 | */ |
734f5023 | 811 | #if __cplusplus >= 201103L |
2203cb90 PC |
812 | template<typename _InputIterator, |
813 | typename = std::_RequireInputIter<_InputIterator>> | |
1ae8edf5 | 814 | _GLIBCXX20_CONSTEXPR |
d7e16fc5 FD |
815 | void |
816 | assign(_InputIterator __first, _InputIterator __last) | |
817 | { _M_assign_dispatch(__first, __last, __false_type()); } | |
2203cb90 | 818 | #else |
af5fb6ab | 819 | template<typename _InputIterator> |
d7e16fc5 FD |
820 | void |
821 | assign(_InputIterator __first, _InputIterator __last) | |
822 | { | |
af5fb6ab | 823 | // Check whether it's an integral type. If so, it's not an iterator. |
c0736a9d | 824 | typedef typename std::__is_integer<_InputIterator>::__type _Integral; |
af5fb6ab BK |
825 | _M_assign_dispatch(__first, __last, _Integral()); |
826 | } | |
2203cb90 | 827 | #endif |
ed6814f7 | 828 | |
734f5023 | 829 | #if __cplusplus >= 201103L |
988499f4 JM |
830 | /** |
831 | * @brief Assigns an initializer list to a %vector. | |
93c66bc6 | 832 | * @param __l An initializer_list. |
988499f4 JM |
833 | * |
834 | * This function fills a %vector with copies of the elements in the | |
93c66bc6 | 835 | * initializer list @a __l. |
988499f4 JM |
836 | * |
837 | * Note that the assignment completely changes the %vector and | |
838 | * that the resulting %vector's size is the same as the number | |
0a2bf188 | 839 | * of elements assigned. |
988499f4 | 840 | */ |
1ae8edf5 | 841 | _GLIBCXX20_CONSTEXPR |
988499f4 JM |
842 | void |
843 | assign(initializer_list<value_type> __l) | |
d7e16fc5 FD |
844 | { |
845 | this->_M_assign_aux(__l.begin(), __l.end(), | |
846 | random_access_iterator_tag()); | |
847 | } | |
988499f4 JM |
848 | #endif |
849 | ||
af5fb6ab | 850 | /// Get a copy of the memory allocation object. |
8a1d8dd9 | 851 | using _Base::get_allocator; |
ed6814f7 | 852 | |
af5fb6ab BK |
853 | // iterators |
854 | /** | |
e135a038 BK |
855 | * Returns a read/write iterator that points to the first |
856 | * element in the %vector. Iteration is done in ordinary | |
857 | * element order. | |
af5fb6ab | 858 | */ |
1ae8edf5 | 859 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
af5fb6ab | 860 | iterator |
d3677132 | 861 | begin() _GLIBCXX_NOEXCEPT |
bc9053ab | 862 | { return iterator(this->_M_impl._M_start); } |
ed6814f7 | 863 | |
af5fb6ab BK |
864 | /** |
865 | * Returns a read-only (constant) iterator that points to the | |
866 | * first element in the %vector. Iteration is done in ordinary | |
867 | * element order. | |
868 | */ | |
1ae8edf5 | 869 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
af5fb6ab | 870 | const_iterator |
d3677132 | 871 | begin() const _GLIBCXX_NOEXCEPT |
bc9053ab | 872 | { return const_iterator(this->_M_impl._M_start); } |
ed6814f7 | 873 | |
af5fb6ab BK |
874 | /** |
875 | * Returns a read/write iterator that points one past the last | |
876 | * element in the %vector. Iteration is done in ordinary | |
877 | * element order. | |
878 | */ | |
1ae8edf5 | 879 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
af5fb6ab | 880 | iterator |
d3677132 | 881 | end() _GLIBCXX_NOEXCEPT |
bc9053ab | 882 | { return iterator(this->_M_impl._M_finish); } |
ed6814f7 | 883 | |
af5fb6ab | 884 | /** |
e135a038 BK |
885 | * Returns a read-only (constant) iterator that points one past |
886 | * the last element in the %vector. Iteration is done in | |
887 | * ordinary element order. | |
af5fb6ab | 888 | */ |
1ae8edf5 | 889 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
af5fb6ab | 890 | const_iterator |
d3677132 | 891 | end() const _GLIBCXX_NOEXCEPT |
bc9053ab | 892 | { return const_iterator(this->_M_impl._M_finish); } |
ed6814f7 | 893 | |
af5fb6ab BK |
894 | /** |
895 | * Returns a read/write reverse iterator that points to the | |
896 | * last element in the %vector. Iteration is done in reverse | |
897 | * element order. | |
898 | */ | |
1ae8edf5 | 899 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
af5fb6ab | 900 | reverse_iterator |
d3677132 | 901 | rbegin() _GLIBCXX_NOEXCEPT |
874e7baa | 902 | { return reverse_iterator(end()); } |
ed6814f7 | 903 | |
af5fb6ab BK |
904 | /** |
905 | * Returns a read-only (constant) reverse iterator that points | |
906 | * to the last element in the %vector. Iteration is done in | |
907 | * reverse element order. | |
908 | */ | |
1ae8edf5 | 909 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
af5fb6ab | 910 | const_reverse_iterator |
d3677132 | 911 | rbegin() const _GLIBCXX_NOEXCEPT |
874e7baa | 912 | { return const_reverse_iterator(end()); } |
ed6814f7 | 913 | |
af5fb6ab | 914 | /** |
e135a038 BK |
915 | * Returns a read/write reverse iterator that points to one |
916 | * before the first element in the %vector. Iteration is done | |
917 | * in reverse element order. | |
af5fb6ab | 918 | */ |
1ae8edf5 | 919 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
af5fb6ab | 920 | reverse_iterator |
d3677132 | 921 | rend() _GLIBCXX_NOEXCEPT |
874e7baa | 922 | { return reverse_iterator(begin()); } |
ed6814f7 | 923 | |
af5fb6ab BK |
924 | /** |
925 | * Returns a read-only (constant) reverse iterator that points | |
926 | * to one before the first element in the %vector. Iteration | |
927 | * is done in reverse element order. | |
928 | */ | |
1ae8edf5 | 929 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
af5fb6ab | 930 | const_reverse_iterator |
d3677132 | 931 | rend() const _GLIBCXX_NOEXCEPT |
874e7baa | 932 | { return const_reverse_iterator(begin()); } |
ed6814f7 | 933 | |
734f5023 | 934 | #if __cplusplus >= 201103L |
0cd50f89 PC |
935 | /** |
936 | * Returns a read-only (constant) iterator that points to the | |
937 | * first element in the %vector. Iteration is done in ordinary | |
938 | * element order. | |
939 | */ | |
1ae8edf5 | 940 | [[__nodiscard__]] _GLIBCXX20_CONSTEXPR |
0cd50f89 | 941 | const_iterator |
d3677132 | 942 | cbegin() const noexcept |
0cd50f89 PC |
943 | { return const_iterator(this->_M_impl._M_start); } |
944 | ||
945 | /** | |
946 | * Returns a read-only (constant) iterator that points one past | |
947 | * the last element in the %vector. Iteration is done in | |
948 | * ordinary element order. | |
949 | */ | |
1ae8edf5 | 950 | [[__nodiscard__]] _GLIBCXX20_CONSTEXPR |
0cd50f89 | 951 | const_iterator |
d3677132 | 952 | cend() const noexcept |
0cd50f89 PC |
953 | { return const_iterator(this->_M_impl._M_finish); } |
954 | ||
955 | /** | |
956 | * Returns a read-only (constant) reverse iterator that points | |
957 | * to the last element in the %vector. Iteration is done in | |
958 | * reverse element order. | |
959 | */ | |
1ae8edf5 | 960 | [[__nodiscard__]] _GLIBCXX20_CONSTEXPR |
0cd50f89 | 961 | const_reverse_iterator |
d3677132 | 962 | crbegin() const noexcept |
0cd50f89 PC |
963 | { return const_reverse_iterator(end()); } |
964 | ||
965 | /** | |
966 | * Returns a read-only (constant) reverse iterator that points | |
967 | * to one before the first element in the %vector. Iteration | |
968 | * is done in reverse element order. | |
969 | */ | |
1ae8edf5 | 970 | [[__nodiscard__]] _GLIBCXX20_CONSTEXPR |
0cd50f89 | 971 | const_reverse_iterator |
d3677132 | 972 | crend() const noexcept |
0cd50f89 PC |
973 | { return const_reverse_iterator(begin()); } |
974 | #endif | |
975 | ||
af5fb6ab BK |
976 | // [23.2.4.2] capacity |
977 | /** Returns the number of elements in the %vector. */ | |
1ae8edf5 | 978 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
af5fb6ab | 979 | size_type |
d3677132 | 980 | size() const _GLIBCXX_NOEXCEPT |
bc9053ab | 981 | { return size_type(this->_M_impl._M_finish - this->_M_impl._M_start); } |
ed6814f7 | 982 | |
af5fb6ab | 983 | /** Returns the size() of the largest possible %vector. */ |
1ae8edf5 | 984 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
af5fb6ab | 985 | size_type |
d3677132 | 986 | max_size() const _GLIBCXX_NOEXCEPT |
af55b3af | 987 | { return _S_max_size(_M_get_Tp_allocator()); } |
ed6814f7 | 988 | |
734f5023 | 989 | #if __cplusplus >= 201103L |
dc2cf706 PC |
990 | /** |
991 | * @brief Resizes the %vector to the specified number of elements. | |
93c66bc6 | 992 | * @param __new_size Number of elements the %vector should contain. |
dc2cf706 PC |
993 | * |
994 | * This function will %resize the %vector to the specified | |
995 | * number of elements. If the number is smaller than the | |
996 | * %vector's current size the %vector is truncated, otherwise | |
997 | * default constructed elements are appended. | |
998 | */ | |
1ae8edf5 | 999 | _GLIBCXX20_CONSTEXPR |
dc2cf706 PC |
1000 | void |
1001 | resize(size_type __new_size) | |
1002 | { | |
1003 | if (__new_size > size()) | |
1004 | _M_default_append(__new_size - size()); | |
1005 | else if (__new_size < size()) | |
1006 | _M_erase_at_end(this->_M_impl._M_start + __new_size); | |
1007 | } | |
1008 | ||
af5fb6ab BK |
1009 | /** |
1010 | * @brief Resizes the %vector to the specified number of elements. | |
93c66bc6 BK |
1011 | * @param __new_size Number of elements the %vector should contain. |
1012 | * @param __x Data with which new elements should be populated. | |
af5fb6ab BK |
1013 | * |
1014 | * This function will %resize the %vector to the specified | |
1015 | * number of elements. If the number is smaller than the | |
1016 | * %vector's current size the %vector is truncated, otherwise | |
1017 | * the %vector is extended and new elements are populated with | |
1018 | * given data. | |
1019 | */ | |
1ae8edf5 | 1020 | _GLIBCXX20_CONSTEXPR |
3971a4d2 | 1021 | void |
dc2cf706 | 1022 | resize(size_type __new_size, const value_type& __x) |
3971a4d2 | 1023 | { |
dc2cf706 | 1024 | if (__new_size > size()) |
d7e16fc5 | 1025 | _M_fill_insert(end(), __new_size - size(), __x); |
dc2cf706 | 1026 | else if (__new_size < size()) |
bc9053ab | 1027 | _M_erase_at_end(this->_M_impl._M_start + __new_size); |
dc2cf706 PC |
1028 | } |
1029 | #else | |
1030 | /** | |
1031 | * @brief Resizes the %vector to the specified number of elements. | |
93c66bc6 BK |
1032 | * @param __new_size Number of elements the %vector should contain. |
1033 | * @param __x Data with which new elements should be populated. | |
dc2cf706 PC |
1034 | * |
1035 | * This function will %resize the %vector to the specified | |
1036 | * number of elements. If the number is smaller than the | |
1037 | * %vector's current size the %vector is truncated, otherwise | |
1038 | * the %vector is extended and new elements are populated with | |
1039 | * given data. | |
1040 | */ | |
1ae8edf5 | 1041 | _GLIBCXX20_CONSTEXPR |
dc2cf706 PC |
1042 | void |
1043 | resize(size_type __new_size, value_type __x = value_type()) | |
1044 | { | |
1045 | if (__new_size > size()) | |
d7e16fc5 | 1046 | _M_fill_insert(end(), __new_size - size(), __x); |
dc2cf706 PC |
1047 | else if (__new_size < size()) |
1048 | _M_erase_at_end(this->_M_impl._M_start + __new_size); | |
3971a4d2 | 1049 | } |
dc2cf706 | 1050 | #endif |
ed6814f7 | 1051 | |
734f5023 | 1052 | #if __cplusplus >= 201103L |
79667f82 | 1053 | /** A non-binding request to reduce capacity() to size(). */ |
1ae8edf5 | 1054 | _GLIBCXX20_CONSTEXPR |
79667f82 PC |
1055 | void |
1056 | shrink_to_fit() | |
8a752dfe | 1057 | { _M_shrink_to_fit(); } |
79667f82 PC |
1058 | #endif |
1059 | ||
af5fb6ab | 1060 | /** |
e135a038 BK |
1061 | * Returns the total number of elements that the %vector can |
1062 | * hold before needing to allocate more memory. | |
af5fb6ab | 1063 | */ |
1ae8edf5 | 1064 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
af5fb6ab | 1065 | size_type |
d3677132 | 1066 | capacity() const _GLIBCXX_NOEXCEPT |
bc9053ab PC |
1067 | { return size_type(this->_M_impl._M_end_of_storage |
1068 | - this->_M_impl._M_start); } | |
ed6814f7 | 1069 | |
af5fb6ab BK |
1070 | /** |
1071 | * Returns true if the %vector is empty. (Thus begin() would | |
1072 | * equal end().) | |
1073 | */ | |
1ae8edf5 JW |
1074 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
1075 | bool | |
d3677132 | 1076 | empty() const _GLIBCXX_NOEXCEPT |
874e7baa | 1077 | { return begin() == end(); } |
ed6814f7 | 1078 | |
af5fb6ab BK |
1079 | /** |
1080 | * @brief Attempt to preallocate enough memory for specified number of | |
1081 | * elements. | |
93c66bc6 | 1082 | * @param __n Number of elements required. |
af5fb6ab BK |
1083 | * @throw std::length_error If @a n exceeds @c max_size(). |
1084 | * | |
1085 | * This function attempts to reserve enough memory for the | |
1086 | * %vector to hold the specified number of elements. If the | |
1087 | * number requested is more than max_size(), length_error is | |
1088 | * thrown. | |
1089 | * | |
1090 | * The advantage of this function is that if optimal code is a | |
1091 | * necessity and the user can determine the number of elements | |
1092 | * that will be required, the user can reserve the memory in | |
1093 | * %advance, and thus prevent a possible reallocation of memory | |
1094 | * and copying of %vector data. | |
1095 | */ | |
1ae8edf5 | 1096 | _GLIBCXX20_CONSTEXPR |
af5fb6ab BK |
1097 | void |
1098 | reserve(size_type __n); | |
ed6814f7 | 1099 | |
af5fb6ab BK |
1100 | // element access |
1101 | /** | |
1102 | * @brief Subscript access to the data contained in the %vector. | |
93c66bc6 | 1103 | * @param __n The index of the element for which data should be |
e135a038 | 1104 | * accessed. |
af5fb6ab BK |
1105 | * @return Read/write reference to data. |
1106 | * | |
1107 | * This operator allows for easy, array-style, data access. | |
1108 | * Note that data access with this operator is unchecked and | |
1109 | * out_of_range lookups are not defined. (For checked lookups | |
1110 | * see at().) | |
1111 | */ | |
1ae8edf5 | 1112 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
af5fb6ab | 1113 | reference |
757b1644 | 1114 | operator[](size_type __n) _GLIBCXX_NOEXCEPT |
bd2ee798 FD |
1115 | { |
1116 | __glibcxx_requires_subscript(__n); | |
1117 | return *(this->_M_impl._M_start + __n); | |
1118 | } | |
ed6814f7 | 1119 | |
af5fb6ab BK |
1120 | /** |
1121 | * @brief Subscript access to the data contained in the %vector. | |
93c66bc6 | 1122 | * @param __n The index of the element for which data should be |
af5fb6ab BK |
1123 | * accessed. |
1124 | * @return Read-only (constant) reference to data. | |
1125 | * | |
1126 | * This operator allows for easy, array-style, data access. | |
1127 | * Note that data access with this operator is unchecked and | |
1128 | * out_of_range lookups are not defined. (For checked lookups | |
1129 | * see at().) | |
1130 | */ | |
1ae8edf5 | 1131 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
af5fb6ab | 1132 | const_reference |
757b1644 | 1133 | operator[](size_type __n) const _GLIBCXX_NOEXCEPT |
bd2ee798 FD |
1134 | { |
1135 | __glibcxx_requires_subscript(__n); | |
1136 | return *(this->_M_impl._M_start + __n); | |
1137 | } | |
ed6814f7 | 1138 | |
af5fb6ab | 1139 | protected: |
4312e020 | 1140 | /// Safety check used only from at(). |
1ae8edf5 | 1141 | _GLIBCXX20_CONSTEXPR |
3971a4d2 | 1142 | void |
af5fb6ab | 1143 | _M_range_check(size_type __n) const |
3971a4d2 | 1144 | { |
af5fb6ab | 1145 | if (__n >= this->size()) |
9779c871 PP |
1146 | __throw_out_of_range_fmt(__N("vector::_M_range_check: __n " |
1147 | "(which is %zu) >= this->size() " | |
1148 | "(which is %zu)"), | |
1149 | __n, this->size()); | |
3971a4d2 | 1150 | } |
ed6814f7 | 1151 | |
af5fb6ab BK |
1152 | public: |
1153 | /** | |
1154 | * @brief Provides access to the data contained in the %vector. | |
93c66bc6 | 1155 | * @param __n The index of the element for which data should be |
af5fb6ab BK |
1156 | * accessed. |
1157 | * @return Read/write reference to data. | |
93c66bc6 | 1158 | * @throw std::out_of_range If @a __n is an invalid index. |
af5fb6ab | 1159 | * |
e135a038 BK |
1160 | * This function provides for safer data access. The parameter |
1161 | * is first checked that it is in the range of the vector. The | |
1162 | * function throws out_of_range if the check fails. | |
af5fb6ab | 1163 | */ |
1ae8edf5 | 1164 | _GLIBCXX20_CONSTEXPR |
af5fb6ab | 1165 | reference |
874e7baa PC |
1166 | at(size_type __n) |
1167 | { | |
1168 | _M_range_check(__n); | |
fe62dd04 | 1169 | return (*this)[__n]; |
874e7baa | 1170 | } |
ed6814f7 | 1171 | |
af5fb6ab BK |
1172 | /** |
1173 | * @brief Provides access to the data contained in the %vector. | |
93c66bc6 | 1174 | * @param __n The index of the element for which data should be |
af5fb6ab BK |
1175 | * accessed. |
1176 | * @return Read-only (constant) reference to data. | |
93c66bc6 | 1177 | * @throw std::out_of_range If @a __n is an invalid index. |
af5fb6ab BK |
1178 | * |
1179 | * This function provides for safer data access. The parameter | |
1180 | * is first checked that it is in the range of the vector. The | |
1181 | * function throws out_of_range if the check fails. | |
1182 | */ | |
1ae8edf5 | 1183 | _GLIBCXX20_CONSTEXPR |
af5fb6ab | 1184 | const_reference |
874e7baa PC |
1185 | at(size_type __n) const |
1186 | { | |
1187 | _M_range_check(__n); | |
1188 | return (*this)[__n]; | |
1189 | } | |
ed6814f7 | 1190 | |
af5fb6ab BK |
1191 | /** |
1192 | * Returns a read/write reference to the data at the first | |
1193 | * element of the %vector. | |
1194 | */ | |
1ae8edf5 | 1195 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
af5fb6ab | 1196 | reference |
757b1644 | 1197 | front() _GLIBCXX_NOEXCEPT |
bd2ee798 FD |
1198 | { |
1199 | __glibcxx_requires_nonempty(); | |
1200 | return *begin(); | |
1201 | } | |
ed6814f7 | 1202 | |
af5fb6ab BK |
1203 | /** |
1204 | * Returns a read-only (constant) reference to the data at the first | |
1205 | * element of the %vector. | |
1206 | */ | |
1ae8edf5 | 1207 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
af5fb6ab | 1208 | const_reference |
757b1644 | 1209 | front() const _GLIBCXX_NOEXCEPT |
bd2ee798 FD |
1210 | { |
1211 | __glibcxx_requires_nonempty(); | |
1212 | return *begin(); | |
1213 | } | |
ed6814f7 | 1214 | |
af5fb6ab | 1215 | /** |
e135a038 BK |
1216 | * Returns a read/write reference to the data at the last |
1217 | * element of the %vector. | |
af5fb6ab | 1218 | */ |
1ae8edf5 | 1219 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
af5fb6ab | 1220 | reference |
757b1644 | 1221 | back() _GLIBCXX_NOEXCEPT |
bd2ee798 FD |
1222 | { |
1223 | __glibcxx_requires_nonempty(); | |
1224 | return *(end() - 1); | |
1225 | } | |
fe62dd04 | 1226 | |
af5fb6ab | 1227 | /** |
e135a038 BK |
1228 | * Returns a read-only (constant) reference to the data at the |
1229 | * last element of the %vector. | |
af5fb6ab | 1230 | */ |
1ae8edf5 | 1231 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
af5fb6ab | 1232 | const_reference |
757b1644 | 1233 | back() const _GLIBCXX_NOEXCEPT |
bd2ee798 FD |
1234 | { |
1235 | __glibcxx_requires_nonempty(); | |
1236 | return *(end() - 1); | |
1237 | } | |
ed6814f7 | 1238 | |
8b5f07a2 PC |
1239 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
1240 | // DR 464. Suggestion for new member functions in standard containers. | |
1241 | // data access | |
1242 | /** | |
1243 | * Returns a pointer such that [data(), data() + size()) is a valid | |
1244 | * range. For a non-empty %vector, data() == &front(). | |
1245 | */ | |
1ae8edf5 | 1246 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
2fb16a39 | 1247 | _Tp* |
d3677132 | 1248 | data() _GLIBCXX_NOEXCEPT |
8a972abd | 1249 | { return _M_data_ptr(this->_M_impl._M_start); } |
8b5f07a2 | 1250 | |
1ae8edf5 | 1251 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
2fb16a39 | 1252 | const _Tp* |
d3677132 | 1253 | data() const _GLIBCXX_NOEXCEPT |
8a972abd | 1254 | { return _M_data_ptr(this->_M_impl._M_start); } |
8b5f07a2 | 1255 | |
af5fb6ab BK |
1256 | // [23.2.4.3] modifiers |
1257 | /** | |
1258 | * @brief Add data to the end of the %vector. | |
93c66bc6 | 1259 | * @param __x Data to be added. |
af5fb6ab BK |
1260 | * |
1261 | * This is a typical stack operation. The function creates an | |
1262 | * element at the end of the %vector and assigns the given data | |
1263 | * to it. Due to the nature of a %vector this operation can be | |
1264 | * done in constant time if the %vector has preallocated space | |
1265 | * available. | |
1266 | */ | |
1ae8edf5 | 1267 | _GLIBCXX20_CONSTEXPR |
3971a4d2 | 1268 | void |
af5fb6ab | 1269 | push_back(const value_type& __x) |
3971a4d2 | 1270 | { |
03f9ea44 | 1271 | if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage) |
af5fb6ab | 1272 | { |
8c7331c5 | 1273 | _GLIBCXX_ASAN_ANNOTATE_GROW(1); |
bd8485dc | 1274 | _Alloc_traits::construct(this->_M_impl, this->_M_impl._M_finish, |
d7e16fc5 | 1275 | __x); |
03f9ea44 | 1276 | ++this->_M_impl._M_finish; |
8c7331c5 | 1277 | _GLIBCXX_ASAN_ANNOTATE_GREW(1); |
af5fb6ab BK |
1278 | } |
1279 | else | |
76225d2c | 1280 | _M_realloc_insert(end(), __x); |
3971a4d2 | 1281 | } |
4dc3e453 | 1282 | |
734f5023 | 1283 | #if __cplusplus >= 201103L |
1ae8edf5 | 1284 | _GLIBCXX20_CONSTEXPR |
4dc3e453 PC |
1285 | void |
1286 | push_back(value_type&& __x) | |
1287 | { emplace_back(std::move(__x)); } | |
1288 | ||
6eef7402 | 1289 | template<typename... _Args> |
594ef205 | 1290 | #if __cplusplus > 201402L |
1ae8edf5 | 1291 | _GLIBCXX20_CONSTEXPR |
fe62dd04 | 1292 | reference |
594ef205 | 1293 | #else |
d7e16fc5 | 1294 | void |
594ef205 | 1295 | #endif |
d7e16fc5 | 1296 | emplace_back(_Args&&... __args); |
6eef7402 | 1297 | #endif |
ed6814f7 | 1298 | |
af5fb6ab BK |
1299 | /** |
1300 | * @brief Removes last element. | |
1301 | * | |
1302 | * This is a typical stack operation. It shrinks the %vector by one. | |
1303 | * | |
e135a038 BK |
1304 | * Note that no data is returned, and if the last element's |
1305 | * data is needed, it should be retrieved before pop_back() is | |
1306 | * called. | |
af5fb6ab | 1307 | */ |
1ae8edf5 | 1308 | _GLIBCXX20_CONSTEXPR |
3971a4d2 | 1309 | void |
757b1644 | 1310 | pop_back() _GLIBCXX_NOEXCEPT |
3971a4d2 | 1311 | { |
bd2ee798 | 1312 | __glibcxx_requires_nonempty(); |
03f9ea44 | 1313 | --this->_M_impl._M_finish; |
bd8485dc | 1314 | _Alloc_traits::destroy(this->_M_impl, this->_M_impl._M_finish); |
8c7331c5 | 1315 | _GLIBCXX_ASAN_ANNOTATE_SHRINK(1); |
3971a4d2 | 1316 | } |
ed6814f7 | 1317 | |
734f5023 | 1318 | #if __cplusplus >= 201103L |
6eef7402 CJ |
1319 | /** |
1320 | * @brief Inserts an object in %vector before specified iterator. | |
7b61c5a9 | 1321 | * @param __position A const_iterator into the %vector. |
93c66bc6 | 1322 | * @param __args Arguments. |
6eef7402 CJ |
1323 | * @return An iterator that points to the inserted data. |
1324 | * | |
1325 | * This function will insert an object of type T constructed | |
1326 | * with T(std::forward<Args>(args)...) before the specified location. | |
1327 | * Note that this kind of operation could be expensive for a %vector | |
1328 | * and if it is frequently used the user should consider using | |
1329 | * std::list. | |
1330 | */ | |
1331 | template<typename... _Args> | |
1ae8edf5 | 1332 | _GLIBCXX20_CONSTEXPR |
d7e16fc5 | 1333 | iterator |
9958c7eb JW |
1334 | emplace(const_iterator __position, _Args&&... __args) |
1335 | { return _M_emplace_aux(__position, std::forward<_Args>(__args)...); } | |
6eef7402 | 1336 | |
7b61c5a9 PC |
1337 | /** |
1338 | * @brief Inserts given value into %vector before specified iterator. | |
1339 | * @param __position A const_iterator into the %vector. | |
1340 | * @param __x Data to be inserted. | |
1341 | * @return An iterator that points to the inserted data. | |
1342 | * | |
1343 | * This function will insert a copy of the given value before | |
1344 | * the specified location. Note that this kind of operation | |
1345 | * could be expensive for a %vector and if it is frequently | |
1346 | * used the user should consider using std::list. | |
1347 | */ | |
1ae8edf5 | 1348 | _GLIBCXX20_CONSTEXPR |
7b61c5a9 PC |
1349 | iterator |
1350 | insert(const_iterator __position, const value_type& __x); | |
1351 | #else | |
af5fb6ab BK |
1352 | /** |
1353 | * @brief Inserts given value into %vector before specified iterator. | |
93c66bc6 BK |
1354 | * @param __position An iterator into the %vector. |
1355 | * @param __x Data to be inserted. | |
af5fb6ab BK |
1356 | * @return An iterator that points to the inserted data. |
1357 | * | |
1358 | * This function will insert a copy of the given value before | |
1359 | * the specified location. Note that this kind of operation | |
1360 | * could be expensive for a %vector and if it is frequently | |
1361 | * used the user should consider using std::list. | |
1362 | */ | |
1363 | iterator | |
1364 | insert(iterator __position, const value_type& __x); | |
7b61c5a9 | 1365 | #endif |
3a9fdf30 | 1366 | |
734f5023 | 1367 | #if __cplusplus >= 201103L |
6eef7402 CJ |
1368 | /** |
1369 | * @brief Inserts given rvalue into %vector before specified iterator. | |
7b61c5a9 | 1370 | * @param __position A const_iterator into the %vector. |
93c66bc6 | 1371 | * @param __x Data to be inserted. |
6eef7402 CJ |
1372 | * @return An iterator that points to the inserted data. |
1373 | * | |
1374 | * This function will insert a copy of the given rvalue before | |
1375 | * the specified location. Note that this kind of operation | |
1376 | * could be expensive for a %vector and if it is frequently | |
1377 | * used the user should consider using std::list. | |
1378 | */ | |
1ae8edf5 | 1379 | _GLIBCXX20_CONSTEXPR |
6eef7402 | 1380 | iterator |
7b61c5a9 | 1381 | insert(const_iterator __position, value_type&& __x) |
9958c7eb | 1382 | { return _M_insert_rval(__position, std::move(__x)); } |
988499f4 JM |
1383 | |
1384 | /** | |
1385 | * @brief Inserts an initializer_list into the %vector. | |
93c66bc6 BK |
1386 | * @param __position An iterator into the %vector. |
1387 | * @param __l An initializer_list. | |
988499f4 | 1388 | * |
fe62dd04 | 1389 | * This function will insert copies of the data in the |
988499f4 JM |
1390 | * initializer_list @a l into the %vector before the location |
1391 | * specified by @a position. | |
1392 | * | |
1393 | * Note that this kind of operation could be expensive for a | |
1394 | * %vector and if it is frequently used the user should | |
1395 | * consider using std::list. | |
1396 | */ | |
1ae8edf5 | 1397 | _GLIBCXX20_CONSTEXPR |
06eed9f5 PC |
1398 | iterator |
1399 | insert(const_iterator __position, initializer_list<value_type> __l) | |
d7e16fc5 FD |
1400 | { |
1401 | auto __offset = __position - cbegin(); | |
1402 | _M_range_insert(begin() + __offset, __l.begin(), __l.end(), | |
1403 | std::random_access_iterator_tag()); | |
1404 | return begin() + __offset; | |
1405 | } | |
6eef7402 CJ |
1406 | #endif |
1407 | ||
06eed9f5 PC |
1408 | #if __cplusplus >= 201103L |
1409 | /** | |
1410 | * @brief Inserts a number of copies of given data into the %vector. | |
1411 | * @param __position A const_iterator into the %vector. | |
1412 | * @param __n Number of elements to be inserted. | |
1413 | * @param __x Data to be inserted. | |
1414 | * @return An iterator that points to the inserted data. | |
1415 | * | |
1416 | * This function will insert a specified number of copies of | |
1417 | * the given data before the location specified by @a position. | |
1418 | * | |
1419 | * Note that this kind of operation could be expensive for a | |
1420 | * %vector and if it is frequently used the user should | |
1421 | * consider using std::list. | |
1422 | */ | |
1ae8edf5 | 1423 | _GLIBCXX20_CONSTEXPR |
06eed9f5 PC |
1424 | iterator |
1425 | insert(const_iterator __position, size_type __n, const value_type& __x) | |
1426 | { | |
1427 | difference_type __offset = __position - cbegin(); | |
bbf264c9 | 1428 | _M_fill_insert(begin() + __offset, __n, __x); |
06eed9f5 PC |
1429 | return begin() + __offset; |
1430 | } | |
1431 | #else | |
af5fb6ab BK |
1432 | /** |
1433 | * @brief Inserts a number of copies of given data into the %vector. | |
93c66bc6 BK |
1434 | * @param __position An iterator into the %vector. |
1435 | * @param __n Number of elements to be inserted. | |
1436 | * @param __x Data to be inserted. | |
af5fb6ab BK |
1437 | * |
1438 | * This function will insert a specified number of copies of | |
1439 | * the given data before the location specified by @a position. | |
1440 | * | |
1441 | * Note that this kind of operation could be expensive for a | |
1442 | * %vector and if it is frequently used the user should | |
1443 | * consider using std::list. | |
1444 | */ | |
1445 | void | |
08addde6 PE |
1446 | insert(iterator __position, size_type __n, const value_type& __x) |
1447 | { _M_fill_insert(__position, __n, __x); } | |
06eed9f5 | 1448 | #endif |
ed6814f7 | 1449 | |
06eed9f5 | 1450 | #if __cplusplus >= 201103L |
af5fb6ab BK |
1451 | /** |
1452 | * @brief Inserts a range into the %vector. | |
06eed9f5 | 1453 | * @param __position A const_iterator into the %vector. |
93c66bc6 BK |
1454 | * @param __first An input iterator. |
1455 | * @param __last An input iterator. | |
06eed9f5 | 1456 | * @return An iterator that points to the inserted data. |
af5fb6ab BK |
1457 | * |
1458 | * This function will insert copies of the data in the range | |
93c66bc6 | 1459 | * [__first,__last) into the %vector before the location specified |
af5fb6ab BK |
1460 | * by @a pos. |
1461 | * | |
1462 | * Note that this kind of operation could be expensive for a | |
1463 | * %vector and if it is frequently used the user should | |
1464 | * consider using std::list. | |
1465 | */ | |
2203cb90 PC |
1466 | template<typename _InputIterator, |
1467 | typename = std::_RequireInputIter<_InputIterator>> | |
1ae8edf5 | 1468 | _GLIBCXX20_CONSTEXPR |
d7e16fc5 FD |
1469 | iterator |
1470 | insert(const_iterator __position, _InputIterator __first, | |
2203cb90 | 1471 | _InputIterator __last) |
d7e16fc5 | 1472 | { |
06eed9f5 | 1473 | difference_type __offset = __position - cbegin(); |
bbf264c9 | 1474 | _M_insert_dispatch(begin() + __offset, |
06eed9f5 PC |
1475 | __first, __last, __false_type()); |
1476 | return begin() + __offset; | |
1477 | } | |
2203cb90 | 1478 | #else |
06eed9f5 PC |
1479 | /** |
1480 | * @brief Inserts a range into the %vector. | |
1481 | * @param __position An iterator into the %vector. | |
1482 | * @param __first An input iterator. | |
1483 | * @param __last An input iterator. | |
1484 | * | |
1485 | * This function will insert copies of the data in the range | |
1486 | * [__first,__last) into the %vector before the location specified | |
1487 | * by @a pos. | |
1488 | * | |
1489 | * Note that this kind of operation could be expensive for a | |
1490 | * %vector and if it is frequently used the user should | |
1491 | * consider using std::list. | |
1492 | */ | |
af5fb6ab | 1493 | template<typename _InputIterator> |
d7e16fc5 FD |
1494 | void |
1495 | insert(iterator __position, _InputIterator __first, | |
e135a038 | 1496 | _InputIterator __last) |
d7e16fc5 | 1497 | { |
af5fb6ab | 1498 | // Check whether it's an integral type. If so, it's not an iterator. |
c0736a9d | 1499 | typedef typename std::__is_integer<_InputIterator>::__type _Integral; |
08addde6 | 1500 | _M_insert_dispatch(__position, __first, __last, _Integral()); |
af5fb6ab | 1501 | } |
2203cb90 | 1502 | #endif |
ed6814f7 | 1503 | |
af5fb6ab BK |
1504 | /** |
1505 | * @brief Remove element at given position. | |
93c66bc6 | 1506 | * @param __position Iterator pointing to element to be erased. |
af5fb6ab BK |
1507 | * @return An iterator pointing to the next element (or end()). |
1508 | * | |
1509 | * This function will erase the element at the given position and thus | |
1510 | * shorten the %vector by one. | |
1511 | * | |
1512 | * Note This operation could be expensive and if it is | |
1513 | * frequently used the user should consider using std::list. | |
1514 | * The user is also cautioned that this function only erases | |
1515 | * the element, and that if the element is itself a pointer, | |
1516 | * the pointed-to memory is not touched in any way. Managing | |
28dac70a | 1517 | * the pointer is the user's responsibility. |
af5fb6ab | 1518 | */ |
1ae8edf5 | 1519 | _GLIBCXX20_CONSTEXPR |
af5fb6ab | 1520 | iterator |
94938aec PC |
1521 | #if __cplusplus >= 201103L |
1522 | erase(const_iterator __position) | |
bbf264c9 | 1523 | { return _M_erase(begin() + (__position - cbegin())); } |
94938aec PC |
1524 | #else |
1525 | erase(iterator __position) | |
bbf264c9 | 1526 | { return _M_erase(__position); } |
94938aec | 1527 | #endif |
ed6814f7 | 1528 | |
af5fb6ab BK |
1529 | /** |
1530 | * @brief Remove a range of elements. | |
93c66bc6 BK |
1531 | * @param __first Iterator pointing to the first element to be erased. |
1532 | * @param __last Iterator pointing to one past the last element to be | |
1533 | * erased. | |
1534 | * @return An iterator pointing to the element pointed to by @a __last | |
af5fb6ab BK |
1535 | * prior to erasing (or end()). |
1536 | * | |
93c66bc6 BK |
1537 | * This function will erase the elements in the range |
1538 | * [__first,__last) and shorten the %vector accordingly. | |
af5fb6ab BK |
1539 | * |
1540 | * Note This operation could be expensive and if it is | |
1541 | * frequently used the user should consider using std::list. | |
1542 | * The user is also cautioned that this function only erases | |
1543 | * the elements, and that if the elements themselves are | |
1544 | * pointers, the pointed-to memory is not touched in any way. | |
28dac70a | 1545 | * Managing the pointer is the user's responsibility. |
af5fb6ab | 1546 | */ |
1ae8edf5 | 1547 | _GLIBCXX20_CONSTEXPR |
af5fb6ab | 1548 | iterator |
94938aec PC |
1549 | #if __cplusplus >= 201103L |
1550 | erase(const_iterator __first, const_iterator __last) | |
bbf264c9 JW |
1551 | { |
1552 | const auto __beg = begin(); | |
1553 | const auto __cbeg = cbegin(); | |
1554 | return _M_erase(__beg + (__first - __cbeg), __beg + (__last - __cbeg)); | |
1555 | } | |
94938aec PC |
1556 | #else |
1557 | erase(iterator __first, iterator __last) | |
bbf264c9 | 1558 | { return _M_erase(__first, __last); } |
94938aec | 1559 | #endif |
ed6814f7 | 1560 | |
af5fb6ab BK |
1561 | /** |
1562 | * @brief Swaps data with another %vector. | |
93c66bc6 | 1563 | * @param __x A %vector of the same element and allocator types. |
af5fb6ab BK |
1564 | * |
1565 | * This exchanges the elements between two vectors in constant time. | |
1566 | * (Three pointers, so it should be quite fast.) | |
1567 | * Note that the global std::swap() function is specialized such that | |
1568 | * std::swap(v1,v2) will feed to this function. | |
0a2bf188 JW |
1569 | * |
1570 | * Whether the allocators are swapped depends on the allocator traits. | |
af5fb6ab | 1571 | */ |
1ae8edf5 | 1572 | _GLIBCXX20_CONSTEXPR |
3971a4d2 | 1573 | void |
c5d9ec56 | 1574 | swap(vector& __x) _GLIBCXX_NOEXCEPT |
3971a4d2 | 1575 | { |
bd2ee798 FD |
1576 | #if __cplusplus >= 201103L |
1577 | __glibcxx_assert(_Alloc_traits::propagate_on_container_swap::value | |
1578 | || _M_get_Tp_allocator() == __x._M_get_Tp_allocator()); | |
1579 | #endif | |
bd8485dc JW |
1580 | this->_M_impl._M_swap_data(__x._M_impl); |
1581 | _Alloc_traits::_S_on_swap(_M_get_Tp_allocator(), | |
d7e16fc5 | 1582 | __x._M_get_Tp_allocator()); |
3971a4d2 | 1583 | } |
ed6814f7 | 1584 | |
af5fb6ab BK |
1585 | /** |
1586 | * Erases all the elements. Note that this function only erases the | |
1587 | * elements, and that if the elements themselves are pointers, the | |
1588 | * pointed-to memory is not touched in any way. Managing the pointer is | |
28dac70a | 1589 | * the user's responsibility. |
af5fb6ab | 1590 | */ |
1ae8edf5 | 1591 | _GLIBCXX20_CONSTEXPR |
3971a4d2 | 1592 | void |
d3677132 | 1593 | clear() _GLIBCXX_NOEXCEPT |
bc9053ab | 1594 | { _M_erase_at_end(this->_M_impl._M_start); } |
ed6814f7 | 1595 | |
af5fb6ab BK |
1596 | protected: |
1597 | /** | |
af5fb6ab BK |
1598 | * Memory expansion handler. Uses the member allocation function to |
1599 | * obtain @a n bytes of memory, and then copies [first,last) into it. | |
af5fb6ab BK |
1600 | */ |
1601 | template<typename _ForwardIterator> | |
1ae8edf5 | 1602 | _GLIBCXX20_CONSTEXPR |
d7e16fc5 FD |
1603 | pointer |
1604 | _M_allocate_and_copy(size_type __n, | |
af5fb6ab | 1605 | _ForwardIterator __first, _ForwardIterator __last) |
d7e16fc5 | 1606 | { |
f2ffecb1 | 1607 | pointer __result = this->_M_allocate(__n); |
bc2631e0 | 1608 | __try |
af5fb6ab | 1609 | { |
1985f1cd | 1610 | std::__uninitialized_copy_a(__first, __last, __result, |
4fd20a8f | 1611 | _M_get_Tp_allocator()); |
af5fb6ab BK |
1612 | return __result; |
1613 | } | |
bc2631e0 | 1614 | __catch(...) |
af5fb6ab BK |
1615 | { |
1616 | _M_deallocate(__result, __n); | |
1617 | __throw_exception_again; | |
1618 | } | |
1619 | } | |
ed6814f7 BI |
1620 | |
1621 | ||
af5fb6ab | 1622 | // Internal constructor functions follow. |
ed6814f7 | 1623 | |
af5fb6ab | 1624 | // Called by the range constructor to implement [23.1.1]/9 |
25959e29 | 1625 | |
e6cad987 | 1626 | #if __cplusplus < 201103L |
25959e29 PC |
1627 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
1628 | // 438. Ambiguity in the "do the right thing" clause | |
af5fb6ab | 1629 | template<typename _Integer> |
d7e16fc5 FD |
1630 | void |
1631 | _M_initialize_dispatch(_Integer __n, _Integer __value, __true_type) | |
1632 | { | |
af55b3af JW |
1633 | this->_M_impl._M_start = _M_allocate(_S_check_init_len( |
1634 | static_cast<size_type>(__n), _M_get_Tp_allocator())); | |
25959e29 PC |
1635 | this->_M_impl._M_end_of_storage = |
1636 | this->_M_impl._M_start + static_cast<size_type>(__n); | |
f4c5578f | 1637 | _M_fill_initialize(static_cast<size_type>(__n), __value); |
af5fb6ab | 1638 | } |
ed6814f7 | 1639 | |
af5fb6ab | 1640 | // Called by the range constructor to implement [23.1.1]/9 |
08addde6 | 1641 | template<typename _InputIterator> |
d7e16fc5 FD |
1642 | void |
1643 | _M_initialize_dispatch(_InputIterator __first, _InputIterator __last, | |
af5fb6ab | 1644 | __false_type) |
d7e16fc5 | 1645 | { |
e6cad987 FD |
1646 | _M_range_initialize(__first, __last, |
1647 | std::__iterator_category(__first)); | |
af5fb6ab | 1648 | } |
e6cad987 | 1649 | #endif |
ed6814f7 | 1650 | |
af5fb6ab BK |
1651 | // Called by the second initialize_dispatch above |
1652 | template<typename _InputIterator> | |
1ae8edf5 | 1653 | _GLIBCXX20_CONSTEXPR |
d7e16fc5 | 1654 | void |
3685dcd7 JW |
1655 | _M_range_initialize(_InputIterator __first, _InputIterator __last, |
1656 | std::input_iterator_tag) | |
d7e16fc5 | 1657 | { |
3685dcd7 JW |
1658 | __try { |
1659 | for (; __first != __last; ++__first) | |
ad6fdc19 | 1660 | #if __cplusplus >= 201103L |
3685dcd7 | 1661 | emplace_back(*__first); |
ad6fdc19 | 1662 | #else |
3685dcd7 | 1663 | push_back(*__first); |
ad6fdc19 | 1664 | #endif |
3685dcd7 JW |
1665 | } __catch(...) { |
1666 | clear(); | |
1667 | __throw_exception_again; | |
1668 | } | |
af5fb6ab | 1669 | } |
ed6814f7 | 1670 | |
af5fb6ab BK |
1671 | // Called by the second initialize_dispatch above |
1672 | template<typename _ForwardIterator> | |
1ae8edf5 | 1673 | _GLIBCXX20_CONSTEXPR |
d7e16fc5 | 1674 | void |
3685dcd7 JW |
1675 | _M_range_initialize(_ForwardIterator __first, _ForwardIterator __last, |
1676 | std::forward_iterator_tag) | |
d7e16fc5 | 1677 | { |
43da93a7 | 1678 | const size_type __n = std::distance(__first, __last); |
af55b3af JW |
1679 | this->_M_impl._M_start |
1680 | = this->_M_allocate(_S_check_init_len(__n, _M_get_Tp_allocator())); | |
03f9ea44 | 1681 | this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __n; |
1985f1cd MA |
1682 | this->_M_impl._M_finish = |
1683 | std::__uninitialized_copy_a(__first, __last, | |
1684 | this->_M_impl._M_start, | |
4fd20a8f | 1685 | _M_get_Tp_allocator()); |
af5fb6ab | 1686 | } |
ed6814f7 | 1687 | |
f4c5578f PC |
1688 | // Called by the first initialize_dispatch above and by the |
1689 | // vector(n,value,a) constructor. | |
1ae8edf5 | 1690 | _GLIBCXX20_CONSTEXPR |
266a2cba | 1691 | void |
f4c5578f PC |
1692 | _M_fill_initialize(size_type __n, const value_type& __value) |
1693 | { | |
e51cf2f5 JW |
1694 | this->_M_impl._M_finish = |
1695 | std::__uninitialized_fill_n_a(this->_M_impl._M_start, __n, __value, | |
1696 | _M_get_Tp_allocator()); | |
f4c5578f PC |
1697 | } |
1698 | ||
734f5023 | 1699 | #if __cplusplus >= 201103L |
dc2cf706 | 1700 | // Called by the vector(n) constructor. |
1ae8edf5 | 1701 | _GLIBCXX20_CONSTEXPR |
dc2cf706 PC |
1702 | void |
1703 | _M_default_initialize(size_type __n) | |
1704 | { | |
e51cf2f5 JW |
1705 | this->_M_impl._M_finish = |
1706 | std::__uninitialized_default_n_a(this->_M_impl._M_start, __n, | |
1707 | _M_get_Tp_allocator()); | |
dc2cf706 PC |
1708 | } |
1709 | #endif | |
ed6814f7 | 1710 | |
af5fb6ab BK |
1711 | // Internal assign functions follow. The *_aux functions do the actual |
1712 | // assignment work for the range versions. | |
ed6814f7 | 1713 | |
af5fb6ab | 1714 | // Called by the range assign to implement [23.1.1]/9 |
25959e29 PC |
1715 | |
1716 | // _GLIBCXX_RESOLVE_LIB_DEFECTS | |
1717 | // 438. Ambiguity in the "do the right thing" clause | |
af5fb6ab | 1718 | template<typename _Integer> |
1ae8edf5 | 1719 | _GLIBCXX20_CONSTEXPR |
d7e16fc5 FD |
1720 | void |
1721 | _M_assign_dispatch(_Integer __n, _Integer __val, __true_type) | |
1722 | { _M_fill_assign(__n, __val); } | |
ed6814f7 | 1723 | |
af5fb6ab | 1724 | // Called by the range assign to implement [23.1.1]/9 |
08addde6 | 1725 | template<typename _InputIterator> |
1ae8edf5 | 1726 | _GLIBCXX20_CONSTEXPR |
d7e16fc5 FD |
1727 | void |
1728 | _M_assign_dispatch(_InputIterator __first, _InputIterator __last, | |
e135a038 | 1729 | __false_type) |
d7e16fc5 | 1730 | { _M_assign_aux(__first, __last, std::__iterator_category(__first)); } |
ed6814f7 | 1731 | |
af5fb6ab BK |
1732 | // Called by the second assign_dispatch above |
1733 | template<typename _InputIterator> | |
1ae8edf5 | 1734 | _GLIBCXX20_CONSTEXPR |
d7e16fc5 FD |
1735 | void |
1736 | _M_assign_aux(_InputIterator __first, _InputIterator __last, | |
6323b34e | 1737 | std::input_iterator_tag); |
ed6814f7 | 1738 | |
af5fb6ab BK |
1739 | // Called by the second assign_dispatch above |
1740 | template<typename _ForwardIterator> | |
1ae8edf5 | 1741 | _GLIBCXX20_CONSTEXPR |
d7e16fc5 FD |
1742 | void |
1743 | _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last, | |
6323b34e | 1744 | std::forward_iterator_tag); |
ed6814f7 | 1745 | |
af5fb6ab BK |
1746 | // Called by assign(n,t), and the range assign when it turns out |
1747 | // to be the same thing. | |
1ae8edf5 | 1748 | _GLIBCXX20_CONSTEXPR |
3971a4d2 | 1749 | void |
af5fb6ab | 1750 | _M_fill_assign(size_type __n, const value_type& __val); |
ed6814f7 | 1751 | |
af5fb6ab | 1752 | // Internal insert functions follow. |
ed6814f7 | 1753 | |
af5fb6ab | 1754 | // Called by the range insert to implement [23.1.1]/9 |
25959e29 PC |
1755 | |
1756 | // _GLIBCXX_RESOLVE_LIB_DEFECTS | |
1757 | // 438. Ambiguity in the "do the right thing" clause | |
af5fb6ab | 1758 | template<typename _Integer> |
1ae8edf5 | 1759 | _GLIBCXX20_CONSTEXPR |
d7e16fc5 FD |
1760 | void |
1761 | _M_insert_dispatch(iterator __pos, _Integer __n, _Integer __val, | |
af5fb6ab | 1762 | __true_type) |
d7e16fc5 | 1763 | { _M_fill_insert(__pos, __n, __val); } |
ed6814f7 | 1764 | |
af5fb6ab BK |
1765 | // Called by the range insert to implement [23.1.1]/9 |
1766 | template<typename _InputIterator> | |
1ae8edf5 | 1767 | _GLIBCXX20_CONSTEXPR |
d7e16fc5 FD |
1768 | void |
1769 | _M_insert_dispatch(iterator __pos, _InputIterator __first, | |
af5fb6ab | 1770 | _InputIterator __last, __false_type) |
d7e16fc5 FD |
1771 | { |
1772 | _M_range_insert(__pos, __first, __last, | |
1773 | std::__iterator_category(__first)); | |
af5fb6ab | 1774 | } |
ed6814f7 | 1775 | |
af5fb6ab BK |
1776 | // Called by the second insert_dispatch above |
1777 | template<typename _InputIterator> | |
1ae8edf5 | 1778 | _GLIBCXX20_CONSTEXPR |
d7e16fc5 FD |
1779 | void |
1780 | _M_range_insert(iterator __pos, _InputIterator __first, | |
6323b34e | 1781 | _InputIterator __last, std::input_iterator_tag); |
ed6814f7 | 1782 | |
af5fb6ab BK |
1783 | // Called by the second insert_dispatch above |
1784 | template<typename _ForwardIterator> | |
1ae8edf5 | 1785 | _GLIBCXX20_CONSTEXPR |
d7e16fc5 FD |
1786 | void |
1787 | _M_range_insert(iterator __pos, _ForwardIterator __first, | |
6323b34e | 1788 | _ForwardIterator __last, std::forward_iterator_tag); |
ed6814f7 | 1789 | |
af5fb6ab BK |
1790 | // Called by insert(p,n,x), and the range insert when it turns out to be |
1791 | // the same thing. | |
1ae8edf5 | 1792 | _GLIBCXX20_CONSTEXPR |
af5fb6ab BK |
1793 | void |
1794 | _M_fill_insert(iterator __pos, size_type __n, const value_type& __x); | |
ed6814f7 | 1795 | |
734f5023 | 1796 | #if __cplusplus >= 201103L |
dc2cf706 | 1797 | // Called by resize(n). |
1ae8edf5 | 1798 | _GLIBCXX20_CONSTEXPR |
dc2cf706 PC |
1799 | void |
1800 | _M_default_append(size_type __n); | |
8a752dfe | 1801 | |
1ae8edf5 | 1802 | _GLIBCXX20_CONSTEXPR |
8a752dfe FD |
1803 | bool |
1804 | _M_shrink_to_fit(); | |
dc2cf706 PC |
1805 | #endif |
1806 | ||
734f5023 | 1807 | #if __cplusplus < 201103L |
9958c7eb | 1808 | // Called by insert(p,x) |
af5fb6ab BK |
1809 | void |
1810 | _M_insert_aux(iterator __position, const value_type& __x); | |
76225d2c FD |
1811 | |
1812 | void | |
1813 | _M_realloc_insert(iterator __position, const value_type& __x); | |
6eef7402 | 1814 | #else |
9958c7eb JW |
1815 | // A value_type object constructed with _Alloc_traits::construct() |
1816 | // and destroyed with _Alloc_traits::destroy(). | |
1817 | struct _Temporary_value | |
1818 | { | |
1819 | template<typename... _Args> | |
1ae8edf5 | 1820 | _GLIBCXX20_CONSTEXPR explicit |
9958c7eb JW |
1821 | _Temporary_value(vector* __vec, _Args&&... __args) : _M_this(__vec) |
1822 | { | |
1823 | _Alloc_traits::construct(_M_this->_M_impl, _M_ptr(), | |
1824 | std::forward<_Args>(__args)...); | |
1825 | } | |
0ae207e9 | 1826 | |
1ae8edf5 | 1827 | _GLIBCXX20_CONSTEXPR |
9958c7eb JW |
1828 | ~_Temporary_value() |
1829 | { _Alloc_traits::destroy(_M_this->_M_impl, _M_ptr()); } | |
0ae207e9 | 1830 | |
1ae8edf5 JW |
1831 | _GLIBCXX20_CONSTEXPR value_type& |
1832 | _M_val() noexcept { return _M_storage._M_val; } | |
9958c7eb JW |
1833 | |
1834 | private: | |
1ae8edf5 JW |
1835 | _GLIBCXX20_CONSTEXPR _Tp* |
1836 | _M_ptr() noexcept { return std::__addressof(_M_storage._M_val); } | |
9958c7eb | 1837 | |
1ae8edf5 JW |
1838 | union _Storage |
1839 | { | |
1840 | constexpr _Storage() : _M_byte() { } | |
1841 | _GLIBCXX20_CONSTEXPR ~_Storage() { } | |
1842 | _Storage& operator=(const _Storage&) = delete; | |
1843 | unsigned char _M_byte; | |
1844 | _Tp _M_val; | |
1845 | }; | |
1846 | ||
1847 | vector* _M_this; | |
1848 | _Storage _M_storage; | |
9958c7eb JW |
1849 | }; |
1850 | ||
1851 | // Called by insert(p,x) and other functions when insertion needs to | |
1852 | // reallocate or move existing elements. _Arg is either _Tp& or _Tp. | |
1853 | template<typename _Arg> | |
1ae8edf5 | 1854 | _GLIBCXX20_CONSTEXPR |
d7e16fc5 | 1855 | void |
9958c7eb | 1856 | _M_insert_aux(iterator __position, _Arg&& __arg); |
cc2ba8e3 | 1857 | |
76225d2c | 1858 | template<typename... _Args> |
1ae8edf5 | 1859 | _GLIBCXX20_CONSTEXPR |
76225d2c FD |
1860 | void |
1861 | _M_realloc_insert(iterator __position, _Args&&... __args); | |
1862 | ||
9958c7eb | 1863 | // Either move-construct at the end, or forward to _M_insert_aux. |
1ae8edf5 | 1864 | _GLIBCXX20_CONSTEXPR |
9958c7eb JW |
1865 | iterator |
1866 | _M_insert_rval(const_iterator __position, value_type&& __v); | |
1867 | ||
9958c7eb JW |
1868 | // Try to emplace at the end, otherwise forward to _M_insert_aux. |
1869 | template<typename... _Args> | |
1ae8edf5 | 1870 | _GLIBCXX20_CONSTEXPR |
9958c7eb JW |
1871 | iterator |
1872 | _M_emplace_aux(const_iterator __position, _Args&&... __args); | |
1873 | ||
1874 | // Emplacing an rvalue of the correct type can use _M_insert_rval. | |
1ae8edf5 | 1875 | _GLIBCXX20_CONSTEXPR |
9958c7eb JW |
1876 | iterator |
1877 | _M_emplace_aux(const_iterator __position, value_type&& __v) | |
1878 | { return _M_insert_rval(__position, std::move(__v)); } | |
6eef7402 | 1879 | #endif |
bc9053ab | 1880 | |
9958c7eb | 1881 | // Called by _M_fill_insert, _M_insert_aux etc. |
1ae8edf5 | 1882 | _GLIBCXX20_CONSTEXPR |
be1088fa ML |
1883 | size_type |
1884 | _M_check_len(size_type __n, const char* __s) const | |
1885 | { | |
1886 | if (max_size() - size() < __n) | |
1887 | __throw_length_error(__N(__s)); | |
1888 | ||
af55b3af | 1889 | const size_type __len = size() + (std::max)(size(), __n); |
be1088fa ML |
1890 | return (__len < size() || __len > max_size()) ? max_size() : __len; |
1891 | } | |
1892 | ||
af55b3af | 1893 | // Called by constructors to check initial size. |
1ae8edf5 | 1894 | static _GLIBCXX20_CONSTEXPR size_type |
af55b3af JW |
1895 | _S_check_init_len(size_type __n, const allocator_type& __a) |
1896 | { | |
1897 | if (__n > _S_max_size(_Tp_alloc_type(__a))) | |
1898 | __throw_length_error( | |
1899 | __N("cannot create std::vector larger than max_size()")); | |
1900 | return __n; | |
1901 | } | |
1902 | ||
1ae8edf5 | 1903 | static _GLIBCXX20_CONSTEXPR size_type |
af55b3af JW |
1904 | _S_max_size(const _Tp_alloc_type& __a) _GLIBCXX_NOEXCEPT |
1905 | { | |
422a9f77 JW |
1906 | // std::distance(begin(), end()) cannot be greater than PTRDIFF_MAX, |
1907 | // and realistically we can't store more than PTRDIFF_MAX/sizeof(T) | |
1908 | // (even if std::allocator_traits::max_size says we can). | |
1909 | const size_t __diffmax | |
1910 | = __gnu_cxx::__numeric_traits<ptrdiff_t>::__max / sizeof(_Tp); | |
af55b3af JW |
1911 | const size_t __allocmax = _Alloc_traits::max_size(__a); |
1912 | return (std::min)(__diffmax, __allocmax); | |
1913 | } | |
1914 | ||
bc9053ab PC |
1915 | // Internal erase functions follow. |
1916 | ||
1917 | // Called by erase(q1,q2), clear(), resize(), _M_fill_assign, | |
1918 | // _M_assign_aux. | |
1ae8edf5 | 1919 | _GLIBCXX20_CONSTEXPR |
bc9053ab | 1920 | void |
757b1644 | 1921 | _M_erase_at_end(pointer __pos) _GLIBCXX_NOEXCEPT |
bc9053ab | 1922 | { |
8c7331c5 JW |
1923 | if (size_type __n = this->_M_impl._M_finish - __pos) |
1924 | { | |
1925 | std::_Destroy(__pos, this->_M_impl._M_finish, | |
1926 | _M_get_Tp_allocator()); | |
1927 | this->_M_impl._M_finish = __pos; | |
1928 | _GLIBCXX_ASAN_ANNOTATE_SHRINK(__n); | |
1929 | } | |
bc9053ab | 1930 | } |
ea2c1a6d | 1931 | |
1ae8edf5 | 1932 | _GLIBCXX20_CONSTEXPR |
94938aec PC |
1933 | iterator |
1934 | _M_erase(iterator __position); | |
1935 | ||
1ae8edf5 | 1936 | _GLIBCXX20_CONSTEXPR |
94938aec PC |
1937 | iterator |
1938 | _M_erase(iterator __first, iterator __last); | |
1939 | ||
734f5023 | 1940 | #if __cplusplus >= 201103L |
ea2c1a6d JW |
1941 | private: |
1942 | // Constant-time move assignment when source object's memory can be | |
1943 | // moved, either because the source's allocator will move too | |
1944 | // or because the allocators are equal. | |
1ae8edf5 | 1945 | _GLIBCXX20_CONSTEXPR |
ea2c1a6d | 1946 | void |
e6cad987 | 1947 | _M_move_assign(vector&& __x, true_type) noexcept |
ea2c1a6d | 1948 | { |
f0bc4aea | 1949 | vector __tmp(get_allocator()); |
ea2c1a6d | 1950 | this->_M_impl._M_swap_data(__x._M_impl); |
e98edc20 | 1951 | __tmp._M_impl._M_swap_data(__x._M_impl); |
20067423 | 1952 | std::__alloc_on_move(_M_get_Tp_allocator(), __x._M_get_Tp_allocator()); |
ea2c1a6d JW |
1953 | } |
1954 | ||
1955 | // Do move assignment when it might not be possible to move source | |
1956 | // object's memory, resulting in a linear-time operation. | |
1ae8edf5 | 1957 | _GLIBCXX20_CONSTEXPR |
ea2c1a6d | 1958 | void |
e6cad987 | 1959 | _M_move_assign(vector&& __x, false_type) |
ea2c1a6d JW |
1960 | { |
1961 | if (__x._M_get_Tp_allocator() == this->_M_get_Tp_allocator()) | |
e6cad987 | 1962 | _M_move_assign(std::move(__x), true_type()); |
ea2c1a6d JW |
1963 | else |
1964 | { | |
1965 | // The rvalue's allocator cannot be moved and is not equal, | |
1966 | // so we need to individually move each element. | |
47519a56 JW |
1967 | this->_M_assign_aux(std::make_move_iterator(__x.begin()), |
1968 | std::make_move_iterator(__x.end()), | |
1969 | std::random_access_iterator_tag()); | |
ea2c1a6d JW |
1970 | __x.clear(); |
1971 | } | |
1972 | } | |
1973 | #endif | |
8a972abd | 1974 | |
8a972abd | 1975 | template<typename _Up> |
1ae8edf5 | 1976 | _GLIBCXX20_CONSTEXPR |
8a972abd | 1977 | _Up* |
405def8d | 1978 | _M_data_ptr(_Up* __ptr) const _GLIBCXX_NOEXCEPT |
8a972abd JW |
1979 | { return __ptr; } |
1980 | ||
405def8d | 1981 | #if __cplusplus >= 201103L |
8a972abd | 1982 | template<typename _Ptr> |
1ae8edf5 | 1983 | _GLIBCXX20_CONSTEXPR |
8a972abd JW |
1984 | typename std::pointer_traits<_Ptr>::element_type* |
1985 | _M_data_ptr(_Ptr __ptr) const | |
2af96386 | 1986 | { return empty() ? nullptr : std::__to_address(__ptr); } |
8a972abd | 1987 | #else |
405def8d JW |
1988 | template<typename _Up> |
1989 | _Up* | |
1990 | _M_data_ptr(_Up* __ptr) _GLIBCXX_NOEXCEPT | |
1991 | { return __ptr; } | |
1992 | ||
1993 | template<typename _Ptr> | |
1994 | value_type* | |
1995 | _M_data_ptr(_Ptr __ptr) | |
2af96386 | 1996 | { return empty() ? (value_type*)0 : __ptr.operator->(); } |
405def8d | 1997 | |
8a972abd | 1998 | template<typename _Ptr> |
405def8d | 1999 | const value_type* |
8a972abd | 2000 | _M_data_ptr(_Ptr __ptr) const |
2af96386 | 2001 | { return empty() ? (const value_type*)0 : __ptr.operator->(); } |
8a972abd | 2002 | #endif |
af5fb6ab | 2003 | }; |
ed6814f7 | 2004 | |
225ab2b0 JW |
2005 | #if __cpp_deduction_guides >= 201606 |
2006 | template<typename _InputIterator, typename _ValT | |
2007 | = typename iterator_traits<_InputIterator>::value_type, | |
2008 | typename _Allocator = allocator<_ValT>, | |
2009 | typename = _RequireInputIter<_InputIterator>, | |
2010 | typename = _RequireAllocator<_Allocator>> | |
2011 | vector(_InputIterator, _InputIterator, _Allocator = _Allocator()) | |
2012 | -> vector<_ValT, _Allocator>; | |
2013 | #endif | |
ed6814f7 | 2014 | |
3971a4d2 PE |
2015 | /** |
2016 | * @brief Vector equality comparison. | |
93c66bc6 BK |
2017 | * @param __x A %vector. |
2018 | * @param __y A %vector of the same type as @a __x. | |
3971a4d2 PE |
2019 | * @return True iff the size and elements of the vectors are equal. |
2020 | * | |
2021 | * This is an equivalence relation. It is linear in the size of the | |
2022 | * vectors. Vectors are considered equivalent if their sizes are equal, | |
2023 | * and if corresponding elements compare equal. | |
2024 | */ | |
af5fb6ab | 2025 | template<typename _Tp, typename _Alloc> |
1ae8edf5 | 2026 | _GLIBCXX20_CONSTEXPR |
3971a4d2 | 2027 | inline bool |
874e7baa PC |
2028 | operator==(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y) |
2029 | { return (__x.size() == __y.size() | |
2030 | && std::equal(__x.begin(), __x.end(), __y.begin())); } | |
ed6814f7 | 2031 | |
bd2420f8 JW |
2032 | #if __cpp_lib_three_way_comparison |
2033 | /** | |
2034 | * @brief Vector ordering relation. | |
2035 | * @param __x A `vector`. | |
2036 | * @param __y A `vector` of the same type as `__x`. | |
2037 | * @return A value indicating whether `__x` is less than, equal to, | |
2038 | * greater than, or incomparable with `__y`. | |
2039 | * | |
2040 | * See `std::lexicographical_compare_three_way()` for how the determination | |
2041 | * is made. This operator is used to synthesize relational operators like | |
2042 | * `<` and `>=` etc. | |
2043 | */ | |
2044 | template<typename _Tp, typename _Alloc> | |
1ae8edf5 | 2045 | _GLIBCXX20_CONSTEXPR |
bd2420f8 JW |
2046 | inline __detail::__synth3way_t<_Tp> |
2047 | operator<=>(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y) | |
2048 | { | |
2049 | return std::lexicographical_compare_three_way(__x.begin(), __x.end(), | |
2050 | __y.begin(), __y.end(), | |
2051 | __detail::__synth3way); | |
2052 | } | |
2053 | #else | |
3971a4d2 PE |
2054 | /** |
2055 | * @brief Vector ordering relation. | |
93c66bc6 BK |
2056 | * @param __x A %vector. |
2057 | * @param __y A %vector of the same type as @a __x. | |
2058 | * @return True iff @a __x is lexicographically less than @a __y. | |
3971a4d2 PE |
2059 | * |
2060 | * This is a total ordering relation. It is linear in the size of the | |
2061 | * vectors. The elements must be comparable with @c <. | |
2062 | * | |
9536ca34 | 2063 | * See std::lexicographical_compare() for how the determination is made. |
3971a4d2 | 2064 | */ |
af5fb6ab | 2065 | template<typename _Tp, typename _Alloc> |
3971a4d2 | 2066 | inline bool |
874e7baa PC |
2067 | operator<(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y) |
2068 | { return std::lexicographical_compare(__x.begin(), __x.end(), | |
2069 | __y.begin(), __y.end()); } | |
ed6814f7 | 2070 | |
3971a4d2 | 2071 | /// Based on operator== |
af5fb6ab | 2072 | template<typename _Tp, typename _Alloc> |
3971a4d2 | 2073 | inline bool |
874e7baa | 2074 | operator!=(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y) |
3971a4d2 | 2075 | { return !(__x == __y); } |
ed6814f7 | 2076 | |
3971a4d2 | 2077 | /// Based on operator< |
af5fb6ab | 2078 | template<typename _Tp, typename _Alloc> |
3971a4d2 | 2079 | inline bool |
874e7baa | 2080 | operator>(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y) |
3971a4d2 | 2081 | { return __y < __x; } |
ed6814f7 | 2082 | |
3971a4d2 | 2083 | /// Based on operator< |
af5fb6ab | 2084 | template<typename _Tp, typename _Alloc> |
3971a4d2 | 2085 | inline bool |
874e7baa | 2086 | operator<=(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y) |
3971a4d2 | 2087 | { return !(__y < __x); } |
ed6814f7 | 2088 | |
3971a4d2 | 2089 | /// Based on operator< |
af5fb6ab | 2090 | template<typename _Tp, typename _Alloc> |
3971a4d2 | 2091 | inline bool |
874e7baa | 2092 | operator>=(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y) |
3971a4d2 | 2093 | { return !(__x < __y); } |
bd2420f8 | 2094 | #endif // three-way comparison |
ed6814f7 | 2095 | |
3971a4d2 | 2096 | /// See std::vector::swap(). |
af5fb6ab | 2097 | template<typename _Tp, typename _Alloc> |
1ae8edf5 | 2098 | _GLIBCXX20_CONSTEXPR |
3971a4d2 | 2099 | inline void |
874e7baa | 2100 | swap(vector<_Tp, _Alloc>& __x, vector<_Tp, _Alloc>& __y) |
c5d9ec56 | 2101 | _GLIBCXX_NOEXCEPT_IF(noexcept(__x.swap(__y))) |
3971a4d2 | 2102 | { __x.swap(__y); } |
3cbc7af0 | 2103 | |
12ffa228 | 2104 | _GLIBCXX_END_NAMESPACE_CONTAINER |
10f26de9 JW |
2105 | |
2106 | #if __cplusplus >= 201703L | |
2107 | namespace __detail::__variant | |
2108 | { | |
2109 | template<typename> struct _Never_valueless_alt; // see <variant> | |
2110 | ||
2111 | // Provide the strong exception-safety guarantee when emplacing a | |
2112 | // vector into a variant, but only if move assignment cannot throw. | |
2113 | template<typename _Tp, typename _Alloc> | |
2114 | struct _Never_valueless_alt<_GLIBCXX_STD_C::vector<_Tp, _Alloc>> | |
2115 | : std::is_nothrow_move_assignable<_GLIBCXX_STD_C::vector<_Tp, _Alloc>> | |
2116 | { }; | |
2117 | } // namespace __detail::__variant | |
2118 | #endif // C++17 | |
2119 | ||
4a15d842 | 2120 | _GLIBCXX_END_NAMESPACE_VERSION |
12ffa228 | 2121 | } // namespace std |
725dc051 | 2122 | |
046d30f4 | 2123 | #endif /* _STL_VECTOR_H */ |