Ark Server API (ASA) - Wiki
Loading...
Searching...
No Matches
TSparseArray< InElementType, Allocator > Class Template Reference

#include <SparseArray.h>

+ Collaboration diagram for TSparseArray< InElementType, Allocator >:

Classes

class  FElementCompareClass
 
class  TBaseIterator
 
class  TConstIterator
 
class  TConstSubsetIterator
 
class  TIterator
 
class  TRangedForConstIterator
 
class  TRangedForIterator
 

Public Member Functions

 ~TSparseArray ()
 
FSparseArrayAllocationInfo AllocateIndex (int32 Index)
 
FSparseArrayAllocationInfo AddUninitialized ()
 
int32 Add (const ElementType &Element)
 
int32 Add (ElementType &&Element)
 
FSparseArrayAllocationInfo AddUninitializedAtLowestFreeIndex (int32 &LowestFreeIndexSearchStart)
 
int32 AddAtLowestFreeIndex (const ElementType &Element, int32 &LowestFreeIndexSearchStart)
 
template<typename... ArgsType>
FORCEINLINE int32 Emplace (ArgsType &&... Args)
 
template<typename... ArgsType>
FORCEINLINE int32 EmplaceAtLowestFreeIndex (int32 &LowestFreeIndexSearchStart, ArgsType &&... Args)
 
template<typename... ArgsType>
FORCEINLINE int32 EmplaceAt (int32 Index, ArgsType &&... Args)
 
FSparseArrayAllocationInfo InsertUninitialized (int32 Index)
 
void Insert (int32 Index, typename TTypeTraits< ElementType >::ConstInitType Element)
 
void RemoveAt (int32 Index, int32 Count=1)
 
void RemoveAtUninitialized (int32 Index, int32 Count=1)
 
void Empty (int32 ExpectedNumElements=0)
 
void Reset ()
 
void Reserve (int32 ExpectedNumElements)
 
void Shrink ()
 
bool Compact ()
 
bool CompactStable ()
 
template<typename PREDICATE_CLASS >
void Sort (const PREDICATE_CLASS &Predicate)
 
void Sort ()
 
template<typename PREDICATE_CLASS >
void StableSort (const PREDICATE_CLASS &Predicate)
 
void StableSort ()
 
void SortFreeList ()
 
SIZE_T GetAllocatedSize (void) const
 
void CountBytes (FArchive &Ar) const
 
bool IsCompact () const
 
bool operator== (const TSparseArray &B) const
 
bool operator!= (const TSparseArray &B) const
 
 TSparseArray ()
 
 TSparseArray (TSparseArray &&InCopy)
 
 TSparseArray (const TSparseArray &InCopy)
 
TSparseArrayoperator= (TSparseArray &&InCopy)
 
TSparseArrayoperator= (const TSparseArray &InCopy)
 
ElementTypeoperator[] (int32 Index)
 
const ElementTypeoperator[] (int32 Index) const
 
int32 PointerToIndex (const ElementType *Ptr) const
 
bool IsValidIndex (int32 Index) const
 
bool IsAllocated (int32 Index) const
 
int32 GetMaxIndex () const
 
bool IsEmpty () const
 
int32 Num () const
 
FORCEINLINE void CheckAddress (const ElementType *Addr) const
 
TIterator CreateIterator ()
 
TConstIterator CreateConstIterator () const
 
FORCEINLINE TRangedForIterator begin ()
 
FORCEINLINE TRangedForConstIterator begin () const
 
FORCEINLINE TRangedForIterator end ()
 
FORCEINLINE TRangedForConstIterator end () const
 
TSparseArrayoperator+= (const TSparseArray &OtherArray)
 
TSparseArrayoperator+= (const TArray< ElementType > &OtherArray)
 
void WriteMemoryImage (FMemoryImageWriter &Writer) const
 
void CopyUnfrozen (const FMemoryUnfreezeContent &Context, void *Dst) const
 

Static Public Member Functions

static void AppendHash (const FPlatformTypeLayoutParameters &LayoutParams, FSHA1 &Hasher)
 

Private Types

using ElementType = InElementType
 
typedef TSparseArrayElementOrFreeListLink< TAlignedBytes< sizeof(ElementType), alignof(ElementType)> > FElementOrFreeListLink
 
typedef TArray< FElementOrFreeListLink, typename Allocator::ElementAllocator > DataType
 
