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