OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 part of dart._internal; | |
6 | |
7 class LRUAssociation<K,V> { | |
8 K key; | |
9 V value; | |
10 LRUAssociation previous; | |
11 LRUAssociation next; | |
Ivan Posva
2014/05/25 20:00:26
Empty lines between methods please.
| |
12 void insertBetween(before, after) { | |
13 after.previous = this; | |
14 before.next = this; | |
15 this.next = after; | |
16 this.previous = before; | |
17 } | |
18 void remove() { | |
19 var after = next; | |
20 var before = previous; | |
21 after.previous = before; | |
22 before.next = after; | |
23 } | |
24 } | |
25 | |
26 /** | |
27 * A map with a fixed capacity that evicts associations when capacity is reached | |
28 * on a least-recently-used basis. Implemented as an open addressing hash table | |
29 * with doubly-linked entries forming the LRU queue. | |
30 */ | |
31 class LRUMap<K,V> { | |
32 final LRUAssociation<K,V> head; | |
33 final List table; | |
34 final int capacity; | |
35 int size = 0; | |
36 | |
37 LRUMap(int capacity) | |
38 : this.capacity = capacity | |
39 , this.table = new List(capacity * 4 ~/ 3) | |
40 , this.head = new LRUAssociation() { | |
41 head.insertBetween(head, head); | |
Ivan Posva
2014/05/25 20:00:26
Tow space indent. This way it looks like part of t
| |
42 } | |
43 | |
44 int _scanFor(K key) { | |
45 var start = key.hashCode % table.length; | |
Ivan Posva
2014/05/25 20:00:26
How about rounding up the length to the next power
rmacnak
2014/05/30 21:26:05
Using a mask instead of modulus is a ~20% improvem
| |
46 var index = start; | |
47 do { | |
48 var assoc = table[index]; | |
49 if (assoc == null || assoc.key == key) { | |
Ivan Posva
2014/05/25 20:00:26
I don't think this will work that well for capacit
rmacnak
2014/05/30 21:26:05
This scheme relies on their always being at least
| |
50 return index; | |
51 } | |
52 index = (index + 1) % table.length; | |
53 } while (index != start); | |
54 // Should never happen because we start evicting associations before the | |
55 // table is full. | |
56 throw "LRUMap out of space"; | |
57 } | |
58 | |
59 void _fixCollisionsAfter(start) { | |
60 var assoc; | |
61 var index = (start + 1) % table.length; | |
62 while ((assoc = table[index]) != null) { | |
63 var newIndex = _scanFor(assoc.key); | |
64 table[newIndex] = assoc; | |
65 table[index] = null; | |
66 index = (index + 1) % table.length; | |
67 } | |
68 } | |
69 | |
70 operator []=(K key, V value) { | |
71 int index = _scanFor(key); | |
72 var assoc = table[index]; | |
73 if (assoc != null) { | |
74 // Existing key, replace value. | |
75 assoc.value = value; | |
76 assoc.remove(); | |
77 assoc.insertBetween(head, head.next); | |
78 } else { | |
79 // New key. | |
80 var newAssoc; | |
81 if (size == capacity) { | |
82 // Knock out the oldest association. | |
83 var lru = head.previous; | |
84 lru.remove(); | |
85 index = _scanFor(lru.key); | |
86 _fixCollisionsAfter(index); | |
87 index = _scanFor(key); | |
88 newAssoc = lru; // Recycle the association. | |
89 } else { | |
90 newAssoc = new LRUAssociation(); | |
91 size++; | |
92 } | |
93 newAssoc.key = key; | |
94 newAssoc.value = value; | |
95 newAssoc.insertBetween(head, head.next); | |
96 table[index] = newAssoc; | |
97 } | |
98 } | |
99 | |
100 V operator [](K key) { | |
101 var index = _scanFor(key); | |
102 var assoc = table[index]; | |
103 if (assoc == null) return null; | |
104 assoc.remove(); | |
105 assoc.insertBetween(head, head.next); | |
106 return assoc.value; | |
107 } | |
108 } | |
OLD | NEW |