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