-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
0146-lru-cache.c
147 lines (112 loc) · 3.43 KB
/
0146-lru-cache.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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
typedef struct Node {
int key;
int value;
struct Node* prev;
struct Node* next;
UT_hash_handle hh; /* makes this structure hashable */
} Node;
typedef struct {
int cap;
Node* leftMost; // LRU
Node* rightMost; // MRU
} LRUCache;
Node* hashedNodes = NULL;
void removeNode(Node* node)
{
node->prev->next = node->next;
node->next->prev = node->prev;
}
// Insert node at the right (MRU), just before the rightMost node
void insertNode(Node* node, LRUCache* cache)
{
Node* prev = cache->rightMost->prev;
Node* next = cache->rightMost;
prev->next = node;
next->prev = node;
node->next = next;
node->prev = prev;
}
LRUCache* lRUCacheCreate(int capacity) {
// Allocate space for LRUCache
LRUCache* cache = (LRUCache*)malloc(sizeof(LRUCache));
// Set the capacity
cache->cap = capacity;
// Allocate space for the leftMost and rightMost nodes
cache->leftMost = (Node*)malloc(sizeof(Node));
cache->rightMost = (Node*)malloc(sizeof(Node));
// Initialize the nodes where we connect the leftMost and rightMost to form the list
cache->leftMost->prev = NULL;
cache->leftMost->next = cache->rightMost;
cache->rightMost->prev = cache->leftMost;
cache->rightMost->next = NULL;
return cache;
}
int lRUCacheGet(LRUCache* obj, int key) {
Node* node;
HASH_FIND_INT(hashedNodes, &key, node);
// Check if the key exists
if(node)
{
// Remove the node from its current position in the list
removeNode(node);
// Insert the node at the right (MRU)
insertNode(node, obj);
return node->value;
}
return -1;
}
void lRUCachePut(LRUCache* obj, int key, int value) {
Node* node;
HASH_FIND_INT(hashedNodes, &key, node);
// Check if the key exists
if(node)
{
// Remove the node from its current position in the list
removeNode(node);
// Update the node's current value
node->value = value;
}
else
{
// Allocate space for a new node
node = (Node*)malloc(sizeof(Node));
// Initialize the node
node->key = key;
node->value = value;
// Add the new node to the hash
HASH_ADD_INT(hashedNodes, key, node);
}
// Insert the node at the right (MRU)
insertNode(node, obj);
// Check if we have exceeded the capacity after adding the new node
if(HASH_COUNT(hashedNodes) > obj->cap)
{
Node* LRUNode = obj->leftMost->next;
// Remove the left node (LRU) from the list
removeNode(LRUNode);
// Remove the node from the hash
HASH_DEL(hashedNodes, LRUNode);
// Deallocate the node
free(LRUNode);
}
}
void lRUCacheFree(LRUCache* obj) {
// Deallocate the nodes first
free(obj->leftMost);
free(obj->rightMost);
// Deallocate the LRUCache
free(obj);
// Delete and deallocate all the nodes from the hash
Node *currentNode, *tmp;
HASH_ITER(hh, hashedNodes, currentNode, tmp) {
HASH_DEL(hashedNodes, currentNode); /* delete; nodes advances to next */
free(currentNode);
}
}
/**
* Your LRUCache struct will be instantiated and called as such:
* LRUCache* obj = lRUCacheCreate(capacity);
* int param_1 = lRUCacheGet(obj, key);
* lRUCachePut(obj, key, value);
* lRUCacheFree(obj);
*/