-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLRU.c
More file actions
122 lines (104 loc) · 3.06 KB
/
LRU.c
File metadata and controls
122 lines (104 loc) · 3.06 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
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
#include "LRU.h"
struct LRUCache* createLRUCache(int size) {
struct LRUCache* cache = (struct LRUCache*)malloc(sizeof(struct LRUCache));
if (cache == NULL) {
perror("Memory allocation for LRU failed");
exit(EXIT_FAILURE);
}
cache->head = NULL;
cache->tail = NULL;
cache->size = size;
return cache;
}
int countNodes(LRUCache* cache) {
int count = 0;
struct Node* node = cache->head;
while (node != NULL) {
count++;
node = node->next;
}
return count;
}
void insertLRU(LRUCache* cache, char* path, int value) {
// Check if the path is already in the cache
struct Node* node = cache->head;
while (node != NULL) {
if (strcmp(node->path, path) == 0) {
// Move the existing node to the front of the queue
if(node->value != value)
node->value = value;
if (node->prev != NULL) {
node->prev->next = node->next;
} else {
cache->head = node->next;
}
if (node->next != NULL) {
node->next->prev = node->prev;
} else {
cache->tail = node->prev;
}
node->prev = NULL;
node->next = cache->head;
if (cache->head != NULL) {
cache->head->prev = node;
}
cache->head = node;
return;
}
node = node->next;
}
// Create a new node
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) {
perror("Memory allocation for LRU failed");
exit(EXIT_FAILURE);
}
// Set node values
newNode->path = (char*) malloc(1000);
strcpy(newNode->path, path);
newNode->value = value;
// Insert the new node at the front of the queue
newNode->prev = NULL;
newNode->next = cache->head;
if (cache->head != NULL) {
cache->head->prev = newNode;
}
cache->head = newNode;
// Update the tail if the queue was empty
if (cache->tail == NULL) {
cache->tail = newNode;
}
// Check if the cache size exceeds the specified size
if (cache->size > 0 && cache->size < countNodes(cache)) {
// Remove the least recently used node from the end of the queue
struct Node* lastNode = cache->tail;
if (lastNode != NULL) {
if (lastNode->prev != NULL) {
lastNode->prev->next = NULL;
} else {
cache->head = NULL;
}
cache->tail = lastNode->prev;
free(lastNode);
}
}
}
int find(LRUCache* cache, char* path) {
struct Node* node = cache->head;
while (node != NULL) {
if (strcmp(node->path, path) == 0) {
return node->value;
}
node = node->next;
}
return -1;
}
void destroyLRUCache(LRUCache* cache) {
struct Node* current = cache->head;
while (current != NULL) {
struct Node* temp = current;
current = current->next;
free(temp);
}
free(cache);
}