UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
ScriptCompactSet.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "ContainersFwd.h"
10
12
13// Untyped set type for accessing TCompactSet data, like FScriptArray for TArray.
14// Must have the same memory representation as a TCompactSet.
15template <typename Allocator>
16class TScriptCompactSet : public TCompactSetBase<typename Allocator::template ElementAllocator<sizeof(uint8)>>
17{
18 using Super = TCompactSetBase<typename Allocator::template ElementAllocator<sizeof(uint8)>>;
19public:
20 [[nodiscard]] static FScriptCompactSetLayout GetScriptLayout(int32 ElementSize, int32 ElementAlignment)
21 {
22 return { ElementSize, FGenericPlatformMath::Max<int32>(ElementAlignment, UE::Core::CompactHashTable::GetMemoryAlignment()) };
23 }
24
26 // Start - intrusive TOptional<TScriptCompactSet> state //
28 constexpr static bool bHasIntrusiveUnsetOptionalState = true;
30
35 // End - intrusive TOptional<TScriptCompactSet> state //
37
39
41 {
42 return Index >= 0 && Index < this->NumElements;
43 }
44
46 {
47 return this->NumElements;
48 }
49
51 {
52 return (uint8*)this->Elements.GetAllocation() + (Layout.Size * Index);
53 }
54
56 {
57 return (const uint8 *)this->Elements.GetAllocation() + (Layout.Size * Index);
58 }
59
61 {
62 checkSlow(this != &Other);
63
64 this->Elements.MoveToEmpty(Other.Elements);
65 this->NumElements = Other.NumElements;
66 this->MaxElements = Other.MaxElements;
67
68 Other.NumElements = 0;
69 Other.MaxElements = 0;
70 }
71
73 {
74 this->ResizeAllocation(Slack, Layout);
75 if (this->MaxElements)
76 {
77 this->GetHashTableView(Layout).Reset();
78 }
79 this->NumElements = 0;
80 }
81
82 void RemoveAt(int32 Index, const FScriptCompactSetLayout& Layout, TFunctionRef<uint32 (const void*)> GetKeyHash, TFunctionRef<void (void*)> DestructItem)
83 {
85
86 void* Dst = GetData(Index, Layout);
87 const uint32 KeyHash = GetKeyHash(Dst);
88
89 if (Index == this->NumElements - 1)
90 {
91 this->GetHashTableView(Layout).Remove(Index, KeyHash, Index, 0);
92 DestructItem(Dst);
93 }
94 else
95 {
96 void* Src = GetData(this->NumElements - 1, Layout);
97
98 this->GetHashTableView(Layout).Remove(Index, KeyHash, this->NumElements - 1, GetKeyHash(Src));
99 DestructItem(Dst);
100
101 // Memmove is fine here as our containers already only work if data is trivially relocatable
102 FMemory::Memmove(Dst, Src, Layout.Size);
103 }
104
105 --this->NumElements;
106 }
107
115 {
116 checkSlow(this->NumElements >= 0 && this->MaxElements >= INDEX_NONE);
117 if (this->NumElements == this->MaxElements)
118 {
119 this->ResizeAllocation(this->AllocatorCalculateSlackGrow(this->NumElements + 1, Layout), Layout);
120 }
121 ++this->NumElements;
122 return this->NumElements - 1;
123 }
124
126 {
127 // Can only be pairs with AddUninitialized for now
128 check(Index == this->NumElements - 1);
129 --this->NumElements;
130 }
131
133 {
134 const uint32 KeyHash = GetKeyHash(GetData(this->NumElements - 1, Layout));
135 this->GetHashTableView(Layout).Add(this->NumElements - 1, KeyHash);
136 }
137
139 {
140 // Keep consistent interface with ScriptSparseSet
141 }
142
143 void Rehash(const FScriptCompactSetLayout& Layout, TFunctionRef<uint32 (const void*)> GetKeyHash)
144 {
145 if (this->MaxElements > 0)
146 {
147 const uint8* ElementData = this->Elements.GetAllocation();
148 FCompactHashTableView HashTable = this->GetHashTableView(Layout);
149
150 HashTable.Reset();
151
152 for (int32 Index = 0; Index < this->NumElements; ++Index)
153 {
154 HashTable.Add(Index, GetKeyHash(ElementData + Layout.Size * Index));
155 }
156 }
157 }
158
159 [[nodiscard]] int32 FindIndex(const void* Element, const FScriptCompactSetLayout& Layout, TFunctionRef<uint32 (const void*)> GetKeyHash, TFunctionRef<bool (const void*, const void*)> EqualityFn) const
160 {
161 if (this->NumElements)
162 {
163 return FindIndexImpl(Element, Layout, GetKeyHash(Element), EqualityFn);
164 }
165
166 return INDEX_NONE;
167 }
168
169 [[nodiscard]] int32 FindIndexByHash(const void* Element, const FScriptCompactSetLayout& Layout, uint32 KeyHash, TFunctionRef<bool (const void*, const void*)> EqualityFn) const
170 {
171 if (this->NumElements)
172 {
173 return FindIndexImpl(Element, Layout, KeyHash, EqualityFn);
174 }
175
176 return INDEX_NONE;
177 }
178
179 int32 FindOrAdd(const void* Element, const FScriptCompactSetLayout& Layout, TFunctionRef<uint32(const void*)> GetKeyHash, TFunctionRef<bool(const void*, const void*)> EqualityFn, TFunctionRef<void(void*)> ConstructFn)
180 {
181 uint32 KeyHash = GetKeyHash(Element);
184 {
185 return OldElementIndex;
186 }
187
188 return AddNewElement(Layout, GetKeyHash, KeyHash, ConstructFn);
189 }
190
191 void Add(const void* Element, const FScriptCompactSetLayout& Layout, TFunctionRef<uint32(const void*)> GetKeyHash, TFunctionRef<bool(const void*, const void*)> EqualityFn, TFunctionRef<void(void*)> ConstructFn, TFunctionRef<void(void*)> DestructFn)
192 {
193 uint32 KeyHash = GetKeyHash(Element);
196 {
198
199 DestructFn(ElementPtr);
200 ConstructFn(ElementPtr);
201
202 // We don't update the hash because we don't need to - the new element
203 // should have the same hash, but let's just check.
205 // Disable deprecations warnings to stop warnings being thrown by our check macro.
206 checkSlow(KeyHash == GetKeyHash(ElementPtr));
208 }
209 else
210 {
211 AddNewElement(Layout, GetKeyHash, KeyHash, ConstructFn);
212 }
213 }
214
215private:
216 [[nodiscard]] int32 FindIndexImpl(const void* Element, const FScriptCompactSetLayout& Layout, uint32 KeyHash, TFunctionRef<bool (const void*, const void*)> EqualityFn) const
217 {
218 return this->GetConstHashTableView(Layout).Find(KeyHash, this->NumElements, [Element, &Layout, &EqualityFn, this](uint32 Index) { return EqualityFn(Element, GetData(Index, Layout)); });
219 }
220
221 int32 AddNewElement(const FScriptCompactSetLayout& Layout, TFunctionRef<uint32(const void*)> GetKeyHash, uint32 KeyHash, TFunctionRef<void(void*)> ConstructFn)
222 {
223 checkSlow(this->NumElements >= 0 && this->MaxElements >= 0);
224 if (this->NumElements == this->MaxElements)
225 {
227 {
228 Rehash(Layout, GetKeyHash);
229 }
230 }
231
232 this->GetHashTableView(Layout).Add(this->NumElements, KeyHash);
233
234 ConstructFn(GetData(this->NumElements, Layout));
235 return this->NumElements++;
236 }
237
238public:
239 // These should really be private, because they shouldn't be called, but there's a bunch of code
240 // that needs to be fixed first.
242 void operator=(const TScriptCompactSet&) { check(false); }
243};
244
245template <typename AllocatorType>
247{
248 enum { Value = true };
249};
250
#define checkSlow(expr)
Definition AssertionMacros.h:332
#define check(expr)
Definition AssertionMacros.h:314
@ INDEX_NONE
Definition CoreMiscDefines.h:150
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
FORCEINLINE constexpr void DestructItem(ElementType *Element)
Definition MemoryOps.h:56
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
#define PRAGMA_ENABLE_DEPRECATION_WARNINGS
Definition GenericPlatformCompilerPreSetup.h:12
#define PRAGMA_DISABLE_DEPRECATION_WARNINGS
Definition GenericPlatformCompilerPreSetup.h:8
uint8_t uint8
Definition binka_ue_file_header.h:8
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition CompactHashTable.h:350
void Reset() const
Definition CompactHashTable.h:357
void Add(uint32 Index, uint32 Key) const
Definition CompactHashTable.h:365
UE_FORCEINLINE_HINT void Remove(uint32 Index, uint32 Key, uint32 LastIndex, uint32 OptLastKey) const
Definition CompactHashTable.h:373
uint32 Find(uint32 Key, uint32 CurrentCount, const PredicateType &Predicate) const
Definition CompactHashTable.h:332
Definition CompactSetBase.h:19
void ResizeAllocation(const int32 NewMaxElements, const FCompactSetLayout &Layout)
Definition CompactSetBase.h:199
FConstCompactHashTableView GetConstHashTableView(const FCompactSetLayout Layout) const
Definition CompactSetBase.h:109
FCompactHashTableView GetHashTableView(const FCompactSetLayout Layout)
Definition CompactSetBase.h:102
ElementAllocatorType Elements
Definition CompactSetBase.h:276
int32 AllocatorCalculateSlackGrow(int32 NewMaxElements, const FCompactSetLayout &Layout) const
Definition CompactSetBase.h:152
bool ResizeAllocationPreserveData(const int32 NewMaxElements, const FCompactSetLayout &Layout, bool bPreserve=true)
Definition CompactSetBase.h:205
Definition AssetRegistryState.h:50
Definition ScriptCompactSet.h:17
void Empty(int32 Slack, const FScriptCompactSetLayout &Layout)
Definition ScriptCompactSet.h:72
bool IsValidIndex(int32 Index) const
Definition ScriptCompactSet.h:40
TScriptCompactSet(FIntrusiveUnsetOptionalState Tag)
Definition ScriptCompactSet.h:31
void CommitLastUninitialized(const FScriptCompactSetLayout &Layout, TFunctionRef< uint32(const void *)> GetKeyHash)
Definition ScriptCompactSet.h:132
void RemoveAt(int32 Index, const FScriptCompactSetLayout &Layout, TFunctionRef< uint32(const void *)> GetKeyHash, TFunctionRef< void(void *)> DestructItem)
Definition ScriptCompactSet.h:82
int32 NumUnchecked() const
Definition ScriptCompactSet.h:45
const void * GetData(int32 Index, const FScriptCompactSetLayout &Layout) const
Definition ScriptCompactSet.h:55
void Add(const void *Element, const FScriptCompactSetLayout &Layout, TFunctionRef< uint32(const void *)> GetKeyHash, TFunctionRef< bool(const void *, const void *)> EqualityFn, TFunctionRef< void(void *)> ConstructFn, TFunctionRef< void(void *)> DestructFn)
Definition ScriptCompactSet.h:191
int32 AddUninitialized(const FScriptCompactSetLayout &Layout)
Definition ScriptCompactSet.h:114
void RemoveAtUninitialized(const FScriptCompactSetLayout &Layout, int32 Index)
Definition ScriptCompactSet.h:125
TScriptCompactSet()=default
void operator=(const TScriptCompactSet &)
Definition ScriptCompactSet.h:242
static FScriptCompactSetLayout GetScriptLayout(int32 ElementSize, int32 ElementAlignment)
Definition ScriptCompactSet.h:20
int32 FindIndexByHash(const void *Element, const FScriptCompactSetLayout &Layout, uint32 KeyHash, TFunctionRef< bool(const void *, const void *)> EqualityFn) const
Definition ScriptCompactSet.h:169
TScriptCompactSet(const TScriptCompactSet &)
Definition ScriptCompactSet.h:241
int32 FindIndex(const void *Element, const FScriptCompactSetLayout &Layout, TFunctionRef< uint32(const void *)> GetKeyHash, TFunctionRef< bool(const void *, const void *)> EqualityFn) const
Definition ScriptCompactSet.h:159
int32 FindOrAdd(const void *Element, const FScriptCompactSetLayout &Layout, TFunctionRef< uint32(const void *)> GetKeyHash, TFunctionRef< bool(const void *, const void *)> EqualityFn, TFunctionRef< void(void *)> ConstructFn)
Definition ScriptCompactSet.h:179
void CommitAllUninitialized(const FScriptCompactSetLayout &Layout, TFunctionRef< uint32(const void *)> GetKeyHash)
Definition ScriptCompactSet.h:138
void * GetData(int32 Index, const FScriptCompactSetLayout &Layout)
Definition ScriptCompactSet.h:50
static constexpr bool bHasIntrusiveUnsetOptionalState
Definition ScriptCompactSet.h:28
void Rehash(const FScriptCompactSetLayout &Layout, TFunctionRef< uint32(const void *)> GetKeyHash)
Definition ScriptCompactSet.h:143
void MoveAssign(TScriptCompactSet &Other, const FScriptCompactSetLayout &Layout)
Definition ScriptCompactSet.h:60
UE_FORCEINLINE_HINT constexpr size_t GetMemoryAlignment()
Definition CompactHashTable.h:69
U16 Index
Definition radfft.cpp:71
Definition CompactSetBase.h:11
int32 Size
Definition CompactSetBase.h:12
Definition IntrusiveUnsetOptionalState.h:71
static UE_FORCEINLINE_HINT void * Memmove(void *Dest, const void *Src, SIZE_T Count)
Definition UnrealMemory.h:109
Definition UnrealTypeTraits.h:172