42template <
typename ElementType,
typename KeyFuncsType>
72 template <
typename CompareType>
73 const ElementType*
Find(
const CompareType& Key)
const;
74 template <
typename CompareType>
75 const ElementType*
FindByHash(
uint32 TypeHash,
const CompareType& Key)
const;
114 uint32 GetTargetHashSize()
const;
124 constexpr static float MaxLoadFactorDuringAdd = 0.75f;
125 constexpr static float TargetLoadFactor = 0.5f;
126 constexpr static uint32 InitialAllocationSize = 8;
127 constexpr static uint32 MinimumNonZeroSize = 8;
129 mutable KeyFuncsType KeyFuncs;
130 ElementType* Hash =
nullptr;
149template <
typename ElementType,
typename KeyFuncsType>
156template <
typename ElementType,
typename KeyFuncsType>
162template <
typename ElementType,
typename KeyFuncsType>
168template <
typename ElementType,
typename KeyFuncsType>
178 KeyFuncs =
Other.KeyFuncs;
179 HashSize =
Other.HashSize;
180 NumValues =
Other.NumValues;
184 Hash =
reinterpret_cast<ElementType*
>(FMemory::Malloc(
sizeof(ElementType) * HashSize,
alignof(ElementType)));
185 for (
uint32 Bucket = 0; Bucket < HashSize; ++Bucket)
187 new (&
Hash[Bucket]) ElementType(
Other.Hash[Bucket]);
193template <
typename ElementType,
typename KeyFuncsType>
205 HashSize =
Other.HashSize;
206 NumValues =
Other.NumValues;
208 Other.Hash =
nullptr;
214template <
typename ElementType,
typename KeyFuncsType>
220template <
typename ElementType,
typename KeyFuncsType>
225 KeyFuncs.~KeyFuncsType();
229template <
typename ElementType,
typename KeyFuncsType>
235 ElementType InvalidValue = KeyFuncs.GetInvalidElement();
236 for (
uint32 Bucket = 0; Bucket < HashSize; ++Bucket)
238 Hash[Bucket].~ElementType();
239 new (&
Hash[Bucket]) ElementType(InvalidValue);
244template <
typename ElementType,
typename KeyFuncsType>
253 ElementType InvalidValue = KeyFuncs.GetInvalidElement();
254 Hash =
reinterpret_cast<ElementType*
>(FMemory::Malloc(
sizeof(ElementType) * HashSize,
alignof(ElementType)));
255 for (
uint32 Bucket = 0; Bucket < HashSize; ++Bucket)
257 new (&
Hash[Bucket]) ElementType(InvalidValue);
262template <
typename ElementType,
typename KeyFuncsType>
272template <
typename ElementType,
typename KeyFuncsType>
282template <
typename ElementType,
typename KeyFuncsType>
288template <
typename ElementType,
typename KeyFuncsType>
291 return sizeof(ElementType) * HashSize;
294template <
typename ElementType,
typename KeyFuncsType>
301 Result.AverageSearch = 0.f;
302 Result.LongestSearch = 0;
329 Result.LongestSearch = 0;
347 Bucket = HashSpaceToBucketSpace(Bucket + 1),
bFirstLoop =
false)
349 const ElementType& Element =
Hash[Bucket];
350 if (KeyFuncs.IsInvalid(Element))
375 Result.LongestSearch = FMath::Max(Result.LongestSearch,
SearchLength);
381 Result.AverageSearch =
static_cast<float>(
SumOfSearches) /
static_cast<float>(NumValues);
385template <
typename ElementType,
typename KeyFuncsType>
386template <
typename CompareType>
389 return FindByHash(KeyFuncs.GetTypeHash(Key), Key);
392template <
typename ElementType,
typename KeyFuncsType>
393template <
typename CompareType>
400 uint32 Bucket = HashSpaceToBucketSpace(TypeHash);
404 const ElementType& Element =
Hash[Bucket];
405 if (KeyFuncs.IsInvalid(Element))
409 if (KeyFuncs.Matches(Element, Key))
413 Bucket = HashSpaceToBucketSpace(Bucket + 1);
420template <
typename ElementType,
typename KeyFuncsType>
426template <
typename ElementType,
typename KeyFuncsType>
429 if (KeyFuncs.IsInvalid(
Value))
431 checkf(
false,
TEXT(
"Add called with invalid element."));
436 Empty(InitialAllocationSize);
442 float LoadFactor =
static_cast<float>(NumValues) /
static_cast<float>(HashSize);
445 Reallocate(GetTargetHashSize());
449template <
typename ElementType,
typename KeyFuncsType>
453 uint32 Bucket = HashSpaceToBucketSpace(TypeHash);
470 Bucket = HashSpaceToBucketSpace(Bucket + 1);
475 Hash[Bucket].~ElementType();
484template <
typename ElementType,
typename KeyFuncsType>
487 return RemoveByHash(KeyFuncs.GetTypeHash(
Value),
Value);
490template <
typename ElementType,
typename KeyFuncsType>
493 if (KeyFuncs.IsInvalid(
Value))
495 checkf(
false,
TEXT(
"Remove called with invalid element."));
505 uint32 Bucket = HashSpaceToBucketSpace(TypeHash);
518 Bucket = HashSpaceToBucketSpace(Bucket + 1);
580 CurrentBucket = HashSpaceToBucketSpace(CurrentBucket + 1);
587 new (&
Hash[
HoleIndex]) ElementType(KeyFuncs.GetInvalidElement());
593template <
typename ElementType,
typename KeyFuncsType>
609 ElementType InvalidValue =
KeyFuncs.GetInvalidElement();
610 Hash =
reinterpret_cast<ElementType*
>(FMemory::Malloc(
sizeof(ElementType) * HashSize,
alignof(ElementType)));
611 for (
uint32 Bucket = 0; Bucket < HashSize; ++Bucket)
613 new (&
Hash[Bucket]) ElementType(InvalidValue);
631template <
typename ElementType,
typename KeyFuncsType>
634 return GetTargetHashSize(NumValues);
637template <
typename ElementType,
typename KeyFuncsType>
645 static_cast<float>(
TargetNumValues) /
static_cast<float>(TargetLoadFactor)));
650template <
typename ElementType,
typename KeyFuncsType>
653 return HashSize != 0 ? (HashKey % HashSize) : 0;
656template <
typename ElementType,
typename KeyFuncsType>
661 for (
uint32 Bucket = 0; Bucket < HashSize; ++Bucket)
663 Hash[Bucket].~ElementType();
670template <
typename ElementType,
typename KeyFuncsType>
675 if (Owner.HashSize > 0 && Owner.KeyFuncs.IsInvalid(Owner.Hash[Bucket]))
681template <
typename ElementType,
typename KeyFuncsType>
684 return Owner.Hash[Bucket];
687template <
typename ElementType,
typename KeyFuncsType>
692 while (Bucket <
Owner.HashSize &&
Owner.KeyFuncs.IsInvalid(
Owner.Hash[Bucket]))
699template <
typename ElementType,
typename KeyFuncsType>
702 return Bucket <
Owner.HashSize;
#define check(expr)
Definition AssertionMacros.h:314
#define checkf(expr, format,...)
Definition AssertionMacros.h:315
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 SetKeyFuncs.h:44
int32 Remove(const ElementType &Value)
Definition SetKeyFuncs.h:485
FSetKeyFuncsStats GetStats() const
Definition SetKeyFuncs.h:296
~TSetKeyFuncs()
Definition SetKeyFuncs.h:215
friend FIterationSentinel end(const TSetKeyFuncs< ElementType, KeyFuncsType > &Set)
Definition SetKeyFuncs.h:107
int32 RemoveByHash(uint32 TypeHash, const ElementType &Value)
Definition SetKeyFuncs.h:491
void Reserve(int32 ExpectedNumElements)
Definition SetKeyFuncs.h:263
void Add(ElementType Value, bool *bAlreadyExists=nullptr)
Definition SetKeyFuncs.h:421
int32 Num() const
Definition SetKeyFuncs.h:283
TSetKeyFuncs & operator=(const TSetKeyFuncs< ElementType, KeyFuncsType > &Other)
Definition SetKeyFuncs.h:170
const ElementType * Find(const CompareType &Key) const
Definition SetKeyFuncs.h:387
void ResizeToTargetSize()
Definition SetKeyFuncs.h:273
SIZE_T GetAllocatedSize() const
Definition SetKeyFuncs.h:289
const ElementType * FindByHash(uint32 TypeHash, const CompareType &Key) const
Definition SetKeyFuncs.h:394
TSetKeyFuncs(KeyFuncsType KeyFuncs, int32 ExpectedNumElements=0)
Definition SetKeyFuncs.h:150
void Reset()
Definition SetKeyFuncs.h:230
friend FIterator begin(const TSetKeyFuncs< ElementType, KeyFuncsType > &Set)
Definition SetKeyFuncs.h:103
void SetKeyFuncs(KeyFuncsType KeyFuncs)
Definition SetKeyFuncs.h:221
void Empty(int32 ExpectedNumElements=0)
Definition SetKeyFuncs.h:245
void AddByHash(uint32 TypeHash, ElementType Value, bool *bAlreadyExists=nullptr)
Definition SetKeyFuncs.h:427
static FORCENOINLINE CORE_API void Free(void *Original)
Definition UnrealMemory.cpp:685
Definition SetKeyFuncs.h:136
float AverageSearch
Definition SetKeyFuncs.h:138
int32 LongestSearch
Definition SetKeyFuncs.h:140
Definition SetKeyFuncs.h:98
Definition SetKeyFuncs.h:85
FIterator & operator++()
Definition SetKeyFuncs.h:689
FIterator(const TSetKeyFuncs< ElementType, KeyFuncsType > &InOwner)
Definition SetKeyFuncs.h:671
const ElementType & operator*() const
Definition SetKeyFuncs.h:682
bool operator!=(FIterationSentinel) const
Definition SetKeyFuncs.h:700