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