6#include "Containers/Set.h"
15template<
typename KeyType>
26 return GetTypeHash(Key);
38template<
typename KeyType,
typename ValueType,
typename KeyComp = DefaultKeyComparer<KeyType> >
48 FCacheEntry* LessRecent;
51 FCacheEntry* MoreRecent;
84 inline void LinkBefore(FCacheEntry*
Other)
90 Other->MoreRecent =
this;
97 if (LessRecent !=
nullptr)
99 LessRecent->MoreRecent = MoreRecent;
102 if (MoreRecent !=
nullptr)
104 MoreRecent->LessRecent = LessRecent;
107 LessRecent =
nullptr;
108 MoreRecent =
nullptr;
113 struct FKeyFuncs :
public BaseKeyFuncs<FCacheEntry*, KeyType>
122 return KeyComp::Matches(
A,
B);
127 return KeyComp::GetKeyHash(Key);
135 : LeastRecent(nullptr)
136 , MostRecent(nullptr)
147 : LeastRecent(nullptr)
148 , MostRecent(nullptr)
173 void Add(
const KeyType& Key,
const ValueType&
Value)
175 check(MaxNumElements != 0 &&
"Cannot add values to zero size TLruCache");
177 FCacheEntry**
EntryPtr = LookupSet.Find(Key);
185 Entry->Value =
Value;
191 if (LookupSet.Num() == MaxNumElements)
196 FCacheEntry* NewEntry =
new FCacheEntry(Key,
Value);
197 NewEntry->LinkBefore(MostRecent);
198 MostRecent = NewEntry;
200 if (LeastRecent ==
nullptr)
202 LeastRecent = NewEntry;
205 LookupSet.Add(NewEntry);
222 check(MaxNumElements != 0 &&
"Cannot add values to zero size TLruCache");
224 FCacheEntry**
EntryPtr = LookupSet.Find(Key);
238 if (LookupSet.Num() == MaxNumElements)
243 FCacheEntry* NewEntry =
new FCacheEntry(Key);
244 NewEntry->LinkBefore(MostRecent);
245 MostRecent = NewEntry;
247 if (LeastRecent ==
nullptr)
249 LeastRecent = NewEntry;
252 LookupSet.Add(NewEntry);
253 return NewEntry->Value;
268 return LookupSet.Contains(Key);
278 template<
typename Predicate>
281 for (
const FCacheEntry* Entry : LookupSet)
283 if (
Pred(Entry->Key, Entry->Value))
300 for (FCacheEntry* Entry : LookupSet)
306 LookupSet.Empty(FMath::Max(MaxNumElements, 0));
308 MostRecent =
nullptr;
309 LeastRecent =
nullptr;
319 template<
typename Predicate>
324 for (
const FCacheEntry* Entry : LookupSet)
326 if (
Pred(Entry->Key, Entry->Value))
328 Result.Add(Entry->Value);
344 FCacheEntry*
const *
EntryPtr = LookupSet.Find(Key);
348 return &(*EntryPtr)->Value;
363 FCacheEntry*
const*
EntryPtr = LookupSet.Find(Key);
367 return &(*EntryPtr)->Value;
381 FCacheEntry*
const *
EntryPtr = LookupSet.Find(Key);
385 return (*EntryPtr)->Value;
396 FCacheEntry*
const *
EntryPtr = LookupSet.Find(Key);
400 return (*EntryPtr)->Value;
411 FCacheEntry*
const *
EntryPtr = LookupSet.Find(Key);
415 return (*EntryPtr)->Value;
430 FCacheEntry**
EntryPtr = LookupSet.Find(Key);
439 return &(*EntryPtr)->Value;
450 FCacheEntry**
EntryPtr = LookupSet.Find(Key);
456 return (*EntryPtr)->Value;
467 FCacheEntry**
EntryPtr = LookupSet.Find(Key);
476 return (*EntryPtr)->Value;
486 template<
typename Predicate>
489 for (
const FCacheEntry* Entry : LookupSet)
491 if (
Pred(Entry->Key, Entry->Value))
493 return &Entry->Value;
508 for (
const FCacheEntry* Entry : LookupSet)
522 return MaxNumElements;
533 return LookupSet.IsEmpty();
544 return LookupSet.Num();
555 FCacheEntry**
EntryPtr = LookupSet.Find(Key);
570 template<
typename Predicate>
575 for (
auto It = LookupSet.CreateIterator(); It; ++It)
577 FCacheEntry* Entry = *It;
578 if (
Pred(Entry->Key, Entry->Value))
580 if (Entry == LeastRecent)
582 LeastRecent = Entry->MoreRecent;
585 if (Entry == MostRecent)
587 MostRecent = Entry->LessRecent;
610 ValueType LeastRecentElement =
MoveTemp(LeastRecent->Value);
612 return LeastRecentElement;
623 return LeastRecent->Key;
639 : CurrentEntry(nullptr)
644 : CurrentEntry(Cache.MostRecent)
658 return CurrentEntry == Rhs.CurrentEntry;
663 return CurrentEntry != Rhs.CurrentEntry;
668 check(CurrentEntry !=
nullptr);
669 return CurrentEntry->Value;
674 check(CurrentEntry !=
nullptr);
675 return CurrentEntry->Value;
680 return (CurrentEntry !=
nullptr);
692 check(CurrentEntry !=
nullptr);
693 return CurrentEntry->Key;
698 check(CurrentEntry !=
nullptr);
699 return CurrentEntry->Value;
711 check(CurrentEntry !=
nullptr);
712 CurrentEntry = CurrentEntry->LessRecent;
717 FCacheEntry* CurrentEntry;
764 check(Cache !=
nullptr);
785 check(LeastRecent !=
nullptr);
786 check(MostRecent !=
nullptr);
789 if ((&Entry == LeastRecent) && (LeastRecent->MoreRecent !=
nullptr))
791 LeastRecent = LeastRecent->MoreRecent;
795 if (&Entry != MostRecent)
798 Entry.LinkBefore(MostRecent);
810 if (Entry ==
nullptr)
815 LookupSet.Remove(Entry->Key);
817 if (Entry == LeastRecent)
819 LeastRecent = Entry->MoreRecent;
822 if (Entry == MostRecent)
824 MostRecent = Entry->LessRecent;
844 FCacheEntry* LeastRecent;
847 FCacheEntry* MostRecent;
850 int32 MaxNumElements;
#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
return true
Definition ExternalRpcRegistry.cpp:601
const bool
Definition NetworkReplayStreaming.h:178
UE_INTRINSIC_CAST UE_REWRITE constexpr std::remove_reference_t< T > && MoveTemp(T &&Obj) noexcept
Definition UnrealTemplate.h:520
Definition LruCache.h:635
void Increment()
Definition LruCache.h:709
UE_FORCEINLINE_HINT TBaseIterator()
Definition LruCache.h:638
UE_FORCEINLINE_HINT bool operator==(const TBaseIterator &Rhs) const
Definition LruCache.h:656
ValueType & Value() const
Definition LruCache.h:696
ValueType & operator*() const
Definition LruCache.h:672
UE_FORCEINLINE_HINT bool operator!=(const TBaseIterator &Rhs) const
Definition LruCache.h:661
TBaseIterator & operator++()
Definition LruCache.h:650
UE_FORCEINLINE_HINT bool operator!() const
Definition LruCache.h:683
FCacheEntry * GetCurrentEntry()
Definition LruCache.h:704
UE_FORCEINLINE_HINT TBaseIterator(const TLruCache &Cache)
Definition LruCache.h:643
KeyType & Key() const
Definition LruCache.h:690
ValueType & operator->() const
Definition LruCache.h:666
Definition LruCache.h:726
UE_FORCEINLINE_HINT TConstIterator(const TLruCache &Cache)
Definition LruCache.h:734
UE_FORCEINLINE_HINT TConstIterator()
Definition LruCache.h:729
Definition LruCache.h:746
TIterator(TLruCache &InCache)
Definition LruCache.h:755
TIterator()
Definition LruCache.h:749
void RemoveCurrentAndIncrement()
Definition LruCache.h:762
ValueType & FindAndTouchChecked(const KeyType &Key)
Definition LruCache.h:448
ValueType * Find(const KeyType &Key)
Definition LruCache.h:361
TIterator end()
Definition LruCache.h:835
UE_FORCEINLINE_HINT int32 Max() const
Definition LruCache.h:520
ValueType & FindChecked(const KeyType &Key)
Definition LruCache.h:394
const ValueType & FindChecked(const KeyType &Key) const
Definition LruCache.h:379
ValueType & AddUninitialized_GetRef(const KeyType &Key)
Definition LruCache.h:220
void Add(const KeyType &Key, const ValueType &Value)
Definition LruCache.h:173
UE_FORCEINLINE_HINT bool Contains(const KeyType &Key) const
Definition LruCache.h:266
KeyType GetLeastRecentKey() const
Definition LruCache.h:620
void Remove(FCacheEntry *Entry)
Definition LruCache.h:808
~TLruCache()
Definition LruCache.h:155
void GetKeys(TArray< KeyType > &OutKeys) const
Definition LruCache.h:506
TLruCache(int32 InMaxNumElements)
Definition LruCache.h:146
void MarkAsRecent(FCacheEntry &Entry)
Definition LruCache.h:783
void Empty(int32 InMaxNumElements=0)
Definition LruCache.h:298
const ValueType * FindByPredicate(Predicate Pred) const
Definition LruCache.h:487
int32 RemoveByPredicate(Predicate Pred)
Definition LruCache.h:571
TConstIterator end() const
Definition LruCache.h:836
TConstIterator begin() const
Definition LruCache.h:834
TLruCache()
Definition LruCache.h:134
bool ContainsByPredicate(Predicate Pred) const
Definition LruCache.h:279
ValueType FindAndTouchRef(const KeyType &Key)
Definition LruCache.h:465
ValueType * FindAndTouch(const KeyType &Key)
Definition LruCache.h:428
TArray< ValueType > FilterByPredicate(Predicate Pred) const
Definition LruCache.h:320
bool IsEmpty() const
Definition LruCache.h:531
ValueType FindRef(const KeyType &Key) const
Definition LruCache.h:409
TIterator begin()
Definition LruCache.h:833
void Remove(const KeyType &Key)
Definition LruCache.h:553
const ValueType * Find(const KeyType &Key) const
Definition LruCache.h:342
ValueType RemoveLeastRecent()
Definition LruCache.h:607
UE_FORCEINLINE_HINT int32 Num() const
Definition LruCache.h:542
@ false
Definition radaudio_common.h:23
Definition SetUtilities.h:23
KeyType KeyType
Definition SetUtilities.h:24
static UE_FORCEINLINE_HINT bool Matches(KeyType A, KeyType B)
Definition LruCache.h:18
static UE_FORCEINLINE_HINT uint32 GetKeyHash(KeyType Key)
Definition LruCache.h:24