typedef TBitArray< typename Allocator::BitArrayAllocator > AllocationBitArrayType
 

Static Private Member Functions

template<typename SparseArrayType >
static FORCEINLINE void Move (SparseArrayType &ToArray, SparseArrayType &FromArray)
 

Private Attributes

DataType Data
 
AllocationBitArrayType AllocationFlags
 
int32 FirstFreeIndex
 
int32 NumFreeIndices
 

Friends

template<typename , typename >
class TScriptSparseArray
 

Detailed Description

template<typename InElementType, typename Allocator>
class TSparseArray< InElementType, Allocator >

A dynamically sized array where element indices aren't necessarily contiguous. Memory is allocated for all elements in the array's index range, so it doesn't save memory; but it does allow O(1) element removal that doesn't invalidate the indices of subsequent elements. It uses TArray to store the elements, and a TBitArray to store whether each element index is allocated (for fast iteration over allocated elements).

Definition at line 74 of file SparseArray.h.

Member Typedef Documentation

◆ AllocationBitArrayType

typedef TBitArray<typename Allocator::BitArrayAllocator> TSparseArray< InElementType, Allocator >::AllocationBitArrayType
private

Definition at line 1118 of file SparseArray.h.

◆ DataType

Definition at line 1115 of file SparseArray.h.

◆ ElementType

Definition at line 76 of file SparseArray.h.

◆ FElementOrFreeListLink

The element type stored is only indirectly related to the element type requested, to avoid instantiating TArray redundantly for compatible types.

Definition at line 1096 of file SparseArray.h.

Constructor & Destructor Documentation

◆ ~TSparseArray()

Destructor.

Definition at line 83 of file SparseArray.h.

◆ TSparseArray() [1/3]

Default constructor.

Definition at line 728 of file SparseArray.h.

◆ TSparseArray() [2/3]

Move constructor.

Definition at line 734 of file SparseArray.h.

◆ TSparseArray() [3/3]

Copy constructor.

Definition at line 740 of file SparseArray.h.

Member Function Documentation

◆ Add() [1/2]

Adds an element to the array.

Definition at line 138 of file SparseArray.h.

◆ Add() [2/2]

Adds an element to the array.

Definition at line 146 of file SparseArray.h.

◆ AddAtLowestFreeIndex()

int32 TSparseArray< InElementType, Allocator >::AddAtLowestFreeIndex ( const ElementType & Element,
int32 & LowestFreeIndexSearchStart )
inline

Add an element at the lowest free index, instead of the last freed index. This requires a search which can be accelerated with LowestFreeIndexSearchStart.

Definition at line 205 of file SparseArray.h.

◆ AddUninitialized()

Allocates space for an element in the array. The element is not initialized, and you must use the corresponding placement new operator to construct the element in the allocated memory.

Definition at line 111 of file SparseArray.h.

◆ AddUninitializedAtLowestFreeIndex()

FSparseArrayAllocationInfo TSparseArray< InElementType, Allocator >::AddUninitializedAtLowestFreeIndex ( int32 & LowestFreeIndexSearchStart)
inline

Definition at line 153 of file SparseArray.h.

◆ AllocateIndex()

Marks an index as allocated, and returns information about the allocation.

Definition at line 90 of file SparseArray.h.

◆ AppendHash()

static void TSparseArray< InElementType, Allocator >::AppendHash ( const FPlatformTypeLayoutParameters & LayoutParams,
FSHA1 & Hasher )
inlinestatic

Definition at line 1208 of file SparseArray.h.

◆ begin() [1/2]

DO NOT USE DIRECTLY STL-like iterators to enable range-based for loop support.

Definition at line 1026 of file SparseArray.h.

◆ begin() [2/2]

◆ CheckAddress()

Checks that the specified address is not part of an element within the container. Used for implementations to check that reference arguments aren't going to be invalidated by possible reallocation.

Parameters
AddrThe address to check.

Definition at line 862 of file SparseArray.h.

◆ Compact()

Compacts the allocated elements into a contiguous index range. Returns true if any elements were relocated, false otherwise.

Definition at line 510 of file SparseArray.h.

◆ CompactStable()

bool TSparseArray< InElementType, Allocator >::CompactStable ( )
inline

Compacts the allocated elements into a contiguous index range. Does not change the iteration order of the elements. Returns true if any elements were relocated, false otherwise.

Definition at line 560 of file SparseArray.h.

