-
Notifications
You must be signed in to change notification settings - Fork 0
/
LRU Cache
58 lines (49 loc) · 1.37 KB
/
LRU Cache
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
class LRUCache{
public:
struct CacheEntry
{
public:
int key;
int value;
CacheEntry(int k, int v) :key(k), value(v) {}
};
LRUCache(int capacity) {
m_capacity = capacity;
}
int get(int key) {
if (m_map.find(key) == m_map.end())
return -1;
MoveToHead(key);
return m_map[key]->value;
}
void set(int key, int value) {
if (m_map.find(key) == m_map.end())
{
CacheEntry newItem(key, value);
if (m_LRU_cache.size() >= m_capacity)
{
//remove from tail
m_map.erase(m_LRU_cache.back().key);
m_LRU_cache.pop_back();
}
// insert in head.
m_LRU_cache.push_front(newItem);
m_map[key] = m_LRU_cache.begin();
return;
}
m_map[key]->value = value;
MoveToHead(key);
}
private:
unordered_map<int, list<CacheEntry>::iterator> m_map;
list<CacheEntry> m_LRU_cache;
int m_capacity;
void MoveToHead(int key)
{
//Move key from current location to head
auto updateEntry = *m_map[key];
m_LRU_cache.erase(m_map[key]);
m_LRU_cache.push_front(updateEntry);
m_map[key] = m_LRU_cache.begin();
}
};