Compare commits

...

3 Commits

7 changed files with 761 additions and 68 deletions

View File

@ -15,6 +15,7 @@ void TestContainers()
TestStaticArray();
TestArrayView();
TestBitset();
TestStaticBitset();
}
NAMESPACE_UNNAMED_BEGIN
@ -263,26 +264,6 @@ void TestBitset()
always_check((BitsetI == FBitset({ true, false, true, false })));
}
{
FBitset BitsetA = { false, true, false };
FBitset BitsetB = { true, false, true, false };
FBitset BitsetC = { false, true, false };
always_check((!(BitsetA == BitsetB)));
always_check(( (BitsetA != BitsetB)));
always_check(( (BitsetA < BitsetB)));
always_check(( (BitsetA <= BitsetB)));
always_check((!(BitsetA > BitsetB)));
always_check((!(BitsetA >= BitsetB)));
always_check(( (BitsetA == BitsetC)));
always_check((!(BitsetA != BitsetC)));
always_check((!(BitsetA < BitsetC)));
always_check(( (BitsetA <= BitsetC)));
always_check((!(BitsetA > BitsetC)));
always_check(( (BitsetA >= BitsetC)));
}
{
FBitset BitsetA(64, 0x0139'0239'0339'0439ull);
uint64 IntA = 0x0139'0239'0339'0439ull;
@ -330,7 +311,7 @@ void TestBitset()
{
FBitset Bitset(64, 0x0139'0239'0339'0439ull);
uint64 Int = 0x0139'0239'0339'0439ull;
uint64 Int = 0x0139'0239'0339'0439ull;
always_check(((Bitset << 40).ToIntegral() == (Int << 40)));
always_check(((Bitset >> 40).ToIntegral() == (Int >> 40)));
@ -401,6 +382,131 @@ void TestBitset()
}
}
void TestStaticBitset()
{
{
TStaticBitset< 0> BitsetA;
TStaticBitset<16> BitsetB;
TStaticBitset<16> BitsetC(0b1010'0100'0100'0010);
TStaticBitset<16> BitsetD(BitsetC);
TStaticBitset<16> BitsetE(MoveTemp(BitsetB));
TStaticBitset< 4> BitsetF(0b0101);
TStaticBitset<16> BitsetG;
TStaticBitset<16> BitsetH;
TStaticBitset< 4> BitsetI;
BitsetG = BitsetD;
BitsetH = MoveTemp(BitsetE);
BitsetI = 0b0101;
always_check((BitsetF == TStaticBitset<4>(0b0101)));
always_check((BitsetI == TStaticBitset<4>(0b0101)));
}
{
TStaticBitset<32> BitsetA(0x0139'0239ull);
uint32 IntA = 0x0139'0239ull;
TStaticBitset<32> BitsetB(0x017F'027Full);
uint32 IntB = 0x017F'027Full;
TStaticBitset<32> BitsetANDA = BitsetA; BitsetANDA &= BitsetB;
TStaticBitset<32> BitsetANDB = BitsetB; BitsetANDB &= BitsetA;
TStaticBitset<32> BitsetORA = BitsetA; BitsetORA &= BitsetB;
TStaticBitset<32> BitsetORB = BitsetB; BitsetORB &= BitsetA;
TStaticBitset<32> BitsetXORA = BitsetA; BitsetXORA &= BitsetB;
TStaticBitset<32> BitsetXORB = BitsetB; BitsetXORB &= BitsetA;
uint32 IntANDA = IntA; IntANDA &= IntB;
uint32 IntANDB = IntB; IntANDB &= IntA;
uint32 IntORA = IntA; IntORA &= IntB;
uint32 IntORB = IntB; IntORB &= IntA;
uint32 IntXORA = IntA; IntXORA &= IntB;
uint32 IntXORB = IntB; IntXORB &= IntA;
always_check((BitsetANDA.ToIntegral() == IntANDA));
always_check((BitsetANDB.ToIntegral() == IntANDB));
always_check((BitsetORA.ToIntegral() == IntORA));
always_check((BitsetORB.ToIntegral() == IntORB));
always_check((BitsetXORA.ToIntegral() == IntXORA));
always_check((BitsetXORB.ToIntegral() == IntXORB));
}
{
TStaticBitset<32> BitsetA(0x0139'0239ull);
uint32 IntA = 0x0139'0239ull;
TStaticBitset<32> BitsetB(0x017F'027Full);
uint32 IntB = 0x017F'027Full;
always_check(((BitsetA & BitsetB).ToIntegral() == (IntA & IntB)));
always_check(((BitsetA | BitsetB).ToIntegral() == (IntA | IntB)));
always_check(((BitsetA ^ BitsetB).ToIntegral() == (IntA ^ IntB)));
}
{
TStaticBitset<64> Bitset(0x0139'0239'0339'0439ull);
uint64 Int = 0x0139'0239'0339'0439ull;
always_check(((Bitset << 40).ToIntegral() == (Int << 40)));
always_check(((Bitset >> 40).ToIntegral() == (Int >> 40)));
}
{
TStaticBitset<4> BitsetA(0b0000ull);
TStaticBitset<4> BitsetB(0b0101ull);
TStaticBitset<4> BitsetC(0b1111ull);
always_check((!BitsetA.All() && !BitsetA.Any() && BitsetA.None()));
always_check((!BitsetB.All() && BitsetB.Any() && !BitsetB.None()));
always_check(( BitsetC.All() && BitsetC.Any() && !BitsetC.None()));
}
{
TStaticBitset<16> Bitset;
Bitset.Set();
always_check((Bitset.Count() == 16));
Bitset.Flip(8);
always_check((Bitset.Count() == 15));
Bitset.Flip(8);
always_check((Bitset.Count() == 16));
Bitset.Flip();
always_check((Bitset.Count() == 0));
}
{
TStaticBitset<4> BitsetA;
TStaticBitset<4> BitsetB;
BitsetA[0] = true;
BitsetA[1] = false;
BitsetA[2] = true;
BitsetA[3] = false;
BitsetB[0] = true;
BitsetB[1] = false;
BitsetB[2] = true;
BitsetB[3] = false;
Swap(BitsetA, BitsetB);
always_check((GetTypeHash(BitsetA) == GetTypeHash(BitsetB)));
}
}
NAMESPACE_END(Testing)
NAMESPACE_MODULE_END(Utility)

View File

@ -304,18 +304,15 @@ public:
return true;
}
/** Compares the contents of two arrays. */
/** Compares the contents of 'LHS' and 'RHS' lexicographically. */
NODISCARD friend auto operator<=>(const TArray& LHS, const TArray& RHS) requires (CSynthThreeWayComparable<ElementType>)
{
using OrderingType = TSynthThreeWayResult<ElementType>;
if (LHS.Num() < RHS.Num()) return OrderingType::less;
if (LHS.Num() > RHS.Num()) return OrderingType::greater;
ConstIterator LHSIter = LHS.Begin();
ConstIterator RHSIter = RHS.Begin();
while (LHSIter != LHS.End())
while (LHSIter != LHS.End() || RHSIter != RHS.End())
{
TSynthThreeWayResult<ElementType> Ordering = SynthThreeWayCompare(*LHSIter, *RHSIter);
@ -325,9 +322,7 @@ public:
++RHSIter;
}
check(RHSIter == RHS.End());
return OrderingType::equivalent;
return LHS.Num() <=> RHS.Num();
}
/** Inserts 'InValue' before 'Iter' in the container. */

View File

@ -280,26 +280,6 @@ public:
return (LHS.Impl.Pointer[LHS.NumBlocks() - 1] & LastBlockBitmask) == (RHS.Impl.Pointer[LHS.NumBlocks() - 1] & LastBlockBitmask);
}
/** Compares the bits of two bitsets. */
NODISCARD friend strong_ordering operator<=>(const TBitset& LHS, const TBitset& RHS)
{
if (LHS.Num() < RHS.Num()) return strong_ordering::less;
if (LHS.Num() > RHS.Num()) return strong_ordering::greater;
if (LHS.NumBlocks() == 0) return strong_ordering::equivalent;
for (size_t Index = 0; Index != LHS.NumBlocks() - 1; ++Index)
{
strong_ordering Ordering = LHS.Impl.Pointer[Index] <=> RHS.Impl.Pointer[Index];
if (Ordering != strong_ordering::equivalent) return Ordering;
}
const BlockType LastBlockBitmask = LHS.Num() % BlockWidth != 0 ? (1ull << LHS.Num() % BlockWidth) - 1 : -1;
return (LHS.Impl.Pointer[LHS.NumBlocks() - 1] & LastBlockBitmask) <=> (RHS.Impl.Pointer[LHS.NumBlocks() - 1] & LastBlockBitmask);
}
/** Sets the bits to the result of binary AND on corresponding pairs of bits of *this and other. */
TBitset& operator&=(const TBitset& InValue)
{
@ -531,7 +511,7 @@ public:
size_t Result = 0;
static constexpr auto BlockCount = [](BlockType Block)
constexpr auto BlockCount = [](BlockType Block)
{
static_assert(sizeof(BlockType) <= sizeof(uint64), "The block width of TBitset is unexpected");
@ -842,7 +822,7 @@ public:
/** @return The reference to the first or last bit. */
NODISCARD FORCEINLINE Reference Front() { return *Begin(); }
NODISCARD FORCEINLINE ConstReference Front() const { return *Begin(); }
NODISCARD FORCEINLINE Reference Back() { return *(End() - 1); }
NODISCARD FORCEINLINE Reference Back() { return *(End() - 1); }
NODISCARD FORCEINLINE ConstReference Back() const { return *(End() - 1); }
/** Erases all bits from the bitset. After this call, Num() returns zero. */
@ -959,11 +939,11 @@ private:
FORCEINLINE IteratorImpl() = default;
# if DO_CHECK
FORCEINLINE IteratorImpl(const IteratorImpl<false> &InValue) requires (bConst)
FORCEINLINE IteratorImpl(const IteratorImpl<false>& InValue) requires (bConst)
: Owner(InValue.Owner), Pointer(InValue.Pointer), Offset(InValue.Offset)
{ }
# else
FORCEINLINE IteratorImpl(const IteratorImpl<false> &InValue) requires (bConst)
FORCEINLINE IteratorImpl(const IteratorImpl<false>& InValue) requires (bConst)
: Pointer(InValue.Pointer), Offset(InValue.Offset)
{ }
# endif
@ -973,9 +953,9 @@ private:
FORCEINLINE IteratorImpl& operator=(const IteratorImpl&) = default;
FORCEINLINE IteratorImpl& operator=(IteratorImpl&&) = default;
NODISCARD friend FORCEINLINE bool operator==(const IteratorImpl& LHS, const IteratorImpl& RHS) { return LHS.Pointer == RHS.Pointer && LHS.Offset == RHS.Offset; }
NODISCARD friend FORCEINLINE bool operator==(const IteratorImpl& LHS, const IteratorImpl& RHS) { check(LHS.Pointer == RHS.Pointer); return LHS.Offset == RHS.Offset; }
NODISCARD friend FORCEINLINE strong_ordering operator<=>(const IteratorImpl& LHS, const IteratorImpl& RHS) { return 0 <=> LHS.Offset - RHS.Offset; }
NODISCARD friend FORCEINLINE strong_ordering operator<=>(const IteratorImpl& LHS, const IteratorImpl& RHS) { check(LHS.Pointer == RHS.Pointer); return LHS.Offset <=> RHS.Offset; }
NODISCARD FORCEINLINE Reference operator*() const requires (!bConst) { CheckThis(true); return Reference(*(Pointer + Offset / BlockWidth), 1ull << Offset % BlockWidth); }
NODISCARD FORCEINLINE ConstReference operator*() const requires ( bConst) { CheckThis(true); return (*(Pointer + Offset / BlockWidth) & (1ull << Offset % BlockWidth)); }
@ -996,7 +976,7 @@ private:
NODISCARD FORCEINLINE IteratorImpl operator-(ptrdiff Offset) const { IteratorImpl Temp = *this; Temp -= Offset; return Temp; }
NODISCARD friend FORCEINLINE ptrdiff operator-(const IteratorImpl& LHS, const IteratorImpl& RHS) { return (reinterpret_cast<uintptr>(LHS.Pointer) * BlockWidth + LHS.Offset) - (reinterpret_cast<uintptr>(RHS.Pointer) * BlockWidth + RHS.Offset); }
NODISCARD friend FORCEINLINE ptrdiff operator-(const IteratorImpl& LHS, const IteratorImpl& RHS) { check(LHS.Pointer == RHS.Pointer); return LHS.Offset - RHS.Offset; }
private:
@ -1004,15 +984,17 @@ private:
const TBitset* Owner = nullptr;
# endif
BlockType* Pointer = nullptr;
size_t Offset = 0;
using BlockPtr = TConditional<bConst, const BlockType*, BlockType*>;
BlockPtr Pointer = nullptr;
size_t Offset = 0;
# if DO_CHECK
FORCEINLINE IteratorImpl(const TBitset* InContainer, BlockType* InPointer, size_t InOffset)
FORCEINLINE IteratorImpl(const TBitset* InContainer, BlockPtr InPointer, size_t InOffset)
: Owner(InContainer), Pointer(InPointer), Offset(InOffset)
{ }
# else
FORCEINLINE IteratorImpl(const TBitset* InContainer, BlockType* InPointer, size_t InOffset)
FORCEINLINE IteratorImpl(const TBitset* InContainer, BlockPtr InPointer, size_t InOffset)
: Pointer(InPointer), Offset(InOffset)
{ }
# endif

View File

@ -6,3 +6,4 @@
#include "Containers/StaticArray.h"
#include "Containers/ArrayView.h"
#include "Containers/Bitset.h"
#include "Containers/StaticBitset.h"

View File

@ -62,18 +62,15 @@ public:
return true;
}
/** Compares the contents of two arrays. */
/** Compares the contents of 'LHS' and 'RHS' lexicographically. */
NODISCARD friend constexpr auto operator<=>(const TStaticArray& LHS, const TStaticArray& RHS) requires (CSynthThreeWayComparable<ElementType>)
{
using OrderingType = TSynthThreeWayResult<ElementType>;
if (LHS.Num() < RHS.Num()) return OrderingType::less;
if (LHS.Num() > RHS.Num()) return OrderingType::greater;
ConstIterator LHSIter = LHS.Begin();
ConstIterator RHSIter = RHS.Begin();
while (LHSIter != LHS.End())
while (LHSIter != LHS.End() || RHSIter != RHS.End())
{
TSynthThreeWayResult<ElementType> Ordering = SynthThreeWayCompare(*LHSIter, *RHSIter);
@ -83,9 +80,7 @@ public:
++RHSIter;
}
check(RHSIter == RHS.End());
return OrderingType::equivalent;
return LHS.Num() <=> RHS.Num();
}
/** @return The pointer to the underlying element storage. */

View File

@ -0,0 +1,613 @@
#pragma once
#include "CoreTypes.h"
#include "Templates/Utility.h"
#include "Templates/TypeHash.h"
#include "Templates/Container.h"
#include "Containers/Iterator.h"
#include "Templates/Noncopyable.h"
#include "TypeTraits/TypeTraits.h"
#include "Miscellaneous/Compare.h"
#include "Memory/MemoryOperator.h"
#include "Miscellaneous/AssertionMacros.h"
NAMESPACE_REDCRAFT_BEGIN
NAMESPACE_MODULE_BEGIN(Redcraft)
NAMESPACE_MODULE_BEGIN(Utility)
NAMESPACE_PRIVATE_BEGIN
template <size_t N>
using TDefaultBitsetBlockType =
TConditional<(N <= 8), uint8,
TConditional<(N <= 16), uint16,
TConditional<(N <= 32), uint32, uint64>>>;
NAMESPACE_PRIVATE_END
#if 1
template <size_t N, CUnsignedIntegral InBlockType = NAMESPACE_PRIVATE::TDefaultBitsetBlockType<N>> requires (!CSameAs<InBlockType, bool>)
class TStaticBitset final
{
private:
template <bool bConst>
class IteratorImpl;
public:
using BlockType = InBlockType;
using ElementType = bool;
class Reference;
using ConstReference = bool;
using Iterator = IteratorImpl<false>;
using ConstIterator = IteratorImpl<true >;
using ReverseIterator = TReverseIterator< Iterator>;
using ConstReverseIterator = TReverseIterator<ConstIterator>;
static_assert(CRandomAccessIterator< Iterator>);
static_assert(CRandomAccessIterator<ConstIterator>);
static constexpr size_t BlockWidth = sizeof(BlockType) * 8;
/** Default constructor. Constructs an empty bitset. */
FORCEINLINE constexpr TStaticBitset() = default;
/** Constructs a bitset from an integer. */
constexpr TStaticBitset(uint64 InValue)
{
static_assert(sizeof(BlockType) <= sizeof(uint64), "The block width of TStaticBitset is unexpected");
if constexpr (sizeof(BlockType) == sizeof(uint8))
{
if constexpr (N > 0) Impl[0] = static_cast<BlockType>(InValue >> 0);
if constexpr (N > 8) Impl[1] = static_cast<BlockType>(InValue >> 8);
if constexpr (N > 16) Impl[2] = static_cast<BlockType>(InValue >> 16);
if constexpr (N > 24) Impl[3] = static_cast<BlockType>(InValue >> 24);
if constexpr (N > 32) Impl[4] = static_cast<BlockType>(InValue >> 32);
if constexpr (N > 40) Impl[5] = static_cast<BlockType>(InValue >> 40);
if constexpr (N > 48) Impl[6] = static_cast<BlockType>(InValue >> 48);
if constexpr (N > 56) Impl[7] = static_cast<BlockType>(InValue >> 56);
}
else if constexpr (sizeof(BlockType) == sizeof(uint16))
{
if constexpr (N > 0) Impl[0] = static_cast<BlockType>(InValue >> 0);
if constexpr (N > 16) Impl[1] = static_cast<BlockType>(InValue >> 16);
if constexpr (N > 32) Impl[2] = static_cast<BlockType>(InValue >> 32);
if constexpr (N > 48) Impl[3] = static_cast<BlockType>(InValue >> 48);
}
else if constexpr (sizeof(BlockType) == sizeof(uint32))
{
if constexpr (N > 0) Impl[0] = static_cast<BlockType>(InValue >> 0);
if constexpr (N > 32) Impl[1] = static_cast<BlockType>(InValue >> 32);
}
else if constexpr (sizeof(BlockType) == sizeof(uint64))
{
if constexpr (N > 0) Impl[0] = static_cast<BlockType>(InValue >> 0);
}
else check_no_entry();
constexpr size_t BlockInteger = sizeof(uint64) / sizeof(BlockType);
if constexpr ((N + BlockWidth - 1) / BlockWidth <= BlockInteger) return;
for (size_t Index = BlockInteger; Index != NumBlocks(); ++Index)
{
Impl[Index] = 0;
}
}
/** Copy constructor. Constructs the bitset with the copy of the bits of 'InValue'. */
FORCEINLINE constexpr TStaticBitset(const TStaticBitset&) = default;
/** Move constructor. After the move, 'InValue' is guaranteed to be empty. */
FORCEINLINE constexpr TStaticBitset(TStaticBitset&&) = default;
/** Destructs the bitset. The storage is deallocated. */
FORCEINLINE constexpr ~TStaticBitset() = default;
/** Copy assignment operator. Replaces the bits with a copy of the bits of 'InValue'. */
FORCEINLINE constexpr TStaticBitset& operator=(const TStaticBitset&) = default;
/** Move assignment operator. After the move, 'InValue' is guaranteed to be empty. */
FORCEINLINE constexpr TStaticBitset& operator=(TStaticBitset&&) = default;
/** Compares the bits of two bitsets. */
NODISCARD friend constexpr bool operator==(const TStaticBitset& LHS, const TStaticBitset& RHS)
{
if constexpr (N == 0) return true;
for (size_t Index = 0; Index != LHS.NumBlocks() - 1; ++Index)
{
if (LHS.Impl[Index] != RHS.Impl[Index]) return false;
}
const BlockType LastBlockBitmask = LHS.Num() % BlockWidth != 0 ? (1ull << LHS.Num() % BlockWidth) - 1 : -1;
return (LHS.Impl[LHS.NumBlocks() - 1] & LastBlockBitmask) == (RHS.Impl[LHS.NumBlocks() - 1] & LastBlockBitmask);
}
/** Sets the bits to the result of binary AND on corresponding pairs of bits of *this and other. */
constexpr TStaticBitset& operator&=(const TStaticBitset& InValue)
{
if constexpr (N == 0) return *this;
if (&InValue == this) UNLIKELY return *this;
for (size_t Index = 0; Index != NumBlocks(); ++Index)
{
Impl[Index] &= InValue.Impl[Index];
}
return *this;
}
/** Sets the bits to the result of binary OR on corresponding pairs of bits of *this and other. */
constexpr TStaticBitset& operator|=(const TStaticBitset& InValue)
{
if constexpr (N == 0) return *this;
if (&InValue == this) UNLIKELY return *this;
for (size_t Index = 0; Index != NumBlocks(); ++Index)
{
Impl[Index] |= InValue.Impl[Index];
}
return *this;
}
/** Sets the bits to the result of binary XOR on corresponding pairs of bits of *this and other. */
constexpr TStaticBitset& operator^=(const TStaticBitset& InValue)
{
if constexpr (N == 0) return *this;
if (&InValue == this) UNLIKELY return Set(false);
for (size_t Index = 0; Index != NumBlocks(); ++Index)
{
Impl[Index] ^= InValue.Impl[Index];
}
return *this;
}
NODISCARD friend FORCEINLINE constexpr TStaticBitset operator&(const TStaticBitset& LHS, const TStaticBitset& RHS) { return TStaticBitset(LHS) &= RHS; }
NODISCARD friend FORCEINLINE constexpr TStaticBitset operator|(const TStaticBitset& LHS, const TStaticBitset& RHS) { return TStaticBitset(LHS) |= RHS; }
NODISCARD friend FORCEINLINE constexpr TStaticBitset operator^(const TStaticBitset& LHS, const TStaticBitset& RHS) { return TStaticBitset(LHS) ^= RHS; }
/** @return The temporary copy of *this with all bits flipped (binary NOT). */
NODISCARD constexpr TStaticBitset operator~() const
{
TStaticBitset Result = *this;
for (size_t Index = 0; Index != NumBlocks(); ++Index)
{
Result.Impl[Index] = ~Result.Impl[Index];
}
return Result;
}
/** Performs binary shift left. */
constexpr TStaticBitset& operator<<=(size_t Offset)
{
if constexpr (N == 0) return *this;
const size_t Blockshift = Offset / BlockWidth;
const size_t Bitshift = Offset % BlockWidth;
if (Blockshift != 0)
{
for (size_t Index = NumBlocks() - 1; Index != -1; --Index)
{
Impl[Index] = Index >= Blockshift ? Impl[Index - Blockshift] : 0;
}
}
if (Bitshift != 0)
{
for (size_t Index = NumBlocks() - 1; Index != 0; --Index)
{
Impl[Index] = Impl[Index] << Bitshift | Impl[Index - 1] >> (BlockWidth - Bitshift);
}
Impl[0] <<= Bitshift;
}
return *this;
}
/** Performs binary shift right. */
constexpr TStaticBitset& operator>>=(size_t Offset)
{
if constexpr (N == 0) return *this;
const size_t Blockshift = Offset / BlockWidth;
const size_t Bitshift = Offset % BlockWidth;
if constexpr (N % BlockWidth != 0)
{
Impl[NumBlocks() - 1] &= (1ull << Num() % BlockWidth) - 1;
}
if (Blockshift != 0)
{
for (size_t Index = 0; Index != NumBlocks(); ++Index)
{
Impl[Index] = Index < NumBlocks() - Blockshift ? Impl[Index + Blockshift] : 0;
}
}
if (Bitshift != 0)
{
for (size_t Index = 0; Index != NumBlocks() - 1; ++Index)
{
Impl[Index] = Impl[Index] >> Bitshift | Impl[Index + 1] << (BlockWidth - Bitshift);
}
Impl[NumBlocks() - 1] >>= Bitshift;
}
return *this;
}
NODISCARD FORCEINLINE constexpr TStaticBitset operator<<(size_t Offset) const { return TStaticBitset(*this) <<= Offset; }
NODISCARD FORCEINLINE constexpr TStaticBitset operator>>(size_t Offset) const { return TStaticBitset(*this) >>= Offset; }
/** @return true if all bits are set to true, otherwise false. */
NODISCARD constexpr bool All() const
{
if constexpr (N == 0) return true;
for (size_t Index = 0; Index != NumBlocks() - 1; ++Index)
{
if (Impl[Index] != -1) return false;
}
const BlockType LastBlockBitmask = Num() % BlockWidth != 0 ? (1ull << Num() % BlockWidth) - 1 : -1;
return (Impl[NumBlocks() - 1] | ~LastBlockBitmask) == -1;
}
/** @return true if any of the bits are set to true, otherwise false. */
NODISCARD constexpr bool Any() const
{
if constexpr (N == 0) return false;
for (size_t Index = 0; Index != NumBlocks() - 1; ++Index)
{
if (Impl[Index] != 0) return true;
}
const BlockType LastBlockBitmask = Num() % BlockWidth != 0 ? (1ull << Num() % BlockWidth) - 1 : -1;
return (Impl[NumBlocks() - 1] & LastBlockBitmask) != 0;
}
/** @return true if none of the bits are set to true, otherwise false. */
NODISCARD FORCEINLINE constexpr bool None() const { return !Any(); }
/** @return The number of bits that are set to true. */
NODISCARD constexpr size_t Count() const
{
if constexpr (N == 0) return 0;
size_t Result = 0;
constexpr auto BlockCount = [](BlockType Block) constexpr
{
static_assert(sizeof(BlockType) <= sizeof(uint64), "The block width of TStaticBitset is unexpected");
if constexpr (sizeof(BlockType) == sizeof(uint8))
{
Block = (Block & 0x55ull) + ((Block >> 1) & 0x55ull);
Block = (Block & 0x33ull) + ((Block >> 2) & 0x33ull);
Block = (Block & 0x0Full) + ((Block >> 4) & 0x0Full);
}
else if constexpr (sizeof(BlockType) == sizeof(uint16))
{
Block = (Block & 0x5555ull) + ((Block >> 1) & 0x5555ull);
Block = (Block & 0x3333ull) + ((Block >> 2) & 0x3333ull);
Block = (Block & 0x0F0Full) + ((Block >> 4) & 0x0F0Full);
Block = (Block & 0x00FFull) + ((Block >> 8) & 0x00FFull);
}
else if constexpr (sizeof(BlockType) == sizeof(uint32))
{
Block = (Block & 0x55555555ull) + ((Block >> 1) & 0x55555555ull);
Block = (Block & 0x33333333ull) + ((Block >> 2) & 0x33333333ull);
Block = (Block & 0x0F0F0F0Full) + ((Block >> 4) & 0x0F0F0F0Full);
Block = (Block & 0x00FF00FFull) + ((Block >> 8) & 0x00FF00FFull);
Block = (Block & 0x0000FFFFull) + ((Block >> 16) & 0x0000FFFFull);
}
else if constexpr (sizeof(BlockType) == sizeof(uint64))
{
Block = (Block & 0x5555555555555555ull) + ((Block >> 1) & 0x5555555555555555ull);
Block = (Block & 0x3333333333333333ull) + ((Block >> 2) & 0x3333333333333333ull);
Block = (Block & 0x0F0F0F0F0F0F0F0Full) + ((Block >> 4) & 0x0F0F0F0F0F0F0F0Full);
Block = (Block & 0x00FF00FF00FF00FFull) + ((Block >> 8) & 0x00FF00FF00FF00FFull);
Block = (Block & 0x0000FFFF0000FFFFull) + ((Block >> 16) & 0x0000FFFF0000FFFFull);
Block = (Block & 0x00000000FFFFFFFFull) + ((Block >> 32) & 0x00000000FFFFFFFFull);
}
else check_no_entry();
return Block;
};
for (size_t Index = 0; Index != NumBlocks() - 1; ++Index)
{
Result += BlockCount(Impl[Index]);
}
const BlockType LastBlockBitmask = Num() % BlockWidth != 0 ? (1ull << Num() % BlockWidth) - 1 : -1;
Result += BlockCount(Impl[NumBlocks() - 1] & LastBlockBitmask);
return Result;
}
/** Sets all bits to true. */
constexpr TStaticBitset& Set(bool InValue = true)
{
for (size_t Index = 0; Index != NumBlocks(); ++Index)
{
Impl[Index] = static_cast<BlockType>(InValue ? -1 : 0);
}
return *this;
}
/** Flips all bits (like operator~, but in-place). */
constexpr TStaticBitset& Flip()
{
for (size_t Index = 0; Index != NumBlocks(); ++Index)
{
Impl[Index] = ~Impl[Index];
}
return *this;
}
/** Flips the bit at the position 'Index'. */
constexpr TStaticBitset& Flip(size_t Index)
{
Impl[Index / BlockWidth] ^= 1ull << Index % BlockWidth;
return *this;
}
/** Converts the contents of the bitset to an uint64 integer. */
NODISCARD constexpr uint64 ToIntegral()
{
checkf(Num() <= 64, TEXT("The bitset can not be represented in uint64. Please check Num()."));
uint64 Result = 0;
static_assert(sizeof(BlockType) <= sizeof(uint64), "The block width of TStaticBitset is unexpected");
if constexpr (sizeof(BlockType) == sizeof(uint8))
{
if constexpr (N > 0) Result |= static_cast<uint64>(Impl[0]) << 0;
if constexpr (N > 8) Result |= static_cast<uint64>(Impl[1]) << 8;
if constexpr (N > 16) Result |= static_cast<uint64>(Impl[2]) << 16;
if constexpr (N > 24) Result |= static_cast<uint64>(Impl[3]) << 24;
if constexpr (N > 32) Result |= static_cast<uint64>(Impl[4]) << 32;
if constexpr (N > 40) Result |= static_cast<uint64>(Impl[5]) << 40;
if constexpr (N > 48) Result |= static_cast<uint64>(Impl[6]) << 48;
if constexpr (N > 56) Result |= static_cast<uint64>(Impl[7]) << 56;
}
else if constexpr (sizeof(BlockType) == sizeof(uint16))
{
if constexpr (N > 0) Result |= static_cast<uint64>(Impl[0]) << 0;
if constexpr (N > 16) Result |= static_cast<uint64>(Impl[1]) << 16;
if constexpr (N > 32) Result |= static_cast<uint64>(Impl[2]) << 32;
if constexpr (N > 48) Result |= static_cast<uint64>(Impl[3]) << 48;
}
else if constexpr (sizeof(BlockType) == sizeof(uint32))
{
if constexpr (N > 0) Result |= static_cast<uint64>(Impl[0]) << 0;
if constexpr (N > 32) Result |= static_cast<uint64>(Impl[1]) << 32;
}
else if constexpr (sizeof(BlockType) == sizeof(uint64))
{
if constexpr (N > 0) Result |= static_cast<uint64>(Impl[0]) << 0;
}
else check_no_entry();
const uint64 Mask = Num() < 64 ? (1ull << Num()) - 1 : -1;
return Result & Mask;
}
/** @return The pointer to the underlying element storage. */
NODISCARD FORCEINLINE constexpr TObserverPtr< BlockType[]> GetData() { return TObserverPtr< BlockType[]>(Impl); }
NODISCARD FORCEINLINE constexpr TObserverPtr<const BlockType[]> GetData() const { return TObserverPtr<const BlockType[]>(Impl); }
/** @return The iterator to the first or end bit. */
NODISCARD FORCEINLINE constexpr Iterator Begin() { return Iterator(this, Impl, 0); }
NODISCARD FORCEINLINE constexpr ConstIterator Begin() const { return ConstIterator(this, Impl, 0); }
NODISCARD FORCEINLINE constexpr Iterator End() { return Iterator(this, Impl, Num()); }
NODISCARD FORCEINLINE constexpr ConstIterator End() const { return ConstIterator(this, Impl, Num()); }
/** @return The reverse iterator to the first or end bit. */
NODISCARD FORCEINLINE constexpr ReverseIterator RBegin() { return ReverseIterator(End()); }
NODISCARD FORCEINLINE constexpr ConstReverseIterator RBegin() const { return ConstReverseIterator(End()); }
NODISCARD FORCEINLINE constexpr ReverseIterator REnd() { return ReverseIterator(Begin()); }
NODISCARD FORCEINLINE constexpr ConstReverseIterator REnd() const { return ConstReverseIterator(Begin()); }
/** @return The number of bits in the bitset. */
NODISCARD FORCEINLINE constexpr size_t Num() const { return N; }
/** @return The number of blocks in the bitset. */
NODISCARD FORCEINLINE constexpr size_t NumBlocks() const { return (Num() + BlockWidth - 1) / BlockWidth; }
/** @return true if the bitset is empty, false otherwise. */
NODISCARD FORCEINLINE constexpr bool IsEmpty() const { return Num() == 0; }
/** @return true if the iterator is valid, false otherwise. */
NODISCARD FORCEINLINE constexpr bool IsValidIterator(ConstIterator Iter) const { return Begin() <= Iter && Iter <= End(); }
/** @return The reference to the requested bit. */
NODISCARD FORCEINLINE constexpr Reference operator[](size_t Index) { checkf(Index < Num(), TEXT("Read access violation. Please check IsValidIterator().")); return *(Begin() + Index); }
NODISCARD FORCEINLINE constexpr ConstReference operator[](size_t Index) const { checkf(Index < Num(), TEXT("Read access violation. Please check IsValidIterator().")); return *(Begin() + Index); }
/** @return The reference to the first or last bit. */
NODISCARD FORCEINLINE constexpr Reference Front() { return *Begin(); }
NODISCARD FORCEINLINE constexpr ConstReference Front() const { return *Begin(); }
NODISCARD FORCEINLINE constexpr Reference Back() { return *(End() - 1); }
NODISCARD FORCEINLINE constexpr ConstReference Back() const { return *(End() - 1); }
/** Overloads the GetTypeHash algorithm for TStaticBitset. */
NODISCARD friend FORCEINLINE constexpr size_t GetTypeHash(const TStaticBitset& A)
{
if constexpr (N == 0) return 1005426566;
size_t Result = 0;
for (size_t Index = 0; Index != A.NumBlocks() - 1; ++Index)
{
Result = HashCombine(Result, GetTypeHash(A.Impl[Index]));
}
const BlockType LastBlockBitmask = A.Num() % BlockWidth != 0 ? (1ull << A.Num() % BlockWidth) - 1 : -1;
return HashCombine(Result, GetTypeHash(A.Impl[A.NumBlocks() - 1] & LastBlockBitmask));
}
/** Overloads the Swap algorithm for TStaticBitset. */
friend constexpr void Swap(TStaticBitset& A, TStaticBitset& B) { Swap(A.Impl, B.Impl); }
ENABLE_RANGE_BASED_FOR_LOOP_SUPPORT
private:
BlockType Impl[N != 0 ? (N + BlockWidth - 1) / BlockWidth : 1];
public:
class Reference final : private FSingleton
{
public:
FORCEINLINE constexpr Reference& operator=(bool InValue) { Data = (Data & ~Mask) | (InValue ? Mask : 0); return *this; }
FORCEINLINE constexpr Reference& operator=(const Reference& InValue) { return *this = static_cast<bool>(InValue); }
FORCEINLINE constexpr Reference& operator&=(bool InValue) { Data &= InValue ? -1 : ~Mask; return *this; }
FORCEINLINE constexpr Reference& operator|=(bool InValue) { Data |= InValue ? Mask : 0; return *this; }
FORCEINLINE constexpr Reference& operator^=(bool InValue) { *this = InValue ^ *this; return *this; }
FORCEINLINE constexpr bool operator~() const { return !*this; }
FORCEINLINE constexpr operator bool() const { return (Data & Mask) != 0; }
private:
FORCEINLINE constexpr Reference(BlockType& InData, BlockType InMask)
: Data(InData), Mask(InMask)
{ }
BlockType& Data;
BlockType Mask;
friend Iterator;
};
private:
template <bool bConst>
class IteratorImpl
{
private:
public:
using ElementType = TConditional<bConst, const bool, bool>;
FORCEINLINE constexpr IteratorImpl() = default;
# if DO_CHECK
FORCEINLINE constexpr IteratorImpl(const IteratorImpl<false>& InValue) requires (bConst)
: Owner(InValue.Owner), Pointer(InValue.Pointer), Offset(InValue.Offset)
{ }
# else
FORCEINLINE constexpr IteratorImpl(const IteratorImpl<false>& InValue) requires (bConst)
: Pointer(InValue.Pointer), Offset(InValue.Offset)
{ }
# endif
FORCEINLINE constexpr IteratorImpl(const IteratorImpl&) = default;
FORCEINLINE constexpr IteratorImpl(IteratorImpl&&) = default;
FORCEINLINE constexpr IteratorImpl& operator=(const IteratorImpl&) = default;
FORCEINLINE constexpr IteratorImpl& operator=(IteratorImpl&&) = default;
NODISCARD friend FORCEINLINE constexpr bool operator==(const IteratorImpl& LHS, const IteratorImpl& RHS) { check(LHS.Pointer == RHS.Pointer); return LHS.Offset == RHS.Offset; }
NODISCARD friend FORCEINLINE constexpr strong_ordering operator<=>(const IteratorImpl& LHS, const IteratorImpl& RHS) { check(LHS.Pointer == RHS.Pointer); return LHS.Offset <=> RHS.Offset; }
NODISCARD FORCEINLINE constexpr Reference operator*() const requires (!bConst) { CheckThis(true); return Reference(*(Pointer + Offset / BlockWidth), 1ull << Offset % BlockWidth); }
NODISCARD FORCEINLINE constexpr ConstReference operator*() const requires ( bConst) { CheckThis(true); return (*(Pointer + Offset / BlockWidth) & (1ull << Offset % BlockWidth)); }
NODISCARD FORCEINLINE constexpr auto operator[](ptrdiff Index) const { IteratorImpl Temp = *this + Index; return *Temp; }
FORCEINLINE constexpr IteratorImpl& operator++() { ++Offset; CheckThis(); return *this; }
FORCEINLINE constexpr IteratorImpl& operator--() { --Offset; CheckThis(); return *this; }
FORCEINLINE constexpr IteratorImpl operator++(int) { IteratorImpl Temp = *this; ++*this; return Temp; }
FORCEINLINE constexpr IteratorImpl operator--(int) { IteratorImpl Temp = *this; --*this; return Temp; }
FORCEINLINE constexpr IteratorImpl& operator+=(ptrdiff Offset) { this->Offset += Offset; CheckThis(); return *this; }
FORCEINLINE constexpr IteratorImpl& operator-=(ptrdiff Offset) { this->Offset -= Offset; CheckThis(); return *this; }
NODISCARD friend FORCEINLINE constexpr IteratorImpl operator+(IteratorImpl Iter, ptrdiff Offset) { IteratorImpl Temp = Iter; Temp += Offset; return Temp; }
NODISCARD friend FORCEINLINE constexpr IteratorImpl operator+(ptrdiff Offset, IteratorImpl Iter) { IteratorImpl Temp = Iter; Temp += Offset; return Temp; }
NODISCARD FORCEINLINE constexpr IteratorImpl operator-(ptrdiff Offset) const { IteratorImpl Temp = *this; Temp -= Offset; return Temp; }
NODISCARD friend FORCEINLINE constexpr ptrdiff operator-(const IteratorImpl& LHS, const IteratorImpl& RHS) { check(LHS.Pointer == RHS.Pointer); return LHS.Offset - RHS.Offset; }
private:
# if DO_CHECK
const TStaticBitset* Owner = nullptr;
# endif
using BlockPtr = TConditional<bConst, const BlockType*, BlockType*>;
BlockPtr Pointer = nullptr;
size_t Offset = 0;
# if DO_CHECK
FORCEINLINE constexpr IteratorImpl(const TStaticBitset* InContainer, BlockPtr InPointer, size_t InOffset)
: Owner(InContainer), Pointer(InPointer), Offset(InOffset)
{ }
# else
FORCEINLINE constexpr IteratorImpl(const TStaticBitset* InContainer, BlockPtr InPointer, size_t InOffset)
: Pointer(InPointer), Offset(InOffset)
{ }
# endif
FORCEINLINE constexpr void CheckThis(bool bExceptEnd = false) const
{
checkf(Owner && Owner->IsValidIterator(*this), TEXT("Read access violation. Please check IsValidIterator()."));
checkf(!(bExceptEnd && Owner->End() == *this), TEXT("Read access violation. Please check IsValidIterator()."));
}
template <bool> friend class IteratorImpl;
friend TStaticBitset;
};
};
#endif
NAMESPACE_MODULE_END(Utility)
NAMESPACE_MODULE_END(Redcraft)
NAMESPACE_REDCRAFT_END

View File

@ -13,6 +13,7 @@ REDCRAFTUTILITY_API void TestArray();
REDCRAFTUTILITY_API void TestStaticArray();
REDCRAFTUTILITY_API void TestArrayView();
REDCRAFTUTILITY_API void TestBitset();
REDCRAFTUTILITY_API void TestStaticBitset();
NAMESPACE_END(Testing)