-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMRUCache.java
297 lines (256 loc) · 6.27 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
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
/*
* The AddressCache has a max age for the elements it's storing, an add method
* for adding elements, a remove method for removing, a peek method which
* returns the most recently added element, and a take method which removes
* and returns the most recently added element.
* All operations are O(1)
*
*/
/*Note :
Asssuming InetAddress is userDefine class and hashcode and equals are overrided in that class
Also Doen't allow NUll Objects
MaxAge is no of address cache can hold at any time
*/
import java.util.HashMap;
class InetAddress {
Integer val;
InetAddress(Integer val)
{
this.val=val;
}
Integer getValue()
{
return val;
}
}
class TimeUnit
{
}
class AddressCache {
private long maxAge=0;
private TimeUnit unit=null;
DoublyLinkedList dll=null;
HashMap<InetAddress,InetAddress> cache=null;
private int count=0;
public AddressCache(long maxAge, TimeUnit unit) {
cache=new HashMap<InetAddress,InetAddress>((int)maxAge);
dll=new DoublyLinkedList();
this.maxAge=maxAge;
this.unit=unit;
}
/**
* add() method must store unique elements only (existing elements must be ignored).
* This will return true if the element was successfully added.
* @param address
* @return
*/
public boolean add(InetAddress address) {
if(count!=maxAge)
{
if(cache.containsKey(address))
{
cache.put(address,address);//always update with new value
if(dll.find(address)!=null)
{
dll.update(address);
}
}
else
{
cache.put(address,address); //add new value
dll.insertFirst(address); // also add this to linked list at head which make sure most recent item always added to head
}
count++;
return true;
}
else if(count==maxAge)//if cache full , remove
{
InetAddress thisAddress = dll.deleteFirst(); // //remove from list, First always be the most recent element
cache.remove(thisAddress);; //remove from map as well
cache.put(address,address); //add new value in list and map
dll.insertFirst(address);
return true;
}
return false;
}
/**
* remove() method will return true if the address was successfully removed
* @param address
* @return
*/
public boolean remove(InetAddress address) {
if(cache.remove(address)!=null && dll.deleteKey(address)!=null)
{
count--;
return true;
}
return false;
}
/**
* The peek() method will return the most recently added element,
* null if no element exists.
* @return
*/
public InetAddress peek() {
return dll.getFirst().iData; //always most recent , because most recent is added to head always
}
/**
* take() method retrieves and removes the most recently added element
* from the cache and waits if necessary until an element becomes available.
* @return
*/
public InetAddress take() {
InetAddress address=dll.getFirst().iData;
dll.deleteFirst(); //remove from list, First always be the most recent element
cache.remove(address); // remover from cache
count--;
return address;
}
}
class Link {
public InetAddress iData;
public Link next;
public Link previous;
public Link(InetAddress id) {
iData = id;
}
public String toString() {
return "{" + iData + "} ";
}
}
class DoublyLinkedList {
private Link first;
private Link last;
public DoublyLinkedList() {
first = null;
last = null;
}
public boolean isEmpty() {
return first == null;
}
public Link getFirst()
{
return first;
}
public Link getLast()
{
return last;
}
public void insertFirst(InetAddress dd) {
Link newLink = new Link(dd);
if (isEmpty()){
last = newLink;
}else{
first.previous = newLink;
}
newLink.next = first;
first = newLink;
}
public InetAddress deleteFirst() {
Link temp = first;
if (first.next == null){
last = null;
}else{
first.next.previous = null;
}
first = first.next;
return temp.iData;
}
public void insertLast(InetAddress dd) {
Link newLink = new Link(dd);
if (isEmpty()){
first = newLink;
}else {
last.next = newLink;
newLink.previous = last;
}
last = newLink;
}
public InetAddress deleteLast() {
Link temp = last;
if (first.next == null){
first = null;
}else{
last.previous.next = null;
}
last = last.previous;
return temp.iData;
}
public InetAddress deleteKey(InetAddress key) {
Link current = first;
while (current.iData != key) {
current = current.next;
if (current == null)
return null;
}
if (current == first){
first = current.next;
}else{
current.previous.next = current.next;
}
if (current == last){
last = current.previous;
}else{
current.next.previous = current.previous;
}
return current.iData;
}
public InetAddress find(InetAddress iData)
{
Link current = first;
while (current!=null) {
if(current.iData==iData)
return current.iData;
current=current.next;
}
return null; // if not found
}
public void update(InetAddress iData)
{
Link current = first;
while (current!=null) {
if(current.iData==iData)
current.iData=iData;
current=current.next;
}
}
public String toString() {
String str = "List (first-->last): ";
Link current = first;
while (current != null) {
str += current.toString();
current = current.next;
}
return str;
}
}
class MRUCache
{
public static void main (String[] args)
{
AddressCache cache=new AddressCache(3,null);
InetAddress a=new InetAddress(1);
InetAddress b=new InetAddress(2);
InetAddress c=new InetAddress(3);
InetAddress d=new InetAddress(4);
InetAddress e=new InetAddress(5);
cache.add(a);
cache.add(b);
cache.add(c);
cache.add(d);
cache.add(e);
System.out.println(cache.peek().getValue());
System.out.println(cache.remove(b));
System.out.println(cache.take().getValue());
System.out.println(cache.peek().getValue());
InetAddress d1=new InetAddress(4);
InetAddress e1=new InetAddress(5);
cache.add(b);
c=new InetAddress(33);
cache.add(c);
System.out.println(cache.peek().getValue());
System.out.println(cache.take().getValue());
System.out.println(cache.peek().getValue());
}
}
Run Here : http://ideone.com/cWNIRx ( Drop me to run this link)