| 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 | |
| 53 | template <class _Cp> |
| 54 | class __bit_const_reference; |
| 55 | |
| 56 | template <class _Tp> |
| 57 | struct __has_storage_type { |
| 58 | static const bool value = false; |
| 59 | }; |
| 60 | |
| 61 | template <class, class> |
| 62 | struct __size_difference_type_traits { |
| 63 | using difference_type = ptrdiff_t; |
| 64 | using size_type = size_t; |
| 65 | }; |
| 66 | |
| 67 | template <class _Cp> |
| 68 | struct __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. |
| 80 | template <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. |
| 88 | template <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. |
| 96 | template <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. |
| 105 | template <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 | |
| 120 | template <class _Cp, bool = __has_storage_type<_Cp>::value> |
| 121 | class __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 | |
| 133 | public: |
| 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 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator<_Cp, false> operator&() const _NOEXCEPT { |
| 169 | return __bit_iterator<_Cp, false>(__seg_, static_cast<unsigned>(std::__countr_zero(__mask_))); |
| 170 | } |
| 171 | |
| 172 | private: |
| 173 | _LIBCPP_HIDE_FROM_ABI |
| 174 | _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit __bit_reference(__storage_pointer __s, __storage_type __m) _NOEXCEPT |
| 175 | : __seg_(__s), |
| 176 | __mask_(__m) {} |
| 177 | }; |
| 178 | |
| 179 | template <class _Cp> |
| 180 | class __bit_reference<_Cp, false> {}; |
| 181 | |
| 182 | template <class _Cp> |
| 183 | inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void |
| 184 | swap(__bit_reference<_Cp> __x, __bit_reference<_Cp> __y) _NOEXCEPT { |
| 185 | bool __t = __x; |
| 186 | __x = __y; |
| 187 | __y = __t; |
| 188 | } |
| 189 | |
| 190 | template <class _Cp, class _Dp> |
| 191 | inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void |
| 192 | swap(__bit_reference<_Cp> __x, __bit_reference<_Dp> __y) _NOEXCEPT { |
| 193 | bool __t = __x; |
| 194 | __x = __y; |
| 195 | __y = __t; |
| 196 | } |
| 197 | |
| 198 | template <class _Cp> |
| 199 | inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void swap(__bit_reference<_Cp> __x, bool& __y) _NOEXCEPT { |
| 200 | bool __t = __x; |
| 201 | __x = __y; |
| 202 | __y = __t; |
| 203 | } |
| 204 | |
| 205 | template <class _Cp> |
| 206 | inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void swap(bool& __x, __bit_reference<_Cp> __y) _NOEXCEPT { |
| 207 | bool __t = __x; |
| 208 | __x = __y; |
| 209 | __y = __t; |
| 210 | } |
| 211 | |
| 212 | template <class _Cp> |
| 213 | class __bit_const_reference { |
| 214 | using __storage_type _LIBCPP_NODEBUG = typename _Cp::__storage_type; |
| 215 | using __storage_pointer _LIBCPP_NODEBUG = typename _Cp::__const_storage_pointer; |
| 216 | |
| 217 | __storage_pointer __seg_; |
| 218 | __storage_type __mask_; |
| 219 | |
| 220 | friend typename _Cp::__self; |
| 221 | friend class __bit_iterator<_Cp, true>; |
| 222 | |
| 223 | public: |
| 224 | using __container _LIBCPP_NODEBUG = typename _Cp::__self; |
| 225 | |
| 226 | _LIBCPP_HIDE_FROM_ABI __bit_const_reference(const __bit_const_reference&) = default; |
| 227 | __bit_const_reference& operator=(const __bit_const_reference&) = delete; |
| 228 | |
| 229 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_const_reference(const __bit_reference<_Cp>& __x) _NOEXCEPT |
| 230 | : __seg_(__x.__seg_), |
| 231 | __mask_(__x.__mask_) {} |
| 232 | |
| 233 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR operator bool() const _NOEXCEPT { |
| 234 | return static_cast<bool>(*__seg_ & __mask_); |
| 235 | } |
| 236 | |
| 237 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator<_Cp, true> operator&() const _NOEXCEPT { |
| 238 | return __bit_iterator<_Cp, true>(__seg_, static_cast<unsigned>(std::__countr_zero(__mask_))); |
| 239 | } |
| 240 | |
| 241 | private: |
| 242 | _LIBCPP_HIDE_FROM_ABI |
| 243 | _LIBCPP_CONSTEXPR explicit __bit_const_reference(__storage_pointer __s, __storage_type __m) _NOEXCEPT |
| 244 | : __seg_(__s), |
| 245 | __mask_(__m) {} |
| 246 | }; |
| 247 | |
| 248 | template <class _Cp> |
| 249 | struct __bit_array { |
| 250 | using difference_type _LIBCPP_NODEBUG = typename __size_difference_type_traits<_Cp>::difference_type; |
| 251 | using __storage_type _LIBCPP_NODEBUG = typename _Cp::__storage_type; |
| 252 | using __storage_pointer _LIBCPP_NODEBUG = typename _Cp::__storage_pointer; |
| 253 | using iterator _LIBCPP_NODEBUG = typename _Cp::iterator; |
| 254 | |
| 255 | static const unsigned __bits_per_word = _Cp::__bits_per_word; |
| 256 | static const unsigned _Np = 4; |
| 257 | |
| 258 | difference_type __size_; |
| 259 | __storage_type __word_[_Np]; |
| 260 | |
| 261 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static difference_type capacity() { |
| 262 | return static_cast<difference_type>(_Np * __bits_per_word); |
| 263 | } |
| 264 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit __bit_array(difference_type __s) : __size_(__s) { |
| 265 | if (__libcpp_is_constant_evaluated()) { |
| 266 | for (size_t __i = 0; __i != __bit_array<_Cp>::_Np; ++__i) |
| 267 | std::__construct_at(__word_ + __i, 0); |
| 268 | } |
| 269 | } |
| 270 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator begin() { |
| 271 | return iterator(pointer_traits<__storage_pointer>::pointer_to(__word_[0]), 0); |
| 272 | } |
| 273 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator end() { |
| 274 | return iterator(pointer_traits<__storage_pointer>::pointer_to(__word_[0]) + __size_ / __bits_per_word, |
| 275 | static_cast<unsigned>(__size_ % __bits_per_word)); |
| 276 | } |
| 277 | }; |
| 278 | |
| 279 | template <class _Cp, bool _IsConst, typename _Cp::__storage_type> |
| 280 | class __bit_iterator { |
| 281 | public: |
| 282 | using difference_type = typename __size_difference_type_traits<_Cp>::difference_type; |
| 283 | using value_type = bool; |
| 284 | using pointer = __bit_iterator; |
| 285 | #ifndef _LIBCPP_ABI_BITSET_VECTOR_BOOL_CONST_SUBSCRIPT_RETURN_BOOL |
| 286 | using reference = __conditional_t<_IsConst, __bit_const_reference<_Cp>, __bit_reference<_Cp> >; |
| 287 | #else |
| 288 | using reference = __conditional_t<_IsConst, bool, __bit_reference<_Cp> >; |
| 289 | #endif |
| 290 | using iterator_category = random_access_iterator_tag; |
| 291 | |
| 292 | private: |
| 293 | using __storage_type _LIBCPP_NODEBUG = typename _Cp::__storage_type; |
| 294 | using __storage_pointer _LIBCPP_NODEBUG = |
| 295 | __conditional_t<_IsConst, typename _Cp::__const_storage_pointer, typename _Cp::__storage_pointer>; |
| 296 | |
| 297 | static const unsigned __bits_per_word = _Cp::__bits_per_word; |
| 298 | |
| 299 | __storage_pointer __seg_; |
| 300 | unsigned __ctz_; |
| 301 | |
| 302 | public: |
| 303 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator() _NOEXCEPT |
| 304 | #if _LIBCPP_STD_VER >= 14 |
| 305 | : __seg_(nullptr), |
| 306 | __ctz_(0) |
| 307 | #endif |
| 308 | { |
| 309 | } |
| 310 | |
| 311 | #ifdef _LIBCPP_ABI_TRIVIALLY_COPYABLE_BIT_ITERATOR |
| 312 | template <bool _IsConstDep = _IsConst, __enable_if_t<_IsConstDep, int> = 0> |
| 313 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator(const __bit_iterator<_Cp, false>& __it) _NOEXCEPT |
| 314 | : __seg_(__it.__seg_), |
| 315 | __ctz_(__it.__ctz_) {} |
| 316 | |
| 317 | _LIBCPP_HIDE_FROM_ABI __bit_iterator(const __bit_iterator&) = default; |
| 318 | _LIBCPP_HIDE_FROM_ABI __bit_iterator& operator=(const __bit_iterator&) = default; |
| 319 | #else |
| 320 | // When _IsConst=false, this is the copy constructor. |
| 321 | // It is non-trivial. Making it trivial would break ABI. |
| 322 | // When _IsConst=true, this is a converting constructor; |
| 323 | // the copy and move constructors are implicitly generated |
| 324 | // and trivial. |
| 325 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator(const __bit_iterator<_Cp, false>& __it) _NOEXCEPT |
| 326 | : __seg_(__it.__seg_), |
| 327 | __ctz_(__it.__ctz_) {} |
| 328 | |
| 329 | // When _IsConst=false, we have a user-provided copy constructor, |
| 330 | // so we must also provide a copy assignment operator because |
| 331 | // the implicit generation of a defaulted one is deprecated. |
| 332 | // When _IsConst=true, the assignment operators are |
| 333 | // implicitly generated and trivial. |
| 334 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator& |
| 335 | operator=(const _If<_IsConst, struct __private_nat, __bit_iterator>& __it) { |
| 336 | __seg_ = __it.__seg_; |
| 337 | __ctz_ = __it.__ctz_; |
| 338 | return *this; |
| 339 | } |
| 340 | #endif |
| 341 | |
| 342 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference operator*() const _NOEXCEPT { |
| 343 | _LIBCPP_ASSERT_INTERNAL(__ctz_ < __bits_per_word, "Dereferencing an invalid __bit_iterator." ); |
| 344 | return __conditional_t<_IsConst, __bit_const_reference<_Cp>, __bit_reference<_Cp> >( |
| 345 | __seg_, __storage_type(1) << __ctz_); |
| 346 | } |
| 347 | |
| 348 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator& operator++() { |
| 349 | if (__ctz_ != __bits_per_word - 1) |
| 350 | ++__ctz_; |
| 351 | else { |
| 352 | __ctz_ = 0; |
| 353 | ++__seg_; |
| 354 | } |
| 355 | return *this; |
| 356 | } |
| 357 | |
| 358 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator operator++(int) { |
| 359 | __bit_iterator __tmp = *this; |
| 360 | ++(*this); |
| 361 | return __tmp; |
| 362 | } |
| 363 | |
| 364 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator& operator--() { |
| 365 | if (__ctz_ != 0) |
| 366 | --__ctz_; |
| 367 | else { |
| 368 | __ctz_ = __bits_per_word - 1; |
| 369 | --__seg_; |
| 370 | } |
| 371 | return *this; |
| 372 | } |
| 373 | |
| 374 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator operator--(int) { |
| 375 | __bit_iterator __tmp = *this; |
| 376 | --(*this); |
| 377 | return __tmp; |
| 378 | } |
| 379 | |
| 380 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator& operator+=(difference_type __n) { |
| 381 | if (__n >= 0) |
| 382 | __seg_ += (__n + __ctz_) / __bits_per_word; |
| 383 | else |
| 384 | __seg_ += static_cast<difference_type>(__n - __bits_per_word + __ctz_ + 1) / |
| 385 | static_cast<difference_type>(__bits_per_word); |
| 386 | __n &= (__bits_per_word - 1); |
| 387 | __ctz_ = static_cast<unsigned>((__n + __ctz_) % __bits_per_word); |
| 388 | return *this; |
| 389 | } |
| 390 | |
| 391 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator& operator-=(difference_type __n) { |
| 392 | return *this += -__n; |
| 393 | } |
| 394 | |
| 395 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator operator+(difference_type __n) const { |
| 396 | __bit_iterator __t(*this); |
| 397 | __t += __n; |
| 398 | return __t; |
| 399 | } |
| 400 | |
| 401 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator operator-(difference_type __n) const { |
| 402 | __bit_iterator __t(*this); |
| 403 | __t -= __n; |
| 404 | return __t; |
| 405 | } |
| 406 | |
| 407 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 friend __bit_iterator |
| 408 | operator+(difference_type __n, const __bit_iterator& __it) { |
| 409 | return __it + __n; |
| 410 | } |
| 411 | |
| 412 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 friend difference_type |
| 413 | operator-(const __bit_iterator& __x, const __bit_iterator& __y) { |
| 414 | return (__x.__seg_ - __y.__seg_) * __bits_per_word + __x.__ctz_ - __y.__ctz_; |
| 415 | } |
| 416 | |
| 417 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference 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 | |
| 464 | private: |
| 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 | |
| 543 | template <class _Cp> |
| 544 | struct __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 | |
| 586 | template <class _Cp, bool _IsConst> |
| 587 | struct __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 pair<__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 std::make_pair(__last, __aligned_impl(__first, __last, __result)); |
| 717 | return std::make_pair(__last, __unaligned_impl(__first, __last, __result)); |
| 718 | } |
| 719 | }; |
| 720 | |
| 721 | template <class _Cl, class _Cr> |
| 722 | struct __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 | |