-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathLFUCacheUsingLinkedHashSet.java
97 lines (82 loc) · 3.04 KB
/
LFUCacheUsingLinkedHashSet.java
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
package by.andd3dfx.cache;
import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
/**
* <pre>
* <a href="https://leetcode.com/problems/lfu-cache/">Task description</a>
*
* 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 null.
* - 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.
*
* Note that the number of times an item is used is the number of calls to the get and put functions
* for that item since it was inserted. This number is set to zero when the item is removed.
* </pre>
*
* @see <a href="https://youtu.be/4hhu0cSVUCA">Video solution</a>
*/
@Slf4j
@RequiredArgsConstructor
public class LFUCacheUsingLinkedHashSet<K, V> {
private final int capacity;
private Map<K, Item> map = new HashMap<>();
private LinkedHashSet<K> keySet = new LinkedHashSet<>();
public V get(K key) {
if (!map.containsKey(key)) {
return null;
}
Item item = map.get(key);
item.hitsCount++;
log.debug("GET: increased hits counter for key={}", key);
keySet.remove(key);
keySet.add(key);
return item.value;
}
public void put(K key, V value) {
if (capacity == 0) {
return;
}
if (map.containsKey(key)) {
Item item = map.get(key);
item.value = value;
item.hitsCount++;
log.debug("PUT: increased hits counter for key={}", key);
return;
}
if (map.size() == capacity) {
K keyToDelete = determineKeyToDelete();
map.remove(keyToDelete);
keySet.remove(keyToDelete);
}
map.put(key, new Item(value, 0));
keySet.add(key);
log.debug("PUT: added counter for key={}", key);
}
K determineKeyToDelete() {
List<K> keys = keySet.stream().toList();
return map.entrySet().stream()
.sorted((o1, o2) -> {
int delta = o1.getValue().hitsCount - o2.getValue().hitsCount;
if (delta != 0) {
return delta;
}
return keys.indexOf(o1.getKey()) - keys.indexOf(o2.getKey());
}).findFirst().get().getKey();
}
@AllArgsConstructor
public class Item {
private V value;
private int hitsCount;
}
}