Compare commits

...

2 Commits

10 changed files with 1484 additions and 239 deletions

View File

@ -14,6 +14,7 @@ void TestContainers()
TestArray(); TestArray();
TestStaticArray(); TestStaticArray();
TestArrayView(); TestArrayView();
TestBitset();
} }
NAMESPACE_UNNAMED_BEGIN NAMESPACE_UNNAMED_BEGIN
@ -240,6 +241,166 @@ void TestArrayView()
} }
} }
void TestBitset()
{
{
FBitset BitsetA;
FBitset BitsetB(16);
FBitset BitsetC(16, 0b1010'0100'0100'0010);
FBitset BitsetD(BitsetC);
FBitset BitsetE(MoveTemp(BitsetB));
FBitset BitsetF({ true, false, true, false });
FBitset BitsetG;
FBitset BitsetH;
FBitset BitsetI;
BitsetG = BitsetD;
BitsetH = MoveTemp(BitsetE);
BitsetI = { true, false, true, false };
always_check((BitsetF == 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, 0x0339'0339'0339'0339ull);
uint64 IntA = 0x0339'0339'0339'0339ull;
FBitset BitsetB(32, 0x017F'017Full);
uint32 IntB = 0x017F'017Full;
FBitset BitsetANDA = BitsetA; BitsetANDA &= BitsetB;
FBitset BitsetANDB = BitsetB; BitsetANDB &= BitsetA;
FBitset BitsetORA = BitsetA; BitsetORA &= BitsetB;
FBitset BitsetORB = BitsetB; BitsetORB &= BitsetA;
FBitset BitsetXORA = BitsetA; BitsetXORA &= BitsetB;
FBitset BitsetXORB = BitsetB; BitsetXORB &= BitsetA;
uint64 IntANDA = IntA; IntANDA &= IntB;
uint32 IntANDB = IntB; IntANDB &= IntA;
uint64 IntORA = IntA; IntORA &= IntB;
uint32 IntORB = IntB; IntORB &= IntA;
uint64 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));
}
{
FBitset BitsetA(64, 0x0339'0339'0339'0339ull);
uint64 IntA = 0x0339'0339'0339'0339ull;
FBitset BitsetB(32, 0x017F'017Full);
uint32 IntB = 0x017F'017Full;
always_check(((BitsetA & BitsetB).ToIntegral() == (IntA & IntB)));
always_check(((BitsetA | BitsetB).ToIntegral() == (IntA | IntB)));
always_check(((BitsetA ^ BitsetB).ToIntegral() == (IntA ^ IntB)));
}
{
FBitset Bitset(64, 0x0339'0339'0339'0339ull);
uint64 Int = 0x0339'0339'0339'0339ull;
always_check(((Bitset << 40).ToIntegral() == (Int << 40)));
always_check(((Bitset >> 40).ToIntegral() == (Int >> 40)));
}
{
FBitset BitsetA(4, 0b0000ull);
FBitset BitsetB(4, 0b0101ull);
FBitset BitsetC(4, 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()));
}
{
FBitset Bitset(16);
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));
Bitset.PushBack(true);
always_check((Bitset.Num() == 17));
always_check((Bitset.Count() == 1));
Bitset.PopBack();
always_check((Bitset.Num() == 16));
always_check((Bitset.Count() == 0));
Bitset.SetNum(32, true, true);
always_check((Bitset.Num() == 32));
always_check((Bitset.Count() == 16));
}
{
FBitset BitsetA(4);
FBitset BitsetB(4);
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_END(Testing)
NAMESPACE_MODULE_END(Utility) NAMESPACE_MODULE_END(Utility)

View File

@ -16,96 +16,15 @@ NAMESPACE_REDCRAFT_BEGIN
NAMESPACE_MODULE_BEGIN(Redcraft) NAMESPACE_MODULE_BEGIN(Redcraft)
NAMESPACE_MODULE_BEGIN(Utility) NAMESPACE_MODULE_BEGIN(Utility)
NAMESPACE_PRIVATE_BEGIN
template <typename ArrayType, typename T>
class TArrayIterator
{
public:
using ElementType = T;
FORCEINLINE TArrayIterator() = default;
# if DO_CHECK
FORCEINLINE TArrayIterator(const TArrayIterator<ArrayType, TRemoveConst<ElementType>>& InValue) requires (CConst<ElementType>)
: Owner(InValue.Owner), Pointer(InValue.Pointer)
{ }
# else
FORCEINLINE TArrayIterator(const TArrayIterator<ArrayType, TRemoveConst<ElementType>>& InValue) requires (CConst<ElementType>)
: Pointer(InValue.Pointer)
{ }
# endif
FORCEINLINE TArrayIterator(const TArrayIterator&) = default;
FORCEINLINE TArrayIterator(TArrayIterator&&) = default;
FORCEINLINE TArrayIterator& operator=(const TArrayIterator&) = default;
FORCEINLINE TArrayIterator& operator=(TArrayIterator&&) = default;
NODISCARD friend FORCEINLINE bool operator==(const TArrayIterator& LHS, const TArrayIterator& RHS) { return LHS.Pointer == RHS.Pointer; }
NODISCARD friend FORCEINLINE strong_ordering operator<=>(const TArrayIterator & LHS, const TArrayIterator & RHS) { return LHS.Pointer <=> RHS.Pointer; }
NODISCARD FORCEINLINE ElementType& operator*() const { CheckThis(true); return *Pointer; }
NODISCARD FORCEINLINE ElementType* operator->() const { CheckThis(true); return Pointer; }
NODISCARD FORCEINLINE ElementType& operator[](ptrdiff Index) const { TArrayIterator Temp = *this + Index; return *Temp; }
FORCEINLINE TArrayIterator& operator++() { ++Pointer; CheckThis(); return *this; }
FORCEINLINE TArrayIterator& operator--() { --Pointer; CheckThis(); return *this; }
FORCEINLINE TArrayIterator operator++(int) { TArrayIterator Temp = *this; ++*this; return Temp; }
FORCEINLINE TArrayIterator operator--(int) { TArrayIterator Temp = *this; --*this; return Temp; }
FORCEINLINE TArrayIterator& operator+=(ptrdiff Offset) { Pointer += Offset; CheckThis(); return *this; }
FORCEINLINE TArrayIterator& operator-=(ptrdiff Offset) { Pointer -= Offset; CheckThis(); return *this; }
NODISCARD friend FORCEINLINE TArrayIterator operator+(TArrayIterator Iter, ptrdiff Offset) { TArrayIterator Temp = Iter; Temp += Offset; return Temp; }
NODISCARD friend FORCEINLINE TArrayIterator operator+(ptrdiff Offset, TArrayIterator Iter) { TArrayIterator Temp = Iter; Temp += Offset; return Temp; }
NODISCARD FORCEINLINE TArrayIterator operator-(ptrdiff Offset) const { TArrayIterator Temp = *this; Temp -= Offset; return Temp; }
NODISCARD friend FORCEINLINE ptrdiff operator-(const TArrayIterator& LHS, const TArrayIterator& RHS) { LHS.CheckThis(); RHS.CheckThis(); return LHS.Pointer - RHS.Pointer; }
NODISCARD FORCEINLINE explicit operator TObserverPtr<ElementType[]>() const { CheckThis(); return TObserverPtr<ElementType[]>(Pointer); }
private:
# if DO_CHECK
const ArrayType* Owner = nullptr;
# endif
ElementType* Pointer = nullptr;
# if DO_CHECK
FORCEINLINE TArrayIterator(const ArrayType* InContainer, ElementType* InPointer)
: Owner(InContainer), Pointer(InPointer)
{ }
# else
FORCEINLINE TArrayIterator(const ArrayType* InContainer, ElementType* InPointer)
: Pointer(InPointer)
{ }
# endif
FORCEINLINE 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()."));
}
friend ArrayType;
template <typename InArrayType, typename InElementType>
friend class TArrayIterator;
};
NAMESPACE_PRIVATE_END
/** Dynamic array. The elements are stored contiguously, which means that elements can be accessed not only through iterators, but also using offsets to regular pointers to elements. */ /** Dynamic array. The elements are stored contiguously, which means that elements can be accessed not only through iterators, but also using offsets to regular pointers to elements. */
template <CElementalObject T, CInstantiableAllocator Allocator = FHeapAllocator> requires (!CConst<T>) template <CElementalObject T, CInstantiableAllocator Allocator = FHeapAllocator> requires (!CConst<T>)
class TArray final class TArray final
{ {
private:
template <bool bConst>
class IteratorImpl;
public: public:
using ElementType = T; using ElementType = T;
@ -114,8 +33,8 @@ public:
using Reference = T&; using Reference = T&;
using ConstReference = const T&; using ConstReference = const T&;
using Iterator = NAMESPACE_PRIVATE::TArrayIterator<TArray, ElementType>; using Iterator = IteratorImpl<false>;
using ConstIterator = NAMESPACE_PRIVATE::TArrayIterator<TArray, const ElementType>; using ConstIterator = IteratorImpl<true >;
using ReverseIterator = TReverseIterator< Iterator>; using ReverseIterator = TReverseIterator< Iterator>;
using ConstReverseIterator = TReverseIterator<ConstIterator>; using ConstReverseIterator = TReverseIterator<ConstIterator>;
@ -155,7 +74,7 @@ public:
{ {
if constexpr (CForwardIterator<I>) if constexpr (CForwardIterator<I>)
{ {
if (CSizedSentinelFor<S, I>) checkf(First <= Last, TEXT("Illegal range iterator. Please check First <= Last.")); if (CSizedSentinelFor<S, I>) { checkf(First <= Last, TEXT("Illegal range iterator. Please check First <= Last.")); }
const size_t Count = Iteration::Distance(First, Last); const size_t Count = Iteration::Distance(First, Last);
@ -615,7 +534,7 @@ public:
if constexpr (CForwardIterator<I>) if constexpr (CForwardIterator<I>)
{ {
if (CSizedSentinelFor<S, I>) checkf(First <= Last, TEXT("Illegal range iterator. Please check First <= Last.")); if (CSizedSentinelFor<S, I>) { checkf(First <= Last, TEXT("Illegal range iterator. Please check First <= Last.")); }
const size_t InsertIndex = Iter - Begin(); const size_t InsertIndex = Iter - Begin();
const size_t Count = Iteration::Distance(First, Last); const size_t Count = Iteration::Distance(First, Last);
@ -1142,6 +1061,89 @@ private:
} }
ALLOCATOR_WRAPPER_END(AllocatorType, ElementType, Impl) ALLOCATOR_WRAPPER_END(AllocatorType, ElementType, Impl)
private:
template <bool bConst>
class IteratorImpl
{
public:
using ElementType = TConditional<bConst, const T, T>;
FORCEINLINE IteratorImpl() = default;
# if DO_CHECK
FORCEINLINE IteratorImpl(const IteratorImpl<false>& InValue) requires (bConst)
: Owner(InValue.Owner), Pointer(InValue.Pointer)
{ }
# else
FORCEINLINE IteratorImpl(const IteratorImpl<false>& InValue) requires (bConst)
: Pointer(InValue.Pointer)
{ }
# endif
FORCEINLINE IteratorImpl(const IteratorImpl&) = default;
FORCEINLINE IteratorImpl(IteratorImpl&&) = default;
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; }
NODISCARD friend FORCEINLINE strong_ordering operator<=>(const IteratorImpl& LHS, const IteratorImpl& RHS) { return LHS.Pointer <=> RHS.Pointer; }
NODISCARD FORCEINLINE ElementType& operator*() const { CheckThis(true); return *Pointer; }
NODISCARD FORCEINLINE ElementType* operator->() const { CheckThis(true); return Pointer; }
NODISCARD FORCEINLINE ElementType& operator[](ptrdiff Index) const { IteratorImpl Temp = *this + Index; return *Temp; }
FORCEINLINE IteratorImpl& operator++() { ++Pointer; CheckThis(); return *this; }
FORCEINLINE IteratorImpl& operator--() { --Pointer; CheckThis(); return *this; }
FORCEINLINE IteratorImpl operator++(int) { IteratorImpl Temp = *this; ++*this; return Temp; }
FORCEINLINE IteratorImpl operator--(int) { IteratorImpl Temp = *this; --*this; return Temp; }
FORCEINLINE IteratorImpl& operator+=(ptrdiff Offset) { Pointer += Offset; CheckThis(); return *this; }
FORCEINLINE IteratorImpl& operator-=(ptrdiff Offset) { Pointer -= Offset; CheckThis(); return *this; }
NODISCARD friend FORCEINLINE IteratorImpl operator+(IteratorImpl Iter, ptrdiff Offset) { IteratorImpl Temp = Iter; Temp += Offset; return Temp; }
NODISCARD friend FORCEINLINE IteratorImpl operator+(ptrdiff Offset, IteratorImpl Iter) { IteratorImpl Temp = Iter; 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) { LHS.CheckThis(); RHS.CheckThis(); return LHS.Pointer - RHS.Pointer; }
NODISCARD FORCEINLINE explicit operator TObserverPtr<ElementType[]>() const { CheckThis(); return TObserverPtr<ElementType[]>(Pointer); }
private:
# if DO_CHECK
const TArray* Owner = nullptr;
# endif
ElementType* Pointer = nullptr;
# if DO_CHECK
FORCEINLINE IteratorImpl(const TArray* InContainer, ElementType* InPointer)
: Owner(InContainer), Pointer(InPointer)
{ }
# else
FORCEINLINE IteratorImpl(const TArray* InContainer, ElementType* InPointer)
: Pointer(InPointer)
{ }
# endif
FORCEINLINE 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 TArray;
};
}; };
template <typename I, typename S> template <typename I, typename S>

View File

@ -14,108 +14,6 @@ NAMESPACE_REDCRAFT_BEGIN
NAMESPACE_MODULE_BEGIN(Redcraft) NAMESPACE_MODULE_BEGIN(Redcraft)
NAMESPACE_MODULE_BEGIN(Utility) NAMESPACE_MODULE_BEGIN(Utility)
template <CElementalObject T, size_t InExtent>
class TArrayView;
NAMESPACE_PRIVATE_BEGIN
template <typename T>
class TArrayViewIterator
{
public:
using ElementType = T;
FORCEINLINE constexpr TArrayViewIterator() = default;
# if DO_CHECK
FORCEINLINE constexpr TArrayViewIterator(const TArrayViewIterator<TRemoveConst<ElementType>>& InValue) requires (CConst<ElementType>)
: Pointer(InValue.Pointer), FirstSentinel(InValue.FirstSentinel), EndSentinel(InValue.EndSentinel)
{ }
# else
FORCEINLINE constexpr TArrayViewIterator(const TArrayViewIterator<TRemoveConst<ElementType>>& InValue) requires (CConst<ElementType>)
: Pointer(InValue.Pointer)
{ }
# endif
FORCEINLINE constexpr TArrayViewIterator(const TArrayViewIterator&) = default;
FORCEINLINE constexpr TArrayViewIterator(TArrayViewIterator&&) = default;
FORCEINLINE constexpr TArrayViewIterator& operator=(const TArrayViewIterator&) = default;
FORCEINLINE constexpr TArrayViewIterator& operator=(TArrayViewIterator&&) = default;
NODISCARD friend FORCEINLINE constexpr bool operator==(const TArrayViewIterator& LHS, const TArrayViewIterator& RHS) { return LHS.Pointer == RHS.Pointer; }
NODISCARD friend FORCEINLINE constexpr strong_ordering operator<=>(const TArrayViewIterator & LHS, const TArrayViewIterator & RHS) { return LHS.Pointer <=> RHS.Pointer; }
NODISCARD FORCEINLINE constexpr ElementType& operator*() const { CheckThis(true); return *Pointer; }
NODISCARD FORCEINLINE constexpr ElementType* operator->() const { CheckThis(true); return Pointer; }
NODISCARD FORCEINLINE constexpr ElementType& operator[](ptrdiff Index) const { TArrayViewIterator Temp = *this + Index; return *Temp; }
FORCEINLINE constexpr TArrayViewIterator& operator++() { ++Pointer; CheckThis(); return *this; }
FORCEINLINE constexpr TArrayViewIterator& operator--() { --Pointer; CheckThis(); return *this; }
FORCEINLINE constexpr TArrayViewIterator operator++(int) { TArrayViewIterator Temp = *this; ++*this; return Temp; }
FORCEINLINE constexpr TArrayViewIterator operator--(int) { TArrayViewIterator Temp = *this; --*this; return Temp; }
FORCEINLINE constexpr TArrayViewIterator& operator+=(ptrdiff Offset) { Pointer += Offset; CheckThis(); return *this; }
FORCEINLINE constexpr TArrayViewIterator& operator-=(ptrdiff Offset) { Pointer -= Offset; CheckThis(); return *this; }
NODISCARD friend FORCEINLINE constexpr TArrayViewIterator operator+(TArrayViewIterator Iter, ptrdiff Offset) { TArrayViewIterator Temp = Iter; Temp += Offset; return Temp; }
NODISCARD friend FORCEINLINE constexpr TArrayViewIterator operator+(ptrdiff Offset, TArrayViewIterator Iter) { TArrayViewIterator Temp = Iter; Temp += Offset; return Temp; }
NODISCARD FORCEINLINE constexpr TArrayViewIterator operator-(ptrdiff Offset) const { TArrayViewIterator Temp = *this; Temp -= Offset; return Temp; }
NODISCARD friend FORCEINLINE constexpr ptrdiff operator-(const TArrayViewIterator& LHS, const TArrayViewIterator& RHS) { LHS.CheckThis(); RHS.CheckThis(); return LHS.Pointer - RHS.Pointer; }
NODISCARD FORCEINLINE constexpr explicit operator TObserverPtr<ElementType[]>() const { CheckThis(); return TObserverPtr<ElementType[]>(Pointer); }
private:
ElementType* Pointer = nullptr;
# if DO_CHECK
ElementType* FirstSentinel = nullptr;
ElementType* EndSentinel = nullptr;
# endif
# if DO_CHECK
FORCEINLINE constexpr TArrayViewIterator(ElementType* InPointer, ElementType* InFirstSentinel, ElementType* InEndSentinel)
: Pointer(InPointer), FirstSentinel(InFirstSentinel), EndSentinel(InEndSentinel)
{ }
# else
FORCEINLINE constexpr TArrayViewIterator(ElementType* InPointer, ElementType* InFirstSentinel, ElementType* InEndSentinel)
: Pointer(InPointer)
{ }
# endif
FORCEINLINE constexpr void CheckThis(bool bExceptEnd = false) const
{
check_code
({
const bool bInLegalRange = FirstSentinel && EndSentinel && FirstSentinel <= Pointer && Pointer <= EndSentinel;
const bool bIsDereferenceable = Pointer != EndSentinel;
checkf(bInLegalRange && (!bExceptEnd || bIsDereferenceable), TEXT("Read access violation. Please check IsValidIterator()."));
});
}
template <typename U>
friend class TArrayViewIterator;
template <CElementalObject U, size_t InExtent>
friend class NAMESPACE_REDCRAFT::TArrayView;
};
template <bool bEnable>
struct TEnableArrayNum { size_t ArrayNum; };
template <>
struct TEnableArrayNum<false> { size_t ArrayNum; };
NAMESPACE_PRIVATE_END
template <CElementalObject T, size_t N> template <CElementalObject T, size_t N>
struct TStaticArray; struct TStaticArray;
@ -130,19 +28,15 @@ inline constexpr size_t DynamicExtent = INDEX_NONE;
* is known at compile-time and encoded in the type, or a dynamic extent. * is known at compile-time and encoded in the type, or a dynamic extent.
*/ */
template <CElementalObject T, size_t InExtent = DynamicExtent> template <CElementalObject T, size_t InExtent = DynamicExtent>
class TArrayView final : private NAMESPACE_PRIVATE::TEnableArrayNum<InExtent == DynamicExtent> class TArrayView final
{ {
private:
using Impl = NAMESPACE_PRIVATE::TEnableArrayNum<InExtent == DynamicExtent>;
public: public:
using ElementType = T; using ElementType = T;
using Reference = T&; using Reference = T&;
using Iterator = NAMESPACE_PRIVATE::TArrayViewIterator<ElementType>; class Iterator;
using ReverseIterator = TReverseIterator<Iterator>; using ReverseIterator = TReverseIterator<Iterator>;
@ -155,35 +49,41 @@ public:
/** Constructs an array view that is a view over the range ['InFirst', 'InFirst' + 'Count'). */ /** Constructs an array view that is a view over the range ['InFirst', 'InFirst' + 'Count'). */
template <CContiguousIterator I> requires (CConvertibleTo<TIteratorElementType<I>(*)[], ElementType(*)[]>) template <CContiguousIterator I> requires (CConvertibleTo<TIteratorElementType<I>(*)[], ElementType(*)[]>)
FORCEINLINE constexpr explicit (Extent != DynamicExtent) TArrayView(I InFirst, size_t InCount) : Pointer(static_cast<TObserverPtr<TIteratorElementType<I>[]>>(InFirst)) FORCEINLINE constexpr explicit (Extent != DynamicExtent) TArrayView(I InFirst, size_t InCount)
{ {
checkf(Extent == DynamicExtent || Extent == InCount, TEXT("Illegal range count. Please check InCount.")); checkf(Extent == DynamicExtent || Extent == InCount, TEXT("Illegal range count. Please check InCount."));
Impl.Pointer = AddressOf(*InFirst);
if constexpr (Extent == DynamicExtent) if constexpr (Extent == DynamicExtent)
{ {
Impl::ArrayNum = InCount; Impl.ArrayNum = InCount;
} }
} }
/** Constructs an array view that is a view over the range ['InFirst', 'InLast'). */ /** Constructs an array view that is a view over the range ['InFirst', 'InLast'). */
template <CContiguousIterator I, CSizedSentinelFor<I> S> requires (CConvertibleTo<TIteratorElementType<I>(*)[], ElementType(*)[]>) template <CContiguousIterator I, CSizedSentinelFor<I> S> requires (CConvertibleTo<TIteratorElementType<I>(*)[], ElementType(*)[]>)
FORCEINLINE constexpr explicit (Extent != DynamicExtent) TArrayView(I InFirst, S InLast) : Pointer(static_cast<TObserverPtr<TIteratorElementType<I>[]>>(InFirst)) FORCEINLINE constexpr explicit (Extent != DynamicExtent) TArrayView(I InFirst, S InLast)
{ {
checkf(Extent == DynamicExtent || Extent == InLast - InFirst, TEXT("Illegal range iterator. Please check InLast - InFirst.")); checkf(Extent == DynamicExtent || Extent == InLast - InFirst, TEXT("Illegal range iterator. Please check InLast - InFirst."));
Impl.Pointer = AddressOf(*InFirst);
if constexpr (Extent == DynamicExtent) if constexpr (Extent == DynamicExtent)
{ {
Impl::ArrayNum = InLast - InFirst; Impl.ArrayNum = InLast - InFirst;
} }
} }
/** Constructs an array view that is a view over the array 'InArray'. */ /** Constructs an array view that is a view over the array 'InArray'. */
template <size_t N> requires (Extent == DynamicExtent || N == Extent) template <size_t N> requires (Extent == DynamicExtent || N == Extent)
FORCEINLINE constexpr TArrayView(ElementType(&InArray)[N]) : Pointer(InArray) FORCEINLINE constexpr TArrayView(ElementType(&InArray)[N])
{ {
Impl.Pointer = AddressOf(InArray[0]);
if constexpr (Extent == DynamicExtent) if constexpr (Extent == DynamicExtent)
{ {
Impl::ArrayNum = N; Impl.ArrayNum = N;
} }
} }
@ -211,13 +111,15 @@ public:
/** Converting constructor from another array view 'InValue'. */ /** Converting constructor from another array view 'InValue'. */
template <typename U, size_t N> requires ((Extent == DynamicExtent || N == DynamicExtent || N == Extent) && CConvertibleTo<U(*)[], ElementType(*)[]>) template <typename U, size_t N> requires ((Extent == DynamicExtent || N == DynamicExtent || N == Extent) && CConvertibleTo<U(*)[], ElementType(*)[]>)
FORCEINLINE constexpr explicit (Extent != DynamicExtent && N == DynamicExtent) TArrayView(TArrayView<U, N> InValue) : Pointer(InValue.GetData()) FORCEINLINE constexpr explicit (Extent != DynamicExtent && N == DynamicExtent) TArrayView(TArrayView<U, N> InValue)
{ {
checkf(Extent == DynamicExtent || Extent == InValue.Num(), TEXT("Illegal view extent. Please check InValue.Num().")); checkf(Extent == DynamicExtent || Extent == InValue.Num(), TEXT("Illegal view extent. Please check InValue.Num()."));
Impl.Pointer = AddressOf(InValue[0]);
if constexpr (Extent == DynamicExtent) if constexpr (Extent == DynamicExtent)
{ {
Impl::ArrayNum = InValue.Num(); Impl.ArrayNum = InValue.Num();
} }
} }
@ -365,18 +267,18 @@ public:
} }
/** @return The pointer to the underlying element storage. */ /** @return The pointer to the underlying element storage. */
NODISCARD FORCEINLINE constexpr TObserverPtr<ElementType[]> GetData() const { return Pointer; } NODISCARD FORCEINLINE constexpr TObserverPtr<ElementType[]> GetData() const { return TObserverPtr<ElementType[]>(Impl.Pointer); }
/** @return The iterator to the first or end element. */ /** @return The iterator to the first or end element. */
NODISCARD FORCEINLINE constexpr Iterator Begin() const { return Iterator(Pointer.Get() , Pointer.Get(), Pointer.Get() + Num()); } NODISCARD FORCEINLINE constexpr Iterator Begin() const { return Iterator(this, Impl.Pointer); }
NODISCARD FORCEINLINE constexpr Iterator End() const { return Iterator(Pointer.Get() + Num(), Pointer.Get(), Pointer.Get() + Num()); } NODISCARD FORCEINLINE constexpr Iterator End() const { return Iterator(this, Impl.Pointer + Num()); }
/** @return The reverse iterator to the first or end element. */ /** @return The reverse iterator to the first or end element. */
NODISCARD FORCEINLINE constexpr ReverseIterator RBegin() const { return ReverseIterator(End()); } NODISCARD FORCEINLINE constexpr ReverseIterator RBegin() const { return ReverseIterator(End()); }
NODISCARD FORCEINLINE constexpr ReverseIterator REnd() const { return ReverseIterator(Begin()); } NODISCARD FORCEINLINE constexpr ReverseIterator REnd() const { return ReverseIterator(Begin()); }
/** @return The number of elements in the container. */ /** @return The number of elements in the container. */
NODISCARD FORCEINLINE constexpr size_t Num() const { if constexpr (Extent == DynamicExtent) return Impl::ArrayNum; return Extent; } NODISCARD FORCEINLINE constexpr size_t Num() const { if constexpr (Extent == DynamicExtent) { return Impl.ArrayNum; } return Extent; }
/** @return The number of bytes in the container. */ /** @return The number of bytes in the container. */
NODISCARD FORCEINLINE constexpr size_t NumBytes() const { return Num() * sizeof(ElementType); } NODISCARD FORCEINLINE constexpr size_t NumBytes() const { return Num() * sizeof(ElementType); }
@ -388,7 +290,7 @@ public:
NODISCARD FORCEINLINE constexpr bool IsValidIterator(Iterator Iter) const { return Begin() <= Iter && Iter <= End(); } NODISCARD FORCEINLINE constexpr bool IsValidIterator(Iterator Iter) const { return Begin() <= Iter && Iter <= End(); }
/** @return The reference to the requested element. */ /** @return The reference to the requested element. */
NODISCARD FORCEINLINE constexpr ElementType& operator[](size_t Index) const { checkf(Index < Num(), TEXT("Read access violation. Please check IsValidIterator().")); return Pointer[Index]; } NODISCARD FORCEINLINE constexpr ElementType& operator[](size_t Index) const { checkf(Index < Num(), TEXT("Read access violation. Please check IsValidIterator().")); return Impl.Pointer[Index]; }
/** @return The reference to the first or last element. */ /** @return The reference to the first or last element. */
NODISCARD FORCEINLINE constexpr ElementType& Front() const { return *Begin(); } NODISCARD FORCEINLINE constexpr ElementType& Front() const { return *Begin(); }
@ -411,7 +313,80 @@ public:
private: private:
TObserverPtr<ElementType[]> Pointer; struct FImplWithoutNum { ElementType* Pointer; };
struct FImplWithNum : FImplWithoutNum { size_t ArrayNum; };
TConditional<InExtent == DynamicExtent, FImplWithNum, FImplWithoutNum> Impl;
public:
class Iterator
{
public:
using ElementType = T;
FORCEINLINE constexpr Iterator() = default;
FORCEINLINE constexpr Iterator(const Iterator&) = default;
FORCEINLINE constexpr Iterator(Iterator&&) = default;
FORCEINLINE constexpr Iterator& operator=(const Iterator&) = default;
FORCEINLINE constexpr Iterator& operator=(Iterator&&) = default;
NODISCARD friend FORCEINLINE constexpr bool operator==(const Iterator& LHS, const Iterator& RHS) { return LHS.Pointer == RHS.Pointer; }
NODISCARD friend FORCEINLINE constexpr strong_ordering operator<=>(const Iterator& LHS, const Iterator& RHS) { return LHS.Pointer <=> RHS.Pointer; }
NODISCARD FORCEINLINE constexpr ElementType& operator*() const { CheckThis(true); return *Pointer; }
NODISCARD FORCEINLINE constexpr ElementType* operator->() const { CheckThis(true); return Pointer; }
NODISCARD FORCEINLINE constexpr ElementType& operator[](ptrdiff Index) const { Iterator Temp = *this + Index; return *Temp; }
FORCEINLINE constexpr Iterator& operator++() { ++Pointer; CheckThis(); return *this; }
FORCEINLINE constexpr Iterator& operator--() { --Pointer; CheckThis(); return *this; }
FORCEINLINE constexpr Iterator operator++(int) { Iterator Temp = *this; ++*this; return Temp; }
FORCEINLINE constexpr Iterator operator--(int) { Iterator Temp = *this; --*this; return Temp; }
FORCEINLINE constexpr Iterator& operator+=(ptrdiff Offset) { Pointer += Offset; CheckThis(); return *this; }
FORCEINLINE constexpr Iterator& operator-=(ptrdiff Offset) { Pointer -= Offset; CheckThis(); return *this; }
NODISCARD friend FORCEINLINE constexpr Iterator operator+(Iterator Iter, ptrdiff Offset) { Iterator Temp = Iter; Temp += Offset; return Temp; }
NODISCARD friend FORCEINLINE constexpr Iterator operator+(ptrdiff Offset, Iterator Iter) { Iterator Temp = Iter; Temp += Offset; return Temp; }
NODISCARD FORCEINLINE constexpr Iterator operator-(ptrdiff Offset) const { Iterator Temp = *this; Temp -= Offset; return Temp; }
NODISCARD friend FORCEINLINE constexpr ptrdiff operator-(const Iterator& LHS, const Iterator& RHS) { LHS.CheckThis(); RHS.CheckThis(); return LHS.Pointer - RHS.Pointer; }
NODISCARD FORCEINLINE constexpr explicit operator TObserverPtr<ElementType[]>() const { CheckThis(); return TObserverPtr<ElementType[]>(Pointer); }
private:
# if DO_CHECK
const TArrayView* Owner = nullptr;
# endif
ElementType* Pointer = nullptr;
# if DO_CHECK
FORCEINLINE constexpr Iterator(const TArrayView* InContainer, ElementType* InPointer)
: Owner(InContainer), Pointer(InPointer)
{ }
# else
FORCEINLINE constexpr Iterator(const TArrayView* InContainer, ElementType* InPointer)
: Pointer(InPointer)
{ }
# 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()."));
}
friend TArrayView;
};
}; };

View File

@ -0,0 +1,969 @@
#pragma once
#include "CoreTypes.h"
#include "Memory/Memory.h"
#include "Memory/Allocator.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)
template <CInstantiableAllocator Allocator>
class TBitset final
{
private:
template <bool bConst>
class IteratorImpl;
public:
using BlockType = uint64;
using ElementType = bool;
using AllocatorType = Allocator;
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 TBitset() : TBitset(0) { }
/** Constructs the bitset with 'Count' uninitialized bits. */
FORCEINLINE explicit TBitset(size_t InCount)
{
Impl.BitsetNum = InCount;
Impl.BlocksMax = Impl->CalculateSlackReserve(NumBlocks());
Impl.Pointer = Impl->Allocate(MaxBlocks());
}
/** Constructs a bitset from an integer. */
TBitset(size_t InCount, uint64 InValue) : TBitset(InCount > 64 ? InCount : 64)
{
size_t BlockInteger = sizeof(uint64) / sizeof(BlockType);
*reinterpret_cast<uint64*>(Impl.Pointer) = InValue;
Memory::Memset(Impl.Pointer + BlockInteger, 0, (NumBlocks() - BlockInteger) * sizeof(BlockType));
Impl.BitsetNum = InCount;
}
/** Constructs the bitset with the bits of the range ['First', 'Last'). */
template <CInputIterator I, CSentinelFor<I> S> requires (CConstructibleFrom<bool, TIteratorReferenceType<I>>)
TBitset(I First, S Last)
{
if constexpr (CForwardIterator<I>)
{
if (CSizedSentinelFor<S, I>) { checkf(First <= Last, TEXT("Illegal range iterator. Please check First <= Last.")); }
const size_t InCount = Iteration::Distance(First, Last);
new (this) TBitset(InCount);
BlockType* CurrentBlock = Impl.Pointer - 1;
for (Reference Ref: *this) Ref = *First++;
}
else
{
new (this) TBitset(0);
while (First != Last)
{
PushBack(*First);
++First;
}
}
}
/** Copy constructor. Constructs the bitset with the copy of the bits of 'InValue'. */
FORCEINLINE TBitset(const TBitset& InValue)
{
Impl.BitsetNum = InValue.Num();
Impl.BlocksMax = Impl->CalculateSlackReserve(NumBlocks());
Impl.Pointer = Impl->Allocate(MaxBlocks());
Memory::Memcpy(Impl.Pointer, InValue.Impl.Pointer, NumBlocks() * sizeof(BlockType));
}
/** Move constructor. After the move, 'InValue' is guaranteed to be empty. */
TBitset(TBitset&& InValue)
{
Impl.BitsetNum = InValue.Num();
if (InValue.Impl->IsTransferable(InValue.Impl.Pointer))
{
Impl.BlocksMax = InValue.MaxBlocks();
Impl.Pointer = InValue.Impl.Pointer;
InValue.Impl.BitsetNum = 0;
InValue.Impl.BlocksMax = InValue.Impl->CalculateSlackReserve(InValue.NumBlocks());
InValue.Impl.Pointer = InValue.Impl->Allocate(InValue.MaxBlocks());
}
else
{
Impl.BlocksMax = Impl->CalculateSlackReserve(NumBlocks());
Impl.Pointer = Impl->Allocate(MaxBlocks());
Memory::Memcpy(Impl.Pointer, InValue.Impl.Pointer, NumBlocks() * sizeof(BlockType));
}
}
/** Constructs the bitset with the bits of the initializer list. */
FORCEINLINE TBitset(initializer_list<bool> IL) : TBitset(Iteration::Begin(IL), Iteration::End(IL)) { }
/** Destructs the bitset. The storage is deallocated. */
~TBitset()
{
Impl->Deallocate(Impl.Pointer);
}
/** Copy assignment operator. Replaces the bits with a copy of the bits of 'InValue'. */
TBitset& operator=(const TBitset& InValue)
{
if (&InValue == this) UNLIKELY return *this;
size_t NumToAllocate = InValue.NumBlocks();
NumToAllocate = NumToAllocate > MaxBlocks() ? Impl->CalculateSlackGrow(InValue.NumBlocks(), MaxBlocks()) : NumToAllocate;
NumToAllocate = NumToAllocate < MaxBlocks() ? Impl->CalculateSlackShrink(InValue.NumBlocks(), MaxBlocks()) : NumToAllocate;
if (NumToAllocate != MaxBlocks())
{
Impl->Deallocate(Impl.Pointer);
Impl.BitsetNum = InValue.Num();
Impl.BlocksMax = NumToAllocate;
Impl.Pointer = Impl->Allocate(MaxBlocks());
Memory::Memcpy(Impl.Pointer, InValue.Impl.Pointer, NumBlocks() * sizeof(BlockType));
return *this;
}
check(InValue.Num() <= Max());
Impl.BitsetNum = InValue.Num();
Memory::Memcpy(Impl.Pointer, InValue.Impl.Pointer, NumBlocks() * sizeof(BlockType));
return *this;
}
/** Move assignment operator. After the move, 'InValue' is guaranteed to be empty. */
TBitset& operator=(TBitset&& InValue)
{
if (&InValue == this) UNLIKELY return *this;
if (InValue.Impl->IsTransferable(InValue.Impl.Pointer))
{
Impl->Deallocate(Impl.Pointer);
Impl.Pointer = InValue.Impl.Pointer;
InValue.Impl.BitsetNum = 0;
InValue.Impl.BlocksMax = InValue.Impl->CalculateSlackReserve(InValue.NumBlocks());
InValue.Impl.Pointer = InValue.Impl->Allocate(InValue.MaxBlocks());
return *this;
}
*this = InValue;
InValue.Reset();
return *this;
}
/** Replaces the bits with those identified by initializer list. */
TBitset& operator=(initializer_list<bool> IL)
{
auto First = Iteration::Begin(IL);
const size_t BlocksCount = (GetNum(IL) + BlockWidth - 1) / BlockWidth;
size_t NumToAllocate = BlocksCount;
NumToAllocate = NumToAllocate > MaxBlocks() ? Impl->CalculateSlackGrow(BlocksCount, MaxBlocks()) : NumToAllocate;
NumToAllocate = NumToAllocate < MaxBlocks() ? Impl->CalculateSlackShrink(BlocksCount, MaxBlocks()) : NumToAllocate;
if (NumToAllocate != MaxBlocks())
{
Impl->Deallocate(Impl.Pointer);
Impl.BitsetNum = GetNum(IL);
Impl.BlocksMax = NumToAllocate;
Impl.Pointer = Impl->Allocate(MaxBlocks());
for (Reference Ref : *this) Ref = *First++;
return *this;
}
Impl.BitsetNum = GetNum(IL);
for (Reference Ref : *this) Ref = *First++;
return *this;
}
/** Compares the bits of two bitsets. */
NODISCARD friend bool operator==(const TBitset& LHS, const TBitset& RHS)
{
if (LHS.Num() != RHS.Num()) return false;
if (LHS.NumBlocks() == 0) return true;
for (size_t Index = 0; Index != LHS.NumBlocks() - 1; ++Index)
{
if (LHS.Impl.Pointer[Index] != RHS.Impl.Pointer[Index]) return false;
}
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);
}
/** 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)
{
if (&InValue == this) UNLIKELY return *this;
if (Num() == 0 || InValue.Num() == 0) return *this;
if (Num() <= InValue.Num())
{
for (size_t Index = 0; Index != NumBlocks(); ++Index)
{
Impl.Pointer[Index] &= InValue.Impl.Pointer[Index];
}
}
else
{
const size_t LastBlock = InValue.NumBlocks() - 1;
for (size_t Index = 0; Index != LastBlock; ++Index)
{
Impl.Pointer[Index] &= InValue.Impl.Pointer[Index];
}
const BlockType LastBlockBitmask = InValue.Num() % BlockWidth != 0 ? (1ull << InValue.Num() % BlockWidth) - 1 : -1;
Impl.Pointer[LastBlock] &= InValue.Impl.Pointer[LastBlock] & LastBlockBitmask;
for (size_t Index = LastBlock + 1; Index != NumBlocks(); ++Index)
{
Impl.Pointer[Index] &= 0;
}
}
return *this;
}
/** Sets the bits to the result of binary OR on corresponding pairs of bits of *this and other. */
TBitset& operator|=(const TBitset& InValue)
{
if (&InValue == this) UNLIKELY return *this;
if (Num() == 0 || InValue.Num() == 0) return *this;
if (Num() <= InValue.Num())
{
for (size_t Index = 0; Index != NumBlocks(); ++Index)
{
Impl.Pointer[Index] |= InValue.Impl.Pointer[Index];
}
}
else
{
const size_t LastBlock = InValue.NumBlocks() - 1;
for (size_t Index = 0; Index != LastBlock; ++Index)
{
Impl.Pointer[Index] |= InValue.Impl.Pointer[Index];
}
const BlockType LastBlockBitmask = InValue.Num() % BlockWidth != 0 ? (1ull << InValue.Num() % BlockWidth) - 1 : -1;
Impl.Pointer[LastBlock] |= InValue.Impl.Pointer[LastBlock] & LastBlockBitmask;
for (size_t Index = LastBlock + 1; Index != NumBlocks(); ++Index)
{
Impl.Pointer[Index] |= 0;
}
}
return *this;
}
/** Sets the bits to the result of binary XOR on corresponding pairs of bits of *this and other. */
TBitset& operator^=(const TBitset& InValue)
{
if (&InValue == this) UNLIKELY return *this;
if (Num() == 0 || InValue.Num() == 0) return *this;
if (Num() <= InValue.Num())
{
for (size_t Index = 0; Index != NumBlocks(); ++Index)
{
Impl.Pointer[Index] ^= InValue.Impl.Pointer[Index];
}
}
else
{
const size_t LastBlock = InValue.NumBlocks() - 1;
for (size_t Index = 0; Index != LastBlock; ++Index)
{
Impl.Pointer[Index] ^= InValue.Impl.Pointer[Index];
}
const BlockType LastBlockBitmask = InValue.Num() % BlockWidth != 0 ? (1ull << InValue.Num() % BlockWidth) - 1 : -1;
Impl.Pointer[LastBlock] ^= InValue.Impl.Pointer[LastBlock] & LastBlockBitmask;
for (size_t Index = LastBlock + 1; Index != NumBlocks(); ++Index)
{
Impl.Pointer[Index] ^= 0;
}
}
return *this;
}
NODISCARD friend FORCEINLINE TBitset operator&(const TBitset& LHS, const TBitset& RHS) { return LHS.Num() < RHS.Num() ? TBitset(RHS) &= LHS : TBitset(LHS) &= RHS; }
NODISCARD friend FORCEINLINE TBitset operator|(const TBitset& LHS, const TBitset& RHS) { return LHS.Num() < RHS.Num() ? TBitset(RHS) |= LHS : TBitset(LHS) |= RHS; }
NODISCARD friend FORCEINLINE TBitset operator^(const TBitset& LHS, const TBitset& RHS) { return LHS.Num() < RHS.Num() ? TBitset(RHS) ^= LHS : TBitset(LHS) ^= RHS; }
/** @return The temporary copy of *this with all bits flipped (binary NOT). */
NODISCARD TBitset operator~() const
{
TBitset Result = *this;
for (size_t Index = 0; Index != NumBlocks(); ++Index)
{
Result.Impl.Pointer[Index] = ~Result.Impl.Pointer[Index];
}
return Result;
}
/** Performs binary shift left. */
TBitset& operator<<=(size_t Offset)
{
const size_t Blockshift = Offset / BlockWidth;
const size_t Bitshift = Offset % BlockWidth;
if (Num() == 0) return *this;
if (Blockshift != 0)
{
for (size_t Index = NumBlocks() - 1; Index != -1; --Index)
{
Impl.Pointer[Index] = Index >= Blockshift ? Impl.Pointer[Index - Blockshift] : 0;
}
}
if (Bitshift != 0)
{
for (size_t Index = NumBlocks() - 1; Index != 0; --Index)
{
Impl.Pointer[Index] = Impl.Pointer[Index] << Bitshift | Impl.Pointer[Index - 1] >> (BlockWidth - Bitshift);
}
Impl.Pointer[0] <<= Bitshift;
}
return *this;
}
/** Performs binary shift right. */
TBitset& operator>>=(size_t Offset)
{
const size_t Blockshift = Offset / BlockWidth;
const size_t Bitshift = Offset % BlockWidth;
if (Num() == 0) return *this;
if (Num() % BlockWidth != 0)
{
Impl.Pointer[NumBlocks() - 1] &= (1ull << Num() % BlockWidth) - 1;
}
if (Blockshift != 0)
{
for (size_t Index = 0; Index != NumBlocks(); ++Index)
{
Impl.Pointer[Index] = Index < NumBlocks() - Blockshift ? Impl.Pointer[Index + Blockshift] : 0;
}
}
if (Bitshift != 0)
{
for (size_t Index = 0; Index != NumBlocks() - 1; ++Index)
{
Impl.Pointer[Index] = Impl.Pointer[Index] >> Bitshift | Impl.Pointer[Index + 1] << (BlockWidth - Bitshift);
}
Impl.Pointer[NumBlocks() - 1] >>= Bitshift;
}
return *this;
}
NODISCARD FORCEINLINE TBitset operator<<(size_t Offset) const { return TBitset(*this) <<= Offset; }
NODISCARD FORCEINLINE TBitset operator>>(size_t Offset) const { return TBitset(*this) >>= Offset; }
/** @return true if all bits are set to true, otherwise false. */
NODISCARD bool All() const
{
if (Num() == 0) return true;
for (size_t Index = 0; Index != NumBlocks() - 1; ++Index)
{
if (Impl.Pointer[Index] != -1) return false;
}
const BlockType LastBlockBitmask = Num() % BlockWidth != 0 ? (1ull << Num() % BlockWidth) - 1 : -1;
return (Impl.Pointer[NumBlocks() - 1] | ~LastBlockBitmask) == -1;
}
/** @return true if any of the bits are set to true, otherwise false. */
NODISCARD bool Any() const
{
if (Num() == 0) return false;
for (size_t Index = 0; Index != NumBlocks() - 1; ++Index)
{
if (Impl.Pointer[Index] != 0) return true;
}
const BlockType LastBlockBitmask = Num() % BlockWidth != 0 ? (1ull << Num() % BlockWidth) - 1 : -1;
return (Impl.Pointer[NumBlocks() - 1] & LastBlockBitmask) != 0;
}
/** @return true if none of the bits are set to true, otherwise false. */
NODISCARD FORCEINLINE bool None() const { return !Any(); }
/** @return The number of bits that are set to true. */
NODISCARD size_t Count() const
{
if (Num() == 0) return 0;
size_t Result = 0;
static constexpr auto BlockCount = [](BlockType Block)
{
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.Pointer[Index]);
}
const BlockType LastBlockBitmask = Num() % BlockWidth != 0 ? (1ull << Num() % BlockWidth) - 1 : -1;
Result += BlockCount(Impl.Pointer[NumBlocks() - 1] & LastBlockBitmask);
return Result;
}
/** Sets all bits to true. */
TBitset& Set(bool InValue = true)
{
Memory::Memset(Impl.Pointer, static_cast<uint8>(InValue ? -1 : 0), NumBlocks() * sizeof(BlockType));
return *this;
}
/** Flips all bits (like operator~, but in-place). */
TBitset& Flip()
{
for (size_t Index = 0; Index != NumBlocks(); ++Index)
{
Impl.Pointer[Index] = ~Impl.Pointer[Index];
}
return *this;
}
/** Flips the bit at the position 'Index'. */
TBitset& Flip(size_t Index)
{
Impl.Pointer[Index / BlockWidth] ^= 1ull << Index % BlockWidth;
return *this;
}
/** Converts the contents of the bitset to an uint64 integer. */
NODISCARD uint64 ToIntegral()
{
checkf(Num() <= 64, TEXT("The bitset can not be represented in uint64. Please check Num()."));
const uint64 Mask = Num() < 64 ? (1ull << Num()) - 1 : -1;
return *reinterpret_cast<uint64*>(Impl.Pointer) & Mask;
}
/** Appends the given bit value to the end of the bitset. */
FORCEINLINE void PushBack(bool InValue)
{
SetNum(Num() + 1);
Back() = InValue;
}
/** Removes the last bit of the bitset. The bitset cannot be empty. */
FORCEINLINE void PopBack(bool bAllowShrinking = true)
{
checkf(Num() != 0, TEXT("The bitset is empty. Please check Num()."));
SetNum(Num() - 1, bAllowShrinking);
}
/** Resizes the bitset to contain 'InCount' bits. Additional uninitialized bits are appended. */
void SetNum(size_t InCount, bool bAllowShrinking = true)
{
const size_t BlocksCount = (InCount + BlockWidth - 1) / BlockWidth;
size_t NumToAllocate = BlocksCount;
NumToAllocate = NumToAllocate > MaxBlocks() ? Impl->CalculateSlackGrow(BlocksCount, MaxBlocks()) : NumToAllocate;
NumToAllocate = NumToAllocate < MaxBlocks() ? (bAllowShrinking ? Impl->CalculateSlackShrink(BlocksCount, MaxBlocks()) : MaxBlocks()) : NumToAllocate;
if (NumToAllocate != MaxBlocks())
{
BlockType* OldAllocation = Impl.Pointer;
const size_t NumToDestruct = NumBlocks();
Impl.BitsetNum = InCount;
Impl.BlocksMax = NumToAllocate;
Impl.Pointer = Impl->Allocate(MaxBlocks());
if (NumToDestruct <= Num())
{
Memory::Memcpy(Impl.Pointer, OldAllocation, NumToDestruct * sizeof(BlockType));
}
else
{
Memory::Memcpy(Impl.Pointer, OldAllocation, BlocksCount * sizeof(BlockType));
}
Impl->Deallocate(OldAllocation);
return;
}
check(InCount <= Max());
Impl.BitsetNum = InCount;
}
/** Resizes the bitset to contain 'InCount' elements. Additional copies of 'InValue' are appended. */
void SetNum(size_t InCount, bool InValue, bool bAllowShrinking /*= true*/)
{
const size_t BlocksCount = (InCount + BlockWidth - 1) / BlockWidth;
size_t NumToAllocate = BlocksCount;
NumToAllocate = NumToAllocate > MaxBlocks() ? Impl->CalculateSlackGrow(BlocksCount, MaxBlocks()) : NumToAllocate;
NumToAllocate = NumToAllocate < MaxBlocks() ? (bAllowShrinking ? Impl->CalculateSlackShrink(BlocksCount, MaxBlocks()) : MaxBlocks()) : NumToAllocate;
const BlockType LastBlockBitmask = Num() % BlockWidth != 0 ? (1ull << Num() % BlockWidth) - 1 : -1;
const BlockType BlocksValueToSet = static_cast<BlockType>(InValue ? -1 : 0);
if (NumToAllocate != MaxBlocks())
{
BlockType* OldAllocation = Impl.Pointer;
const size_t NumToDestruct = NumBlocks();
Impl.BitsetNum = InCount;
Impl.BlocksMax = NumToAllocate;
Impl.Pointer = Impl->Allocate(MaxBlocks());
if (NumToDestruct <= NumBlocks())
{
if (NumToDestruct != 0)
{
Memory::Memcpy(Impl.Pointer, OldAllocation, (NumToDestruct - 1) * sizeof(BlockType));
Impl.Pointer[NumToDestruct - 1] = OldAllocation[NumToDestruct - 1] & LastBlockBitmask | BlocksValueToSet & ~LastBlockBitmask;
}
Memory::Memset(Impl.Pointer + NumToDestruct, static_cast<uint8>(BlocksValueToSet), (BlocksCount - NumToDestruct) * sizeof(BlockType));
}
else
{
Memory::Memcpy(Impl.Pointer, OldAllocation, BlocksCount * sizeof(BlockType));
}
Impl->Deallocate(OldAllocation);
return;
}
check(InCount <= Max());
if (InCount > Num())
{
if (NumBlocks() != 0)
{
Impl.Pointer[NumBlocks() - 1] = Impl.Pointer[NumBlocks() - 1] & LastBlockBitmask | BlocksValueToSet & ~LastBlockBitmask;
}
Memory::Memset(Impl.Pointer + NumBlocks(), static_cast<uint8>(BlocksValueToSet), (BlocksCount - NumBlocks()) * sizeof(BlockType));
}
Impl.BitsetNum = InCount;
}
/** Increase the max capacity of the bitset to a value that's greater or equal to 'InCount'. */
void Reserve(size_t InCount)
{
if (InCount <= Max()) return;
const size_t BlocksCount = (InCount + BlockWidth - 1) / BlockWidth;
const size_t NumToAllocate = Impl->CalculateSlackReserve(BlocksCount);
BlockType* OldAllocation = Impl.Pointer;
check(NumToAllocate > MaxBlocks());
Impl.BlocksMax = NumToAllocate;
Impl.Pointer = Impl->Allocate(MaxBlocks());
Memory::Memcpy(Impl.Pointer, OldAllocation, NumBlocks() * sizeof(BlockType));
Impl->Deallocate(OldAllocation);
}
/** Requests the removal of unused capacity. */
void Shrink()
{
size_t NumToAllocate = Impl->CalculateSlackReserve(NumBlocks());
check(NumToAllocate <= MaxBlocks());
if (NumToAllocate == MaxBlocks()) return;
BlockType* OldAllocation = Impl.Pointer;
Impl.BitsetNum = NumToAllocate * BlockWidth;
Impl.Pointer = Impl->Allocate(MaxBlocks());
Memory::Memcpy(Impl.Pointer, OldAllocation, NumBlocks() * sizeof(BlockType));
Impl->Deallocate(OldAllocation);
}
/** @return The pointer to the underlying element storage. */
NODISCARD FORCEINLINE TObserverPtr< BlockType[]> GetData() { return TObserverPtr< BlockType[]>(Impl.Pointer); }
NODISCARD FORCEINLINE TObserverPtr<const BlockType[]> GetData() const { return TObserverPtr<const BlockType[]>(Impl.Pointer); }
/** @return The iterator to the first or end bit. */
NODISCARD FORCEINLINE Iterator Begin() { return Iterator(this, Impl.Pointer, 0); }
NODISCARD FORCEINLINE ConstIterator Begin() const { return ConstIterator(this, Impl.Pointer, 0); }
NODISCARD FORCEINLINE Iterator End() { return Iterator(this, Impl.Pointer, Num()); }
NODISCARD FORCEINLINE ConstIterator End() const { return ConstIterator(this, Impl.Pointer, Num()); }
/** @return The reverse iterator to the first or end bit. */
NODISCARD FORCEINLINE ReverseIterator RBegin() { return ReverseIterator(End()); }
NODISCARD FORCEINLINE ConstReverseIterator RBegin() const { return ConstReverseIterator(End()); }
NODISCARD FORCEINLINE ReverseIterator REnd() { return ReverseIterator(Begin()); }
NODISCARD FORCEINLINE ConstReverseIterator REnd() const { return ConstReverseIterator(Begin()); }
/** @return The number of bits in the bitset. */
NODISCARD FORCEINLINE size_t Num() const { return Impl.BitsetNum; }
/** @return The number of bits that can be held in currently allocated storage. */
NODISCARD FORCEINLINE size_t Max() const { return MaxBlocks() * BlockWidth; }
/** @return The number of blocks in the bitset. */
NODISCARD FORCEINLINE size_t NumBlocks() const { return (Num() + BlockWidth - 1) / BlockWidth; }
/** @return The number of blocks that can be held in currently allocated storage. */
NODISCARD FORCEINLINE size_t MaxBlocks() const { return Impl.BlocksMax; }
/** @return true if the bitset is empty, false otherwise. */
NODISCARD FORCEINLINE bool IsEmpty() const { return Num() == 0; }
/** @return true if the iterator is valid, false otherwise. */
NODISCARD FORCEINLINE bool IsValidIterator(ConstIterator Iter) const { return Begin() <= Iter && Iter <= End(); }
/** @return The reference to the requested bit. */
NODISCARD FORCEINLINE Reference operator[](size_t Index) { checkf(Index < Num(), TEXT("Read access violation. Please check IsValidIterator().")); return *(Begin() + Index); }
NODISCARD FORCEINLINE 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 Reference Front() { return *Begin(); }
NODISCARD FORCEINLINE ConstReference Front() const { return *Begin(); }
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. */
void Reset(bool bAllowShrinking = true)
{
const size_t NumToAllocate = Impl->CalculateSlackReserve(0);
if (bAllowShrinking && NumToAllocate != MaxBlocks())
{
Impl->Deallocate(Impl.Pointer);
Impl.BitsetNum = 0;
Impl.BlocksMax = Impl->CalculateSlackReserve(NumBlocks());
Impl.Pointer = Impl->Allocate(MaxBlocks());
return;
}
Impl.BitsetNum = 0;
}
/** Overloads the GetTypeHash algorithm for TBitset. */
NODISCARD friend FORCEINLINE size_t GetTypeHash(const TBitset& A)
{
if (A.NumBlocks() == 0) return 855406835;
size_t Result = 0;
for (size_t Index = 0; Index != A.NumBlocks() - 1; ++Index)
{
Result = HashCombine(Result, GetTypeHash(A.Impl.Pointer[Index]));
}
const BlockType LastBlockBitmask = A.Num() % BlockWidth != 0 ? (1ull << A.Num() % BlockWidth) - 1 : -1;
return HashCombine(Result, GetTypeHash(A.Impl.Pointer[A.NumBlocks() - 1] & LastBlockBitmask));
}
/** Overloads the Swap algorithm for TBitset. */
friend void Swap(TBitset& A, TBitset& B)
{
const bool bIsTransferable =
A.Impl->IsTransferable(A.Impl.Pointer) &&
B.Impl->IsTransferable(B.Impl.Pointer);
if (bIsTransferable)
{
Swap(A.Impl.BitsetNum, B.Impl.BitsetNum);
Swap(A.Impl.BlocksMax, B.Impl.BlocksMax);
Swap(A.Impl.Pointer, B.Impl.Pointer);
return;
}
TBitset Temp = MoveTemp(A);
A = MoveTemp(B);
B = MoveTemp(Temp);
}
ENABLE_RANGE_BASED_FOR_LOOP_SUPPORT
private:
ALLOCATOR_WRAPPER_BEGIN(AllocatorType, BlockType, Impl)
{
size_t BitsetNum;
size_t BlocksMax;
BlockType* Pointer;
}
ALLOCATOR_WRAPPER_END(AllocatorType, BlockType, Impl)
public:
class Reference final : private FSingleton
{
public:
FORCEINLINE Reference& operator=(bool InValue) { Data = (Data & ~Mask) | (InValue ? Mask : 0); return *this; }
FORCEINLINE Reference& operator=(const Reference& InValue) { return *this = static_cast<bool>(InValue); }
FORCEINLINE Reference& operator&=(bool InValue) { Data &= InValue ? -1 : ~Mask; return *this; }
FORCEINLINE Reference& operator|=(bool InValue) { Data |= InValue ? Mask : 0; return *this; }
FORCEINLINE Reference& operator^=(bool InValue) { *this = InValue ^ *this; return *this; }
FORCEINLINE bool operator~() const { return !*this; }
FORCEINLINE operator bool() const { return (Data & Mask) != 0; }
private:
FORCEINLINE 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 IteratorImpl() = default;
# if DO_CHECK
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)
: Pointer(InValue.Pointer), Offset(InValue.Offset)
{ }
# endif
FORCEINLINE IteratorImpl(const IteratorImpl&) = default;
FORCEINLINE IteratorImpl(IteratorImpl&&) = default;
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 strong_ordering operator<=>(const IteratorImpl& LHS, const IteratorImpl& RHS) { return 0 <=> 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)); }
NODISCARD FORCEINLINE auto operator[](ptrdiff Index) const { IteratorImpl Temp = *this + Index; return *Temp; }
FORCEINLINE IteratorImpl& operator++() { ++Offset; CheckThis(); return *this; }
FORCEINLINE IteratorImpl& operator--() { --Offset; CheckThis(); return *this; }
FORCEINLINE IteratorImpl operator++(int) { IteratorImpl Temp = *this; ++*this; return Temp; }
FORCEINLINE IteratorImpl operator--(int) { IteratorImpl Temp = *this; --*this; return Temp; }
FORCEINLINE IteratorImpl& operator+=(ptrdiff Offset) { this->Offset += Offset; CheckThis(); return *this; }
FORCEINLINE IteratorImpl& operator-=(ptrdiff Offset) { this->Offset -= Offset; CheckThis(); return *this; }
NODISCARD friend FORCEINLINE IteratorImpl operator+(IteratorImpl Iter, ptrdiff Offset) { IteratorImpl Temp = Iter; Temp += Offset; return Temp; }
NODISCARD friend FORCEINLINE IteratorImpl operator+(ptrdiff Offset, IteratorImpl Iter) { IteratorImpl Temp = Iter; 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); }
private:
# if DO_CHECK
const TBitset* Owner = nullptr;
# endif
BlockType* Pointer = nullptr;
size_t Offset = 0;
# if DO_CHECK
FORCEINLINE IteratorImpl(const TBitset* InContainer, BlockType* InPointer, size_t InOffset)
: Owner(InContainer), Pointer(InPointer), Offset(InOffset)
{ }
# else
FORCEINLINE IteratorImpl(const TBitset* InContainer, BlockType* InPointer, size_t InOffset)
: Pointer(InPointer), Offset(InOffset)
{ }
# endif
FORCEINLINE 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 TBitset;
};
};
using FBitset = TBitset<TInlineAllocator<((40 - 3 * sizeof(size_t)) / sizeof(TBitset<FHeapAllocator>::BlockType))>>;
static_assert(sizeof(FBitset) == 40, "The byte size of FBitset is unexpected");
NAMESPACE_MODULE_END(Utility)
NAMESPACE_MODULE_END(Redcraft)
NAMESPACE_REDCRAFT_END

