libstdc++
inplace_vector
Go to the documentation of this file.
1// Sequence container with fixed capacity -*- C++ -*-
2
3// Copyright The GNU Toolchain Authors.
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
8// Free Software Foundation; either version 3, or (at your option)
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
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.
19
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/>.
24
25/** @file include/inplace_vector
26 * This is a Standard C++ Library header.
27 * @ingroup sequences
28 */
29
30#ifndef _GLIBCXX_INPLACE_VECTOR
31#define _GLIBCXX_INPLACE_VECTOR 1
32
33#pragma GCC system_header
34
35#define __glibcxx_want_constexpr_inplace_vector
36#define __glibcxx_want_hardened_inplace_vector
37#define __glibcxx_want_inplace_vector
38#include <bits/version.h>
39
40#ifdef __glibcxx_inplace_vector // C++ >= 26
41#include <compare>
42#include <initializer_list>
43#include <optional>
45#include <bits/range_access.h>
46#include <bits/ranges_base.h> // borrowed_iterator_t, __detail::__container_compatible_range, __static_sized_range
47#include <bits/ranges_util.h> // subrange
49#include <bits/stl_construct.h>
51#include <bits/stl_algo.h> // rotate
52#include <bits/erase_if.h>
53
54namespace std _GLIBCXX_VISIBILITY(default)
55{
56_GLIBCXX_BEGIN_NAMESPACE_VERSION
57_GLIBCXX_BEGIN_NAMESPACE_CONTAINER
58
59 // [indirect], class template indirect
60 template<typename _Tp, size_t _Nm>
61 class inplace_vector
62 {
63 public:
64
65 // types:
66 using value_type = _Tp;
67 using pointer = _Tp*;
68 using const_pointer = const _Tp*;
69 using reference = value_type&;
70 using const_reference = const value_type&;
71 using size_type = size_t;
72 using difference_type = ptrdiff_t;
73 using iterator
74 = __gnu_cxx::__normal_iterator<_Tp*, inplace_vector>;
75 using const_iterator
76 = __gnu_cxx::__normal_iterator<const _Tp*, inplace_vector>;
77 using reverse_iterator = std::reverse_iterator<iterator>;
78 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
79
80 // [containers.sequences.inplace.vector.cons], construct/copy/destroy
81 constexpr
82 inplace_vector() noexcept
83 { _M_init(); }
84
85 constexpr explicit
86 inplace_vector(size_type __n)
87 {
88 _M_init();
89 _S_check_size(__n);
91 _M_size = __n;
92 }
93
94 constexpr
95 inplace_vector(size_type __n, const _Tp& __value)
96 {
97 _M_init();
98 _S_check_size(__n);
99 std::uninitialized_fill_n(data(), __n, __value);
100 _M_size = __n;
101 }
102
103 template<__any_input_iterator _InputIterator>
104 constexpr
105 inplace_vector(_InputIterator __first, _InputIterator __last)
106 : inplace_vector()
107 {
108 if constexpr (__any_sizeable_iterator<_InputIterator>)
109 {
110 const size_type __n = _S_distance_if_fits(0, __first, __last);
111 std::uninitialized_copy(__first, __last, data());
112 _M_size = __n;
113 }
114 else
115 {
116 while (__first != __last)
117 emplace_back(*__first++);
118 }
119 }
120
121 template <__detail::__container_compatible_range<_Tp> _Rg>
122 constexpr
123 inplace_vector(from_range_t, _Rg&& __rg)
124 : inplace_vector()
125 {
126 // _GLIBCXX_RESOLVE_LIB_DEFECTS
127 // 4396. Improve inplace_vector(from_range_t, R&& rg)
128 if constexpr (ranges::__static_sized_range<_Rg>)
129 static_assert(ranges::size(__rg) <= _Nm);
130
131 append_range(__rg);
132 }
133
134 constexpr
135 inplace_vector(initializer_list<_Tp> __il)
136 {
137 _M_init();
138 _S_check_size(__il.size());
139 std::uninitialized_copy(__il.begin(), __il.end(), data());
140 _M_size = __il.size();
141 }
142
143 inplace_vector(const inplace_vector&)
144 requires is_trivially_copy_constructible_v<_Tp>
145 = default;
146
147 constexpr
148 inplace_vector(const inplace_vector& __other)
149 noexcept(is_nothrow_copy_constructible_v<_Tp>)
150 {
151 _M_init();
152 std::uninitialized_copy(__other.begin(), __other.end(), data());
153 _M_size = __other.size();
154 }
155
156 inplace_vector(inplace_vector&&)
157 requires is_trivially_move_constructible_v<_Tp>
158 = default;
159
160 constexpr
161 inplace_vector(inplace_vector&& __other)
162 noexcept(is_nothrow_move_constructible_v<_Tp>)
163 {
164 _M_init();
165 std::uninitialized_move(__other.begin(), __other.end(), data());
166 _M_size = __other.size();
167 }
168
169 ~inplace_vector()
170 requires is_trivially_destructible_v<_Tp>
171 = default;
172
173 constexpr
174 ~inplace_vector()
175 { clear(); }
176
177 inplace_vector&
178 operator=(const inplace_vector&)
179 requires is_trivially_copy_assignable_v<_Tp>
180 && is_trivially_copy_constructible_v<_Tp>
181 && is_trivially_destructible_v<_Tp>
182 = default;
183
184 constexpr inplace_vector&
185 operator=(const inplace_vector& __other)
186 noexcept(is_nothrow_copy_assignable_v<_Tp>
187 && is_nothrow_copy_constructible_v<_Tp>)
188 {
189 if (std::addressof(__other) != this) [[likely]]
190 assign(__other.begin(), __other.end());
191 return *this;
192 }
193
194 inplace_vector&
195 operator=(inplace_vector&&)
196 requires is_trivially_move_assignable_v<_Tp>
197 && is_trivially_move_constructible_v<_Tp>
198 && is_trivially_destructible_v<_Tp>
199 = default;
200
201 constexpr inplace_vector&
202 operator=(inplace_vector&& __other)
203 noexcept(is_nothrow_move_assignable_v<_Tp>
204 && is_nothrow_move_constructible_v<_Tp>)
205 {
206 if (std::addressof(__other) != this) [[likely]]
207 assign(std::make_move_iterator(__other.begin()),
208 std::make_move_iterator(__other.end()));
209 return *this;
210 }
211
212 constexpr inplace_vector&
213 operator=(initializer_list<_Tp> __il)
214 {
215 assign(__il.begin(), __il.end());
216 return *this;
217 }
218
219 template<__any_input_iterator _InputIterator>
220 constexpr void
221 assign(_InputIterator __first, _InputIterator __last)
222 {
223 if constexpr (__any_sizeable_iterator<_InputIterator>)
224 {
225 const size_type __n = _S_distance_if_fits(0, __first, __last);
226 if (_M_size <= __n)
227 {
228 for (size_t __i = 0; __i < _M_size; ++__i, (void)++__first)
229 _M_elems[__i] = *__first;
230 std::uninitialized_copy(__first, __last, end());
231 }
232 else
233 std::destroy(std::copy(__first, __last, begin()), end());
234 _M_size = __n;
235 }
236 else
237 {
238 size_t __i = 0;
239 for (;__first != __last && __i < _M_size; ++__first)
240 _M_elems[__i++] = *__first;
241 if (__first == __last)
242 {
243 std::_Destroy_n(data() + __i, _M_size - __i);
244 _M_size = __i;
245 }
246 else
247 {
248 while (__first != __last)
249 emplace_back(*__first++);
250 }
251 }
252 }
253
254 template<__detail::__container_compatible_range<_Tp> _Rg>
255 constexpr void
256 assign_range(_Rg&& __rg)
257 {
258 if constexpr (ranges::forward_range<_Rg> || ranges::sized_range<_Rg>)
259 {
260 const size_type __sz = _S_distance_if_fits(0, __rg);
261 if (__sz <= size())
262 {
263 ranges::copy_n(ranges::begin(__rg), __sz, data());
264 std::destroy(data() + __sz, data() + _M_size);
265 }
266 else
267 {
268 auto [__in, __out] = ranges::copy_n(
269 ranges::begin(__rg), _M_size,
270 data());
271 ranges::uninitialized_copy(
272 std::move(__in), ranges::end(__rg),
273 __out, unreachable_sentinel);
274 }
275 _M_size = __sz;
276 }
277 else
278 {
279 auto __in = ranges::begin(__rg);
280 auto __end = ranges::end(__rg);
281 size_type __n = 0;
282 for (; __n < _M_size && __in != __end; ++__in)
283 _M_elems[__n++] = *__in;
284
285 if (__in == __end)
286 {
287 std::destroy(data() + __n, data() + _M_size);
288 _M_size = __n;
289 return;
290 }
291 else if (__n < _Nm)
292 {
293 auto __res = ranges::uninitialized_copy(
294 std::move(__in), __end,
295 data() + __n, data() + _Nm);
296 _M_size = __res.out - data();
297 if (__res.in == ranges::end(__rg))
298 return;
299 }
300 __throw_bad_alloc();
301 }
302 }
303
304 constexpr void
305 assign(size_type __n, const _Tp& __u)
306 {
307 _S_check_size(__n);
308 if (_M_size <= __n)
309 std::uninitialized_fill_n(std::fill_n(data(), _M_size, __u),
310 __n - _M_size, __u);
311 else
312 std::destroy_n(std::fill_n(data(), __n, __u), _M_size - __n);
313 _M_size = __n;
314 }
315
316 constexpr void
317 assign(initializer_list<_Tp> __il)
318 { assign(__il.begin(), __il.end()); }
319
320 // iterators
321 [[nodiscard]]
322 constexpr iterator
323 begin() noexcept { return iterator(data()); }
324
325 [[nodiscard]]
326 constexpr const_iterator
327 begin() const noexcept { return const_iterator(data()); }
328
329 [[nodiscard]]
330 constexpr iterator
331 end() noexcept
332 { return iterator(data() + _M_size); }
333
334 [[nodiscard]]
335 constexpr const_iterator
336 end() const noexcept
337 { return const_iterator(data() + _M_size); }
338
339 [[nodiscard]]
340 constexpr reverse_iterator
341 rbegin() noexcept
342 { return reverse_iterator(end()); }
343
344 [[nodiscard]]
345 constexpr const_reverse_iterator
346 rbegin() const noexcept
347 { return const_reverse_iterator(end()); }
348
349 [[nodiscard]]
350 constexpr reverse_iterator
351 rend() noexcept { return reverse_iterator(begin()); }
352
353 [[nodiscard]]
354 constexpr const_reverse_iterator
355 rend() const noexcept { return const_reverse_iterator(begin()); }
356
357 [[nodiscard]]
358 constexpr const_iterator
359 cbegin() const noexcept { return begin(); }
360
361 [[nodiscard]]
362 constexpr const_iterator
363 cend() const noexcept { return end(); }
364
365 [[nodiscard]]
366 constexpr const_reverse_iterator
367 crbegin() const noexcept { return rbegin(); }
368
369 [[nodiscard]]
370 constexpr const_reverse_iterator
371 crend() const noexcept { return rend(); }
372
373 // [containers.sequences.inplace.vector.members] size/capacity
374 [[nodiscard]]
375 constexpr bool
376 empty() const noexcept { return _M_size == 0; }
377
378 [[nodiscard]]
379 constexpr size_type
380 size() const noexcept
381 {
382 if (_M_size > _Nm)
383 __builtin_unreachable();
384 return _M_size;
385 }
386
387 [[nodiscard]]
388 static constexpr size_type
389 max_size() noexcept { return _Nm; }
390
391 [[nodiscard]]
392 static constexpr size_type
393 capacity() noexcept { return _Nm; }
394
395 constexpr void
396 resize(size_type __n)
397 {
398 _S_check_size(__n);
399 if (__n > _M_size)
400 std::uninitialized_value_construct_n(data() + _M_size, __n - _M_size);
401 else if (__n < _M_size)
402 std::destroy_n(data() + __n, _M_size - __n);
403 _M_size = __n;
404 }
405
406 constexpr void
407 resize(size_type __n, const _Tp& __c)
408 {
409 _S_check_size(__n);
410 if (__n > _M_size)
411 std::uninitialized_fill_n(data() + _M_size, __n - _M_size, __c);
412 else if (__n < _M_size)
413 std::destroy_n(data() + __n, _M_size - __n);
414 _M_size = __n;
415 }
416
417 static constexpr void
418 reserve(size_type __n)
419 { _S_check_size(__n); }
420
421 static constexpr void
422 shrink_to_fit() { }
423
424 // element access
425 [[nodiscard]]
426 constexpr reference
427 operator[](size_type __n)
428 {
429 __glibcxx_requires_subscript(__n);
430 return _M_elems[__n];
431 }
432
433 [[nodiscard]]
434 constexpr const_reference
435 operator[](size_type __n) const
436 {
437 __glibcxx_requires_subscript(__n);
438 return _M_elems[__n];
439 }
440
441 [[nodiscard]]
442 constexpr const_reference
443 at(size_type __n) const
444 {
445 if (__n >= _M_size)
446 std::__throw_out_of_range_fmt(__N("inplace_vector::at: __n "
447 "(which is %zu) "
448 ">= size() (which is %zu)"),
449 __n, _M_size);
450 return _M_elems[__n];
451 }
452
453 [[nodiscard]]
454 constexpr reference
455 at(size_type __n)
456 {
457 if (__n >= _M_size)
458 std::__throw_out_of_range_fmt(__N("inplace_vector::at: __n "
459 "(which is %zu) "
460 ">= size() (which is %zu)"),
461 __n, _M_size);
462 return _M_elems[__n];
463 }
464
465 [[nodiscard]]
466 constexpr reference
467 front()
468 {
469 __glibcxx_requires_nonempty();
470 return _M_elems[0];
471 }
472
473 [[nodiscard]]
474 constexpr const_reference
475 front() const
476 {
477 __glibcxx_requires_nonempty();
478 return _M_elems[0];
479 }
480
481 [[nodiscard]]
482 constexpr reference
483 back()
484 {
485 __glibcxx_requires_nonempty();
486 return _M_elems[_M_size - 1];
487 }
488
489 [[nodiscard]]
490 constexpr const_reference
491 back() const
492 {
493 __glibcxx_requires_nonempty();
494 return _M_elems[_M_size - 1];
495 }
496
497 // [containers.sequences.inplace.vector.data], data access
498
499 [[nodiscard]]
500 constexpr _Tp*
501 data() noexcept
502 { return static_cast<pointer>(_M_elems); }
503
504 [[nodiscard]]
505 constexpr const _Tp*
506 data() const noexcept
507 { return static_cast<const_pointer>(_M_elems); }
508
509 // [containers.sequences.inplace.vector.modifiers], modifiers
510 template<typename... _Args>
511 constexpr _Tp&
512 emplace_back(_Args&&... __args)
513 {
514 if (_M_size >= _Nm)
515 __throw_bad_alloc();
516 return unchecked_emplace_back(std::forward<_Args>(__args)...);
517 }
518
519 constexpr _Tp&
520 push_back(const _Tp& __x)
521 { return emplace_back(__x); }
522
523 constexpr _Tp&
524 push_back(_Tp&& __x)
525 { return emplace_back(std::move(__x)); }
526
527 template<__detail::__container_compatible_range<_Tp> _Rg>
528 constexpr void
529 append_range(_Rg&& __rg)
530 {
531 if constexpr (ranges::forward_range<_Rg> || ranges::sized_range<_Rg>)
532 {
533 const size_t __sz = _S_distance_if_fits(_M_size, __rg);
534 // Bounded on output range due PR121143
535 ranges::uninitialized_copy(
536 ranges::begin(__rg), unreachable_sentinel,
537 data() + _M_size, data() + _M_size + __sz);
538 _M_size += __sz;
539 }
540 else
541 {
542 ranges::subrange<pointer> __tail(data() + _M_size, data() + _Nm);
543 auto [__in, __out] = ranges::uninitialized_copy(__rg, __tail);
544 _M_size = __out - data();
545 if (__in != ranges::end(__rg))
546 __throw_bad_alloc();
547 }
548 }
549
550 constexpr void
551 pop_back()
552 {
553 __glibcxx_requires_nonempty();
554 --_M_size;
555 _M_elems[_M_size].~_Tp();
556 }
557
558 template<typename... _Args>
559 constexpr optional<_Tp&>
560 try_emplace_back(_Args&&... __args)
561 {
562 if (_M_size >= _Nm) [[unlikely]]
563 return nullopt;
564 return optional<_Tp&>(in_place,
565 unchecked_emplace_back(std::forward<_Args>(__args)...));
566 }
567
568 constexpr optional<_Tp&>
569 try_push_back(const _Tp& __x)
570 {
571 if (_M_size >= _Nm) [[unlikely]]
572 return nullopt;
573 return optional<_Tp&>(in_place, unchecked_emplace_back(__x));
574 }
575
576 constexpr optional<_Tp&>
577 try_push_back(_Tp&& __x)
578 {
579 if (_M_size >= _Nm) [[unlikely]]
580 return nullopt;
581 return optional<_Tp&>(in_place, unchecked_emplace_back(std::move(__x)));
582 }
583
584 template<typename... _Args>
585 constexpr _Tp&
586 unchecked_emplace_back(_Args&&... __args)
587 {
588 __glibcxx_assert(_M_size < _Nm);
589 auto __p = std::construct_at(data() + _M_size,
590 std::forward<_Args>(__args)...);
591 ++_M_size;
592 return *__p;
593 }
594
595 constexpr _Tp&
596 unchecked_push_back(const _Tp& __x)
597 { return unchecked_emplace_back(__x); }
598
599 constexpr _Tp&
600 unchecked_push_back(_Tp&& __x)
601 { return unchecked_emplace_back(std::move(__x)); }
602
603 template<typename... _Args>
604 constexpr iterator
605 emplace(const_iterator __position, _Args&&... __args)
606 {
607 size_t __b = __position - cbegin(); // elements before position
608 __glibcxx_assert(__b <= _M_size);
609 if (_M_size >= _Nm)
610 __throw_bad_alloc();
611 iterator __pos = begin() + __b;
612 std::construct_at(data() + _M_size, std::forward<_Args>(__args)...);
613 if (_M_size++)
614 std::rotate(__pos, end() - 1, end());
615 return __pos;
616 }
617
618 constexpr iterator
619 insert(const_iterator __position, const _Tp& __x)
620 { return emplace(__position, __x); }
621
622 constexpr iterator
623 insert(const_iterator __position, _Tp&& __x)
624 { return emplace(__position, std::move(__x)); }
625
626 constexpr iterator
627 insert(const_iterator __position, size_type __n, const _Tp& __x)
628 {
629 size_t __b = __position - cbegin(); // elements before position
630 __glibcxx_assert(__b <= _M_size);
631 if ((_Nm - _M_size) < __n)
632 __throw_bad_alloc();
633 iterator __pos = begin() + __b;
634 std::uninitialized_fill_n(data() + _M_size, __n, __x);
635 if (std::__exchange(_M_size, _M_size + __n))
636 std::rotate(__pos, end() - __n, end());
637 return __pos;
638 }
639
640 template<__any_input_iterator _InputIterator>
641 constexpr iterator
642 insert(const_iterator __position, _InputIterator __first,
643 _InputIterator __last)
644 {
645 size_t __b = __position - cbegin(); // elements before position
646 __glibcxx_assert(__b <= _M_size);
647 iterator __pos = begin() + __b;
648 const size_t __s = _M_size;
649 if constexpr (__any_sizeable_iterator<_InputIterator>)
650 {
651 const size_type __n = _S_distance_if_fits(_M_size, __first, __last);
652 std::uninitialized_copy(__first, __last, data() + _M_size);
653 _M_size += __n;
654 }
655 else
656 {
657 while (__first != __last)
658 emplace_back(*__first++);
659 }
660 if (__b < __s)
661 std::rotate(__pos, begin() + __s, end());
662 return __pos;
663 }
664
665 template<__detail::__container_compatible_range<_Tp> _Rg>
666 constexpr iterator
667 insert_range(const_iterator __position, _Rg&& __rg)
668 {
669 iterator __pos = begin() + (__position - cbegin());
670 const auto __end = end();
671 if constexpr (ranges::forward_range<_Rg> || ranges::sized_range<_Rg>)
672 {
673 const size_type __sz = _S_distance_if_fits(_M_size, __rg);
674 if (!__sz) [[unlikely]]
675 return __pos;
676
677 const size_type __num_after = __end - __pos;
678 if (__num_after >= __sz)
679 {
680 ranges::uninitialized_move(__end - __sz, __end,
681 __end, unreachable_sentinel);
682 _M_size += __sz;
683 ranges::move_backward(__pos, __end - __sz, __end);
684 ranges::copy(__rg, __pos);
685 }
686 else if constexpr (ranges::forward_range<_Rg>)
687 {
688 auto __mid = ranges::next(ranges::begin(__rg), __num_after);
689 ranges::uninitialized_copy(__mid, ranges::end(__rg),
690 __end, unreachable_sentinel);
691 _M_size += __sz - __num_after;
692 ranges::uninitialized_move(__pos, __end,
693 __pos + __sz, unreachable_sentinel);
694 _M_size += __num_after;
695 ranges::copy(ranges::begin(__rg), __mid, __pos);
696 }
697 else
698 {
699 ranges::uninitialized_copy(
700 ranges::begin(__rg), ranges::end(__rg),
701 __end, unreachable_sentinel);
702 _M_size += __sz;
703 std::rotate(__pos, __end, end());
704 }
705 }
706 else
707 {
708 append_range(__rg);
709 std::rotate(__pos, __end, end());
710 }
711 return __pos;
712 }
713
714 constexpr iterator
715 insert(const_iterator __position, initializer_list<_Tp> __il)
716 { return insert(__position, __il.begin(), __il.end()); }
717
718 constexpr iterator
719 erase(const_iterator __position)
720 {
721 size_t __n = __position - cbegin();
722 __glibcxx_assert(__n < _M_size);
723 iterator __pos = begin() + __n;
724 std::move(__pos + 1, end(), __pos);
725 pop_back();
726 return __pos;
727 }
728
729 constexpr iterator
730 erase(const_iterator __first, const_iterator __last)
731 {
732 size_t __n = __first - cbegin();
733 size_t __x = __last - __first;
734 __glibcxx_assert(__n <= _M_size);
735 __glibcxx_assert(__x <= _M_size);
736 iterator __pos = begin() + __n;
737 iterator __end = std::move(__pos + __x, end(), __pos);
738 std::destroy_n(__end, __x);
739 _M_size -= __x;
740 return __pos;
741 }
742
743 constexpr void
744 swap(inplace_vector& __x)
745 noexcept(is_nothrow_swappable_v<_Tp> && is_nothrow_move_constructible_v<_Tp>)
746 {
747 inplace_vector* __vs[2]{ this, std::addressof(__x) };
748 const auto __smaller = __vs[__x.size() < size()];
749 const auto __bigger = __vs[__x.size() >= size()];
750 size_type __n = __smaller->size();
751 size_type __n2 = __bigger->size();
752
753 if constexpr (is_nothrow_move_constructible_v<_Tp>)
754 {
755 for (size_type __i = __n; __i < __n2; ++__i)
756 {
757 std::construct_at(__smaller->data() + __i,
758 std::move(*(__bigger->data() + __i)));
759 std::destroy_at(__bigger->data() + __i);
760 }
761 }
762 else
763 {
764 std::uninitialized_copy(__bigger->data() + __n,
765 __bigger->data() + __n2,
766 __smaller->data() + __n);
767 std::destroy(__bigger->data() + __n, __bigger->data() + __n2);
768 }
769 __smaller->_M_size = __n2;
770 __bigger->_M_size = __n;
771
772 using std::swap;
773 for (size_type __i = 0; __i < __n; __i++)
774 swap(_M_elems[__i], __x._M_elems[__i]);
775 }
776
777 constexpr void
778 clear() noexcept
779 {
780 std::destroy_n(data(), size_t(_M_size));
781 _M_size = 0;
782 }
783
784 constexpr friend bool
785 operator==(const inplace_vector& __x, const inplace_vector& __y)
786 { return std::equal(__x.begin(), __x.end(), __y.begin(), __y.end()); }
787
788 constexpr friend auto
789 operator<=>(const inplace_vector& __x, const inplace_vector& __y)
790 requires requires (const _Tp __t) {
791 { __t < __t } -> __detail::__boolean_testable;
792 }
793 {
794 return std::lexicographical_compare_three_way(__x.begin(), __x.end(),
795 __y.begin(), __y.end(),
796 __detail::__synth3way);
797 }
798
799 // [inplace.vector.special], specialized algorithms
800 constexpr friend void
801 swap(inplace_vector& __x, inplace_vector& __y)
802 noexcept(is_nothrow_swappable_v<_Tp> && is_nothrow_move_constructible_v<_Tp>)
803 { __x.swap(__y); }
804
805 private:
806 union {
807 _Tp _M_elems[_Nm];
808 };
809
810 // Check whether integer type _UInt is wide enough to store _Nm,
811 // so that we use a smaller type for _M_size when that saves space.
812 template<typename _UInt, bool = (alignof(_Tp) <= sizeof(_UInt))>
813 static constexpr bool __fits
814 = _Nm <= __gnu_cxx::__int_traits<_UInt>::__max;
815
816 // Don't bother using a smaller type if alignment of the array elements
817 // means that it doesn't actually save space.
818 template<typename _UInt>
819 static constexpr bool __fits<_UInt, false> = false;
820
821 static consteval auto __select_size_type()
822 {
823 if constexpr (__fits<unsigned char>)
824 return (unsigned char)0;
825#if __SHRT_WIDTH__ < __SIZE_WIDTH__
826 else if constexpr (__fits<unsigned short>)
827 return (unsigned short)0;
828#endif
829#if __INT_WIDTH__ < __SIZE_WIDTH__ && __INT_WIDTH__ > __SHRT_WIDTH__
830 else if constexpr (__fits<unsigned int>)
831 return 0u;
832#endif
833#if __LONG_WIDTH__ < __SIZE_WIDTH__ && __LONG_WIDTH__ > __INT_WIDTH__
834 else if constexpr (__fits<unsigned long>)
835 return 0ul;
836#endif
837 else // Just use size_t.
838 return 0uz;
839 }
840 decltype(__select_size_type()) _M_size = 0;
841
842 constexpr void
843 _M_init()
844 {
845#if __glibcxx_start_lifetime
846 std::start_lifetime(_M_elems);
847#else
848 if consteval
849 {
850 if constexpr (is_trivially_default_constructible_v<_Tp>
851 && is_trivially_copyable_v<_Tp>)
852 for (size_t __i = 0; __i < _Nm; ++__i)
853 _M_elems[__i] = _Tp();
854 else
855# if __has_builtin(__builtin_constexpr_diag)
856 __builtin_constexpr_diag(2, "",
857 "std::inplace_vector supports only trivally copyable and "
858 "trivially default constructible types at compile time");
859# else
860 __builtin_unreachable();
861# endif
862 }
863#endif
864 }
865
866 template<typename _Integer>
867 static constexpr size_type
868 _S_check_size(_Integer __n, size_type __size = 0)
869 {
870 if constexpr (__detail::__is_signed_integer_like<_Integer>)
871 if (__n < 0)
872 __throw_bad_alloc();
873 if (__n > (_Nm - __size))
874 __throw_bad_alloc();
875 return static_cast<size_type>(__n);
876 }
877
878 template<typename _Iterator, typename _Sentinel>
879 static constexpr size_type
880 _S_check_distance(_Iterator __first, _Sentinel __last, size_type __size)
881 {
882 for (size_type __i = __size; __i < _Nm; ++__i)
883 if (__first == __last)
884 return __i - __size;
885 else
886 ++__first;
887
888 if (__first == __last)
889 return _Nm - __size;
890
891 __throw_bad_alloc();
892 }
893
894 // Returns the size of the range if it will fit in the remaining
895 // capacity (_Nm - used), otherwise throws bad_alloc.
896 template<typename _InputIterator>
897 constexpr static size_t
898 _S_distance_if_fits(size_t __size, _InputIterator __first, _InputIterator __last)
899 {
900 if constexpr (sized_sentinel_for<_InputIterator, _InputIterator>)
901 return _S_check_size(__last - __first, __size);
902 else
903 return _S_check_distance(__first, __last, __size);
904 }
905
906 // See above.
907 template<typename _Rg>
908 constexpr static size_t
909 _S_distance_if_fits(size_t __size, _Rg& __rg)
910 {
911 if constexpr (ranges::sized_range<_Rg>)
912 return _S_check_size(ranges::size(__rg), __size);
913 else
914 return _S_check_distance(ranges::begin(__rg), ranges::end(__rg),
915 __size);
916 }
917 };
918
919 // specialization for zero capacity, that is required to be trivally copyable
920 // and empty regardless of _Tp.
921 template<typename _Tp>
922 class inplace_vector<_Tp, 0>
923 {
924 public:
925 // types:
926 using value_type = _Tp;
927 using pointer = _Tp*;
928 using const_pointer = const _Tp*;
929 using reference = value_type&;
930 using const_reference = const value_type&;
931 using size_type = size_t;
932 using difference_type = ptrdiff_t;
933 using iterator
934 = __gnu_cxx::__normal_iterator<_Tp*, inplace_vector>;
935 using const_iterator
936 = __gnu_cxx::__normal_iterator<const _Tp*, inplace_vector>;
937 using reverse_iterator = std::reverse_iterator<iterator>;
938 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
939
940 // [containers.sequences.inplace.vector.cons], construct/copy/destroy
941 inplace_vector() = default;
942
943 constexpr explicit
944 inplace_vector(size_type __n)
945 {
946 if (__n != 0)
947 __throw_bad_alloc();
948 }
949
950 constexpr
951 inplace_vector(size_type __n, const _Tp& __value)
952 {
953 if (__n != 0)
954 __throw_bad_alloc();
955 }
956
957 template<__any_input_iterator _InputIterator>
958 constexpr
959 inplace_vector(_InputIterator __first, _InputIterator __last)
960 {
961 if (__first != __last)
962 __throw_bad_alloc();
963 }
964
965 template <__detail::__container_compatible_range<_Tp> _Rg>
966 constexpr
967 inplace_vector(from_range_t, _Rg&& __rg)
968 {
969 // _GLIBCXX_RESOLVE_LIB_DEFECTS
970 // 4396. Improve inplace_vector(from_range_t, R&& rg)
971 if constexpr (ranges::__static_sized_range<_Rg>)
972 static_assert(ranges::size(__rg) == 0);
973
974 if (ranges::begin(__rg) != ranges::end(__rg))
975 __throw_bad_alloc();
976 }
977
978 constexpr
979 inplace_vector(initializer_list<_Tp> __il)
980 {
981 if (__il.size() != 0)
982 __throw_bad_alloc();
983 }
984
985 inplace_vector(const inplace_vector&) = default;
986 inplace_vector(inplace_vector&&) = default;
987
988 constexpr
989 ~inplace_vector() = default;
990
991 inplace_vector&
992 operator=(const inplace_vector&) = default;
993
994 inplace_vector&
995 operator=(inplace_vector&&) = default;
996
997 constexpr inplace_vector&
998 operator=(initializer_list<_Tp> __il)
999 {
1000 if (__il.size() != 0)
1001 __throw_bad_alloc();
1002 return *this;
1003 }
1004
1005 template<__any_input_iterator _InputIterator>
1006 constexpr void
1007 assign(_InputIterator __first, _InputIterator __last)
1008 {
1009 if (__first != __last)
1010 __throw_bad_alloc();
1011 }
1012
1013 template<__detail::__container_compatible_range<_Tp> _Rg>
1014 constexpr void
1015 assign_range(_Rg&& __rg)
1016 {
1017 if (ranges::begin(__rg) != ranges::end(__rg))
1018 __throw_bad_alloc();
1019 }
1020
1021 constexpr void
1022 assign(size_type __n, const _Tp& __u)
1023 {
1024 if (__n != 0)
1025 __throw_bad_alloc();
1026 }
1027
1028 constexpr void
1029 assign(initializer_list<_Tp> __il)
1030 {
1031 if (__il.size() != 0)
1032 __throw_bad_alloc();
1033 }
1034
1035 // iterators
1036 [[nodiscard]]
1037 constexpr iterator
1038 begin() noexcept { return iterator(nullptr); }
1039
1040 [[nodiscard]]
1041 constexpr const_iterator
1042 begin() const noexcept { return const_iterator(nullptr); }
1043
1044 [[nodiscard]]
1045 constexpr iterator
1046 end() noexcept { return iterator(nullptr); }
1047
1048 [[nodiscard]]
1049 constexpr const_iterator
1050 end() const noexcept { return const_iterator(nullptr); }
1051
1052 [[nodiscard]]
1053 constexpr reverse_iterator
1054 rbegin() noexcept
1055 { return reverse_iterator(end()); }
1056
1057 [[nodiscard]]
1058 constexpr const_reverse_iterator
1059 rbegin() const noexcept
1060 { return const_reverse_iterator(end()); }
1061
1062 [[nodiscard]]
1063 constexpr reverse_iterator
1064 rend() noexcept { return reverse_iterator(begin()); }
1065
1066 [[nodiscard]]
1067 constexpr const_reverse_iterator
1068 rend() const noexcept { return const_reverse_iterator(begin()); }
1069
1070 [[nodiscard]]
1071 constexpr const_iterator
1072 cbegin() const noexcept { return begin(); }
1073
1074 [[nodiscard]]
1075 constexpr const_iterator
1076 cend() const noexcept { return end(); }
1077
1078 [[nodiscard]]
1079 constexpr const_reverse_iterator
1080 crbegin() const noexcept { return rbegin(); }
1081
1082 [[nodiscard]]
1083 constexpr const_reverse_iterator
1084 crend() const noexcept { return rend(); }
1085
1086 // [containers.sequences.inplace.vector.members] size/capacity
1087 [[nodiscard]]
1088 constexpr bool
1089 empty() const noexcept { return true; }
1090
1091 [[nodiscard]]
1092 constexpr size_type
1093 size() const noexcept { return 0; }
1094
1095 [[nodiscard]]
1096 static constexpr size_type
1097 max_size() noexcept { return 0; }
1098
1099 [[nodiscard]]
1100 static constexpr size_type
1101 capacity() noexcept { return 0; }
1102
1103 constexpr void
1104 resize(size_type __n)
1105 {
1106 if (__n != 0)
1107 __throw_bad_alloc();
1108 }
1109
1110 constexpr void
1111 resize(size_type __n, const _Tp&)
1112 {
1113 if (__n != 0)
1114 __throw_bad_alloc();
1115 }
1116
1117 static constexpr void
1118 reserve(size_type __n)
1119 {
1120 if (__n != 0)
1121 __throw_bad_alloc();
1122 }
1123
1124 static constexpr void
1125 shrink_to_fit() { }
1126
1127 // element access
1128 [[nodiscard,noreturn]]
1129 constexpr reference
1130 operator[](size_type)
1131 { __builtin_trap(); }
1132
1133 [[nodiscard,noreturn]]
1134 constexpr const_reference
1135 operator[](size_type) const
1136 { __builtin_trap(); }
1137
1138 [[nodiscard,noreturn]]
1139 constexpr const_reference
1140 at(size_type __n) const
1141 {
1142 std::__throw_out_of_range_fmt(__N("inplace_vector::at: __n "
1143 "(which is %zu) "
1144 ">= size() (which is 0)"),
1145 __n);
1146 }
1147
1148 [[nodiscard,noreturn]]
1149 constexpr reference
1150 at(size_type __n)
1151 {
1152 std::__throw_out_of_range_fmt(__N("inplace_vector::at: __n "
1153 "(which is %zu) "
1154 ">= size() (which is 0)"),
1155 __n);
1156 }
1157
1158 [[nodiscard,noreturn]]
1159 constexpr reference
1160 front()
1161 { __builtin_trap(); }
1162
1163 [[nodiscard,noreturn]]
1164 constexpr const_reference
1165 front() const
1166 { __builtin_trap(); }
1167
1168 [[nodiscard,noreturn]]
1169 constexpr reference
1170 back()
1171 { __builtin_trap(); }
1172
1173 [[nodiscard,noreturn]]
1174 constexpr const_reference
1175 back() const
1176 { __builtin_trap(); }
1177
1178 // [containers.sequences.inplace.vector.data], data access
1179
1180 [[nodiscard]]
1181 constexpr _Tp*
1182 data() noexcept
1183 { return nullptr; }
1184
1185 [[nodiscard]]
1186 constexpr const _Tp*
1187 data() const noexcept
1188 { return nullptr; }
1189
1190 // [containers.sequences.inplace.vector.modifiers], modifiers
1191 template<typename... _Args>
1192 [[noreturn]]
1193 constexpr _Tp&
1194 emplace_back(_Args&&...)
1195 { __throw_bad_alloc(); }
1196
1197 [[noreturn]]
1198 constexpr _Tp&
1199 push_back(const _Tp&)
1200 { __throw_bad_alloc(); }
1201
1202 [[noreturn]]
1203 constexpr _Tp&
1204 push_back(_Tp&&)
1205 { __throw_bad_alloc(); }
1206
1207 template<__detail::__container_compatible_range<_Tp> _Rg>
1208 constexpr void
1209 append_range(_Rg&& __rg)
1210 {
1211 if (ranges::begin(__rg) != ranges::end(__rg))
1212 __throw_bad_alloc();
1213 }
1214
1215 [[noreturn]]
1216 constexpr void
1217 pop_back()
1218 { __builtin_trap(); }
1219
1220 template<typename... _Args>
1221 constexpr optional<_Tp&>
1222 try_emplace_back(_Args&&...)
1223 { return nullopt; }
1224
1225 constexpr optional<_Tp&>
1226 try_push_back(const _Tp&)
1227 { return nullopt; }
1228
1229 constexpr optional<_Tp&>
1230 try_push_back(_Tp&&)
1231 { return nullopt; }
1232
1233 template<typename... _Args>
1234 [[noreturn]]
1235 constexpr _Tp&
1236 unchecked_emplace_back(_Args&&...)
1237 { __builtin_trap(); }
1238
1239 [[noreturn]]
1240 constexpr _Tp&
1241 unchecked_push_back(const _Tp&)
1242 { __builtin_trap(); }
1243
1244 [[noreturn]]
1245 constexpr _Tp&
1246 unchecked_push_back(_Tp&&)
1247 { __builtin_trap(); }
1248
1249 template<typename... _Args>
1250 [[noreturn]]
1251 constexpr iterator
1252 emplace(const_iterator, _Args&&...)
1253 { __throw_bad_alloc(); }
1254
1255 [[noreturn]]
1256 constexpr iterator
1257 insert(const_iterator, const _Tp&)
1258 { __throw_bad_alloc(); }
1259
1260 [[noreturn]]
1261 constexpr iterator
1262 insert(const_iterator, _Tp&&)
1263 { __throw_bad_alloc(); }
1264
1265 constexpr iterator
1266 insert(const_iterator, size_type __n, const _Tp&)
1267 {
1268 if (__n != 0)
1269 __throw_bad_alloc();
1270 return begin();
1271 }
1272
1273 template<typename _InputIterator>
1274 constexpr iterator
1275 insert(const_iterator, _InputIterator __first, _InputIterator __last)
1276 {
1277 if (__first != __last)
1278 __throw_bad_alloc();
1279 return begin();
1280 }
1281
1282 template<__detail::__container_compatible_range<_Tp> _Rg>
1283 constexpr iterator
1284 insert_range(const_iterator, _Rg&& __rg)
1285 {
1286 if (ranges::begin(__rg) != ranges::end(__rg))
1287 __throw_bad_alloc();
1288 return begin();
1289 }
1290
1291 constexpr iterator
1292 insert(const_iterator, initializer_list<_Tp> __il)
1293 {
1294 if (__il.size() != 0)
1295 __throw_bad_alloc();
1296 return begin();
1297 }
1298
1299 [[noreturn]]
1300 constexpr iterator
1301 erase(const_iterator)
1302 { __builtin_trap(); }
1303
1304 constexpr iterator
1305 erase(const_iterator __first, const_iterator __last)
1306 {
1307 __glibcxx_assert(__first == __last);
1308 return begin();
1309 }
1310
1311 constexpr void
1312 swap(inplace_vector& __x)
1313 noexcept
1314 { }
1315
1316 constexpr void
1317 clear() noexcept
1318 { }
1319
1320 constexpr friend bool
1321 operator==(const inplace_vector&, const inplace_vector&)
1322 { return true; }
1323
1324 constexpr friend auto
1325 operator<=>(const inplace_vector&, const inplace_vector&)
1326 requires requires (const _Tp __t) {
1327 { __t < __t } -> __detail::__boolean_testable;
1328 }
1329 { return std::strong_ordering::equal; }
1330
1331 // n.b. there is not explicit wording requiring that swap for inplace_vector,
1332 // with zero size, works even if element type is not swappable. However given
1333 // that move operations are required to be present and trivial, it makes sense
1334 // to support them.
1335 constexpr friend void
1336 swap(inplace_vector&, inplace_vector&) noexcept
1337 { }
1338 };
1339
1340_GLIBCXX_END_NAMESPACE_CONTAINER
1341
1342 template<typename _Tp, size_t _Nm, typename _Predicate>
1343 constexpr size_t
1344 erase_if(_GLIBCXX_STD_C::inplace_vector<_Tp, _Nm>& __cont,
1345 _Predicate __pred)
1346 {
1347 if constexpr (_Nm != 0)
1348 return __detail::__erase_if(__cont, __cont, std::move(__pred));
1349
1350 return 0;
1351 }
1352
1353 template<typename _Tp, size_t _Nm, typename _Up = _Tp>
1354 constexpr size_t
1355 erase(_GLIBCXX_STD_C::inplace_vector<_Tp, _Nm>& __cont, const _Up& __value)
1356 { return std::erase_if(__cont, __gnu_cxx::__ops::__equal_to(__value)); }
1357
1358_GLIBCXX_END_NAMESPACE_VERSION
1359} // namespace
1360
1361#ifdef _GLIBCXX_DEBUG
1362# include <debug/inplace_vector>
1363#endif
1364
1365#endif // __glibcxx_inplace_vector
1366#endif // _GLIBCXX_INPLACE_VECTOR
constexpr _ForwardIterator uninitialized_value_construct_n(_ForwardIterator __first, _Size __count)
Value-initializes objects in the range [first,first+count).
constexpr _ForwardIterator uninitialized_move(_InputIterator __first, _InputIterator __last, _ForwardIterator __result)
Move-construct from the range [first,last) into result.
constexpr _ForwardIterator uninitialized_fill_n(_ForwardIterator __first, _Size __n, const _Tp &__x)
Copies the value x into the range [first,first+n).
constexpr _ForwardIterator uninitialized_copy(_InputIterator __first, _InputIterator __last, _ForwardIterator __result)
Copies the range [first,last) into result.
constexpr _Tp * addressof(_Tp &__r) noexcept
Returns the actual address of the object or function referenced by r, even in the presence of an over...
Definition move.h:176
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Definition move.h:138
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
Definition move.h:72
constexpr auto lexicographical_compare_three_way(_InputIter1 __first1, _InputIter1 __last1, _InputIter2 __first2, _InputIter2 __last2, _Comp __comp) -> decltype(__comp(*__first1, *__first2))
Performs dictionary comparison on ranges.
constexpr nullopt_t nullopt
Tag to disengage optional objects.
ISO C++ entities toplevel namespace is std.
constexpr auto rbegin(_Container &__cont) noexcept(noexcept(__cont.rbegin())) -> decltype(__cont.rbegin())
Return a reverse iterator pointing to the last element of the container.
constexpr _ForwardIterator _Destroy_n(_ForwardIterator __first, _Size __count)
constexpr auto cend(const _Container &__cont) noexcept(noexcept(std::end(__cont))) -> decltype(std::end(__cont))
Return an iterator pointing to one past the last element of the const container.
constexpr auto end(_Container &__cont) noexcept(noexcept(__cont.end())) -> decltype(__cont.end())
Return an iterator pointing to one past the last element of the container.
constexpr auto empty(const _Container &__cont) noexcept(noexcept(__cont.empty())) -> decltype(__cont.empty())
Return whether a container is empty.
constexpr auto crbegin(const _Container &__cont) noexcept(noexcept(std::rbegin(__cont))) -> decltype(std::rbegin(__cont))
Return a reverse iterator pointing to the last element of the const container.
constexpr auto size(const _Container &__cont) noexcept(noexcept(__cont.size())) -> decltype(__cont.size())
Return the size of a container.
constexpr auto data(_Container &__cont) noexcept(noexcept(__cont.data())) -> decltype(__cont.data())
Return the data pointer of a container.
constexpr auto cbegin(const _Container &__cont) noexcept(noexcept(std::begin(__cont))) -> decltype(std::begin(__cont))
Return an iterator pointing to the first element of the const container.
constexpr auto rend(_Container &__cont) noexcept(noexcept(__cont.rend())) -> decltype(__cont.rend())
Return a reverse iterator pointing one past the first element of the container.
constexpr auto begin(_Container &__cont) noexcept(noexcept(__cont.begin())) -> decltype(__cont.begin())
Return an iterator pointing to the first element of the container.
constexpr auto crend(const _Container &__cont) noexcept(noexcept(std::rend(__cont))) -> decltype(std::rend(__cont))
Return a reverse iterator pointing one past the first element of the const container.
initializer_list
Common iterator class.