OmniSciDB  c0231cc57d
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
LruCache< key_t, value_t, hash_t > Class Template Reference

#include <LruCache.hpp>

Public Types

using const_list_iterator_t = typename cache_list_t::const_iterator
 

Public Member Functions

 LruCache (const size_t max_size)
 
void put (const key_t &key, value_t &&value)
 
void put (const key_t &key, const value_t &value)
 
value_t * get (const key_t &key)
 
const_list_iterator_t find (const key_t &key) const
 
const_list_iterator_t cend () const
 
void clear ()
 
void evictFractionEntries (const float fraction)
 
void evictNEntries (const size_t n)
 
size_t size () const
 

Private Types

using key_value_pair_t = typename std::pair< key_t, value_t >
 
using cache_list_t = typename std::list< key_value_pair_t >
 
using list_iterator_t = typename cache_list_t::iterator
 
using map_t = typename std::unordered_map< key_t, list_iterator_t, hash_t >
 
using map_t_iterator = typename map_t::iterator
 

Private Member Functions

void putCommon (map_t_iterator &it, key_t const &key)
 
void evictCommon (const size_t entries_to_evict)
 

Private Attributes

cache_list_t cache_items_list_
 
map_t cache_items_map_
 
size_t max_size_
 

Detailed Description

template<typename key_t, typename value_t, class hash_t = std::hash<key_t>>
class LruCache< key_t, value_t, hash_t >

Definition at line 16 of file LruCache.hpp.

Member Typedef Documentation

template<typename key_t, typename value_t, class hash_t = std::hash<key_t>>
using LruCache< key_t, value_t, hash_t >::cache_list_t = typename std::list<key_value_pair_t>
private

Definition at line 19 of file LruCache.hpp.

template<typename key_t, typename value_t, class hash_t = std::hash<key_t>>
using LruCache< key_t, value_t, hash_t >::const_list_iterator_t = typename cache_list_t::const_iterator

Definition at line 47 of file LruCache.hpp.

template<typename key_t, typename value_t, class hash_t = std::hash<key_t>>
using LruCache< key_t, value_t, hash_t >::key_value_pair_t = typename std::pair<key_t, value_t>
private

Definition at line 18 of file LruCache.hpp.

template<typename key_t, typename value_t, class hash_t = std::hash<key_t>>
using LruCache< key_t, value_t, hash_t >::list_iterator_t = typename cache_list_t::iterator
private

Definition at line 20 of file LruCache.hpp.

template<typename key_t, typename value_t, class hash_t = std::hash<key_t>>
using LruCache< key_t, value_t, hash_t >::map_t = typename std::unordered_map<key_t, list_iterator_t, hash_t>
private

Definition at line 21 of file LruCache.hpp.

template<typename key_t, typename value_t, class hash_t = std::hash<key_t>>
using LruCache< key_t, value_t, hash_t >::map_t_iterator = typename map_t::iterator
private

Definition at line 22 of file LruCache.hpp.

Constructor & Destructor Documentation

template<typename key_t, typename value_t, class hash_t = std::hash<key_t>>
LruCache< key_t, value_t, hash_t >::LruCache ( const size_t  max_size)
inline

Definition at line 25 of file LruCache.hpp.

25 : max_size_(max_size) {}
size_t max_size_
Definition: LruCache.hpp:109

Member Function Documentation

template<typename key_t, typename value_t, class hash_t = std::hash<key_t>>
const_list_iterator_t LruCache< key_t, value_t, hash_t >::cend ( ) const
inline

Definition at line 58 of file LruCache.hpp.

Referenced by LruCache< CompilationContext >::find().

58 { return (cache_items_list_.cend()); }
cache_list_t cache_items_list_
Definition: LruCache.hpp:107

+ Here is the caller graph for this function:

template<typename key_t, typename value_t, class hash_t = std::hash<key_t>>
void LruCache< key_t, value_t, hash_t >::clear ( )
inline

Definition at line 60 of file LruCache.hpp.

60  {
61  cache_items_list_.clear();
62  cache_items_map_.clear();
63  }
map_t cache_items_map_
Definition: LruCache.hpp:108
cache_list_t cache_items_list_
Definition: LruCache.hpp:107
template<typename key_t, typename value_t, class hash_t = std::hash<key_t>>
void LruCache< key_t, value_t, hash_t >::evictCommon ( const size_t  entries_to_evict)
inlineprivate

Definition at line 96 of file LruCache.hpp.

Referenced by LruCache< CompilationContext >::evictFractionEntries(), and LruCache< CompilationContext >::evictNEntries().

96  {
97  auto last = cache_items_list_.end();
98  size_t entries_erased = 0;
99  while (entries_erased < entries_to_evict && last != cache_items_list_.begin()) {
100  last--;
101  cache_items_map_.erase(last->first);
102  last = cache_items_list_.erase(last);
103  entries_erased++;
104  }
105  }
map_t cache_items_map_
Definition: LruCache.hpp:108
cache_list_t cache_items_list_
Definition: LruCache.hpp:107

+ Here is the caller graph for this function:

template<typename key_t, typename value_t, class hash_t = std::hash<key_t>>
void LruCache< key_t, value_t, hash_t >::evictFractionEntries ( const float  fraction)
inline

