9#ifndef HPP_ALIB_LANG_BITSET
10#define HPP_ALIB_LANG_BITSET 1
15namespace alib {
namespace lang {
68template<
typename TInterface, TInterface TEnd, TInterface TBegin= 0>
74 static_assert( !(TBegin > TEnd),
"First Idx greater or equal than last index + 1" );
77 template<
typename TBitSetCM,
bool isReverse>
class TBidiIterator;
84 static constexpr int Capacity=
static_cast<int>( TEnd )
85 -
static_cast<int>( TBegin );
110 constexpr static int wordIdx(TInterface b)
noexcept
111 {
return (
static_cast<int>(b) -
static_cast<int>(TBegin)) /
bitsof(
TWord); }
116 constexpr static int bitIdx (TInterface b)
noexcept
119 &&
static_cast<int>(b) <
static_cast<int>(TEnd),
120 "ALIB/BITS",
"Given bit index out of bounds: ",
static_cast<int>(b) )
121 return (
static_cast<int>(b) -
static_cast<int>(TBegin)) %
bitsof(
TWord);
153 constexpr static TWord mask1101(TInterface b)
noexcept {
return ~mask0010(b); }
162 template<
typename TBitSetCM>
167 template<
typename T,
bool isReverse>
friend class TBidiIterator;
179 constexpr Reference(TBitSetCM& set, TInterface b) noexcept
197 constexpr TInterface
Bit() const noexcept
198 {
return TInterface(
bit); }
218 constexpr operator bool() noexcept
231 #if ALIB_CPP_STANDARD >= 20 || DOXYGEN
240 return bit == rhs.bit
256 constexpr TBitSet(
const std::nullptr_t&)
noexcept {}
286 template <
typename... T>
299 template <
typename... T>
301 TBitSet&
Set( TInterface firstBit, T&&... furtherBits )
noexcept
304 if constexpr (
sizeof...(furtherBits) > 0)
313 template <
typename... T>
318 if constexpr (
sizeof...(furtherBits) > 0)
319 Reset(furtherBits...);
327 template <
typename... T>
332 if constexpr (
sizeof...(furtherBits) > 0)
333 Flip(furtherBits...);
350 bool Test( TInterface bit )
noexcept
424 template<
int TEnableif=QtyWords>
typename std::enable_if<TEnableif==1, TWord&>::type
Export() noexcept {
return words[ 0]; }
425 template<
int TEnableif=QtyWords>
typename std::enable_if<TEnableif==1,const TWord >::type
Export() const noexcept {
return words[ 0]; }
426 template<
int TEnableif=QtyWords>
typename std::enable_if<TEnableif!=1, TWord&>::type
Export(
int wordIdx)
noexcept {
428 template<
int TEnableif=
QtyWords> typename std::enable_if<TEnableif!=1,const
TWord >::type
Export(
int wordIdx) const noexcept {
451 template<
int TEnableif=QtyWords>
452 typename std::enable_if<TEnableif==1,void>::type
457 template<
int TEnableif=QtyWords>
458 typename std::enable_if<TEnableif!=1,void>::type
462 "Index out of bounds." )
474 if(
words[w] != rhs.words[w] )
479 #if ALIB_CPP_STANDARD < 20 || DOXYGEN
520 ALIB_ASSERT_ERROR( cnt >= 0,
"ALIB/BITS",
"Negative value for TBitSet shift operation given." )
532 for(
int w=
QtyWords -1; w >= 0; --w )
533 target.words[w]= w - offW >= 0 ?
words[w - offW]
537 for(
int w=
QtyWords -1; w >= 0; --w )
538 target.words[w]= w >= offW ? (
TWord(
words[w - offW] << offB)
552 ALIB_ASSERT_ERROR( cnt >= 0,
"ALIB/BITS",
"Negative value for TBitSet shift operation given." )
634 template<
typename TBitSetCM,
bool isReverse>
656 ref.bit= TInterface(
static_cast<int>(
ref.bit) + 1);
659 while(
static_cast<int>(
ref.bit) <
static_cast<int>(TEnd))
671 if(
static_cast<int>(
ref.bit) >
static_cast<int>(TEnd))
672 ref.bit= TInterface(TEnd);
677 ref.bit= TInterface(
static_cast<int>(
ref.bit) + trailingZeros - bIdx);
689 ref.bit= TInterface(
static_cast<int>(
ref.bit) - 1);
692 while(
static_cast<int>(
ref.bit) >=
static_cast<int>(TBegin))
705 ref.bit= TInterface(
static_cast<int>(
ref.bit) - bIdx -1);
706 if(
static_cast<int>(
ref.bit) <
static_cast<int>(TBegin) -1)
707 ref.bit= TInterface(
static_cast<int>(TBegin) - 1);
712 ref.bit= TInterface(
static_cast<int>(
ref.bit)
725 :
ref(bitSet, startBit)
733 if constexpr (!isReverse)
return up();
760 return !(*
this == other);
776 if constexpr (isReverse)
return up();
795 {
return ref.bit < other.
ref.bit; }
802 {
return ref.bit <= other.
ref.bit; }
809 {
return ref.bit > other.
ref.bit; }
816 {
return ref.bit >= other.
ref.bit; }
897DOX_MARKER([DOX_MANUAL_ALIASES_BITSET])
899template<
int TEnd,
int TBegin= 0,
typename TInterface=
int>
901DOX_MARKER([DOX_MANUAL_ALIASES_BITSET])
921template<
typename TInterface, TInterface TEnd, TInterface TBegin>
929 result.
Import( lhs.Export() & rhs.Export() );
931 for (
int w = 0; w < alib::lang::TBitSet<TInterface,TEnd,TBegin>::QtyWords; ++w)
932 result.
Import( lhs.Export(w) & rhs.Export(w), w );
946template<
typename TInterface, TInterface TEnd, TInterface TBegin>
954 result.
Import( lhs.Export() | rhs.Export() );
956 for (
int w = 0; w < alib::lang::TBitSet<TInterface,TEnd,TBegin>::QtyWords; ++w)
957 result.
Import( lhs.Export(w) | rhs.Export(w), w );
971template<
typename TInterface, TInterface TEnd, TInterface TBegin>
979 result.
Import( lhs.Export() ^ rhs.Export() );
981 for (
int w = 0; w < alib::lang::TBitSet<TInterface,TEnd,TBegin>::QtyWords; ++w)
982 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
The constant iterator type.
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
Same as Iterator, but working from the end to the start of the TBitSet.
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 TBitSet & Set(TInterface firstBit, T &&... furtherBits) noexcept
constexpr bool Any() const noexcept
constexpr TBitSet & Reset(TInterface firstBit, T &&... furtherBits) 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
Same as ConstIterator, but working from the end to the start of the TBitSet.
constexpr Reference< TBitSet > operator[](TInterface bit) noexcept
constexpr TBitSet & Reset() noexcept
Sets all bits to 0.
Iterator begin(int skip=0)
ConstReverseIterator rbegin(int skip=0) const
constexpr TBitSet operator<<(int cnt) const noexcept
constexpr TBitSet() noexcept
Default constructor initializing all bits to not set.
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 TBitSet(const std::nullptr_t &) noexcept
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
The size of the TWord array containing the bits.
static constexpr int bitIdx(TInterface b) noexcept
constexpr TBitSet & Flip(TInterface firstBit, T &&... furtherBits) 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
Flips all bits from 0 to 1 and vice versa.
ConstReverseIterator crbegin(int skip=0) const
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
Sets all bits to 1.
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
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 int CLZ(TIntegral value)
constexpr TIntegral LowerMask()
constexpr int CTZ(TIntegral value)
constexpr TIntegral UpperMask()
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