diff options
author | kikht <kikht@yandex-team.ru> | 2022-02-10 16:45:14 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:45:14 +0300 |
commit | 778e51ba091dc39e7b7fcab2b9cf4dbedfb6f2b5 (patch) | |
tree | be835aa92c6248212e705f25388ebafcf84bc7a1 /library/cpp/cache | |
parent | 194cae0e8855b11be2005e1eff12c660c3ee9774 (diff) | |
download | ydb-778e51ba091dc39e7b7fcab2b9cf4dbedfb6f2b5.tar.gz |
Restoring authorship annotation for <kikht@yandex-team.ru>. Commit 2 of 2.
Diffstat (limited to 'library/cpp/cache')
-rw-r--r-- | library/cpp/cache/cache.h | 102 | ||||
-rw-r--r-- | library/cpp/cache/ut/cache_ut.cpp | 278 |
2 files changed, 190 insertions, 190 deletions
diff --git a/library/cpp/cache/cache.h b/library/cpp/cache/cache.h index c5231e42d3..6dc997076d 100644 --- a/library/cpp/cache/cache.h +++ b/library/cpp/cache/cache.h @@ -101,20 +101,20 @@ public: return ItemsAmount; } - size_t GetTotalSize() const { - return TotalSize; - } - - size_t GetMaxSize() const { - return MaxSize; - } - - // It does not remove current items if newSize is less than TotalSize. - // Caller should use RemoveIfOverflown to clean up list in this case - void SetMaxSize(size_t newSize) { - MaxSize = newSize; - } - + size_t GetTotalSize() const { + return TotalSize; + } + + size_t GetMaxSize() const { + return MaxSize; + } + + // It does not remove current items if newSize is less than TotalSize. + // Caller should use RemoveIfOverflown to clean up list in this case + void SetMaxSize(size_t newSize) { + MaxSize = newSize; + } + private: typedef TIntrusiveList<TItem> TListType; TListType List; @@ -212,16 +212,16 @@ public: return ListSize; } - size_t GetMaxSize() const { - return MaxSize; - } - - // It does not remove current items if newSize is less than TotalSize. - // Caller should use RemoveIfOverflown to clean up list in this case - void SetMaxSize(size_t newSize) { - MaxSize = newSize; - } - + size_t GetMaxSize() const { + return MaxSize; + } + + // It does not remove current items if newSize is less than TotalSize. + // Caller should use RemoveIfOverflown to clean up list in this case + void SetMaxSize(size_t newSize) { + MaxSize = newSize; + } + private: typedef TIntrusiveList<TItem> TListType; TListType List; @@ -333,16 +333,16 @@ public: return Size; } - size_t GetMaxSize() const { - return MaxSize; - } - - // It does not remove current items if newSize is less than TotalSize. - // Caller should use RemoveIfOverflown to clean up list in this case - void SetMaxSize(size_t newSize) { - MaxSize = newSize; - } - + size_t GetMaxSize() const { + return MaxSize; + } + + // It does not remove current items if newSize is less than TotalSize. + // Caller should use RemoveIfOverflown to clean up list in this case + void SetMaxSize(size_t newSize) { + MaxSize = newSize; + } + void Clear() { Heap.clear(); Removed.clear(); @@ -531,20 +531,20 @@ public: Index.clear(); } - void SetMaxSize(size_t newSize) { - List.SetMaxSize(newSize); - - TItem* removedItem = nullptr; - while ((removedItem = List.RemoveIfOverflown())) { - EraseFromIndex(removedItem); - } - Y_ASSERT(Index.size() == List.GetSize()); - } - - size_t GetMaxSize() const { - return List.GetMaxSize(); - } - + void SetMaxSize(size_t newSize) { + List.SetMaxSize(newSize); + + TItem* removedItem = nullptr; + while ((removedItem = List.RemoveIfOverflown())) { + EraseFromIndex(removedItem); + } + Y_ASSERT(Index.size() == List.GetSize()); + } + + size_t GetMaxSize() const { + return List.GetMaxSize(); + } + protected: TIndex Index; TListType List; @@ -595,10 +595,10 @@ public: TIterator FindOldest() { return TBase::Empty() ? TBase::End() : this->FindByItem(TBase::List.GetOldest()); } - + size_t TotalSize() const { - return TBase::List.GetTotalSize(); - } + return TBase::List.GetTotalSize(); + } }; template <typename TKey, typename TValue, typename TDeleter = TNoopDelete> diff --git a/library/cpp/cache/ut/cache_ut.cpp b/library/cpp/cache/ut/cache_ut.cpp index 87601ae3d6..329872cfde 100644 --- a/library/cpp/cache/ut/cache_ut.cpp +++ b/library/cpp/cache/ut/cache_ut.cpp @@ -172,145 +172,145 @@ Y_UNIT_TEST_SUITE(TCacheTest) { UNIT_ASSERT(s.Find(3) == s.End()); } - Y_UNIT_TEST(LRUSetMaxSizeTest) { - typedef TLRUCache<int, TString> TCache; - TCache s(2); // size 2 - s.Insert(1, "abcd"); - s.Insert(2, "efgh"); - s.Insert(3, "ijkl"); - UNIT_ASSERT(s.GetOldest() == "efgh"); - UNIT_ASSERT(s.Find(1) == s.End()); - UNIT_ASSERT(s.Find(2) != s.End()); - UNIT_ASSERT(s.Find(3) != s.End()); - - // Increasing size should not change anything - s.SetMaxSize(3); - UNIT_ASSERT(s.GetOldest() == "efgh"); - UNIT_ASSERT(s.Find(1) == s.End()); - UNIT_ASSERT(s.Find(2) != s.End()); - UNIT_ASSERT(s.Find(3) != s.End()); - - // And we should be able to add fit more entries - s.Insert(4, "mnop"); - s.Insert(5, "qrst"); - UNIT_ASSERT(s.GetOldest() == "ijkl"); - UNIT_ASSERT(s.Find(1) == s.End()); - UNIT_ASSERT(s.Find(2) == s.End()); - UNIT_ASSERT(s.Find(3) != s.End()); - UNIT_ASSERT(s.Find(4) != s.End()); - UNIT_ASSERT(s.Find(5) != s.End()); - - // Decreasing size should remove oldest entries - s.SetMaxSize(2); - UNIT_ASSERT(s.GetOldest() == "mnop"); - UNIT_ASSERT(s.Find(1) == s.End()); - UNIT_ASSERT(s.Find(2) == s.End()); - UNIT_ASSERT(s.Find(3) == s.End()); - UNIT_ASSERT(s.Find(4) != s.End()); - UNIT_ASSERT(s.Find(5) != s.End()); - - // Ano no more entries will fit - s.Insert(6, "uvwx"); - UNIT_ASSERT(s.GetOldest() == "qrst"); - UNIT_ASSERT(s.Find(1) == s.End()); - UNIT_ASSERT(s.Find(2) == s.End()); - UNIT_ASSERT(s.Find(3) == s.End()); - UNIT_ASSERT(s.Find(4) == s.End()); - UNIT_ASSERT(s.Find(5) != s.End()); - UNIT_ASSERT(s.Find(6) != s.End()); - } - - Y_UNIT_TEST(LWSetMaxSizeTest) { - typedef TLWCache<int, TString, size_t, TStrokaWeighter> TCache; - TCache s(2); // size 2 - s.Insert(1, "a"); - s.Insert(2, "aa"); - s.Insert(3, "aaa"); - UNIT_ASSERT(s.GetLightest() == "aa"); - UNIT_ASSERT(s.Find(1) == s.End()); - UNIT_ASSERT(s.Find(2) != s.End()); - UNIT_ASSERT(s.Find(3) != s.End()); - - // Increasing size should not change anything - s.SetMaxSize(3); - UNIT_ASSERT(s.GetLightest() == "aa"); - UNIT_ASSERT(s.Find(1) == s.End()); - UNIT_ASSERT(s.Find(2) != s.End()); - UNIT_ASSERT(s.Find(3) != s.End()); - - // And we should be able to add fit more entries - s.Insert(4, "aaaa"); - s.Insert(5, "aaaaa"); - UNIT_ASSERT(s.GetLightest() == "aaa"); - UNIT_ASSERT(s.Find(1) == s.End()); - UNIT_ASSERT(s.Find(2) == s.End()); - UNIT_ASSERT(s.Find(3) != s.End()); - UNIT_ASSERT(s.Find(4) != s.End()); - UNIT_ASSERT(s.Find(5) != s.End()); - - // Decreasing size should remove oldest entries - s.SetMaxSize(2); - UNIT_ASSERT(s.GetLightest() == "aaaa"); - UNIT_ASSERT(s.Find(1) == s.End()); - UNIT_ASSERT(s.Find(2) == s.End()); - UNIT_ASSERT(s.Find(3) == s.End()); - UNIT_ASSERT(s.Find(4) != s.End()); - UNIT_ASSERT(s.Find(5) != s.End()); - - // Ano no more entries will fit - s.Insert(6, "aaaaaa"); - UNIT_ASSERT(s.GetLightest() == "aaaaa"); - UNIT_ASSERT(s.Find(1) == s.End()); - UNIT_ASSERT(s.Find(2) == s.End()); - UNIT_ASSERT(s.Find(3) == s.End()); - UNIT_ASSERT(s.Find(4) == s.End()); - UNIT_ASSERT(s.Find(5) != s.End()); - UNIT_ASSERT(s.Find(6) != s.End()); - } - - Y_UNIT_TEST(LFUSetMaxSizeTest) { - typedef TLFUCache<int, TString> TCache; - TCache s(2); // size 2 - s.Insert(1, "abcd"); - s.Insert(2, "efgh"); - s.Insert(3, "ijkl"); - UNIT_ASSERT(s.Find(1) == s.End()); - UNIT_ASSERT(s.Find(2) != s.End()); - UNIT_ASSERT(s.Find(3) != s.End()); - - // Increasing size should not change anything - s.SetMaxSize(3); - UNIT_ASSERT(s.Find(1) == s.End()); - UNIT_ASSERT(s.Find(2) != s.End()); - UNIT_ASSERT(s.Find(3) != s.End()); - - // And we should be able to add fit more entries - s.Insert(4, "mnop"); - s.Insert(5, "qrst"); - UNIT_ASSERT(s.Find(1) == s.End()); - UNIT_ASSERT(s.Find(2) == s.End()); - UNIT_ASSERT(s.Find(3) != s.End()); - UNIT_ASSERT(s.Find(4) != s.End()); - UNIT_ASSERT(s.Find(5) != s.End()); - - // Decreasing size should remove oldest entries - s.SetMaxSize(2); - UNIT_ASSERT(s.Find(1) == s.End()); - UNIT_ASSERT(s.Find(2) == s.End()); - UNIT_ASSERT(s.Find(3) != s.End()); - UNIT_ASSERT(s.Find(4) == s.End()); - UNIT_ASSERT(s.Find(5) != s.End()); - - // Ano no more entries will fit - s.Insert(6, "uvwx"); - UNIT_ASSERT(s.Find(1) == s.End()); - UNIT_ASSERT(s.Find(2) == s.End()); - UNIT_ASSERT(s.Find(3) != s.End()); - UNIT_ASSERT(s.Find(4) == s.End()); - UNIT_ASSERT(s.Find(5) == s.End()); - UNIT_ASSERT(s.Find(6) != s.End()); - } - + Y_UNIT_TEST(LRUSetMaxSizeTest) { + typedef TLRUCache<int, TString> TCache; + TCache s(2); // size 2 + s.Insert(1, "abcd"); + s.Insert(2, "efgh"); + s.Insert(3, "ijkl"); + UNIT_ASSERT(s.GetOldest() == "efgh"); + UNIT_ASSERT(s.Find(1) == s.End()); + UNIT_ASSERT(s.Find(2) != s.End()); + UNIT_ASSERT(s.Find(3) != s.End()); + + // Increasing size should not change anything + s.SetMaxSize(3); + UNIT_ASSERT(s.GetOldest() == "efgh"); + UNIT_ASSERT(s.Find(1) == s.End()); + UNIT_ASSERT(s.Find(2) != s.End()); + UNIT_ASSERT(s.Find(3) != s.End()); + + // And we should be able to add fit more entries + s.Insert(4, "mnop"); + s.Insert(5, "qrst"); + UNIT_ASSERT(s.GetOldest() == "ijkl"); + UNIT_ASSERT(s.Find(1) == s.End()); + UNIT_ASSERT(s.Find(2) == s.End()); + UNIT_ASSERT(s.Find(3) != s.End()); + UNIT_ASSERT(s.Find(4) != s.End()); + UNIT_ASSERT(s.Find(5) != s.End()); + + // Decreasing size should remove oldest entries + s.SetMaxSize(2); + UNIT_ASSERT(s.GetOldest() == "mnop"); + UNIT_ASSERT(s.Find(1) == s.End()); + UNIT_ASSERT(s.Find(2) == s.End()); + UNIT_ASSERT(s.Find(3) == s.End()); + UNIT_ASSERT(s.Find(4) != s.End()); + UNIT_ASSERT(s.Find(5) != s.End()); + + // Ano no more entries will fit + s.Insert(6, "uvwx"); + UNIT_ASSERT(s.GetOldest() == "qrst"); + UNIT_ASSERT(s.Find(1) == s.End()); + UNIT_ASSERT(s.Find(2) == s.End()); + UNIT_ASSERT(s.Find(3) == s.End()); + UNIT_ASSERT(s.Find(4) == s.End()); + UNIT_ASSERT(s.Find(5) != s.End()); + UNIT_ASSERT(s.Find(6) != s.End()); + } + + Y_UNIT_TEST(LWSetMaxSizeTest) { + typedef TLWCache<int, TString, size_t, TStrokaWeighter> TCache; + TCache s(2); // size 2 + s.Insert(1, "a"); + s.Insert(2, "aa"); + s.Insert(3, "aaa"); + UNIT_ASSERT(s.GetLightest() == "aa"); + UNIT_ASSERT(s.Find(1) == s.End()); + UNIT_ASSERT(s.Find(2) != s.End()); + UNIT_ASSERT(s.Find(3) != s.End()); + + // Increasing size should not change anything + s.SetMaxSize(3); + UNIT_ASSERT(s.GetLightest() == "aa"); + UNIT_ASSERT(s.Find(1) == s.End()); + UNIT_ASSERT(s.Find(2) != s.End()); + UNIT_ASSERT(s.Find(3) != s.End()); + + // And we should be able to add fit more entries + s.Insert(4, "aaaa"); + s.Insert(5, "aaaaa"); + UNIT_ASSERT(s.GetLightest() == "aaa"); + UNIT_ASSERT(s.Find(1) == s.End()); + UNIT_ASSERT(s.Find(2) == s.End()); + UNIT_ASSERT(s.Find(3) != s.End()); + UNIT_ASSERT(s.Find(4) != s.End()); + UNIT_ASSERT(s.Find(5) != s.End()); + + // Decreasing size should remove oldest entries + s.SetMaxSize(2); + UNIT_ASSERT(s.GetLightest() == "aaaa"); + UNIT_ASSERT(s.Find(1) == s.End()); + UNIT_ASSERT(s.Find(2) == s.End()); + UNIT_ASSERT(s.Find(3) == s.End()); + UNIT_ASSERT(s.Find(4) != s.End()); + UNIT_ASSERT(s.Find(5) != s.End()); + + // Ano no more entries will fit + s.Insert(6, "aaaaaa"); + UNIT_ASSERT(s.GetLightest() == "aaaaa"); + UNIT_ASSERT(s.Find(1) == s.End()); + UNIT_ASSERT(s.Find(2) == s.End()); + UNIT_ASSERT(s.Find(3) == s.End()); + UNIT_ASSERT(s.Find(4) == s.End()); + UNIT_ASSERT(s.Find(5) != s.End()); + UNIT_ASSERT(s.Find(6) != s.End()); + } + + Y_UNIT_TEST(LFUSetMaxSizeTest) { + typedef TLFUCache<int, TString> TCache; + TCache s(2); // size 2 + s.Insert(1, "abcd"); + s.Insert(2, "efgh"); + s.Insert(3, "ijkl"); + UNIT_ASSERT(s.Find(1) == s.End()); + UNIT_ASSERT(s.Find(2) != s.End()); + UNIT_ASSERT(s.Find(3) != s.End()); + + // Increasing size should not change anything + s.SetMaxSize(3); + UNIT_ASSERT(s.Find(1) == s.End()); + UNIT_ASSERT(s.Find(2) != s.End()); + UNIT_ASSERT(s.Find(3) != s.End()); + + // And we should be able to add fit more entries + s.Insert(4, "mnop"); + s.Insert(5, "qrst"); + UNIT_ASSERT(s.Find(1) == s.End()); + UNIT_ASSERT(s.Find(2) == s.End()); + UNIT_ASSERT(s.Find(3) != s.End()); + UNIT_ASSERT(s.Find(4) != s.End()); + UNIT_ASSERT(s.Find(5) != s.End()); + + // Decreasing size should remove oldest entries + s.SetMaxSize(2); + UNIT_ASSERT(s.Find(1) == s.End()); + UNIT_ASSERT(s.Find(2) == s.End()); + UNIT_ASSERT(s.Find(3) != s.End()); + UNIT_ASSERT(s.Find(4) == s.End()); + UNIT_ASSERT(s.Find(5) != s.End()); + + // Ano no more entries will fit + s.Insert(6, "uvwx"); + UNIT_ASSERT(s.Find(1) == s.End()); + UNIT_ASSERT(s.Find(2) == s.End()); + UNIT_ASSERT(s.Find(3) != s.End()); + UNIT_ASSERT(s.Find(4) == s.End()); + UNIT_ASSERT(s.Find(5) == s.End()); + UNIT_ASSERT(s.Find(6) != s.End()); + } + Y_UNIT_TEST(MultiCacheTest) { typedef TLRUCache<int, TString> TCache; TCache s(3, true); |