Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add forwarding references to Put operation to avoid always copying to shared_ptr #36

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions include/cache.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ class fixed_sized_cache
* \param[in] key Key value to use
* \param[in] value Value to assign to the given key
*/
void Put(const Key &key, const Value &value) noexcept
template<typename T=Value>
WrappedValue<Value> Put(const Key &key, T &&value) noexcept
{
operation_guard lock{safe_op};
auto elem_it = FindElem(key);
Expand All @@ -88,12 +89,12 @@ class fixed_sized_cache
Erase(disp_candidate_key);
}

Insert(key, value);
return Insert(key, std::forward<T>(value));
}
else
{
// update previous value
Update(key, value);
return Update(key, std::forward<T>(value));
}
}

Expand Down Expand Up @@ -204,10 +205,13 @@ class fixed_sized_cache
}

protected:
void Insert(const Key &key, const Value &value)
template<typename T=Value>
WrappedValue<Value> Insert(const Key &key, T &&value)
{
cache_policy.Insert(key);
cache_items_map.emplace(std::make_pair(key, std::make_shared<Value>(value)));
auto ptr = std::make_shared<Value>(std::forward<T>(value));
cache_items_map.emplace(std::make_pair(key, ptr));
return ptr;
}

void Erase(const_iterator elem)
Expand All @@ -224,10 +228,13 @@ class fixed_sized_cache
Erase(elem_it);
}

void Update(const Key &key, const Value &value)
template<class T=Value>
WrappedValue<Value> Update(const Key &key, T &&value)
{
cache_policy.Touch(key);
cache_items_map[key] = std::make_shared<Value>(value);
auto ptr = std::make_shared<Value>(std::forward<T>(value));;
cache_items_map[key] = ptr;
return ptr;
}

const_iterator FindElem(const Key &key) const
Expand Down
7 changes: 5 additions & 2 deletions test/fifo_cache_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ TEST(FIFOCache, Simple_Test)
{
fifo_cache_t<int, int> fc(2);

fc.Put(1, 10);
fc.Put(2, 20);
auto val1 = fc.Put(1, 10);
auto val2 = fc.Put(2, 20);

EXPECT_EQ(*val1, 10);
EXPECT_EQ(*val2, 20);

EXPECT_EQ(fc.Size(), 2);
EXPECT_EQ(*fc.Get(1), 10);
Expand Down
7 changes: 5 additions & 2 deletions test/lfu_cache_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@ TEST(LFUCache, Simple_Test)
constexpr size_t THIRD_FREQ = 8;
lfu_cache_t<std::string, int> cache(3);

cache.Put("A", 1);
cache.Put("B", 2);
auto val1 = cache.Put("A", 1);
auto val2 = cache.Put("B", 2);
cache.Put("C", 3);

EXPECT_EQ(*val1, 1);
EXPECT_EQ(*val2, 2);

for (size_t i = 0; i < FIRST_FREQ; ++i)
{
EXPECT_EQ(*cache.Get("B"), 2);
Expand Down
3 changes: 2 additions & 1 deletion test/lru_cache_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ TEST(CacheTest, SimplePut)
{
lru_cache_t<std::string, int> cache(1);

cache.Put("test", 666);
auto val = cache.Put("test", 666);
EXPECT_EQ(*val, 666);

EXPECT_EQ(*cache.Get("test"), 666);
}
Expand Down
14 changes: 14 additions & 0 deletions test/nopolicy_cache_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@ TEST(NoPolicyCache, Add_one_element)
ASSERT_EQ(*cache.Get("Hello"), 1);
}

TEST(NoPolicyCache, Put_returns_shared_ptr)
{
constexpr std::size_t cache_size = 1;
caches::fixed_sized_cache<std::string, int> cache(cache_size);

auto val = cache.Put("Hello", 1);
ASSERT_EQ(*val, 1);
ASSERT_EQ(*cache.Get("Hello"), 1);

// update existing element
auto val_updated = cache.Put("Hello", 2);
ASSERT_EQ(*val_updated, 2);
}

TEST(NoPolicyCache, Add_delete_add_one_element)
{
constexpr std::size_t cache_size = 1;
Expand Down