Doxygen
Loading...
Searching...
No Matches
Cache< K, V > Class Template Reference

#include <src/cache.h>

Inheritance diagram for Cache< K, V >:

Public Types

using kv_pair = std::pair<K,V>
using iterator = typename std::list<kv_pair>::iterator
using const_iterator = typename std::list<kv_pair>::const_iterator

Public Member Functions

 Cache (size_t capacity)
 creates a cache that can hold capacity elements
V * insert (const K &key, V &&value)
 Inserts value under key in the cache.
V * insert (const K &key, const V &value)
 Inserts value under key in the cache.
void remove (const K &key)
V * find (const K &key)
size_t size () const
 Returns the number of values stored in the cache.
size_t capacity () const
 Returns the maximum number of values that can be stored in the cache.
uint64_t hits () const
 Returns how many of the find() calls did find a value in the cache.
uint64_t misses () const
 Returns how many of the find() calls did not found a value in the cache.
void clear ()
 Clears all values in the cache.
iterator begin ()
iterator end ()
const_iterator begin () const
const_iterator end () const

Private Member Functions

void resize ()

Private Attributes

size_t m_capacity
std::list< kv_pairm_cacheItemList
std::unordered_map< K, iteratorm_cacheItemMap
uint64_t m_hits =0
uint64_t m_misses =0

Detailed Description

template<typename K, typename V>
class Cache< K, V >

Fixed size cache for value type V using keys of type K.

When the maximum capacity has reached, the least recently used value is removed from the cache (LRU strategy).

Definition at line 31 of file cache.h.

Member Typedef Documentation

◆ const_iterator

template<typename K, typename V>
using Cache< K, V >::const_iterator = typename std::list<kv_pair>::const_iterator

Definition at line 36 of file cache.h.

◆ iterator

template<typename K, typename V>
using Cache< K, V >::iterator = typename std::list<kv_pair>::iterator

Definition at line 35 of file cache.h.

◆ kv_pair

template<typename K, typename V>
using Cache< K, V >::kv_pair = std::pair<K,V>

Definition at line 34 of file cache.h.

Constructor & Destructor Documentation

◆ Cache()

template<typename K, typename V>
Cache< K, V >::Cache ( size_t capacity)
inline

creates a cache that can hold capacity elements

Definition at line 39 of file cache.h.

40 {
41 }
size_t capacity() const
Returns the maximum number of values that can be stored in the cache.
Definition cache.h:132
size_t m_capacity
Definition cache.h:174

Member Function Documentation

◆ begin() [1/2]

template<typename K, typename V>
iterator Cache< K, V >::begin ( )
inline

Definition at line 156 of file cache.h.

156{ return m_cacheItemList.begin(); }
std::list< kv_pair > m_cacheItemList
Definition cache.h:176

◆ begin() [2/2]

template<typename K, typename V>
const_iterator Cache< K, V >::begin ( ) const
inline

Definition at line 158 of file cache.h.

158{ return m_cacheItemList.cbegin(); }

◆ capacity()

template<typename K, typename V>
size_t Cache< K, V >::capacity ( ) const
inline

Returns the maximum number of values that can be stored in the cache.

Definition at line 132 of file cache.h.

133 {
134 return m_capacity;
135 }

Referenced by mergeStatistics().

◆ clear()

template<typename K, typename V>
void Cache< K, V >::clear ( )
inline

Clears all values in the cache.

Definition at line 150 of file cache.h.

151 {
152 m_cacheItemMap.clear();
153 m_cacheItemList.clear();
154 }
std::unordered_map< K, iterator > m_cacheItemMap
Definition cache.h:178

◆ end() [1/2]

template<typename K, typename V>
iterator Cache< K, V >::end ( )
inline

Definition at line 157 of file cache.h.

157{ return m_cacheItemList.end(); }

◆ end() [2/2]

template<typename K, typename V>
const_iterator Cache< K, V >::end ( ) const
inline

Definition at line 159 of file cache.h.

159{ return m_cacheItemList.cend(); }

◆ find()

template<typename K, typename V>
V * Cache< K, V >::find ( const K & key)
inline

Finds a value in the cache given the corresponding key.

Returns
a pointer to the value or nullptr if the key is not found in the cache
Note
The hit and miss counters are updated, see hits() and misses().

Definition at line 105 of file cache.h.

106 {
107 auto it = m_cacheItemMap.find(key);
108 if (it != m_cacheItemMap.end())
109 {
110 // move the item to the front of the list
111 m_cacheItemList.splice(m_cacheItemList.begin(),
113 it->second);
114 m_hits++;
115 // return the value
116 return &it->second->second;
117 }
118 else
119 {
120 m_misses++;
121 }
122 return nullptr;
123 }
Definition cache.h:32
uint64_t m_misses
Definition cache.h:180
uint64_t m_hits
Definition cache.h:179

