-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdemo_lru_linked_list_2.c
350 lines (277 loc) · 7.22 KB
/
demo_lru_linked_list_2.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "demo_lru_linked_list_2.h"
List* create_queue() {
List *l = (List *)malloc(sizeof(List));
assert(l != NULL);
l->head = NULL;
l->tail = NULL;
l->count = 0;
return l;
}
bool enqueue(List *list, char *data) {
assert(list != NULL);
LinkQueue *p = (LinkQueue*)malloc(sizeof(LinkQueue));
assert(p != NULL);
p->key = data;
p->next = NULL;
p->prev = NULL;
if (list->head == NULL) {
list->head = p;
} else {
list->tail->next = p;
p->prev = list->tail;
}
list->tail = p;
list->count++;
return true;
}
char* dequeue(List *list) {
assert(list != NULL);
LinkQueue *tmp = list->head;
char *key = tmp->key;
if (list->head->next != NULL) {
list->head->next->prev = NULL;
list->head = list->head->next;
}
tmp->prev = NULL;
tmp->next = NULL;
list->count--;
if (list->head == NULL) {
list->tail = NULL;
}
return key;
}
char* dequeueTail(List *list) {
assert(list != NULL);
LinkQueue *tmp = list->tail;
char *key = tmp->key;
if (list->tail->prev != NULL) {
list->tail->prev->next = NULL;
list->tail = list->tail->prev;
}
tmp->next = NULL;
tmp->prev = NULL;
list->count--;
if (list->tail == NULL) {
list->head = NULL;
}
return key;
}
void destory(List *list) {
if (list == NULL || list->count == 0) return;
while (!list->count == 0) {
dequeue(list);
}
free(list);
}
int hash_init(HashTable *ht) {
assert(ht != NULL);
ht->elem_num = 0;
ht->size = HASH_TABLE_INIT_SIZE;
ht->buckets = (Bucket**)calloc(ht->size, sizeof(Bucket*));
assert(ht->buckets != NULL);
int i;
for (i = 0; i < ht->size; i++) {
ht->buckets[i] = NULL;
}
return true;
}
int hash_insert(HashTable *ht, char *key, void *value) {
assert(ht != NULL);
assert(key != NULL);
assert(value != NULL);
resize_hash_table_if_needed(ht);
int index = HASH_INDEX(ht, key);
Bucket *org_bucket = ht->buckets[index];
Bucket *tmp_bucket = org_bucket;
while (tmp_bucket) {
if (strcmp(tmp_bucket->key, key) == 0) {
tmp_bucket->value = value;
return true;
}
tmp_bucket = tmp_bucket->next;
}
Bucket *bucket = (Bucket*)malloc(sizeof(Bucket));
assert(bucket != NULL);
bucket->key = key;
bucket->value = value;
bucket->next = NULL;
if (org_bucket != NULL) {
org_bucket->next = bucket;
}
ht->buckets[index] = bucket;
ht->elem_num++;
return true;
}
int hash_lookup(HashTable *ht, char *key, void **result) {
assert(ht != NULL);
assert(key != NULL);
int index = HASH_INDEX(ht, key);
Bucket *bucket = ht->buckets[index];
if (bucket == NULL) return false;
while (bucket) {
if (strcmp(bucket->key, key) == 0) {
*result = bucket->value;
return true;
}
bucket = bucket->next;
}
return false;
}
int hash_remove(HashTable *ht, char *key) {
assert(ht != NULL);
assert(key != NULL);
int index = HASH_INDEX(ht, key);
Bucket *bucket = ht->buckets[index];
if (bucket == NULL) return false;
Bucket *prev = NULL;
while (bucket) {
if (strcmp(bucket->key, key) == 0) {
if (prev == NULL) {
ht->buckets[index] = bucket->next;
} else {
prev->next = bucket->next;
}
free(bucket);
return true;
}
prev = bucket;
bucket = bucket->next;
}
return false;
}
int hash_destory(HashTable *ht) {
assert(ht != NULL);
int i;
Bucket *cur = NULL;
Bucket *tmp = NULL;
for (i = 0; i < ht->size; i++) {
cur = ht->buckets[i];
while (cur) {
tmp = cur;
cur = cur->next;
free(tmp);
}
}
free(ht);
return true;
}
int lru_create(lru_list *lru) {
assert(lru != NULL);
lru->max_capacity = QLEN;
List *l = create_queue(l);
lru->Q = l;
HashTable *ht = (HashTable*)malloc(sizeof(HashTable));
int result = hash_init(ht);
assert(result == true);
lru->map = ht;
assert(lru->map != NULL);
return true;
}
int lru_enqueue(lru_list *lru, char *key, void *data) {
assert(lru != NULL);
if (lru->Q->count >= lru->max_capacity) {
char *tmp = dequeueTail(lru->Q);
char *find_str = NULL;
int ret = hash_lookup(lru->map, tmp, (void **)&find_str);
if (ret == true) {
ret = hash_remove(lru->map, tmp);
if (ret == false) {
printf("lru_enqueue dequeueTail fail\n");
return false;
}
} else {
printf("hash_lookup: %s failed\n", tmp);
return false;
}
}
enqueue(lru->Q, key);
hash_insert(lru->map, key, data);
return true;
}
int lru_dequeue(lru_list *lru) {
char *key = dequeue(lru->Q);
char *find_str = NULL;
int ret = hash_lookup(lru->map, key, (void **)&find_str);
if (ret == true) {
printf("key:%s, val:%s\n", key, find_str);
ret = hash_remove(lru->map, key);
if (ret == false) {
printf("lru_dequeue dequeue fail\n");
}
}
return true;
}
int lru_get(lru_list *lru, char *key) {
char *find_str = NULL;
int ret = hash_lookup(lru->map, key, (void **)&find_str);
if (ret == true) {
printf("key:%s, val:%s\n", key, find_str);
}
return true;
}
void lru_destory(lru_list *lru) {
destory(lru->Q);
hash_destory(lru->map);
free(lru);
}
void test_link_queue() {
;
List *l = create_queue();
int i;
enqueue(l, "a1");
enqueue(l, "a2");
enqueue(l, "a3");
enqueue(l, "a4");
enqueue(l, "a5");
enqueue(l, "a6");
enqueue(l, "a7");
enqueue(l, "a8");
// printf("dequeue val: %s\n", dequeueTail(&l));
for (i = 0 ; i < 8; i++) {
printf("dequeue val: %s\n", dequeue(l));
}
destory(l);
}
void test_hash_table() {
HashTable *ht = (HashTable*)malloc(sizeof(HashTable));
int result = hash_init(ht);
assert(result == true);
int int1 = 10;
int int2 = 20;
char str1[] = "Hello TIPI";
char str2[] = "Value";
int *j = NULL;
char *find_str = NULL;
hash_insert(ht, "KeyInt", &int1);
hash_insert(ht, "asdfKeyStrass", str1);
hash_insert(ht, "K13eyStras", str1);
hash_insert(ht, "KeyStr5", str1);
hash_insert(ht, "KeyStr", str1);
hash_lookup(ht, "KeyInt", (void **)&j);
hash_lookup(ht, "KeyStr", (void **)&find_str);
assert(strcmp(find_str, str1) == 0);
}
void test_lru_cache() {
lru_list *lru = (lru_list*)malloc(sizeof(lru_list));
int ret = lru_create(lru);
lru_enqueue(lru, "name", "ivan");
lru_enqueue(lru, "age", "18");
lru_enqueue(lru, "high", "178cm");
lru_enqueue(lru, "weigth", "65kg");
lru_enqueue(lru, "nickname", "abc");
lru_enqueue(lru, "school", "qqq");
lru_enqueue(lru, "home", "next street");
lru_dequeue(lru);
lru_dequeue(lru);
char *get = "home";
lru_get(lru, get);
lru_destory(lru);
}
int main () {
test_lru_cache();
return 0;
}