-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLRU_Cache.cpp
More file actions
56 lines (55 loc) · 1.2 KB
/
LRU_Cache.cpp
File metadata and controls
56 lines (55 loc) · 1.2 KB
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
class Entry
{
public:
int key;
int value;
Entry(int key, int value) :key(key), value(value){};
};
class LRUCache
{
public:
int capacity, currentSize;
unordered_map < int, list<Entry*>::iterator> hash;
list<Entry*> lru; // front is the lastest
public:
LRUCache(int capacity) :capacity(capacity), currentSize(0)
{
}
int get(int key)
{
Entry *entry = nullptr;
auto it = hash.find(key);
if (it == hash.end())
return -1;
else
{
//让it指向的元素移动到lru链表的首部
lru.splice(lru.begin(), lru, it->second);
return (*(it->second))->value;
}
}
void set(int key, int value)
{
Entry *entry = nullptr;
auto it = hash.find(key);
if (it == hash.end()) //不存在key
{
entry = new Entry(key, value);
if (currentSize >= capacity) //空间用尽,删除首部元素
{
hash.erase(lru.back()->key);
delete lru.back();
lru.pop_back();
}
else
currentSize++;
lru.push_front(entry);
hash[key] = lru.begin();
}
else
{
(*(it->second))->value = value;
lru.splice(lru.begin(), lru, it->second); //移动到首部
}
}
};