forked from lennylxx/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
146.c
284 lines (234 loc) · 6.1 KB
/
146.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#include <stdio.h>
#include <stdlib.h>
/**
Run time: 28 ms.
I use a "double" double-linked list.
A double-linked list to store key value pair, and other two pointers are
used to implement "Separate chaining" method of hash table.
Time of insertion and deletion of node are both O(1).
It's like this one, but all the links between two nodes are two-ways.
http://www.geeksforgeeks.org/a-linked-list-with-next-and-arbit-pointer/
*/
struct node_t {
int key;
int value;
struct node_t *prev;
struct node_t *next;
struct node_t *hash_prev;
struct node_t *hash_next;
};
struct lru_t {
int size;
int capacity;
struct node_t *head;
struct node_t *tail;
struct node_t **hash_table;
} m_lru;
int hash(int key) {
int index = key % m_lru.capacity;
return index > 0 ? index : (-index);
}
struct node_t* findAndAdjust(int key) {
int index = hash(key);
struct node_t *p = m_lru.hash_table[index];
while (p) {
if (p->key == key) {
break;
}
p = p->hash_next;
}
/* found, take it and insert to the head of double-linked list */
if (p != NULL) {
if (m_lru.head != p) { /* p is not head */
/* take it*/
p->prev->next = p->next;
if (p->next != NULL) { /* it's not tail either */
p->next->prev = p->prev;
}
if (m_lru.tail == p) { /* if p is tail */
m_lru.tail = p->prev;
m_lru.tail->next = NULL;
if (m_lru.tail->prev == NULL) {
m_lru.tail->prev = p;
}
}
/* make it becomes the new head */
p->prev = NULL;
p->next = m_lru.head;
m_lru.head->prev = p;
m_lru.head = p;
}
}
return p;
}
void lruCacheInit(int capacity) {
m_lru.size = 0;
m_lru.head = m_lru.tail = NULL;
if (capacity <= 0) {
m_lru.capacity = 0;
m_lru.hash_table = NULL;
}
else {
m_lru.capacity = capacity;
m_lru.hash_table =
(struct node_t **)calloc(capacity, sizeof(struct node_t *));
}
}
void lruCacheFree() {
m_lru.size = 0;
m_lru.capacity = 0;
struct node_t *p = NULL;
while (m_lru.tail) {
p = m_lru.tail;
m_lru.tail = p->prev;
if (m_lru.tail == NULL) {
break;
}
else {
m_lru.tail->next = NULL;
}
free(p);
}
free(m_lru.hash_table);
m_lru.head = m_lru.tail = NULL;
m_lru.hash_table = NULL;
}
int lruCacheGet(int key) {
if (m_lru.capacity == 0 || m_lru.hash_table == NULL) return -1;
struct node_t *p = findAndAdjust(key);
if (p != NULL) {
return p->value;
}
else {
return -1;
}
}
void lruCacheSet(int key, int value) {
if (m_lru.capacity == 0 || m_lru.hash_table == NULL) return;
/* check if we already have the value in cache */
struct node_t *p = findAndAdjust(key);
if (p != NULL) {
p->value = value; /* update value */
}
else {
if (m_lru.size == m_lru.capacity) { /* already reaches the capacity */
if (m_lru.tail != NULL) { /* remove the tail */
struct node_t *t = m_lru.tail;
m_lru.tail = m_lru.tail->prev;
if (m_lru.tail != NULL) {
m_lru.tail->next = NULL;
}
/* remove old tail's corresponding hash links */
if (t->hash_prev != NULL) {
t->hash_prev->hash_next = t->hash_next;
if (t->hash_next != NULL) {
t->hash_next->hash_prev = t->hash_prev;
}
}
else {
if (t->hash_next != NULL) {
t->hash_next->hash_prev = NULL;
}
int idx = hash(t->key);
m_lru.hash_table[idx] = t->hash_next;
}
free(t);
m_lru.size--;
}
}
struct node_t *new_node =
(struct node_t *)malloc(sizeof(struct node_t));
new_node->key = key;
new_node->value = value;
new_node->prev = NULL;
new_node->next = m_lru.head;
new_node->hash_prev = NULL;
new_node->hash_next = NULL;
int index = hash(key);
if (m_lru.size == 0) { /* the list is empty now */
m_lru.tail = new_node;
}
else {
m_lru.head->prev = new_node;
}
m_lru.head = new_node;
m_lru.size++;
if (m_lru.hash_table[index]) {
new_node->hash_next = m_lru.hash_table[index];
m_lru.hash_table[index]->hash_prev = new_node;
}
m_lru.hash_table[index] = new_node;
}
}
void print() {
struct node_t *p = m_lru.head;
while (p) {
printf("(%d, %d) ", p->key, p->value);
p = p->next;
}
printf("\n");
}
int main() {
printf("first test\n");
lruCacheInit(2);
lruCacheSet(2, 1);
print();
lruCacheSet(1, 1);
print();
lruCacheGet(2);
print();
lruCacheSet(4, 1);
print();
lruCacheGet(1);
print();
lruCacheGet(2);
print();
printf("second test\n");
lruCacheInit(2);
lruCacheSet(2, 1);
print();
lruCacheSet(1, 1);
print();
lruCacheSet(2, 3);
print();
lruCacheSet(4, 1);
print();
lruCacheGet(1);
print();
lruCacheGet(2);
print();
lruCacheFree();
printf("third test\n");
lruCacheInit(3);
lruCacheSet(1, 1);
print();
lruCacheSet(2, 2);
print();
lruCacheSet(3, 3);
print();
lruCacheSet(4, 4);
print();
lruCacheGet(4);
print();
lruCacheGet(3);
print();
lruCacheGet(2);
print();
lruCacheGet(1);
print();
lruCacheSet(5, 5);
print();
lruCacheGet(1);
print();
lruCacheGet(2);
print();
lruCacheGet(3);
print();
lruCacheGet(4);
print();
lruCacheGet(5);
print();
lruCacheFree();
printf("all tests passed!\n");
return 0;
}