-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy path460. LFU Cache.c
182 lines (166 loc) · 4.35 KB
/
460. LFU 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
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
/*
460. LFU Cache
Design and implement a data structure for Least Frequently Used (LFU) cache. It should support the following operations: get and put.
get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.
put(key, value) - Set or insert the value if the key is not already present. When the cache reaches its capacity, it should invalidate the least frequently used item before inserting a new item. For the purpose of this problem, when there is a tie (i.e., two or more keys that have the same frequency), the least recently used key would be evicted.
Follow up:
Could you do both operations in O(1) time complexity?
Example:
LFUCache cache = new LFUCache( 2 /* capacity */ );
cache.put(1, 1);
cache.put(2, 2);
cache.get(1); // returns 1
cache.put(3, 3); // evicts key 2
cache.get(2); // returns -1 (not found)
cache.get(3); // returns 3.
cache.put(4, 4); // evicts key 1.
cache.get(1); // returns -1 (not found)
cache.get(3); // returns 3
cache.get(4); // returns 4
*/
typedef struct item_s {
int key;
int val;
int ref;
// add mutex if it is used in multithreaded program
struct item_s *shadow;
struct item_s *prev;
struct item_s *next;
} item_t;
typedef struct {
int sz;
item_t **data;
item_t *head;
item_t *buff;
} LFUCache;
LFUCache* lFUCacheCreate(int capacity) {
LFUCache *p;
item_t *buff;
int i;
if (capacity <= 0) return NULL;
p = malloc(sizeof(item_t));
//assert(p);
p->data = calloc(capacity, sizeof(item_t *));
//assert(p->data);
buff = calloc(capacity, sizeof(item_t));
//assert(buff);
p->head = p->buff = &buff[0];
if (capacity > 1) {
buff[0].next = &buff[1];
for (i = 1; i < capacity - 1; i ++) {
buff[i].prev = &buff[i - 1];
buff[i].next = &buff[i + 1];
}
buff[capacity - 1].prev = &buff[capacity - 2];
}
p->sz = capacity;
return p;
}
void move2next(item_t *t, LFUCache *obj) {
item_t *a, *b;
a = t;
b = t->next;
while (b && b->ref <= t->ref) {
a = b;
b = b->next;
}
if (a != t) {
t->next->prev = t->prev;
if (t->prev) t->prev->next = t->next;
else obj->head = t->next;
a->next = t;
t->prev = a;
t->next = b;
if (b) b->prev = t;
}
}
item_t *lookup(LFUCache* obj, int key) {
item_t *t;
t = obj->data[key % obj->sz];
while (t && t->key != key) {
t = t->shadow;
}
return t;
}
int lFUCacheGet(LFUCache* obj, int key) {
item_t *t, *n, *np;
t = obj ? lookup(obj, key) : NULL;
if (!t) {
return -1;
}
t->ref ++;
move2next(t, obj);
return t->val;
}
void lFUCachePut(LFUCache* obj, int key, int value) {
item_t *t, **pp;
if (!obj) return;
t = lookup(obj, key);
if (!t) {
t = obj->head; // least frequently used
// take it out of the cache
pp = &obj->data[t->key % obj->sz];
while (*pp && *pp != t) {
pp = &(*pp)->shadow;
}
if (*pp) {
*pp = (*pp)->shadow;
}
// put it back with new key
t->key = key;
t->ref = 1;
t->shadow = obj->data[key % obj->sz];
obj->data[key % obj->sz] = t;
} else {
t->ref ++;
}
t->val = value;
move2next(t, obj);
}
void lFUCacheFree(LFUCache* obj) {
if (!obj) return;
free(obj->buff);
free(obj->data);
free(obj);
}
/**
* Your LFUCache struct will be instantiated and called as such:
* struct LFUCache* obj = lFUCacheCreate(capacity);
* int param_1 = lFUCacheGet(obj, key);
* lFUCachePut(obj, key, value);
* lFUCacheFree(obj);
*/
/*
Difficulty:Hard
Total Accepted:12.7K
Total Submissions:53.6K
Companies Amazon Google
Related Topics Design
Similar Questions
LRU Cache
Design In-Memory File System
*/