28#include <olp/core/porting/try_emplace.h>
66template <
typename Key,
typename Value,
67 typename CacheCostFunc = CacheCost<Value>,
68 typename Compare = std::less<Key>,
69 template <
typename>
class Alloc = std::allocator>
73 std::map<Key, Bucket, Compare, Alloc<std::pair<const Key, Bucket> > >;
97 inline const Key&
key()
const;
104 inline const Value&
value()
const;
108 typename MapType::const_iterator
m_it;
200 template <
typename,
typename,
typename,
typename,
template <
typename>
class>
203 explicit const_iterator(
const typename MapType::const_iterator& it) {
236 LruCache(std::size_t maxSize, CacheCostFunc cacheCostFunc = CacheCostFunc(),
239 : cache_cost_func_(std::move(cacheCostFunc)),
240 map_(compare, alloc),
251 : eviction_callback_(std::move(other.eviction_callback_)),
252 cache_cost_func_(std::move(other.cache_cost_func_)),
253 map_(std::move(other.map_)),
254 first_(other.first_ != map_.end() ? map_.find(other.first_->first)
256 last_(other.last_ != map_.
end() ? map_.find(other.last_->first)
258 max_size_(other.max_size_),
259 size_(other.size_) {}
266 eviction_callback_ = std::move(other.eviction_callback_);
267 cache_cost_func_ = std::move(other.cache_cost_func_);
268 map_ = std::move(other.map_);
269 first_ = other.first_ != map_.end() ? map_.find(other.first_->first)
272 other.last_ != map_.end() ? map_.find(other.last_->first) : map_.end();
273 std::swap(max_size_, other.max_size_);
274 std::swap(size_, other.size_);
303 template <
typename _Key,
typename _Value>
304 std::pair<const_iterator, bool>
Insert(_Key&& key, _Value&& value);
322 template <
typename _Value>
334 auto it = map_.find(key);
335 if (it == map_.end())
362 std::size_t
Size()
const {
return size_; }
395 auto it = map_.find(key);
396 if (it != map_.end())
412 auto it = map_.find(key);
426 const Value&
Find(
const Key& key,
const Value& nullValue) {
428 return it ==
end() ? nullValue : it.
value();
451 first_ = last_ = map_.end();
468 eviction_callback_ = std::move(func);
472 friend struct CacheLruChecker;
475 CacheCostFunc cache_cost_func_;
477 typename MapType::iterator first_;
478 typename MapType::iterator last_;
479 std::size_t max_size_;
484 inline bool IsEqual(
const Key& key1,
const Key& key2) {
485 auto comp = map_.key_comp();
486 return !comp(key1, key2) && !comp(key2, key1);
492 using Iterator =
typename MapType::iterator;
496#if defined(_MSC_VER) && _MSC_VER < 1900
497 inline Bucket(Iterator next, Iterator previous, Value&& value)
498 : next_(std::move(next)),
499 previous_(std::move(previous)),
500 value_(std::move(value)) {}
501 inline Bucket(Iterator next, Iterator previous,
const Value& value)
502 : next_(std::move(next)),
503 previous_(std::move(previous)),
505 inline Bucket(
const Bucket&) =
delete;
506 inline Bucket(Bucket&& other)
507 : next_(std::move(other.next_)),
508 previous_(std::move(other.previous_)),
509 value_(std::move(other.value_)) {}
516 inline void setNext(Iterator it) { next_ = std::move(it); }
518 inline void setPrevious(Iterator it) { previous_ = std::move(it); }
521 void Erase(
typename MapType::iterator it,
bool);
522 void AddInternal(
const typename MapType::iterator& it, std::size_t cost,
523 std::size_t* oldCost =
nullptr);
524 void Promote(
const typename MapType::iterator& it);
527 while (size_ > max_size_)
532template <
typename Key,
typename Value,
typename CacheCostFunc,
533 typename Compare,
template <
typename>
class Alloc>
534template <
typename _Key,
typename _Value>
560 -> std::pair<const_iterator, bool> {
561 Bucket bucket{map_.end(), map_.end(), std::forward<_Value>(value)};
564 std::size_t valueCost = cache_cost_func_(bucket.value_);
565 if (valueCost > max_size_)
569 porting::try_emplace(map_, std::forward<_Key>(key), std::move(bucket));
572 AddInternal(it.first, valueCost);
579template <
typename Key,
typename Value,
typename CacheCostFunc,
580 typename Compare,
template <
typename>
class Alloc>
581template <
typename _Value>
599 Key key, _Value&& value) -> std::pair<const_iterator, bool> {
602 auto it = map_.lower_bound(key);
603 if (it != map_.end() && IsEqual(key, it->first)) {
605 std::size_t oldCost = cache_cost_func_(it->second.value_);
606 it->second.value_ = std::forward<_Value>(value);
607 std::size_t newCost = cache_cost_func_(it->second.value_);
608 AddInternal(it, newCost, &oldCost);
612 Bucket bucket{map_.end(), map_.end(), std::forward<_Value>(value)};
613 std::size_t newCost = cache_cost_func_(bucket.value_);
614 if (newCost > max_size_)
618 map_.insert(it, std::make_pair(std::move(key), std::move(bucket)));
619 AddInternal(new_it, newCost);
624template <
typename Key,
typename Value,
typename CacheCostFunc,
625 typename Compare,
template <
typename>
class Alloc>
627 const typename MapType::iterator& it) {
632 it->second.previous_->second.setNext(it->second.next_);
633 if (it->second.next_ != map_.end())
634 it->second.next_->second.setPrevious(it->second.previous_);
636 last_ = it->second.previous_;
639 it->second.setPrevious(map_.end());
640 it->second.setNext(first_);
643 first_->second.setPrevious(it);
647template <
typename Key,
typename Value,
typename CacheCostFunc,
648 typename Compare,
template <
typename>
class Alloc>
649void LruCache<Key, Value, CacheCostFunc, Compare, Alloc>::AddInternal(
650 const typename MapType::iterator& it, std::size_t cost,
651 std::size_t* oldCost) {
654 if (map_.size() == 1) {
656 assert(last_ == map_.end() && first_ == map_.end());
660 it->second.setNext(first_);
661 first_->second.setPrevious(it);
667 size_ += cost - *oldCost;
674template <
typename Key,
typename Value,
typename CacheCostFunc,
675 typename Compare,
template <
typename>
class Alloc>
677 typename MapType::iterator it,
bool doEvictionCallback) {
678 std::size_t cost = cache_cost_func_(it->second.value_);
680 if (it->second.next_ == map_.end())
681 last_ = it->second.previous_;
683 it->second.next_->second.setPrevious(it->second.previous_);
685 if (it->second.previous_ == map_.end())
686 first_ = it->second.next_;
688 it->second.previous_->second.setNext(it->second.next_);
690 if (doEvictionCallback && eviction_callback_)
691 eviction_callback_(it->first, std::move(it->second.value_));
697template <
typename Key,
typename Value,
typename CacheCostFunc,
698 typename Compare,
template <
typename>
class Alloc>
699void LruCache<Key, Value, CacheCostFunc, Compare, Alloc>::PopLast() {
701 assert(last_ != map_.end());
703 assert(last_->second.next_ == map_.end());
708template <
typename Key,
typename Value,
typename CacheCostFunc,
709 typename Compare,
template <
typename>
class Alloc>
715template <
typename Key,
typename Value,
typename CacheCostFunc,
716 typename Compare,
template <
typename>
class Alloc>
719 return m_it->second.value_;
722template <
typename Key,
typename Value,
typename CacheCostFunc,
723 typename Compare,
template <
typename>
class Alloc>
727 return this->m_it == other.
m_it;
730template <
typename Key,
typename Value,
typename CacheCostFunc,
731 typename Compare,
template <
typename>
class Alloc>
732inline typename LruCache<Key, Value, CacheCostFunc, Compare,
733 Alloc>::const_iterator&
736 this->m_it = this->m_it->second.next_;
740template <
typename Key,
typename Value,
typename CacheCostFunc,
741 typename Compare,
template <
typename>
class Alloc>
746 typename MapType::const_iterator old_value = this->m_it;
747 this->m_it = this->m_it->second.next_;
751template <
typename Key,
typename Value,
typename CacheCostFunc,
752 typename Compare,
template <
typename>
class Alloc>
753inline typename LruCache<Key, Value, CacheCostFunc, Compare,
754 Alloc>::const_iterator&
757 this->m_it = this->m_it->second.previous_;
761template <
typename Key,
typename Value,
typename CacheCostFunc,
762 typename Compare,
template <
typename>
class Alloc>
767 typename MapType::const_iterator old_value = this->m_it;
768 this->m_it = this->m_it->second.previous_;
A type of objects to be stored.
Definition LruCache.h:90
const Key & key() const
Gets the key of the ValueType object.
Definition LruCache.h:711
MapType::const_iterator m_it
A typename of the map constant iterator.
Definition LruCache.h:108
const Value & value() const
Gets the value of the ValueType object.
Definition LruCache.h:718
A constant iterator of the LruCache object.
Definition LruCache.h:112
bool operator!=(const const_iterator &other) const
Checks whether the values of the const_iterator parameter are not the same as the values of the other...
Definition LruCache.h:153
std::ptrdiff_t difference_type
A typedef for the difference type.
Definition LruCache.h:117
const value_type & reference
A typedef for the ValueType constant reference.
Definition LruCache.h:121
const_iterator()=default
Creates a constant iterator object.
std::bidirectional_iterator_tag iterator_category
A typedef for the iterator category.
Definition LruCache.h:115
const_iterator(const const_iterator &)=default
Creates a constant iterator object.
pointer operator->() const
Gets a pointer to this object.
Definition LruCache.h:197
reference operator*() const
Gets a reference to this object.
Definition LruCache.h:190
const_iterator & operator=(const const_iterator &)=default
Copies this and the specified iterator to this.
bool operator==(const const_iterator &other) const
Checks whether the values of the const_iterator parameter are the same as the values of the other par...
Definition LruCache.h:725
const_iterator & operator++()
Iterates to the next LruCache object.
Definition LruCache.h:735
ValueType value_type
A typedef for the ValueType type.
Definition LruCache.h:119
const value_type * pointer
A typedef for the ValueType constant pointer.
Definition LruCache.h:123
const_iterator & operator--()
Iterates to the previous LruCache object.
Definition LruCache.h:756
A generic key-value LRU cache.
Definition LruCache.h:70
std::size_t Size() const
Gets the current size of the cache.
Definition LruCache.h:362
const_iterator Erase(const_iterator &it)
Removes a key from the cache.
Definition LruCache.h:349
const_iterator FindNoPromote(const Key &key) const
Finds a value in the cache.
Definition LruCache.h:411
void Resize(size_t maxSize)
Sets the new maximum size of the cache.
Definition LruCache.h:379
typename MapType::key_compare CompareType
An alias for the key comparison function.
Definition LruCache.h:80
LruCache(const AllocType &alloc=AllocType())
Creates an LruCache instance.
Definition LruCache.h:220
void SetEvictionCallback(EvictionFunction func)
Sets a function that is invoked when a value is evicted from the cache.
Definition LruCache.h:467
bool Erase(const Key &key)
Removes a key from the cache.
Definition LruCache.h:333
std::function< void(const Key &, Value &&)> EvictionFunction
An alias for the eviction function.
Definition LruCache.h:77
typename MapType::allocator_type AllocType
An alias for the cache allocator type.
Definition LruCache.h:83
LruCache(std::size_t maxSize, CacheCostFunc cacheCostFunc=CacheCostFunc(), const CompareType &compare=CompareType(), const AllocType &alloc=AllocType())
Creates an LruCache instance.
Definition LruCache.h:236
const_iterator rend() const
Returns a reverse constant iterator to the end.
Definition LruCache.h:441
std::pair< const_iterator, bool > InsertOrAssign(Key key, _Value &&value)
Inserts a key-value pair in the cache or updates an existing key-value pair.
const Value & Find(const Key &key, const Value &nullValue)
Finds a value in the cache.
Definition LruCache.h:426
const_iterator rbegin() const
Returns a reverse constant iterator to the beginning.
Definition LruCache.h:438
const_iterator Find(const Key &key)
Finds a value in the cache.
Definition LruCache.h:394
std::pair< const_iterator, bool > Insert(_Key &&key, _Value &&value)
Inserts a key-value pair in the cache.
void Clear()
Removes all items from the cache.
Definition LruCache.h:449
LruCache(const LruCache &)=delete
The deleted copy constructor.
std::size_t GetMaxSize() const
Gets the maximum size of the cache.
Definition LruCache.h:369
LruCache & operator=(const LruCache &)=delete
The deleted assignment operator.
LruCache(LruCache &&other) noexcept
The default move constructor.
Definition LruCache.h:250
LruCache & operator=(LruCache &&other) noexcept
The default move assignment operator.
Definition LruCache.h:265
const_iterator begin() const
Returns a constant iterator to the beginning.
Definition LruCache.h:432
const_iterator end() const
Returns a constant iterator to the end.
Definition LruCache.h:435
Rules all the other namespaces.
Definition AppleSignInProperties.h:24
The default cost operator for LruCache.
Definition LruCache.h:41
std::size_t operator()(const T &) const
Gets the size of the specified object.
Definition LruCache.h:43