View File

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

View File

@ -25,6 +25,13 @@ template <typename I> struct TIteratorElementType { using Type = typename I::Ele
template <typename T> struct TIteratorElementType<T*> { using Type = T; }; template <typename T> struct TIteratorElementType<T*> { using Type = T; };
template <typename I> struct TIteratorPointerType { using Type = void; };
template <typename T> struct TIteratorPointerType<T*> { using Type = T*; };
template <typename I> requires (requires(I& Iter) { { Iter.operator->() } -> CPointer; })
struct TIteratorPointerType<I> { using Type = decltype(DeclVal<I&>().operator->()); };
NAMESPACE_PRIVATE_END NAMESPACE_PRIVATE_END
template <typename T> template <typename T>
@ -36,6 +43,9 @@ concept CDereferenceable = requires(T& A) { { *A } -> CReferenceable; };
template <typename I> template <typename I>
using TIteratorElementType = typename NAMESPACE_PRIVATE::TIteratorElementType<TRemoveCVRef<I>>::Type; using TIteratorElementType = typename NAMESPACE_PRIVATE::TIteratorElementType<TRemoveCVRef<I>>::Type;
template <typename I>
using TIteratorPointerType = typename NAMESPACE_PRIVATE::TIteratorPointerType<TRemoveCVRef<I>>::Type;
template <CReferenceable I> template <CReferenceable I>
using TIteratorReferenceType = decltype(*DeclVal<I&>()); using TIteratorReferenceType = decltype(*DeclVal<I&>());
@ -165,10 +175,10 @@ public:
template <CBidirectionalIterator J> requires (CSizedSentinelFor<J, IteratorType>) template <CBidirectionalIterator J> requires (CSizedSentinelFor<J, IteratorType>)
NODISCARD friend FORCEINLINE constexpr TCompareThreeWayResult<J, IteratorType> operator<=>(const TReverseIterator& LHS, const TReverseIterator<J>& RHS) { return RHS.GetBase() <=> LHS.GetBase(); } NODISCARD friend FORCEINLINE constexpr TCompareThreeWayResult<J, IteratorType> operator<=>(const TReverseIterator& LHS, const TReverseIterator<J>& RHS) { return RHS.GetBase() <=> LHS.GetBase(); }
NODISCARD FORCEINLINE constexpr ElementType& operator*() const { IteratorType Temp = GetBase(); return *--Temp; } NODISCARD FORCEINLINE constexpr TIteratorReferenceType<IteratorType> operator*() const { IteratorType Temp = GetBase(); return *--Temp; }
NODISCARD FORCEINLINE constexpr ElementType* operator->() const { return AddressOf(operator*()); } NODISCARD FORCEINLINE constexpr TIteratorPointerType<IteratorType> operator->() const { return AddressOf(operator*()); }
NODISCARD FORCEINLINE constexpr ElementType& operator[](ptrdiff Index) const requires (CRandomAccessIterator<IteratorType>) { return GetBase()[-Index - 1]; } NODISCARD FORCEINLINE constexpr TIteratorReferenceType<IteratorType> operator[](ptrdiff Index) const requires (CRandomAccessIterator<IteratorType>) { return GetBase()[-Index - 1]; }
FORCEINLINE constexpr TReverseIterator& operator++() { --Current; return *this; } FORCEINLINE constexpr TReverseIterator& operator++() { --Current; return *this; }
FORCEINLINE constexpr TReverseIterator& operator--() { ++Current; return *this; } FORCEINLINE constexpr TReverseIterator& operator--() { ++Current; return *this; }
@ -241,10 +251,10 @@ public:
template <CInputIterator J> requires (CSizedSentinelFor<J, IteratorType>) template <CInputIterator J> requires (CSizedSentinelFor<J, IteratorType>)
NODISCARD friend FORCEINLINE constexpr TCompareThreeWayResult<J, IteratorType> operator<=>(const TMoveIterator& LHS, const TMoveIterator<J>& RHS) { return LHS.GetBase() <=> RHS.GetBase(); } NODISCARD friend FORCEINLINE constexpr TCompareThreeWayResult<J, IteratorType> operator<=>(const TMoveIterator& LHS, const TMoveIterator<J>& RHS) { return LHS.GetBase() <=> RHS.GetBase(); }
NODISCARD FORCEINLINE constexpr ElementType&& operator*() const { return MoveTemp(*GetBase()); } NODISCARD FORCEINLINE constexpr TIteratorRValueReferenceType<IteratorType> operator*() const { return MoveTemp(*GetBase()); }
NODISCARD FORCEINLINE constexpr ElementType* operator->() const = delete; NODISCARD FORCEINLINE constexpr TIteratorPointerType<IteratorType> operator->() const = delete;
NODISCARD FORCEINLINE constexpr ElementType&& operator[](ptrdiff Index) const requires (CRandomAccessIterator<IteratorType>) { return MoveTemp(GetBase()[Index]); } NODISCARD FORCEINLINE constexpr TIteratorRValueReferenceType<IteratorType> operator[](ptrdiff Index) const requires (CRandomAccessIterator<IteratorType>) { return MoveTemp(GetBase()[Index]); }
FORCEINLINE constexpr TMoveIterator& operator++() { ++Current; return *this; } FORCEINLINE constexpr TMoveIterator& operator++() { ++Current; return *this; }
FORCEINLINE constexpr TMoveIterator& operator--() requires (CBidirectionalIterator<IteratorType>) { --Current; return *this; } FORCEINLINE constexpr TMoveIterator& operator--() requires (CBidirectionalIterator<IteratorType>) { --Current; return *this; }
@ -393,12 +403,12 @@ public:
NODISCARD FORCEINLINE constexpr strong_ordering operator<=>(FDefaultSentinel) const& { return static_cast<ptrdiff>(0) <=> Length; } NODISCARD FORCEINLINE constexpr strong_ordering operator<=>(FDefaultSentinel) const& { return static_cast<ptrdiff>(0) <=> Length; }
NODISCARD FORCEINLINE constexpr decltype(auto) operator*() { CheckThis(true); return *Current; } NODISCARD FORCEINLINE constexpr TIteratorReferenceType<IteratorType> operator*() { CheckThis(true); return *Current; }
NODISCARD FORCEINLINE constexpr decltype(auto) operator*() const { CheckThis(true); return *Current; } NODISCARD FORCEINLINE constexpr TIteratorReferenceType<IteratorType> operator*() const { CheckThis(true); return *Current; }
NODISCARD FORCEINLINE constexpr decltype(auto) operator->() { CheckThis(true); return AddressOf(operator*()); } NODISCARD FORCEINLINE constexpr TIteratorPointerType<IteratorType> operator->() { CheckThis(true); return AddressOf(operator*()); }
NODISCARD FORCEINLINE constexpr decltype(auto) operator->() const { CheckThis(true); return AddressOf(operator*()); } NODISCARD FORCEINLINE constexpr TIteratorPointerType<IteratorType> operator->() const { CheckThis(true); return AddressOf(operator*()); }
NODISCARD FORCEINLINE constexpr decltype(auto) operator[](ptrdiff Index) const requires (CRandomAccessIterator<IteratorType>) { TCountedIterator Temp = *this + Index; return *Temp; } NODISCARD FORCEINLINE constexpr TIteratorReferenceType<IteratorType> operator[](ptrdiff Index) const requires (CRandomAccessIterator<IteratorType>) { TCountedIterator Temp = *this + Index; return *Temp; }
FORCEINLINE constexpr TCountedIterator& operator++() { ++Current; --Length; CheckThis(); return *this; } FORCEINLINE constexpr TCountedIterator& operator++() { ++Current; --Length; CheckThis(); return *this; }
FORCEINLINE constexpr TCountedIterator& operator--() requires (CBidirectionalIterator<IteratorType>) { --Current; ++Length; CheckThis(); return *this; } FORCEINLINE constexpr TCountedIterator& operator--() requires (CBidirectionalIterator<IteratorType>) { --Current; ++Length; CheckThis(); return *this; }

View File

@ -20,14 +20,20 @@ NAMESPACE_MODULE_BEGIN(Utility)
template <CElementalObject T, size_t N> template <CElementalObject T, size_t N>
struct TStaticArray final struct TStaticArray final
{ {
private:
template <bool bConst>
class IteratorImpl;
public:
using ElementType = T; using ElementType = T;
using Reference = T&; using Reference = T&;
using ConstReference = const T&; using ConstReference = const T&;
using Iterator = TArrayView< T, N>::Iterator; using Iterator = IteratorImpl<false>;
using ConstIterator = TArrayView<const T, N>::Iterator; using ConstIterator = IteratorImpl<true >;
using ReverseIterator = TReverseIterator< Iterator>; using ReverseIterator = TReverseIterator< Iterator>;
using ConstReverseIterator = TReverseIterator<ConstIterator>; using ConstReverseIterator = TReverseIterator<ConstIterator>;
@ -87,10 +93,10 @@ struct TStaticArray final
NODISCARD FORCEINLINE constexpr TObserverPtr<const ElementType[]> GetData() const { return TObserverPtr<const ElementType[]>(_); } NODISCARD FORCEINLINE constexpr TObserverPtr<const ElementType[]> GetData() const { return TObserverPtr<const ElementType[]>(_); }
/** @return The iterator to the first or end element. */ /** @return The iterator to the first or end element. */
NODISCARD FORCEINLINE constexpr Iterator Begin() { return TArrayView< T, N>(*this).Begin(); } NODISCARD FORCEINLINE constexpr Iterator Begin() { return Iterator(this, _); }
NODISCARD FORCEINLINE constexpr ConstIterator Begin() const { return TArrayView<const T, N>(*this).Begin(); } NODISCARD FORCEINLINE constexpr ConstIterator Begin() const { return ConstIterator(this, _); }
NODISCARD FORCEINLINE constexpr Iterator End() { return TArrayView< T, N>(*this).End(); } NODISCARD FORCEINLINE constexpr Iterator End() { return Iterator(this, _ + Num()); }
NODISCARD FORCEINLINE constexpr ConstIterator End() const { return TArrayView<const T, N>(*this).End(); } NODISCARD FORCEINLINE constexpr ConstIterator End() const { return ConstIterator(this, _ + Num()); }
/** @return The reverse iterator to the first or end element. */ /** @return The reverse iterator to the first or end element. */
NODISCARD FORCEINLINE constexpr ReverseIterator RBegin() { return ReverseIterator(End()); } NODISCARD FORCEINLINE constexpr ReverseIterator RBegin() { return ReverseIterator(End()); }
@ -137,6 +143,89 @@ struct TStaticArray final
T _[N]; T _[N];
private:
template <bool bConst>
class IteratorImpl
{
public:
using ElementType = TConditional<bConst, const T, T>;
FORCEINLINE constexpr IteratorImpl() = default;
# if DO_CHECK
FORCEINLINE IteratorImpl(const IteratorImpl<false>& InValue) requires (bConst)
: Owner(InValue.Owner), Pointer(InValue.Pointer)
{ }
# else
FORCEINLINE IteratorImpl(const IteratorImpl<false>& InValue) requires (bConst)
: Pointer(InValue.Pointer)
{ }
# 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) { return LHS.Pointer == RHS.Pointer; }
NODISCARD friend FORCEINLINE constexpr strong_ordering operator<=>(const IteratorImpl& LHS, const IteratorImpl& RHS) { return LHS.Pointer <=> RHS.Pointer; }
NODISCARD FORCEINLINE constexpr ElementType& operator*() const { CheckThis(true); return *Pointer; }
NODISCARD FORCEINLINE constexpr ElementType* operator->() const { CheckThis(true); return Pointer; }
NODISCARD FORCEINLINE constexpr ElementType& operator[](ptrdiff Index) const { IteratorImpl Temp = *this + Index; return *Temp; }
FORCEINLINE constexpr IteratorImpl& operator++() { ++Pointer; CheckThis(); return *this; }
FORCEINLINE constexpr IteratorImpl& operator--() { --Pointer; 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) { Pointer += Offset; CheckThis(); return *this; }
FORCEINLINE constexpr IteratorImpl& operator-=(ptrdiff Offset) { Pointer -= 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) { LHS.CheckThis(); RHS.CheckThis(); return LHS.Pointer - RHS.Pointer; }
NODISCARD FORCEINLINE constexpr explicit operator TObserverPtr<ElementType[]>() const { CheckThis(); return TObserverPtr<ElementType[]>(Pointer); }
private:
# if DO_CHECK
const TStaticArray* Owner = nullptr;
# endif
ElementType* Pointer = nullptr;
# if DO_CHECK
FORCEINLINE constexpr IteratorImpl(const TStaticArray* InContainer, ElementType* InPointer)
: Owner(InContainer), Pointer(InPointer)
{ }
# else
FORCEINLINE constexpr IteratorImpl(const TStaticArray* InContainer, ElementType* InPointer)
: Pointer(InPointer)
{ }
# 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 TStaticArray;
};
}; };
template <typename T, typename... U> requires (true && ... && CSameAs<T, U>) template <typename T, typename... U> requires (true && ... && CSameAs<T, U>)

View File

@ -12,8 +12,17 @@ NAMESPACE_MODULE_BEGIN(Utility)
struct FAllocatorInterface; struct FAllocatorInterface;
template <typename T> template <typename A, typename T = int>
concept CInstantiableAllocator = CDerivedFrom<T, FAllocatorInterface> && !CSameAs<T, FAllocatorInterface>; concept CInstantiableAllocator = !CSameAs<A, FAllocatorInterface>
&& requires (typename A::template ForElementType<T>& Allocator, T* InPtr, size_t Num, size_t NumAllocated)
{
{ Allocator.Allocate(Num) } -> CSameAs<T*>;
{ Allocator.Deallocate(InPtr) } -> CSameAs<void>;
{ AsConst(Allocator).IsTransferable(InPtr) } -> CBooleanTestable;
{ AsConst(Allocator).CalculateSlackGrow(Num, NumAllocated) } -> CSameAs<size_t>;
{ AsConst(Allocator).CalculateSlackShrink(Num, NumAllocated) } -> CSameAs<size_t>;
{ AsConst(Allocator).CalculateSlackReserve(Num) } -> CSameAs<size_t>;
};
/** /**
* This is the allocator interface, the allocator does not use virtual, this contains the default of * This is the allocator interface, the allocator does not use virtual, this contains the default of
@ -24,10 +33,16 @@ concept CInstantiableAllocator = CDerivedFrom<T, FAllocatorInterface> && !CSameA
struct FAllocatorInterface struct FAllocatorInterface
{ {
template <CObject T> template <CObject T>
class ForElementType : private FSingleton class ForElementType /*: private FSingleton*/
{ {
public: public:
ForElementType() = default;
ForElementType(const ForElementType&) = delete;
ForElementType(ForElementType&&) = delete;
ForElementType& operator=(const ForElementType&) = delete;
ForElementType& operator=(ForElementType&&) = delete;
/** /**
* Allocates uninitialized storage. * Allocates uninitialized storage.
* Should be allocated according to the results given by the CalculateSlackReserve() family, * Should be allocated according to the results given by the CalculateSlackReserve() family,
@ -57,7 +72,7 @@ struct FAllocatorInterface
#define ALLOCATOR_WRAPPER_BEGIN(Allocator, Type, Name) \ #define ALLOCATOR_WRAPPER_BEGIN(Allocator, Type, Name) \
\ \
struct PREPROCESSOR_JOIN(F, Name) : private FSingleton struct PREPROCESSOR_JOIN(F, Name) /*: private FSingleton*/
#define ALLOCATOR_WRAPPER_END(Allocator, Type, Name) ; \ #define ALLOCATOR_WRAPPER_END(Allocator, Type, Name) ; \
\ \
@ -90,13 +105,19 @@ struct FAllocatorInterface
PREPROCESSOR_JOIN(T, Name)<typename Allocator::template ForElementType<Type>> Name; PREPROCESSOR_JOIN(T, Name)<typename Allocator::template ForElementType<Type>> Name;
/** This is heap allocator that calls Memory::Malloc() directly for memory allocation. */ /** This is heap allocator that calls Memory::Malloc() directly for memory allocation. */
struct FHeapAllocator : public FAllocatorInterface struct FHeapAllocator
{ {
template <CObject T> template <CObject T>
class ForElementType : public FAllocatorInterface::ForElementType<T> class ForElementType
{ {
public: public:
ForElementType() = default;
ForElementType(const ForElementType&) = delete;
ForElementType(ForElementType&&) = delete;
ForElementType& operator=(const ForElementType&) = delete;
ForElementType& operator=(ForElementType&&) = delete;
NODISCARD FORCEINLINE T* Allocate(size_t InNum) NODISCARD FORCEINLINE T* Allocate(size_t InNum)
{ {
return InNum != 0 ? static_cast<T*>(Memory::Malloc(Memory::QuantizeSize(InNum * sizeof(T)), alignof(T))) : nullptr; return InNum != 0 ? static_cast<T*>(Memory::Malloc(Memory::QuantizeSize(InNum * sizeof(T)), alignof(T))) : nullptr;
@ -107,6 +128,8 @@ struct FHeapAllocator : public FAllocatorInterface
Memory::Free(InPtr); Memory::Free(InPtr);
} }
NODISCARD FORCEINLINE bool IsTransferable(T* InPtr) const { return true; }
NODISCARD FORCEINLINE size_t CalculateSlackGrow(size_t Num, size_t NumAllocated) const NODISCARD FORCEINLINE size_t CalculateSlackGrow(size_t Num, size_t NumAllocated) const
{ {
const size_t FirstGrow = 4; const size_t FirstGrow = 4;
@ -159,13 +182,19 @@ struct FHeapAllocator : public FAllocatorInterface
* Any allocation needed beyond that causes all data to be moved into an indirect allocation. * Any allocation needed beyond that causes all data to be moved into an indirect allocation.
*/ */
template <size_t NumInline, CInstantiableAllocator SecondaryAllocator = FHeapAllocator> template <size_t NumInline, CInstantiableAllocator SecondaryAllocator = FHeapAllocator>
struct TInlineAllocator : public FAllocatorInterface struct TInlineAllocator
{ {
template <CObject T> template <CObject T>
class ForElementType : public FAllocatorInterface::ForElementType<T> class ForElementType
{ {
public: public:
ForElementType() = default;
ForElementType(const ForElementType&) = delete;
ForElementType(ForElementType&&) = delete;
ForElementType& operator=(const ForElementType&) = delete;
ForElementType& operator=(ForElementType&&) = delete;
NODISCARD FORCEINLINE T* Allocate(size_t InNum) NODISCARD FORCEINLINE T* Allocate(size_t InNum)
{ {
if (InNum == 0) return nullptr; if (InNum == 0) return nullptr;
@ -233,13 +262,19 @@ struct TInlineAllocator : public FAllocatorInterface
}; };
/** This is a null allocator for which all operations are illegal. */ /** This is a null allocator for which all operations are illegal. */
struct FNullAllocator : public FAllocatorInterface struct FNullAllocator
{ {
template <CObject T> template <CObject T>
class ForElementType : public FAllocatorInterface::ForElementType<T> class ForElementType
{ {
public: public:
ForElementType() = default;
ForElementType(const ForElementType&) = delete;
ForElementType(ForElementType&&) = delete;
ForElementType& operator=(const ForElementType&) = delete;
ForElementType& operator=(ForElementType&&) = delete;
NODISCARD FORCEINLINE T* Allocate(size_t InNum) { check_no_entry(); return nullptr; } NODISCARD FORCEINLINE T* Allocate(size_t InNum) { check_no_entry(); return nullptr; }
FORCEINLINE void Deallocate(T* InPtr) { check_no_entry(); } FORCEINLINE void Deallocate(T* InPtr) { check_no_entry(); }

View File

@ -6,6 +6,8 @@ NAMESPACE_REDCRAFT_BEGIN
NAMESPACE_MODULE_BEGIN(Redcraft) NAMESPACE_MODULE_BEGIN(Redcraft)
NAMESPACE_MODULE_BEGIN(Utility) NAMESPACE_MODULE_BEGIN(Utility)
// WARNING: Using these helper classes as base classes may raise potential EBO issues and is not recommended for objects with strict size constraints.
/** A class indicates that a derived class cannot be copied. */ /** A class indicates that a derived class cannot be copied. */
struct FNoncopyable struct FNoncopyable
{ {

View File

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