-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path146.lru缓存机制.cpp
More file actions
189 lines (166 loc) · 3.76 KB
/
146.lru缓存机制.cpp
File metadata and controls
189 lines (166 loc) · 3.76 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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
* @lc app=leetcode.cn id=146 lang=cpp
*
* [146] LRU缓存机制
*/
// @lc code=start
// 时间复杂度较高仍有待优化。
class List
{
private:
typedef struct Node
{
int key;
struct Node* next;
}Node;
Node* head; // 置换队列。最近使用的放在后面。
Node* tail; // 每次使用后直接置换到后面去。
public:
void Updata(int key);
void Add(int key);
int Del();
void Cout();
List();
~List();
};
void List::Cout()
{
cout << "node key:";
Node* move = head;
while (move)
{
cout << move->key << ":";
move = move->next;
}
if (tail)
{
cout << "tail:" << tail->key;
}
cout << endl;
}
void List::Updata(int key) {
Node* lastmove = head;
Node* move = head;
while (move->key != key) // 外层判断保证了一定能找到对应 key。
{
lastmove = move;
move = move->next;
}
if (move == head || move == tail) // 两端
{
// move == head && move == tail 两端重合 move != head && move == tail
// 只要 move 在尾部就无须操作。
if (move == tail)
{
return;
}
else if(move == head) // 头非尾。
{
head = head->next;
move->next = nullptr;
tail->next = move;
tail = tail->next;
}
}
else // 在中间时。
{
lastmove->next = move->next;
tail->next = move;
move->next = nullptr;
tail = tail->next;
}
}
void List::Add(int key) {
if (tail == nullptr || head == nullptr) // 头结点为 nullptr 则尾节点必为 nullptr。
{
Node* node = new Node{ key, nullptr };
head = node;
tail = head;
}
else
{
Node* node = new Node{ key, nullptr };
tail->next = node;
tail = tail->next;
}
}
int List::Del() {
int key = head->key;
Node* move = head;
head = head->next;
if (head == nullptr)
{
tail = nullptr;
}
delete move;
return key;
}
List::List()
{
head = nullptr;
tail = nullptr;
}
List::~List()
{
while (head)
{
Node* buffer = head;
head = head->next;
delete buffer;
}
}
class LRUCache {
public:
map<int, int> Cache; // 缓存。
int CacheCapacity; // Cache 容量
List list; // 最近最少使用队列。
void Cout() {
cout << "map:";
for (auto iter = Cache.begin(); iter != Cache.end(); iter++)
{
cout << iter->first << ":";
}
cout << endl;
}
LRUCache(int capacity) {
CacheCapacity = capacity; // 缓存容量。
}
int get(int key) {
auto iter = Cache.find(key);
if (iter != Cache.end()) {
list.Updata(key); // 队列更新。
return iter->second;
}
else {
return -1;
}
}
void put(int key, int value) {
auto iter = Cache.find(key);
if (iter != Cache.end())
{
list.Updata(key); // 队列更新。
iter->second = value;
}
else
{
if (Cache.size() + 1 > CacheCapacity)
{
int delkey = list.Del(); // 节点删除。
if (!Cache.empty()) {
auto CounterMin = Cache.find(delkey);
Cache.erase(CounterMin);
}
}
list.Add(key); // 节点添加。
Cache.insert(map<int, int>::value_type(key, 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);
*/
// @lc code=end