-
Notifications
You must be signed in to change notification settings - Fork 1
/
LRU.c
65 lines (60 loc) · 1.28 KB
/
LRU.c
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
using namespace std;
// linked list to store the order of elements
//nsert to head and delete the least recently used from head->prev;
class Cache{
private:
int size;
map<key, Node>
int max_size;
LRUCache* head, *tail;
// called when entry is present in the cache and rearrange the linked list
// to bring the entry to the front
removeAndAttach(Node* n)
{ // removing the node from existing place
n->prev->next = n ->next;
n->next->prev = n->prev
//attaching to front
n->next = head->next;
n->prev = head;
head-> next = n;
n->next->prev = n;
}
insertFront(Node*n){
n->next = head->next;
head->next = n;
}
public :
Cache(int max_size)
{
Node* head = new Node();
Node* tail = new Node();
tail -> next = head;
}
}
struct LRUCache{
struct LRUCache* next;
struct LRUCache* prev;
int Key,Value;
}
typedef struct LRUCache Node
//hashmap for find operations
void insert(int key, int value){
size++;
Node *n = map.find(key);
if ( n )
{
removeAndAttach(n);
}
if (size > max)
{
removeTail();
Node* n = createNode(key, value);
map.insert(key, n);
}
else
{
Node* n = createNode(key, value);
insertFront(n);
map.insert(key, n);
}
}