| 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 { |
| 11 // Each occupied entry in _index is a fixed-size integer that encodes a pair: | 11 // Each occupied entry in _index is a fixed-size integer that encodes a pair: |
| 12 // [ hash pattern for key | index of entry in _data ] | 12 // [ hash pattern for key | index of entry in _data ] |
| 13 // The hash pattern is based on hashCode, but is guaranteed to be non-zero. | 13 // The hash pattern is based on hashCode, but is guaranteed to be non-zero. |
| 14 // The length of _index is always a power of two, and there is always at | 14 // The length of _index is always a power of two, and there is always at |
| 15 // least one unoccupied entry. | 15 // least one unoccupied entry. |
| 16 // NOTE: When maps are deserialized, their _index and _hashMask is regenerated | 16 // NOTE: When maps are deserialized, their _index and _hashMask is regenerated |
| 17 // lazily by _regenerateIndex. | 17 // lazily by _regenerateIndex. |
| 18 // TODO(koda): Consider also using null _index for tiny, linear-search tables. | 18 // TODO(koda): Consider also using null _index for tiny, linear-search tables. |
| 19 Uint32List _index = new Uint32List(_HashBase._INITIAL_INDEX_SIZE); | 19 Uint32List _index = new Uint32List(_HashBase._INITIAL_INDEX_SIZE); |
| 20 | 20 |
| 21 // Cached in-place mask for the hash pattern component. | 21 // Cached in-place mask for the hash pattern component. |
| 22 int _hashMask = _HashBase._indexSizeToHashMask(_HashBase._INITIAL_INDEX_SIZE); | 22 int _hashMask = _HashBase._indexSizeToHashMask(_HashBase._INITIAL_INDEX_SIZE); |
| 23 | 23 |
| 24 // Fixed-length list of keys (set) or key/value at even/odd indices (map). | 24 // Fixed-length list of keys (set) or key/value at even/odd indices (map). |
| 25 List _data; | 25 List _data; |
| 26 | 26 |
| 27 // Length of _data that is used (i.e., keys + values for a map). | 27 // Length of _data that is used (i.e., keys + values for a map). |
| 28 int _usedData = 0; | 28 int _usedData = 0; |
| 29 | 29 |
| 30 // Number of deleted keys. | 30 // Number of deleted keys. |
| 31 int _deletedKeys = 0; | 31 int _deletedKeys = 0; |
| (...skipping 24 matching lines...) Expand all Loading... |
| 56 | 56 |
| 57 // This mixin can be applied to _HashFieldBase or _HashVMBase (for | 57 // This mixin can be applied to _HashFieldBase or _HashVMBase (for |
| 58 // normal and VM-internalized classes, respectivley), which provide the | 58 // normal and VM-internalized classes, respectivley), which provide the |
| 59 // actual fields/accessors that this mixin assumes. | 59 // actual fields/accessors that this mixin assumes. |
| 60 // TODO(koda): Consider moving field comments to _HashFieldBase. | 60 // TODO(koda): Consider moving field comments to _HashFieldBase. |
| 61 abstract class _HashBase { | 61 abstract class _HashBase { |
| 62 // The number of bits used for each component is determined by table size. | 62 // The number of bits used for each component is determined by table size. |
| 63 // The length of _index is twice the number of entries in _data, and both | 63 // The length of _index is twice the number of entries in _data, and both |
| 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 // On 32-bit, the top bits are wasted to avoid Mint allocation. | 74 // 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 | 75 // TODO(koda): Reclaim the bits by making the compiler treat hash patterns |
| 76 // as unsigned words. | 76 // as unsigned words. |
| 77 static int _indexSizeToHashMask(int indexSize) { | 77 static int _indexSizeToHashMask(int indexSize) { |
| 78 int indexBits = indexSize.bitLength - 2; | 78 int indexBits = indexSize.bitLength - 2; |
| 79 return internal.is64Bit | 79 return internal.is64Bit ? (1 << (32 - indexBits)) - 1 : |
| 80 ? (1 << (32 - indexBits)) - 1 | 80 (1 << (30 - indexBits)) - 1; |
| 81 : (1 << (30 - indexBits)) - 1; | |
| 82 } | 81 } |
| 83 | 82 |
| 84 static int _hashPattern(int fullHash, int hashMask, int size) { | 83 static int _hashPattern(int fullHash, int hashMask, int size) { |
| 85 final int maskedHash = fullHash & hashMask; | 84 final int maskedHash = fullHash & hashMask; |
| 86 // TODO(koda): Consider keeping bit length and use left shift. | 85 // TODO(koda): Consider keeping bit length and use left shift. |
| 87 return (maskedHash == 0) ? (size >> 1) : maskedHash * (size >> 1); | 86 return (maskedHash == 0) ? (size >> 1) : maskedHash * (size >> 1); |
| 88 } | 87 } |
| 89 | 88 |
| 90 // Linear probing. | 89 // Linear probing. |
| 91 static int _firstProbe(int fullHash, int sizeMask) { | 90 static int _firstProbe(int fullHash, int sizeMask) { |
| 92 final int i = fullHash & sizeMask; | 91 final int i = fullHash & sizeMask; |
| 93 // Light, fast shuffle to mitigate bad hashCode (e.g., sequential). | 92 // Light, fast shuffle to mitigate bad hashCode (e.g., sequential). |
| 94 return ((i << 1) + i) & sizeMask; | 93 return ((i << 1) + i) & sizeMask; |
| 95 } | 94 } |
| 96 | |
| 97 static int _nextProbe(int i, int sizeMask) => (i + 1) & sizeMask; | 95 static int _nextProbe(int i, int sizeMask) => (i + 1) & sizeMask; |
| 98 | 96 |
| 99 // A self-loop is used to mark a deleted key or value. | 97 // A self-loop is used to mark a deleted key or value. |
| 100 static bool _isDeleted(List data, Object keyOrValue) => | 98 static bool _isDeleted(List data, Object keyOrValue) => |
| 101 identical(keyOrValue, data); | 99 identical(keyOrValue, data); |
| 102 static void _setDeletedAt(List data, int d) { | 100 static void _setDeletedAt(List data, int d) { |
| 103 data[d] = data; | 101 data[d] = data; |
| 104 } | 102 } |
| 105 | 103 |
| 106 // Concurrent modification detection relies on this checksum monotonically | 104 // Concurrent modification detection relies on this checksum monotonically |
| 107 // increasing between reallocations of _data. | 105 // increasing between reallocations of _data. |
| 108 int get _checkSum => _usedData + _deletedKeys; | 106 int get _checkSum => _usedData + _deletedKeys; |
| 109 bool _isModifiedSince(List oldData, int oldCheckSum) => | 107 bool _isModifiedSince(List oldData, int oldCheckSum) => |
| 110 !identical(_data, oldData) || (_checkSum != oldCheckSum); | 108 !identical(_data, oldData) || (_checkSum != oldCheckSum); |
| 111 } | 109 } |
| 112 | 110 |
| 113 class _OperatorEqualsAndHashCode { | 111 class _OperatorEqualsAndHashCode { |
| 114 int _hashCode(e) => e.hashCode; | 112 int _hashCode(e) => e.hashCode; |
| 115 bool _equals(e1, e2) => e1 == e2; | 113 bool _equals(e1, e2) => e1 == e2; |
| 116 } | 114 } |
| 117 | 115 |
| 118 class _IdenticalAndIdentityHashCode { | 116 class _IdenticalAndIdentityHashCode { |
| 119 int _hashCode(e) => identityHashCode(e); | 117 int _hashCode(e) => identityHashCode(e); |
| 120 bool _equals(e1, e2) => identical(e1, e2); | 118 bool _equals(e1, e2) => identical(e1, e2); |
| 121 } | 119 } |
| 122 | 120 |
| 123 // VM-internalized implementation of a default-constructed LinkedHashMap. | 121 // VM-internalized implementation of a default-constructed LinkedHashMap. |
| 124 class _InternalLinkedHashMap<K, V> extends _HashVMBase | 122 class _InternalLinkedHashMap<K, V> extends _HashVMBase |
| 125 with | 123 with MapMixin<K, V>, _LinkedHashMapMixin<K, V>, _HashBase, |
| 126 MapMixin<K, V>, | 124 _OperatorEqualsAndHashCode |
| 127 _LinkedHashMapMixin<K, V>, | |
| 128 _HashBase, | |
| 129 _OperatorEqualsAndHashCode | |
| 130 implements LinkedHashMap<K, V> { | 125 implements LinkedHashMap<K, V> { |
| 131 _InternalLinkedHashMap() { | 126 _InternalLinkedHashMap() { |
| 132 _index = new Uint32List(_HashBase._INITIAL_INDEX_SIZE); | 127 _index = new Uint32List(_HashBase._INITIAL_INDEX_SIZE); |
| 133 _hashMask = _HashBase._indexSizeToHashMask(_HashBase._INITIAL_INDEX_SIZE); | 128 _hashMask = _HashBase._indexSizeToHashMask(_HashBase._INITIAL_INDEX_SIZE); |
| 134 _data = new List(_HashBase._INITIAL_INDEX_SIZE); | 129 _data = new List(_HashBase._INITIAL_INDEX_SIZE); |
| 135 _usedData = 0; | 130 _usedData = 0; |
| 136 _deletedKeys = 0; | 131 _deletedKeys = 0; |
| 137 } | 132 } |
| 138 } | 133 } |
| 139 | 134 |
| 140 class _LinkedHashMapMixin<K, V> { | 135 class _LinkedHashMapMixin<K, V> { |
| 141 int get length => (_usedData >> 1) - _deletedKeys; | 136 int get length => (_usedData >> 1) - _deletedKeys; |
| 142 bool get isEmpty => length == 0; | 137 bool get isEmpty => length == 0; |
| 143 bool get isNotEmpty => !isEmpty; | 138 bool get isNotEmpty => !isEmpty; |
| 144 | 139 |
| 145 void _rehash() { | 140 void _rehash() { |
| 146 if ((_deletedKeys << 2) > _usedData) { | 141 if ((_deletedKeys << 2) > _usedData) { |
| 147 // TODO(koda): Consider shrinking. | 142 // TODO(koda): Consider shrinking. |
| 148 // TODO(koda): Consider in-place compaction and more costly CME check. | 143 // TODO(koda): Consider in-place compaction and more costly CME check. |
| 149 _init(_index.length, _hashMask, _data, _usedData); | 144 _init(_index.length, _hashMask, _data, _usedData); |
| 150 } else { | 145 } else { |
| 151 // TODO(koda): Support 32->64 bit transition (and adjust _hashMask). | 146 // TODO(koda): Support 32->64 bit transition (and adjust _hashMask). |
| 152 _init(_index.length << 1, _hashMask >> 1, _data, _usedData); | 147 _init(_index.length << 1, _hashMask >> 1, _data, _usedData); |
| 153 } | 148 } |
| 154 } | 149 } |
| 155 | 150 |
| 156 void clear() { | 151 void clear() { |
| 157 if (!isEmpty) { | 152 if (!isEmpty) { |
| 158 // Use _data.length, since _index might be null. | 153 // Use _data.length, since _index might be null. |
| 159 _init(_data.length, _hashMask, null, 0); | 154 _init(_data.length, _hashMask, null, 0); |
| 160 } | 155 } |
| 161 } | 156 } |
| 162 | 157 |
| 163 // Allocate new _index and _data, and optionally copy existing contents. | 158 // Allocate new _index and _data, and optionally copy existing contents. |
| 164 void _init(int size, int hashMask, List oldData, int oldUsed) { | 159 void _init(int size, int hashMask, List oldData, int oldUsed) { |
| 165 assert(size & (size - 1) == 0); | 160 assert(size & (size - 1) == 0); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 190 assert(_hashMask == 0); | 185 assert(_hashMask == 0); |
| 191 _hashMask = _HashBase._indexSizeToHashMask(_index.length); | 186 _hashMask = _HashBase._indexSizeToHashMask(_index.length); |
| 192 final int tmpUsed = _usedData; | 187 final int tmpUsed = _usedData; |
| 193 _usedData = 0; | 188 _usedData = 0; |
| 194 for (int i = 0; i < tmpUsed; i += 2) { | 189 for (int i = 0; i < tmpUsed; i += 2) { |
| 195 // TODO(koda): Avoid redundant equality tests and stores into _data. | 190 // TODO(koda): Avoid redundant equality tests and stores into _data. |
| 196 this[_data[i]] = _data[i + 1]; | 191 this[_data[i]] = _data[i + 1]; |
| 197 } | 192 } |
| 198 return _index.length; | 193 return _index.length; |
| 199 } | 194 } |
| 200 | 195 |
| 201 void _insert(K key, V value, int hashPattern, int i) { | 196 void _insert(K key, V value, int hashPattern, int i) { |
| 202 if (_usedData == _data.length) { | 197 if (_usedData == _data.length) { |
| 203 _rehash(); | 198 _rehash(); |
| 204 this[key] = value; | 199 this[key] = value; |
| 205 } else { | 200 } else { |
| 206 assert(1 <= hashPattern && hashPattern < (1 << 32)); | 201 assert(1 <= hashPattern && hashPattern < (1 << 32)); |
| 207 final int index = _usedData >> 1; | 202 final int index = _usedData >> 1; |
| 208 assert((index & hashPattern) == 0); | 203 assert((index & hashPattern) == 0); |
| 209 _index[i] = hashPattern | index; | 204 _index[i] = hashPattern | index; |
| 210 _data[_usedData++] = key; | 205 _data[_usedData++] = key; |
| 211 _data[_usedData++] = value; | 206 _data[_usedData++] = value; |
| 212 } | 207 } |
| 213 } | 208 } |
| 214 | 209 |
| 215 // If key is present, returns the index of the value in _data, else returns | 210 // If key is present, returns the index of the value in _data, else returns |
| 216 // the negated insertion point in _index. | 211 // the negated insertion point in _index. |
| 217 int _findValueOrInsertPoint(K key, int fullHash, int hashPattern, int size) { | 212 int _findValueOrInsertPoint(K key, int fullHash, int hashPattern, int size) { |
| 218 final int sizeMask = size - 1; | 213 final int sizeMask = size - 1; |
| 219 final int maxEntries = size >> 1; | 214 final int maxEntries = size >> 1; |
| 220 int i = _HashBase._firstProbe(fullHash, sizeMask); | 215 int i = _HashBase._firstProbe(fullHash, sizeMask); |
| 221 int firstDeleted = -1; | 216 int firstDeleted = -1; |
| 222 int pair = _index[i]; | 217 int pair = _index[i]; |
| 223 while (pair != _HashBase._UNUSED_PAIR) { | 218 while (pair != _HashBase._UNUSED_PAIR) { |
| 224 if (pair == _HashBase._DELETED_PAIR) { | 219 if (pair == _HashBase._DELETED_PAIR) { |
| 225 if (firstDeleted < 0) { | 220 if (firstDeleted < 0) { |
| 226 firstDeleted = i; | 221 firstDeleted = i; |
| 227 } | 222 } |
| 228 } else { | 223 } else { |
| 229 final int entry = hashPattern ^ pair; | 224 final int entry = hashPattern ^ pair; |
| 230 if (entry < maxEntries) { | 225 if (entry < maxEntries) { |
| 231 final int d = entry << 1; | 226 final int d = entry << 1; |
| 232 if (_equals(key, _data[d])) { | 227 if (_equals(key, _data[d])) { |
| 233 return d + 1; | 228 return d + 1; |
| 234 } | 229 } |
| 235 } | 230 } |
| 236 } | 231 } |
| 237 i = _HashBase._nextProbe(i, sizeMask); | 232 i = _HashBase._nextProbe(i, sizeMask); |
| 238 pair = _index[i]; | 233 pair = _index[i]; |
| 239 } | 234 } |
| 240 return firstDeleted >= 0 ? -firstDeleted : -i; | 235 return firstDeleted >= 0 ? -firstDeleted : -i; |
| 241 } | 236 } |
| 242 | 237 |
| 243 void operator []=(K key, V value) { | 238 void operator[]=(K key, V value) { |
| 244 final int size = _getIndexLength(); | 239 final int size = _getIndexLength(); |
| 245 final int sizeMask = size - 1; | 240 final int sizeMask = size - 1; |
| 246 final int fullHash = _hashCode(key); | 241 final int fullHash = _hashCode(key); |
| 247 final int hashPattern = _HashBase._hashPattern(fullHash, _hashMask, size); | 242 final int hashPattern = _HashBase._hashPattern(fullHash, _hashMask, size); |
| 248 final int d = _findValueOrInsertPoint(key, fullHash, hashPattern, size); | 243 final int d = _findValueOrInsertPoint(key, fullHash, hashPattern, size); |
| 249 if (d > 0) { | 244 if (d > 0) { |
| 250 _data[d] = value; | 245 _data[d] = value; |
| 251 } else { | 246 } else { |
| 252 final int i = -d; | 247 final int i = -d; |
| 253 _insert(key, value, hashPattern, i); | 248 _insert(key, value, hashPattern, i); |
| 254 } | 249 } |
| 255 } | 250 } |
| 256 | 251 |
| 257 V putIfAbsent(K key, V ifAbsent()) { | 252 V putIfAbsent(K key, V ifAbsent()) { |
| 258 final int size = _getIndexLength(); | 253 final int size = _getIndexLength(); |
| 259 final int sizeMask = size - 1; | 254 final int sizeMask = size - 1; |
| 260 final int maxEntries = size >> 1; | 255 final int maxEntries = size >> 1; |
| 261 final int fullHash = _hashCode(key); | 256 final int fullHash = _hashCode(key); |
| 262 final int hashPattern = _HashBase._hashPattern(fullHash, _hashMask, size); | 257 final int hashPattern = _HashBase._hashPattern(fullHash, _hashMask, size); |
| 263 final int d = _findValueOrInsertPoint(key, fullHash, hashPattern, size); | 258 final int d = _findValueOrInsertPoint(key, fullHash, hashPattern, size); |
| 264 if (d > 0) { | 259 if (d > 0) { |
| 265 return _data[d]; | 260 return _data[d]; |
| 266 } | 261 } |
| 267 // 'ifAbsent' is allowed to modify the map. | 262 // 'ifAbsent' is allowed to modify the map. |
| 268 List oldData = _data; | 263 List oldData = _data; |
| 269 int oldCheckSum = _checkSum; | 264 int oldCheckSum = _checkSum; |
| 270 V value = ifAbsent(); | 265 V value = ifAbsent(); |
| 271 if (_isModifiedSince(oldData, oldCheckSum)) { | 266 if (_isModifiedSince(oldData, oldCheckSum)) { |
| 272 this[key] = value; | 267 this[key] = value; |
| 273 } else { | 268 } else { |
| 274 final int i = -d; | 269 final int i = -d; |
| 275 _insert(key, value, hashPattern, i); | 270 _insert(key, value, hashPattern, i); |
| 276 } | 271 } |
| 277 return value; | 272 return value; |
| 278 } | 273 } |
| 279 | 274 |
| 280 V remove(Object key) { | 275 V remove(Object key) { |
| 281 final int size = _getIndexLength(); | 276 final int size = _getIndexLength(); |
| 282 final int sizeMask = size - 1; | 277 final int sizeMask = size - 1; |
| 283 final int maxEntries = size >> 1; | 278 final int maxEntries = size >> 1; |
| 284 final int fullHash = _hashCode(key); | 279 final int fullHash = _hashCode(key); |
| 285 final int hashPattern = _HashBase._hashPattern(fullHash, _hashMask, size); | 280 final int hashPattern = _HashBase._hashPattern(fullHash, _hashMask, size); |
| 286 int i = _HashBase._firstProbe(fullHash, sizeMask); | 281 int i = _HashBase._firstProbe(fullHash, sizeMask); |
| 287 int pair = _index[i]; | 282 int pair = _index[i]; |
| 288 while (pair != _HashBase._UNUSED_PAIR) { | 283 while (pair != _HashBase._UNUSED_PAIR) { |
| 289 if (pair != _HashBase._DELETED_PAIR) { | 284 if (pair != _HashBase._DELETED_PAIR) { |
| 290 final int entry = hashPattern ^ pair; | 285 final int entry = hashPattern ^ pair; |
| 291 if (entry < maxEntries) { | 286 if (entry < maxEntries) { |
| 292 final int d = entry << 1; | 287 final int d = entry << 1; |
| 293 if (_equals(key, _data[d])) { | 288 if (_equals(key, _data[d])) { |
| 294 _index[i] = _HashBase._DELETED_PAIR; | 289 _index[i] = _HashBase._DELETED_PAIR; |
| 295 _HashBase._setDeletedAt(_data, d); | 290 _HashBase._setDeletedAt(_data, d); |
| 296 V value = _data[d + 1]; | 291 V value = _data[d + 1]; |
| 297 _HashBase._setDeletedAt(_data, d + 1); | 292 _HashBase._setDeletedAt(_data, d + 1); |
| 298 ++_deletedKeys; | 293 ++_deletedKeys; |
| 299 return value; | 294 return value; |
| 300 } | 295 } |
| 301 } | 296 } |
| 302 } | 297 } |
| 303 i = _HashBase._nextProbe(i, sizeMask); | 298 i = _HashBase._nextProbe(i, sizeMask); |
| 304 pair = _index[i]; | 299 pair = _index[i]; |
| 305 } | 300 } |
| 306 return null; | 301 return null; |
| 307 } | 302 } |
| 308 | 303 |
| 309 // If key is absent, return _data (which is never a value). | 304 // If key is absent, return _data (which is never a value). |
| 310 Object _getValueOrData(Object key) { | 305 Object _getValueOrData(Object key) { |
| 311 final int size = _getIndexLength(); | 306 final int size = _getIndexLength(); |
| 312 final int sizeMask = size - 1; | 307 final int sizeMask = size - 1; |
| 313 final int maxEntries = size >> 1; | 308 final int maxEntries = size >> 1; |
| 314 final int fullHash = _hashCode(key); | 309 final int fullHash = _hashCode(key); |
| 315 final int hashPattern = _HashBase._hashPattern(fullHash, _hashMask, size); | 310 final int hashPattern = _HashBase._hashPattern(fullHash, _hashMask, size); |
| 316 int i = _HashBase._firstProbe(fullHash, sizeMask); | 311 int i = _HashBase._firstProbe(fullHash, sizeMask); |
| 317 int pair = _index[i]; | 312 int pair = _index[i]; |
| 318 while (pair != _HashBase._UNUSED_PAIR) { | 313 while (pair != _HashBase._UNUSED_PAIR) { |
| 319 if (pair != _HashBase._DELETED_PAIR) { | 314 if (pair != _HashBase._DELETED_PAIR) { |
| 320 final int entry = hashPattern ^ pair; | 315 final int entry = hashPattern ^ pair; |
| 321 if (entry < maxEntries) { | 316 if (entry < maxEntries) { |
| 322 final int d = entry << 1; | 317 final int d = entry << 1; |
| 323 if (_equals(key, _data[d])) { | 318 if (_equals(key, _data[d])) { |
| 324 return _data[d + 1]; | 319 return _data[d + 1]; |
| 325 } | 320 } |
| 326 } | 321 } |
| 327 } | 322 } |
| 328 i = _HashBase._nextProbe(i, sizeMask); | 323 i = _HashBase._nextProbe(i, sizeMask); |
| 329 pair = _index[i]; | 324 pair = _index[i]; |
| 330 } | 325 } |
| 331 return _data; | 326 return _data; |
| 332 } | 327 } |
| 333 | 328 |
| 334 bool containsKey(Object key) => !identical(_data, _getValueOrData(key)); | 329 bool containsKey(Object key) => !identical(_data, _getValueOrData(key)); |
| 335 | 330 |
| 336 V operator [](Object key) { | 331 V operator[](Object key) { |
| 337 var v = _getValueOrData(key); | 332 var v = _getValueOrData(key); |
| 338 return identical(_data, v) ? null : v; | 333 return identical(_data, v) ? null : v; |
| 339 } | 334 } |
| 340 | 335 |
| 341 bool containsValue(Object value) { | 336 bool containsValue(Object value) { |
| 342 for (var v in values) { | 337 for (var v in values) { |
| 343 // Spec. says this should always use "==", also for identity maps, etc. | 338 // Spec. says this should always use "==", also for identity maps, etc. |
| 344 if (v == value) { | 339 if (v == value) { |
| 345 return true; | 340 return true; |
| 346 } | 341 } |
| 347 } | 342 } |
| 348 return false; | 343 return false; |
| 349 } | 344 } |
| 350 | 345 |
| 351 void forEach(void f(K key, V value)) { | 346 void forEach(void f(K key, V value)) { |
| 352 var ki = keys.iterator; | 347 var ki = keys.iterator; |
| 353 var vi = values.iterator; | 348 var vi = values.iterator; |
| 354 while (ki.moveNext()) { | 349 while (ki.moveNext()) { |
| 355 vi.moveNext(); | 350 vi.moveNext(); |
| 356 f(ki.current, vi.current); | 351 f(ki.current, vi.current); |
| 357 } | 352 } |
| 358 } | 353 } |
| 359 | 354 |
| 360 Iterable<K> get keys => | 355 Iterable<K> get keys => |
| 361 new _CompactIterable<K>(this, _data, _usedData, -2, 2); | 356 new _CompactIterable<K>(this, _data, _usedData, -2, 2); |
| 362 Iterable<V> get values => | 357 Iterable<V> get values => |
| 363 new _CompactIterable<V>(this, _data, _usedData, -1, 2); | 358 new _CompactIterable<V>(this, _data, _usedData, -1, 2); |
| 364 } | 359 } |
| 365 | 360 |
| 366 class _CompactLinkedIdentityHashMap<K, V> extends _HashFieldBase | 361 class _CompactLinkedIdentityHashMap<K, V> extends _HashFieldBase |
| 367 with | 362 with MapMixin<K, V>, _LinkedHashMapMixin<K, V>, _HashBase, |
| 368 MapMixin<K, V>, | 363 _IdenticalAndIdentityHashCode |
| 369 _LinkedHashMapMixin<K, V>, | |
| 370 _HashBase, | |
| 371 _IdenticalAndIdentityHashCode | |
| 372 implements LinkedHashMap<K, V> { | 364 implements LinkedHashMap<K, V> { |
| 365 |
| 373 _CompactLinkedIdentityHashMap() : super(_HashBase._INITIAL_INDEX_SIZE); | 366 _CompactLinkedIdentityHashMap() : super(_HashBase._INITIAL_INDEX_SIZE); |
| 374 } | 367 } |
| 375 | 368 |
| 376 class _CompactLinkedCustomHashMap<K, V> extends _HashFieldBase | 369 class _CompactLinkedCustomHashMap<K, V> extends _HashFieldBase |
| 377 with MapMixin<K, V>, _LinkedHashMapMixin<K, V>, _HashBase | 370 with MapMixin<K, V>, _LinkedHashMapMixin<K, V>, _HashBase |
| 378 implements LinkedHashMap<K, V> { | 371 implements LinkedHashMap<K, V> { |
| 379 final _equality; | 372 final _equality; |
| 380 final _hasher; | 373 final _hasher; |
| 381 final _validKey; | 374 final _validKey; |
| 382 | 375 |
| 383 // TODO(koda): Ask gbracha why I cannot have fields _equals/_hashCode. | 376 // TODO(koda): Ask gbracha why I cannot have fields _equals/_hashCode. |
| 384 int _hashCode(e) => _hasher(e); | 377 int _hashCode(e) => _hasher(e); |
| 385 bool _equals(e1, e2) => _equality(e1, e2); | 378 bool _equals(e1, e2) => _equality(e1, e2); |
| 386 | 379 |
| 387 bool containsKey(Object o) => _validKey(o) ? super.containsKey(o) : false; | 380 bool containsKey(Object o) => _validKey(o) ? super.containsKey(o) : false; |
| 388 V operator [](Object o) => _validKey(o) ? super[o] : null; | 381 V operator[](Object o) => _validKey(o) ? super[o] : null; |
| 389 V remove(Object o) => _validKey(o) ? super.remove(o) : null; | 382 V remove(Object o) => _validKey(o) ? super.remove(o) : null; |
| 390 | 383 |
| 391 _CompactLinkedCustomHashMap(this._equality, this._hasher, validKey) | 384 _CompactLinkedCustomHashMap(this._equality, this._hasher, validKey) |
| 392 : _validKey = (validKey != null) ? validKey : new _TypeTest<K>().test, | 385 : _validKey = (validKey != null) ? validKey : new _TypeTest<K>().test, |
| 393 super(_HashBase._INITIAL_INDEX_SIZE); | 386 super(_HashBase._INITIAL_INDEX_SIZE); |
| 394 } | 387 } |
| 395 | 388 |
| 396 // Iterates through _data[_offset + _step], _data[_offset + 2*_step], ... | 389 // Iterates through _data[_offset + _step], _data[_offset + 2*_step], ... |
| 397 // and checks for concurrent modification. | 390 // and checks for concurrent modification. |
| 398 class _CompactIterable<E> extends IterableBase<E> { | 391 class _CompactIterable<E> extends IterableBase<E> { |
| 399 final _table; | 392 final _table; |
| 400 final List _data; | 393 final List _data; |
| 401 final int _len; | 394 final int _len; |
| 402 final int _offset; | 395 final int _offset; |
| 403 final int _step; | 396 final int _step; |
| 404 | 397 |
| 405 _CompactIterable( | 398 _CompactIterable(this._table, this._data, this._len, |
| 406 this._table, this._data, this._len, this._offset, this._step); | 399 this._offset, this._step); |
| 407 | 400 |
| 408 Iterator<E> get iterator => | 401 Iterator<E> get iterator => |
| 409 new _CompactIterator<E>(_table, _data, _len, _offset, _step); | 402 new _CompactIterator<E>(_table, _data, _len, _offset, _step); |
| 410 | 403 |
| 411 int get length => _table.length; | 404 int get length => _table.length; |
| 412 bool get isEmpty => length == 0; | 405 bool get isEmpty => length == 0; |
| 413 bool get isNotEmpty => !isEmpty; | 406 bool get isNotEmpty => !isEmpty; |
| 414 } | 407 } |
| 415 | 408 |
| 416 class _CompactIterator<E> implements Iterator<E> { | 409 class _CompactIterator<E> implements Iterator<E> { |
| 417 final _table; | 410 final _table; |
| 418 final List _data; | 411 final List _data; |
| 419 final int _len; | 412 final int _len; |
| 420 int _offset; | 413 int _offset; |
| 421 final int _step; | 414 final int _step; |
| 422 final int _checkSum; | 415 final int _checkSum; |
| 423 E current; | 416 E current; |
| 424 | 417 |
| 425 _CompactIterator(table, this._data, this._len, this._offset, this._step) | 418 _CompactIterator(table, this._data, this._len, this._offset, this._step) : |
| 426 : _table = table, | 419 _table = table, _checkSum = table._checkSum; |
| 427 _checkSum = table._checkSum; | |
| 428 | 420 |
| 429 bool moveNext() { | 421 bool moveNext() { |
| 430 if (_table._isModifiedSince(_data, _checkSum)) { | 422 if (_table._isModifiedSince(_data, _checkSum)) { |
| 431 throw new ConcurrentModificationError(_table); | 423 throw new ConcurrentModificationError(_table); |
| 432 } | 424 } |
| 433 do { | 425 do { |
| 434 _offset += _step; | 426 _offset += _step; |
| 435 } while (_offset < _len && _HashBase._isDeleted(_data, _data[_offset])); | 427 } while (_offset < _len && _HashBase._isDeleted(_data, _data[_offset])); |
| 436 if (_offset < _len) { | 428 if (_offset < _len) { |
| 437 current = _data[_offset]; | 429 current = _data[_offset]; |
| 438 return true; | 430 return true; |
| 439 } else { | 431 } else { |
| 440 current = null; | 432 current = null; |
| 441 return false; | 433 return false; |
| 442 } | 434 } |
| 443 } | 435 } |
| 444 } | 436 } |
| 445 | 437 |
| 446 // Set implementation, analogous to _CompactLinkedHashMap. | 438 // Set implementation, analogous to _CompactLinkedHashMap. |
| 447 class _CompactLinkedHashSet<E> extends _HashFieldBase | 439 class _CompactLinkedHashSet<E> extends _HashFieldBase |
| 448 with _HashBase, _OperatorEqualsAndHashCode, SetMixin<E> | 440 with _HashBase, _OperatorEqualsAndHashCode, SetMixin<E> |
| 449 implements LinkedHashSet<E> { | 441 implements LinkedHashSet<E> { |
| 442 |
| 450 _CompactLinkedHashSet() : super(_HashBase._INITIAL_INDEX_SIZE >> 1) { | 443 _CompactLinkedHashSet() : super(_HashBase._INITIAL_INDEX_SIZE >> 1) { |
| 451 assert(_HashBase._UNUSED_PAIR == 0); | 444 assert(_HashBase._UNUSED_PAIR == 0); |
| 452 } | 445 } |
| 453 | 446 |
| 454 int get length => _usedData - _deletedKeys; | 447 int get length => _usedData - _deletedKeys; |
| 455 | 448 |
| 456 void _rehash() { | 449 void _rehash() { |
| 457 if ((_deletedKeys << 1) > _usedData) { | 450 if ((_deletedKeys << 1) > _usedData) { |
| 458 _init(_index.length, _hashMask, _data, _usedData); | 451 _init(_index.length, _hashMask, _data, _usedData); |
| 459 } else { | 452 } else { |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 525 final int sizeMask = size - 1; | 518 final int sizeMask = size - 1; |
| 526 final int maxEntries = size >> 1; | 519 final int maxEntries = size >> 1; |
| 527 final int fullHash = _hashCode(key); | 520 final int fullHash = _hashCode(key); |
| 528 final int hashPattern = _HashBase._hashPattern(fullHash, _hashMask, size); | 521 final int hashPattern = _HashBase._hashPattern(fullHash, _hashMask, size); |
| 529 int i = _HashBase._firstProbe(fullHash, sizeMask); | 522 int i = _HashBase._firstProbe(fullHash, sizeMask); |
| 530 int pair = _index[i]; | 523 int pair = _index[i]; |
| 531 while (pair != _HashBase._UNUSED_PAIR) { | 524 while (pair != _HashBase._UNUSED_PAIR) { |
| 532 if (pair != _HashBase._DELETED_PAIR) { | 525 if (pair != _HashBase._DELETED_PAIR) { |
| 533 final int d = hashPattern ^ pair; | 526 final int d = hashPattern ^ pair; |
| 534 if (d < maxEntries && _equals(key, _data[d])) { | 527 if (d < maxEntries && _equals(key, _data[d])) { |
| 535 return _data[d]; // Note: Must return the existing key. | 528 return _data[d]; // Note: Must return the existing key. |
| 536 } | 529 } |
| 537 } | 530 } |
| 538 i = _HashBase._nextProbe(i, sizeMask); | 531 i = _HashBase._nextProbe(i, sizeMask); |
| 539 pair = _index[i]; | 532 pair = _index[i]; |
| 540 } | 533 } |
| 541 return _data; | 534 return _data; |
| 542 } | 535 } |
| 543 | 536 |
| 544 E lookup(Object key) { | 537 E lookup(Object key) { |
| 545 var k = _getKeyOrData(key); | 538 var k = _getKeyOrData(key); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 574 | 567 |
| 575 Iterator<E> get iterator => | 568 Iterator<E> get iterator => |
| 576 new _CompactIterator<E>(this, _data, _usedData, -1, 1); | 569 new _CompactIterator<E>(this, _data, _usedData, -1, 1); |
| 577 | 570 |
| 578 // Returns a set of the same type, although this | 571 // Returns a set of the same type, although this |
| 579 // is not required by the spec. (For instance, always using an identity set | 572 // is not required by the spec. (For instance, always using an identity set |
| 580 // would be technically correct, albeit surprising.) | 573 // would be technically correct, albeit surprising.) |
| 581 Set<E> toSet() => new _CompactLinkedHashSet<E>()..addAll(this); | 574 Set<E> toSet() => new _CompactLinkedHashSet<E>()..addAll(this); |
| 582 } | 575 } |
| 583 | 576 |
| 584 class _CompactLinkedIdentityHashSet<E> extends _CompactLinkedHashSet<E> | 577 class _CompactLinkedIdentityHashSet<E> |
| 585 with _IdenticalAndIdentityHashCode { | 578 extends _CompactLinkedHashSet<E> with _IdenticalAndIdentityHashCode { |
| 586 Set<E> toSet() => new _CompactLinkedIdentityHashSet<E>()..addAll(this); | 579 Set<E> toSet() => new _CompactLinkedIdentityHashSet<E>()..addAll(this); |
| 587 } | 580 } |
| 588 | 581 |
| 589 class _CompactLinkedCustomHashSet<E> extends _CompactLinkedHashSet<E> { | 582 class _CompactLinkedCustomHashSet<E> |
| 583 extends _CompactLinkedHashSet<E> { |
| 590 final _equality; | 584 final _equality; |
| 591 final _hasher; | 585 final _hasher; |
| 592 final _validKey; | 586 final _validKey; |
| 593 | 587 |
| 594 int _hashCode(e) => _hasher(e); | 588 int _hashCode(e) => _hasher(e); |
| 595 bool _equals(e1, e2) => _equality(e1, e2); | 589 bool _equals(e1, e2) => _equality(e1, e2); |
| 596 | 590 |
| 597 bool contains(Object o) => _validKey(o) ? super.contains(o) : false; | 591 bool contains(Object o) => _validKey(o) ? super.contains(o) : false; |
| 598 E lookup(Object o) => _validKey(o) ? super.lookup(o) : null; | 592 E lookup(Object o) => _validKey(o) ? super.lookup(o) : null; |
| 599 bool remove(Object o) => _validKey(o) ? super.remove(o) : false; | 593 bool remove(Object o) => _validKey(o) ? super.remove(o) : false; |
| 600 | 594 |
| 601 _CompactLinkedCustomHashSet(this._equality, this._hasher, validKey) | 595 _CompactLinkedCustomHashSet(this._equality, this._hasher, validKey) |
| 602 : _validKey = (validKey != null) ? validKey : new _TypeTest<E>().test; | 596 : _validKey = (validKey != null) ? validKey : new _TypeTest<E>().test; |
| 603 | 597 |
| 604 Set<E> toSet() => | 598 Set<E> toSet() => |
| 605 new _CompactLinkedCustomHashSet<E>(_equality, _hasher, _validKey) | 599 new _CompactLinkedCustomHashSet<E>(_equality, _hasher, _validKey) |
| 606 ..addAll(this); | 600 ..addAll(this); |
| 607 } | 601 } |
| OLD | NEW |