5#ifndef LFU_CACHE_POLICY_HPP
6#define LFU_CACHE_POLICY_HPP
12#include <unordered_map>
29template <
typename Key>
33 using lfu_iterator =
typename std::multimap<std::size_t, Key>::iterator;
40 constexpr std::size_t INIT_VAL = 1;
43 frequency_storage.emplace_hint(frequency_storage.cbegin(), INIT_VAL, key);
46 void Touch(
const Key &key)
override
49 auto elem_for_update = lfu_storage[key];
50 auto updated_elem = std::make_pair(elem_for_update->first + 1, elem_for_update->second);
52 frequency_storage.erase(elem_for_update);
54 frequency_storage.emplace_hint(frequency_storage.cend(), std::move(updated_elem));
57 void Erase(
const Key &key)
noexcept override
59 frequency_storage.erase(lfu_storage[key]);
60 lfu_storage.erase(key);
67 return frequency_storage.cbegin()->second;
71 std::multimap<std::size_t, Key> frequency_storage;
72 std::unordered_map<Key, lfu_iterator> lfu_storage;
Cache policy interface declaration.
Cache policy abstract base class.
Definition: cache_policy.hpp:19
LFU (Least frequently used) cache policy.
Definition: lfu_cache_policy.hpp:31
void Touch(const Key &key) override
Handle request to the key-element in a cache.
Definition: lfu_cache_policy.hpp:46
const Key & ReplCandidate() const noexcept override
Return a key of a replacement candidate.
Definition: lfu_cache_policy.hpp:63
void Erase(const Key &key) noexcept override
Handle element deletion from a cache.
Definition: lfu_cache_policy.hpp:57
void Insert(const Key &key) override
Handle element insertion in a cache.
Definition: lfu_cache_policy.hpp:38