Skip to content

Commit caa387f

Browse files
author
xuyuanhui
committed
LRU and New using usage
Signed-off-by: xuyuanhui <xuyuanhui@huayun.com>
1 parent 35ef911 commit caa387f

File tree

2 files changed

+220
-0
lines changed

2 files changed

+220
-0
lines changed

Language/C_plus/LRU/LruCache.cpp

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
#include <iostream>
2+
#include <map>
3+
4+
using namespace std;
5+
6+
class Node {
7+
public:
8+
int key, value;
9+
Node *prev, *next;
10+
Node(int k, int v): key(k), value(v), prev(NULL), next(NULL) {}
11+
};
12+
13+
class DoublyLinkedList {
14+
Node *front, *rear;
15+
16+
bool isEmpty() {
17+
return rear == NULL;
18+
}
19+
20+
public:
21+
DoublyLinkedList(): front(NULL), rear(NULL) {}
22+
23+
Node* add_page_to_head(int key, int value) {
24+
Node *page = new Node(key, value);
25+
if(!front && !rear) {
26+
front = rear = page;
27+
}
28+
else {
29+
page->next = front;
30+
front->prev = page;
31+
front = page;
32+
}
33+
return page;
34+
}
35+
36+
void move_page_to_head(Node *page) {
37+
if(page==front) {
38+
return;
39+
}
40+
if(page == rear) {
41+
rear = rear->prev;
42+
rear->next = NULL;
43+
}
44+
else {
45+
page->prev->next = page->next;
46+
page->next->prev = page->prev;
47+
}
48+
49+
page->next = front;
50+
page->prev = NULL;
51+
front->prev = page;
52+
front = page;
53+
}
54+
55+
void remove_rear_page() {
56+
if(isEmpty()) {
57+
return;
58+
}
59+
if(front == rear) {
60+
delete rear;
61+
front = rear = NULL;
62+
}
63+
else {
64+
Node *temp = rear;
65+
rear = rear->prev;
66+
rear->next = NULL;
67+
delete temp;
68+
}
69+
}
70+
Node* get_rear_page() {
71+
return rear;
72+
}
73+
74+
};
75+
76+
class LRUCache{
77+
int capacity, size;
78+
DoublyLinkedList *pageList;
79+
map<int, Node*> pageMap;
80+
81+
public:
82+
LRUCache(int capacity) {
83+
this->capacity = capacity;
84+
size = 0;
85+
pageList = new DoublyLinkedList();
86+
pageMap = map<int, Node*>();
87+
}
88+
89+
int get(int key) {
90+
if(pageMap.find(key)==pageMap.end()) {
91+
return -1;
92+
}
93+
int val = pageMap[key]->value;
94+
95+
// move the page to front
96+
pageList->move_page_to_head(pageMap[key]);
97+
return val;
98+
}
99+
100+
void put(int key, int value) {
101+
if(pageMap.find(key)!=pageMap.end()) {
102+
// if key already present, update value and move page to head
103+
pageMap[key]->value = value;
104+
pageList->move_page_to_head(pageMap[key]);
105+
return;
106+
}
107+
108+
if(size == capacity) {
109+
// remove rear page
110+
int k = pageList->get_rear_page()->key;
111+
pageMap.erase(k);
112+
pageList->remove_rear_page();
113+
size--;
114+
}
115+
116+
// add new page to head to Queue
117+
Node *page = pageList->add_page_to_head(key, value);
118+
size++;
119+
pageMap[key] = page;
120+
}
121+
122+
~LRUCache() {
123+
map<int, Node*>::iterator i1;
124+
for(i1=pageMap.begin();i1!=pageMap.end();i1++) {
125+
delete i1->second;
126+
}
127+
delete pageList;
128+
}
129+
};
130+
131+
int main() {
132+
LRUCache cache(2); // cache capacity 2
133+
cache.put(2,2);
134+
cout << cache.get(2) << endl;
135+
cout << cache.get(1) << endl;
136+
cache.put(1,1);
137+
cache.put(1,5);
138+
cout << cache.get(1) << endl;
139+
cout << cache.get(2) << endl;
140+
cache.put(8,8);
141+
cout << cache.get(1) << endl;
142+
cout << cache.get(8) << endl;
143+
}
144+
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <map>
4+
using namespace std;
5+
6+
class Base
7+
{
8+
public:
9+
void menfcn()
10+
{
11+
cout << "Base function" << endl;
12+
}
13+
void menfcn(int n)
14+
{
15+
cout << "Base function with int" << endl;
16+
}
17+
};
18+
19+
class Derived : public Base
20+
{
21+
public:
22+
using Base::menfcn;
23+
int menfcn(int num)
24+
{
25+
cout << "Derived functin with int: " << num << endl;
26+
return num;
27+
}
28+
29+
};
30+
31+
using IntVec = std::vector<int>;
32+
33+
void testUsing()
34+
{
35+
IntVec vec = {1, 2, 3, 4, 5};
36+
vec.push_back(123);
37+
for (int num : vec)
38+
cout << "vec num " << num << endl;
39+
cout << is_same < std::vector<int>, IntVec>::value << endl;
40+
}
41+
42+
template <typename T>
43+
using MapStr = std::map<T, std::string>;
44+
void testUsing2()
45+
{
46+
MapStr<int> intStrMap;
47+
intStrMap.insert(make_pair(123, "aaa"));
48+
intStrMap.insert(make_pair(456, "bbb"));
49+
MapStr<int>::iterator iter;
50+
51+
for (iter = intStrMap.begin(); iter != intStrMap.end(); iter++)
52+
cout << iter->first << " " << iter->second << endl;
53+
54+
MapStr<std::string> strStrMap;
55+
strStrMap.insert(make_pair("ccc", "ddd"));
56+
strStrMap.insert(make_pair("eee", "fff"));
57+
MapStr<std::string>::iterator striter;
58+
59+
for (striter = strStrMap.begin(); striter != strStrMap.end(); striter++)
60+
cout << striter->first << " " << striter->second << endl;
61+
}
62+
63+
int main()
64+
{
65+
Base b;
66+
Derived d;
67+
b.menfcn();
68+
d.menfcn();
69+
d.menfcn(123);
70+
71+
testUsing();
72+
testUsing2();
73+
74+
return 0;
75+
}
76+

0 commit comments

Comments
 (0)