UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
SparseIndexCollectionTypes.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3// partial port of geometry3sharp Indexing.cs
4
5#pragma once
6
7#include "IndexTypes.h"
8
9#include <CoreMinimal.h>
10
11
12namespace UE
13{
14namespace Geometry
15{
16
21{
22private:
23 TOptional<TArray<bool>> Dense; // TODO: consider switching to bit array; but test first as performance of bit array may by significantly worse
24 int DenseCount; // only tracked for Dense
25 TOptional<TSet<int>> Sparse;
26
27public:
28 FIndexFlagSet(bool bSetSparse = true, int MaxIndex = -1)
29 {
30 InitManual(bSetSparse, MaxIndex);
31 }
32
33 FIndexFlagSet(int MaxIndex, int SubsetCountEst)
34 {
35 InitAuto(MaxIndex, SubsetCountEst);
36 }
37
41 void InitAuto(int MaxIndex, int SubsetCountEst)
42 {
43 bool bSmall = MaxIndex < 32000;
44 constexpr float PercentThresh = 0.05f;
45
46 InitManual(!bSmall && ((float)SubsetCountEst / (float)MaxIndex < PercentThresh), MaxIndex);
47 }
48
54 void InitManual(bool bSetSparse, int MaxIndex = -1)
55 {
56 if (bSetSparse)
57 {
58 Sparse = TSet<int>();
59 }
60 else
61 {
63 check(MaxIndex >= 0);
64 Dense->SetNumZeroed(FMath::Max(0, MaxIndex));
65 }
66 DenseCount = 0;
67 }
68
73 inline bool Contains(int Index) const
74 {
75 if (Dense.IsSet())
76 {
77 return Dense.GetValue()[Index];
78 }
79 else
80 {
81 check(Sparse.IsSet());
82 return Sparse->Contains(Index);
83 }
84 }
85
89 inline void Add(int Index)
90 {
91 if (Dense.IsSet())
92 {
93 bool &Value = Dense.GetValue()[Index];
94 if (!Value)
95 {
96 Value = true;
97 DenseCount++;
98 }
99 }
100 else
101 {
102 check(Sparse.IsSet());
103 Sparse->Add(Index);
104 }
105 }
106
110 inline void Remove(int Index)
111 {
112 if (Dense.IsSet())
113 {
114 bool &Value = Dense.GetValue()[Index];
115 if (Value)
116 {
117 Value = false;
118 DenseCount--;
119 }
120 }
121 else
122 {
123 check(Sparse.IsSet());
124 Sparse->Remove(Index);
125 }
126 }
127
131 inline int Count() const
132 {
133 if (Dense.IsSet())
134 {
135 return DenseCount;
136 }
137 else
138 {
139 return Sparse->Num();
140 }
141 }
142
146 inline const bool operator[](unsigned int Index) const
147 {
148 return Contains(Index);
149 }
150
151
152 // TODO: iterator support
153
154};
155
161{
162 // storage used when map is dense
164 // storage used when map is sparse
166
167 // mapping to assign defaults for indices that are not explicitly assigned; computed as Index*DefaultScale + DefaultOffset
169
170 // max index in map; -1 will leave the map unbounded (invalid for Dense maps)
171 int MaxIndex = -1;
172
173 // different kinds of storage that could be used for the map
174 enum class EMapType : uint8
175 {
177 };
178
179 // choice of storage backing the map
181
185
196
201
207
212 {
213 Dense.Reset();
214 Sparse.Reset();
216
217 // if buffer is less than threshold, just use dense map
218 bool bSmall = MaxIndex < 32000;
219 float fPercent = (float)SubsetCountEst / (float)MaxIndex;
220 float fPercentThresh = 0.1f;
221
223 {
226 }
227 else
228 {
230 }
231
232 InitDefaults();
233 }
234
239 {
241 ToRet.DefaultScale = 1;
242 ToRet.DefaultOffset = 0;
243 return ToRet;
244 }
245
249 static FOptionallySparseIndexMap ConstantMap(int ConstantValue, int MaxIndex = -1)
250 {
252 ToRet.DefaultScale = 0;
253 ToRet.DefaultOffset = ConstantValue;
254 return ToRet;
255 }
256
261 {
263 ToRet.DefaultScale = 1;
264 ToRet.DefaultOffset = 0;
265 return ToRet;
266 }
267
268
269 /* Initialize default values for dense map (sparse defaults are computed on the fly) */
271 {
273 {
274 for (int i = 0; i < Dense.Num(); ++i)
275 {
277 }
278 }
279 }
280
282 inline bool BadIndex(int Index) const
283 {
284 return (Index < 0) || (MaxIndex >= 0 && Index >= MaxIndex);
285 }
286
293 bool Contains(int Index)
294 {
295 if (BadIndex(Index))
296 {
297 return false;
298 }
299 switch (MapType)
300 {
301 case EMapType::Dense:
302 return Dense[Index] >= 0;
303 case EMapType::Sparse:
304 return Sparse.Contains(Index) || (Index * DefaultScale + DefaultOffset >= 0);
306 return Index * DefaultScale + DefaultOffset >= 0;
307 }
308 check(false);
309 return false;
310 }
311
315 inline const int operator[](int Index) const
316 {
317 if (BadIndex(Index))
318 {
320 }
321 switch (MapType)
322 {
323 case EMapType::Dense:
324 return Dense[Index];
325 case EMapType::Sparse:
326 {
327 const int *Value = Sparse.Find(Index);
328 return Value ? (*Value) : (Index * DefaultScale + DefaultOffset);
329 }
332 }
333 check(false);
335 }
336
340 void Unset(int Index)
341 {
344 {
346 }
347 else
348 {
349 Sparse.Remove(Index);
350 }
351 }
352
354 void Set(int Index, int Value)
355 {
359 {
360 Dense[Index] = Value;
361 }
362 else
363 {
364 Sparse.FindOrAdd(Index) = Value;
365 }
366 }
367
369 inline void SetInvalid(int Index)
370 {
372 }
373};
374
375} // end namespace UE::Geometry
376} // end namespace UE
#define check(expr)
Definition AssertionMacros.h:314
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
USkinnedMeshComponent float
Definition SkinnedMeshComponent.h:60
uint8_t uint8
Definition binka_ue_file_header.h:8
Definition Array.h:670
UE_REWRITE SizeType Num() const
Definition Array.h:1144
void Reset(SizeType NewSize=0)
Definition Array.h:2246
void SetNum(SizeType NewNum, EAllowShrinking AllowShrinking=UE::Core::Private::AllowShrinkingByDefault< AllocatorType >())
Definition Array.h:2308
Definition UnrealString.h.inl:34
Definition SparseIndexCollectionTypes.h:21
int Count() const
Definition SparseIndexCollectionTypes.h:131
const bool operator[](unsigned int Index) const
Definition SparseIndexCollectionTypes.h:146
FIndexFlagSet(bool bSetSparse=true, int MaxIndex=-1)
Definition SparseIndexCollectionTypes.h:28
void Remove(int Index)
Definition SparseIndexCollectionTypes.h:110
void Add(int Index)
Definition SparseIndexCollectionTypes.h:89
void InitAuto(int MaxIndex, int SubsetCountEst)
Definition SparseIndexCollectionTypes.h:41
FIndexFlagSet(int MaxIndex, int SubsetCountEst)
Definition SparseIndexCollectionTypes.h:33
bool Contains(int Index) const
Definition SparseIndexCollectionTypes.h:73
void InitManual(bool bSetSparse, int MaxIndex=-1)
Definition SparseIndexCollectionTypes.h:54
constexpr int InvalidID
Definition IndexTypes.h:13
Definition AdvancedWidgetsModule.cpp:13
U16 Index
Definition radfft.cpp:71
Definition Optional.h:131
Definition SparseIndexCollectionTypes.h:161
int DefaultScale
Definition SparseIndexCollectionTypes.h:168
FOptionallySparseIndexMap(EMapType MapType, int MaxIndex=-1)
Definition SparseIndexCollectionTypes.h:187
int DefaultOffset
Definition SparseIndexCollectionTypes.h:168
static FOptionallySparseIndexMap SparseIdentityMap(int MaxIndex=-1)
Definition SparseIndexCollectionTypes.h:260
bool Contains(int Index)
Definition SparseIndexCollectionTypes.h:293
FOptionallySparseIndexMap(TArray< int > Dense, int MaxIndex=-1)
Definition SparseIndexCollectionTypes.h:198
void Unset(int Index)
Definition SparseIndexCollectionTypes.h:340
EMapType
Definition SparseIndexCollectionTypes.h:175
void SetInvalid(int Index)
Definition SparseIndexCollectionTypes.h:369
TMap< int, int > Sparse
Definition SparseIndexCollectionTypes.h:165
static FOptionallySparseIndexMap ConstantMap(int ConstantValue, int MaxIndex=-1)
Definition SparseIndexCollectionTypes.h:249
int MaxIndex
Definition SparseIndexCollectionTypes.h:171
void InitDefaults()
Definition SparseIndexCollectionTypes.h:270
FOptionallySparseIndexMap(int MaxIndex, int SubsetCountEst)
Definition SparseIndexCollectionTypes.h:203
bool BadIndex(int Index) const
Definition SparseIndexCollectionTypes.h:282
EMapType MapType
Definition SparseIndexCollectionTypes.h:180
void Set(int Index, int Value)
Definition SparseIndexCollectionTypes.h:354
void Initialize(int MaxIndexIn, int SubsetCountEst)
Definition SparseIndexCollectionTypes.h:211
TArray< int > Dense
Definition SparseIndexCollectionTypes.h:163
static FOptionallySparseIndexMap IdentityMap(int MaxIndex=-1)
Definition SparseIndexCollectionTypes.h:238
const int operator[](int Index) const
Definition SparseIndexCollectionTypes.h:315
FOptionallySparseIndexMap()
Definition SparseIndexCollectionTypes.h:183