6#include "Containers/Set.h"
14template<
typename KeyType,
typename ValueType>
24 FCacheEntry* LessRecent;
27 FCacheEntry* MoreRecent;
38 FCacheEntry(
const KeyType&
InKey,
const ValueType&
InValue)
46 inline void LinkBefore(FCacheEntry*
Other)
52 Other->MoreRecent =
this;
59 if (LessRecent !=
nullptr)
61 LessRecent->MoreRecent = MoreRecent;
64 if (MoreRecent !=
nullptr)
66 MoreRecent->LessRecent = LessRecent;
75 struct FKeyFuncs :
public BaseKeyFuncs<FCacheEntry*, KeyType>
77 inline static const KeyType& GetSetKey(
const FCacheEntry* Entry)
89 return GetTypeHash(Key);
97 : LeastRecent(nullptr)
108 : LeastRecent(nullptr)
109 , MostRecent(nullptr)
138 check(MaxNumElements > 0 &&
"Cannot add values to zero size FOpenGLLruCache");
139 check(!LookupSet.Contains(Key));
141 check(LookupSet.Num() < MaxNumElements);
143 FCacheEntry* NewEntry =
new FCacheEntry(Key,
Value);
144 NewEntry->LinkBefore(MostRecent);
145 MostRecent = NewEntry;
147 if (LeastRecent ==
nullptr)
149 LeastRecent = NewEntry;
151 return LookupSet.Add(NewEntry);
163 return LookupSet.Contains(Key);
173 template<
typename Predicate>
176 for (
const FCacheEntry* Entry : LookupSet)
178 if (
Pred(Entry->Key, Entry->Value))
197 for (FCacheEntry* Entry : LookupSet)
203 LookupSet.Empty(MaxNumElements);
205 MostRecent =
nullptr;
206 LeastRecent =
nullptr;
216 template<
typename Predicate>
221 for (
const FCacheEntry* Entry : LookupSet)
223 if (
Pred(Entry->Key, Entry->Value))
225 Result.Add(Entry->Value);
239 inline const ValueType*
Find(
const KeyType& Key)
const
241 FCacheEntry**
EntryPtr = LookupSet.Find(Key);
245 return &(*EntryPtr)->Value;
260 FCacheEntry**
EntryPtr = LookupSet.Find(Key);
269 return &(*EntryPtr)->Value;
279 template<
typename Predicate>
282 for (
const FCacheEntry* Entry : LookupSet)
284 if (
Pred(Entry->Key, Entry->Value))
286 return &Entry->Value;
301 for (
const FCacheEntry* Entry : LookupSet)
315 return MaxNumElements;
326 return LookupSet.Num();
337 FCacheEntry**
EntryPtr = LookupSet.Find(Key);
347 FCacheEntry**
EntryPtr = LookupSet.Find(Key);
365 template<
typename Predicate>
370 for (
const FCacheEntry* Entry : LookupSet)
372 if (
Pred(Entry->Key, Entry->Value))
390 ValueType LeastRecentElement =
MoveTemp(LeastRecent->Value);
392 return LeastRecentElement;
403 return LeastRecent->Value;
414 ValueType MostRecentElement =
MoveTemp(MostRecent->Value);
416 return MostRecentElement;
437 : CurrentEntry(nullptr)
441 : CurrentEntry(Cache.MostRecent)
454 return Lhs.CurrentEntry == Rhs.CurrentEntry;
459 return Lhs.CurrentEntry != Rhs.CurrentEntry;
464 check(CurrentEntry !=
nullptr);
465 return CurrentEntry->Value;
470 check(CurrentEntry !=
nullptr);
471 return CurrentEntry->Value;
474 inline explicit operator bool()
const
476 return (CurrentEntry !=
nullptr);
486 inline KeyType&
Key()
const
488 check(CurrentEntry !=
nullptr);
489 return CurrentEntry->Key;
494 check(CurrentEntry !=
nullptr);
495 return CurrentEntry->Value;
507 check(CurrentEntry !=
nullptr);
508 CurrentEntry = CurrentEntry->LessRecent;
513 FCacheEntry* CurrentEntry;
556 check(Cache !=
nullptr);
577 check(LeastRecent !=
nullptr);
578 check(MostRecent !=
nullptr);
581 if ((&Entry == LeastRecent) && (LeastRecent->MoreRecent !=
nullptr))
583 LeastRecent = LeastRecent->MoreRecent;
587 if (&Entry != MostRecent)
590 Entry.LinkBefore(MostRecent);
602 if (Entry ==
nullptr)
607 LookupSet.Remove(Entry->Key);
609 if (Entry == LeastRecent)
611 LeastRecent = Entry->MoreRecent;
614 if (Entry == MostRecent)
616 MostRecent = Entry->LessRecent;
636 FCacheEntry* LeastRecent;
639 FCacheEntry* MostRecent;
642 int32 MaxNumElements;
#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 SetUtilities.h:95
Definition PsoLruCache.h:433
FCacheEntry * GetCurrentEntry()
Definition PsoLruCache.h:500
friend bool operator==(const TBaseIterator &Lhs, const TBaseIterator &Rhs)
Definition PsoLruCache.h:452
TBaseIterator()
Definition PsoLruCache.h:436
KeyType & Key() const
Definition PsoLruCache.h:486
TBaseIterator(const TPsoLruCache &Cache)
Definition PsoLruCache.h:440
ValueType & Value() const
Definition PsoLruCache.h:492
void Increment()
Definition PsoLruCache.h:505
ValueType & operator*() const
Definition PsoLruCache.h:468
bool operator!() const
Definition PsoLruCache.h:479
TBaseIterator & operator++()
Definition PsoLruCache.h:446
ValueType & operator->() const
Definition PsoLruCache.h:462
friend bool operator!=(const TBaseIterator &Lhs, const TBaseIterator &Rhs)
Definition PsoLruCache.h:457
Definition PsoLruCache.h:522
TConstIterator(const TPsoLruCache &Cache)
Definition PsoLruCache.h:529
TConstIterator()
Definition PsoLruCache.h:525
Definition PsoLruCache.h:540
void RemoveCurrentAndIncrement()
Definition PsoLruCache.h:554
TIterator(TPsoLruCache &InCache)
Definition PsoLruCache.h:548
TIterator()
Definition PsoLruCache.h:543
Definition PsoLruCache.h:16
ValueType RemoveMostRecent()
Definition PsoLruCache.h:411
ValueType RemoveLeastRecent()
Definition PsoLruCache.h:387
const ValueType * FindAndTouch(const KeyType &Key)
Definition PsoLruCache.h:258
void MarkAsRecent(FCacheEntry &Entry)
Definition PsoLruCache.h:575
void Remove(const KeyType &Key)
Definition PsoLruCache.h:335
friend TConstIterator begin(const TPsoLruCache &Cache)
Definition PsoLruCache.h:626
bool Contains(const KeyType &Key) const
Definition PsoLruCache.h:161
TPsoLruCache(int32 InMaxNumElements)
Definition PsoLruCache.h:107
int32 Max() const
Definition PsoLruCache.h:313
friend TIterator begin(TPsoLruCache &Cache)
Definition PsoLruCache.h:625
TPsoLruCache()
Definition PsoLruCache.h:96
int32 RemoveByPredicate(Predicate Pred)
Definition PsoLruCache.h:366
void Remove(FCacheEntry *Entry)
Definition PsoLruCache.h:600
void GetKeys(TArray< KeyType > &OutKeys) const
Definition PsoLruCache.h:299
const ValueType GetLeastRecent() const
Definition PsoLruCache.h:400
int32 Num() const
Definition PsoLruCache.h:324
bool Remove(const KeyType &Key, ValueType &RemovedValue)
Definition PsoLruCache.h:345
friend TConstIterator end(const TPsoLruCache &Cache)
Definition PsoLruCache.h:628
void Empty(int32 InMaxNumElements=0)
Definition PsoLruCache.h:193
TArray< ValueType > FilterByPredicate(Predicate Pred) const
Definition PsoLruCache.h:217
bool ContainsByPredicate(Predicate Pred) const
Definition PsoLruCache.h:174
FSetElementId Add(const KeyType &Key, const ValueType &Value)
Definition PsoLruCache.h:136
~TPsoLruCache()
Definition PsoLruCache.h:116
const ValueType * FindByPredicate(Predicate Pred) const
Definition PsoLruCache.h:280
void MarkAsRecent(const FSetElementId &LRUNode)
Definition PsoLruCache.h:419
friend TIterator end(TPsoLruCache &Cache)
Definition PsoLruCache.h:627
const ValueType * Find(const KeyType &Key) const
Definition PsoLruCache.h:239
@ false
Definition radaudio_common.h:23
Definition SetUtilities.h:23
KeyType KeyType
Definition SetUtilities.h:24