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