diff options
author | vskipin <vskipin@yandex-team.ru> | 2022-02-10 16:46:00 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:46:00 +0300 |
commit | 4d8b546b89b5afc08cf3667e176271c7ba935f33 (patch) | |
tree | 1a2c5ffcf89eb53ecd79dbc9bc0a195c27404d0c /library/cpp/cache | |
parent | 4e4b78bd7b67e2533da4dbb9696374a6d6068e32 (diff) | |
download | ydb-4d8b546b89b5afc08cf3667e176271c7ba935f33.tar.gz |
Restoring authorship annotation for <vskipin@yandex-team.ru>. Commit 2 of 2.
Diffstat (limited to 'library/cpp/cache')
-rw-r--r-- | library/cpp/cache/cache.h | 198 | ||||
-rw-r--r-- | library/cpp/cache/ut/cache_ut.cpp | 24 |
2 files changed, 111 insertions, 111 deletions
diff --git a/library/cpp/cache/cache.h b/library/cpp/cache/cache.h index 93c24f691d..6dc997076d 100644 --- a/library/cpp/cache/cache.h +++ b/library/cpp/cache/cache.h @@ -124,94 +124,94 @@ private: size_t MaxSize; }; -template <typename TKey, typename TValue> -class TLFUList { -public: - TLFUList(size_t maxSize) - : List() - , ListSize(0) - , MaxSize(maxSize) - { - } - +template <typename TKey, typename TValue> +class TLFUList { +public: + TLFUList(size_t maxSize) + : List() + , ListSize(0) + , MaxSize(maxSize) + { + } + struct TItem: public TIntrusiveListItem<TItem> { - typedef TIntrusiveListItem<TItem> TBase; - TItem(const TKey& key, const TValue& value = TValue()) - : TBase() - , Key(key) - , Value(value) - , Counter(0) - { - } - - TItem(const TItem& rhs) - : TBase() - , Key(rhs.Key) - , Value(rhs.Value) - , Counter(rhs.Counter) - { - } - + typedef TIntrusiveListItem<TItem> TBase; + TItem(const TKey& key, const TValue& value = TValue()) + : TBase() + , Key(key) + , Value(value) + , Counter(0) + { + } + + TItem(const TItem& rhs) + : TBase() + , Key(rhs.Key) + , Value(rhs.Value) + , Counter(rhs.Counter) + { + } + bool operator<(const TItem& rhs) const { - return Key < rhs.Key; - } - + return Key < rhs.Key; + } + bool operator==(const TItem& rhs) const { - return Key == rhs.Key; - } - - TKey Key; - TValue Value; - size_t Counter; - - struct THash { + return Key == rhs.Key; + } + + TKey Key; + TValue Value; + size_t Counter; + + struct THash { size_t operator()(const TItem& item) const { - return ::THash<TKey>()(item.Key); - } - }; - }; - -public: - TItem* Insert(TItem* item) { - List.PushBack(item); // give a chance for promotion - ++ListSize; + return ::THash<TKey>()(item.Key); + } + }; + }; + +public: + TItem* Insert(TItem* item) { + List.PushBack(item); // give a chance for promotion + ++ListSize; return RemoveIfOverflown(); } TItem* RemoveIfOverflown() { TItem* deleted = nullptr; - if (ListSize > MaxSize) { - deleted = GetLeastFrequentlyUsed(); - Erase(deleted); - } - return deleted; - } - - TItem* GetLeastFrequentlyUsed() { + if (ListSize > MaxSize) { + deleted = GetLeastFrequentlyUsed(); + Erase(deleted); + } + return deleted; + } + + TItem* GetLeastFrequentlyUsed() { typename TListType::TIterator it = List.Begin(); Y_ASSERT(it != List.End()); - return &*it; - } - - void Erase(TItem* item) { - item->Unlink(); - --ListSize; - } - - void Promote(TItem* item) { - size_t counter = ++item->Counter; + return &*it; + } + + void Erase(TItem* item) { + item->Unlink(); + --ListSize; + } + + void Promote(TItem* item) { + size_t counter = ++item->Counter; typename TListType::TIterator it = item; - while (it != List.End() && counter >= it->Counter) { - ++it; - } - item->LinkBefore(&*it); - } - - size_t GetSize() const { - return ListSize; - } - + while (it != List.End() && counter >= it->Counter) { + ++it; + } + item->LinkBefore(&*it); + } + + size_t GetSize() const { + return ListSize; + } + size_t GetMaxSize() const { return MaxSize; } @@ -222,13 +222,13 @@ public: MaxSize = newSize; } -private: +private: typedef TIntrusiveList<TItem> TListType; TListType List; - size_t ListSize; - size_t MaxSize; -}; - + size_t ListSize; + size_t MaxSize; +}; + // Least Weighted list // discards the least weighted items first // doesn't support promotion @@ -578,7 +578,7 @@ template <typename TKey, typename TValue, typename TDeleter = TNoopDelete, class class TLRUCache: public TCache<TKey, TValue, TLRUList<TKey, TValue, TSizeProvider>, TDeleter> { using TListType = TLRUList<TKey, TValue, TSizeProvider>; typedef TCache<TKey, TValue, TListType, TDeleter> TBase; - + public: TLRUCache(size_t maxSize, bool multiValue = false, const TSizeProvider& sizeProvider = TSizeProvider()) : TBase(TListType(maxSize, sizeProvider), multiValue) @@ -600,28 +600,28 @@ public: return TBase::List.GetTotalSize(); } }; - -template <typename TKey, typename TValue, typename TDeleter = TNoopDelete> + +template <typename TKey, typename TValue, typename TDeleter = TNoopDelete> class TLFUCache: public TCache<TKey, TValue, TLFUList<TKey, TValue>, TDeleter> { typedef TCache<TKey, TValue, TLFUList<TKey, TValue>, TDeleter> TBase; using TListType = TLFUList<TKey, TValue>; - -public: - typedef typename TBase::TIterator TIterator; - - TLFUCache(size_t maxSize, bool multiValue = false) + +public: + typedef typename TBase::TIterator TIterator; + + TLFUCache(size_t maxSize, bool multiValue = false) : TBase(TListType(maxSize), multiValue) - { - } - - TValue& GetLeastFrequentlyUsed() { - return TBase::List.GetLeastFrequentlyUsed()->Value; - } - - TIterator FindLeastFrequentlyUsed() { - return TBase::Empty() ? TBase::End() : this->FindByItem(TBase::List.GetLeastFrequentlyUsed()); - } -}; + { + } + + TValue& GetLeastFrequentlyUsed() { + return TBase::List.GetLeastFrequentlyUsed()->Value; + } + + TIterator FindLeastFrequentlyUsed() { + return TBase::Empty() ? TBase::End() : this->FindByItem(TBase::List.GetLeastFrequentlyUsed()); + } +}; // Least Weighted cache // discards the least weighted items first diff --git a/library/cpp/cache/ut/cache_ut.cpp b/library/cpp/cache/ut/cache_ut.cpp index 96fabc10d7..329872cfde 100644 --- a/library/cpp/cache/ut/cache_ut.cpp +++ b/library/cpp/cache/ut/cache_ut.cpp @@ -12,23 +12,23 @@ Y_UNIT_TEST_SUITE(TCacheTest) { Y_UNIT_TEST(LRUListTest) { typedef TLRUList<int, TString> TListType; TListType list(2); - + TListType::TItem x1(1, "ttt"); list.Insert(&x1); UNIT_ASSERT_EQUAL(list.GetOldest()->Key, 1); - + TListType::TItem x2(2, "yyy"); list.Insert(&x2); UNIT_ASSERT_EQUAL(list.GetOldest()->Key, 1); - + list.Promote(list.GetOldest()); UNIT_ASSERT_EQUAL(list.GetOldest()->Key, 2); - + TListType::TItem x3(3, "zzz"); list.Insert(&x3); UNIT_ASSERT_EQUAL(list.GetOldest()->Key, 1); - } - + } + Y_UNIT_TEST(LRUListWeightedTest) { typedef TLRUList<int, TString, size_t (*)(const TString&)> TListType; TListType list(7, [](auto& string) { @@ -68,23 +68,23 @@ Y_UNIT_TEST_SUITE(TCacheTest) { Y_UNIT_TEST(LFUListTest) { typedef TLFUList<int, TString> TListType; TListType list(2); - + TListType::TItem x1(1, "ttt"); list.Insert(&x1); UNIT_ASSERT_EQUAL(list.GetLeastFrequentlyUsed()->Key, 1); - + TListType::TItem x2(2, "yyy"); list.Insert(&x2); UNIT_ASSERT_EQUAL(list.GetLeastFrequentlyUsed()->Key, 1); - + list.Promote(list.GetLeastFrequentlyUsed()); UNIT_ASSERT_EQUAL(list.GetLeastFrequentlyUsed()->Key, 2); - + TListType::TItem x3(3, "zzz"); list.Insert(&x3); UNIT_ASSERT_EQUAL(list.GetLeastFrequentlyUsed()->Key, 1); - } - + } + Y_UNIT_TEST(LWListTest) { typedef TLWList<int, TString, size_t, TStrokaWeighter> TListType; TListType list(2); |