12 template <
class KeyType,
class ValueType>
23 return this->map_.empty();
27 return this->capacity_;
30 inline std::size_t
size()
const {
31 return this->map_.size();
34 inline bool contains(
const KeyType &key)
const {
35 return this->map_.find(key) != this->map_.end();
48 inline void insert(
const KeyType &key,
const ValueType &value) {
53 if(this->
size() >= this->capacity_) {
55 this->map_.erase(this->queue_.back());
56 this->queue_.pop_back();
60 this->queue_.push_front(key);
61 this->map_.emplace(key, std::make_pair(value, this->queue_.begin()));
69 inline ValueType *
get(
const KeyType &key) {
70 const auto i = this->map_.find(key);
71 if(i == this->map_.end()) {
77 const auto oldIt = i->second.second;
78 if(oldIt != this->queue_.begin()) {
80 this->queue_.erase(oldIt);
81 this->queue_.push_front(key);
84 this->map_[key].second = this->queue_.begin();
88 return &i->second.first;
92 using ListType = std::list<KeyType>;
94 = std::map<KeyType, std::pair<ValueType, typename ListType::iterator>>;
98 std::size_t capacity_;
LRU cache implementation.
std::size_t capacity() const
ValueType * get(const KeyType &key)
Get value pointer from key.
LRUCache(const std::size_t capacity)
void insert(const KeyType &key, const ValueType &value)
Insert new (key, value) entry.
bool contains(const KeyType &key) const