Backport more common classes
This commit is contained in:
@ -16,7 +16,10 @@ class LRUCache
|
||||
using MapType = std::map<K, Item>;
|
||||
|
||||
public:
|
||||
LRUCache(std::size_t max_capacity = 16) : m_max_capacity(max_capacity) {}
|
||||
LRUCache(std::size_t max_capacity = 16, bool manual_evict = false)
|
||||
: m_max_capacity(max_capacity), m_manual_evict(manual_evict)
|
||||
{
|
||||
}
|
||||
~LRUCache() = default;
|
||||
|
||||
std::size_t GetSize() const { return m_items.size(); }
|
||||
@ -31,7 +34,8 @@ public:
|
||||
Evict(m_items.size() - m_max_capacity);
|
||||
}
|
||||
|
||||
V* Lookup(const K& key)
|
||||
template<typename KeyT>
|
||||
V* Lookup(const KeyT& key)
|
||||
{
|
||||
auto iter = m_items.find(key);
|
||||
if (iter == m_items.end())
|
||||
@ -41,7 +45,7 @@ public:
|
||||
return &iter->second.value;
|
||||
}
|
||||
|
||||
V* Insert(const K& key, V value)
|
||||
V* Insert(K key, V value)
|
||||
{
|
||||
ShrinkForNewItem();
|
||||
|
||||
@ -57,7 +61,7 @@ public:
|
||||
Item it;
|
||||
it.last_access = ++m_last_counter;
|
||||
it.value = std::move(value);
|
||||
auto ip = m_items.emplace(key, std::move(it));
|
||||
auto ip = m_items.emplace(std::move(key), std::move(it));
|
||||
return &ip.first->second.value;
|
||||
}
|
||||
}
|
||||
@ -76,7 +80,8 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
bool Remove(const K& key)
|
||||
template<typename KeyT>
|
||||
bool Remove(const KeyT& key)
|
||||
{
|
||||
auto iter = m_items.find(key);
|
||||
if (iter == m_items.end())
|
||||
@ -86,6 +91,20 @@ public:
|
||||
return true;
|
||||
}
|
||||
|
||||
void SetManualEvict(bool block)
|
||||
{
|
||||
m_manual_evict = block;
|
||||
if (!m_manual_evict)
|
||||
ManualEvict();
|
||||
}
|
||||
|
||||
void ManualEvict()
|
||||
{
|
||||
// evict if we went over
|
||||
while (m_items.size() > m_max_capacity)
|
||||
Evict(m_items.size() - (m_max_capacity - 1));
|
||||
}
|
||||
|
||||
private:
|
||||
void ShrinkForNewItem()
|
||||
{
|
||||
@ -98,4 +117,5 @@ private:
|
||||
MapType m_items;
|
||||
CounterType m_last_counter = 0;
|
||||
std::size_t m_max_capacity = 0;
|
||||
bool m_manual_evict = false;
|
||||
};
|
||||
Reference in New Issue
Block a user