-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlru_hash.h
184 lines (156 loc) · 5.3 KB
/
lru_hash.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/**
* Author: Brendan Creane
*
* Hash (unordered_map) with LRU eviction when hash size exceeds capacity.
*/
#ifndef LRU_HASH_LRU_HASH_H
#define LRU_HASH_LRU_HASH_H
#include <unordered_map>
#include <cstdlib>
#include <list>
/**
* @file lru_hash.h
* @brief Provide a key/value hash with least recently used eviction when hash size exceeds capacity.
*
* Example: LruHash<int, std::string> fun(10);
* fun.insert(2, std::string("fun"));
*
* std::string value;
* fun.lookup(2, &value);
* fun.erase(2);
*/
template <class Key_t, class Value_t>
class LruHash
{
public:
/**
* @name LruHash
* @param[in] capacity Number of values the hash will hold before evicting least recently used key/values
*/
LruHash(std::size_t capacity);
/**
* @name get_capacity
* @brief retrieve the hash capacity
* @retval the number of values the hash will hold
*/
std::size_t get_capacity() const {return capacity_;}
/**
* @name set_capacity
* @brief set the hash capacity
* @param[in] capacity Number of values the hash will hold before evicting least recently used key/values
*
* Set_capacity will potentially evict enough key/value pairs to bring hash size below the new capacity.
*/
void set_capacity(std::size_t capacity);
/**
* @name lookup
* @brief Retrieve the value associated with given key. If the key isn't present in the hash,
* return false.
* @retval True if value_ptr holds the value associated with the key, false otherwise
* @param[in] key key to lookup
* @param[out] value_ptr pointer to Value_t. Value_ptr will point to a copy of the value associated
* with key.
*
* If lookup finds a key/value pair, that pair is moved to the "most recently used" position.
*/
bool lookup(const Key_t& key, Value_t* value_ptr);
/**
* @name insert
* @brief If key doesn't exist already, create hash entry for key/value. Otherwise, return false
* and leave
* original key/value pair.
* @retval Return true if a new key/value pair is inserted in hash, false if key already exists.
* @param[in] key key to associate with value
* @param[in] value value to associate with key
*/
bool insert(const Key_t& key, const Value_t& value);
/**
* @name erase
* @brief Erase key/value from hash.
* @retval Returns true if key exists, false otherwise.
* @param[in] key key to erase
*/
bool erase(const Key_t& key);
/**
* @name size
* @brief retrieve number of key/value pairs in hash
* @retval the number of key/value pairs in hash
*/
std::size_t size() const {return hash_map_.size();}
private:
std::size_t capacity_;
typedef std::unordered_map<Key_t, std::pair<typename std::list<Key_t>::iterator,
Value_t>> hash_t;
hash_t hash_map_;
std::list<Key_t> lru_dqueue_;
/**
* @name move_to_front
* @brief mark key/value pair as most recently used
* @param[in] key key to mark as most recently used
* @param[in] it iterator pointing to key/value pair in hash
*/
void move_to_front(const Key_t& key, const typename hash_t::iterator& it);
};
template <class Key_t, class Value_t>
bool LruHash<Key_t, Value_t>::erase(const Key_t& key)
{
auto it = hash_map_.find(key);
if (it == hash_map_.end()) {
return false;
}
lru_dqueue_.erase(it->second.first);
hash_map_.erase(it);
return true;
}
template <class Key_t, class Value_t>
void LruHash<Key_t, Value_t>::move_to_front(const Key_t& key,
const typename hash_t::iterator& it)
{
UNREFERENCED_PARAMETER(key);
// Move iterator to beginning of list
lru_dqueue_.splice(lru_dqueue_.begin(), lru_dqueue_, it->second.first);
it->second.first = lru_dqueue_.begin();
}
template <class Key_t, class Value_t>
LruHash<Key_t, Value_t>::LruHash(std::size_t capacity)
: capacity_(capacity)
{
}
template <class Key_t, class Value_t>
void LruHash<Key_t, Value_t>::set_capacity(std::size_t capacity)
{
capacity_ = capacity;
while (hash_map_.size() > capacity_) {
hash_map_.erase(lru_dqueue_.back());
lru_dqueue_.pop_back();
}
}
template <class Key_t, class Value_t>
bool LruHash<Key_t, Value_t>::insert(const Key_t& key, const Value_t& value)
{
auto it = hash_map_.find(key);
if (it != hash_map_.end()) {
move_to_front(key, it);
return false;
} else {
if (hash_map_.size() >= capacity_) {
hash_map_.erase(lru_dqueue_.back());
lru_dqueue_.pop_back();
}
lru_dqueue_.emplace_front(key);
hash_map_[key] = {lru_dqueue_.begin(), value};
}
return true;
}
template <class Key_t, class Value_t>
bool LruHash<Key_t, Value_t>::lookup(const Key_t& key, Value_t* value_ptr)
{
auto it = hash_map_.find(key);
if (it == hash_map_.end()) {
return false;
}
*value_ptr = it->second.second;
move_to_front(key, it);
return true;
}
#endif // LRU_HASH_LRU_HASH_H