Definition at line 65 of file LruCache.hpp.

65  {
66  size_t entries_to_evict =
67  std::min(std::max(static_cast<size_t>(cache_items_map_.size() * fraction),
68  static_cast<size_t>(1)),
69  cache_items_map_.size());
70  evictCommon(entries_to_evict);
71  }
map_t cache_items_map_
Definition: LruCache.hpp:108
void evictCommon(const size_t entries_to_evict)
Definition: LruCache.hpp:96
template<typename key_t, typename value_t, class hash_t = std::hash<key_t>>
void LruCache< key_t, value_t, hash_t >::evictNEntries ( const size_t  n)
inline

Definition at line 73 of file LruCache.hpp.

73  {
74  size_t entries_to_evict = std::min(n, cache_items_map_.size());
75  evictCommon(entries_to_evict);
76  }
map_t cache_items_map_
Definition: LruCache.hpp:108
void evictCommon(const size_t entries_to_evict)
Definition: LruCache.hpp:96
constexpr double n
Definition: Utm.h:38
template<typename key_t, typename value_t, class hash_t = std::hash<key_t>>
const_list_iterator_t LruCache< key_t, value_t, hash_t >::find ( const key_t &  key) const
inline

Definition at line 49 of file LruCache.hpp.

49  {
50  auto it = cache_items_map_.find(key);
51  if (it == cache_items_map_.end()) {
52  return cend();
53  } else {
54  return it->second;
55  }
56  }
const_list_iterator_t cend() const
Definition: LruCache.hpp:58
map_t cache_items_map_
Definition: LruCache.hpp:108
template<typename key_t, typename value_t, class hash_t = std::hash<key_t>>
value_t* LruCache< key_t, value_t, hash_t >::get ( const key_t &  key)
inline

Definition at line 39 of file LruCache.hpp.

39  {
40  auto it = cache_items_map_.find(key);
41  if (it == cache_items_map_.end()) {
42  return nullptr;
43  }
44  cache_items_list_.splice(cache_items_list_.begin(), cache_items_list_, it->second);
45  return &it->second->second;
46  }
map_t cache_items_map_
Definition: LruCache.hpp:108
cache_list_t cache_items_list_
Definition: LruCache.hpp:107
template<typename key_t, typename value_t, class hash_t = std::hash<key_t>>
void LruCache< key_t, value_t, hash_t >::put ( const key_t &  key,
value_t &&  value 
)
inline

Definition at line 27 of file LruCache.hpp.

27  {
28  auto it = cache_items_map_.find(key);
29  cache_items_list_.emplace_front(key, std::forward<value_t&&>(value));
30  putCommon(it, key);
31  }
map_t cache_items_map_
Definition: LruCache.hpp:108
cache_list_t cache_items_list_
Definition: LruCache.hpp:107
void putCommon(map_t_iterator &it, key_t const &key)
Definition: LruCache.hpp:81
template<typename key_t, typename value_t, class hash_t = std::hash<key_t>>
void LruCache< key_t, value_t, hash_t >::put ( const key_t &  key,
const value_t &  value 
)
inline

Definition at line 33 of file LruCache.hpp.

33  {
34  auto it = cache_items_map_.find(key);
35  cache_items_list_.emplace_front(key, std::forward<const value_t&&>(value));
36  putCommon(it, key);
37  }
map_t cache_items_map_
Definition: LruCache.hpp:108
cache_list_t cache_items_list_
Definition: LruCache.hpp:107
void putCommon(map_t_iterator &it, key_t const &key)
Definition: LruCache.hpp:81
template<typename key_t, typename value_t, class hash_t = std::hash<key_t>>
void LruCache< key_t, value_t, hash_t >::putCommon ( map_t_iterator it,
key_t const &  key 
)
inlineprivate

Definition at line 81 of file LruCache.hpp.

Referenced by LruCache< CompilationContext >::put().

81  {
82  if (it != cache_items_map_.end()) {
83  cache_items_list_.erase(it->second);
84  cache_items_map_.erase(it);
85  }
86  cache_items_map_[key] = cache_items_list_.begin();
87 
88  if (cache_items_map_.size() > max_size_) {
89  auto last = cache_items_list_.end();
90  last--;
91  cache_items_map_.erase(last->first);
92  cache_items_list_.pop_back();
93  }
94  }
map_t cache_items_map_
Definition: LruCache.hpp:108
cache_list_t cache_items_list_
Definition: LruCache.hpp:107
size_t max_size_
Definition: LruCache.hpp:109

+ Here is the caller graph for this function:

template<typename key_t, typename value_t, class hash_t = std::hash<key_t>>
size_t LruCache< key_t, value_t, hash_t >::size ( ) const
inline

Definition at line 78 of file LruCache.hpp.

78 { return cache_items_list_.size(); }
cache_list_t cache_items_list_
Definition: LruCache.hpp:107

Member Data Documentation

template<typename key_t, typename value_t, class hash_t = std::hash<key_t>>
size_t LruCache< key_t, value_t, hash_t >::max_size_
private

Definition at line 109 of file LruCache.hpp.

Referenced by LruCache< CompilationContext >::putCommon().


The documentation for this class was generated from the following file: