libstdc++
stl_bvector.h
Go to the documentation of this file.
1// vector<bool> specialization -*- C++ -*-
2
3// Copyright (C) 2001-2023 Free Software Foundation, Inc.
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/*
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-1999
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
51/** @file bits/stl_bvector.h
52 * This is an internal header file, included by other library headers.
53 * Do not attempt to use it directly. @headername{vector}
54 */
55
56#ifndef _STL_BVECTOR_H
57#define _STL_BVECTOR_H 1
58
59#ifndef _GLIBCXX_ALWAYS_INLINE
60#define _GLIBCXX_ALWAYS_INLINE inline __attribute__((__always_inline__))
61#endif
62
63#if __cplusplus >= 201103L
64#include <initializer_list>
66#endif
67
68namespace std _GLIBCXX_VISIBILITY(default)
69{
70_GLIBCXX_BEGIN_NAMESPACE_VERSION
71
72 typedef unsigned long _Bit_type;
73 enum { _S_word_bit = int(__CHAR_BIT__ * sizeof(_Bit_type)) };
74
75 __attribute__((__nonnull__))
76 _GLIBCXX20_CONSTEXPR
77 void
78 __fill_bvector_n(_Bit_type*, size_t, bool) _GLIBCXX_NOEXCEPT;
79
80_GLIBCXX_BEGIN_NAMESPACE_CONTAINER
81
82 struct _Bit_reference
83 {
84 _Bit_type * _M_p;
85 _Bit_type _M_mask;
86
87 _GLIBCXX20_CONSTEXPR
88 _Bit_reference(_Bit_type * __x, _Bit_type __y)
89 : _M_p(__x), _M_mask(__y) { }
90
91 _GLIBCXX20_CONSTEXPR
92 _Bit_reference() _GLIBCXX_NOEXCEPT : _M_p(0), _M_mask(0) { }
93
94#if __cplusplus >= 201103L
95 _Bit_reference(const _Bit_reference&) = default;
96#endif
97
98 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
99 operator bool() const _GLIBCXX_NOEXCEPT
100 { return !!(*_M_p & _M_mask); }
101
102 _GLIBCXX20_CONSTEXPR
103 _Bit_reference&
104 operator=(bool __x) _GLIBCXX_NOEXCEPT
105 {
106 if (__x)
107 *_M_p |= _M_mask;
108 else
109 *_M_p &= ~_M_mask;
110 return *this;
111 }
112
113#if __cplusplus > 202002L
114 constexpr const _Bit_reference&
115 operator=(bool __x) const noexcept
116 {
117 if (__x)
118 *_M_p |= _M_mask;
119 else
120 *_M_p &= ~_M_mask;
121 return *this;
122 }
123#endif // C++23
124
125 _GLIBCXX20_CONSTEXPR
126 _Bit_reference&
127 operator=(const _Bit_reference& __x) _GLIBCXX_NOEXCEPT
128 { return *this = bool(__x); }
129
130 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
131 bool
132 operator==(const _Bit_reference& __x) const
133 { return bool(*this) == bool(__x); }
134
135 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
136 bool
137 operator<(const _Bit_reference& __x) const
138 { return !bool(*this) && bool(__x); }
139
140 _GLIBCXX20_CONSTEXPR
141 void
142 flip() _GLIBCXX_NOEXCEPT
143 { *_M_p ^= _M_mask; }
144
145#if __cplusplus >= 201103L
146 _GLIBCXX20_CONSTEXPR
147 friend void
148 swap(_Bit_reference __x, _Bit_reference __y) noexcept
149 {
150 bool __tmp = __x;
151 __x = __y;
152 __y = __tmp;
153 }
154
155 _GLIBCXX20_CONSTEXPR
156 friend void
157 swap(_Bit_reference __x, bool& __y) noexcept
158 {
159 bool __tmp = __x;
160 __x = __y;
161 __y = __tmp;
162 }
163
164 _GLIBCXX20_CONSTEXPR
165 friend void
166 swap(bool& __x, _Bit_reference __y) noexcept
167 {
168 bool __tmp = __x;
169 __x = __y;
170 __y = __tmp;
171 }
172#endif
173 };
174
175// Ignore warnings about std::iterator.
176#pragma GCC diagnostic push
177#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
178 struct _Bit_iterator_base
179 : public std::iterator<std::random_access_iterator_tag, bool>
180 {
181 _Bit_type * _M_p;
182 unsigned int _M_offset;
183
184 _GLIBCXX20_CONSTEXPR _GLIBCXX_ALWAYS_INLINE
185 void
186 _M_assume_normalized() const
187 {
188#if __has_attribute(__assume__) && !defined(__clang__)
189 unsigned int __ofst = _M_offset;
190 __attribute__ ((__assume__ (__ofst < unsigned(_S_word_bit))));
191#endif
192 }
193
194 _GLIBCXX20_CONSTEXPR
195 _Bit_iterator_base(_Bit_type * __x, unsigned int __y)
196 : _M_p(__x), _M_offset(__y) { }
197
198 _GLIBCXX20_CONSTEXPR
199 void
200 _M_bump_up()
201 {
202 _M_assume_normalized();
203 if (_M_offset++ == int(_S_word_bit) - 1)
204 {
205 _M_offset = 0;
206 ++_M_p;
207 }
208 }
209
210 _GLIBCXX20_CONSTEXPR
211 void
212 _M_bump_down()
213 {
214 _M_assume_normalized();
215 if (_M_offset-- == 0)
216 {
217 _M_offset = int(_S_word_bit) - 1;
218 --_M_p;
219 }
220 }
221
222 _GLIBCXX20_CONSTEXPR
223 void
224 _M_incr(ptrdiff_t __i)
225 {
226 _M_assume_normalized();
227 difference_type __n = __i + _M_offset;
228 _M_p += __n / int(_S_word_bit);
229 __n = __n % int(_S_word_bit);
230 if (__n < 0)
231 {
232 __n += int(_S_word_bit);
233 --_M_p;
234 }
235 _M_offset = static_cast<unsigned int>(__n);
236 }
237
238 _GLIBCXX_NODISCARD
239 friend _GLIBCXX20_CONSTEXPR bool
240 operator==(const _Bit_iterator_base& __x, const _Bit_iterator_base& __y)
241 {
242 __x._M_assume_normalized();
243 __y._M_assume_normalized();
244 return __x._M_p == __y._M_p && __x._M_offset == __y._M_offset;
245 }
246
247#if __cpp_lib_three_way_comparison
248 [[nodiscard]]
249 friend constexpr strong_ordering
250 operator<=>(const _Bit_iterator_base& __x, const _Bit_iterator_base& __y)
251 noexcept
252 {
253 __x._M_assume_normalized();
254 __y._M_assume_normalized();
255 if (const auto __cmp = __x._M_p <=> __y._M_p; __cmp != 0)
256 return __cmp;
257 return __x._M_offset <=> __y._M_offset;
258 }
259#else
260 _GLIBCXX_NODISCARD
261 friend bool
262 operator<(const _Bit_iterator_base& __x, const _Bit_iterator_base& __y)
263 {
264 __x._M_assume_normalized();
265 __y._M_assume_normalized();
266 return __x._M_p < __y._M_p
267 || (__x._M_p == __y._M_p && __x._M_offset < __y._M_offset);
268 }
269
270 _GLIBCXX_NODISCARD
271 friend bool
272 operator!=(const _Bit_iterator_base& __x, const _Bit_iterator_base& __y)
273 { return !(__x == __y); }
274
275 _GLIBCXX_NODISCARD
276 friend bool
277 operator>(const _Bit_iterator_base& __x, const _Bit_iterator_base& __y)
278 { return __y < __x; }
279
280 _GLIBCXX_NODISCARD
281 friend bool
282 operator<=(const _Bit_iterator_base& __x, const _Bit_iterator_base& __y)
283 { return !(__y < __x); }
284
285 _GLIBCXX_NODISCARD
286 friend bool
287 operator>=(const _Bit_iterator_base& __x, const _Bit_iterator_base& __y)
288 { return !(__x < __y); }
289#endif // three-way comparison
290
291 friend _GLIBCXX20_CONSTEXPR ptrdiff_t
292 operator-(const _Bit_iterator_base& __x, const _Bit_iterator_base& __y)
293 {
294 __x._M_assume_normalized();
295 __y._M_assume_normalized();
296 return (int(_S_word_bit) * (__x._M_p - __y._M_p)
297 + __x._M_offset - __y._M_offset);
298 }
299 };
300#pragma GCC diagnostic pop
301
302 struct _Bit_iterator : public _Bit_iterator_base
303 {
304 typedef _Bit_reference reference;
305#if __cplusplus > 201703L
306 typedef void pointer;
307#else
308 typedef _Bit_reference* pointer;
309#endif
310 typedef _Bit_iterator iterator;
311
312 _GLIBCXX20_CONSTEXPR
313 _Bit_iterator() : _Bit_iterator_base(0, 0) { }
314
315 _GLIBCXX20_CONSTEXPR
316 _Bit_iterator(_Bit_type * __x, unsigned int __y)
317 : _Bit_iterator_base(__x, __y) { }
318
319 _GLIBCXX20_CONSTEXPR
320 iterator
321 _M_const_cast() const
322 { return *this; }
323
324 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
325 reference
326 operator*() const
327 {
328 _M_assume_normalized();
329 return reference(_M_p, 1UL << _M_offset);
330 }
331
332 _GLIBCXX20_CONSTEXPR
333 iterator&
334 operator++()
335 {
336 _M_bump_up();
337 return *this;
338 }
339
340 _GLIBCXX20_CONSTEXPR
341 iterator
342 operator++(int)
343 {
344 iterator __tmp = *this;
345 _M_bump_up();
346 return __tmp;
347 }
348
349 _GLIBCXX20_CONSTEXPR
350 iterator&
351 operator--()
352 {
353 _M_bump_down();
354 return *this;
355 }
356
357 _GLIBCXX20_CONSTEXPR
358 iterator
359 operator--(int)
360 {
361 iterator __tmp = *this;
362 _M_bump_down();
363 return __tmp;
364 }
365
366 _GLIBCXX20_CONSTEXPR
367 iterator&
368 operator+=(difference_type __i)
369 {
370 _M_incr(__i);
371 return *this;
372 }
373
374 _GLIBCXX20_CONSTEXPR
375 iterator&
376 operator-=(difference_type __i)
377 {
378 *this += -__i;
379 return *this;
380 }
381
382 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
383 reference
384 operator[](difference_type __i) const
385 { return *(*this + __i); }
386
387 _GLIBCXX_NODISCARD
388 friend _GLIBCXX20_CONSTEXPR iterator
389 operator+(const iterator& __x, difference_type __n)
390 {
391 iterator __tmp = __x;
392 __tmp += __n;
393 return __tmp;
394 }
395
396 _GLIBCXX_NODISCARD
397 friend _GLIBCXX20_CONSTEXPR iterator
398 operator+(difference_type __n, const iterator& __x)
399 { return __x + __n; }
400
401 _GLIBCXX_NODISCARD
402 friend _GLIBCXX20_CONSTEXPR iterator
403 operator-(const iterator& __x, difference_type __n)
404 {
405 iterator __tmp = __x;
406 __tmp -= __n;
407 return __tmp;
408 }
409 };
410
411 struct _Bit_const_iterator : public _Bit_iterator_base
412 {
413 typedef bool reference;
414 typedef bool const_reference;
415#if __cplusplus > 201703L
416 typedef void pointer;
417#else
418 typedef const bool* pointer;
419#endif
420 typedef _Bit_const_iterator const_iterator;
421
422 _GLIBCXX20_CONSTEXPR
423 _Bit_const_iterator() : _Bit_iterator_base(0, 0) { }
424
425 _GLIBCXX20_CONSTEXPR
426 _Bit_const_iterator(_Bit_type * __x, unsigned int __y)
427 : _Bit_iterator_base(__x, __y) { }
428
429 _GLIBCXX20_CONSTEXPR
430 _Bit_const_iterator(const _Bit_iterator& __x)
431 : _Bit_iterator_base(__x._M_p, __x._M_offset) { }
432
433 _GLIBCXX20_CONSTEXPR
434 _Bit_iterator
435 _M_const_cast() const
436 { return _Bit_iterator(_M_p, _M_offset); }
437
438 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
439 const_reference
440 operator*() const
441 {
442 _M_assume_normalized();
443 return _Bit_reference(_M_p, 1UL << _M_offset);
444 }
445
446 _GLIBCXX20_CONSTEXPR
447 const_iterator&
448 operator++()
449 {
450 _M_bump_up();
451 return *this;
452 }
453
454 _GLIBCXX20_CONSTEXPR
455 const_iterator
456 operator++(int)
457 {
458 const_iterator __tmp = *this;
459 _M_bump_up();
460 return __tmp;
461 }
462
463 _GLIBCXX20_CONSTEXPR
464 const_iterator&
465 operator--()
466 {
467 _M_bump_down();
468 return *this;
469 }
470
471 _GLIBCXX20_CONSTEXPR
472 const_iterator
473 operator--(int)
474 {
475 const_iterator __tmp = *this;
476 _M_bump_down();
477 return __tmp;
478 }
479
480 _GLIBCXX20_CONSTEXPR
481 const_iterator&
482 operator+=(difference_type __i)
483 {
484 _M_incr(__i);
485 return *this;
486 }
487
488 _GLIBCXX20_CONSTEXPR
489 const_iterator&
490 operator-=(difference_type __i)
491 {
492 *this += -__i;
493 return *this;
494 }
495
496 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
497 const_reference
498 operator[](difference_type __i) const
499 { return *(*this + __i); }
500
501 _GLIBCXX_NODISCARD
502 friend _GLIBCXX20_CONSTEXPR const_iterator
503 operator+(const const_iterator& __x, difference_type __n)
504 {
505 const_iterator __tmp = __x;
506 __tmp += __n;
507 return __tmp;
508 }
509
510 _GLIBCXX_NODISCARD
511 friend _GLIBCXX20_CONSTEXPR const_iterator
512 operator-(const const_iterator& __x, difference_type __n)
513 {
514 const_iterator __tmp = __x;
515 __tmp -= __n;
516 return __tmp;
517 }
518
519 _GLIBCXX_NODISCARD
520 friend _GLIBCXX20_CONSTEXPR const_iterator
521 operator+(difference_type __n, const const_iterator& __x)
522 { return __x + __n; }
523 };
524
525 template<typename _Alloc>
526 struct _Bvector_base
527 {
528 typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template
529 rebind<_Bit_type>::other _Bit_alloc_type;
530 typedef typename __gnu_cxx::__alloc_traits<_Bit_alloc_type>
531 _Bit_alloc_traits;
532 typedef typename _Bit_alloc_traits::pointer _Bit_pointer;
533
534 struct _Bvector_impl_data
535 {
536#if !_GLIBCXX_INLINE_VERSION
537 _Bit_iterator _M_start;
538#else
539 // We don't need the offset field for the start, it's always zero.
540 struct {
541 _Bit_type* _M_p;
542 // Allow assignment from iterators (assume offset is zero):
543 _GLIBCXX20_CONSTEXPR
544 void operator=(_Bit_iterator __it) { _M_p = __it._M_p; }
545 } _M_start;
546#endif
547 _Bit_iterator _M_finish;
548 _Bit_pointer _M_end_of_storage;
549
550 _GLIBCXX20_CONSTEXPR
551 _Bvector_impl_data() _GLIBCXX_NOEXCEPT
552 : _M_start(), _M_finish(), _M_end_of_storage()
553 { }
554
555#if __cplusplus >= 201103L
556 _Bvector_impl_data(const _Bvector_impl_data&) = default;
557
558 _Bvector_impl_data&
559 operator=(const _Bvector_impl_data&) = default;
560
561 _GLIBCXX20_CONSTEXPR
562 _Bvector_impl_data(_Bvector_impl_data&& __x) noexcept
563 : _Bvector_impl_data(__x)
564 { __x._M_reset(); }
565
566 _GLIBCXX20_CONSTEXPR
567 void
568 _M_move_data(_Bvector_impl_data&& __x) noexcept
569 {
570 *this = __x;
571 __x._M_reset();
572 }
573#endif
574
575 _GLIBCXX20_CONSTEXPR
576 void
577 _M_reset() _GLIBCXX_NOEXCEPT
578 { *this = _Bvector_impl_data(); }
579
580 _GLIBCXX20_CONSTEXPR
581 void
582 _M_swap_data(_Bvector_impl_data& __x) _GLIBCXX_NOEXCEPT
583 {
584 // Do not use std::swap(_M_start, __x._M_start), etc as it loses
585 // information used by TBAA.
586 std::swap(*this, __x);
587 }
588 };
589
590 struct _Bvector_impl
591 : public _Bit_alloc_type, public _Bvector_impl_data
592 {
593 _GLIBCXX20_CONSTEXPR
594 _Bvector_impl() _GLIBCXX_NOEXCEPT_IF(
595 is_nothrow_default_constructible<_Bit_alloc_type>::value)
596#if __cpp_concepts && __cpp_lib_type_trait_variable_templates
597 requires is_default_constructible_v<_Bit_alloc_type>
598#endif
599 : _Bit_alloc_type()
600 { }
601
602 _GLIBCXX20_CONSTEXPR
603 _Bvector_impl(const _Bit_alloc_type& __a) _GLIBCXX_NOEXCEPT
604 : _Bit_alloc_type(__a)
605 { }
606
607#if __cplusplus >= 201103L
608 // Not defaulted, to enforce noexcept(true) even when
609 // !is_nothrow_move_constructible<_Bit_alloc_type>.
610 _GLIBCXX20_CONSTEXPR
611 _Bvector_impl(_Bvector_impl&& __x) noexcept
612 : _Bit_alloc_type(std::move(__x)), _Bvector_impl_data(std::move(__x))
613 { }
614
615 _GLIBCXX20_CONSTEXPR
616 _Bvector_impl(_Bit_alloc_type&& __a, _Bvector_impl&& __x) noexcept
617 : _Bit_alloc_type(std::move(__a)), _Bvector_impl_data(std::move(__x))
618 { }
619#endif
620
621 _GLIBCXX20_CONSTEXPR
622 _Bit_type*
623 _M_end_addr() const _GLIBCXX_NOEXCEPT
624 {
625 if (this->_M_end_of_storage)
626 return std::__addressof(this->_M_end_of_storage[-1]) + 1;
627 return 0;
628 }
629 };
630
631 public:
632 typedef _Alloc allocator_type;
633
634 _GLIBCXX20_CONSTEXPR
635 _Bit_alloc_type&
636 _M_get_Bit_allocator() _GLIBCXX_NOEXCEPT
637 { return this->_M_impl; }
638
639 _GLIBCXX20_CONSTEXPR
640 const _Bit_alloc_type&
641 _M_get_Bit_allocator() const _GLIBCXX_NOEXCEPT
642 { return this->_M_impl; }
643
644 _GLIBCXX20_CONSTEXPR
645 allocator_type
646 get_allocator() const _GLIBCXX_NOEXCEPT
647 { return allocator_type(_M_get_Bit_allocator()); }
648
649#if __cplusplus >= 201103L
650 _Bvector_base() = default;
651#else
652 _Bvector_base() { }
653#endif
654
655 _GLIBCXX20_CONSTEXPR
656 _Bvector_base(const allocator_type& __a)
657 : _M_impl(_Bit_alloc_type(__a)) { }
658
659#if __cplusplus >= 201103L
660 _Bvector_base(_Bvector_base&&) = default;
661
662 _GLIBCXX20_CONSTEXPR
663 _Bvector_base(_Bvector_base&& __x, const allocator_type& __a) noexcept
664 : _M_impl(_Bit_alloc_type(__a), std::move(__x._M_impl))
665 { }
666#endif
667
668 _GLIBCXX20_CONSTEXPR
669 ~_Bvector_base()
670 { this->_M_deallocate(); }
671
672 protected:
673 _Bvector_impl _M_impl;
674
675 _GLIBCXX20_CONSTEXPR
676 _Bit_pointer
677 _M_allocate(size_t __n)
678 {
679 _Bit_pointer __p = _Bit_alloc_traits::allocate(_M_impl, _S_nword(__n));
680#if __cpp_lib_is_constant_evaluated && __cpp_constexpr_dynamic_alloc
682 {
683 __n = _S_nword(__n);
684 for (size_t __i = 0; __i < __n; ++__i)
685 std::construct_at(std::to_address(__p) + __i);
686 }
687#endif
688 return __p;
689 }
690
691 _GLIBCXX20_CONSTEXPR
692 void
693 _M_deallocate()
694 {
695 if (_M_impl._M_start._M_p)
696 {
697 const size_t __n = _M_impl._M_end_addr() - _M_impl._M_start._M_p;
699 _M_impl._M_end_of_storage - __n,
700 __n);
701 _M_impl._M_reset();
702 }
703 }
704
705#if __cplusplus >= 201103L
706 _GLIBCXX20_CONSTEXPR
707 void
708 _M_move_data(_Bvector_base&& __x) noexcept
709 { _M_impl._M_move_data(std::move(__x._M_impl)); }
710#endif
711
712 // Caller must ensure the addition won't overflow.
713 _GLIBCXX_CONSTEXPR
714 static size_t
715 _S_nword(size_t __n)
716 { return (__n + int(_S_word_bit) - 1) / int(_S_word_bit); }
717 };
718
719 /**
720 * @brief A specialization of vector for booleans which offers fixed time
721 * access to individual elements in any order.
722 *
723 * @ingroup sequences
724 * @headerfile vector
725 * @since C++98
726 *
727 * @tparam _Alloc Allocator type.
728 *
729 * Note that vector<bool> does not actually meet the requirements for being
730 * a container. This is because the reference and pointer types are not
731 * really references and pointers to bool. See DR96 for details. @see
732 * vector for function documentation.
733 *
734 * In some terminology a %vector can be described as a dynamic
735 * C-style array, it offers fast and efficient access to individual
736 * elements in any order and saves the user from worrying about
737 * memory and size allocation. Subscripting ( @c [] ) access is
738 * also provided as with C-style arrays.
739 */
740 template<typename _Alloc>
741 class vector<bool, _Alloc> : protected _Bvector_base<_Alloc>
742 {
743 typedef _Bvector_base<_Alloc> _Base;
744 typedef typename _Base::_Bit_pointer _Bit_pointer;
745 typedef typename _Base::_Bit_alloc_traits _Bit_alloc_traits;
746
747#if __cplusplus >= 201103L
748 friend struct std::hash<vector>;
749#endif
750
751 public:
752 typedef bool value_type;
753 typedef size_t size_type;
754 typedef ptrdiff_t difference_type;
755 typedef _Bit_reference reference;
756 typedef bool const_reference;
757 typedef _Bit_reference* pointer;
758 typedef const bool* const_pointer;
759 typedef _Bit_iterator iterator;
760 typedef _Bit_const_iterator const_iterator;
761 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
762 typedef std::reverse_iterator<iterator> reverse_iterator;
763 typedef _Alloc allocator_type;
764
765 _GLIBCXX20_CONSTEXPR
766 allocator_type
767 get_allocator() const
768 { return _Base::get_allocator(); }
769
770 protected:
771 using _Base::_M_allocate;
772 using _Base::_M_deallocate;
773 using _Base::_S_nword;
774 using _Base::_M_get_Bit_allocator;
775
776 public:
777#if __cplusplus >= 201103L
778 vector() = default;
779#else
780 vector() { }
781#endif
782
783 _GLIBCXX20_CONSTEXPR
784 explicit
785 vector(const allocator_type& __a)
786 : _Base(__a) { }
787
788#if __cplusplus >= 201103L
789 _GLIBCXX20_CONSTEXPR
790 explicit
791 vector(size_type __n, const allocator_type& __a = allocator_type())
792 : vector(__n, false, __a)
793 { }
794
795 _GLIBCXX20_CONSTEXPR
796 vector(size_type __n, const bool& __value,
797 const allocator_type& __a = allocator_type())
798#else
799 explicit
800 vector(size_type __n, const bool& __value = bool(),
801 const allocator_type& __a = allocator_type())
802#endif
803 : _Base(__a)
804 {
805 _M_check_and_initialize(__n);
806 _M_initialize_value(__value);
807 }
808
809 _GLIBCXX20_CONSTEXPR
810 vector(const vector& __x)
811 : _Base(_Bit_alloc_traits::_S_select_on_copy(__x._M_get_Bit_allocator()))
812 {
813 const_iterator __xbegin = __x.begin(), __xend = __x.end();
814 _M_initialize(__x.size());
815 _M_copy_aligned(__xbegin, __xend, begin());
816 }
817
818#if __cplusplus >= 201103L
819 vector(vector&&) = default;
820
821 private:
822 _GLIBCXX20_CONSTEXPR
823 vector(vector&& __x, const allocator_type& __a, true_type) noexcept
824 : _Base(std::move(__x), __a)
825 { }
826
827 _GLIBCXX20_CONSTEXPR
828 vector(vector&& __x, const allocator_type& __a, false_type)
829 : _Base(__a)
830 {
831 if (__x.get_allocator() == __a)
832 this->_M_move_data(std::move(__x));
833 else
834 {
835 _M_initialize(__x.size());
836 _M_copy_aligned(__x.begin(), __x.end(), begin());
837 __x.clear();
838 }
839 }
840
841 public:
842 _GLIBCXX20_CONSTEXPR
843 vector(vector&& __x, const __type_identity_t<allocator_type>& __a)
844 noexcept(_Bit_alloc_traits::_S_always_equal())
845 : vector(std::move(__x), __a,
847 { }
848
849 _GLIBCXX20_CONSTEXPR
850 vector(const vector& __x, const __type_identity_t<allocator_type>& __a)
851 : _Base(__a)
852 {
853 _M_initialize(__x.size());
854 _M_copy_aligned(__x.begin(), __x.end(), begin());
855 }
856
857 _GLIBCXX20_CONSTEXPR
859 const allocator_type& __a = allocator_type())
860 : _Base(__a)
861 {
862 _M_initialize_range(__l.begin(), __l.end(),
864 }
865#endif
866
867#if __cplusplus >= 201103L
868 template<typename _InputIterator,
869 typename = std::_RequireInputIter<_InputIterator>>
870 _GLIBCXX20_CONSTEXPR
871 vector(_InputIterator __first, _InputIterator __last,
872 const allocator_type& __a = allocator_type())
873 : _Base(__a)
874 {
875 _M_initialize_range(__first, __last,
876 std::__iterator_category(__first));
877 }
878#else
879 template<typename _InputIterator>
880 vector(_InputIterator __first, _InputIterator __last,
881 const allocator_type& __a = allocator_type())
882 : _Base(__a)
883 {
884 // Check whether it's an integral type. If so, it's not an iterator.
885 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
886 _M_initialize_dispatch(__first, __last, _Integral());
887 }
888#endif
889
890 _GLIBCXX20_CONSTEXPR
891 ~vector() _GLIBCXX_NOEXCEPT { }
892
893 _GLIBCXX20_CONSTEXPR
894 vector&
895 operator=(const vector& __x)
896 {
897 if (&__x == this)
898 return *this;
899#if __cplusplus >= 201103L
900 if (_Bit_alloc_traits::_S_propagate_on_copy_assign())
901 {
902 if (this->_M_get_Bit_allocator() != __x._M_get_Bit_allocator())
903 {
904 this->_M_deallocate();
905 std::__alloc_on_copy(_M_get_Bit_allocator(),
906 __x._M_get_Bit_allocator());
907 _M_initialize(__x.size());
908 }
909 else
910 std::__alloc_on_copy(_M_get_Bit_allocator(),
911 __x._M_get_Bit_allocator());
912 }
913#endif
914 if (__x.size() > capacity())
915 {
916 this->_M_deallocate();
917 _M_initialize(__x.size());
918 }
919 this->_M_impl._M_finish = _M_copy_aligned(__x.begin(), __x.end(),
920 begin());
921 return *this;
922 }
923
924#if __cplusplus >= 201103L
925 _GLIBCXX20_CONSTEXPR
926 vector&
927 operator=(vector&& __x) noexcept(_Bit_alloc_traits::_S_nothrow_move())
928 {
929 if (_Bit_alloc_traits::_S_propagate_on_move_assign()
930 || this->_M_get_Bit_allocator() == __x._M_get_Bit_allocator())
931 {
932 this->_M_deallocate();
933 this->_M_move_data(std::move(__x));
934 std::__alloc_on_move(_M_get_Bit_allocator(),
935 __x._M_get_Bit_allocator());
936 }
937 else
938 {
939 if (__x.size() > capacity())
940 {
941 this->_M_deallocate();
942 _M_initialize(__x.size());
943 }
944 this->_M_impl._M_finish = _M_copy_aligned(__x.begin(), __x.end(),
945 begin());
946 __x.clear();
947 }
948 return *this;
949 }
950
951 _GLIBCXX20_CONSTEXPR
952 vector&
954 {
955 this->assign(__l.begin(), __l.end());
956 return *this;
957 }
958#endif
959
960 // assign(), a generalized assignment member function. Two
961 // versions: one that takes a count, and one that takes a range.
962 // The range version is a member template, so we dispatch on whether
963 // or not the type is an integer.
964 _GLIBCXX20_CONSTEXPR
965 void
966 assign(size_type __n, const bool& __x)
967 { _M_fill_assign(__n, __x); }
968
969#if __cplusplus >= 201103L
970 template<typename _InputIterator,
971 typename = std::_RequireInputIter<_InputIterator>>
972 _GLIBCXX20_CONSTEXPR
973 void
974 assign(_InputIterator __first, _InputIterator __last)
975 { _M_assign_aux(__first, __last, std::__iterator_category(__first)); }
976#else
977 template<typename _InputIterator>
978 void
979 assign(_InputIterator __first, _InputIterator __last)
980 {
981 // Check whether it's an integral type. If so, it's not an iterator.
982 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
983 _M_assign_dispatch(__first, __last, _Integral());
984 }
985#endif
986
987#if __cplusplus >= 201103L
988 _GLIBCXX20_CONSTEXPR
989 void
991 { _M_assign_aux(__l.begin(), __l.end(), random_access_iterator_tag()); }
992#endif
993
994 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
995 iterator
996 begin() _GLIBCXX_NOEXCEPT
997 { return iterator(this->_M_impl._M_start._M_p, 0); }
998
999 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1000 const_iterator
1001 begin() const _GLIBCXX_NOEXCEPT
1002 { return const_iterator(this->_M_impl._M_start._M_p, 0); }
1003
1004 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1005 iterator
1006 end() _GLIBCXX_NOEXCEPT
1007 { return this->_M_impl._M_finish; }
1008
1009 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1010 const_iterator
1011 end() const _GLIBCXX_NOEXCEPT
1012 { return this->_M_impl._M_finish; }
1013
1014 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1015 reverse_iterator
1016 rbegin() _GLIBCXX_NOEXCEPT
1017 { return reverse_iterator(end()); }
1018
1019 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1020 const_reverse_iterator
1021 rbegin() const _GLIBCXX_NOEXCEPT
1022 { return const_reverse_iterator(end()); }
1023
1024 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1025 reverse_iterator
1026 rend() _GLIBCXX_NOEXCEPT
1027 { return reverse_iterator(begin()); }
1028
1029 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1030 const_reverse_iterator
1031 rend() const _GLIBCXX_NOEXCEPT
1032 { return const_reverse_iterator(begin()); }
1033
1034#if __cplusplus >= 201103L
1035 [[__nodiscard__]] _GLIBCXX20_CONSTEXPR
1036 const_iterator
1037 cbegin() const noexcept
1038 { return const_iterator(this->_M_impl._M_start._M_p, 0); }
1039
1040 [[__nodiscard__]] _GLIBCXX20_CONSTEXPR
1041 const_iterator
1042 cend() const noexcept
1043 { return this->_M_impl._M_finish; }
1044
1045 [[__nodiscard__]] _GLIBCXX20_CONSTEXPR
1046 const_reverse_iterator
1047 crbegin() const noexcept
1048 { return const_reverse_iterator(end()); }
1049
1050 [[__nodiscard__]] _GLIBCXX20_CONSTEXPR
1051 const_reverse_iterator
1052 crend() const noexcept
1053 { return const_reverse_iterator(begin()); }
1054#endif
1055
1056 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1057 size_type
1058 size() const _GLIBCXX_NOEXCEPT
1059 { return size_type(end() - begin()); }
1060
1061 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1062 size_type
1063 max_size() const _GLIBCXX_NOEXCEPT
1064 {
1065 const size_type __isize =
1066 __gnu_cxx::__numeric_traits<difference_type>::__max
1067 - int(_S_word_bit) + 1;
1068 const size_type __asize
1069 = _Bit_alloc_traits::max_size(_M_get_Bit_allocator());
1070 return (__asize <= __isize / int(_S_word_bit)
1071 ? __asize * int(_S_word_bit) : __isize);
1072 }
1073
1074 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1075 size_type
1076 capacity() const _GLIBCXX_NOEXCEPT
1077 { return size_type(const_iterator(this->_M_impl._M_end_addr(), 0)
1078 - begin()); }
1079
1080 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1081 bool
1082 empty() const _GLIBCXX_NOEXCEPT
1083 { return begin() == end(); }
1084
1085 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1086 reference
1087 operator[](size_type __n)
1088 { return begin()[__n]; }
1089
1090 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1091 const_reference
1092 operator[](size_type __n) const
1093 { return begin()[__n]; }
1094
1095 protected:
1096 _GLIBCXX20_CONSTEXPR
1097 void
1098 _M_range_check(size_type __n) const
1099 {
1100 if (__n >= this->size())
1101 __throw_out_of_range_fmt(__N("vector<bool>::_M_range_check: __n "
1102 "(which is %zu) >= this->size() "
1103 "(which is %zu)"),
1104 __n, this->size());
1105 }
1106
1107 public:
1108 _GLIBCXX20_CONSTEXPR
1109 reference
1110 at(size_type __n)
1111 {
1112 _M_range_check(__n);
1113 return (*this)[__n];
1114 }
1115
1116 _GLIBCXX20_CONSTEXPR
1117 const_reference
1118 at(size_type __n) const
1119 {
1120 _M_range_check(__n);
1121 return (*this)[__n];
1122 }
1123
1124 _GLIBCXX20_CONSTEXPR
1125 void
1126 reserve(size_type __n)
1127 {
1128 if (__n > max_size())
1129 __throw_length_error(__N("vector::reserve"));
1130 if (capacity() < __n)
1131 _M_reallocate(__n);
1132 }
1133
1134 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1135 reference
1136 front()
1137 { return *begin(); }
1138
1139 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1140 const_reference
1141 front() const
1142 { return *begin(); }
1143
1144 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1145 reference
1146 back()
1147 { return *(end() - 1); }
1148
1149 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1150 const_reference
1151 back() const
1152 { return *(end() - 1); }
1153
1154 _GLIBCXX20_CONSTEXPR
1155 void
1156 push_back(bool __x)
1157 {
1158 if (this->_M_impl._M_finish._M_p != this->_M_impl._M_end_addr())
1159 *this->_M_impl._M_finish++ = __x;
1160 else
1161 _M_insert_aux(end(), __x);
1162 }
1163
1164 _GLIBCXX20_CONSTEXPR
1165 void
1166 swap(vector& __x) _GLIBCXX_NOEXCEPT
1167 {
1168#if __cplusplus >= 201103L
1169 __glibcxx_assert(_Bit_alloc_traits::propagate_on_container_swap::value
1170 || _M_get_Bit_allocator() == __x._M_get_Bit_allocator());
1171#endif
1172 this->_M_impl._M_swap_data(__x._M_impl);
1173 _Bit_alloc_traits::_S_on_swap(_M_get_Bit_allocator(),
1174 __x._M_get_Bit_allocator());
1175 }
1176
1177 // [23.2.5]/1, third-to-last entry in synopsis listing
1178 _GLIBCXX20_CONSTEXPR
1179 static void
1180 swap(reference __x, reference __y) _GLIBCXX_NOEXCEPT
1181 {
1182 bool __tmp = __x;
1183 __x = __y;
1184 __y = __tmp;
1185 }
1186
1187 _GLIBCXX20_CONSTEXPR
1188 iterator
1189#if __cplusplus >= 201103L
1190 insert(const_iterator __position, const bool& __x)
1191#else
1192 insert(iterator __position, const bool& __x)
1193#endif
1194 {
1195 const difference_type __n = __position - begin();
1196 if (this->_M_impl._M_finish._M_p != this->_M_impl._M_end_addr()
1197 && __position == end())
1198 *this->_M_impl._M_finish++ = __x;
1199 else
1200 _M_insert_aux(__position._M_const_cast(), __x);
1201 return begin() + __n;
1202 }
1203
1204#if _GLIBCXX_USE_DEPRECATED
1205 _GLIBCXX_DEPRECATED_SUGGEST("insert(position, false)")
1206 iterator
1207 insert(const_iterator __position)
1208 { return this->insert(__position._M_const_cast(), false); }
1209#endif
1210
1211#if __cplusplus >= 201103L
1212 template<typename _InputIterator,
1213 typename = std::_RequireInputIter<_InputIterator>>
1214 _GLIBCXX20_CONSTEXPR
1215 iterator
1216 insert(const_iterator __position,
1217 _InputIterator __first, _InputIterator __last)
1218 {
1219 difference_type __offset = __position - cbegin();
1220 _M_insert_range(__position._M_const_cast(),
1221 __first, __last,
1222 std::__iterator_category(__first));
1223 return begin() + __offset;
1224 }
1225#else
1226 template<typename _InputIterator>
1227 void
1228 insert(iterator __position,
1229 _InputIterator __first, _InputIterator __last)
1230 {
1231 // Check whether it's an integral type. If so, it's not an iterator.
1232 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
1233 _M_insert_dispatch(__position, __first, __last, _Integral());
1234 }
1235#endif
1236
1237#if __cplusplus >= 201103L
1238 _GLIBCXX20_CONSTEXPR
1239 iterator
1240 insert(const_iterator __position, size_type __n, const bool& __x)
1241 {
1242 difference_type __offset = __position - cbegin();
1243 _M_fill_insert(__position._M_const_cast(), __n, __x);
1244 return begin() + __offset;
1245 }
1246#else
1247 void
1248 insert(iterator __position, size_type __n, const bool& __x)
1249 { _M_fill_insert(__position, __n, __x); }
1250#endif
1251
1252#if __cplusplus >= 201103L
1253 _GLIBCXX20_CONSTEXPR
1254 iterator
1255 insert(const_iterator __p, initializer_list<bool> __l)
1256 { return this->insert(__p, __l.begin(), __l.end()); }
1257#endif
1258
1259 _GLIBCXX20_CONSTEXPR
1260 void
1261 pop_back()
1262 { --this->_M_impl._M_finish; }
1263
1264 _GLIBCXX20_CONSTEXPR
1265 iterator
1266#if __cplusplus >= 201103L
1267 erase(const_iterator __position)
1268#else
1269 erase(iterator __position)
1270#endif
1271 { return _M_erase(__position._M_const_cast()); }
1272
1273 _GLIBCXX20_CONSTEXPR
1274 iterator
1275#if __cplusplus >= 201103L
1276 erase(const_iterator __first, const_iterator __last)
1277#else
1278 erase(iterator __first, iterator __last)
1279#endif
1280 { return _M_erase(__first._M_const_cast(), __last._M_const_cast()); }
1281
1282 _GLIBCXX20_CONSTEXPR
1283 void
1284 resize(size_type __new_size, bool __x = bool())
1285 {
1286 if (__new_size < size())
1287 _M_erase_at_end(begin() + difference_type(__new_size));
1288 else
1289 insert(end(), __new_size - size(), __x);
1290 }
1291
1292#if __cplusplus >= 201103L
1293 _GLIBCXX20_CONSTEXPR
1294 void
1296 { _M_shrink_to_fit(); }
1297#endif
1298
1299 _GLIBCXX20_CONSTEXPR
1300 void
1301 flip() _GLIBCXX_NOEXCEPT
1302 {
1303 _Bit_type * const __end = this->_M_impl._M_end_addr();
1304 for (_Bit_type * __p = this->_M_impl._M_start._M_p; __p != __end; ++__p)
1305 *__p = ~*__p;
1306 }
1307
1308 _GLIBCXX20_CONSTEXPR
1309 void
1310 clear() _GLIBCXX_NOEXCEPT
1311 { _M_erase_at_end(begin()); }
1312
1313#if __cplusplus >= 201103L
1314 template<typename... _Args>
1315#if __cplusplus > 201402L
1316 _GLIBCXX20_CONSTEXPR
1317 reference
1318#else
1319 void
1320#endif
1321 emplace_back(_Args&&... __args)
1322 {
1323 push_back(bool(std::forward<_Args>(__args)...));
1324#if __cplusplus > 201402L
1325 return back();
1326#endif
1327 }
1328
1329 template<typename... _Args>
1330 _GLIBCXX20_CONSTEXPR
1331 iterator
1332 emplace(const_iterator __pos, _Args&&... __args)
1333 { return insert(__pos, bool(std::forward<_Args>(__args)...)); }
1334#endif
1335
1336 protected:
1337 // Precondition: __first._M_offset == 0 && __result._M_offset == 0.
1338 _GLIBCXX20_CONSTEXPR
1339 iterator
1340 _M_copy_aligned(const_iterator __first, const_iterator __last,
1341 iterator __result)
1342 {
1343 _Bit_type* __q = std::copy(__first._M_p, __last._M_p, __result._M_p);
1344 return std::copy(const_iterator(__last._M_p, 0), __last,
1345 iterator(__q, 0));
1346 }
1347
1348 _GLIBCXX20_CONSTEXPR
1349 void
1350 _M_initialize(size_type __n)
1351 {
1352 // Caller must ensure this, to avoid overflow in _S_nword.
1353 _GLIBCXX_DEBUG_ASSERT(__n <= (size_t)-_S_word_bit);
1354
1355 if (__n)
1356 {
1357 _Bit_pointer __q = this->_M_allocate(__n);
1358 this->_M_impl._M_end_of_storage = __q + _S_nword(__n);
1359 iterator __start = iterator(std::__addressof(*__q), 0);
1360 this->_M_impl._M_start = __start;
1361 this->_M_impl._M_finish = __start + difference_type(__n);
1362 }
1363 }
1364
1365 template<typename _SizeT>
1366 _GLIBCXX20_CONSTEXPR
1367 void
1368 _M_check_and_initialize(_SizeT __n)
1369 {
1370 if (__n > max_size() || __n < 0)
1371 __throw_length_error(
1372 __N("Cannot create std::vector<bool> larger than max_size()"));
1373 _M_initialize(static_cast<size_type>(__n));
1374 }
1375
1376 _GLIBCXX20_CONSTEXPR
1377 void
1378 _M_initialize_value(bool __x) _GLIBCXX_NOEXCEPT
1379 {
1380 if (_Bit_type* __p = this->_M_impl._M_start._M_p)
1381 __fill_bvector_n(__p, this->_M_impl._M_end_addr() - __p, __x);
1382 }
1383
1384 _GLIBCXX20_CONSTEXPR
1385 void
1386 _M_reallocate(size_type __n);
1387
1388#if __cplusplus >= 201103L
1389 _GLIBCXX20_CONSTEXPR
1390 bool
1391 _M_shrink_to_fit();
1392#endif
1393
1394#if __cplusplus < 201103L
1395 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1396 // 438. Ambiguity in the "do the right thing" clause
1397 template<typename _Integer>
1398 void
1399 _M_initialize_dispatch(_Integer __n, _Integer __x, __true_type)
1400 {
1401 _M_check_and_initialize(static_cast<size_type>(__n));
1402 _M_initialize_value(__x);
1403 }
1404
1405 template<typename _InputIterator>
1406 void
1407 _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
1408 __false_type)
1409 { _M_initialize_range(__first, __last,
1410 std::__iterator_category(__first)); }
1411#endif
1412
1413 template<typename _InputIterator>
1414 _GLIBCXX20_CONSTEXPR
1415 void
1416 _M_initialize_range(_InputIterator __first, _InputIterator __last,
1418 {
1419 for (; __first != __last; ++__first)
1420 push_back(*__first);
1421 }
1422
1423 template<typename _ForwardIterator>
1424 _GLIBCXX20_CONSTEXPR
1425 void
1426 _M_initialize_range(_ForwardIterator __first, _ForwardIterator __last,
1428 {
1429 _M_check_and_initialize(std::distance(__first, __last));
1430 std::copy(__first, __last, begin());
1431 }
1432
1433#if __cplusplus < 201103L
1434 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1435 // 438. Ambiguity in the "do the right thing" clause
1436 template<typename _Integer>
1437 void
1438 _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
1439 { _M_fill_assign(__n, __val); }
1440
1441 template<class _InputIterator>
1442 void
1443 _M_assign_dispatch(_InputIterator __first, _InputIterator __last,
1444 __false_type)
1445 { _M_assign_aux(__first, __last, std::__iterator_category(__first)); }
1446#endif
1447
1448 _GLIBCXX20_CONSTEXPR
1449 void
1450 _M_fill_assign(size_t __n, bool __x)
1451 {
1452 if (__n > size())
1453 {
1454 _M_initialize_value(__x);
1455 insert(end(), __n - size(), __x);
1456 }
1457 else
1458 {
1459 _M_erase_at_end(begin() + __n);
1460 _M_initialize_value(__x);
1461 }
1462 }
1463
1464 template<typename _InputIterator>
1465 _GLIBCXX20_CONSTEXPR
1466 void
1467 _M_assign_aux(_InputIterator __first, _InputIterator __last,
1469 {
1470 iterator __cur = begin();
1471 for (; __first != __last && __cur != end(); ++__cur, (void)++__first)
1472 *__cur = *__first;
1473 if (__first == __last)
1474 _M_erase_at_end(__cur);
1475 else
1476 insert(end(), __first, __last);
1477 }
1478
1479 template<typename _ForwardIterator>
1480 _GLIBCXX20_CONSTEXPR
1481 void
1482 _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
1484 {
1485 const size_type __len = std::distance(__first, __last);
1486 if (__len < size())
1487 _M_erase_at_end(std::copy(__first, __last, begin()));
1488 else
1489 {
1490 _ForwardIterator __mid = __first;
1491 std::advance(__mid, size());
1492 std::copy(__first, __mid, begin());
1493 insert(end(), __mid, __last);
1494 }
1495 }
1496
1497#if __cplusplus < 201103L
1498 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1499 // 438. Ambiguity in the "do the right thing" clause
1500 template<typename _Integer>
1501 void
1502 _M_insert_dispatch(iterator __pos, _Integer __n, _Integer __x,
1503 __true_type)
1504 { _M_fill_insert(__pos, __n, __x); }
1505
1506 template<typename _InputIterator>
1507 void
1508 _M_insert_dispatch(iterator __pos,
1509 _InputIterator __first, _InputIterator __last,
1510 __false_type)
1511 { _M_insert_range(__pos, __first, __last,
1512 std::__iterator_category(__first)); }
1513#endif
1514
1515 _GLIBCXX20_CONSTEXPR
1516 void
1517 _M_fill_insert(iterator __position, size_type __n, bool __x);
1518
1519 template<typename _InputIterator>
1520 _GLIBCXX20_CONSTEXPR
1521 void
1522 _M_insert_range(iterator __pos, _InputIterator __first,
1523 _InputIterator __last, std::input_iterator_tag)
1524 {
1525 for (; __first != __last; ++__first)
1526 {
1527 __pos = insert(__pos, *__first);
1528 ++__pos;
1529 }
1530 }
1531
1532 template<typename _ForwardIterator>
1533 _GLIBCXX20_CONSTEXPR
1534 void
1535 _M_insert_range(iterator __position, _ForwardIterator __first,
1536 _ForwardIterator __last, std::forward_iterator_tag);
1537
1538 _GLIBCXX20_CONSTEXPR
1539 void
1540 _M_insert_aux(iterator __position, bool __x);
1541
1542 _GLIBCXX20_CONSTEXPR
1543 size_type
1544 _M_check_len(size_type __n, const char* __s) const
1545 {
1546 if (max_size() - size() < __n)
1547 __throw_length_error(__N(__s));
1548
1549 const size_type __len = size() + std::max(size(), __n);
1550 return (__len < size() || __len > max_size()) ? max_size() : __len;
1551 }
1552
1553 _GLIBCXX20_CONSTEXPR
1554 void
1555 _M_erase_at_end(iterator __pos)
1556 { this->_M_impl._M_finish = __pos; }
1557
1558 _GLIBCXX20_CONSTEXPR
1559 iterator
1560 _M_erase(iterator __pos);
1561
1562 _GLIBCXX20_CONSTEXPR
1563 iterator
1564 _M_erase(iterator __first, iterator __last);
1565
1566 protected:
1567 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1568 // DR 464. Suggestion for new member functions in standard containers.
1569 // N.B. DR 464 says nothing about vector<bool> but we need something
1570 // here due to the using-declaration in __gnu_debug::vector.
1571 // vector class.
1572#if __cplusplus >= 201103L
1573 void data() = delete;
1574#else
1575 void data() { }
1576#endif
1577 };
1578
1579_GLIBCXX_END_NAMESPACE_CONTAINER
1580
1581 // Fill a partial word.
1582 _GLIBCXX20_CONSTEXPR
1583 inline void
1584 __fill_bvector(_Bit_type* __v, unsigned int __first, unsigned int __last,
1585 bool __x) _GLIBCXX_NOEXCEPT
1586 {
1587 const _Bit_type __fmask = ~0ul << __first;
1588 const _Bit_type __lmask = ~0ul >> (_S_word_bit - __last);
1589 const _Bit_type __mask = __fmask & __lmask;
1590
1591 if (__x)
1592 *__v |= __mask;
1593 else
1594 *__v &= ~__mask;
1595 }
1596
1597 // Fill N full words, as if using memset, but usable in constant expressions.
1598 __attribute__((__nonnull__))
1599 _GLIBCXX20_CONSTEXPR
1600 inline void
1601 __fill_bvector_n(_Bit_type* __p, size_t __n, bool __x) _GLIBCXX_NOEXCEPT
1602 {
1603#if __cpp_lib_is_constant_evaluated
1605 {
1606 for (size_t __i = 0; __i < __n; ++__i)
1607 __p[__i] = __x ? ~0ul : 0ul;
1608 return;
1609 }
1610#endif
1611 __builtin_memset(__p, __x ? ~0 : 0, __n * sizeof(_Bit_type));
1612 }
1613
1614
1615 _GLIBCXX20_CONSTEXPR
1616 inline void
1617 __fill_a1(_GLIBCXX_STD_C::_Bit_iterator __first,
1618 _GLIBCXX_STD_C::_Bit_iterator __last, const bool& __x)
1619 {
1620 if (__first._M_p != __last._M_p)
1621 {
1622 _Bit_type* __first_p = __first._M_p;
1623 if (__first._M_offset != 0)
1624 __fill_bvector(__first_p++, __first._M_offset, _S_word_bit, __x);
1625
1626 __fill_bvector_n(__first_p, __last._M_p - __first_p, __x);
1627
1628 if (__last._M_offset != 0)
1629 __fill_bvector(__last._M_p, 0, __last._M_offset, __x);
1630 }
1631 else if (__first._M_offset != __last._M_offset)
1632 __fill_bvector(__first._M_p, __first._M_offset, __last._M_offset, __x);
1633 }
1634
1635#if __cplusplus >= 201103L
1636 // DR 1182.
1637 /// std::hash specialization for vector<bool>.
1638 template<typename _Alloc>
1639 struct hash<_GLIBCXX_STD_C::vector<bool, _Alloc>>
1640 : public __hash_base<size_t, _GLIBCXX_STD_C::vector<bool, _Alloc>>
1641 {
1642 size_t
1643 operator()(const _GLIBCXX_STD_C::vector<bool, _Alloc>&) const noexcept;
1644 };
1645#endif // C++11
1646
1647_GLIBCXX_END_NAMESPACE_VERSION
1648} // namespace std
1649
1650#endif
constexpr _Tp * to_address(_Tp *__ptr) noexcept
Obtain address referenced by a pointer to an object.
Definition ptr_traits.h:250
integral_constant< bool, true > true_type
The type used as a compile-time boolean with true value.
Definition type_traits:82
constexpr bool is_constant_evaluated() noexcept
Returns true only when called during constant evaluation.
Definition type_traits:3651
integral_constant< bool, false > false_type
The type used as a compile-time boolean with false value.
Definition type_traits:85
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Definition move.h:97
void swap(any &__x, any &__y) noexcept
Exchange the states of two any objects.
Definition any:429
constexpr _Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
Definition move.h:51
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
Definition move.h:70
constexpr const _Tp & max(const _Tp &, const _Tp &)
This does what you think it does.
constexpr iterator_traits< _Iter >::iterator_category __iterator_category(const _Iter &)
ISO C++ entities toplevel namespace is std.
constexpr iterator_traits< _InputIterator >::difference_type distance(_InputIterator __first, _InputIterator __last)
A generalization of pointer arithmetic.
constexpr void advance(_InputIterator &__i, _Distance __n)
A generalization of pointer arithmetic.
initializer_list
Primary class template hash.
typename __detected_or_t< is_empty< _Bit_alloc_type >, __equal, _Bit_alloc_type >::type is_always_equal
Marking input iterators.
Forward iterators support a superset of input iterator operations.
Random-access iterators support a superset of bidirectional iterator operations.
A standard container which offers fixed time access to individual elements in any order.
Definition stl_vector.h:429
constexpr iterator insert(const_iterator __position, const value_type &__x)
Inserts given value into vector before specified iterator.
Definition vector.tcc:135
constexpr void push_back(const value_type &__x)
Add data to the end of the vector.
constexpr reverse_iterator rbegin() noexcept
Definition stl_vector.h:913
constexpr iterator end() noexcept
Definition stl_vector.h:893
vector()=default
Creates a vector with no elements.
constexpr iterator emplace(const_iterator __position, _Args &&... __args)
Inserts an object in vector before specified iterator.
constexpr iterator begin() noexcept
Definition stl_vector.h:873
constexpr size_type capacity() const noexcept
constexpr ~vector() noexcept
Definition stl_vector.h:733
constexpr void assign(size_type __n, const value_type &__val)
Assigns a given value to a vector.
Definition stl_vector.h:808
constexpr void swap(vector &__x) noexcept
Swaps data with another vector.
constexpr _Tp * data() noexcept
constexpr void pop_back() noexcept
Removes last element.
constexpr void reserve(size_type __n)
Attempt to preallocate enough memory for specified number of elements.
Definition vector.tcc:68
constexpr reference at(size_type __n)
Provides access to the data contained in the vector.
constexpr void resize(size_type __new_size)
Resizes the vector to the specified number of elements.
constexpr void _M_range_check(size_type __n) const
Safety check used only from at().
constexpr reference front() noexcept
constexpr iterator erase(const_iterator __position)
Remove element at given position.
constexpr bool empty() const noexcept
constexpr reverse_iterator rend() noexcept
Definition stl_vector.h:933
constexpr const_reverse_iterator crbegin() const noexcept
Definition stl_vector.h:974
constexpr const_iterator cbegin() const noexcept
Definition stl_vector.h:954
constexpr void clear() noexcept
constexpr allocator_type get_allocator() const noexcept
Get a copy of the memory allocation object.
Definition stl_vector.h:311
constexpr size_type size() const noexcept
Definition stl_vector.h:992
constexpr vector & operator=(const vector &__x)
Vector assignment operator.
Definition vector.tcc:211
constexpr reference back() noexcept
constexpr const_reverse_iterator crend() const noexcept
Definition stl_vector.h:984
constexpr const_iterator cend() const noexcept
Definition stl_vector.h:964
constexpr reference operator[](size_type __n) noexcept
Subscript access to the data contained in the vector.
constexpr void shrink_to_fit()
constexpr size_type max_size() const noexcept
Definition stl_vector.h:998
static constexpr pointer allocate(_Bit_alloc_type &__a, size_type __n)
static constexpr void deallocate(_Bit_alloc_type &__a, pointer __p, size_type __n)
static constexpr size_type max_size(const _Bit_alloc_type &__a) noexcept