2010-05-11 19:42:16 +00:00
|
|
|
// -*- C++ -*-
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2019-01-19 10:56:40 +00:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2010-05-11 19:42:16 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef _LIBCPP___BIT_REFERENCE
|
|
|
|
#define _LIBCPP___BIT_REFERENCE
|
|
|
|
|
2025-02-26 12:18:25 -05:00
|
|
|
#include <__algorithm/comp.h>
|
2025-01-30 11:26:26 -05:00
|
|
|
#include <__algorithm/copy.h>
|
2025-01-30 14:55:05 -05:00
|
|
|
#include <__algorithm/copy_backward.h>
|
[libc++] Implement P1004R2 (constexpr std::vector)
Reviewed By: #libc, ldionne
Spies: mgorny, var-const, ormris, philnik, miscco, hiraditya, steven_wu, jkorous, ldionne, christof, libcxx-commits
Differential Revision: https://reviews.llvm.org/D68365
2022-07-27 14:41:40 +02:00
|
|
|
#include <__algorithm/copy_n.h>
|
2025-02-26 12:18:25 -05:00
|
|
|
#include <__algorithm/equal.h>
|
2022-02-15 18:18:08 +01:00
|
|
|
#include <__algorithm/min.h>
|
2025-03-13 14:07:23 -04:00
|
|
|
#include <__algorithm/rotate.h>
|
2025-03-04 17:15:36 -05:00
|
|
|
#include <__algorithm/swap_ranges.h>
|
2025-02-05 11:39:49 -05:00
|
|
|
#include <__assert>
|
2022-12-27 02:31:38 +01:00
|
|
|
#include <__bit/countr.h>
|
2024-07-24 19:42:48 +02:00
|
|
|
#include <__compare/ordering.h>
|
2022-01-07 09:45:05 -05:00
|
|
|
#include <__config>
|
2025-01-14 10:30:30 -05:00
|
|
|
#include <__cstddef/ptrdiff_t.h>
|
2024-12-18 21:10:27 +01:00
|
|
|
#include <__cstddef/size_t.h>
|
2025-02-26 12:18:25 -05:00
|
|
|
#include <__functional/identity.h>
|
2023-08-01 10:27:48 -07:00
|
|
|
#include <__fwd/bit_reference.h>
|
2022-02-15 18:18:08 +01:00
|
|
|
#include <__iterator/iterator_traits.h>
|
[libc++] Implement P1004R2 (constexpr std::vector)
Reviewed By: #libc, ldionne
Spies: mgorny, var-const, ormris, philnik, miscco, hiraditya, steven_wu, jkorous, ldionne, christof, libcxx-commits
Differential Revision: https://reviews.llvm.org/D68365
2022-07-27 14:41:40 +02:00
|
|
|
#include <__memory/construct_at.h>
|
2022-02-15 18:18:08 +01:00
|
|
|
#include <__memory/pointer_traits.h>
|
2023-02-12 12:32:36 +01:00
|
|
|
#include <__type_traits/conditional.h>
|
2025-02-26 12:18:25 -05:00
|
|
|
#include <__type_traits/desugars_to.h>
|
2025-02-05 11:39:49 -05:00
|
|
|
#include <__type_traits/enable_if.h>
|
2024-12-18 21:10:27 +01:00
|
|
|
#include <__type_traits/is_constant_evaluated.h>
|
2025-02-26 12:18:25 -05:00
|
|
|
#include <__type_traits/is_same.h>
|
2025-02-05 11:39:49 -05:00
|
|
|
#include <__type_traits/is_unsigned.h>
|
2025-01-14 10:30:30 -05:00
|
|
|
#include <__type_traits/void_t.h>
|
2025-01-30 11:26:26 -05:00
|
|
|
#include <__utility/pair.h>
|
2023-02-12 12:32:36 +01:00
|
|
|
#include <__utility/swap.h>
|
2025-02-05 11:39:49 -05:00
|
|
|
#include <climits>
|
2010-05-11 19:42:16 +00:00
|
|
|
|
2011-10-17 20:05:10 +00:00
|
|
|
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
|
2022-02-01 20:16:40 -05:00
|
|
|
# pragma GCC system_header
|
2011-10-17 20:05:10 +00:00
|
|
|
#endif
|
2010-05-11 19:42:16 +00:00
|
|
|
|
2017-05-31 22:07:49 +00:00
|
|
|
_LIBCPP_PUSH_MACROS
|
|
|
|
#include <__undef_macros>
|
|
|
|
|
2010-05-11 19:42:16 +00:00
|
|
|
_LIBCPP_BEGIN_NAMESPACE_STD
|
|
|
|
|
2023-07-28 10:43:38 -07:00
|
|
|
template <class _Cp>
|
|
|
|
class __bit_const_reference;
|
2010-05-11 19:42:16 +00:00
|
|
|
|
2011-07-02 20:33:23 +00:00
|
|
|
template <class _Tp>
|
2023-07-28 10:43:38 -07:00
|
|
|
struct __has_storage_type {
|
|
|
|
static const bool value = false;
|
2011-07-02 20:33:23 +00:00
|
|
|
};
|
|
|
|
|
2025-01-14 10:30:30 -05:00
|
|
|
template <class, class>
|
|
|
|
struct __size_difference_type_traits {
|
|
|
|
using difference_type = ptrdiff_t;
|
|
|
|
using size_type = size_t;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class _Cp>
|
|
|
|
struct __size_difference_type_traits<_Cp, __void_t<typename _Cp::difference_type, typename _Cp::size_type> > {
|
|
|
|
using difference_type = typename _Cp::difference_type;
|
|
|
|
using size_type = typename _Cp::size_type;
|
|
|
|
};
|
|
|
|
|
2025-03-13 14:15:03 -04:00
|
|
|
// The `__x_mask` functions are designed to work exclusively with any unsigned `_StorageType`s, including small
|
|
|
|
// integral types such as unsigned char/short, `uint8_t`, and `uint16_t`. To prevent undefined behavior or
|
|
|
|
// ambiguities due to integral promotions for the small integral types, all intermediate bitwise operations are
|
|
|
|
// explicitly cast back to the unsigned `_StorageType`.
|
|
|
|
|
|
|
|
// Creates a mask of type `_StorageType` with a specified number of leading zeros (__clz) and sets all remaining
|
|
|
|
// bits to one.
|
|
|
|
template <class _StorageType>
|
|
|
|
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _StorageType __trailing_mask(unsigned __clz) {
|
|
|
|
static_assert(is_unsigned<_StorageType>::value, "__trailing_mask only works with unsigned types");
|
|
|
|
return static_cast<_StorageType>(~static_cast<_StorageType>(0)) >> __clz;
|
|
|
|
}
|
|
|
|
|
2025-03-19 11:51:21 -04:00
|
|
|
// Creates a mask of type `_StorageType` with a specified number of trailing zeros (__ctz) and sets all remaining
|
|
|
|
// bits to one.
|
|
|
|
template <class _StorageType>
|
|
|
|
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _StorageType __leading_mask(unsigned __ctz) {
|
|
|
|
static_assert(is_unsigned<_StorageType>::value, "__leading_mask only works with unsigned types");
|
|
|
|
return static_cast<_StorageType>(~static_cast<_StorageType>(0)) << __ctz;
|
|
|
|
}
|
|
|
|
|
2025-03-13 14:15:03 -04:00
|
|
|
// Creates a mask of type `_StorageType` with a specified number of leading zeros (__clz), a specified number of
|
|
|
|
// trailing zeros (__ctz), and sets all bits in between to one.
|
|
|
|
template <class _StorageType>
|
|
|
|
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _StorageType __middle_mask(unsigned __clz, unsigned __ctz) {
|
|
|
|
static_assert(is_unsigned<_StorageType>::value, "__middle_mask only works with unsigned types");
|
2025-03-19 11:51:21 -04:00
|
|
|
return std::__leading_mask<_StorageType>(__ctz) & std::__trailing_mask<_StorageType>(__clz);
|
2025-03-13 14:15:03 -04:00
|
|
|
}
|
|
|
|
|
2025-02-05 11:39:49 -05:00
|
|
|
// This function is designed to operate correctly even for smaller integral types like `uint8_t`, `uint16_t`,
|
2025-03-13 14:15:03 -04:00
|
|
|
// or `unsigned short`.
|
2025-02-05 11:39:49 -05:00
|
|
|
// See https://github.com/llvm/llvm-project/pull/122410.
|
|
|
|
template <class _StoragePointer>
|
|
|
|
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 void
|
|
|
|
__fill_masked_range(_StoragePointer __word, unsigned __clz, unsigned __ctz, bool __fill_val) {
|
|
|
|
static_assert(is_unsigned<typename pointer_traits<_StoragePointer>::element_type>::value,
|
|
|
|
"__fill_masked_range must be called with unsigned type");
|
|
|
|
using _StorageType = typename pointer_traits<_StoragePointer>::element_type;
|
|
|
|
_LIBCPP_ASSERT_VALID_INPUT_RANGE(
|
|
|
|
__ctz + __clz < sizeof(_StorageType) * CHAR_BIT, "__fill_masked_range called with invalid range");
|
2025-03-13 14:15:03 -04:00
|
|
|
_StorageType __m = std::__middle_mask<_StorageType>(__clz, __ctz);
|
2025-02-05 11:39:49 -05:00
|
|
|
if (__fill_val)
|
|
|
|
*__word |= __m;
|
|
|
|
else
|
2025-03-13 14:15:03 -04:00
|
|
|
*__word &= ~__m;
|
2025-02-05 11:39:49 -05:00
|
|
|
}
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Cp, bool = __has_storage_type<_Cp>::value>
|
2023-07-28 10:43:38 -07:00
|
|
|
class __bit_reference {
|
2025-01-08 17:12:59 +01:00
|
|
|
using __storage_type _LIBCPP_NODEBUG = typename _Cp::__storage_type;
|
|
|
|
using __storage_pointer _LIBCPP_NODEBUG = typename _Cp::__storage_pointer;
|
2023-07-28 10:43:38 -07:00
|
|
|
|
|
|
|
__storage_pointer __seg_;
|
|
|
|
__storage_type __mask_;
|
2010-05-11 19:42:16 +00:00
|
|
|
|
2023-07-28 10:43:38 -07:00
|
|
|
friend typename _Cp::__self;
|
2010-05-11 19:42:16 +00:00
|
|
|
|
2023-07-28 10:43:38 -07:00
|
|
|
friend class __bit_const_reference<_Cp>;
|
|
|
|
friend class __bit_iterator<_Cp, false>;
|
2017-01-06 21:42:58 +00:00
|
|
|
|
2010-05-11 19:42:16 +00:00
|
|
|
public:
|
2025-01-08 17:12:59 +01:00
|
|
|
using __container _LIBCPP_NODEBUG = typename _Cp::__self;
|
2022-11-29 07:57:30 +01:00
|
|
|
|
2023-07-28 10:43:38 -07:00
|
|
|
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_reference(const __bit_reference&) = default;
|
2019-12-05 15:54:08 -08:00
|
|
|
|
2023-07-28 10:43:38 -07:00
|
|
|
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 operator bool() const _NOEXCEPT {
|
|
|
|
return static_cast<bool>(*__seg_ & __mask_);
|
|
|
|
}
|
|
|
|
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool operator~() const _NOEXCEPT {
|
|
|
|
return !static_cast<bool>(*this);
|
|
|
|
}
|
2010-05-11 19:42:16 +00:00
|
|
|
|
2023-07-28 10:43:38 -07:00
|
|
|
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_reference& operator=(bool __x) _NOEXCEPT {
|
|
|
|
if (__x)
|
|
|
|
*__seg_ |= __mask_;
|
|
|
|
else
|
|
|
|
*__seg_ &= ~__mask_;
|
|
|
|
return *this;
|
|
|
|
}
|
2010-08-22 00:02:43 +00:00
|
|
|
|
2023-02-14 00:56:09 +01:00
|
|
|
#if _LIBCPP_STD_VER >= 23
|
2023-07-28 10:43:38 -07:00
|
|
|
_LIBCPP_HIDE_FROM_ABI constexpr const __bit_reference& operator=(bool __x) const noexcept {
|
|
|
|
if (__x)
|
|
|
|
*__seg_ |= __mask_;
|
|
|
|
else
|
|
|
|
*__seg_ &= ~__mask_;
|
|
|
|
return *this;
|
|
|
|
}
|
2022-02-02 22:48:42 +01:00
|
|
|
#endif
|
|
|
|
|
2023-07-28 10:43:38 -07:00
|
|
|
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_reference& operator=(const __bit_reference& __x) _NOEXCEPT {
|
|
|
|
return operator=(static_cast<bool>(__x));
|
|
|
|
}
|
|
|
|
|
|
|
|
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void flip() _NOEXCEPT { *__seg_ ^= __mask_; }
|
|
|
|
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator<_Cp, false> operator&() const _NOEXCEPT {
|
|
|
|
return __bit_iterator<_Cp, false>(__seg_, static_cast<unsigned>(std::__libcpp_ctz(__mask_)));
|
|
|
|
}
|
2010-05-11 19:42:16 +00:00
|
|
|
|
|
|
|
private:
|
2023-07-28 10:43:38 -07:00
|
|
|
_LIBCPP_HIDE_FROM_ABI
|
|
|
|
_LIBCPP_CONSTEXPR_SINCE_CXX20 explicit __bit_reference(__storage_pointer __s, __storage_type __m) _NOEXCEPT
|
|
|
|
: __seg_(__s),
|
|
|
|
__mask_(__m) {}
|
2010-05-11 19:42:16 +00:00
|
|
|
};
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Cp>
|
2023-07-28 10:43:38 -07:00
|
|
|
class __bit_reference<_Cp, false> {};
|
2011-07-02 20:33:23 +00:00
|
|
|
|
2013-03-26 13:48:57 +00:00
|
|
|
template <class _Cp>
|
2023-07-28 10:43:38 -07:00
|
|
|
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
|
|
|
|
swap(__bit_reference<_Cp> __x, __bit_reference<_Cp> __y) _NOEXCEPT {
|
|
|
|
bool __t = __x;
|
|
|
|
__x = __y;
|
|
|
|
__y = __t;
|
2013-03-26 13:48:57 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Cp, class _Dp>
|
2023-07-28 10:43:38 -07:00
|
|
|
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
|
|
|
|
swap(__bit_reference<_Cp> __x, __bit_reference<_Dp> __y) _NOEXCEPT {
|
|
|
|
bool __t = __x;
|
|
|
|
__x = __y;
|
|
|
|
__y = __t;
|
2010-05-11 19:42:16 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Cp>
|
2023-07-28 10:43:38 -07:00
|
|
|
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void swap(__bit_reference<_Cp> __x, bool& __y) _NOEXCEPT {
|
|
|
|
bool __t = __x;
|
|
|
|
__x = __y;
|
|
|
|
__y = __t;
|
2010-05-11 19:42:16 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Cp>
|
2023-07-28 10:43:38 -07:00
|
|
|
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void swap(bool& __x, __bit_reference<_Cp> __y) _NOEXCEPT {
|
|
|
|
bool __t = __x;
|
|
|
|
__x = __y;
|
|
|
|
__y = __t;
|
2010-05-11 19:42:16 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Cp>
|
2023-07-28 10:43:38 -07:00
|
|
|
class __bit_const_reference {
|
2025-01-08 17:12:59 +01:00
|
|
|
using __storage_type _LIBCPP_NODEBUG = typename _Cp::__storage_type;
|
|
|
|
using __storage_pointer _LIBCPP_NODEBUG = typename _Cp::__const_storage_pointer;
|
2023-07-28 10:43:38 -07:00
|
|
|
|
|
|
|
__storage_pointer __seg_;
|
|
|
|
__storage_type __mask_;
|
2010-05-11 19:42:16 +00:00
|
|
|
|
2023-07-28 10:43:38 -07:00
|
|
|
friend typename _Cp::__self;
|
|
|
|
friend class __bit_iterator<_Cp, true>;
|
2010-05-11 19:42:16 +00:00
|
|
|
|
|
|
|
public:
|
2025-01-08 17:12:59 +01:00
|
|
|
using __container _LIBCPP_NODEBUG = typename _Cp::__self;
|
2023-02-17 18:55:48 +01:00
|
|
|
|
2023-07-28 10:43:38 -07:00
|
|
|
_LIBCPP_HIDE_FROM_ABI __bit_const_reference(const __bit_const_reference&) = default;
|
2024-06-18 10:51:57 +02:00
|
|
|
__bit_const_reference& operator=(const __bit_const_reference&) = delete;
|
2019-12-05 15:54:08 -08:00
|
|
|
|
2023-07-28 10:43:38 -07:00
|
|
|
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_const_reference(const __bit_reference<_Cp>& __x) _NOEXCEPT
|
|
|
|
: __seg_(__x.__seg_),
|
|
|
|
__mask_(__x.__mask_) {}
|
2010-05-11 19:42:16 +00:00
|
|
|
|
2023-07-28 10:43:38 -07:00
|
|
|
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR operator bool() const _NOEXCEPT {
|
|
|
|
return static_cast<bool>(*__seg_ & __mask_);
|
|
|
|
}
|
|
|
|
|
|
|
|
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator<_Cp, true> operator&() const _NOEXCEPT {
|
|
|
|
return __bit_iterator<_Cp, true>(__seg_, static_cast<unsigned>(std::__libcpp_ctz(__mask_)));
|
|
|
|
}
|
2010-05-11 19:42:16 +00:00
|
|
|
|
|
|
|
private:
|
2023-07-28 10:43:38 -07:00
|
|
|
_LIBCPP_HIDE_FROM_ABI
|
|
|
|
_LIBCPP_CONSTEXPR explicit __bit_const_reference(__storage_pointer __s, __storage_type __m) _NOEXCEPT
|
|
|
|
: __seg_(__s),
|
|
|
|
__mask_(__m) {}
|
2010-05-11 19:42:16 +00:00
|
|
|
};
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Cp>
|
2023-07-28 10:43:38 -07:00
|
|
|
struct __bit_array {
|
2025-01-14 10:30:30 -05:00
|
|
|
using difference_type _LIBCPP_NODEBUG = typename __size_difference_type_traits<_Cp>::difference_type;
|
2025-01-08 17:12:59 +01:00
|
|
|
using __storage_type _LIBCPP_NODEBUG = typename _Cp::__storage_type;
|
|
|
|
using __storage_pointer _LIBCPP_NODEBUG = typename _Cp::__storage_pointer;
|
|
|
|
using iterator _LIBCPP_NODEBUG = typename _Cp::iterator;
|
2023-07-28 10:43:38 -07:00
|
|
|
|
|
|
|
static const unsigned __bits_per_word = _Cp::__bits_per_word;
|
|
|
|
static const unsigned _Np = 4;
|
|
|
|
|
|
|
|
difference_type __size_;
|
|
|
|
__storage_type __word_[_Np];
|
|
|
|
|
|
|
|
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static difference_type capacity() {
|
|
|
|
return static_cast<difference_type>(_Np * __bits_per_word);
|
|
|
|
}
|
|
|
|
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit __bit_array(difference_type __s) : __size_(__s) {
|
|
|
|
if (__libcpp_is_constant_evaluated()) {
|
|
|
|
for (size_t __i = 0; __i != __bit_array<_Cp>::_Np; ++__i)
|
|
|
|
std::__construct_at(__word_ + __i, 0);
|
2013-06-27 19:35:32 +00:00
|
|
|
}
|
2023-07-28 10:43:38 -07:00
|
|
|
}
|
|
|
|
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator begin() {
|
|
|
|
return iterator(pointer_traits<__storage_pointer>::pointer_to(__word_[0]), 0);
|
|
|
|
}
|
|
|
|
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator end() {
|
|
|
|
return iterator(pointer_traits<__storage_pointer>::pointer_to(__word_[0]) + __size_ / __bits_per_word,
|
|
|
|
static_cast<unsigned>(__size_ % __bits_per_word));
|
|
|
|
}
|
2010-05-11 19:42:16 +00:00
|
|
|
};
|
|
|
|
|
2023-07-28 10:43:38 -07:00
|
|
|
template <class _Cp, bool _IsConst, typename _Cp::__storage_type>
|
|
|
|
class __bit_iterator {
|
2010-05-11 19:42:16 +00:00
|
|
|
public:
|
2025-01-14 10:30:30 -05:00
|
|
|
using difference_type = typename __size_difference_type_traits<_Cp>::difference_type;
|
2023-07-28 08:32:39 -07:00
|
|
|
using value_type = bool;
|
|
|
|
using pointer = __bit_iterator;
|
2022-04-22 20:56:58 +02:00
|
|
|
#ifndef _LIBCPP_ABI_BITSET_VECTOR_BOOL_CONST_SUBSCRIPT_RETURN_BOOL
|
2023-07-28 08:32:39 -07:00
|
|
|
using reference = __conditional_t<_IsConst, __bit_const_reference<_Cp>, __bit_reference<_Cp> >;
|
2022-04-22 20:56:58 +02:00
|
|
|
#else
|
2023-07-28 08:32:39 -07:00
|
|
|
using reference = __conditional_t<_IsConst, bool, __bit_reference<_Cp> >;
|
2022-04-22 20:56:58 +02:00
|
|
|
#endif
|
2023-07-28 08:32:39 -07:00
|
|
|
using iterator_category = random_access_iterator_tag;
|
2010-05-11 19:42:16 +00:00
|
|
|
|
|
|
|
private:
|
2025-01-08 17:12:59 +01:00
|
|
|
using __storage_type _LIBCPP_NODEBUG = typename _Cp::__storage_type;
|
|
|
|
using __storage_pointer _LIBCPP_NODEBUG =
|
2023-07-28 08:32:39 -07:00
|
|
|
__conditional_t<_IsConst, typename _Cp::__const_storage_pointer, typename _Cp::__storage_pointer>;
|
|
|
|
|
2023-07-28 10:43:38 -07:00
|
|
|
static const unsigned __bits_per_word = _Cp::__bits_per_word;
|
2010-05-11 19:42:16 +00:00
|
|
|
|
2023-07-28 10:43:38 -07:00
|
|
|
__storage_pointer __seg_;
|
|
|
|
unsigned __ctz_;
|
2010-05-11 19:42:16 +00:00
|
|
|
|
|
|
|
public:
|
2023-07-28 10:43:38 -07:00
|
|
|
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator() _NOEXCEPT
|
2023-02-14 00:56:09 +01:00
|
|
|
#if _LIBCPP_STD_VER >= 14
|
2023-07-28 10:43:38 -07:00
|
|
|
: __seg_(nullptr),
|
|
|
|
__ctz_(0)
|
2013-08-07 20:53:44 +00:00
|
|
|
#endif
|
2023-07-28 10:43:38 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// When _IsConst=false, this is the copy constructor.
|
|
|
|
// It is non-trivial. Making it trivial would break ABI.
|
|
|
|
// When _IsConst=true, this is a converting constructor;
|
|
|
|
// the copy and move constructors are implicitly generated
|
|
|
|
// and trivial.
|
|
|
|
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator(const __bit_iterator<_Cp, false>& __it) _NOEXCEPT
|
|
|
|
: __seg_(__it.__seg_),
|
|
|
|
__ctz_(__it.__ctz_) {}
|
|
|
|
|
|
|
|
// When _IsConst=false, we have a user-provided copy constructor,
|
|
|
|
// so we must also provide a copy assignment operator because
|
|
|
|
// the implicit generation of a defaulted one is deprecated.
|
|
|
|
// When _IsConst=true, the assignment operators are
|
|
|
|
// implicitly generated and trivial.
|
|
|
|
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator&
|
|
|
|
operator=(const _If<_IsConst, struct __private_nat, __bit_iterator>& __it) {
|
|
|
|
__seg_ = __it.__seg_;
|
|
|
|
__ctz_ = __it.__ctz_;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference operator*() const _NOEXCEPT {
|
2025-03-26 12:02:03 -04:00
|
|
|
_LIBCPP_ASSERT_INTERNAL(__ctz_ < __bits_per_word, "Dereferencing an invalid __bit_iterator.");
|
2023-07-28 10:43:38 -07:00
|
|
|
return __conditional_t<_IsConst, __bit_const_reference<_Cp>, __bit_reference<_Cp> >(
|
|
|
|
__seg_, __storage_type(1) << __ctz_);
|
|
|
|
}
|
|
|
|
|
|
|
|
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator& operator++() {
|
|
|
|
if (__ctz_ != __bits_per_word - 1)
|
|
|
|
++__ctz_;
|
|
|
|
else {
|
|
|
|
__ctz_ = 0;
|
|
|
|
++__seg_;
|
2010-05-11 19:42:16 +00:00
|
|
|
}
|
2023-07-28 10:43:38 -07:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator operator++(int) {
|
|
|
|
__bit_iterator __tmp = *this;
|
|
|
|
++(*this);
|
|
|
|
return __tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator& operator--() {
|
|
|
|
if (__ctz_ != 0)
|
|
|
|
--__ctz_;
|
|
|
|
else {
|
|
|
|
__ctz_ = __bits_per_word - 1;
|
|
|
|
--__seg_;
|
2010-05-11 19:42:16 +00:00
|
|
|
}
|
2023-07-28 10:43:38 -07:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator operator--(int) {
|
|
|
|
__bit_iterator __tmp = *this;
|
|
|
|
--(*this);
|
|
|
|
return __tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator& operator+=(difference_type __n) {
|
|
|
|
if (__n >= 0)
|
|
|
|
__seg_ += (__n + __ctz_) / __bits_per_word;
|
|
|
|
else
|
|
|
|
__seg_ += static_cast<difference_type>(__n - __bits_per_word + __ctz_ + 1) /
|
|
|
|
static_cast<difference_type>(__bits_per_word);
|
|
|
|
__n &= (__bits_per_word - 1);
|
|
|
|
__ctz_ = static_cast<unsigned>((__n + __ctz_) % __bits_per_word);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator& operator-=(difference_type __n) {
|
|
|
|
return *this += -__n;
|
|
|
|
}
|
|
|
|
|
|
|
|
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator operator+(difference_type __n) const {
|
|
|
|
__bit_iterator __t(*this);
|
|
|
|
__t += __n;
|
|
|
|
return __t;
|
|
|
|
}
|
|
|
|
|
|
|
|
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator operator-(difference_type __n) const {
|
|
|
|
__bit_iterator __t(*this);
|
|
|
|
__t -= __n;
|
|
|
|
return __t;
|
|
|
|
}
|
|
|
|
|
|
|
|
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 friend __bit_iterator
|
|
|
|
operator+(difference_type __n, const __bit_iterator& __it) {
|
|
|
|
return __it + __n;
|
|
|
|
}
|
|
|
|
|
|
|
|
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 friend difference_type
|
|
|
|
operator-(const __bit_iterator& __x, const __bit_iterator& __y) {
|
|
|
|
return (__x.__seg_ - __y.__seg_) * __bits_per_word + __x.__ctz_ - __y.__ctz_;
|
|
|
|
}
|
|
|
|
|
|
|
|
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference operator[](difference_type __n) const {
|
|
|
|
return *(*this + __n);
|
|
|
|
}
|
|
|
|
|
|
|
|
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 friend bool
|
|
|
|
operator==(const __bit_iterator& __x, const __bit_iterator& __y) {
|
|
|
|
return __x.__seg_ == __y.__seg_ && __x.__ctz_ == __y.__ctz_;
|
|
|
|
}
|
|
|
|
|
2024-07-24 19:42:48 +02:00
|
|
|
#if _LIBCPP_STD_VER <= 17
|
2023-07-28 10:43:38 -07:00
|
|
|
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 friend bool
|
|
|
|
operator!=(const __bit_iterator& __x, const __bit_iterator& __y) {
|
|
|
|
return !(__x == __y);
|
|
|
|
}
|
|
|
|
|
|
|
|
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 friend bool
|
|
|
|
operator<(const __bit_iterator& __x, const __bit_iterator& __y) {
|
|
|
|
return __x.__seg_ < __y.__seg_ || (__x.__seg_ == __y.__seg_ && __x.__ctz_ < __y.__ctz_);
|
|
|
|
}
|
|
|
|
|
|
|
|
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 friend bool
|
|
|
|
operator>(const __bit_iterator& __x, const __bit_iterator& __y) {
|
|
|
|
return __y < __x;
|
|
|
|
}
|
|
|
|
|
|
|
|
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 friend bool
|
|
|
|
operator<=(const __bit_iterator& __x, const __bit_iterator& __y) {
|
|
|
|
return !(__y < __x);
|
|
|
|
}
|
|
|
|
|
|
|
|
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 friend bool
|
|
|
|
operator>=(const __bit_iterator& __x, const __bit_iterator& __y) {
|
|
|
|
return !(__x < __y);
|
|
|
|
}
|
2024-07-24 19:42:48 +02:00
|
|
|
#else // _LIBCPP_STD_VER <= 17
|
|
|
|
_LIBCPP_HIDE_FROM_ABI constexpr friend strong_ordering
|
|
|
|
operator<=>(const __bit_iterator& __x, const __bit_iterator& __y) {
|
|
|
|
if (__x.__seg_ < __y.__seg_)
|
|
|
|
return strong_ordering::less;
|
|
|
|
|
|
|
|
if (__x.__seg_ == __y.__seg_)
|
|
|
|
return __x.__ctz_ <=> __y.__ctz_;
|
|
|
|
|
|
|
|
return strong_ordering::greater;
|
|
|
|
}
|
|
|
|
#endif // _LIBCPP_STD_VER <= 17
|
2010-05-11 19:42:16 +00:00
|
|
|
|
|
|
|
private:
|
2023-07-28 10:43:38 -07:00
|
|
|
_LIBCPP_HIDE_FROM_ABI
|
|
|
|
_LIBCPP_CONSTEXPR_SINCE_CXX20 explicit __bit_iterator(__storage_pointer __s, unsigned __ctz) _NOEXCEPT
|
|
|
|
: __seg_(__s),
|
2025-03-26 12:02:03 -04:00
|
|
|
__ctz_(__ctz) {
|
|
|
|
_LIBCPP_ASSERT_INTERNAL(
|
|
|
|
__ctz_ < __bits_per_word, "__bit_iterator initialized with an invalid number of trailing zeros.");
|
|
|
|
}
|
2023-07-28 10:43:38 -07:00
|
|
|
|
|
|
|
friend typename _Cp::__self;
|
|
|
|
|
|
|
|
friend class __bit_reference<_Cp>;
|
|
|
|
friend class __bit_const_reference<_Cp>;
|
|
|
|
friend class __bit_iterator<_Cp, true>;
|
|
|
|
template <class _Dp>
|
|
|
|
friend struct __bit_array;
|
2024-03-17 20:00:54 +01:00
|
|
|
|
2024-02-05 17:41:12 +01:00
|
|
|
template <bool _FillVal, class _Dp>
|
2024-03-17 20:00:54 +01:00
|
|
|
_LIBCPP_CONSTEXPR_SINCE_CXX20 friend void
|
2025-01-14 10:30:30 -05:00
|
|
|
__fill_n_bool(__bit_iterator<_Dp, false> __first, typename __size_difference_type_traits<_Dp>::size_type __n);
|
2023-07-28 10:43:38 -07:00
|
|
|
|
|
|
|
template <class _Dp, bool _IC>
|
|
|
|
_LIBCPP_CONSTEXPR_SINCE_CXX20 friend __bit_iterator<_Dp, false> __copy_aligned(
|
|
|
|
__bit_iterator<_Dp, _IC> __first, __bit_iterator<_Dp, _IC> __last, __bit_iterator<_Dp, false> __result);
|
|
|
|
template <class _Dp, bool _IC>
|
|
|
|
_LIBCPP_CONSTEXPR_SINCE_CXX20 friend __bit_iterator<_Dp, false> __copy_unaligned(
|
|
|
|
__bit_iterator<_Dp, _IC> __first, __bit_iterator<_Dp, _IC> __last, __bit_iterator<_Dp, false> __result);
|
|
|
|
template <class _Dp, bool _IC>
|
2025-01-30 11:26:26 -05:00
|
|
|
_LIBCPP_CONSTEXPR_SINCE_CXX20 friend pair<__bit_iterator<_Dp, _IC>, __bit_iterator<_Dp, false> >
|
|
|
|
__copy_impl::operator()(
|
|
|
|
__bit_iterator<_Dp, _IC> __first, __bit_iterator<_Dp, _IC> __last, __bit_iterator<_Dp, false> __result) const;
|
2023-07-28 10:43:38 -07:00
|
|
|
template <class _Dp, bool _IC>
|
|
|
|
_LIBCPP_CONSTEXPR_SINCE_CXX20 friend __bit_iterator<_Dp, false> __copy_backward_aligned(
|
|
|
|
__bit_iterator<_Dp, _IC> __first, __bit_iterator<_Dp, _IC> __last, __bit_iterator<_Dp, false> __result);
|
|
|
|
template <class _Dp, bool _IC>
|
|
|
|
_LIBCPP_CONSTEXPR_SINCE_CXX20 friend __bit_iterator<_Dp, false> __copy_backward_unaligned(
|
|
|
|
__bit_iterator<_Dp, _IC> __first, __bit_iterator<_Dp, _IC> __last, __bit_iterator<_Dp, false> __result);
|
2025-01-30 14:55:05 -05:00
|
|
|
template <class _AlgPolicy>
|
|
|
|
friend struct __copy_backward_impl;
|
2023-07-28 10:43:38 -07:00
|
|
|
template <class _Cl, class _Cr>
|
2025-03-04 17:15:36 -05:00
|
|
|
_LIBCPP_CONSTEXPR_SINCE_CXX20 friend __bit_iterator<_Cr, false>
|
2023-07-28 10:43:38 -07:00
|
|
|
__swap_ranges_aligned(__bit_iterator<_Cl, false>, __bit_iterator<_Cl, false>, __bit_iterator<_Cr, false>);
|
|
|
|
template <class _Cl, class _Cr>
|
2025-03-04 17:15:36 -05:00
|
|
|
_LIBCPP_CONSTEXPR_SINCE_CXX20 friend __bit_iterator<_Cr, false>
|
2023-07-28 10:43:38 -07:00
|
|
|
__swap_ranges_unaligned(__bit_iterator<_Cl, false>, __bit_iterator<_Cl, false>, __bit_iterator<_Cr, false>);
|
2025-03-04 17:15:36 -05:00
|
|
|
template <class, class _Cl, class _Cr>
|
|
|
|
_LIBCPP_CONSTEXPR_SINCE_CXX20 friend pair<__bit_iterator<_Cl, false>, __bit_iterator<_Cr, false> >
|
|
|
|
__swap_ranges(__bit_iterator<_Cl, false>, __bit_iterator<_Cl, false>, __bit_iterator<_Cr, false>);
|
2025-03-13 14:07:23 -04:00
|
|
|
template <class, class _Dp>
|
|
|
|
_LIBCPP_CONSTEXPR_SINCE_CXX20 friend pair<__bit_iterator<_Dp, false>, __bit_iterator<_Dp, false> >
|
|
|
|
__rotate(__bit_iterator<_Dp, false>, __bit_iterator<_Dp, false>, __bit_iterator<_Dp, false>);
|
2025-02-26 12:18:25 -05:00
|
|
|
template <class _Dp, bool _IsConst1, bool _IsConst2>
|
2023-07-28 10:43:38 -07:00
|
|
|
_LIBCPP_CONSTEXPR_SINCE_CXX20 friend bool
|
2025-02-26 12:18:25 -05:00
|
|
|
__equal_aligned(__bit_iterator<_Dp, _IsConst1>, __bit_iterator<_Dp, _IsConst1>, __bit_iterator<_Dp, _IsConst2>);
|
|
|
|
template <class _Dp, bool _IsConst1, bool _IsConst2>
|
2023-07-28 10:43:38 -07:00
|
|
|
_LIBCPP_CONSTEXPR_SINCE_CXX20 friend bool
|
2025-02-26 12:18:25 -05:00
|
|
|
__equal_unaligned(__bit_iterator<_Dp, _IsConst1>, __bit_iterator<_Dp, _IsConst1>, __bit_iterator<_Dp, _IsConst2>);
|
|
|
|
template <class _Dp,
|
|
|
|
bool _IsConst1,
|
|
|
|
bool _IsConst2,
|
|
|
|
class _BinaryPredicate,
|
|
|
|
__enable_if_t<__desugars_to_v<__equal_tag, _BinaryPredicate, bool, bool>, int> >
|
|
|
|
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 friend bool __equal_iter_impl(
|
|
|
|
__bit_iterator<_Dp, _IsConst1>, __bit_iterator<_Dp, _IsConst1>, __bit_iterator<_Dp, _IsConst2>, _BinaryPredicate);
|
|
|
|
template <class _Dp,
|
|
|
|
bool _IsConst1,
|
|
|
|
bool _IsConst2,
|
|
|
|
class _Pred,
|
|
|
|
class _Proj1,
|
|
|
|
class _Proj2,
|
|
|
|
__enable_if_t<__desugars_to_v<__equal_tag, _Pred, bool, bool> && __is_identity<_Proj1>::value &&
|
|
|
|
__is_identity<_Proj2>::value,
|
|
|
|
int> >
|
|
|
|
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 friend bool __equal_impl(
|
|
|
|
__bit_iterator<_Dp, _IsConst1> __first1,
|
|
|
|
__bit_iterator<_Dp, _IsConst1> __last1,
|
|
|
|
__bit_iterator<_Dp, _IsConst2> __first2,
|
|
|
|
__bit_iterator<_Dp, _IsConst2>,
|
|
|
|
_Pred&,
|
|
|
|
_Proj1&,
|
|
|
|
_Proj2&);
|
2023-07-28 10:43:38 -07:00
|
|
|
template <bool _ToFind, class _Dp, bool _IC>
|
|
|
|
_LIBCPP_CONSTEXPR_SINCE_CXX20 friend __bit_iterator<_Dp, _IC>
|
2025-01-14 10:30:30 -05:00
|
|
|
__find_bool(__bit_iterator<_Dp, _IC>, typename __size_difference_type_traits<_Dp>::size_type);
|
2023-07-28 10:43:38 -07:00
|
|
|
template <bool _ToCount, class _Dp, bool _IC>
|
2025-01-14 10:30:30 -05:00
|
|
|
friend typename __bit_iterator<_Dp, _IC>::difference_type _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
|
|
|
|
__count_bool(__bit_iterator<_Dp, _IC>, typename __size_difference_type_traits<_Dp>::size_type);
|
2010-05-11 19:42:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
_LIBCPP_END_NAMESPACE_STD
|
|
|
|
|
2017-05-31 22:07:49 +00:00
|
|
|
_LIBCPP_POP_MACROS
|
|
|
|
|
2021-04-20 12:03:32 -04:00
|
|
|
#endif // _LIBCPP___BIT_REFERENCE
|