Repository navigation
Expand file tree
/
Copy pathlru_cache.cpp
More file actions
105 lines (91 loc) · 2.58 KB
/
Copy pathlru_cache.cpp
File metadata and controls
105 lines (91 loc) · 2.58 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
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
class node{
public:
int key, val;
node* next, *prev;
node(int key, int val){
this->key = key;
this->val = val;
this->next = nullptr;
this->prev = nullptr;
}
};
class LRUCache {
public:
node *head, *tail;
unordered_map<int, node*> map;
int capacity;
LRUCache(int capacity) {
this->capacity = capacity;
this->head = nullptr;
this->tail = nullptr;
}
node* deleteNode(node* curr){
node *prev_node = nullptr, *next_node = nullptr;
if(curr->prev != nullptr)
prev_node = curr->prev;
if(curr->next != nullptr)
next_node = curr->next;
curr->next = nullptr;
curr->prev = nullptr;
if(prev_node != nullptr)
prev_node->next = next_node;
else
this->head = next_node;
if(next_node != nullptr)
next_node->prev = prev_node;
else
this->tail = prev_node;
return curr;
}
void updateHead(node* curr){
if(this->head == nullptr){
this->head = curr;
this->tail = curr;
}
else{
node* temp = this->head;
this->head = curr;
curr->next = temp;
if(temp)
temp->prev = this->head;
}
}
int get(int key) {
if(this->map.find(key) == this->map.end()){
return -1;
}
else{
node* delete_node = this->deleteNode(this->map[key]);
this->updateHead(delete_node);
return delete_node->val;
}
}
void put(int key, int val) {
if(this->map.size() < this->capacity){
if(map.find(key) == this->map.end()){
node* new_node = new node(key, val);
map[key] = new_node;
this->updateHead(new_node);
}
else{
node* deleted_node = this->deleteNode(this->map[key]);
deleted_node->val = val;
this->updateHead(deleted_node);
}
}
else{
if(this->map.find(key) == this->map.end()){
node* deleted_node = this->deleteNode(this->tail);
this->map.erase(deleted_node->key);
node* new_node = new node(key, val);
map[key] = new_node;
updateHead(new_node);
}
else{
node* deleted_node = this->deleteNode(this->map[key]);
deleted_node->val = val;
updateHead(deleted_node);
}
}
}
};