OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 import 'dart:typed_data' show Uint32List; | 5 import 'dart:typed_data' show Uint32List; |
6 import 'dart:_internal' as internal; | 6 import 'dart:_internal' as internal; |
7 | 7 |
8 // Hash table with open addressing that separates the index from keys/values. | 8 // Hash table with open addressing that separates the index from keys/values. |
9 | 9 |
10 abstract class _HashFieldBase { | 10 abstract class _HashFieldBase { |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 // are doubled when _data is full. Thus, _index will have a max load factor | 64 // are doubled when _data is full. Thus, _index will have a max load factor |
65 // of 1/2, which enables one more bit to be used for the hash. | 65 // of 1/2, which enables one more bit to be used for the hash. |
66 // TODO(koda): Consider growing _data by factor sqrt(2), twice as often. | 66 // TODO(koda): Consider growing _data by factor sqrt(2), twice as often. |
67 static const int _INITIAL_INDEX_BITS = 3; | 67 static const int _INITIAL_INDEX_BITS = 3; |
68 static const int _INITIAL_INDEX_SIZE = 1 << (_INITIAL_INDEX_BITS + 1); | 68 static const int _INITIAL_INDEX_SIZE = 1 << (_INITIAL_INDEX_BITS + 1); |
69 | 69 |
70 // Unused and deleted entries are marked by 0 and 1, respectively. | 70 // Unused and deleted entries are marked by 0 and 1, respectively. |
71 static const int _UNUSED_PAIR = 0; | 71 static const int _UNUSED_PAIR = 0; |
72 static const int _DELETED_PAIR = 1; | 72 static const int _DELETED_PAIR = 1; |
73 | 73 |
| 74 static const int _MAX_LINEAR_DATA = 4096; |
| 75 static const int _MAX_LINEAR_MASK = 4095; |
| 76 static const int _MAX_LINEAR_DATA_LOG_2 = 12; |
| 77 |
| 78 // For sizes up to _MAX_LINEAR_DATA the size of the _data array is just the |
| 79 // size we ask for. Above that size we add enough elements onto the _data |
| 80 // array to hold _MAX_LINEAR_DATA-sized sub-arrays for the rest of the |
| 81 // entries. |
| 82 static int _sizeToBaseListSize(int size) { |
| 83 if (size <= _MAX_LINEAR_DATA) return size; |
| 84 // Round up. |
| 85 size = ((size - 1) | (_MAX_LINEAR_DATA - 1)) + 1; |
| 86 // First few entries are in the linear area. |
| 87 size -= _MAX_LINEAR_DATA; |
| 88 // Enough entries for the sub-arrays. |
| 89 size >>= _MAX_LINEAR_DATA_LOG_2; |
| 90 return _MAX_LINEAR_DATA + size; |
| 91 } |
| 92 |
| 93 static int _baseListSizeToSize(int baseListSize) { |
| 94 if (baseListSize <= _MAX_LINEAR_DATA) return baseListSize; |
| 95 baseListSize -= _MAX_LINEAR_DATA; |
| 96 baseListSize <<= _MAX_LINEAR_DATA_LOG_2; |
| 97 return baseListSize + _MAX_LINEAR_DATA; |
| 98 } |
| 99 |
| 100 static List _indexToList(List base, int index) { |
| 101 if (index < _MAX_LINEAR_DATA) return base; |
| 102 index >>= _MAX_LINEAR_DATA_LOG_2; |
| 103 return base[_MAX_LINEAR_DATA - 1 + index]; |
| 104 } |
| 105 |
| 106 static List _setSublist(List base, int index, List sublist) { |
| 107 assert(index >= _MAX_LINEAR_DATA); |
| 108 index -= _MAX_LINEAR_DATA; |
| 109 index >>= _MAX_LINEAR_DATA_LOG_2; |
| 110 base[_MAX_LINEAR_DATA + index] = sublist; |
| 111 } |
| 112 |
74 // On 32-bit, the top bits are wasted to avoid Mint allocation. | 113 // On 32-bit, the top bits are wasted to avoid Mint allocation. |
75 // TODO(koda): Reclaim the bits by making the compiler treat hash patterns | 114 // TODO(koda): Reclaim the bits by making the compiler treat hash patterns |
76 // as unsigned words. | 115 // as unsigned words. |
77 static int _indexSizeToHashMask(int indexSize) { | 116 static int _indexSizeToHashMask(int indexSize) { |
78 int indexBits = indexSize.bitLength - 2; | 117 int indexBits = indexSize.bitLength - 2; |
79 return internal.is64Bit | 118 return internal.is64Bit |
80 ? (1 << (32 - indexBits)) - 1 | 119 ? (1 << (32 - indexBits)) - 1 |
81 : (1 << (30 - indexBits)) - 1; | 120 : (1 << (30 - indexBits)) - 1; |
82 } | 121 } |
83 | 122 |
84 static int _hashPattern(int fullHash, int hashMask, int size) { | 123 static int _hashPattern(int fullHash, int hashMask, int size) { |
85 final int maskedHash = fullHash & hashMask; | 124 final int maskedHash = fullHash & hashMask; |
86 // TODO(koda): Consider keeping bit length and use left shift. | 125 // TODO(koda): Consider keeping bit length and use left shift. |
87 return (maskedHash == 0) ? (size >> 1) : maskedHash * (size >> 1); | 126 return (maskedHash == 0) ? (size >> 1) : maskedHash * (size >> 1); |
88 } | 127 } |
89 | 128 |
90 // Linear probing. | 129 // Linear probing. |
91 static int _firstProbe(int fullHash, int sizeMask) { | 130 static int _firstProbe(int fullHash, int sizeMask) { |
92 final int i = fullHash & sizeMask; | 131 final int i = fullHash & sizeMask; |
93 // Light, fast shuffle to mitigate bad hashCode (e.g., sequential). | 132 // Light, fast shuffle to mitigate bad hashCode (e.g., sequential). |
94 return ((i << 1) + i) & sizeMask; | 133 return ((i << 1) + i) & sizeMask; |
95 } | 134 } |
96 | 135 |
97 static int _nextProbe(int i, int sizeMask) => (i + 1) & sizeMask; | 136 static int _nextProbe(int i, int sizeMask) => (i + 1) & sizeMask; |
98 | 137 |
99 // A self-loop is used to mark a deleted key or value. | 138 // A self-loop is used to mark a deleted key or value. |
100 static bool _isDeleted(List data, Object keyOrValue) => | 139 static bool _isDeleted(List data, Object keyOrValue) => |
101 identical(keyOrValue, data); | 140 identical(data, keyOrValue); |
102 static void _setDeletedAt(List data, int d) { | 141 static void _setDeletedAt(List data, List sublist, int modulus) { |
103 data[d] = data; | 142 sublist[modulus] = data; |
104 } | 143 } |
105 | 144 |
106 // Concurrent modification detection relies on this checksum monotonically | 145 // Concurrent modification detection relies on this checksum monotonically |
107 // increasing between reallocations of _data. | 146 // increasing between reallocations of _data. |
108 int get _checkSum => _usedData + _deletedKeys; | 147 int get _checkSum => _usedData + _deletedKeys; |
109 bool _isModifiedSince(List oldData, int oldCheckSum) => | 148 bool _isModifiedSince(List oldData, int oldCheckSum) => |
110 !identical(_data, oldData) || (_checkSum != oldCheckSum); | 149 !identical(_data, oldData) || (_checkSum != oldCheckSum); |
111 } | 150 } |
112 | 151 |
113 class _OperatorEqualsAndHashCode { | 152 class _OperatorEqualsAndHashCode { |
(...skipping 25 matching lines...) Expand all Loading... |
139 | 178 |
140 class _LinkedHashMapMixin<K, V> { | 179 class _LinkedHashMapMixin<K, V> { |
141 int get length => (_usedData >> 1) - _deletedKeys; | 180 int get length => (_usedData >> 1) - _deletedKeys; |
142 bool get isEmpty => length == 0; | 181 bool get isEmpty => length == 0; |
143 bool get isNotEmpty => !isEmpty; | 182 bool get isNotEmpty => !isEmpty; |
144 | 183 |
145 void _rehash() { | 184 void _rehash() { |
146 if ((_deletedKeys << 2) > _usedData) { | 185 if ((_deletedKeys << 2) > _usedData) { |
147 // TODO(koda): Consider shrinking. | 186 // TODO(koda): Consider shrinking. |
148 // TODO(koda): Consider in-place compaction and more costly CME check. | 187 // TODO(koda): Consider in-place compaction and more costly CME check. |
149 _init(_index.length, _hashMask, _data, _usedData); | 188 _init(_index.length, _hashMask, _data); |
150 } else { | 189 } else { |
151 // TODO(koda): Support 32->64 bit transition (and adjust _hashMask). | 190 // TODO(koda): Support 32->64 bit transition (and adjust _hashMask). |
152 _init(_index.length << 1, _hashMask >> 1, _data, _usedData); | 191 _init(_index.length << 1, _hashMask >> 1, _data); |
153 } | 192 } |
154 } | 193 } |
155 | 194 |
156 void clear() { | 195 void clear() { |
157 if (!isEmpty) { | 196 if (!isEmpty) { |
158 // Use _data.length, since _index might be null. | 197 int size = _HashBase._INITIAL_INDEX_SIZE; |
159 _init(_data.length, _hashMask, null, 0); | 198 _init(size, _HashBase._indexSizeToHashMask(size), null); |
160 } | 199 } |
161 } | 200 } |
162 | 201 |
163 // Allocate new _index and _data, and optionally copy existing contents. | 202 // Allocate new _index and _data, and optionally copy existing contents. |
164 void _init(int size, int hashMask, List oldData, int oldUsed) { | 203 void _init(int size, int hashMask, List oldData) { |
165 assert(size & (size - 1) == 0); | 204 assert(size & (size - 1) == 0); |
166 assert(_HashBase._UNUSED_PAIR == 0); | 205 assert(_HashBase._UNUSED_PAIR == 0); |
167 _index = new Uint32List(size); | 206 _index = new Uint32List(size); |
168 _hashMask = hashMask; | 207 _hashMask = hashMask; |
169 _data = new List(size); | 208 if (_deletedKeys == 0 && _data == oldData) { |
| 209 _rebuildIndex(size, oldData); |
| 210 return; |
| 211 } |
| 212 _data = new List(_HashBase._sizeToBaseListSize(size)); |
| 213 int oldUsed = _usedData; |
170 _usedData = 0; | 214 _usedData = 0; |
171 _deletedKeys = 0; | 215 _deletedKeys = 0; |
172 if (oldData != null) { | 216 if (oldData != null) { |
173 for (int i = 0; i < oldUsed; i += 2) { | 217 for (int i = 0; i < oldUsed; i += 2) { |
174 var key = oldData[i]; | 218 List sublist = _HashBase._indexToList(oldData, i); |
| 219 int modulus = i & _HashBase._MAX_LINEAR_MASK; |
| 220 var key = sublist[modulus]; |
175 if (!_HashBase._isDeleted(oldData, key)) { | 221 if (!_HashBase._isDeleted(oldData, key)) { |
176 // TODO(koda): While there are enough hash bits, avoid hashCode calls. | 222 // TODO(koda): While there are enough hash bits, avoid hashCode calls. |
177 this[key] = oldData[i + 1]; | 223 this[key] = sublist[modulus + 1]; |
178 } | 224 } |
179 } | 225 } |
180 } | 226 } |
181 } | 227 } |
182 | 228 |
| 229 void _rebuildIndex(int size, List oldData) { |
| 230 int dataSize = _HashBase._sizeToBaseListSize(size); |
| 231 if (_data.length != dataSize) { |
| 232 _data = new List(dataSize); |
| 233 for (int i = 0; i < oldData.length; i++) { |
| 234 _data[i] = oldData[i]; |
| 235 } |
| 236 } |
| 237 int i = 0; |
| 238 int notYetAdded = _usedData; |
| 239 _usedData = 0; |
| 240 for (; i < notYetAdded && i < _HashBase._MAX_LINEAR_DATA; i += 2) { |
| 241 _setAlreadyThere(oldData[i]); |
| 242 } |
| 243 for (; i < oldData.length; i++) { |
| 244 notYetAdded -= _HashBase._MAX_LINEAR_DATA; |
| 245 List sublist = oldData[i]; |
| 246 for (int j = 0; j < notYetAdded && j < sublist.length; j += 2) { |
| 247 _setAlreadyThere(sublist[j]); |
| 248 } |
| 249 } |
| 250 } |
| 251 |
183 int _getIndexLength() { | 252 int _getIndexLength() { |
184 return (_index == null) ? _regenerateIndex() : _index.length; | 253 return (_index == null) ? _regenerateIndex() : _index.length; |
185 } | 254 } |
186 | 255 |
187 int _regenerateIndex() { | 256 int _regenerateIndex() { |
188 assert(_index == null); | 257 assert(_index == null); |
189 _index = new Uint32List(_data.length); | 258 _index = new Uint32List(_HashBase._baseListSizeToSize(_data.length)); |
190 assert(_hashMask == 0); | 259 assert(_hashMask == 0); |
191 _hashMask = _HashBase._indexSizeToHashMask(_index.length); | 260 _hashMask = _HashBase._indexSizeToHashMask(_index.length); |
192 final int tmpUsed = _usedData; | 261 _rebuildIndex(_data.length, _data); |
193 _usedData = 0; | |
194 for (int i = 0; i < tmpUsed; i += 2) { | |
195 // TODO(koda): Avoid redundant equality tests and stores into _data. | |
196 this[_data[i]] = _data[i + 1]; | |
197 } | |
198 return _index.length; | 262 return _index.length; |
199 } | 263 } |
200 | 264 |
201 void _insert(K key, V value, int hashPattern, int i) { | 265 void _insert(K key, V value, int hashPattern, int i) { |
202 if (_usedData == _data.length) { | 266 if (_usedData == _getIndexLength()) { |
203 _rehash(); | 267 _rehash(); |
204 this[key] = value; | 268 this[key] = value; |
205 } else { | 269 } else { |
206 assert(1 <= hashPattern && hashPattern < (1 << 32)); | 270 assert(1 <= hashPattern && hashPattern < (1 << 32)); |
207 final int index = _usedData >> 1; | 271 final int index = _usedData >> 1; |
208 assert((index & hashPattern) == 0); | 272 assert((index & hashPattern) == 0); |
209 _index[i] = hashPattern | index; | 273 _index[i] = hashPattern | index; |
210 _data[_usedData++] = key; | 274 List sublist = _HashBase._indexToList(_data, _usedData); |
211 _data[_usedData++] = value; | 275 if (sublist == null) { |
| 276 sublist = new List(_HashBase._MAX_LINEAR_DATA); |
| 277 _HashBase._setSublist(_data, _usedData, sublist); |
| 278 } |
| 279 int modulus = _usedData & _HashBase._MAX_LINEAR_MASK; |
| 280 sublist[modulus] = key; |
| 281 sublist[modulus + 1] = value; |
| 282 _usedData += 2; |
212 } | 283 } |
213 } | 284 } |
214 | 285 |
215 // If key is present, returns the index of the value in _data, else returns | 286 // If key is present, returns the index of the value in _data, else returns |
216 // the negated insertion point in _index. | 287 // the negated insertion point in _index. |
217 int _findValueOrInsertPoint(K key, int fullHash, int hashPattern, int size) { | 288 int _findValueOrInsertPoint(K key, int fullHash, int hashPattern, int size) { |
218 final int sizeMask = size - 1; | 289 final int sizeMask = size - 1; |
219 final int maxEntries = size >> 1; | 290 final int maxEntries = size >> 1; |
220 int i = _HashBase._firstProbe(fullHash, sizeMask); | 291 int i = _HashBase._firstProbe(fullHash, sizeMask); |
221 int firstDeleted = -1; | 292 int firstDeleted = -1; |
222 int pair = _index[i]; | 293 int pair = _index[i]; |
223 while (pair != _HashBase._UNUSED_PAIR) { | 294 while (pair != _HashBase._UNUSED_PAIR) { |
224 if (pair == _HashBase._DELETED_PAIR) { | 295 if (pair == _HashBase._DELETED_PAIR) { |
225 if (firstDeleted < 0) { | 296 if (firstDeleted < 0) { |
226 firstDeleted = i; | 297 firstDeleted = i; |
227 } | 298 } |
228 } else { | 299 } else { |
229 final int entry = hashPattern ^ pair; | 300 final int entry = hashPattern ^ pair; |
230 if (entry < maxEntries) { | 301 if (entry < maxEntries) { |
231 final int d = entry << 1; | 302 final int d = entry << 1; |
232 if (_equals(key, _data[d])) { | 303 List sublist = _HashBase._indexToList(_data, d); |
233 return d + 1; | 304 if (sublist != null) { |
| 305 int modulus = d & _HashBase._MAX_LINEAR_MASK; |
| 306 if (_equals(key, sublist[modulus])) { |
| 307 return d + 1; |
| 308 } |
234 } | 309 } |
235 } | 310 } |
236 } | 311 } |
237 i = _HashBase._nextProbe(i, sizeMask); | 312 i = _HashBase._nextProbe(i, sizeMask); |
238 pair = _index[i]; | 313 pair = _index[i]; |
239 } | 314 } |
240 return firstDeleted >= 0 ? -firstDeleted : -i; | 315 return firstDeleted >= 0 ? -firstDeleted : -i; |
241 } | 316 } |
242 | 317 |
| 318 // Adds a key to the index where the (key, value) are already in the data. |
| 319 void _setAlreadyThere(K key) { |
| 320 final int size = _getIndexLength(); |
| 321 final int sizeMask = size - 1; |
| 322 final int fullHash = _hashCode(key); |
| 323 final int hashPattern = _HashBase._hashPattern(fullHash, _hashMask, size); |
| 324 |
| 325 final int maxEntries = size >> 1; |
| 326 int i = _HashBase._firstProbe(fullHash, sizeMask); |
| 327 int pair = _index[i]; |
| 328 while (pair != _HashBase._UNUSED_PAIR) { |
| 329 i = _HashBase._nextProbe(i, sizeMask); |
| 330 pair = _index[i]; |
| 331 } |
| 332 |
| 333 assert(1 <= hashPattern && hashPattern < (1 << 32)); |
| 334 final int index = _usedData >> 1; |
| 335 assert((index & hashPattern) == 0); |
| 336 _index[i] = hashPattern | index; |
| 337 _usedData += 2; |
| 338 } |
| 339 |
243 void operator []=(K key, V value) { | 340 void operator []=(K key, V value) { |
244 final int size = _getIndexLength(); | 341 final int size = _getIndexLength(); |
245 final int sizeMask = size - 1; | 342 final int sizeMask = size - 1; |
246 final int fullHash = _hashCode(key); | 343 final int fullHash = _hashCode(key); |
247 final int hashPattern = _HashBase._hashPattern(fullHash, _hashMask, size); | 344 final int hashPattern = _HashBase._hashPattern(fullHash, _hashMask, size); |
248 final int d = _findValueOrInsertPoint(key, fullHash, hashPattern, size); | 345 final int d = _findValueOrInsertPoint(key, fullHash, hashPattern, size); |
249 if (d > 0) { | 346 if (d > 0) { |
250 _data[d] = value; | 347 List sublist = _HashBase._indexToList(_data, d); |
| 348 int modulus = d & _HashBase._MAX_LINEAR_MASK; |
| 349 sublist[modulus] = value; |
251 } else { | 350 } else { |
252 final int i = -d; | 351 final int i = -d; |
253 _insert(key, value, hashPattern, i); | 352 _insert(key, value, hashPattern, i); |
254 } | 353 } |
255 } | 354 } |
256 | 355 |
257 V putIfAbsent(K key, V ifAbsent()) { | 356 V putIfAbsent(K key, V ifAbsent()) { |
258 final int size = _getIndexLength(); | 357 final int size = _getIndexLength(); |
259 final int sizeMask = size - 1; | 358 final int sizeMask = size - 1; |
260 final int maxEntries = size >> 1; | 359 final int maxEntries = size >> 1; |
261 final int fullHash = _hashCode(key); | 360 final int fullHash = _hashCode(key); |
262 final int hashPattern = _HashBase._hashPattern(fullHash, _hashMask, size); | 361 final int hashPattern = _HashBase._hashPattern(fullHash, _hashMask, size); |
263 final int d = _findValueOrInsertPoint(key, fullHash, hashPattern, size); | 362 final int d = _findValueOrInsertPoint(key, fullHash, hashPattern, size); |
264 if (d > 0) { | 363 if (d > 0) { |
265 return _data[d]; | 364 List sublist = _HashBase._indexToList(_data, d); |
| 365 int modulus = d & _HashBase._MAX_LINEAR_MASK; |
| 366 return sublist[modulus]; |
266 } | 367 } |
267 // 'ifAbsent' is allowed to modify the map. | 368 // 'ifAbsent' is allowed to modify the map. |
268 List oldData = _data; | 369 List oldData = _data; |
269 int oldCheckSum = _checkSum; | 370 int oldCheckSum = _checkSum; |
270 V value = ifAbsent(); | 371 V value = ifAbsent(); |
271 if (_isModifiedSince(oldData, oldCheckSum)) { | 372 if (_isModifiedSince(oldData, oldCheckSum)) { |
272 this[key] = value; | 373 this[key] = value; |
273 } else { | 374 } else { |
274 final int i = -d; | 375 final int i = -d; |
275 _insert(key, value, hashPattern, i); | 376 _insert(key, value, hashPattern, i); |
276 } | 377 } |
277 return value; | 378 return value; |
278 } | 379 } |
279 | 380 |
280 V remove(Object key) { | 381 V remove(Object key) { |
281 final int size = _getIndexLength(); | 382 final int size = _getIndexLength(); |
282 final int sizeMask = size - 1; | 383 final int sizeMask = size - 1; |
283 final int maxEntries = size >> 1; | 384 final int maxEntries = size >> 1; |
284 final int fullHash = _hashCode(key); | 385 final int fullHash = _hashCode(key); |
285 final int hashPattern = _HashBase._hashPattern(fullHash, _hashMask, size); | 386 final int hashPattern = _HashBase._hashPattern(fullHash, _hashMask, size); |
286 int i = _HashBase._firstProbe(fullHash, sizeMask); | 387 int i = _HashBase._firstProbe(fullHash, sizeMask); |
287 int pair = _index[i]; | 388 int pair = _index[i]; |
288 while (pair != _HashBase._UNUSED_PAIR) { | 389 while (pair != _HashBase._UNUSED_PAIR) { |
289 if (pair != _HashBase._DELETED_PAIR) { | 390 if (pair != _HashBase._DELETED_PAIR) { |
290 final int entry = hashPattern ^ pair; | 391 final int entry = hashPattern ^ pair; |
291 if (entry < maxEntries) { | 392 if (entry < maxEntries) { |
292 final int d = entry << 1; | 393 final int d = entry << 1; |
293 if (_equals(key, _data[d])) { | 394 List sublist = _HashBase._indexToList(_data, d); |
| 395 int modulus = d & _HashBase._MAX_LINEAR_MASK; |
| 396 if (_equals(key, sublist[modulus])) { |
294 _index[i] = _HashBase._DELETED_PAIR; | 397 _index[i] = _HashBase._DELETED_PAIR; |
295 _HashBase._setDeletedAt(_data, d); | 398 _HashBase._setDeletedAt(_data, sublist, modulus); |
296 V value = _data[d + 1]; | 399 V value = sublist[modulus + 1]; |
297 _HashBase._setDeletedAt(_data, d + 1); | 400 _HashBase._setDeletedAt(_data, sublist, modulus + 1); |
298 ++_deletedKeys; | 401 ++_deletedKeys; |
299 return value; | 402 return value; |
300 } | 403 } |
301 } | 404 } |
302 } | 405 } |
303 i = _HashBase._nextProbe(i, sizeMask); | 406 i = _HashBase._nextProbe(i, sizeMask); |
304 pair = _index[i]; | 407 pair = _index[i]; |
305 } | 408 } |
306 return null; | 409 return null; |
307 } | 410 } |
308 | 411 |
309 // If key is absent, return _data (which is never a value). | 412 // If key is absent, return _data (which is never a value). |
310 Object _getValueOrData(Object key) { | 413 Object _getValueOrData(Object key) { |
311 final int size = _getIndexLength(); | 414 final int size = _getIndexLength(); |
312 final int sizeMask = size - 1; | 415 final int sizeMask = size - 1; |
313 final int maxEntries = size >> 1; | 416 final int maxEntries = size >> 1; |
314 final int fullHash = _hashCode(key); | 417 final int fullHash = _hashCode(key); |
315 final int hashPattern = _HashBase._hashPattern(fullHash, _hashMask, size); | 418 final int hashPattern = _HashBase._hashPattern(fullHash, _hashMask, size); |
316 int i = _HashBase._firstProbe(fullHash, sizeMask); | 419 int i = _HashBase._firstProbe(fullHash, sizeMask); |
317 int pair = _index[i]; | 420 int pair = _index[i]; |
318 while (pair != _HashBase._UNUSED_PAIR) { | 421 while (pair != _HashBase._UNUSED_PAIR) { |
319 if (pair != _HashBase._DELETED_PAIR) { | 422 if (pair != _HashBase._DELETED_PAIR) { |
320 final int entry = hashPattern ^ pair; | 423 final int entry = hashPattern ^ pair; |
321 if (entry < maxEntries) { | 424 if (entry < maxEntries) { |
322 final int d = entry << 1; | 425 final int d = entry << 1; |
323 if (_equals(key, _data[d])) { | 426 List sublist = _HashBase._indexToList(_data, d); |
324 return _data[d + 1]; | 427 int modulus = d & _HashBase._MAX_LINEAR_MASK; |
| 428 if (_equals(key, sublist[modulus])) { |
| 429 return sublist[modulus + 1]; |
325 } | 430 } |
326 } | 431 } |
327 } | 432 } |
328 i = _HashBase._nextProbe(i, sizeMask); | 433 i = _HashBase._nextProbe(i, sizeMask); |
329 pair = _index[i]; | 434 pair = _index[i]; |
330 } | 435 } |
331 return _data; | 436 return _data; |
332 } | 437 } |
333 | 438 |
334 bool containsKey(Object key) => !identical(_data, _getValueOrData(key)); | 439 bool containsKey(Object key) => !identical(_data, _getValueOrData(key)); |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
410 | 515 |
411 int get length => _table.length; | 516 int get length => _table.length; |
412 bool get isEmpty => length == 0; | 517 bool get isEmpty => length == 0; |
413 bool get isNotEmpty => !isEmpty; | 518 bool get isNotEmpty => !isEmpty; |
414 } | 519 } |
415 | 520 |
416 class _CompactIterator<E> implements Iterator<E> { | 521 class _CompactIterator<E> implements Iterator<E> { |
417 final _table; | 522 final _table; |
418 final List _data; | 523 final List _data; |
419 final int _len; | 524 final int _len; |
| 525 int _limit; |
| 526 List _currentList; |
420 int _offset; | 527 int _offset; |
421 final int _step; | 528 final int _step; |
422 final int _checkSum; | 529 final int _checkSum; |
423 E current; | 530 E current; |
424 | 531 |
425 _CompactIterator(table, this._data, this._len, this._offset, this._step) | 532 _CompactIterator(table, this._data, this._len, this._offset, this._step) |
426 : _table = table, | 533 : _table = table, |
427 _checkSum = table._checkSum; | 534 _checkSum = table._checkSum { |
| 535 _limit = -3; |
| 536 _currentList = _data; |
| 537 } |
428 | 538 |
429 bool moveNext() { | 539 bool moveNext() { |
430 if (_table._isModifiedSince(_data, _checkSum)) { | 540 if (_table._isModifiedSince(_data, _checkSum)) { |
431 throw new ConcurrentModificationError(_table); | 541 throw new ConcurrentModificationError(_table); |
432 } | 542 } |
| 543 int offset = _offset + _step; |
| 544 if (offset < _limit && |
| 545 !_HashBase._isDeleted(_data, |
| 546 current = _currentList[offset & _HashBase._MAX_LINEAR_MASK])) { |
| 547 _offset = offset; |
| 548 return true; |
| 549 } |
| 550 |
433 do { | 551 do { |
| 552 current = null; |
434 _offset += _step; | 553 _offset += _step; |
435 } while (_offset < _len && _HashBase._isDeleted(_data, _data[_offset])); | 554 int page = _offset >> _HashBase._MAX_LINEAR_DATA_LOG_2; |
436 if (_offset < _len) { | 555 _currentList = |
437 current = _data[_offset]; | 556 page == 0 ? _data : _data[_HashBase._MAX_LINEAR_DATA - 1 + page]; |
438 return true; | 557 if (page == _len >> _HashBase._MAX_LINEAR_DATA_LOG_2) { |
439 } else { | 558 _limit = _len; |
440 current = null; | 559 } else { |
441 return false; | 560 _limit = (page + 1) << _HashBase._MAX_LINEAR_DATA_LOG_2; |
442 } | 561 } |
| 562 } while (_offset < _limit && |
| 563 _HashBase._isDeleted(_data, |
| 564 current = _currentList[_offset & _HashBase._MAX_LINEAR_MASK])); |
| 565 |
| 566 return _offset < _limit; |
443 } | 567 } |
444 } | 568 } |
445 | 569 |
446 // Set implementation, analogous to _CompactLinkedHashMap. | 570 // Set implementation, analogous to _CompactLinkedHashMap. |
447 class _CompactLinkedHashSet<E> extends _HashFieldBase | 571 class _CompactLinkedHashSet<E> extends _HashFieldBase |
448 with _HashBase, _OperatorEqualsAndHashCode, SetMixin<E> | 572 with _HashBase, _OperatorEqualsAndHashCode, SetMixin<E> |
449 implements LinkedHashSet<E> { | 573 implements LinkedHashSet<E> { |
450 _CompactLinkedHashSet() : super(_HashBase._INITIAL_INDEX_SIZE >> 1) { | 574 _CompactLinkedHashSet() : super(_HashBase._INITIAL_INDEX_SIZE >> 1) { |
451 assert(_HashBase._UNUSED_PAIR == 0); | 575 assert(_HashBase._UNUSED_PAIR == 0); |
452 } | 576 } |
453 | 577 |
454 int get length => _usedData - _deletedKeys; | 578 int get length => _usedData - _deletedKeys; |
455 | 579 |
456 void _rehash() { | 580 void _rehash() { |
457 if ((_deletedKeys << 1) > _usedData) { | 581 if ((_deletedKeys << 1) > _usedData) { |
458 _init(_index.length, _hashMask, _data, _usedData); | 582 _init(_index.length, _hashMask, _data); |
459 } else { | 583 } else { |
460 _init(_index.length << 1, _hashMask >> 1, _data, _usedData); | 584 _init(_index.length << 1, _hashMask >> 1, _data); |
461 } | 585 } |
462 } | 586 } |
463 | 587 |
464 void clear() { | 588 void clear() { |
465 if (!isEmpty) { | 589 if (!isEmpty) { |
466 _init(_index.length, _hashMask, null, 0); | 590 int size = _HashBase._INITIAL_INDEX_SIZE; |
| 591 _init(size, _HashBase._indexSizeToHashMask(size), null); |
467 } | 592 } |
468 } | 593 } |
469 | 594 |
470 void _init(int size, int hashMask, List oldData, int oldUsed) { | 595 void _init(int size, int hashMask, List oldData) { |
471 _index = new Uint32List(size); | 596 _index = new Uint32List(size); |
472 _hashMask = hashMask; | 597 _hashMask = hashMask; |
473 _data = new List(size >> 1); | 598 if (_deletedKeys == 0 && _data == oldData) { |
| 599 _rebuildIndex(size, oldData); |
| 600 return; |
| 601 } |
| 602 _data = new List(_HashBase._sizeToBaseListSize(size >> 1)); |
| 603 int oldUsed = _usedData; |
474 _usedData = 0; | 604 _usedData = 0; |
475 _deletedKeys = 0; | 605 _deletedKeys = 0; |
476 if (oldData != null) { | 606 if (oldData != null) { |
477 for (int i = 0; i < oldUsed; i += 1) { | 607 for (int i = 0; i < oldUsed; i += 1) { |
478 var key = oldData[i]; | 608 List sublist = _HashBase._indexToList(oldData, i); |
| 609 int modulus = i & _HashBase._MAX_LINEAR_MASK; |
| 610 var key = sublist[modulus]; |
479 if (!_HashBase._isDeleted(oldData, key)) { | 611 if (!_HashBase._isDeleted(oldData, key)) { |
480 add(key); | 612 add(key); |
481 } | 613 } |
482 } | 614 } |
483 } | 615 } |
484 } | 616 } |
485 | 617 |
| 618 void _rebuildIndex(int size, List oldData) { |
| 619 int dataSize = _HashBase._sizeToBaseListSize(size >> 1); |
| 620 if (_data.length != dataSize) { |
| 621 _data = new List(dataSize); |
| 622 for (int i = 0; i < oldData.length; i++) { |
| 623 _data[i] = oldData[i]; |
| 624 } |
| 625 } |
| 626 _usedData = 0; |
| 627 // Unlike in the map case, this Set method is only called when the |
| 628 // data array and sublists are full, so we don't need to keep track of |
| 629 // the number of entries, we can just add everything from the data arrays |
| 630 // to the new index. |
| 631 for (int i = 0; i < oldData.length; i++) { |
| 632 if (i < _HashBase._MAX_LINEAR_DATA) { |
| 633 _addAlreadyThere(oldData[i]); |
| 634 } else { |
| 635 List sublist = oldData[i]; |
| 636 for (int j = 0; j < sublist.length; j++) { |
| 637 _addAlreadyThere(sublist[j]); |
| 638 } |
| 639 } |
| 640 } |
| 641 } |
| 642 |
486 bool add(E key) { | 643 bool add(E key) { |
487 final int size = _index.length; | 644 final int size = _index.length; |
488 final int sizeMask = size - 1; | 645 final int sizeMask = size - 1; |
489 final int maxEntries = size >> 1; | 646 final int maxEntries = size >> 1; |
490 final int fullHash = _hashCode(key); | 647 final int fullHash = _hashCode(key); |
491 final int hashPattern = _HashBase._hashPattern(fullHash, _hashMask, size); | 648 final int hashPattern = _HashBase._hashPattern(fullHash, _hashMask, size); |
492 int i = _HashBase._firstProbe(fullHash, sizeMask); | 649 int i = _HashBase._firstProbe(fullHash, sizeMask); |
493 int firstDeleted = -1; | 650 int firstDeleted = -1; |
494 int pair = _index[i]; | 651 int pair = _index[i]; |
495 while (pair != _HashBase._UNUSED_PAIR) { | 652 while (pair != _HashBase._UNUSED_PAIR) { |
496 if (pair == _HashBase._DELETED_PAIR) { | 653 if (pair == _HashBase._DELETED_PAIR) { |
497 if (firstDeleted < 0) { | 654 if (firstDeleted < 0) { |
498 firstDeleted = i; | 655 firstDeleted = i; |
499 } | 656 } |
500 } else { | 657 } else { |
501 final int d = hashPattern ^ pair; | 658 final int d = hashPattern ^ pair; |
502 if (d < maxEntries && _equals(key, _data[d])) { | 659 if (d < maxEntries) { |
503 return false; | 660 List sublist = _HashBase._indexToList(_data, d); |
| 661 if (sublist != null) { |
| 662 int modulus = d & _HashBase._MAX_LINEAR_MASK; |
| 663 if (_equals(key, sublist[modulus])) { |
| 664 return false; |
| 665 } |
| 666 } |
504 } | 667 } |
505 } | 668 } |
506 i = _HashBase._nextProbe(i, sizeMask); | 669 i = _HashBase._nextProbe(i, sizeMask); |
507 pair = _index[i]; | 670 pair = _index[i]; |
508 } | 671 } |
509 if (_usedData == _data.length) { | 672 if (_usedData == maxEntries) { |
510 _rehash(); | 673 _rehash(); |
511 add(key); | 674 add(key); |
512 } else { | 675 } else { |
513 final int insertionPoint = (firstDeleted >= 0) ? firstDeleted : i; | 676 final int insertionPoint = (firstDeleted >= 0) ? firstDeleted : i; |
514 assert(1 <= hashPattern && hashPattern < (1 << 32)); | 677 assert(1 <= hashPattern && hashPattern < (1 << 32)); |
515 assert((hashPattern & _usedData) == 0); | 678 assert((hashPattern & _usedData) == 0); |
516 _index[insertionPoint] = hashPattern | _usedData; | 679 _index[insertionPoint] = hashPattern | _usedData; |
517 _data[_usedData++] = key; | 680 List sublist = _HashBase._indexToList(_data, _usedData); |
| 681 if (sublist == null) { |
| 682 sublist = new List(_HashBase._MAX_LINEAR_DATA); |
| 683 _HashBase._setSublist(_data, _usedData, sublist); |
| 684 } |
| 685 int modulus = _usedData & _HashBase._MAX_LINEAR_MASK; |
| 686 sublist[modulus] = key; |
| 687 _usedData++; |
518 } | 688 } |
519 return true; | 689 return true; |
520 } | 690 } |
521 | 691 |
| 692 // Adds a key to the index which is already in the data. |
| 693 void _addAlreadyThere(E key) { |
| 694 final int size = _index.length; |
| 695 final int sizeMask = size - 1; |
| 696 final int maxEntries = size >> 1; |
| 697 final int fullHash = _hashCode(key); |
| 698 final int hashPattern = _HashBase._hashPattern(fullHash, _hashMask, size); |
| 699 int i = _HashBase._firstProbe(fullHash, sizeMask); |
| 700 int pair = _index[i]; |
| 701 while (pair != _HashBase._UNUSED_PAIR) { |
| 702 i = _HashBase._nextProbe(i, sizeMask); |
| 703 pair = _index[i]; |
| 704 } |
| 705 |
| 706 assert(1 <= hashPattern && hashPattern < (1 << 32)); |
| 707 assert((hashPattern & _usedData) == 0); |
| 708 _index[i] = hashPattern | _usedData; |
| 709 _usedData++; |
| 710 } |
| 711 |
522 // If key is absent, return _data (which is never a value). | 712 // If key is absent, return _data (which is never a value). |
523 Object _getKeyOrData(Object key) { | 713 Object _getKeyOrData(Object key) { |
524 final int size = _index.length; | 714 final int size = _index.length; |
525 final int sizeMask = size - 1; | 715 final int sizeMask = size - 1; |
526 final int maxEntries = size >> 1; | 716 final int maxEntries = size >> 1; |
527 final int fullHash = _hashCode(key); | 717 final int fullHash = _hashCode(key); |
528 final int hashPattern = _HashBase._hashPattern(fullHash, _hashMask, size); | 718 final int hashPattern = _HashBase._hashPattern(fullHash, _hashMask, size); |
529 int i = _HashBase._firstProbe(fullHash, sizeMask); | 719 int i = _HashBase._firstProbe(fullHash, sizeMask); |
530 int pair = _index[i]; | 720 int pair = _index[i]; |
531 while (pair != _HashBase._UNUSED_PAIR) { | 721 while (pair != _HashBase._UNUSED_PAIR) { |
532 if (pair != _HashBase._DELETED_PAIR) { | 722 if (pair != _HashBase._DELETED_PAIR) { |
533 final int d = hashPattern ^ pair; | 723 final int d = hashPattern ^ pair; |
534 if (d < maxEntries && _equals(key, _data[d])) { | 724 if (d < maxEntries) { |
535 return _data[d]; // Note: Must return the existing key. | 725 List sublist = _HashBase._indexToList(_data, d); |
| 726 if (sublist != null) { |
| 727 int modulus = d & _HashBase._MAX_LINEAR_MASK; |
| 728 if (_equals(key, sublist[modulus])) { |
| 729 return sublist[modulus]; // Note: Must return the existing key. |
| 730 } |
| 731 } |
536 } | 732 } |
537 } | 733 } |
538 i = _HashBase._nextProbe(i, sizeMask); | 734 i = _HashBase._nextProbe(i, sizeMask); |
539 pair = _index[i]; | 735 pair = _index[i]; |
540 } | 736 } |
541 return _data; | 737 return _data; |
542 } | 738 } |
543 | 739 |
544 E lookup(Object key) { | 740 E lookup(Object key) { |
545 var k = _getKeyOrData(key); | 741 var k = _getKeyOrData(key); |
546 return identical(_data, k) ? null : k; | 742 return identical(_data, k) ? null : k; |
547 } | 743 } |
548 | 744 |
549 bool contains(Object key) => !identical(_data, _getKeyOrData(key)); | 745 bool contains(Object key) => !identical(_data, _getKeyOrData(key)); |
550 | 746 |
551 bool remove(Object key) { | 747 bool remove(Object key) { |
552 final int size = _index.length; | 748 final int size = _index.length; |
553 final int sizeMask = size - 1; | 749 final int sizeMask = size - 1; |
554 final int maxEntries = size >> 1; | 750 final int maxEntries = size >> 1; |
555 final int fullHash = _hashCode(key); | 751 final int fullHash = _hashCode(key); |
556 final int hashPattern = _HashBase._hashPattern(fullHash, _hashMask, size); | 752 final int hashPattern = _HashBase._hashPattern(fullHash, _hashMask, size); |
557 int i = _HashBase._firstProbe(fullHash, sizeMask); | 753 int i = _HashBase._firstProbe(fullHash, sizeMask); |
558 int pair = _index[i]; | 754 int pair = _index[i]; |
559 while (pair != _HashBase._UNUSED_PAIR) { | 755 while (pair != _HashBase._UNUSED_PAIR) { |
560 if (pair != _HashBase._DELETED_PAIR) { | 756 if (pair != _HashBase._DELETED_PAIR) { |
561 final int d = hashPattern ^ pair; | 757 final int d = hashPattern ^ pair; |
562 if (d < maxEntries && _equals(key, _data[d])) { | 758 if (d < maxEntries) { |
563 _index[i] = _HashBase._DELETED_PAIR; | 759 List sublist = _HashBase._indexToList(_data, d); |
564 _HashBase._setDeletedAt(_data, d); | 760 if (sublist != null) { |
565 ++_deletedKeys; | 761 int modulus = d & _HashBase._MAX_LINEAR_MASK; |
566 return true; | 762 if (_equals(key, sublist[modulus])) { |
| 763 _index[i] = _HashBase._DELETED_PAIR; |
| 764 _HashBase._setDeletedAt(_data, sublist, modulus); |
| 765 ++_deletedKeys; |
| 766 return true; |
| 767 } |
| 768 } |
567 } | 769 } |
568 } | 770 } |
569 i = _HashBase._nextProbe(i, sizeMask); | 771 i = _HashBase._nextProbe(i, sizeMask); |
570 pair = _index[i]; | 772 pair = _index[i]; |
571 } | 773 } |
572 return false; | 774 return false; |
573 } | 775 } |
574 | 776 |
575 Iterator<E> get iterator => | 777 Iterator<E> get iterator => |
576 new _CompactIterator<E>(this, _data, _usedData, -1, 1); | 778 new _CompactIterator<E>(this, _data, _usedData, -1, 1); |
(...skipping 21 matching lines...) Expand all Loading... |
598 E lookup(Object o) => _validKey(o) ? super.lookup(o) : null; | 800 E lookup(Object o) => _validKey(o) ? super.lookup(o) : null; |
599 bool remove(Object o) => _validKey(o) ? super.remove(o) : false; | 801 bool remove(Object o) => _validKey(o) ? super.remove(o) : false; |
600 | 802 |
601 _CompactLinkedCustomHashSet(this._equality, this._hasher, validKey) | 803 _CompactLinkedCustomHashSet(this._equality, this._hasher, validKey) |
602 : _validKey = (validKey != null) ? validKey : new _TypeTest<E>().test; | 804 : _validKey = (validKey != null) ? validKey : new _TypeTest<E>().test; |
603 | 805 |
604 Set<E> toSet() => | 806 Set<E> toSet() => |
605 new _CompactLinkedCustomHashSet<E>(_equality, _hasher, _validKey) | 807 new _CompactLinkedCustomHashSet<E>(_equality, _hasher, _validKey) |
606 ..addAll(this); | 808 ..addAll(this); |
607 } | 809 } |
OLD | NEW |