-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlru.c
130 lines (111 loc) · 2.41 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
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
//实现一个LRU list,并且支持先进先出
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct kv {
int key;
int value;
struct kv *prev, *next;
};
struct lru {
struct kv head;
int size;
int count;
} lru;
int init_lrc_cache(struct lru *lru, int size)
{
if (!lru)
exit(1);
lru->size = size;
lru->count = 0;
lru->head.next = &lru->head;
lru->head.prev = &lru->head;
}
int full(struct lru *lru)
{
if (!lru)
exit(1);
if (lru->count == lru->size)
return 1;
else
return 0;
}
int empty(struct lru *lru)
{
if (!lru)
exit(1);
if (lru->count == 0)
return 1;
else
return 0;
}
int get(struct lru *lru, int key)
{
struct kv *temp;
struct kv *head = &lru->head;
if (!lru)
exit(1);
if (empty(lru))
return -1;
temp = head->next;
while(temp != head) {
if (temp->key == key) {
temp->prev->next = temp->next;
temp->next->prev = temp->prev;
temp->next = head->next;
temp->prev = head;
head->next->prev = temp;
head->next = temp;
return temp->value;
}
temp = temp->next;
}
return -1;
}
int put(struct lru *lru, int key, int value)
{
struct kv *temp, *last;
struct kv *head = &lru->head;
if (!lru)
exit(1);
temp = (struct kv *)malloc(sizeof(struct kv));
temp->key = key;
temp->value = value;
//insert
temp->next = head->next;
temp->prev = head;
head->next->prev = temp;
head->next = temp;
lru->count++;
while (lru->count > lru->size) {
last = head->prev;
last->prev->next = last->next;
last->next->prev = last->prev;
last->prev = NULL;
last->next = NULL;
free(last);
lru->count--;
}
return 0;
}
int main(void)
{
char command;
int key, value, size;
while(1) {
scanf("%c", &command);
if (command == 'p') { //put
scanf("%d", &key);
scanf("%d", &value);
put(&lru, key, value);
} else if (command == 'g'){ //get
scanf("%d", &key);
value = get(&lru, key);
printf("get:key:%d value:%d\n", key, value);
} else if (command == 'c') { //create
scanf("%d", &size);
init_lrc_cache(&lru, size);
}
}
return 0;
}