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/in_out_result.h>
20#include <__algorithm/min.h>
21#include <__algorithm/rotate.h>
22#include <__algorithm/specialized_algorithms.h>
23#include <__assert>
24#include <__bit/countr.h>
25#include <__compare/ordering.h>
26#include <__config>
27#include <__cstddef/ptrdiff_t.h>
28#include <__cstddef/size_t.h>
29#include <__functional/identity.h>
30#include <__fwd/bit_reference.h>
31#include <__iterator/iterator_traits.h>
32#include <__memory/construct_at.h>
33#include <__memory/pointer_traits.h>
34#include <__type_traits/conditional.h>
35#include <__type_traits/desugars_to.h>
36#include <__type_traits/enable_if.h>
37#include <__type_traits/is_constant_evaluated.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 [[__nodiscard__]] _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 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator
393 operator+(difference_type __n) const {
394 __bit_iterator __t(*this);
395 __t += __n;
396 return __t;
397 }
398
399 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator
400 operator-(difference_type __n) const {
401 __bit_iterator __t(*this);
402 __t -= __n;
403 return __t;
404 }
405
406 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 friend __bit_iterator
407 operator+(difference_type __n, const __bit_iterator& __it) {
408 return __it + __n;
409 }
410
411 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 friend difference_type
412 operator-(const __bit_iterator& __x, const __bit_iterator& __y) {
413 return (__x.__seg_ - __y.__seg_) * __bits_per_word + __x.__ctz_ - __y.__ctz_;
414 }
415
416 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference
417 operator[](difference_type __n) const {
418 return *(*this + __n);
419 }
420
421 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 friend bool
422 operator==(const __bit_iterator& __x, const __bit_iterator& __y) {
423 return __x.__seg_ == __y.__seg_ && __x.__ctz_ == __y.__ctz_;
424 }
425
426#if _LIBCPP_STD_VER <= 17
427 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 friend bool
428 operator!=(const __bit_iterator& __x, const __bit_iterator& __y) {
429 return !(__x == __y);
430 }
431
432 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 friend bool
433 operator<(const __bit_iterator& __x, const __bit_iterator& __y) {
434 return __x.__seg_ < __y.__seg_ || (__x.__seg_ == __y.__seg_ && __x.__ctz_ < __y.__ctz_);
435 }
436
437 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 friend bool
438 operator>(const __bit_iterator& __x, const __bit_iterator& __y) {
439 return __y < __x;
440 }
441
442 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 friend bool
443 operator<=(const __bit_iterator& __x, const __bit_iterator& __y) {
444 return !(__y < __x);
445 }
446
447 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 friend bool
448 operator>=(const __bit_iterator& __x, const __bit_iterator& __y) {
449 return !(__x < __y);
450 }
451#else // _LIBCPP_STD_VER <= 17
452 _LIBCPP_HIDE_FROM_ABI constexpr friend strong_ordering
453 operator<=>(const __bit_iterator& __x, const __bit_iterator& __y) {
454 if (__x.__seg_ < __y.__seg_)
455 return strong_ordering::less;
456
457 if (__x.__seg_ == __y.__seg_)
458 return __x.__ctz_ <=> __y.__ctz_;
459
460 return strong_ordering::greater;
461 }
462#endif // _LIBCPP_STD_VER <= 17
463
464private:
465 _LIBCPP_HIDE_FROM_ABI
466 _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit __bit_iterator(__storage_pointer __s, unsigned __ctz) _NOEXCEPT
467 : __seg_(__s),
468 __ctz_(__ctz) {
469 _LIBCPP_ASSERT_INTERNAL(
470 __ctz_ < __bits_per_word, "__bit_iterator initialized with an invalid number of trailing zeros.");
471 }
472
473 friend typename _Cp::__self;
474
475 friend class __bit_reference<_Cp>;
476 friend class __bit_const_reference<_Cp>;
477 friend class __bit_iterator<_Cp, true>;
478 template <class _Dp>
479 friend struct __bit_array;
480
481 template <class _Dp, bool _IC>
482 _LIBCPP_CONSTEXPR_SINCE_CXX20 friend __bit_iterator<_Dp, false> __copy_backward_aligned(
483 __bit_iterator<_Dp, _IC> __first, __bit_iterator<_Dp, _IC> __last, __bit_iterator<_Dp, false> __result);
484 template <class _Dp, bool _IC>
485 _LIBCPP_CONSTEXPR_SINCE_CXX20 friend __bit_iterator<_Dp, false> __copy_backward_unaligned(
486 __bit_iterator<_Dp, _IC> __first, __bit_iterator<_Dp, _IC> __last, __bit_iterator<_Dp, false> __result);
487 template <class _AlgPolicy>
488 friend struct __copy_backward_impl;
489 template <class, class _Dp>
490 _LIBCPP_CONSTEXPR_SINCE_CXX20 friend pair<__bit_iterator<_Dp, false>, __bit_iterator<_Dp, false> >
491 __rotate(__bit_iterator<_Dp, false>, __bit_iterator<_Dp, false>, __bit_iterator<_Dp, false>);
492 template <class _Dp, bool _IsConst1, bool _IsConst2>
493 _LIBCPP_CONSTEXPR_SINCE_CXX20 friend bool
494 __equal_aligned(__bit_iterator<_Dp, _IsConst1>, __bit_iterator<_Dp, _IsConst1>, __bit_iterator<_Dp, _IsConst2>);
495 template <class _Dp, bool _IsConst1, bool _IsConst2>
496 _LIBCPP_CONSTEXPR_SINCE_CXX20 friend bool
497 __equal_unaligned(__bit_iterator<_Dp, _IsConst1>, __bit_iterator<_Dp, _IsConst1>, __bit_iterator<_Dp, _IsConst2>);
498 template <class _Dp,
499 bool _IsConst1,
500 bool _IsConst2,
501 class _BinaryPredicate,
502 class _Proj1,
503 class _Proj2,
504 __enable_if_t<__is_identity<_Proj1>::value && __is_identity<_Proj2>::value &&
505 __desugars_to_v<__equal_tag, _BinaryPredicate, bool, bool>,
506 int> >
507 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 friend bool __equal_iter_impl(
508 __bit_iterator<_Dp, _IsConst1>,
509 __bit_iterator<_Dp, _IsConst1>,
510 __bit_iterator<_Dp, _IsConst2>,
511 _BinaryPredicate,
512 _Proj1&,
513 _Proj2&);
514 template <bool,
515 class _Dp,
516 bool _IsConst1,
517 bool _IsConst2,
518 class _Pred,
519 class _Proj1,
520 class _Proj2,
521 __enable_if_t<__desugars_to_v<__equal_tag, _Pred, bool, bool> && __is_identity<_Proj1>::value &&
522 __is_identity<_Proj2>::value,
523 int> >
524 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 friend bool __equal_impl(
525 __bit_iterator<_Dp, _IsConst1> __first1,
526 __bit_iterator<_Dp, _IsConst1> __last1,
527 __bit_iterator<_Dp, _IsConst2> __first2,
528 __bit_iterator<_Dp, _IsConst2>,
529 _Pred&,
530 _Proj1&,
531 _Proj2&);
532 template <bool _ToFind, class _Dp, bool _IC>
533 _LIBCPP_CONSTEXPR_SINCE_CXX20 friend __bit_iterator<_Dp, _IC>
534 __find_bool(__bit_iterator<_Dp, _IC>, typename __size_difference_type_traits<_Dp>::size_type);
535 template <bool _ToCount, class _Dp, bool _IC>
536 friend typename __bit_iterator<_Dp, _IC>::difference_type _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
537 __count_bool(__bit_iterator<_Dp, _IC>, typename __size_difference_type_traits<_Dp>::size_type);
538
539 template <class, class...>
540 friend struct __specialized_algorithm;
541};
542
543template <class _Cp>
544struct __specialized_algorithm<_Algorithm::__fill_n, __single_iterator<__bit_iterator<_Cp, false> > > {
545 static const bool __has_algorithm = true;
546
547 template <bool _FillVal>
548 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static void
549 __impl(__bit_iterator<_Cp, false> __first, typename __size_difference_type_traits<_Cp>::size_type __n) {
550 using _It = __bit_iterator<_Cp, false>;
551 using __storage_type = typename _It::__storage_type;
552
553 const int __bits_per_word = _It::__bits_per_word;
554 // do first partial word
555 if (__first.__ctz_ != 0) {
556 __storage_type __clz_f = static_cast<__storage_type>(__bits_per_word - __first.__ctz_);
557 __storage_type __dn = std::min(__clz_f, __n);
558 std::__fill_masked_range(std::__to_address(__first.__seg_), __clz_f - __dn, __first.__ctz_, _FillVal);
559 __n -= __dn;
560 ++__first.__seg_;
561 }
562 // do middle whole words
563 __storage_type __nw = __n / __bits_per_word;
564 std::__fill_n(std::__to_address(__first.__seg_), __nw, _FillVal ? static_cast<__storage_type>(-1) : 0);
565 __n -= __nw * __bits_per_word;
566 // do last partial word
567 if (__n > 0) {
568 __first.__seg_ += __nw;
569 std::__fill_masked_range(std::__to_address(__first.__seg_), __bits_per_word - __n, 0u, _FillVal);
570 }
571 }
572
573 template <class _Size, class _Tp>
574 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static __bit_iterator<_Cp, false>
575 operator()(__bit_iterator<_Cp, false> __first, _Size __n, const _Tp& __value) {
576 if (__n > 0) {
577 if (__value)
578 __impl<true>(__first, __n);
579 else
580 __impl<false>(__first, __n);
581 }
582 return __first + __n;
583 }
584};
585
586template <class _Cp, bool _IsConst>
587struct __specialized_algorithm<_Algorithm::__copy,
588 __iterator_pair<__bit_iterator<_Cp, _IsConst>, __bit_iterator<_Cp, _IsConst> >,
589 __single_iterator<__bit_iterator<_Cp, false> > > {
590 static const bool __has_algorithm = true;
591
592 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static __bit_iterator<_Cp, false>
593 __aligned_impl(__bit_iterator<_Cp, _IsConst> __first,
594 __bit_iterator<_Cp, _IsConst> __last,
595 __bit_iterator<_Cp, false> __result) {
596 using _In = __bit_iterator<_Cp, _IsConst>;
597 using difference_type = typename _In::difference_type;
598 using __storage_type = typename _In::__storage_type;
599
600 const int __bits_per_word = _In::__bits_per_word;
601 difference_type __n = __last - __first;
602 if (__n > 0) {
603 // do first word
604 if (__first.__ctz_ != 0) {
605 unsigned __clz = __bits_per_word - __first.__ctz_;
606 difference_type __dn = std::min(static_cast<difference_type>(__clz), __n);
607 __n -= __dn;
608 __storage_type __m = std::__middle_mask<__storage_type>(__clz - __dn, __first.__ctz_);
609 __storage_type __b = *__first.__seg_ & __m;
610 *__result.__seg_ &= ~__m;
611 *__result.__seg_ |= __b;
612 __result.__seg_ += (__dn + __result.__ctz_) / __bits_per_word;
613 __result.__ctz_ = static_cast<unsigned>((__dn + __result.__ctz_) % __bits_per_word);
614 ++__first.__seg_;
615 // __first.__ctz_ = 0;
616 }
617 // __first.__ctz_ == 0;
618 // do middle words
619 __storage_type __nw = __n / __bits_per_word;
620 std::copy(std::__to_address(__first.__seg_),
621 std::__to_address(__first.__seg_ + __nw),
622 std::__to_address(__result.__seg_));
623 __n -= __nw * __bits_per_word;
624 __result.__seg_ += __nw;
625 // do last word
626 if (__n > 0) {
627 __first.__seg_ += __nw;
628 __storage_type __m = std::__trailing_mask<__storage_type>(__bits_per_word - __n);
629 __storage_type __b = *__first.__seg_ & __m;
630 *__result.__seg_ &= ~__m;
631 *__result.__seg_ |= __b;
632 __result.__ctz_ = static_cast<unsigned>(__n);
633 }
634 }
635 return __result;
636 }
637
638 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static __bit_iterator<_Cp, false>
639 __unaligned_impl(__bit_iterator<_Cp, _IsConst> __first,
640 __bit_iterator<_Cp, _IsConst> __last,
641 __bit_iterator<_Cp, false> __result) {
642 using _In = __bit_iterator<_Cp, _IsConst>;
643 using difference_type = typename _In::difference_type;
644 using __storage_type = typename _In::__storage_type;
645
646 const int __bits_per_word = _In::__bits_per_word;
647 difference_type __n = __last - __first;
648 if (__n > 0) {
649 // do first word
650 if (__first.__ctz_ != 0) {
651 unsigned __clz_f = __bits_per_word - __first.__ctz_;
652 difference_type __dn = std::min(static_cast<difference_type>(__clz_f), __n);
653 __n -= __dn;
654 __storage_type __m = std::__middle_mask<__storage_type>(__clz_f - __dn, __first.__ctz_);
655 __storage_type __b = *__first.__seg_ & __m;
656 unsigned __clz_r = __bits_per_word - __result.__ctz_;
657 __storage_type __ddn = std::min<__storage_type>(__dn, __clz_r);
658 __m = std::__middle_mask<__storage_type>(__clz_r - __ddn, __result.__ctz_);
659 *__result.__seg_ &= ~__m;
660 if (__result.__ctz_ > __first.__ctz_)
661 *__result.__seg_ |= __b << (__result.__ctz_ - __first.__ctz_);
662 else
663 *__result.__seg_ |= __b >> (__first.__ctz_ - __result.__ctz_);
664 __result.__seg_ += (__ddn + __result.__ctz_) / __bits_per_word;
665 __result.__ctz_ = static_cast<unsigned>((__ddn + __result.__ctz_) % __bits_per_word);
666 __dn -= __ddn;
667 if (__dn > 0) {
668 __m = std::__trailing_mask<__storage_type>(__bits_per_word - __dn);
669 *__result.__seg_ &= ~__m;
670 *__result.__seg_ |= __b >> (__first.__ctz_ + __ddn);
671 __result.__ctz_ = static_cast<unsigned>(__dn);
672 }
673 ++__first.__seg_;
674 // __first.__ctz_ = 0;
675 }
676 // __first.__ctz_ == 0;
677 // do middle words
678 unsigned __clz_r = __bits_per_word - __result.__ctz_;
679 __storage_type __m = std::__leading_mask<__storage_type>(__result.__ctz_);
680 for (; __n >= __bits_per_word; __n -= __bits_per_word, ++__first.__seg_) {
681 __storage_type __b = *__first.__seg_;
682 *__result.__seg_ &= ~__m;
683 *__result.__seg_ |= __b << __result.__ctz_;
684 ++__result.__seg_;
685 *__result.__seg_ &= __m;
686 *__result.__seg_ |= __b >> __clz_r;
687 }
688 // do last word
689 if (__n > 0) {
690 __m = std::__trailing_mask<__storage_type>(__bits_per_word - __n);
691 __storage_type __b = *__first.__seg_ & __m;
692 __storage_type __dn = std::min(__n, static_cast<difference_type>(__clz_r));
693 __m = std::__middle_mask<__storage_type>(__clz_r - __dn, __result.__ctz_);
694 *__result.__seg_ &= ~__m;
695 *__result.__seg_ |= __b << __result.__ctz_;
696 __result.__seg_ += (__dn + __result.__ctz_) / __bits_per_word;
697 __result.__ctz_ = static_cast<unsigned>((__dn + __result.__ctz_) % __bits_per_word);
698 __n -= __dn;
699 if (__n > 0) {
700 __m = std::__trailing_mask<__storage_type>(__bits_per_word - __n);
701 *__result.__seg_ &= ~__m;
702 *__result.__seg_ |= __b >> __dn;
703 __result.__ctz_ = static_cast<unsigned>(__n);
704 }
705 }
706 }
707 return __result;
708 }
709
710 _LIBCPP_HIDE_FROM_ABI
711 _LIBCPP_CONSTEXPR_SINCE_CXX20 static __in_out_result<__bit_iterator<_Cp, _IsConst>, __bit_iterator<_Cp, false> >
712 operator()(__bit_iterator<_Cp, _IsConst> __first,
713 __bit_iterator<_Cp, _IsConst> __last,
714 __bit_iterator<_Cp, false> __result) {
715 if (__first.__ctz_ == __result.__ctz_)
716 return {__last, __aligned_impl(__first, __last, __result)};
717 return {__last, __unaligned_impl(__first, __last, __result)};
718 }
719};
720
721template <class _Cl, class _Cr>
722struct __specialized_algorithm<_Algorithm::__swap_ranges,
723 __iterator_pair<__bit_iterator<_Cl, false>, __bit_iterator<_Cl, false> >,
724 __single_iterator<__bit_iterator<_Cr, false> > > {
725 static const bool __has_algorithm = true;
726
727 _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator<_Cr, false> __aligned_impl(
728 __bit_iterator<_Cl, false> __first, __bit_iterator<_Cl, false> __last, __bit_iterator<_Cr, false> __result) {
729 using _I1 = __bit_iterator<_Cl, false>;
730 using difference_type = typename _I1::difference_type;
731 using __storage_type = typename _I1::__storage_type;
732
733 const int __bits_per_word = _I1::__bits_per_word;
734 difference_type __n = __last - __first;
735 if (__n > 0) {
736 // do first word
737 if (__first.__ctz_ != 0) {
738 unsigned __clz = __bits_per_word - __first.__ctz_;
739 difference_type __dn = std::min(static_cast<difference_type>(__clz), __n);
740 __n -= __dn;
741 __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz - __dn));
742 __storage_type __b1 = *__first.__seg_ & __m;
743 *__first.__seg_ &= ~__m;
744 __storage_type __b2 = *__result.__seg_ & __m;
745 *__result.__seg_ &= ~__m;
746 *__result.__seg_ |= __b1;
747 *__first.__seg_ |= __b2;
748 __result.__seg_ += (__dn + __result.__ctz_) / __bits_per_word;
749 __result.__ctz_ = static_cast<unsigned>((__dn + __result.__ctz_) % __bits_per_word);
750 ++__first.__seg_;
751 // __first.__ctz_ = 0;
752 }
753 // __first.__ctz_ == 0;
754 // do middle words
755 for (; __n >= __bits_per_word; __n -= __bits_per_word, ++__first.__seg_, ++__result.__seg_)
756 swap(*__first.__seg_, *__result.__seg_);
757 // do last word
758 if (__n > 0) {
759 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
760 __storage_type __b1 = *__first.__seg_ & __m;
761 *__first.__seg_ &= ~__m;
762 __storage_type __b2 = *__result.__seg_ & __m;
763 *__result.__seg_ &= ~__m;
764 *__result.__seg_ |= __b1;
765 *__first.__seg_ |= __b2;
766 __result.__ctz_ = static_cast<unsigned>(__n);
767 }
768 }
769 return __result;
770 }
771
772 _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator<_Cr, false> __unaligned_impl(
773 __bit_iterator<_Cl, false> __first, __bit_iterator<_Cl, false> __last, __bit_iterator<_Cr, false> __result) {
774 using _I1 = __bit_iterator<_Cl, false>;
775 using difference_type = typename _I1::difference_type;
776 using __storage_type = typename _I1::__storage_type;
777
778 const int __bits_per_word = _I1::__bits_per_word;
779 difference_type __n = __last - __first;
780 if (__n > 0) {
781 // do first word
782 if (__first.__ctz_ != 0) {
783 unsigned __clz_f = __bits_per_word - __first.__ctz_;
784 difference_type __dn = std::min(static_cast<difference_type>(__clz_f), __n);
785 __n -= __dn;
786 __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn));
787 __storage_type __b1 = *__first.__seg_ & __m;
788 *__first.__seg_ &= ~__m;
789 unsigned __clz_r = __bits_per_word - __result.__ctz_;
790 __storage_type __ddn = std::min<__storage_type>(__dn, __clz_r);
791 __m = (~__storage_type(0) << __result.__ctz_) & (~__storage_type(0) >> (__clz_r - __ddn));
792 __storage_type __b2 = *__result.__seg_ & __m;
793 *__result.__seg_ &= ~__m;
794 if (__result.__ctz_ > __first.__ctz_) {
795 unsigned __s = __result.__ctz_ - __first.__ctz_;
796 *__result.__seg_ |= __b1 << __s;
797 *__first.__seg_ |= __b2 >> __s;
798 } else {
799 unsigned __s = __first.__ctz_ - __result.__ctz_;
800 *__result.__seg_ |= __b1 >> __s;
801 *__first.__seg_ |= __b2 << __s;
802 }
803 __result.__seg_ += (__ddn + __result.__ctz_) / __bits_per_word;
804 __result.__ctz_ = static_cast<unsigned>((__ddn + __result.__ctz_) % __bits_per_word);
805 __dn -= __ddn;
806 if (__dn > 0) {
807 __m = ~__storage_type(0) >> (__bits_per_word - __dn);
808 __b2 = *__result.__seg_ & __m;
809 *__result.__seg_ &= ~__m;
810 unsigned __s = __first.__ctz_ + __ddn;
811 *__result.__seg_ |= __b1 >> __s;
812 *__first.__seg_ |= __b2 << __s;
813 __result.__ctz_ = static_cast<unsigned>(__dn);
814 }
815 ++__first.__seg_;
816 // __first.__ctz_ = 0;
817 }
818 // __first.__ctz_ == 0;
819 // do middle words
820 __storage_type __m = ~__storage_type(0) << __result.__ctz_;
821 unsigned __clz_r = __bits_per_word - __result.__ctz_;
822 for (; __n >= __bits_per_word; __n -= __bits_per_word, ++__first.__seg_) {
823 __storage_type __b1 = *__first.__seg_;
824 __storage_type __b2 = *__result.__seg_ & __m;
825 *__result.__seg_ &= ~__m;
826 *__result.__seg_ |= __b1 << __result.__ctz_;
827 *__first.__seg_ = __b2 >> __result.__ctz_;
828 ++__result.__seg_;
829 __b2 = *__result.__seg_ & ~__m;
830 *__result.__seg_ &= __m;
831 *__result.__seg_ |= __b1 >> __clz_r;
832 *__first.__seg_ |= __b2 << __clz_r;
833 }
834 // do last word
835 if (__n > 0) {
836 __m = ~__storage_type(0) >> (__bits_per_word - __n);
837 __storage_type __b1 = *__first.__seg_ & __m;
838 *__first.__seg_ &= ~__m;
839 __storage_type __dn = std::min<__storage_type>(__n, __clz_r);
840 __m = (~__storage_type(0) << __result.__ctz_) & (~__storage_type(0) >> (__clz_r - __dn));
841 __storage_type __b2 = *__result.__seg_ & __m;
842 *__result.__seg_ &= ~__m;
843 *__result.__seg_ |= __b1 << __result.__ctz_;
844 *__first.__seg_ |= __b2 >> __result.__ctz_;
845 __result.__seg_ += (__dn + __result.__ctz_) / __bits_per_word;
846 __result.__ctz_ = static_cast<unsigned>((__dn + __result.__ctz_) % __bits_per_word);
847 __n -= __dn;
848 if (__n > 0) {
849 __m = ~__storage_type(0) >> (__bits_per_word - __n);
850 __b2 = *__result.__seg_ & __m;
851 *__result.__seg_ &= ~__m;
852 *__result.__seg_ |= __b1 >> __dn;
853 *__first.__seg_ |= __b2 << __dn;
854 __result.__ctz_ = static_cast<unsigned>(__n);
855 }
856 }
857 }
858 return __result;
859 }
860
861 _LIBCPP_HIDE_FROM_ABI static
862 _LIBCPP_CONSTEXPR_SINCE_CXX20 pair<__bit_iterator<_Cl, false>, __bit_iterator<_Cr, false> >
863 operator()(__bit_iterator<_Cl, false> __first1,
864 __bit_iterator<_Cl, false> __last1,
865 __bit_iterator<_Cr, false> __first2) {
866 if (__first1.__ctz_ == __first2.__ctz_)
867 return std::make_pair(__last1, __aligned_impl(first: __first1, last: __last1, result: __first2));
868 return std::make_pair(__last1, __unaligned_impl(first: __first1, last: __last1, result: __first2));
869 }
870};
871
872_LIBCPP_END_NAMESPACE_STD
873
874_LIBCPP_POP_MACROS
875
876#endif // _LIBCPP___BIT_REFERENCE
877