Skip to content

Commit 3c9726b

Browse files
committed
Merge pull request xtaci#16 from wyh267/master
modify LRU
2 parents 4946788 + de365ad commit 3c9726b

File tree

2 files changed

+123
-101
lines changed

2 files changed

+123
-101
lines changed

include/LRU_cache.h

Lines changed: 96 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -36,30 +36,37 @@
3636
#include <stdio.h>
3737
#include <stdlib.h>
3838
#include <string.h>
39+
#include <map>
40+
41+
using namespace std;
3942

4043
namespace alg {
41-
class LRUCache{
44+
45+
template<typename K,typename V>
46+
class LRUCache
47+
{
4248
typedef struct _Node_{
43-
int key;
44-
int value;
49+
K key;
50+
V value;
4551

4652
struct _Node_ *next;
4753
struct _Node_ *pre;
4854

4955
} CacheNode;
50-
51-
public:
56+
57+
public:
5258
LRUCache(int cache_size=10) {
5359
cache_size_ = cache_size;
54-
cache_real_size_ = 0;
5560
p_cache_list_head = new CacheNode();
5661
p_cache_list_near = new CacheNode();
5762
p_cache_list_head->next = p_cache_list_near;
5863
p_cache_list_head->pre = NULL;
5964
p_cache_list_near->pre = p_cache_list_head;
6065
p_cache_list_near->next = NULL;
66+
6167
}
62-
68+
69+
6370
~LRUCache() {
6471
CacheNode *p;
6572
p = p_cache_list_head->next;
@@ -69,82 +76,106 @@ namespace alg {
6976
}
7077

7178
delete p_cache_list_near;
79+
cache_hash.clear();
7280
}
73-
74-
int getValue(int key) {
75-
CacheNode *p=p_cache_list_head->next;
76-
while (p->next!=NULL) {
77-
if (p->key == key) { //catch node
78-
detachNode(p);
79-
addToFront(p);
80-
return p->value;
81-
}
82-
p=p->next;
81+
82+
83+
84+
V getValue(K key) {
85+
//put the value in front of the list if find the key
86+
if(cache_hash.find(key) != cache_hash.end()){
87+
CacheNode *p=cache_hash[key];
88+
detachNode(p);
89+
addFristNode(p);
90+
return (cache_hash[key]->value);
91+
}else{
92+
93+
cout << "[ERROR]No key with name ::" << key << endl;
94+
return V();
8395
}
84-
return -1;
85-
}
8696

87-
bool putValue(int key,int value) {
88-
CacheNode *p=p_cache_list_head->next;
89-
while (p->next!=NULL) {
90-
if(p->key == key) { //catch node
91-
p->value=value;
92-
getValue(key);
93-
return true;
94-
}
95-
p=p->next;
96-
}
97+
}
98+
99+
100+
101+
102+
bool putValue(K key,V value) {
103+
if(cache_hash.find(key) != cache_hash.end()){
104+
cache_hash[key]->value=value;
105+
detachNode((CacheNode *)cache_hash[key]);
106+
addFristNode((CacheNode *)cache_hash[key]);
107+
if(cache_hash.size()>cache_size_){
108+
delEndNode();
109+
}
110+
}else{
111+
CacheNode *p=new CacheNode();
112+
p->key=key;
113+
p->value=value;
114+
addFristNode(p);
115+
cache_hash[key]=p;
116+
if(cache_hash.size()>cache_size_){
117+
cout << "[INFO]LRU Cache is full ... Delete Last One..." << endl;
118+
delEndNode();
119+
}
120+
97121

98-
if (cache_real_size_ >= cache_size_) {
99-
std::cout << "free" <<std::endl;
100-
p=p_cache_list_near->pre->pre;
101-
delete p->next;
102-
p->next=p_cache_list_near;
103-
p_cache_list_near->pre=p;
104122
}
105-
106-
p = new CacheNode();//(CacheNode *)malloc(sizeof(CacheNode));
107-
108-
if (p==NULL)
109-
return false;
110-
111-
addToFront(p);
112-
p->key=key;
113-
p->value=value;
114-
115-
cache_real_size_++;
116-
123+
117124
return true;
118125
}
119-
120-
void displayNodes() {
126+
127+
128+
129+
130+
131+
void display(){
121132
CacheNode *p=p_cache_list_head->next;
122-
123133
while(p->next!=NULL) {
124-
std::cout << " Key : " << p->key << " Value : " << p->value << std::endl;
134+
std::cout << " KEY[" << p->key << "]<==>VALUE[" << p->value <<"]" << std::endl;
125135
p=p->next;
126-
127136
}
128-
std::cout << std::endl;
137+
std::cout << std::endl;
129138
}
130-
131-
private:
139+
140+
private:
132141
int cache_size_;
133-
int cache_real_size_;
134142
CacheNode *p_cache_list_head;
135143
CacheNode *p_cache_list_near;
136-
137-
void detachNode(CacheNode *node) {
144+
map<K,CacheNode*>cache_hash;
145+
146+
147+
void detachNode(CacheNode *node){
138148
node->pre->next=node->next;
139-
node->next->pre=node->pre;
149+
node->next->pre=node->pre;
140150
}
141-
142-
void addToFront(CacheNode *node) {
143-
node->next=p_cache_list_head->next;
144-
p_cache_list_head->next->pre=node;
145-
p_cache_list_head->next=node;
146-
node->pre=p_cache_list_head;
151+
152+
153+
void addFristNode(CacheNode *node){
154+
node->pre=p_cache_list_head;
155+
if(cache_hash.empty())
156+
{
157+
node->next=p_cache_list_near;
158+
p_cache_list_near->pre=node;
159+
p_cache_list_head->next=node;
160+
}else
161+
{
162+
node->next=p_cache_list_head->next;
163+
p_cache_list_head->next->pre=node;
164+
p_cache_list_head->next=node;
165+
}
166+
}
167+
168+
169+
170+
void delEndNode(){
171+
CacheNode *p=p_cache_list_near->pre;
172+
detachNode(p);
173+
cout << "[INFO]Delete key ::: " << p->key <<endl;
174+
cache_hash.erase(p->key);
175+
free(p);
176+
147177
}
178+
148179
};
149180
}
150181

src/LRU_cache_demo.cpp

Lines changed: 27 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,33 @@
33
#include "LRU_cache.h"
44

55
using namespace std;
6-
int main() {
7-
#if 1
8-
alg::LRUCache *cache=new alg::LRUCache(9);
9-
10-
cache->putValue(1,1);
11-
cache->putValue(2,2);
12-
cache->putValue(3,3);
13-
cache->putValue(4,3);
14-
cache->putValue(5,2);
15-
cache->displayNodes();
16-
17-
cout << cache->getValue(4) << endl;
18-
cache->displayNodes();
19-
//cache->displayNodes();
20-
cout << cache->getValue(3) << endl;
21-
cache->displayNodes();
22-
cout << cache->getValue(3) << endl;
23-
cache->displayNodes();
24-
cout << cache->getValue(1) << endl;
25-
cache->displayNodes();
26-
cout << cache->getValue(2) << endl;
27-
cache->displayNodes();
28-
cout << cache->getValue(9) << endl;
6+
using namespace alg;
297

30-
cache->displayNodes();
31-
32-
cache->putValue(4,9);
33-
//cout << cache->getValue(2) << endl;
34-
//cout << cache->getValue(3) << endl;
35-
cache->displayNodes();
36-
cout << cache->getValue(4) << endl;
37-
cache->displayNodes();
38-
cout << cache->getValue(2) << endl;
39-
cache->displayNodes();
8+
int main() {
9+
10+
11+
LRUCache<string,string> Cache(5);
12+
Cache.putValue("key1","value1");
13+
Cache.putValue("key2","value2");
14+
Cache.putValue("key3","value3");
15+
Cache.putValue("key4","value4");
16+
Cache.putValue("key5","value5");
17+
Cache.putValue("key6","value6");
18+
19+
20+
cout << "Display The LRU Cache...." << endl;
21+
Cache.display();
22+
23+
cout << "Now,Visit the LRU Cache with \"key4\"" << endl;
24+
cout << "The \"key4\" Value is : "<< Cache.getValue("key4") << endl;
25+
cout << "The New LRU Cache is ... " << endl;
26+
Cache.display();
27+
28+
cout << "Now, Update The LRU Cache with \"key5\" and new value is \"newValue5\" ... " << endl;
29+
Cache.putValue("key5","newValue5");
30+
cout << "The New LRU Cache is ... " << endl;
31+
Cache.display();
32+
Cache.getValue("aaa");
33+
4034

41-
delete cache;
42-
#endif
43-
return 1;
4435
}

0 commit comments

Comments
 (0)