-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path146_LRU_Cache.cpp
More file actions
71 lines (65 loc) · 2.03 KB
/
146_LRU_Cache.cpp
File metadata and controls
71 lines (65 loc) · 2.03 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include<iostream>
#include<vector>
#include<string>
#include <list>
#include <unordered_map>
using namespace std;
static const auto x=[](){
std::ios::sync_with_stdio(false);
std:cin.tie(nullptr);
return nullptr;
}();
typedef std::pair<int,int> cache_item_type;
typedef std::list<cache_item_type> cache_type;
typedef std::unordered_map <int,cache_type::iterator> cache_map_type;
class LRUCache
{
private:
int capacity;
cache_type cache;
cache_map_type cache_map;
public:
LRUCache(int capacity_):capacity(capacity_){};
int get(int key)
{
//cache不存在这个key
if(not cache_map.count(key)) return -1;
auto ele = cache_map[key];
//cache中存在这个key,根据key得到对应的链表节点,插入到front
cache.splice(cache.begin(), cache, ele);
return (*ele).second;
}
void put(int key, int value)
{
if(cache_map.count(key))
{//cache中已经存在这个key了,更新value,然后promote对应的链表节点
auto ele = cache_map[key];
(*ele).second=value;
cache.splice(cache.begin(), cache, ele);
return;
}
if( cache.size() == capacity)
{//cache已满,把最后一个删掉,既要从map中删,也要从list中删
auto last = cache.back();
int oldkey = last.first;
cache_map.erase(oldkey);
cache.pop_back();
}
//在front创建一个key,value,map保存key和对应的iterator
cache.emplace_front(key,value);
cache_map[key] = cache.begin();
}
};
int main(int argc, char const *argv[])
{
LRUCache cache ( 2/* capacity */ );
std::cout<<std::endl<<cache.get(1); // returns 1
cache.put(3, 3); // evicts key 2
std::cout<<std::endl<<cache.get(2); // returns -1 (not found)
cache.put(4, 4); // evicts key 1
cache.put(3, 4); // evicts key 1
std::cout<<std::endl<<cache.get(1); // returns -1 (not found)
std::cout<<std::endl<<cache.get(3); // returns 4
std::cout<<std::endl<<cache.get(4); // returns 4
return 0;
}