9#ifndef HPP_ALIB_LANG_BITSET
10#define HPP_ALIB_LANG_BITSET 1
12#if !defined(HPP_ALIB_LANG_BITS)
16namespace alib {
namespace lang {
69template<
typename TInterface, TInterface TEnd, TInterface TBegin= 0>
73 #if !defined(ALIB_DOX)
75 static_assert( !(TBegin > TEnd),
"First Idx greater or equal than last index + 1" );
78 template<
typename TBitSetCM,
bool isReverse>
class TBidiIterator;
85 static constexpr int Capacity=
static_cast<int>( TEnd )
86 -
static_cast<int>( TBegin );
111 constexpr static int wordIdx(TInterface b)
noexcept
112 {
return (
static_cast<int>(b) -
static_cast<int>(TBegin)) /
bitsof(
TWord); }
117 constexpr static int bitIdx (TInterface b)
noexcept
120 &&
static_cast<int>(b) <
static_cast<int>(TEnd),
121 "ALIB/BITS",
"Given bit index out of bounds: ",
static_cast<int>(b) )
122 return (
static_cast<int>(b) -
static_cast<int>(TBegin)) %
bitsof(
TWord);
143 : LowerMask<Capacity % bitsof(TWord), TWord>();
154 constexpr static TWord mask1101(TInterface b)
noexcept {
return ~mask0010(b); }
163 template<
typename TBitSetCM>
167 #if !defined(ALIB_DOX)
168 template<
typename T,
bool isReverse>
friend class TBidiIterator;
180 constexpr Reference(TBitSetCM& set, TInterface b) noexcept
198 constexpr TInterface
Bit() const noexcept
199 {
return TInterface(
bit); }
219 constexpr operator bool() noexcept
232 #if ALIB_CPP_STANDARD >= 20 || defined(ALIB_DOX)
288 template <
typename... T>
301 template <
typename... T>
306 if constexpr (
sizeof...(args) > 0)
315 template <
typename... T>
320 if constexpr (
sizeof...(args) > 0)
329 template <
typename... T>
334 if constexpr (
sizeof...(args) > 0)
352 bool Test( TInterface bit )
noexcept
428 template<
int TEnableif=QtyWords>
typename std::enable_if<TEnableif==1, TWord&>::type
Export() noexcept {
return words[ 0]; }
429 template<
int TEnableif=QtyWords>
typename std::enable_if<TEnableif==1,const TWord >::type
Export() const noexcept {
return words[ 0]; }
430 template<
int TEnableif=QtyWords>
typename std::enable_if<TEnableif!=1, TWord&>::type
Export(
int wordIdx)
noexcept {
432 template<
int TEnableif=
QtyWords> typename std::enable_if<TEnableif!=1,const
TWord >::type
Export(
int wordIdx) const noexcept {
456 template<
int TEnableif=QtyWords>
457 typename std::enable_if<TEnableif==1,void>::type
462 template<
int TEnableif=QtyWords>
463 typename std::enable_if<TEnableif!=1,void>::type
467 "Index out of bounds." )
479 if(
words[w] != rhs.words[w] )
484 #if ALIB_CPP_STANDARD < 20 || defined(ALIB_DOX)
525 ALIB_ASSERT_ERROR( cnt >= 0,
"ALIB/BITS",
"Negative value for TBitSet shift operation given." )
537 for(
int w=
QtyWords -1; w >= 0; --w )
538 target.words[w]= w - offW >= 0 ?
words[w - offW]
542 for(
int w=
QtyWords -1; w >= 0; --w )
543 target.words[w]= w >= offW ? (
TWord(
words[w - offW] << offB)
547 target.words[
QtyWords - 1]&= LowerMask<Capacity % bitsof(TWord), TWord>();
557 ALIB_ASSERT_ERROR( cnt >= 0,
"ALIB/BITS",
"Negative value for TBitSet shift operation given." )
579 target.words[
QtyWords - 1]&= LowerMask<Capacity % bitsof(TWord), TWord>();
639 template<
typename TBitSetCM,
bool isReverse>
661 ref.bit= TInterface(
static_cast<int>(
ref.bit) + 1);
664 while(
static_cast<int>(
ref.bit) <
static_cast<int>(TEnd))
668 & UpperMask<TWord>(bIdx);
676 if(
static_cast<int>(
ref.bit) >
static_cast<int>(TEnd))
677 ref.bit= TInterface(TEnd);
682 ref.bit= TInterface(
static_cast<int>(
ref.bit) + trailingZeros - bIdx);
694 ref.bit= TInterface(
static_cast<int>(
ref.bit) - 1);
697 while(
static_cast<int>(
ref.bit) >=
static_cast<int>(TBegin))
710 ref.bit= TInterface(
static_cast<int>(
ref.bit) - bIdx -1);
711 if(
static_cast<int>(
ref.bit) <
static_cast<int>(TBegin) -1)
712 ref.bit= TInterface(
static_cast<int>(TBegin) - 1);
717 ref.bit= TInterface(
static_cast<int>(
ref.bit)
730 :
ref(bitSet, startBit)
738 if constexpr (!isReverse)
return up();
765 return !(*
this == other);
781 if constexpr (isReverse)
return up();
800 {
return ref.bit < other.
ref.bit; }
807 {
return ref.bit <= other.
ref.bit; }
814 {
return ref.bit > other.
ref.bit; }
821 {
return ref.bit >= other.
ref.bit; }
903template<
int TEnd,
int TBegin= 0,
typename TInterface=
int>
926template<
typename TInterface, TInterface TEnd, TInterface TBegin>
934 result.
Import( lhs.Export() & rhs.Export() );
936 for (
int w = 0; w < alib::lang::TBitSet<TInterface,TEnd,TBegin>::QtyWords; ++w)
937 result.
Import( lhs.Export(w) & rhs.Export(w), w );
953template<
typename TInterface, TInterface TEnd, TInterface TBegin>
961 result.
Import( lhs.Export() | rhs.Export() );
963 for (
int w = 0; w < alib::lang::TBitSet<TInterface,TEnd,TBegin>::QtyWords; ++w)
964 result.
Import( lhs.Export(w) | rhs.Export(w), w );
980template<
typename TInterface, TInterface TEnd, TInterface TBegin>
988 result.
Import( lhs.Export() ^ rhs.Export() );
990 for (
int w = 0; w < alib::lang::TBitSet<TInterface,TEnd,TBegin>::QtyWords; ++w)
991 result.
Import( lhs.Export(w) ^ rhs.Export(w), w );
constexpr Reference(TBitSetCM &set, TInterface b) noexcept
constexpr const class TBitSet & TBitSet() const noexcept
constexpr bool operator~() noexcept
constexpr bool operator==(const Reference &rhs) const noexcept=default
TBitSetCM * bitSet
The TBitSet this reference addresses.
TInterface bit
The bit number in bitSet this reference addresses.
Reference & operator=(const Reference &other) noexcept=default
constexpr TInterface Bit() const noexcept
constexpr Reference & Flip() noexcept
constexpr class TBitSet & TBitSet() noexcept
constexpr Reference(const Reference &) noexcept=default
Defaulted copy constructor.
constexpr Reference & operator=(bool val) noexcept
~Reference() noexcept
Destructor. (Is declared constexpr with C++23.)
int difference_type
Implementation of std::iterator_traits.
bool operator<=(TBidiIterator other) const
std::bidirectional_iterator_tag iterator_category
Implementation of std::iterator_traits.
bool operator<(TBidiIterator other) const
void pointer
Implementation of std::iterator_traits.
bool operator==(const TBidiIterator &other) const
TBidiIterator operator--(int)
TBidiIterator operator++(int)
TInterface value_type
Implementation of std::iterator_traits.
TBidiIterator & operator--()
bool operator>(TBidiIterator other) const
Reference< TBitSetCM > ref
bool operator>=(TBidiIterator other) const
TBidiIterator & operator++()
TBidiIterator(TBitSetCM &bitSet, TInterface startBit)
bool operator!=(const TBidiIterator &other) const
TBidiIterator< const TBitSet, false > ConstIterator
ConstIterator cend() const
constexpr bool All() const noexcept
constexpr TBitSet & operator^=(const TBitSet &rhs) noexcept
constexpr bool None() const noexcept
constexpr int Count() const noexcept
constexpr TBitSet operator>>(int cnt) const noexcept
TBidiIterator< TBitSet, true > ReverseIterator
constexpr bool operator==(const TBitSet &rhs) const noexcept
constexpr TBitSet & operator>>=(int cnt) noexcept
TBidiIterator< TBitSet, false > Iterator
TWord words[QtyWords ? QtyWords :1]
constexpr TWord & Export(int wordIdx) noexcept
constexpr bool Any() const noexcept
constexpr TBitSet & Reset(TInterface bit, T &&... args) noexcept
constexpr TBitSet & Set(TInterface bit, bool val) noexcept
ConstReverseIterator rend() const
constexpr TWord & word(TInterface b) noexcept
constexpr TBitSet & operator<<=(int cnt) noexcept
TBidiIterator< const TBitSet, true > ConstReverseIterator
constexpr Reference< TBitSet > operator[](TInterface bit) noexcept
constexpr TBitSet & Reset() noexcept
Iterator begin(int skip=0)
constexpr TBitSet(const TOmitInitialization &) noexcept
ConstReverseIterator rbegin(int skip=0) const
constexpr TBitSet operator<<(int cnt) const noexcept
constexpr TBitSet() noexcept
constexpr bool Test(TInterface bit) noexcept
static constexpr TWord mask1101(TInterface b) noexcept
ConstIterator cbegin(int skip=0) const
ConstIterator end() const
ConstIterator begin(int skip=0) const
constexpr const TWord & word(TInterface b) const noexcept
ATMP_IF_T_F((Capacity > bitsof(long)), unsigned long long, ATMP_IF_T_F((Capacity > bitsof(int)), unsigned long, ATMP_IF_T_F((Capacity > bitsof(short)), unsigned int, ATMP_IF_T_F((Capacity > bitsof(char)), unsigned short, unsigned char)))) TWord
static constexpr int QtyWords
static constexpr int bitIdx(TInterface b) noexcept
constexpr bool operator!=(const TBitSet &rhs) const noexcept
constexpr const Reference< const TBitSet > operator[](int bit) const noexcept
ReverseIterator rbegin(int skip=0)
static constexpr int Capacity
constexpr TBitSet & operator|=(const TBitSet &rhs) noexcept
static constexpr TWord mask0010(TInterface b) noexcept
constexpr TBitSet & Flip() noexcept
ConstReverseIterator crbegin(int skip=0) const
constexpr TBitSet & Set(TInterface bit, T &&... args) noexcept
constexpr TWord & Import(TWord val, int wordIdx) noexcept
constexpr TBitSet & operator&=(const TBitSet &rhs) noexcept
static constexpr int wordIdx(TInterface b) noexcept
constexpr const TWord & Export(int wordIdx) const noexcept
ConstReverseIterator crend() const
constexpr TBitSet & Set() noexcept
constexpr TBitSet operator~() const noexcept
constexpr TBitSet(TWord preset) noexcept
constexpr void shiftLeft(int cnt, TBitSet &target) const noexcept
constexpr TBitSet(bool preset) noexcept
constexpr void shiftRight(int cnt, TBitSet &target) const noexcept
constexpr TBitSet & Flip(TInterface bit, T &&... args) noexcept
static constexpr TWord mask(int wIdx) noexcept
#define ATMP_IF_T_F( Cond, T, F)
#define ALIB_WARNINGS_RESTORE
#define ALIB_ASSERT_ERROR(cond,...)
#define ALIB_WARNINGS_ALLOW_UNSAFE_BUFFER_USAGE
constexpr int BitCount(TIntegral value)
constexpr alib::lang::TBitSet< TInterface, TEnd, TBegin > operator^(const alib::lang::TBitSet< TInterface, TEnd, TBegin > &lhs, const alib::lang::TBitSet< TInterface, TEnd, TBegin > &rhs) noexcept
constexpr alib::lang::TBitSet< TInterface, TEnd, TBegin > operator|(const alib::lang::TBitSet< TInterface, TEnd, TBegin > &lhs, const alib::lang::TBitSet< TInterface, TEnd, TBegin > &rhs) noexcept
constexpr alib::lang::TBitSet< TInterface, TEnd, TBegin > operator&(const alib::lang::TBitSet< TInterface, TEnd, TBegin > &lhs, const alib::lang::TBitSet< TInterface, TEnd, TBegin > &rhs) noexcept