-
Notifications
You must be signed in to change notification settings - Fork 19.5k
/
MRUCache.java
230 lines (207 loc) · 6.56 KB
/
MRUCache.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
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
package com.thealgorithms.datastructures.caches;
import java.util.HashMap;
import java.util.Map;
/**
* Represents a Most Recently Used (MRU) Cache.
* <p>
* In contrast to the Least Recently Used (LRU) strategy, the MRU caching policy
* evicts the most recently accessed items first. This class provides methods to
* store key-value pairs and manage cache eviction based on this policy.
*
* For more information, refer to:
* <a href="https://en.wikipedia.org/wiki/Cache_replacement_policies#Most_recently_used_(MRU)">MRU on Wikipedia</a>.
*
* @param <K> the type of keys maintained by this cache
* @param <V> the type of values associated with the keys
*/
public class MRUCache<K, V> {
private final Map<K, Entry<K, V>> data = new HashMap<>();
private Entry<K, V> head;
private Entry<K, V> tail;
private int cap;
private static final int DEFAULT_CAP = 100;
/**
* Creates an MRUCache with the default capacity.
*/
public MRUCache() {
setCapacity(DEFAULT_CAP);
}
/**
* Creates an MRUCache with a specified capacity.
*
* @param cap the maximum number of items the cache can hold
*/
public MRUCache(int cap) {
setCapacity(cap);
}
/**
* Sets the capacity of the cache and evicts items if the new capacity
* is less than the current number of items.
*
* @param newCapacity the new capacity to set
*/
private void setCapacity(int newCapacity) {
checkCapacity(newCapacity);
while (data.size() > newCapacity) {
Entry<K, V> evicted = evict();
data.remove(evicted.getKey());
}
this.cap = newCapacity;
}
/**
* Checks if the specified capacity is valid.
*
* @param capacity the capacity to check
* @throws IllegalArgumentException if the capacity is less than or equal to zero
*/
private void checkCapacity(int capacity) {
if (capacity <= 0) {
throw new IllegalArgumentException("Capacity must be greater than 0!");
}
}
/**
* Evicts the most recently used entry from the cache.
*
* @return the evicted entry
* @throws RuntimeException if the cache is empty
*/
private Entry<K, V> evict() {
if (head == null) {
throw new RuntimeException("Cache cannot be empty!");
}
final Entry<K, V> evicted = this.tail;
tail = evicted.getPreEntry();
if (tail != null) {
tail.setNextEntry(null);
}
evicted.setNextEntry(null);
return evicted;
}
/**
* Retrieves the value associated with the specified key.
*
* @param key the key whose associated value is to be returned
* @return the value associated with the specified key, or null if the key does not exist
*/
public V get(K key) {
if (!data.containsKey(key)) {
return null;
}
final Entry<K, V> entry = data.get(key);
moveEntryToLast(entry);
return entry.getValue();
}
/**
* Associates the specified value with the specified key in the cache.
* If the key already exists, its value is updated and the entry is moved to the most recently used position.
* If the cache is full, the most recently used entry is evicted before adding the new entry.
*
* @param key the key with which the specified value is to be associated
* @param value the value to be associated with the specified key
*/
public void put(K key, V value) {
if (data.containsKey(key)) {
final Entry<K, V> existingEntry = data.get(key);
existingEntry.setValue(value);
moveEntryToLast(existingEntry);
return;
}
Entry<K, V> newEntry;
if (data.size() == cap) {
newEntry = evict();
data.remove(newEntry.getKey());
} else {
newEntry = new Entry<>();
}
newEntry.setKey(key);
newEntry.setValue(value);
addNewEntry(newEntry);
data.put(key, newEntry);
}
/**
* Adds a new entry to the cache and updates the head and tail pointers accordingly.
*
* @param newEntry the new entry to be added
*/
private void addNewEntry(Entry<K, V> newEntry) {
if (data.isEmpty()) {
head = newEntry;
tail = newEntry;
return;
}
tail.setNextEntry(newEntry);
newEntry.setPreEntry(tail);
newEntry.setNextEntry(null);
tail = newEntry;
}
/**
* Moves the specified entry to the most recently used position in the cache.
*
* @param entry the entry to be moved
*/
private void moveEntryToLast(Entry<K, V> entry) {
if (tail == entry) {
return;
}
final Entry<K, V> preEntry = entry.getPreEntry();
final Entry<K, V> nextEntry = entry.getNextEntry();
if (preEntry != null) {
preEntry.setNextEntry(nextEntry);
}
if (nextEntry != null) {
nextEntry.setPreEntry(preEntry);
}
if (head == entry) {
head = nextEntry;
}
tail.setNextEntry(entry);
entry.setPreEntry(tail);
entry.setNextEntry(null);
tail = entry;
}
/**
* A nested class representing an entry in the cache, which holds a key-value pair
* and references to the previous and next entries in the linked list structure.
*
* @param <I> the type of the key
* @param <J> the type of the value
*/
static final class Entry<I, J> {
private Entry<I, J> preEntry;
private Entry<I, J> nextEntry;
private I key;
private J value;
Entry() {
}
Entry(Entry<I, J> preEntry, Entry<I, J> nextEntry, I key, J value) {
this.preEntry = preEntry;
this.nextEntry = nextEntry;
this.key = key;
this.value = value;
}
public Entry<I, J> getPreEntry() {
return preEntry;
}
public void setPreEntry(Entry<I, J> preEntry) {
this.preEntry = preEntry;
}
public Entry<I, J> getNextEntry() {
return nextEntry;
}
public void setNextEntry(Entry<I, J> nextEntry) {
this.nextEntry = nextEntry;
}
public I getKey() {
return key;
}
public void setKey(I key) {
this.key = key;
}
public J getValue() {
return value;
}
public void setValue(J value) {
this.value = value;
}
}
}