Files
Project-Reboot-3.0/Project Reboot 3.0/SparseArray.h
2023-03-05 14:01:15 -05:00

66 lines
1.7 KiB
C++

#pragma once
#include "Array.h"
#include "BitArray.h"
template <typename ElementType>
union TSparseArrayElementOrListLink
{
TSparseArrayElementOrListLink(ElementType& InElement)
: ElementData(InElement)
{
}
TSparseArrayElementOrListLink(ElementType&& InElement)
: ElementData(InElement)
{
}
TSparseArrayElementOrListLink(int32 InPrevFree, int32 InNextFree)
: PrevFreeIndex(InPrevFree)
, NextFreeIndex(InNextFree)
{
}
TSparseArrayElementOrListLink<ElementType> operator=(const TSparseArrayElementOrListLink<ElementType>& Other)
{
return TSparseArrayElementOrListLink(Other.NextFreeIndex, Other.PrevFreeIndex);
}
/** If the element is allocated, its value is stored here. */
ElementType ElementData;
struct
{
/** If the element isn't allocated, this is a link to the previous element in the array's free list. */
int PrevFreeIndex;
/** If the element isn't allocated, this is a link to the next element in the array's free list. */
int NextFreeIndex;
};
};
template <typename ArrayType>
class TSparseArray
{
public:
typedef TSparseArrayElementOrListLink<ArrayType> FSparseArrayElement;
TArray<FSparseArrayElement> Data;
TBitArray AllocationFlags;
int32 FirstFreeIndex;
int32 NumFreeIndices;
FORCEINLINE FSparseArrayElement& operator[](uint32 Index)
{
return *(FSparseArrayElement*)&Data.at(Index).ElementData;
}
FORCEINLINE const FSparseArrayElement& operator[](uint32 Index) const
{
return *(const FSparseArrayElement*)&Data.at(Index).ElementData;
}
FORCEINLINE int32 Num() const
{
return Data.Num() - NumFreeIndices;
}
};