-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathLruCache.hx
241 lines (204 loc) · 5.74 KB
/
LruCache.hx
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
package moon.data.map;
import haxe.Constraints.IMap;
import moon.data.list.DoubleLinkedList;
import moon.core.Signal;
/**
* Least Recently Used (LRU) Cache
*
* LruCache<T> works like a Map<String, T> except that it
* has limited `capacity`. When you add an item and the cache
* is full, the cache will remove an item from the back of
* the queue, to make room for a new item.
*
* Each time you access an item, that item will be moved
* to the front of the queue.
*
* @author Munir Hussin
*/
class LruCache<T> implements IMap<String, T>
{
private var map:Map<String, DoubleLinkedNode<CacheInfo<T>>>;
private var list:DoubleLinkedList<CacheInfo<T>>;
public var evicted:Signal<String, T>;
//public var missing:Signal<String, Ref<T>>;
public var onMiss:String->T;
public var capacity(default, set):Int;
public var length(get, never):Int;
public var evictionCount(default, null):Int;
public var hitCount(default, null):Int;
public var missCount(default, null):Int;
public function new(capacity:Int=-1, ?onMiss:String->T)
{
evictionCount = 0;
hitCount = 0;
missCount = 0;
map = new Map<String, DoubleLinkedNode<CacheInfo<T>>>();
list = new DoubleLinkedList<CacheInfo<T>>();
evicted = new Signal<String, T>();
//missing = new Signal<String, Ref<T>>();
this.capacity = capacity;
this.onMiss = onMiss;
}
/*==================================================
Properties
==================================================*/
private inline function get_length():Int
{
return list.length;
}
private inline function set_capacity(v:Int):Int
{
capacity = v;
trim();
return v;
}
/*==================================================
Methods
==================================================*/
/**
* Removes excess entries to satisfy capacity requirements.
*/
private inline function trim():Void
{
if (capacity >= 0)
while (length >= capacity)
evict();
}
/**
* Clears the cache.
*/
public inline function clear():Void
{
while (length > 0) remove(list.first.key);
}
/**
* Remove the oldest item that was accessed
*/
public function evict():Null<T>
{
if (length > 0)
{
var info = list.shift();
map.remove(info.key);
++evictionCount;
evicted.dispatch(info.key, info.value);
return info.value;
}
else
{
return null;
}
}
public function set(key:String, value:T):Void
{
var node = map.get(key);
// already exist. no length change. just update
if (node != null)
{
DoubleLinkedList.removeNode(list, node);
DoubleLinkedList.insertLast(list, node);
node.data.value = value;
}
// does not exist. need to add
else
{
// if cache is full, make room
trim();
// add it in
var info = new CacheInfo<T>(key, value);
list.push(info);
map.set(key, DoubleLinkedList.lastNode(list));
}
}
public function get(key:String):Null<T>
{
var node = map.get(key);
// node exist
if (node != null)
{
++hitCount;
DoubleLinkedList.removeNode(list, node);
DoubleLinkedList.insertLast(list, node);
return node.data.value;
}
// node does not exist
else
{
++missCount;
var value:T = null;
if (onMiss != null)
{
value = onMiss(key);
set(key, value);
}
return value;
// create a reference variable, and trigger missing signal
//var ref = new Ref<T>();
//missing.dispatch(key, ref);
// if reference now has a value (set by one of the listeners)
// then use that value
//if (ref.value != null)
// set(key, ref.value);
//return ref.value;
}
}
public inline function exists(key:String):Bool
{
return map.exists(key);
}
public function remove(key:String):Bool
{
var node = map.get(key);
if (node != null)
{
DoubleLinkedList.removeNode(list, node);
map.remove(key);
return true;
}
else
{
return false;
}
}
public inline function keys():Iterator<String>
{
return map.keys();
}
public inline function iterator():Iterator<T>
{
return new CacheIterator<T>(list);
}
public function toString():String
{
var arr:Array<String> = [];
for (info in list.iterator())
arr.push('${info.key} => ${info.value}');
return "{ " + arr.join(", ") + " }";
}
}
private class CacheInfo<T>
{
public var key:String;
public var value:T;
public function new(key:String, value:T)
{
this.key = key;
this.value = value;
}
}
private class CacheIterator<T>
{
public var it:Iterator<CacheInfo<T>>;
public inline function new(list:DoubleLinkedList<CacheInfo<T>>)
{
it = list.iterator();
}
public inline function hasNext():Bool
{
return it.hasNext();
}
public inline function next():T
{
return it.next().value;
}
}