-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLRU.cpp
More file actions
91 lines (74 loc) · 1.2 KB
/
Copy pathLRU.cpp
File metadata and controls
91 lines (74 loc) · 1.2 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
#include<iostream>
using namespace std;
struct Node{
int key;
int value;
public : Node(int key, int value, int index):key(key), value(value){}
};
class LRUCache{
public:
LRUCache(int capacity){
length = capacity;
pointer = 0;
queue = new int[length];
node = new Node[length];
}
int get(int key) {
int index = find_key( key );
if( index == -1 )
return -1;
return node[index].value;
}
void set(int key, int value) {
int index = find_key( key );
if( key != -1 )
{
node[index].value = value;
return;
}
}
void ~LRUCache()
{
delete[] node;
delete[] queue;
}
private:
int find_key( int key )
{
int cur_length = pointer > length ? length : 0;
int start = 0;
while( start < cur_length )
{
if( key == node[start].key )
return start;
if( key < node[start].key )
start = 2 * start + 1;
else
start = 2 * start + 2;
}
return -1;
}
private:
int length, pointer;
Node *node;
int *queue;
};
class Node
{
public:
Node(int value) : value(value)
{
}
int get_value()
{
return value;
}
private:
int value;
};
int main()
{
Node n(3);
cout << n.get_value() << endl;
return 0;
}