diff options
author | lapshov <lapshov@yandex-team.ru> | 2022-02-10 16:49:39 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:49:39 +0300 |
commit | 1ef52da9919aaa7ec7e3c51da7fdaa637ab133b7 (patch) | |
tree | 5d5cb817648f650d76cf1076100726fd9b8448e8 /library/cpp/cache | |
parent | 4f36f44b1e216dca1f44ada8d126e7b70f05da2f (diff) | |
download | ydb-1ef52da9919aaa7ec7e3c51da7fdaa637ab133b7.tar.gz |
Restoring authorship annotation for <lapshov@yandex-team.ru>. Commit 2 of 2.
Diffstat (limited to 'library/cpp/cache')
-rw-r--r-- | library/cpp/cache/cache.h | 72 | ||||
-rw-r--r-- | library/cpp/cache/ut/cache_ut.cpp | 20 |
2 files changed, 46 insertions, 46 deletions
diff --git a/library/cpp/cache/cache.h b/library/cpp/cache/cache.h index aa387ef110..6dc997076d 100644 --- a/library/cpp/cache/cache.h +++ b/library/cpp/cache/cache.h @@ -5,7 +5,7 @@ #include <util/generic/intrlist.h> #include <util/generic/hash_set.h> #include <util/generic/vector.h> -#include <util/generic/yexception.h> +#include <util/generic/yexception.h> #include <utility> template <class TValue> @@ -57,7 +57,7 @@ public: struct THash { size_t operator()(const TItem& item) const { - return ::THash<TKey>()(item.Key); + return ::THash<TKey>()(item.Key); } }; }; @@ -427,7 +427,7 @@ public: TCache(TListType&& list, bool multiValue = false) : Index() , List(std::move(list)) - , MultiValue(multiValue) + , MultiValue(multiValue) { } @@ -435,10 +435,10 @@ public: Clear(); } - size_t Size() const { - return Index.size(); - } - + size_t Size() const { + return Index.size(); + } + TIterator Begin() const { return TIterator(Index.begin()); } @@ -458,28 +458,28 @@ public: return TIterator(Index.find(TItem(key))); } - // note: it shouldn't touch 'value' if it returns false. + // note: it shouldn't touch 'value' if it returns false. bool PickOut(const TKey& key, TValue* value) { Y_ASSERT(value); - TIndexIterator it = Index.find(TItem(key)); - if (it == Index.end()) - return false; - *value = it->Value; - List.Erase(const_cast<TItem*>(&*it)); - Index.erase(it); + TIndexIterator it = Index.find(TItem(key)); + if (it == Index.end()) + return false; + *value = it->Value; + List.Erase(const_cast<TItem*>(&*it)); + Index.erase(it); Y_ASSERT(Index.size() == List.GetSize()); - return true; - } - + return true; + } + bool Insert(const std::pair<TKey, TValue>& p) { return Insert(p.first, p.second); } bool Insert(const TKey& key, const TValue& value) { - TItem tmpItem(key, value); - if (!MultiValue && Index.find(tmpItem) != Index.end()) - return false; - TIndexIterator it = Index.insert(tmpItem); + TItem tmpItem(key, value); + if (!MultiValue && Index.find(tmpItem) != Index.end()) + return false; + TIndexIterator it = Index.insert(tmpItem); TItem* insertedItem = const_cast<TItem*>(&*it); auto removedItem = List.Insert(insertedItem); @@ -497,8 +497,8 @@ public: } void Update(const TKey& key, const TValue& value) { - if (MultiValue) - ythrow yexception() << "TCache: can't \"Update\" in multicache"; + if (MultiValue) + ythrow yexception() << "TCache: can't \"Update\" in multicache"; TIterator it = Find(key); if (it != End()) { Erase(it); @@ -548,23 +548,23 @@ public: protected: TIndex Index; TListType List; - bool MultiValue; + bool MultiValue; TIterator FindByItem(TItem* item) { std::pair<TIndexIterator, TIndexIterator> p = Index.equal_range(*item); - // we have to delete the exact unlinked item (there may be multiple items for one key) - TIndexIterator it; + // we have to delete the exact unlinked item (there may be multiple items for one key) + TIndexIterator it; for (it = p.first; it != p.second; ++it) - if (&*it == item) - break; + if (&*it == item) + break; return (it == p.second ? End() : TIterator(it)); - } - + } + void EraseFromIndex(TItem* item) { TDeleter::Destroy(item->Value); - TIterator it = FindByItem(item); + TIterator it = FindByItem(item); Y_ASSERT(it != End()); - Index.erase(it.Iter); + Index.erase(it.Iter); } }; @@ -579,7 +579,7 @@ class TLRUCache: public TCache<TKey, TValue, TLRUList<TKey, TValue, TSizeProvide using TListType = TLRUList<TKey, TValue, TSizeProvider>; typedef TCache<TKey, TValue, TListType, TDeleter> TBase; -public: +public: TLRUCache(size_t maxSize, bool multiValue = false, const TSizeProvider& sizeProvider = TSizeProvider()) : TBase(TListType(maxSize, sizeProvider), multiValue) { @@ -591,10 +591,10 @@ public: TValue& GetOldest() { return TBase::List.GetOldest()->Value; } - - TIterator FindOldest() { + + TIterator FindOldest() { return TBase::Empty() ? TBase::End() : this->FindByItem(TBase::List.GetOldest()); - } + } size_t TotalSize() const { return TBase::List.GetTotalSize(); diff --git a/library/cpp/cache/ut/cache_ut.cpp b/library/cpp/cache/ut/cache_ut.cpp index 3f54a232a0..329872cfde 100644 --- a/library/cpp/cache/ut/cache_ut.cpp +++ b/library/cpp/cache/ut/cache_ut.cpp @@ -313,17 +313,17 @@ Y_UNIT_TEST_SUITE(TCacheTest) { Y_UNIT_TEST(MultiCacheTest) { typedef TLRUCache<int, TString> TCache; - TCache s(3, true); - UNIT_ASSERT(s.Insert(1, "abcd")); - UNIT_ASSERT(s.Insert(1, "bcde")); - UNIT_ASSERT(s.Insert(2, "fghi")); - UNIT_ASSERT(s.Insert(2, "ghij")); - // (1, "abcd") will be deleted - UNIT_ASSERT(*s.Find(1) == "bcde"); - // (1, "bcde") will be promoted - UNIT_ASSERT(*s.FindOldest() == "fghi"); + TCache s(3, true); + UNIT_ASSERT(s.Insert(1, "abcd")); + UNIT_ASSERT(s.Insert(1, "bcde")); + UNIT_ASSERT(s.Insert(2, "fghi")); + UNIT_ASSERT(s.Insert(2, "ghij")); + // (1, "abcd") will be deleted + UNIT_ASSERT(*s.Find(1) == "bcde"); + // (1, "bcde") will be promoted + UNIT_ASSERT(*s.FindOldest() == "fghi"); } - + struct TMyDelete { static int count; template <typename T> |