Referenced by SymbolResolver::Private::getResolvedSymbolRec(), and SymbolResolver::Private::getResolvedTypeRec().

◆ hits()

template<typename K, typename V>
uint64_t Cache< K, V >::hits ( ) const
inline

Returns how many of the find() calls did find a value in the cache.

Definition at line 138 of file cache.h.

139 {
140 return m_hits;
141 }

Referenced by mergeStatistics().

◆ insert() [1/2]

template<typename K, typename V>
V * Cache< K, V >::insert ( const K & key,
const V & value )
inline

Inserts value under key in the cache.

Definition at line 67 of file cache.h.

68 {
69 // reuse item if it already exists
70 auto it = m_cacheItemMap.find(key);
71 if (it != m_cacheItemMap.end())
72 {
73 // move the item to the front of the list
74 m_cacheItemList.splice(m_cacheItemList.begin(),
76 it->second);
77 it->second->second = value;
78 return &it->second->second;
79 }
80 // store new item
81 m_cacheItemList.push_front(kv_pair(key,value));
82 V *result = &m_cacheItemList.front().second;
84 // remove least recently used item if cache is full
85 resize();
86 return result;
87 }
std::pair< K, V > kv_pair
Definition cache.h:34
void resize()
Definition cache.h:163

◆ insert() [2/2]

template<typename K, typename V>
V * Cache< K, V >::insert ( const K & key,
V && value )
inline

Inserts value under key in the cache.

Definition at line 44 of file cache.h.

45 {
46 // reuse item if it already exists
47 auto it = m_cacheItemMap.find(key);
48 if (it != m_cacheItemMap.end())
49 {
50 // move the item to the front of the list
51 m_cacheItemList.splice(m_cacheItemList.begin(),
53 it->second);
54 std::exchange(it->second->second,value);
55 return &it->second->second;
56 }
57 // create new item
59 V *result = &m_cacheItemList.front().second;
61 // remove least recently used item if cache is full
62 resize();
63 return result;
64 }

Referenced by SymbolResolver::Private::getResolvedSymbolRec(), and SymbolResolver::Private::getResolvedTypeRec().

◆ misses()

template<typename K, typename V>
uint64_t Cache< K, V >::misses ( ) const
inline

Returns how many of the find() calls did not found a value in the cache.

Definition at line 144 of file cache.h.

145 {
146 return m_misses;
147 }

Referenced by mergeStatistics().

◆ remove()

template<typename K, typename V>
void Cache< K, V >::remove ( const K & key)
inline

Removes entry key from the cache.

Note
this invalidates any iterators

Definition at line 91 of file cache.h.

92 {
93 // remove item if it already exists
94 auto it = m_cacheItemMap.find(key);
95 if (it != m_cacheItemMap.end())
96 {
97 m_cacheItemList.erase(it->second);
98 m_cacheItemMap.erase(it);
99 }
100 }

◆ resize()

template<typename K, typename V>
void Cache< K, V >::resize ( )
inlineprivate

Definition at line 163 of file cache.h.

164 {
165 if (m_cacheItemMap.size() > m_capacity)
166 {
167 auto last_it = m_cacheItemList.end();
168 --last_it;
169 m_cacheItemMap.erase(last_it->first);
170 m_cacheItemList.pop_back();
171 }
172 }

Referenced by Cache< std::string, LookupInfo >::insert(), and Cache< std::string, LookupInfo >::insert().

◆ size()

template<typename K, typename V>
size_t Cache< K, V >::size ( ) const
inline

Returns the number of values stored in the cache.

Definition at line 126 of file cache.h.

127 {
128 return m_cacheItemMap.size();
129 }

Referenced by mergeStatistics().

Member Data Documentation

◆ m_cacheItemList

template<typename K, typename V>
std::list<kv_pair> Cache< K, V >::m_cacheItemList
private

Definition at line 176 of file cache.h.

◆ m_cacheItemMap

template<typename K, typename V>
std::unordered_map<K,iterator> Cache< K, V >::m_cacheItemMap
private

Definition at line 178 of file cache.h.

◆ m_capacity

template<typename K, typename V>
size_t Cache< K, V >::m_capacity
private

Definition at line 174 of file cache.h.

◆ m_hits

template<typename K, typename V>
uint64_t Cache< K, V >::m_hits =0
private

Definition at line 179 of file cache.h.

◆ m_misses

template<typename K, typename V>
uint64_t Cache< K, V >::m_misses =0
private

Definition at line 180 of file cache.h.


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