-
Notifications
You must be signed in to change notification settings - Fork 5
/
LC_146_LRUCache.cpp
166 lines (138 loc) · 4.11 KB
/
LC_146_LRUCache.cpp
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
/*
https://leetcode.com/problems/lru-cache/
146. LRU Cache
https://practice.geeksforgeeks.org/problems/lru-cache/1
*/
class DLinkedNode{
public:
int key, val;
DLinkedNode *prev, *next;
DLinkedNode() : key(0), val(0), prev(nullptr), next(nullptr){}
DLinkedNode(int k, int v) : key(k), val(v), prev(nullptr), next(nullptr){}
};
class LRUCache {
public:
unordered_map<int, DLinkedNode*> cache;
DLinkedNode* head, *tail;// dummy node to handle edges cases
int cap, size=0;
LRUCache(int capacity): cap(capacity){
// cap = capacity;
head = new DLinkedNode();
tail = new DLinkedNode();
head->next = tail;
tail->prev = head;
}
int get(int key) {
if(cache.find(key) == cache.end())
return -1;
DLinkedNode* node = cache[key];
moveToFront(node);
return node->val;
}
void put(int key, int value) {
if(cache.find(key) != cache.end())
{
DLinkedNode* node = cache[key];
node->val = value;
moveToFront(node);
return;
}
if(size == cap)
{
DLinkedNode* deleteNode = tail->prev;
removeNode(deleteNode); // change pointers
cache.erase(deleteNode->key);
delete deleteNode;
}
else
size++;
DLinkedNode* newnode = new DLinkedNode(key, value);
cache[key] = newnode;
addToHead(newnode);
// cout<<newnode->key<<" "<<newnode->val<<endl;
}//
void addToHead (DLinkedNode* node)
{
node->prev = head;
node->next = head->next;
head->next->prev = node;
head->next = node;
}
void moveToFront(DLinkedNode* node)
{
removeNode(node);
addToHead(node);
}
void removeNode(DLinkedNode* node)
{
node->prev->next = node->next;
node->next->prev = node->prev;
}
};
/*
class LRUCache {
public:
list<pair<int,int>> cache;
unordered_map<int, list<pair<int,int>>::iterator> cacheMap;
int capacity_;
LRUCache(int capacity) : capacity_(capacity){
// capacity_ = capacity;
}
// void moveToFront(list<pair<int,int>>::iterator it)
// {
// cache.push_front({it->first, it->second});
// cache.erase(it);
// }//
int get(int key)
{
if(cacheMap.find(key) == cacheMap.end())
return -1;
cache.splice(cache.begin(), cache, cacheMap[key]); // transfer to front
return cacheMap[key]->second; // return its value
// moveToFront(cacheMap[key]); // move to front
// cacheMap[key] = cache.begin(); // update its address
// return cacheMap[key]->second; // return its value
}//
void put(int key, int value)
{
if(cacheMap.find(key) != cacheMap.end())
{
cache.splice(cache.begin(), cache, cacheMap[key]);
cacheMap[key]->second = value;
return;
}
if(cache.size() == capacity_)
{
cacheMap.erase(cache.back().first);
cache.pop_back();
}
cache.push_front({key, value});
cacheMap[key] = cache.begin();
// if(cacheMap.count(key)) // key doesn't exist
// {
// if(cache.size()<capacity_)
// {
// cache.push_front({key,value});
// cacheMap[key] = cache.begin();
// }
// else
// {
// cacheMap.erase(cache.back().first);
// cache.pop_back();
// cache.push_front({key, value});
// cacheMap[key] = cache.begin();
// }
// }
// else //key exist and move to front
// {
// cache.begin()->second = value;
// }
}//
};
*/
/**
* Your LRUCache object will be instantiated and called as such:
* LRUCache* obj = new LRUCache(capacity);
* int param_1 = obj->get(key);
* obj->put(key,value);
*/