14template <
typename ElementType>
30 check(NumElements > 0);
32 const int32 InsertIndex = SpanAllocator.
Allocate(NumElements);
43 for (
int32 ElementIndex = InsertIndex; ElementIndex < InsertIndex + NumElements; ++ElementIndex)
46 Elements[ElementIndex] = ElementType();
49 AllocatedElementsBitArray.
SetRange(InsertIndex, NumElements,
true);
56 check(NumElements > 0);
61 Elements[ElementIndex] = ElementType();
72 if (Elements.
Num() > SpanAllocator.
GetMaxSize() * ShrinkThreshold)
82 SpanAllocator.
Reset();
89 return Elements[
Index];
95 return Elements[
Index];
100 if (ElementIndex <
Num())
102 return AllocatedElementsBitArray[ElementIndex];
121 while (ElementIndex < Array.Num() && !Array.AllocatedElementsBitArray[ElementIndex])
133 }
while (ElementIndex < Array.Num() && !Array.AllocatedElementsBitArray[ElementIndex]);
140 return ElementIndex !=
Other.ElementIndex;
145 return Array.Elements[ElementIndex];
161 while (ElementIndex < Array.Num() && !Array.AllocatedElementsBitArray[ElementIndex])
173 }
while (ElementIndex < Array.Num() && !Array.AllocatedElementsBitArray[ElementIndex]);
180 return ElementIndex !=
Other.ElementIndex;
185 return Array.Elements[ElementIndex];
201 static constexpr int32 ShrinkThreshold = 2;
208template <
typename ElementType, u
int32 BytesPerChunk = 2 * 1024 * 1024>
230 FreeElementIndexHint =
Other.FreeElementIndexHint;
231 NumAllocatedElements =
Other.NumAllocatedElements;
232 MaxAllocatedElementIndexPlusOne =
Other.MaxAllocatedElementIndexPlusOne;
233 AllocatedElementsBitArray =
Other.AllocatedElementsBitArray;
239 ElementChunks.
Add((ElementType*)FMemory::Malloc(BytesPerChunk));
244 const int32 ElementIndex = It.GetIndex();
245 ElementType& Element = (*this)[ElementIndex];
246 new (&Element) ElementType(
Other[ElementIndex]);
259 FreeElementIndexHint =
Other.FreeElementIndexHint;
260 NumAllocatedElements =
Other.NumAllocatedElements;
261 MaxAllocatedElementIndexPlusOne =
Other.MaxAllocatedElementIndexPlusOne;
262 Other.FreeElementIndexHint = 0;
263 Other.NumAllocatedElements = 0;
264 Other.MaxAllocatedElementIndexPlusOne = 0;
266 AllocatedElementsBitArray =
MoveTemp(
Other.AllocatedElementsBitArray);
280 for (ElementType& Element : *
this)
282 Element.~ElementType();
285 for (ElementType* Chunk : ElementChunks)
290 ElementChunks.
Empty();
291 AllocatedElementsBitArray.
Empty();
292 FreeElementIndexHint = 0;
293 NumAllocatedElements = 0;
294 MaxAllocatedElementIndexPlusOne = 0;
299 return MaxAllocatedElementIndexPlusOne;
306 AllocatedElementsBitArray.
SetNum(FMath::Max(ElementChunks.
Num(),
NewNumChunks) * ElementsPerChunk,
false);
310 ElementChunks.
Add((ElementType*)FMemory::Malloc(BytesPerChunk));
318 if (NumAllocatedElements == MaxAllocatedElementIndexPlusOne)
320 check(MaxAllocatedElementIndexPlusOne <= AllocatedElementsBitArray.
Num());
321 InsertIndex = MaxAllocatedElementIndexPlusOne;
322 ++MaxAllocatedElementIndexPlusOne;
324 if (InsertIndex < AllocatedElementsBitArray.
Num())
326 AllocatedElementsBitArray[InsertIndex] =
true;
331 check(NumAllocatedElements < MaxAllocatedElementIndexPlusOne);
333 check(InsertIndex < MaxAllocatedElementIndexPlusOne);
336 FreeElementIndexHint = InsertIndex + 1;
337 ++NumAllocatedElements;
340 if (MaxAllocatedElementIndexPlusOne > ElementChunks.
Num() * ElementsPerChunk)
342 check(InsertIndex + 1 == MaxAllocatedElementIndexPlusOne);
343 check(InsertIndex == ElementChunks.
Num() * ElementsPerChunk);
344 check(AllocatedElementsBitArray.
Num() == ElementChunks.
Num() * ElementsPerChunk);
346 ElementChunks.
Add((ElementType*)FMemory::Malloc(BytesPerChunk));
347 AllocatedElementsBitArray.
Add(
false, ElementsPerChunk);
348 AllocatedElementsBitArray[InsertIndex] =
true;
352 const int32 ChunkIndex = InsertIndex / ElementsPerChunk;
353 const int32 Index = InsertIndex - ChunkIndex * ElementsPerChunk;
355 new (&ElementChunks[ChunkIndex][
Index]) ElementType;
363 const int32 ChunkIndex = ElementIndex / ElementsPerChunk;
364 const int32 Index = ElementIndex - ChunkIndex * ElementsPerChunk;
365 ElementChunks[ChunkIndex][
Index].~ElementType();
367 AllocatedElementsBitArray[ElementIndex] =
false;
369 --NumAllocatedElements;
370 FreeElementIndexHint = FMath::Min(FreeElementIndexHint, ElementIndex);
371 if (ElementIndex + 1 == MaxAllocatedElementIndexPlusOne)
373 MaxAllocatedElementIndexPlusOne = AllocatedElementsBitArray.
FindLastFrom(
true, ElementIndex - 1) + 1;
375 check(FreeElementIndexHint <= MaxAllocatedElementIndexPlusOne);
383 AllocatedElementsBitArray.
SetNumUninitialized(FMath::Min(ElementChunks.
Num(), ChunksToKeep) * ElementsPerChunk);
385 while (ElementChunks.
Num() > ChunksToKeep)
395 const int32 ChunkIndex = ElementIndex / ElementsPerChunk;
396 const int32 Index = ElementIndex - ChunkIndex * ElementsPerChunk;
397 return ElementChunks[ChunkIndex][
Index];
403 const int32 ChunkIndex = ElementIndex / ElementsPerChunk;
404 const int32 Index = ElementIndex - ChunkIndex * ElementsPerChunk;
405 return ElementChunks[ChunkIndex][
Index];
412 return AllocatedElementsBitArray[ElementIndex];
431 while (ElementIndex < Array.GetMaxSize() && !Array.AllocatedElementsBitArray[ElementIndex])
443 }
while (ElementIndex < Array.GetMaxSize() && !Array.AllocatedElementsBitArray[ElementIndex]);
450 return ElementIndex !=
Other.ElementIndex;
455 return Array[ElementIndex];
471 while (ElementIndex < Array.GetMaxSize() && !Array.AllocatedElementsBitArray[ElementIndex])
483 }
while (ElementIndex < Array.GetMaxSize() && !Array.AllocatedElementsBitArray[ElementIndex]);
490 return ElementIndex !=
Other.ElementIndex;
495 return Array[ElementIndex];
510 static constexpr int32 ElementsPerChunk = BytesPerChunk /
sizeof(ElementType);
512 int32 FreeElementIndexHint = 0;
513 int32 NumAllocatedElements = 0;
514 int32 MaxAllocatedElementIndexPlusOne = 0;
#define checkSlow(expr)
Definition AssertionMacros.h:332
#define check(expr)
Definition AssertionMacros.h:314
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
UE_INTRINSIC_CAST UE_REWRITE constexpr std::remove_reference_t< T > && MoveTemp(T &&Obj) noexcept
Definition UnrealTemplate.h:520
Definition SpanAllocator.h:15
void Free(int32 BaseOffset, int32 Num=1)
Definition SpanAllocator.h:63
int32 GetMaxSize() const
Definition SpanAllocator.h:80
ENGINE_API void Consolidate()
Definition SpanAllocator.cpp:58
SIZE_T GetAllocatedSize() const
Definition SpanAllocator.h:95
int32 Allocate(int32 Num=1)
Definition SpanAllocator.h:23
ENGINE_API void Reset()
Definition SpanAllocator.cpp:15
UE_REWRITE SizeType Num() const
Definition Array.h:1144
UE_NODEBUG UE_FORCEINLINE_HINT ElementType & Last(SizeType IndexFromTheEnd=0) UE_LIFETIMEBOUND
Definition Array.h:1263
void Reset(SizeType NewSize=0)
Definition Array.h:2246
SizeType AddDefaulted()
Definition Array.h:2795
void SetNum(SizeType NewNum, EAllowShrinking AllowShrinking=UE::Core::Private::AllowShrinkingByDefault< AllocatorType >())
Definition Array.h:2308
UE_NODEBUG UE_FORCEINLINE_HINT SizeType Add(ElementType &&Item)
Definition Array.h:2696
UE_NODEBUG UE_FORCEINLINE_HINT SIZE_T GetAllocatedSize(void) const
Definition Array.h:1059
ElementType Pop(EAllowShrinking AllowShrinking=UE::Core::Private::AllowShrinkingByDefault< AllocatorType >())
Definition Array.h:1196
void Empty(SizeType Slack=0)
Definition Array.h:2273
UE_FORCEINLINE_HINT void Reserve(SizeType Number)
Definition Array.h:3016
UE_FORCEINLINE_HINT int32 Num() const
Definition BitArray.h:1466
int32 Add(const bool Value)
Definition BitArray.h:615
void Empty(int32 ExpectedNumBits=0)
Definition BitArray.h:779
int32 FindAndSetFirstZeroBit(int32 StartIndex=0)
Definition BitArray.h:1258
uint32 GetAllocatedSize(void) const
Definition BitArray.h:1062
void SetNumUninitialized(int32 InNumBits)
Definition BitArray.h:849
FORCENOINLINE void SetNum(int32 InNumBits, ValueType bValue)
Definition BitArray.h:870
FORCENOINLINE void SetRange(int32 Index, int32 NumBitsToSet, bool Value)
Definition BitArray.h:887
int32 FindLastFrom(bool bValue, IndexType EndIndexInclusive) const
Definition BitArray.h:1228
Definition LumenSparseSpanArray.h:464
const ElementType & operator*() const
Definition LumenSparseSpanArray.h:493
TRangedForConstIterator(const TChunkedSparseArray< ElementType > &InArray, int32 InElementIndex)
Definition LumenSparseSpanArray.h:466
TRangedForConstIterator operator++()
Definition LumenSparseSpanArray.h:477
bool operator!=(const TRangedForConstIterator &Other) const
Definition LumenSparseSpanArray.h:488
Definition LumenSparseSpanArray.h:424
ElementType & operator*()
Definition LumenSparseSpanArray.h:453
TRangedForIterator(TChunkedSparseArray< ElementType > &InArray, int32 InElementIndex)
Definition LumenSparseSpanArray.h:426
bool operator!=(const TRangedForIterator &Other) const
Definition LumenSparseSpanArray.h:448
TRangedForIterator operator++()
Definition LumenSparseSpanArray.h:437
Definition LumenSparseSpanArray.h:210
void Empty()
Definition LumenSparseSpanArray.h:278
void Reserve(int32 NumElements)
Definition LumenSparseSpanArray.h:302
void Shrink()
Definition LumenSparseSpanArray.h:378
TRangedForIterator end()
Definition LumenSparseSpanArray.h:505
const ElementType & operator[](int32 ElementIndex) const
Definition LumenSparseSpanArray.h:400
SIZE_T GetAllocatedSize() const
Definition LumenSparseSpanArray.h:418
TChunkedSparseArray(TChunkedSparseArray &&Other)
Definition LumenSparseSpanArray.h:219
~TChunkedSparseArray()
Definition LumenSparseSpanArray.h:273
TChunkedSparseArray & operator=(TChunkedSparseArray &&Other)
Definition LumenSparseSpanArray.h:253
int32 GetMaxSize() const
Definition LumenSparseSpanArray.h:297
int32 AddDefaulted()
Definition LumenSparseSpanArray.h:314
bool IsAllocated(int32 ElementIndex) const
Definition LumenSparseSpanArray.h:408
TRangedForConstIterator end() const
Definition LumenSparseSpanArray.h:507
TRangedForConstIterator begin() const
Definition LumenSparseSpanArray.h:506
void RemoveAt(int32 ElementIndex)
Definition LumenSparseSpanArray.h:360
TChunkedSparseArray()=default
TRangedForIterator begin()
Definition LumenSparseSpanArray.h:504
TChunkedSparseArray(const TChunkedSparseArray &Other)
Definition LumenSparseSpanArray.h:214
TChunkedSparseArray & operator=(const TChunkedSparseArray &Other)
Definition LumenSparseSpanArray.h:224
ElementType & operator[](int32 ElementIndex)
Definition LumenSparseSpanArray.h:392
Definition BitArray.h:1944
UE_FORCEINLINE_HINT int32 GetIndex() const
Definition BitArray.h:2011
Definition LumenSparseSpanArray.h:154
TRangedForConstIterator(const TSparseSpanArray< ElementType > &InArray, int32 InElementIndex)
Definition LumenSparseSpanArray.h:156
const ElementType & operator*() const
Definition LumenSparseSpanArray.h:183
TRangedForConstIterator operator++()
Definition LumenSparseSpanArray.h:167
bool operator!=(const TRangedForConstIterator &Other) const
Definition LumenSparseSpanArray.h:178
Definition LumenSparseSpanArray.h:114
ElementType & operator*()
Definition LumenSparseSpanArray.h:143
bool operator!=(const TRangedForIterator &Other) const
Definition LumenSparseSpanArray.h:138
TRangedForIterator(TSparseSpanArray< ElementType > &InArray, int32 InElementIndex)
Definition LumenSparseSpanArray.h:116
TRangedForIterator operator++()
Definition LumenSparseSpanArray.h:127
Definition LumenSparseSpanArray.h:16
bool IsAllocated(int32 ElementIndex) const
Definition LumenSparseSpanArray.h:98
void Reserve(int32 NumElements)
Definition LumenSparseSpanArray.h:23
SIZE_T GetAllocatedSize() const
Definition LumenSparseSpanArray.h:108
TRangedForConstIterator begin() const
Definition LumenSparseSpanArray.h:196
ElementType & operator[](int32 Index)
Definition LumenSparseSpanArray.h:86
TRangedForIterator end()
Definition LumenSparseSpanArray.h:195
const ElementType & operator[](int32 Index) const
Definition LumenSparseSpanArray.h:92
TRangedForIterator begin()
Definition LumenSparseSpanArray.h:194
void Reset()
Definition LumenSparseSpanArray.h:79
void RemoveSpan(int32 FirstElementIndex, int32 NumElements)
Definition LumenSparseSpanArray.h:54
int32 AddSpan(int32 NumElements)
Definition LumenSparseSpanArray.h:28
TRangedForConstIterator end() const
Definition LumenSparseSpanArray.h:197
int32 Num() const
Definition LumenSparseSpanArray.h:18
void Consolidate()
Definition LumenSparseSpanArray.h:68
U16 Index
Definition radfft.cpp:71
static constexpr UE_FORCEINLINE_HINT T DivideAndRoundUp(T Dividend, T Divisor)
Definition UnrealMathUtility.h:694
static FORCENOINLINE CORE_API void Free(void *Original)
Definition UnrealMemory.cpp:685