◆ CopyUnfrozen()

void TSparseArray< InElementType, Allocator >::CopyUnfrozen ( const FMemoryUnfreezeContent & Context,
void * Dst ) const
inline

Definition at line 1173 of file SparseArray.h.

◆ CountBytes()

void TSparseArray< InElementType, Allocator >::CountBytes ( FArchive & Ar) const
inline

Tracks the container's memory use through an archive.

Definition at line 676 of file SparseArray.h.

◆ CreateConstIterator()

TConstIterator TSparseArray< InElementType, Allocator >::CreateConstIterator ( ) const
inline

Creates a const iterator for the contents of this array

Definition at line 1016 of file SparseArray.h.

◆ CreateIterator()

Creates an iterator for the contents of this array

Definition at line 1010 of file SparseArray.h.

◆ Emplace()

template<typename... ArgsType>
FORCEINLINE int32 TSparseArray< InElementType, Allocator >::Emplace ( ArgsType &&... Args)
inline

Constructs a new item at the last freed index of the array.

Parameters
ArgsThe arguments to forward to the constructor of the new item.
Returns
Index to the new item

Definition at line 219 of file SparseArray.h.

◆ EmplaceAt()

template<typename... ArgsType>
FORCEINLINE int32 TSparseArray< InElementType, Allocator >::EmplaceAt ( int32 Index,
ArgsType &&... Args )
inline

Constructs a new item at a given index of the array.

Parameters
IndexIndex at which the new allocation will be done
ArgsThe arguments to forward to the constructor of the new item.
Returns
Index to the new item

Definition at line 250 of file SparseArray.h.

◆ EmplaceAtLowestFreeIndex()

template<typename... ArgsType>
FORCEINLINE int32 TSparseArray< InElementType, Allocator >::EmplaceAtLowestFreeIndex ( int32 & LowestFreeIndexSearchStart,
ArgsType &&... Args )
inline

Constructs a new item at the lowest free index of the array. This requires a search which can be accelerated with LowestFreeIndexSearchStart.

Parameters
LowestFreeIndexSearchStartWhere to start the search for a free index.
ArgsThe arguments to forward to the constructor of the new item.
Returns
Index to the new item

Definition at line 235 of file SparseArray.h.

◆ Empty()

void TSparseArray< InElementType, Allocator >::Empty ( int32 ExpectedNumElements = 0)
inline

Removes all elements from the array, potentially leaving space allocated for an expected number of elements about to be added.

Parameters
ExpectedNumElements- The expected number of elements about to be added.

Definition at line 376 of file SparseArray.h.

◆ end() [1/2]

◆ end() [2/2]

◆ GetAllocatedSize()

SIZE_T TSparseArray< InElementType, Allocator >::GetAllocatedSize ( void ) const
inline

Helper function to return the amount of memory allocated by this container Only returns the size of allocations made directly by the container, not the elements themselves.

Returns
number of bytes allocated by this container

Definition at line 670 of file SparseArray.h.

◆ GetMaxIndex()

int32 TSparseArray< InElementType, Allocator >::GetMaxIndex ( ) const
inline

Definition at line 852 of file SparseArray.h.

◆ Insert()

void TSparseArray< InElementType, Allocator >::Insert ( int32 Index,
typename TTypeTraits< ElementType >::ConstInitType Element )
inline

Inserts an element to the array.

Definition at line 328 of file SparseArray.h.

◆ InsertUninitialized()

Allocates space for an element in the array at a given index. The element is not initialized, and you must use the corresponding placement new operator to construct the element in the allocated memory.

Definition at line 271 of file SparseArray.h.

◆ IsAllocated()

bool TSparseArray< InElementType, Allocator >::IsAllocated ( int32 Index) const
inline

Definition at line 851 of file SparseArray.h.

◆ IsCompact()

bool TSparseArray< InElementType, Allocator >::IsCompact ( ) const
inline

Definition at line 682 of file SparseArray.h.

◆ IsEmpty()

bool TSparseArray< InElementType, Allocator >::IsEmpty ( ) const
inline

Definition at line 853 of file SparseArray.h.

◆ IsValidIndex()

bool TSparseArray< InElementType, Allocator >::IsValidIndex ( int32 Index) const
inline

Definition at line 847 of file SparseArray.h.

◆ Move()

Definition at line 806 of file SparseArray.h.

◆ Num()

Definition at line 854 of file SparseArray.h.

