fix(containers): remove operator<=> of TBitset because its order is not defined

This commit is contained in:
_Redstone_c_ 2023-03-14 19:29:25 +08:00
parent 1daf90adee
commit 5021171324
2 changed files with 13 additions and 51 deletions

View File

@ -263,26 +263,6 @@ void TestBitset()
always_check((BitsetI == FBitset({ true, false, true, false }))); 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); FBitset BitsetA(64, 0x0139'0239'0339'0439ull);
uint64 IntA = 0x0139'0239'0339'0439ull; uint64 IntA = 0x0139'0239'0339'0439ull;

View File

@ -280,26 +280,6 @@ public:
return (LHS.Impl.Pointer[LHS.NumBlocks() - 1] & LastBlockBitmask) == (RHS.Impl.Pointer[LHS.NumBlocks() - 1] & LastBlockBitmask); 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. */ /** Sets the bits to the result of binary AND on corresponding pairs of bits of *this and other. */
TBitset& operator&=(const TBitset& InValue) TBitset& operator&=(const TBitset& InValue)
{ {
@ -531,7 +511,7 @@ public:
size_t Result = 0; 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"); static_assert(sizeof(BlockType) <= sizeof(uint64), "The block width of TBitset is unexpected");
@ -959,11 +939,11 @@ private:
FORCEINLINE IteratorImpl() = default; FORCEINLINE IteratorImpl() = default;
# if DO_CHECK # 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) : Owner(InValue.Owner), Pointer(InValue.Pointer), Offset(InValue.Offset)
{ } { }
# else # else
FORCEINLINE IteratorImpl(const IteratorImpl<false> &InValue) requires (bConst) FORCEINLINE IteratorImpl(const IteratorImpl<false>& InValue) requires (bConst)
: Pointer(InValue.Pointer), Offset(InValue.Offset) : Pointer(InValue.Pointer), Offset(InValue.Offset)
{ } { }
# endif # endif
@ -973,9 +953,9 @@ private:
FORCEINLINE IteratorImpl& operator=(const IteratorImpl&) = default; FORCEINLINE IteratorImpl& operator=(const IteratorImpl&) = default;
FORCEINLINE IteratorImpl& operator=(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 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)); } 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 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: private:
@ -1004,15 +984,17 @@ private:
const TBitset* Owner = nullptr; const TBitset* Owner = nullptr;
# endif # endif
BlockType* Pointer = nullptr; using BlockPtr = TConditional<bConst, const BlockType*, BlockType*>;
BlockPtr Pointer = nullptr;
size_t Offset = 0; size_t Offset = 0;
# if DO_CHECK # 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) : Owner(InContainer), Pointer(InPointer), Offset(InOffset)
{ } { }
# else # 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) : Pointer(InPointer), Offset(InOffset)
{ } { }
# endif # endif