OLD | NEW |
---|---|
(Empty) | |
1 part of dart.collection; | |
2 | |
3 class _DeadEntry { | |
4 const _DeadEntry(); | |
5 } | |
6 | |
7 class _NullKey { | |
8 const _NullKey(); | |
9 int get hashCode => null.hashCode; | |
10 } | |
11 | |
12 const _TOMBSTONE = const _DeadEntry(); | |
13 const _NULL = const _NullKey(); | |
14 | |
15 class _HashTable<K> { | |
16 /** | |
17 * Table of entries with [_entrySize] elements per entry. | |
floitsch
2013/02/06 10:43:58
[_entrySize] slots per entry.
| |
18 * | |
19 * Capacity in entries must be factor of two. | |
20 */ | |
21 List _table; | |
22 /** Current capacity. Always equal to [:_table.length ~/ _entrySize:]. */ | |
23 int _capacity; | |
24 /** Count of occupied entries, including deleted ones. */ | |
25 int _entryCount = 0; | |
26 /** Count of deleted entries. */ | |
27 int _deletedCount = 0; | |
28 /** Counter incremented when table is modified. */ | |
29 int _modificationCount = 0; | |
30 | |
31 _HashTable(int initialCapacity) : _capacity = initialCapacity { | |
32 _table = _createTable(initialCapacity); | |
33 } | |
34 | |
35 Object _key(offset) { | |
36 Object key = _table[offset]; | |
37 if (!identical(key, _NULL)) return key; | |
38 return null; | |
39 } | |
40 | |
41 void _setKey(int offset, Object key) { | |
42 if (key == null) key = _NULL; | |
43 _table[offset] = key; | |
44 } | |
45 | |
46 int get _elementCount => _entryCount - _deletedCount; | |
47 | |
48 /** Size of each entry. */ | |
49 int get _entrySize => 1; | |
50 | |
51 void _checkModification(int expectedModificationCount) { | |
52 if (_modificationCount != expectedModificationCount) { | |
53 throw new ConcurrentModificationError(this); | |
54 } | |
55 } | |
56 | |
57 /** | |
58 * Create an empty table. | |
59 */ | |
60 List _createTable(int capacity) { | |
61 List table = new List.fixedLength(capacity * _entrySize); | |
62 return table; | |
63 } | |
64 | |
65 /** First table probe. */ | |
66 int _firstProbe(int hashCode, int capacity) { | |
67 return hashCode & (capacity - 1); | |
68 } | |
69 | |
70 /** Following table probes. */ | |
71 int _nextProbe(int previousIndex, int probeCount, int capacity) { | |
72 return (previousIndex + probeCount) & (capacity - 1); | |
73 } | |
74 | |
75 /** Whether an object is a free-marker (either tombstone or free). */ | |
76 bool _isFree(Object marker) => | |
77 marker == null || identical(marker, _TOMBSTONE); | |
78 | |
79 /** | |
80 * Look up the offset for an object in the table. | |
81 * | |
82 * Finds the offset of the object in the table, if it is there, | |
83 * or the first free offset for its hashCode. | |
84 */ | |
85 int _probeForAdd(int hashCode, Object object) { | |
86 if (object == null) object = _NULL; | |
87 int entrySize = _entrySize; | |
88 int index = _firstProbe(hashCode, _capacity); | |
89 int firstTombstone = -1; | |
90 int probeCount = 0; | |
91 do { | |
92 int offset = index * entrySize; | |
93 Object entry = _key(offset); | |
94 if (identical(entry, _TOMBSTONE)) { | |
95 if (firstTombstone < 0) firstTombstone = offset; | |
96 } else if (entry == null) { | |
97 if (firstTombstone < 0) return offset; | |
98 return firstTombstone; | |
99 } else if (_equals(entry, object)) { | |
100 // TODO(lrn): Test if caching the last found key | |
101 // is better than (almost) always reading it again. | |
102 return offset; | |
103 } | |
104 // The _nextProbe must be designed so that it hits | |
105 // every index eventually. | |
106 index = _nextProbe(index, ++probeCount, _capacity); | |
107 } while (true); | |
108 } | |
109 | |
110 int _probeForLookup(int hashCode, Object object) { | |
111 if (object == null) object = _NULL; | |
112 int entrySize = _entrySize; | |
113 int index = _firstProbe(hashCode, _capacity); | |
114 int probeCount = 0; | |
115 while (true) { | |
floitsch
2013/02/06 10:43:58
Make it a do while(true) ?
erikcorry
2013/02/06 10:57:56
Why?
floitsch
2013/02/06 11:45:46
consistency with above.
And in general (without lo
erikcorry
2013/02/06 11:48:41
This must be a German language thing. You like ha
sra1
2013/02/06 17:01:08
I like
for (;;) {...}
If you know there is so
Lasse Reichstein Nielsen
2013/02/08 13:53:01
I've changed it to while(true). It is more readabl
| |
116 int offset = index * entrySize; | |
117 Object entry = _key(offset); | |
118 if (entry == null) { | |
119 return -1; | |
120 } else if (_equals(entry, object)) { | |
121 // TODO(lrn): Test if caching the last found key | |
122 // is better than (almost) always reading it again. | |
123 return offset; | |
124 } | |
125 // The _nextProbe must be designed so that it hits | |
126 // every index eventually. | |
127 index = _nextProbe(index, ++probeCount, _capacity); | |
128 } | |
129 } | |
130 | |
131 // Override the following two to change equality/hashCode computations | |
132 | |
133 /** | |
134 * Compare two object for equality. | |
135 * | |
136 * The first object is the one already in the table, | |
137 * and the second is the one being searched for. | |
138 */ | |
139 bool _equals(Object element, Object other) { | |
sra1
2013/02/06 05:35:21
Who overrides this?
Do you plan to use it for an i
Lasse Reichstein Nielsen
2013/02/08 13:53:01
Exactly.
| |
140 return element == other; | |
floitsch
2013/02/06 10:43:58
In the last implementation we were hitting a lot o
Lasse Reichstein Nielsen
2013/02/08 13:53:01
If that is a problem, maybe it can be avoided by a
| |
141 } | |
142 | |
143 /** | |
144 * Compute hash-code for an object. | |
145 */ | |
146 int _hashCodeOf(Object object) => object.hashCode; | |
147 | |
148 /** | |
149 * Ensure that the table has room to add [moreElements] entries. | |
150 * | |
151 * Call this before adding one or more elements. | |
152 */ | |
153 int _ensureCapacity(int moreElements) { | |
154 // Compute everything in multiples of entrySize to avoid division. | |
155 int entrySize = _entrySize; | |
156 int capacity = _capacity; | |
157 // Assume worst-case where no deleted elements are reused. | |
158 int newEntryCount = _entryCount + moreElements; | |
159 int newFreeCount = capacity - newEntryCount; | |
160 if (newFreeCount * 4 < capacity || | |
161 newFreeCount < _deletedCount) { | |
162 // Less than 25% free or less free entries than deleted entries. | |
floitsch
2013/02/06 10:43:58
fewer
Lasse Reichstein Nielsen
2013/02/08 13:53:01
Done.
| |
163 _grow(_entryCount - _deletedCount + moreElements); | |
164 } | |
165 } | |
166 | |
167 void _grow(int contentCount) { | |
168 int capacity = _capacity; | |
169 // Don't grow to less than twice the needed capacity. | |
170 int minCapacity = contentCount * 2; | |
171 do { | |
floitsch
2013/02/06 10:43:58
If a table adds and removes elements frequently, i
Lasse Reichstein Nielsen
2013/02/08 13:53:01
Good point. I've had the same thought too.
| |
172 capacity *= 2; | |
173 } while (capacity < minCapacity); | |
174 // Reset to another table and add all existing elements. | |
175 List oldTable = _table; | |
176 _table = _createTable(capacity); | |
177 _capacity = capacity; | |
178 _entryCount = 0; | |
179 _deletedCount = 0; | |
180 _addAllEntries(oldTable); | |
181 } | |
182 | |
183 /** | |
184 * Copies all non-free entries from the old table to the new empty table. | |
185 */ | |
186 void _addAllEntries(List oldTable) { | |
187 for (int i = 0; i < oldTable.length; i += _entrySize) { | |
188 Object object = oldTable[i]; | |
189 if (!_isFree(object)) { | |
190 int toOffset = _put(object); | |
191 _copyEntry(oldTable, i, toOffset); | |
192 } | |
193 } | |
194 } | |
195 | |
196 /** | |
197 * Copies everything but the key element from one entry to another. | |
198 * | |
199 * Called while growing the base array. | |
200 * | |
201 * Override this if verbatim copying isn't sufficient. | |
202 */ | |
203 void _copyEntry(List fromTable, int fromOffset, int toOffset) { | |
floitsch
2013/02/06 10:43:58
I don't think there is an implementation that uses
Lasse Reichstein Nielsen
2013/02/08 13:53:01
It's reduced to doing nothing. That is still corre
| |
204 int entrySize = _entrySize; | |
205 for (int i = 1; i < entrySize; i++) { | |
206 _table[toOffset + i] = fromTable[fromOffset + i]; | |
207 } | |
208 } | |
209 | |
210 // The following three methods are for simple get/set/remove operations. | |
211 // They only affect the key of an entry. The remaining fields must be | |
floitsch
2013/02/06 10:43:58
Not true. The clear method removes everything.
Lasse Reichstein Nielsen
2013/02/08 13:53:01
Whoops, moving _clear below the three methods I ac
| |
212 // filled by the caller. | |
213 | |
214 /** Clears the table completely, leaving it empty. */ | |
215 void _clear() { | |
216 for (int i = 0; i < _table.length; i ++) { | |
217 _table[i] = null; | |
218 } | |
219 _entryCount = _deletedCount = 0; | |
220 _modificationCount++; | |
221 } | |
222 | |
223 /** | |
224 * Returns the offset of a key in [_table], or negative if it's not there. | |
225 */ | |
226 int _get(K key) { | |
227 return _probeForLookup(_hashCodeOf(key), key); | |
228 } | |
229 | |
230 /** | |
231 * Puts the key into the table and returns its offset into [_table]. | |
232 * | |
233 * If [_entrySize] is greater than 1, the caller should fill the | |
234 * remaining fileds. | |
floitsch
2013/02/06 10:43:58
fields
Lasse Reichstein Nielsen
2013/02/08 13:53:01
Done.
| |
235 * | |
236 * Remember to call [_ensureCapacity] before using this method. | |
237 * You can call it once with a larger number before doing a sequence | |
238 * of put operations. | |
239 */ | |
240 int _put(K key) { | |
241 int offset = _probeForAdd(_hashCodeOf(key), key); | |
242 Object oldEntry = _key(offset); | |
243 if (oldEntry == null) { | |
244 _entryCount++; | |
245 _setKey(offset, key); | |
246 } else if (identical(oldEntry, _TOMBSTONE)) { | |
247 _deletedCount--; | |
248 _setKey(offset, key); | |
249 } | |
250 return offset; | |
251 } | |
252 | |
253 /** | |
254 * Removes a key from the table and returns its offset into [_table]. | |
255 * | |
256 * Returns null if the key was not in the table. | |
257 * If [_entrySize] is greater than 1, the caller should clean up the | |
258 * remaining fields. | |
259 */ | |
260 int _remove(K key) { | |
261 int offset = _probeForLookup(_hashCodeOf(key), key); | |
262 if (offset >= 0) { | |
263 _deleteEntry(offset); | |
264 } | |
265 return offset; | |
266 } | |
267 | |
268 void _deleteEntry(int offset) { | |
269 assert(!_isFree(_key(offset))); | |
270 _setKey(offset, _TOMBSTONE); | |
271 _deletedCount++; | |
272 } | |
273 } | |
274 | |
275 class _HashTableKeyIterable<K> extends Iterable<K> { | |
276 final _HashTable<K> _hashTable; | |
277 _HashTableKeyIterable(this._hashTable); | |
278 Iterator<K> get iterator => new _HashTableKeyIterator<K>(_hashTable); | |
279 | |
280 int get length => _hashTable._elementCount; | |
281 | |
282 bool contains(Object value) => _hashTable._get(value) >= 0; | |
283 | |
284 bool get isEmpty => _hashTable._elementCount == 0; | |
285 | |
286 K get single { | |
287 if (_hashTable._elementCount > 1) { | |
288 throw new StateError("More than one element"); | |
289 } | |
290 return first; | |
291 } | |
292 } | |
293 | |
294 class _HashTableKeyIterator<K> implements Iterator<K> { | |
295 final _HashTable<K> _hashTable; | |
296 final int _modificationCount; | |
297 /** Location right after last found element. */ | |
298 int _offset = 0; | |
299 K _current = null; | |
300 | |
301 _HashTableKeyIterator(_HashTable<K> hashTable) | |
302 : _hashTable = hashTable, | |
303 _modificationCount = hashTable._modificationCount; | |
304 | |
305 bool moveNext() { | |
306 _hashTable._checkModification(_modificationCount); | |
307 | |
308 List table = _hashTable._table; | |
309 int entrySize = _hashTable._entrySize; | |
310 | |
311 while (_offset < table.length) { | |
312 Object key = table[_offset]; | |
313 _offset += entrySize; | |
314 if (!_hashTable._isFree(key)) { | |
315 if (identical(key, _NULL)) { | |
316 _current = null; | |
317 } else { | |
318 _current = key; | |
319 } | |
320 return true; | |
321 } | |
322 } | |
323 _current = null; | |
324 return false; | |
325 } | |
326 | |
327 K get current => _current; | |
328 } | |
329 | |
330 class _HashTableValueIterable<V> extends Iterable<V> { | |
331 final _HashTable _hashTable; | |
332 final int _entryIndex; | |
333 _HashTableValueIterable(this._hashTable, this._entryIndex); | |
334 | |
335 Iterator<V> get iterator => | |
336 new _HashTableValueIterator<V>(_hashTable, _entryIndex); | |
337 | |
338 int get length => _hashTable._elementCount; | |
339 } | |
340 | |
341 /** | |
342 * Iterator traversing a [HashTable] and returning a value for each entry. | |
343 */ | |
344 class _HashTableValueIterator<V> implements Iterator<V> { | |
345 final _HashTable _hashTable; | |
346 /** The index of the value in the entry.*/ | |
347 final int _entryIndex; | |
348 final int _modificationCount; | |
349 /** Location right after last found element. */ | |
350 int _offset = 0; | |
351 V _current = null; | |
352 | |
353 _HashTableValueIterator(_HashTable hashTable, this._entryIndex) | |
354 : _hashTable = hashTable, | |
355 _modificationCount = hashTable._modificationCount { | |
356 assert(_entryIndex > 0); // Use key-iterator above for index 0. | |
357 assert(_entryIndex < hashTable._entrySize); | |
358 } | |
359 | |
360 bool moveNext() { | |
361 _hashTable._checkModification(_modificationCount); | |
362 | |
363 List table = _hashTable._table; | |
364 int entrySize = _hashTable._entrySize; | |
365 | |
366 while (_offset < table.length) { | |
367 Object key = table[_offset]; | |
368 int currentOffset = _offset; | |
369 _offset += entrySize; | |
370 if (!_hashTable._isFree(key)) { | |
371 _current = table[currentOffset + _entryIndex]; | |
372 return true; | |
373 } | |
374 } | |
375 _current = null; | |
376 return false; | |
377 } | |
378 | |
379 V get current => _current; | |
380 } | |
OLD | NEW |