◆ operator!=()

Inequality comparison operator. Checks that both arrays have the same elements and element indices; that means that unallocated elements are signifigant!

Definition at line 722 of file SparseArray.h.

◆ operator+=() [1/2]

Definition at line 1078 of file SparseArray.h.

◆ operator+=() [2/2]

Concatenation operators

Definition at line 1069 of file SparseArray.h.

◆ operator=() [1/2]

Copy assignment operator.

Definition at line 758 of file SparseArray.h.

◆ operator=() [2/2]

Move assignment operator.

Definition at line 748 of file SparseArray.h.

◆ operator==()

Equality comparison operator. Checks that both arrays have the same elements and element indices; that means that unallocated elements are signifigant!

Definition at line 691 of file SparseArray.h.

◆ operator[]() [1/2]

Definition at line 828 of file SparseArray.h.

◆ operator[]() [2/2]

Definition at line 834 of file SparseArray.h.

◆ PointerToIndex()

int32 TSparseArray< InElementType, Allocator >::PointerToIndex ( const ElementType * Ptr) const
inline

Definition at line 840 of file SparseArray.h.

◆ RemoveAt()

void TSparseArray< InElementType, Allocator >::RemoveAt ( int32 Index,
int32 Count = 1 )
inline

Removes Count elements from the array, starting from Index.

Definition at line 334 of file SparseArray.h.

◆ RemoveAtUninitialized()

void TSparseArray< InElementType, Allocator >::RemoveAtUninitialized ( int32 Index,
int32 Count = 1 )
inline

Removes Count elements from the array, starting from Index, without destructing them.

Definition at line 349 of file SparseArray.h.

◆ Reserve()

void TSparseArray< InElementType, Allocator >::Reserve ( int32 ExpectedNumElements)
inline

Preallocates enough memory to contain the specified number of elements.

Parameters
ExpectedNumElementsthe total number of elements that the array will have

Definition at line 420 of file SparseArray.h.

◆ Reset()

Empties the array, but keep its allocated memory as slack.

Definition at line 396 of file SparseArray.h.

◆ Shrink()

Shrinks the array's storage to avoid slack.

Definition at line 456 of file SparseArray.h.

◆ Sort() [1/2]

Sorts the elements assuming < operator is defined for ElementType.

Definition at line 596 of file SparseArray.h.

◆ Sort() [2/2]

Sorts the elements using the provided comparison class.

Definition at line 583 of file SparseArray.h.

◆ SortFreeList()

void TSparseArray< InElementType, Allocator >::SortFreeList ( )
inline

Sort the free element list so that subsequent allocations will occur in the lowest available position resulting in tighter packing without moving any existing items. This also means that assigned indices no longer depend on the order in which old items were removed, making it easier to use the container when determinism is required (without a container reset).

E.g., call SortFreeList() each frame to make the container assign the same indices when we perform the following operations: Frame1: Add(A) -> [0]; Add(B) -> [1]; Remove(A); Remove(B); (Free list is now {[1],[0],...}) Frame2: Add(A) -> [1]; Add(B) -> [0]; Remove(A); Remove(B); (Free list is now {[0],[1],...})

NOTE: This is operation is currently O(N) with N = GetMaxIndex(). This could be improved for large mostly-full arrays if necessary.

Definition at line 635 of file SparseArray.h.

◆ StableSort() [1/2]

Stable sorts the elements assuming < operator is defined for ElementType.

Definition at line 616 of file SparseArray.h.

◆ StableSort() [2/2]

Stable sorts the elements using the provided comparison class.

Definition at line 603 of file SparseArray.h.

◆ WriteMemoryImage()

void TSparseArray< InElementType, Allocator >::WriteMemoryImage ( FMemoryImageWriter & Writer) const
inline

Definition at line 1128 of file SparseArray.h.

Friends And Related Symbol Documentation

◆ TScriptSparseArray

Definition at line 79 of file SparseArray.h.

Member Data Documentation

◆ AllocationFlags

Definition at line 1119 of file SparseArray.h.

◆ Data

Definition at line 1116 of file SparseArray.h.

◆ FirstFreeIndex

The index of an unallocated element in the array that currently contains the head of the linked list of free elements.

Definition at line 1122 of file SparseArray.h.

◆ NumFreeIndices

The number of elements in the free list.

Definition at line 1125 of file SparseArray.h.


The documentation for this class was generated from the following file: