diff options
author | lucius <lucius@yandex-team.ru> | 2022-02-10 16:50:14 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:50:14 +0300 |
commit | fcc260ce89e9b359b47474d8dfa6dfcb6aae3fe9 (patch) | |
tree | 5d5cb817648f650d76cf1076100726fd9b8448e8 /library/cpp/cache | |
parent | 95ad0bd7bde5cd56bd6395ad6b7c47f0e73e4c99 (diff) | |
download | ydb-fcc260ce89e9b359b47474d8dfa6dfcb6aae3fe9.tar.gz |
Restoring authorship annotation for <lucius@yandex-team.ru>. Commit 2 of 2.
Diffstat (limited to 'library/cpp/cache')
-rw-r--r-- | library/cpp/cache/cache.h | 232 | ||||
-rw-r--r-- | library/cpp/cache/ut/cache_ut.cpp | 24 |
2 files changed, 128 insertions, 128 deletions
diff --git a/library/cpp/cache/cache.h b/library/cpp/cache/cache.h index cc9005d2b0..6dc997076d 100644 --- a/library/cpp/cache/cache.h +++ b/library/cpp/cache/cache.h @@ -1,6 +1,6 @@ #pragma once -#include <util/generic/algorithm.h> +#include <util/generic/algorithm.h> #include <util/generic/ptr.h> #include <util/generic/intrlist.h> #include <util/generic/hash_set.h> @@ -229,60 +229,60 @@ private: size_t MaxSize; }; -// Least Weighted list -// discards the least weighted items first -// doesn't support promotion -template <typename TKey, typename TValue, typename TWeight, typename TWeighter> -class TLWList { -public: - TLWList(size_t maxSize) - : Size(0) - , MaxSize(maxSize) - { - } - - struct TItem { - TItem(const TKey& key, const TValue& value = TValue()) - : Key(key) - , Value(value) - , Weight(TWeighter::Weight(value)) - { - } - - TItem(const TItem& rhs) - : Key(rhs.Key) - , Value(rhs.Value) - , Weight(rhs.Weight) - { - } - +// Least Weighted list +// discards the least weighted items first +// doesn't support promotion +template <typename TKey, typename TValue, typename TWeight, typename TWeighter> +class TLWList { +public: + TLWList(size_t maxSize) + : Size(0) + , MaxSize(maxSize) + { + } + + struct TItem { + TItem(const TKey& key, const TValue& value = TValue()) + : Key(key) + , Value(value) + , Weight(TWeighter::Weight(value)) + { + } + + TItem(const TItem& rhs) + : Key(rhs.Key) + , Value(rhs.Value) + , Weight(rhs.Weight) + { + } + 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; - TWeight Weight; - - struct THash { + return Key == rhs.Key; + } + + TKey Key; + TValue Value; + TWeight Weight; + + struct THash { size_t operator()(const TItem& item) const { - return ::THash<TKey>()(item.Key); - } - }; - }; - - struct THeapComparator { - bool operator()(TItem* const item1, TItem* const item2) const { - return item1->Weight > item2->Weight; - } - }; - -public: - TItem* Insert(TItem* item) { + return ::THash<TKey>()(item.Key); + } + }; + }; + + struct THeapComparator { + bool operator()(TItem* const item1, TItem* const item2) const { + return item1->Weight > item2->Weight; + } + }; + +public: + TItem* Insert(TItem* item) { FixHeap(); if (Size >= MaxSize && item->Weight < GetLightest()->Weight) { @@ -298,41 +298,41 @@ public: TItem* RemoveIfOverflown() { if (Size <= MaxSize) { - return nullptr; - } + return nullptr; + } auto lightest = GetLightest(); Erase(lightest); PopHeap(Heap.begin(), Heap.end(), THeapComparator()); return lightest; - } - - TItem* GetLightest() { + } + + TItem* GetLightest() { FixHeap(); Y_ASSERT(!Heap.empty()); - return Heap.front(); - } - - // This method doesn't remove items from the heap. - // Erased items are stored in Removed set - // and will be deleted on-access (using FixHeap method) - void Erase(TItem* item) { + return Heap.front(); + } + + // This method doesn't remove items from the heap. + // Erased items are stored in Removed set + // and will be deleted on-access (using FixHeap method) + void Erase(TItem* item) { Y_ASSERT(Size > 0); - --Size; + --Size; Removed.insert(item); - } - - void Promote(TItem*) { - // do nothing - } - + } + + void Promote(TItem*) { + // do nothing + } + [[nodiscard]] size_t GetSize() const { - return Size; - } - + return Size; + } + size_t GetMaxSize() const { return MaxSize; } @@ -343,18 +343,18 @@ public: MaxSize = newSize; } - void Clear() { - Heap.clear(); - Removed.clear(); - Size = 0; - } - + void Clear() { + Heap.clear(); + Removed.clear(); + Size = 0; + } + private: - // Physically remove erased elements from the heap - void FixHeap() { + // Physically remove erased elements from the heap + void FixHeap() { if (Removed.empty()) { return; - } + } Heap.erase(std::remove_if(Heap.begin(), Heap.end(), [this](TItem* item) { return this->Removed.contains(item); @@ -363,16 +363,16 @@ private: MakeHeap(Heap.begin(), Heap.end(), THeapComparator()); Removed.clear(); Size = Heap.size(); - } - -private: + } + +private: TVector<TItem*> Heap; THashSet<TItem*> Removed; - - size_t Size; - size_t MaxSize; -}; - + + size_t Size; + size_t MaxSize; +}; + template <typename TKey, typename TValue, typename TListType, typename TDeleter> class TCache { typedef typename TListType::TItem TItem; @@ -481,11 +481,11 @@ public: return false; TIndexIterator it = Index.insert(tmpItem); - TItem* insertedItem = const_cast<TItem*>(&*it); + TItem* insertedItem = const_cast<TItem*>(&*it); auto removedItem = List.Insert(insertedItem); auto insertedWasRemoved = removedItem == insertedItem; - if (removedItem) { - EraseFromIndex(removedItem); + if (removedItem) { + EraseFromIndex(removedItem); while ((removedItem = List.RemoveIfOverflown())) { insertedWasRemoved = insertedWasRemoved || insertedItem == removedItem; EraseFromIndex(removedItem); @@ -622,28 +622,28 @@ public: return TBase::Empty() ? TBase::End() : this->FindByItem(TBase::List.GetLeastFrequentlyUsed()); } }; - -// Least Weighted cache -// discards the least weighted items first -// doesn't support promotion -template <typename TKey, typename TValue, typename TWeight, typename TWeighter, typename TDeleter = TNoopDelete> + +// Least Weighted cache +// discards the least weighted items first +// doesn't support promotion +template <typename TKey, typename TValue, typename TWeight, typename TWeighter, typename TDeleter = TNoopDelete> class TLWCache: public TCache<TKey, TValue, TLWList<TKey, TValue, TWeight, TWeighter>, TDeleter> { - typedef TCache<TKey, TValue, TLWList<TKey, TValue, TWeight, TWeighter>, TDeleter> TBase; + typedef TCache<TKey, TValue, TLWList<TKey, TValue, TWeight, TWeighter>, TDeleter> TBase; using TListType = TLWList<TKey, TValue, TWeight, TWeighter>; - -public: - typedef typename TBase::TIterator TIterator; - - TLWCache(size_t maxSize, bool multiValue = false) + +public: + typedef typename TBase::TIterator TIterator; + + TLWCache(size_t maxSize, bool multiValue = false) : TBase(TListType(maxSize), multiValue) - { - } - - TValue& GetLightest() { - return TBase::List.GetLightest()->Value; - } - - TIterator FindLightest() { - return TBase::Empty() ? TBase::End() : this->FindByItem(TBase::List.GetLightest()); - } -}; + { + } + + TValue& GetLightest() { + return TBase::List.GetLightest()->Value; + } + + TIterator FindLightest() { + return TBase::Empty() ? TBase::End() : this->FindByItem(TBase::List.GetLightest()); + } +}; diff --git a/library/cpp/cache/ut/cache_ut.cpp b/library/cpp/cache/ut/cache_ut.cpp index cf2ea3eeab..329872cfde 100644 --- a/library/cpp/cache/ut/cache_ut.cpp +++ b/library/cpp/cache/ut/cache_ut.cpp @@ -2,12 +2,12 @@ #include <library/cpp/cache/thread_safe_cache.h> #include <library/cpp/testing/unittest/registar.h> -struct TStrokaWeighter { +struct TStrokaWeighter { static size_t Weight(const TString& s) { - return s.size(); - } -}; - + return s.size(); + } +}; + Y_UNIT_TEST_SUITE(TCacheTest) { Y_UNIT_TEST(LRUListTest) { typedef TLRUList<int, TString> TListType; @@ -88,32 +88,32 @@ Y_UNIT_TEST_SUITE(TCacheTest) { Y_UNIT_TEST(LWListTest) { typedef TLWList<int, TString, size_t, TStrokaWeighter> TListType; TListType list(2); - + TListType::TItem x1(1, "tt"); list.Insert(&x1); UNIT_ASSERT_EQUAL(list.GetLightest()->Key, 1); UNIT_ASSERT_EQUAL(list.GetSize(), 1); - + TListType::TItem x2(2, "yyyy"); list.Insert(&x2); UNIT_ASSERT_EQUAL(list.GetLightest()->Key, 1); UNIT_ASSERT_EQUAL(list.GetSize(), 2); - + TListType::TItem x3(3, "z"); list.Insert(&x3); UNIT_ASSERT_EQUAL(list.GetLightest()->Key, 1); UNIT_ASSERT_EQUAL(list.GetSize(), 2); - + TListType::TItem x4(4, "xxxxxx"); list.Insert(&x4); UNIT_ASSERT_EQUAL(list.GetLightest()->Key, 2); UNIT_ASSERT_EQUAL(list.GetSize(), 2); - + list.Erase(&x2); UNIT_ASSERT_EQUAL(list.GetLightest()->Key, 4); UNIT_ASSERT_EQUAL(list.GetSize(), 1); - } - + } + Y_UNIT_TEST(SimpleTest) { typedef TLRUCache<int, TString> TCache; TCache s(2); // size 2 |