-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLRUCache.cpp
160 lines (144 loc) · 4.13 KB
/
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
#include "LRUCache.h"
#include <iostream>
#include <map>
#include <algorithm>
LRU_Cache::LRU_Cache(int size) : m_capacity(size), pHead(nullptr), pTail(nullptr) {}
LRU_Cache::~LRU_Cache() {
std::map<std::string, ListNode*>::iterator it = mp.begin();
for (; it != mp.end();)
{
delete it->second;
it->second = NULL;
mp.erase(it++);
}
delete pHead;
pHead == NULL;
delete pTail;
pTail == NULL;
}
void LRU_Cache::Remove(ListNode* pNode) {
// 如果是头节点
if (pNode->Pre == NULL)
{
pHead = pNode->Next;
pHead->Pre = NULL;
}
// 如果是尾节点
if (pNode->Next == NULL)
{
pTail = pNode->Pre;
pTail->Next = NULL;
}
else
{
pNode->Pre->Next = pNode->Next;
pNode->Next->Pre = pNode->Pre;
}
}
void LRU_Cache::SetHead(ListNode* pNode)
{
pNode->Next = pHead;
pNode->Pre = NULL;
if (pHead == NULL)
{
pHead = pNode;
}
else
{
pHead->Pre = pNode;
pHead = pNode;
}
if (pTail == NULL)
{
pTail = pHead;
}
}
void LRU_Cache::Insert(std::string key, int value)
{
std::map<std::string, ListNode*>::iterator it = mp.find(key);//如果新key等于旧key,则更新value
if (it != mp.end())
{
ListNode* Node = it->second;
Node->l_value = value;
//Remove(Node);
SetHead(Node);
}
else
{
ListNode* NewNode = new ListNode(key, value);
if (mp.size() >= m_capacity)
{
std::cout << std::endl << "[Cache] " << "警告:已经超出最大容量:" << m_capacity << "个数据" << std::endl;
std::string x = pTail->l_key;
std::map<std::string, ListNode*>::iterator it = mp.find(x);
Remove(pTail); //移除表尾指针内存
int y = it->second->l_value;
delete it->second;//删除value
std::cout << "[Cache] " << "为您移除key为" << x << ",value为" << y << "的节点" << std::endl << std::endl;
mp.erase(it);
}
SetHead(NewNode);//放到头部
mp[key] = NewNode;
}
}
int LRU_Cache::Get(std::string key)//调用get就会更新数据,转移至开头
{
std::map<std::string, ListNode*>::iterator it = mp.find(key);
if (it != mp.end())
{
ListNode* Node = it->second;
//Remove(Node);
//SetHead(Node);
return Node->l_value;
}
else
{
return -1; //有待商讨
}
}
int LRU_Cache::GetSize()
{
return mp.size();
}
void LRU_Cache::show() {
int i = 0;
int n = mp.size();
if (n == 0) std::cout << "empty task" << std::endl;
else {
std::map<std::string, ListNode*>::iterator it = mp.begin();
std::cout << "[Cache] " << "当前一共有" << n << "个数据: " << std::endl;
for (; it != mp.end(); ++it)
{
std::cout << "key值为:" << it->first << ",value值为: " << it->second->l_value << std::endl;
i++;
}
std::cout << std::endl;
}
}
void LRU_Cache::reset_size(int size_num)
{
if (size_num > m_capacity) {
std::cout << "[Cache] " << "执行扩容操作:" << std::endl;
m_capacity = size_num;
std::cout << "[Cache] " << "容量大小变为" << m_capacity << std::endl;
}
else if (size_num < m_capacity)
{
int n = m_capacity - size_num;
while (n > 0)
{
std::string x = pTail->l_key;
std::map<std::string, ListNode*>::iterator it = mp.find(x);
Remove(pTail); //移除表尾指针内存
int y = it->second->l_value;
delete it->second;//删除value
m_capacity--;
std::cout << std::endl << "为您移除key为" << x << ",value为" << y << "的节点" << "并缩容到" << m_capacity << std::endl;
mp.erase(it);
n--;
}
}
else {
std::cout << "容量大小未发生变化" << std::endl;
}
}