1// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP___BIT_REFERENCE
11#define _LIBCPP___BIT_REFERENCE
12
13#include <__algorithm/comp.h>
14#include <__algorithm/copy.h>
15#include <__algorithm/copy_backward.h>
16#include <__algorithm/copy_n.h>
17#include <__algorithm/equal.h>
18#include <__algorithm/fill_n.h>
19#include <__algorithm/min.h>
20#include <__algorithm/rotate.h>
21#include <__algorithm/specialized_algorithms.h>
22#include <__assert>
23#include <__bit/countr.h>
24#include <__compare/ordering.h>
25#include <__config>
26#include <__cstddef/ptrdiff_t.h>
27#include <__cstddef/size_t.h>
28#include <__functional/identity.h>
29#include <__fwd/bit_reference.h>
30#include <__iterator/iterator_traits.h>
31#include <__memory/construct_at.h>
32#include <__memory/pointer_traits.h>
33#include <__type_traits/conditional.h>
34#include <__type_traits/desugars_to.h>
35#include <__type_traits/enable_if.h>
36#include <__type_traits/is_constant_evaluated.h>
37#include <__type_traits/is_same.h>
38#include <__type_traits/is_unsigned.h>
39#include <__type_traits/void_t.h>
40#include <__utility/pair.h>
41#include <__utility/swap.h>
42#include <climits>
43
44#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
45# pragma GCC system_header
46#endif
47
48_LIBCPP_PUSH_MACROS
49#include <__undef_macros>
50
51_LIBCPP_BEGIN_NAMESPACE_STD
52
53template <class _Cp>
54class __bit_const_reference;
55
56template <class _Tp>
57struct __has_storage_type {
58 static const bool value = false;
59};
60
61template <class, class>
62struct __size_difference_type_traits {
63 using difference_type = ptrdiff_t;
64 using size_type = size_t;
65};
66
67template <class _Cp>
68struct __size_difference_type_traits<_Cp, __void_t<typename _Cp::difference_type, typename _Cp::size_type> > {
69 using difference_type = typename _Cp::difference_type;
70 using size_type = typename _Cp::size_type;
71};
72
73// The `__x_mask` functions are designed to work exclusively with any unsigned `_StorageType`s, including small
74// integral types such as unsigned char/short, `uint8_t`, and `uint16_t`. To prevent undefined behavior or
75// ambiguities due to integral promotions for the small integral types, all intermediate bitwise operations are
76// explicitly cast back to the unsigned `_StorageType`.
77
78// Creates a mask of type `_StorageType` with a specified number of leading zeros (__clz) and sets all remaining
79// bits to one.
80template <class _StorageType>
81_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _StorageType __trailing_mask(unsigned __clz) {
82 static_assert(is_unsigned<_StorageType>::value, "__trailing_mask only works with unsigned types");
83 return static_cast<_StorageType>(~static_cast<_StorageType>(0)) >> __clz;
84}
85
86// Creates a mask of type `_StorageType` with a specified number of trailing zeros (__ctz) and sets all remaining
87// bits to one.
88template <class _StorageType>
89_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _StorageType __leading_mask(unsigned __ctz) {
90 static_assert(is_unsigned<_StorageType>::value, "__leading_mask only works with unsigned types");
91 return static_cast<_StorageType>(~static_cast<_StorageType>(0)) << __ctz;
92}
93
94// Creates a mask of type `_StorageType` with a specified number of leading zeros (__clz), a specified number of
95// trailing zeros (__ctz), and sets all bits in between to one.
96template <class _StorageType>
97_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _StorageType __middle_mask(unsigned __clz, unsigned __ctz) {
98 static_assert(is_unsigned<_StorageType>::value, "__middle_mask only works with unsigned types");
99 return std::__leading_mask<_StorageType>(__ctz) & std::__trailing_mask<_StorageType>(__clz);
100}
101
102// This function is designed to operate correctly even for smaller integral types like `uint8_t`, `uint16_t`,
103// or `unsigned short`.
104// See https://github.com/llvm/llvm-project/pull/122410.
105template <class _StoragePointer>
106_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 void
107__fill_masked_range(_StoragePointer __word, unsigned __clz, unsigned __ctz, bool __fill_val) {
108 static_assert(is_unsigned<typename pointer_traits<_StoragePointer>::element_type>::value,
109 "__fill_masked_range must be called with unsigned type");
110 using _StorageType = typename pointer_traits<_StoragePointer>::element_type;
111 _LIBCPP_ASSERT_VALID_INPUT_RANGE(
112 __ctz + __clz < sizeof(_StorageType) * CHAR_BIT, "__fill_masked_range called with invalid range");
113 _StorageType __m = std::__middle_mask<_StorageType>(__clz, __ctz);
114 if (__fill_val)
115 *__word |= __m;
116 else
117 *__word &= ~__m;
118}
119
120template <class _Cp, bool = __has_storage_type<_Cp>::value>
121class __bit_reference {
122 using __storage_type _LIBCPP_NODEBUG = typename _Cp::__storage_type;
123 using __storage_pointer _LIBCPP_NODEBUG = typename _Cp::__storage_pointer;
124
125 __storage_pointer __seg_;
126 __storage_type __mask_;
127
128 friend typename _Cp::__self;
129
130 friend class __bit_const_reference<_Cp>;
131 friend class __bit_iterator<_Cp, false>;
132
133public:
134 using __container _LIBCPP_NODEBUG = typename _Cp::__self;
135
136 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_reference(const __bit_reference&) = default;
137
138 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 operator bool() const _NOEXCEPT {
139 return static_cast<bool>(*__seg_ & __mask_);
140 }
141 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool operator~() const _NOEXCEPT {
142 return !static_cast<bool>(*this);
143 }
144
145 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_reference& operator=(bool __x) _NOEXCEPT {
146 if (__x)
147 *__seg_ |= __mask_;
148 else
149 *__seg_ &= ~__mask_;
150 return *this;
151 }
152
153#if _LIBCPP_STD_VER >= 23
154 _LIBCPP_HIDE_FROM_ABI constexpr const __bit_reference& operator=(bool __x) const noexcept {
155 if (__x)
156 *__seg_ |= __mask_;
157 else
158 *__seg_ &= ~__mask_;
159 return *this;
160 }
161#endif
162
163 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_reference& operator=(const __bit_reference& __x) _NOEXCEPT {
164 return operator=(static_cast<bool>(__x));
165 }
166
167 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void flip() _NOEXCEPT { *__seg_ ^= __mask_; }
168
169private:
170 _LIBCPP_HIDE_FROM_ABI
171 _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit __bit_reference(__storage_pointer __s, __storage_type __m) _NOEXCEPT
172 : __seg_(__s),
173 __mask_(__m) {}
174};
175
176template <class _Cp>
177class __bit_reference<_Cp, false> {};
178
179template <class _Cp>
180inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
181swap(__bit_reference<_Cp> __x, __bit_reference<_Cp> __y) _NOEXCEPT {
182 bool __t = __x;
183 __x = __y;
184 __y = __t;
185}
186
187template <class _Cp, class _Dp>
188inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
189swap(__bit_reference<_Cp> __x, __bit_reference<_Dp> __y) _NOEXCEPT {
190 bool __t = __x;
191 __x = __y;
192 __y = __t;
193}
194
195template <class _Cp>
196inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void swap(__bit_reference<_Cp> __x, bool& __y) _NOEXCEPT {
197 bool __t = __x;
198 __x = __y;
199 __y = __t;
200}
201
202template <class _Cp>
203inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void swap(bool& __x, __bit_reference<_Cp> __y) _NOEXCEPT {
204 bool __t = __x;
205 __x = __y;
206 __y = __t;
207}
208
209template <class _Cp>
210class __bit_const_reference {
211 using __storage_type _LIBCPP_NODEBUG = typename _Cp::__storage_type;
212 using __storage_pointer _LIBCPP_NODEBUG = typename _Cp::__const_storage_pointer;
213
214 __storage_pointer __seg_;
215 __storage_type __mask_;
216
217 friend typename _Cp::__self;
218 friend class __bit_iterator<_Cp, true>;
219
220public:
221 using __container _LIBCPP_NODEBUG = typename _Cp::__self;
222
223 _LIBCPP_HIDE_FROM_ABI __bit_const_reference(const __bit_const_reference&) = default;
224 __bit_const_reference& operator=(const __bit_const_reference&) = delete;
225
226 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_const_reference(const __bit_reference<_Cp>& __x) _NOEXCEPT
227 : __seg_(__x.__seg_),
228 __mask_(__x.__mask_) {}
229
230 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR operator bool() const _NOEXCEPT {
231 return static_cast<bool>(*__seg_ & __mask_);
232 }
233
234 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator<_Cp, true> operator&() const _NOEXCEPT {
235 return __bit_iterator<_Cp, true>(__seg_, static_cast<unsigned>(std::__countr_zero(__mask_)));
236 }
237
238private:
239 _LIBCPP_HIDE_FROM_ABI
240 _LIBCPP_CONSTEXPR explicit __bit_const_reference(__storage_pointer __s, __storage_type __m) _NOEXCEPT
241 : __seg_(__s),
242 __mask_(__m) {}
243};
244
245template <class _Cp>
246struct __bit_array {
247 using difference_type _LIBCPP_NODEBUG = typename __size_difference_type_traits<_Cp>::difference_type;
248 using __storage_type _LIBCPP_NODEBUG = typename _Cp::__storage_type;
249 using __storage_pointer _LIBCPP_NODEBUG = typename _Cp::__storage_pointer;
250 using iterator _LIBCPP_NODEBUG = typename _Cp::iterator;
251
252 static const unsigned __bits_per_word = _Cp::__bits_per_word;
253 static const unsigned _Np = 4;
254
255 difference_type __size_;
256 __storage_type __word_[_Np];
257
258 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static difference_type capacity() {
259 return static_cast<difference_type>(_Np * __bits_per_word);
260 }
261 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit __bit_array(difference_type __s) : __size_(__s) {
262 if (__libcpp_is_constant_evaluated()) {
263 for (size_t __i = 0; __i != __bit_array<_Cp>::_Np; ++__i)
264 std::__construct_at(__word_ + __i, 0);
265 }
266 }
267 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator begin() {
268 return iterator(pointer_traits<__storage_pointer>::pointer_to(__word_[0]), 0);
269 }
270 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator end() {
271 return iterator(pointer_traits<__storage_pointer>::pointer_to(__word_[0]) + __size_ / __bits_per_word,
272 static_cast<unsigned>(__size_ % __bits_per_word));
273 }
274};
275
276template <class _Cp, bool _IsConst, typename _Cp::__storage_type>
277class __bit_iterator {
278public:
279 using difference_type = typename __size_difference_type_traits<_Cp>::difference_type;
280 using value_type = bool;
281 using pointer = __bit_iterator;
282#ifndef _LIBCPP_ABI_BITSET_VECTOR_BOOL_CONST_SUBSCRIPT_RETURN_BOOL
283 using reference = __conditional_t<_IsConst, __bit_const_reference<_Cp>, __bit_reference<_Cp> >;
284#else
285 using reference = __conditional_t<_IsConst, bool, __bit_reference<_Cp> >;
286#endif
287 using iterator_category = random_access_iterator_tag;
288
289private:
290 using __storage_type _LIBCPP_NODEBUG = typename _Cp::__storage_type;
291 using __storage_pointer _LIBCPP_NODEBUG =
292 __conditional_t<_IsConst, typename _Cp::__const_storage_pointer, typename _Cp::__storage_pointer>;
293
294 static const unsigned __bits_per_word = _Cp::__bits_per_word;
295
296 __storage_pointer __seg_;
297 unsigned __ctz_;
298
299public:
300 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator() _NOEXCEPT
301#if _LIBCPP_STD_VER >= 14
302 : __seg_(nullptr),
303 __ctz_(0)
304#endif
305 {
306 }
307
308#ifdef _LIBCPP_ABI_TRIVIALLY_COPYABLE_BIT_ITERATOR
309 template <bool _IsConstDep = _IsConst, __enable_if_t<_IsConstDep, int> = 0>
310 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator(const __bit_iterator<_Cp, false>& __it) _NOEXCEPT
311 : __seg_(__it.__seg_),
312 __ctz_(__it.__ctz_) {}
313
314 _LIBCPP_HIDE_FROM_ABI __bit_iterator(const __bit_iterator&) = default;
315 _LIBCPP_HIDE_FROM_ABI __bit_iterator& operator=(const __bit_iterator&) = default;
316#else
317 // When _IsConst=false, this is the copy constructor.
318 // It is non-trivial. Making it trivial would break ABI.
319 // When _IsConst=true, this is a converting constructor;
320 // the copy and move constructors are implicitly generated
321 // and trivial.
322 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator(const __bit_iterator<_Cp, false>& __it) _NOEXCEPT
323 : __seg_(__it.__seg_),
324 __ctz_(__it.__ctz_) {}
325
326 // When _IsConst=false, we have a user-provided copy constructor,
327 // so we must also provide a copy assignment operator because
328 // the implicit generation of a defaulted one is deprecated.
329 // When _IsConst=true, the assignment operators are
330 // implicitly generated and trivial.
331 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator&
332 operator=(const _If<_IsConst, struct __private_nat, __bit_iterator>& __it) {
333 __seg_ = __it.__seg_;
334 __ctz_ = __it.__ctz_;
335 return *this;
336 }
337#endif
338
339 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference operator*() const _NOEXCEPT {
340 _LIBCPP_ASSERT_INTERNAL(__ctz_ < __bits_per_word, "Dereferencing an invalid __bit_iterator.");
341 return __conditional_t<_IsConst, __bit_const_reference<_Cp>, __bit_reference<_Cp> >(
342 __seg_, __storage_type(1) << __ctz_);
343 }
344
345 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator& operator++() {
346 if (__ctz_ != __bits_per_word - 1)
347 ++__ctz_;
348 else {
349 __ctz_ = 0;
350 ++__seg_;
351 }
352 return *this;
353 }
354
355 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator operator++(int) {
356 __bit_iterator __tmp = *this;
357 ++(*this);
358 return __tmp;
359 }
360
361 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator& operator--() {
362 if (__ctz_ != 0)
363 --__ctz_;
364 else {
365 __ctz_ = __bits_per_word - 1;
366 --__seg_;
367 }
368 return *this;
369 }
370
371 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator operator--(int) {
372 __bit_iterator __tmp = *this;
373 --(*this);
374 return __tmp;
375 }
376
377 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator& operator+=(difference_type __n) {
378 if (__n >= 0)
379 __seg_ += (__n + __ctz_) / __bits_per_word;
380 else
381 __seg_ += static_cast<difference_type>(__n - __bits_per_word + __ctz_ + 1) /
382 static_cast<difference_type>(__bits_per_word);
383 __n &= (__bits_per_word - 1);
384 __ctz_ = static_cast<unsigned>((__n + __ctz_) % __bits_per_word);
385 return *this;
386 }
387
388 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator& operator-=(difference_type __n) {
389 return *this += -__n;
390 }
391
392 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator operator+(difference_type __n) const {
393 __bit_iterator __t(*this);
394 __t += __n;
395 return __t;
396 }
397
398 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator operator-(difference_type __n) const {
399 __bit_iterator __t(*this);
400 __t -= __n;
401 return __t;
402 }
403
404 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 friend __bit_iterator
405 operator+(difference_type __n, const __bit_iterator& __it) {
406 return __it + __n;
407 }
408
409 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 friend difference_type
410 operator-(const __bit_iterator& __x, const __bit_iterator& __y) {
411 return (__x.__seg_ - __y.__seg_) * __bits_per_word + __x.__ctz_ - __y.__ctz_;
412 }
413
414 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference operator[](difference_type __n) const {
415 return *(*this + __n);
416 }
417
418 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 friend bool
419 operator==(const __bit_iterator& __x, const __bit_iterator& __y) {
420 return __x.__seg_ == __y.__seg_ && __x.__ctz_ == __y.__ctz_;
421 }
422
423#if _LIBCPP_STD_VER <= 17
424 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 friend bool
425 operator!=(const __bit_iterator& __x, const __bit_iterator& __y) {
426 return !(__x == __y);
427 }
428
429 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 friend bool
430 operator<(const __bit_iterator& __x, const __bit_iterator& __y) {
431 return __x.__seg_ < __y.__seg_ || (__x.__seg_ == __y.__seg_ && __x.__ctz_ < __y.__ctz_);
432 }
433
434 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 friend bool
435 operator>(const __bit_iterator& __x, const __bit_iterator& __y) {
436 return __y < __x;
437 }
438
439 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 friend bool
440 operator<=(const __bit_iterator& __x, const __bit_iterator& __y) {
441 return !(__y < __x);
442 }
443
444 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 friend bool
445 operator>=(const __bit_iterator& __x, const __bit_iterator& __y) {
446 return !(__x < __y);
447 }
448#else // _LIBCPP_STD_VER <= 17
449 _LIBCPP_HIDE_FROM_ABI constexpr friend strong_ordering
450 operator<=>(const __bit_iterator& __x, const __bit_iterator& __y) {
451 if (__x.__seg_ < __y.__seg_)
452 return strong_ordering::less;
453
454 if (__x.__seg_ == __y.__seg_)
455 return __x.__ctz_ <=> __y.__ctz_;
456
457 return strong_ordering::greater;
458 }
459#endif // _LIBCPP_STD_VER <= 17
460
461private:
462 _LIBCPP_HIDE_FROM_ABI
463 _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit __bit_iterator(__storage_pointer __s, unsigned __ctz) _NOEXCEPT
464 : __seg_(__s),
465 __ctz_(__ctz) {
466 _LIBCPP_ASSERT_INTERNAL(
467 __ctz_ < __bits_per_word, "__bit_iterator initialized with an invalid number of trailing zeros.");
468 }
469
470 friend typename _Cp::__self;
471
472 friend class __bit_reference<_Cp>;
473 friend class __bit_const_reference<_Cp>;
474 friend class __bit_iterator<_Cp, true>;
475 template <class _Dp>
476 friend struct __bit_array;
477
478 template <class _Dp, bool _IC>
479 _LIBCPP_CONSTEXPR_SINCE_CXX20 friend __bit_iterator<_Dp, false> __copy_backward_aligned(
480 __bit_iterator<_Dp, _IC> __first, __bit_iterator<_Dp, _IC> __last, __bit_iterator<_Dp, false> __result);
481 template <class _Dp, bool _IC>
482 _LIBCPP_CONSTEXPR_SINCE_CXX20 friend __bit_iterator<_Dp, false> __copy_backward_unaligned(
483 __bit_iterator<_Dp, _IC> __first, __bit_iterator<_Dp, _IC> __last, __bit_iterator<_Dp, false> __result);
484 template <class _AlgPolicy>
485 friend struct __copy_backward_impl;
486 template <class, class _Dp>
487 _LIBCPP_CONSTEXPR_SINCE_CXX20 friend pair<__bit_iterator<_Dp, false>, __bit_iterator<_Dp, false> >
488 __rotate(__bit_iterator<_Dp, false>, __bit_iterator<_Dp, false>, __bit_iterator<_Dp, false>);
489 template <class _Dp, bool _IsConst1, bool _IsConst2>
490 _LIBCPP_CONSTEXPR_SINCE_CXX20 friend bool
491 __equal_aligned(__bit_iterator<_Dp, _IsConst1>, __bit_iterator<_Dp, _IsConst1>, __bit_iterator<_Dp, _IsConst2>);
492 template <class _Dp, bool _IsConst1, bool _IsConst2>
493 _LIBCPP_CONSTEXPR_SINCE_CXX20 friend bool
494 __equal_unaligned(__bit_iterator<_Dp, _IsConst1>, __bit_iterator<_Dp, _IsConst1>, __bit_iterator<_Dp, _IsConst2>);
495 template <class _Dp,
496 bool _IsConst1,
497 bool _IsConst2,
498 class _BinaryPredicate,
499 class _Proj1,
500 class _Proj2,
501 __enable_if_t<__is_identity<_Proj1>::value && __is_identity<_Proj2>::value &&
502 __desugars_to_v<__equal_tag, _BinaryPredicate, bool, bool>,
503 int> >
504 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 friend bool __equal_iter_impl(
505 __bit_iterator<_Dp, _IsConst1>,
506 __bit_iterator<_Dp, _IsConst1>,
507 __bit_iterator<_Dp, _IsConst2>,
508 _BinaryPredicate,
509 _Proj1&,
510 _Proj2&);
511 template <bool,
512 class _Dp,
513 bool _IsConst1,
514 bool _IsConst2,
515 class _Pred,
516 class _Proj1,
517 class _Proj2,
518 __enable_if_t<__desugars_to_v<__equal_tag, _Pred, bool, bool> && __is_identity<_Proj1>::value &&
519 __is_identity<_Proj2>::value,
520 int> >
521 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 friend bool __equal_impl(
522 __bit_iterator<_Dp, _IsConst1> __first1,
523 __bit_iterator<_Dp, _IsConst1> __last1,
524 __bit_iterator<_Dp, _IsConst2> __first2,
525 __bit_iterator<_Dp, _IsConst2>,
526 _Pred&,
527 _Proj1&,
528 _Proj2&);
529 template <bool _ToFind, class _Dp, bool _IC>
530 _LIBCPP_CONSTEXPR_SINCE_CXX20 friend __bit_iterator<_Dp, _IC>
531 __find_bool(__bit_iterator<_Dp, _IC>, typename __size_difference_type_traits<_Dp>::size_type);
532 template <bool _ToCount, class _Dp, bool _IC>
533 friend typename __bit_iterator<_Dp, _IC>::difference_type _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
534 __count_bool(__bit_iterator<_Dp, _IC>, typename __size_difference_type_traits<_Dp>::size_type);
535
536 template <class, class...>
537 friend struct __specialized_algorithm;
538};
539
540template <class _Cp>
541struct __specialized_algorithm<_Algorithm::__fill_n, __single_iterator<__bit_iterator<_Cp, false> > > {
542 static const bool __has_algorithm = true;
543
544 template <bool _FillVal>
545 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static void
546 __impl(__bit_iterator<_Cp, false> __first, typename __size_difference_type_traits<_Cp>::size_type __n) {
547 using _It = __bit_iterator<_Cp, false>;
548 using __storage_type = typename _It::__storage_type;
549
550 const int __bits_per_word = _It::__bits_per_word;
551 // do first partial word
552 if (__first.__ctz_ != 0) {
553 __storage_type __clz_f = static_cast<__storage_type>(__bits_per_word - __first.__ctz_);
554 __storage_type __dn = std::min(__clz_f, __n);
555 std::__fill_masked_range(std::__to_address(__first.__seg_), __clz_f - __dn, __first.__ctz_, _FillVal);
556 __n -= __dn;
557 ++__first.__seg_;
558 }
559 // do middle whole words
560 __storage_type __nw = __n / __bits_per_word;
561 std::__fill_n(std::__to_address(__first.__seg_), __nw, _FillVal ? static_cast<__storage_type>(-1) : 0);
562 __n -= __nw * __bits_per_word;
563 // do last partial word
564 if (__n > 0) {
565 __first.__seg_ += __nw;
566 std::__fill_masked_range(std::__to_address(__first.__seg_), __bits_per_word - __n, 0u, _FillVal);
567 }
568 }
569
570 template <class _Size, class _Tp>
571 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static __bit_iterator<_Cp, false>
572 operator()(__bit_iterator<_Cp, false> __first, _Size __n, const _Tp& __value) {
573 if (__n > 0) {
574 if (__value)
575 __impl<true>(__first, __n);
576 else
577 __impl<false>(__first, __n);
578 }
579 return __first + __n;
580 }
581};
582
583template <class _Cp, bool _IsConst>
584struct __specialized_algorithm<_Algorithm::__copy,
585 __iterator_pair<__bit_iterator<_Cp, _IsConst>, __bit_iterator<_Cp, _IsConst> >,
586 __single_iterator<__bit_iterator<_Cp, false> > > {
587 static const bool __has_algorithm = true;
588
589 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static __bit_iterator<_Cp, false>
590 __aligned_impl(__bit_iterator<_Cp, _IsConst> __first,
591 __bit_iterator<_Cp, _IsConst> __last,
592 __bit_iterator<_Cp, false> __result) {
593 using _In = __bit_iterator<_Cp, _IsConst>;
594 using difference_type = typename _In::difference_type;
595 using __storage_type = typename _In::__storage_type;
596
597 const int __bits_per_word = _In::__bits_per_word;
598 difference_type __n = __last - __first;
599 if (__n > 0) {
600 // do first word
601 if (__first.__ctz_ != 0) {
602 unsigned __clz = __bits_per_word - __first.__ctz_;
603 difference_type __dn = std::min(static_cast<difference_type>(__clz), __n);
604 __n -= __dn;
605 __storage_type __m = std::__middle_mask<__storage_type>(__clz - __dn, __first.__ctz_);
606 __storage_type __b = *__first.__seg_ & __m;
607 *__result.__seg_ &= ~__m;
608 *__result.__seg_ |= __b;
609 __result.__seg_ += (__dn + __result.__ctz_) / __bits_per_word;
610 __result.__ctz_ = static_cast<unsigned>((__dn + __result.__ctz_) % __bits_per_word);
611 ++__first.__seg_;
612 // __first.__ctz_ = 0;
613 }
614 // __first.__ctz_ == 0;
615 // do middle words
616 __storage_type __nw = __n / __bits_per_word;
617 std::copy(std::__to_address(__first.__seg_),
618 std::__to_address(__first.__seg_ + __nw),
619 std::__to_address(__result.__seg_));
620 __n -= __nw * __bits_per_word;
621 __result.__seg_ += __nw;
622 // do last word
623 if (__n > 0) {
624 __first.__seg_ += __nw;
625 __storage_type __m = std::__trailing_mask<__storage_type>(__bits_per_word - __n);
626 __storage_type __b = *__first.__seg_ & __m;
627 *__result.__seg_ &= ~__m;
628 *__result.__seg_ |= __b;
629 __result.__ctz_ = static_cast<unsigned>(__n);
630 }
631 }
632 return __result;
633 }
634
635 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static __bit_iterator<_Cp, false>
636 __unaligned_impl(__bit_iterator<_Cp, _IsConst> __first,
637 __bit_iterator<_Cp, _IsConst> __last,
638 __bit_iterator<_Cp, false> __result) {
639 using _In = __bit_iterator<_Cp, _IsConst>;
640 using difference_type = typename _In::difference_type;
641 using __storage_type = typename _In::__storage_type;
642
643 const int __bits_per_word = _In::__bits_per_word;
644 difference_type __n = __last - __first;
645 if (__n > 0) {
646 // do first word
647 if (__first.__ctz_ != 0) {
648 unsigned __clz_f = __bits_per_word - __first.__ctz_;
649 difference_type __dn = std::min(static_cast<difference_type>(__clz_f), __n);
650 __n -= __dn;
651 __storage_type __m = std::__middle_mask<__storage_type>(__clz_f - __dn, __first.__ctz_);
652 __storage_type __b = *__first.__seg_ & __m;
653 unsigned __clz_r = __bits_per_word - __result.__ctz_;
654 __storage_type __ddn = std::min<__storage_type>(__dn, __clz_r);
655 __m = std::__middle_mask<__storage_type>(__clz_r - __ddn, __result.__ctz_);
656 *__result.__seg_ &= ~__m;
657 if (__result.__ctz_ > __first.__ctz_)
658 *__result.__seg_ |= __b << (__result.__ctz_ - __first.__ctz_);
659 else
660 *__result.__seg_ |= __b >> (__first.__ctz_ - __result.__ctz_);
661 __result.__seg_ += (__ddn + __result.__ctz_) / __bits_per_word;
662 __result.__ctz_ = static_cast<unsigned>((__ddn + __result.__ctz_) % __bits_per_word);
663 __dn -= __ddn;
664 if (__dn > 0) {
665 __m = std::__trailing_mask<__storage_type>(__bits_per_word - __dn);
666 *__result.__seg_ &= ~__m;
667 *__result.__seg_ |= __b >> (__first.__ctz_ + __ddn);
668 __result.__ctz_ = static_cast<unsigned>(__dn);
669 }
670 ++__first.__seg_;
671 // __first.__ctz_ = 0;
672 }
673 // __first.__ctz_ == 0;
674 // do middle words
675 unsigned __clz_r = __bits_per_word - __result.__ctz_;
676 __storage_type __m = std::__leading_mask<__storage_type>(__result.__ctz_);
677 for (; __n >= __bits_per_word; __n -= __bits_per_word, ++__first.__seg_) {
678 __storage_type __b = *__first.__seg_;
679 *__result.__seg_ &= ~__m;
680 *__result.__seg_ |= __b << __result.__ctz_;
681 ++__result.__seg_;
682 *__result.__seg_ &= __m;
683 *__result.__seg_ |= __b >> __clz_r;
684 }
685 // do last word
686 if (__n > 0) {
687 __m = std::__trailing_mask<__storage_type>(__bits_per_word - __n);
688 __storage_type __b = *__first.__seg_ & __m;
689 __storage_type __dn = std::min(__n, static_cast<difference_type>(__clz_r));
690 __m = std::__middle_mask<__storage_type>(__clz_r - __dn, __result.__ctz_);
691 *__result.__seg_ &= ~__m;
692 *__result.__seg_ |= __b << __result.__ctz_;
693 __result.__seg_ += (__dn + __result.__ctz_) / __bits_per_word;
694 __result.__ctz_ = static_cast<unsigned>((__dn + __result.__ctz_) % __bits_per_word);
695 __n -= __dn;
696 if (__n > 0) {
697 __m = std::__trailing_mask<__storage_type>(__bits_per_word - __n);
698 *__result.__seg_ &= ~__m;
699 *__result.__seg_ |= __b >> __dn;
700 __result.__ctz_ = static_cast<unsigned>(__n);
701 }
702 }
703 }
704 return __result;
705 }
706
707 _LIBCPP_HIDE_FROM_ABI
708 _LIBCPP_CONSTEXPR_SINCE_CXX20 static pair<__bit_iterator<_Cp, _IsConst>, __bit_iterator<_Cp, false> >
709 operator()(__bit_iterator<_Cp, _IsConst> __first,
710 __bit_iterator<_Cp, _IsConst> __last,
711 __bit_iterator<_Cp, false> __result) {
712 if (__first.__ctz_ == __result.__ctz_)
713 return std::make_pair(__last, __aligned_impl(__first, __last, __result));
714 return std::make_pair(__last, __unaligned_impl(__first, __last, __result));
715 }
716};
717
718template <class _Cl, class _Cr>
719struct __specialized_algorithm<_Algorithm::__swap_ranges,
720 __iterator_pair<__bit_iterator<_Cl, false>, __bit_iterator<_Cl, false> >,
721 __single_iterator<__bit_iterator<_Cr, false> > > {
722 static const bool __has_algorithm = true;
723
724 _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator<_Cr, false> __aligned_impl(
725 __bit_iterator<_Cl, false> __first, __bit_iterator<_Cl, false> __last, __bit_iterator<_Cr, false> __result) {
726 using _I1 = __bit_iterator<_Cl, false>;
727 using difference_type = typename _I1::difference_type;
728 using __storage_type = typename _I1::__storage_type;
729
730 const int __bits_per_word = _I1::__bits_per_word;
731 difference_type __n = __last - __first;
732 if (__n > 0) {
733 // do first word
734 if (__first.__ctz_ != 0) {
735 unsigned __clz = __bits_per_word - __first.__ctz_;
736 difference_type __dn = std::min(static_cast<difference_type>(__clz), __n);
737 __n -= __dn;
738 __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz - __dn));
739 __storage_type __b1 = *__first.__seg_ & __m;
740 *__first.__seg_ &= ~__m;
741 __storage_type __b2 = *__result.__seg_ & __m;
742 *__result.__seg_ &= ~__m;
743 *__result.__seg_ |= __b1;
744 *__first.__seg_ |= __b2;
745 __result.__seg_ += (__dn + __result.__ctz_) / __bits_per_word;
746 __result.__ctz_ = static_cast<unsigned>((__dn + __result.__ctz_) % __bits_per_word);
747 ++__first.__seg_;
748 // __first.__ctz_ = 0;
749 }
750 // __first.__ctz_ == 0;
751 // do middle words
752 for (; __n >= __bits_per_word; __n -= __bits_per_word, ++__first.__seg_, ++__result.__seg_)
753 swap(*__first.__seg_, *__result.__seg_);
754 // do last word
755 if (__n > 0) {
756 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
757 __storage_type __b1 = *__first.__seg_ & __m;
758 *__first.__seg_ &= ~__m;
759 __storage_type __b2 = *__result.__seg_ & __m;
760 *__result.__seg_ &= ~__m;
761 *__result.__seg_ |= __b1;
762 *__first.__seg_ |= __b2;
763 __result.__ctz_ = static_cast<unsigned>(__n);
764 }
765 }
766 return __result;
767 }
768
769 _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator<_Cr, false> __unaligned_impl(
770 __bit_iterator<_Cl, false> __first, __bit_iterator<_Cl, false> __last, __bit_iterator<_Cr, false> __result) {
771 using _I1 = __bit_iterator<_Cl, false>;
772 using difference_type = typename _I1::difference_type;
773 using __storage_type = typename _I1::__storage_type;
774
775 const int __bits_per_word = _I1::__bits_per_word;
776 difference_type __n = __last - __first;
777 if (__n > 0) {
778 // do first word
779 if (__first.__ctz_ != 0) {
780 unsigned __clz_f = __bits_per_word - __first.__ctz_;
781 difference_type __dn = std::min(static_cast<difference_type>(__clz_f), __n);
782 __n -= __dn;
783 __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn));
784 __storage_type __b1 = *__first.__seg_ & __m;
785 *__first.__seg_ &= ~__m;
786 unsigned __clz_r = __bits_per_word - __result.__ctz_;
787 __storage_type __ddn = std::min<__storage_type>(__dn, __clz_r);
788 __m = (~__storage_type(0) << __result.__ctz_) & (~__storage_type(0) >> (__clz_r - __ddn));
789 __storage_type __b2 = *__result.__seg_ & __m;
790 *__result.__seg_ &= ~__m;
791 if (__result.__ctz_ > __first.__ctz_) {
792 unsigned __s = __result.__ctz_ - __first.__ctz_;
793 *__result.__seg_ |= __b1 << __s;
794 *__first.__seg_ |= __b2 >> __s;
795 } else {
796 unsigned __s = __first.__ctz_ - __result.__ctz_;
797 *__result.__seg_ |= __b1 >> __s;
798 *__first.__seg_ |= __b2 << __s;
799 }
800 __result.__seg_ += (__ddn + __result.__ctz_) / __bits_per_word;
801 __result.__ctz_ = static_cast<unsigned>((__ddn + __result.__ctz_) % __bits_per_word);
802 __dn -= __ddn;
803 if (__dn > 0) {
804 __m = ~__storage_type(0) >> (__bits_per_word - __dn);
805 __b2 = *__result.__seg_ & __m;
806 *__result.__seg_ &= ~__m;
807 unsigned __s = __first.__ctz_ + __ddn;
808 *__result.__seg_ |= __b1 >> __s;
809 *__first.__seg_ |= __b2 << __s;
810 __result.__ctz_ = static_cast<unsigned>(__dn);
811 }
812 ++__first.__seg_;
813 // __first.__ctz_ = 0;
814 }
815 // __first.__ctz_ == 0;
816 // do middle words
817 __storage_type __m = ~__storage_type(0) << __result.__ctz_;
818 unsigned __clz_r = __bits_per_word - __result.__ctz_;
819 for (; __n >= __bits_per_word; __n -= __bits_per_word, ++__first.__seg_) {
820 __storage_type __b1 = *__first.__seg_;
821 __storage_type __b2 = *__result.__seg_ & __m;
822 *__result.__seg_ &= ~__m;
823 *__result.__seg_ |= __b1 << __result.__ctz_;
824 *__first.__seg_ = __b2 >> __result.__ctz_;
825 ++__result.__seg_;
826 __b2 = *__result.__seg_ & ~__m;
827 *__result.__seg_ &= __m;
828 *__result.__seg_ |= __b1 >> __clz_r;
829 *__first.__seg_ |= __b2 << __clz_r;
830 }
831 // do last word
832 if (__n > 0) {
833 __m = ~__storage_type(0) >> (__bits_per_word - __n);
834 __storage_type __b1 = *__first.__seg_ & __m;
835 *__first.__seg_ &= ~__m;
836 __storage_type __dn = std::min<__storage_type>(__n, __clz_r);
837 __m = (~__storage_type(0) << __result.__ctz_) & (~__storage_type(0) >> (__clz_r - __dn));
838 __storage_type __b2 = *__result.__seg_ & __m;
839 *__result.__seg_ &= ~__m;
840 *__result.__seg_ |= __b1 << __result.__ctz_;
841 *__first.__seg_ |= __b2 >> __result.__ctz_;
842 __result.__seg_ += (__dn + __result.__ctz_) / __bits_per_word;
843 __result.__ctz_ = static_cast<unsigned>((__dn + __result.__ctz_) % __bits_per_word);
844 __n -= __dn;
845 if (__n > 0) {
846 __m = ~__storage_type(0) >> (__bits_per_word - __n);
847 __b2 = *__result.__seg_ & __m;
848 *__result.__seg_ &= ~__m;
849 *__result.__seg_ |= __b1 >> __dn;
850 *__first.__seg_ |= __b2 << __dn;
851 __result.__ctz_ = static_cast<unsigned>(__n);
852 }
853 }
854 }
855 return __result;
856 }
857
858 _LIBCPP_HIDE_FROM_ABI static
859 _LIBCPP_CONSTEXPR_SINCE_CXX20 pair<__bit_iterator<_Cl, false>, __bit_iterator<_Cr, false> >
860 operator()(__bit_iterator<_Cl, false> __first1,
861 __bit_iterator<_Cl, false> __last1,
862 __bit_iterator<_Cr, false> __first2) {
863 if (__first1.__ctz_ == __first2.__ctz_)
864 return std::make_pair(__last1, __aligned_impl(first: __first1, last: __last1, result: __first2));
865 return std::make_pair(__last1, __unaligned_impl(first: __first1, last: __last1, result: __first2));
866 }
867};
868
869_LIBCPP_END_NAMESPACE_STD
870
871_LIBCPP_POP_MACROS
872
873#endif // _LIBCPP___BIT_REFERENCE
874