| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 patch class HashMap<K, V> { | 5 patch class HashMap<K, V> { |
| 6 /* patch */ factory HashMap({ bool equals(K key1, K key2), | 6 /* patch */ factory HashMap({ bool equals(K key1, K key2), |
| 7 int hashCode(K key), | 7 int hashCode(K key), |
| 8 bool isValidKey(potentialKey) }) { | 8 bool isValidKey(potentialKey) }) { |
| 9 if (isValidKey == null) { | 9 if (isValidKey == null) { |
| 10 if (hashCode == null) { | 10 if (hashCode == null) { |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 hashCode = _defaultHashCode; | 26 hashCode = _defaultHashCode; |
| 27 } | 27 } |
| 28 if (equals == null) { | 28 if (equals == null) { |
| 29 equals = _defaultEquals; | 29 equals = _defaultEquals; |
| 30 } | 30 } |
| 31 } | 31 } |
| 32 return new _CustomHashMap<K, V>(equals, hashCode, isValidKey); | 32 return new _CustomHashMap<K, V>(equals, hashCode, isValidKey); |
| 33 } | 33 } |
| 34 | 34 |
| 35 /* patch */ factory HashMap.identity() = _IdentityHashMap<K, V>; | 35 /* patch */ factory HashMap.identity() = _IdentityHashMap<K, V>; |
| 36 |
| 37 Set<K> _newKeySet(); |
| 36 } | 38 } |
| 37 | 39 |
| 38 | 40 |
| 39 const int _MODIFICATION_COUNT_MASK = 0x3fffffff; | 41 const int _MODIFICATION_COUNT_MASK = 0x3fffffff; |
| 40 | 42 |
| 41 class _HashMap<K, V> implements HashMap<K, V> { | 43 class _HashMap<K, V> implements HashMap<K, V> { |
| 42 static const int _INITIAL_CAPACITY = 8; | 44 static const int _INITIAL_CAPACITY = 8; |
| 43 | 45 |
| 44 | 46 |
| 45 int _elementCount = 0; | 47 int _elementCount = 0; |
| (...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 217 int index = hashCode & (newLength - 1); | 219 int index = hashCode & (newLength - 1); |
| 218 entry.next = newBuckets[index]; | 220 entry.next = newBuckets[index]; |
| 219 newBuckets[index] = entry; | 221 newBuckets[index] = entry; |
| 220 entry = next; | 222 entry = next; |
| 221 } | 223 } |
| 222 } | 224 } |
| 223 _buckets = newBuckets; | 225 _buckets = newBuckets; |
| 224 } | 226 } |
| 225 | 227 |
| 226 String toString() => Maps.mapToString(this); | 228 String toString() => Maps.mapToString(this); |
| 229 |
| 230 Set<K> _newKeySet() => new _HashSet<K>(); |
| 227 } | 231 } |
| 228 | 232 |
| 229 class _CustomHashMap<K, V> extends _HashMap<K, V> { | 233 class _CustomHashMap<K, V> extends _HashMap<K, V> { |
| 230 final _Equality<K> _equals; | 234 final _Equality<K> _equals; |
| 231 final _Hasher<K> _hashCode; | 235 final _Hasher<K> _hashCode; |
| 232 final _Predicate _validKey; | 236 final _Predicate _validKey; |
| 233 _CustomHashMap(this._equals, this._hashCode, validKey) | 237 _CustomHashMap(this._equals, this._hashCode, validKey) |
| 234 : _validKey = (validKey != null) ? validKey : new _TypeTest<K>().test; | 238 : _validKey = (validKey != null) ? validKey : new _TypeTest<K>().test; |
| 235 | 239 |
| 236 | 240 |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 316 (_modificationCount + 1) & _MODIFICATION_COUNT_MASK; | 320 (_modificationCount + 1) & _MODIFICATION_COUNT_MASK; |
| 317 return entry.value; | 321 return entry.value; |
| 318 } | 322 } |
| 319 previous = entry; | 323 previous = entry; |
| 320 entry = next; | 324 entry = next; |
| 321 } | 325 } |
| 322 return null; | 326 return null; |
| 323 } | 327 } |
| 324 | 328 |
| 325 String toString() => Maps.mapToString(this); | 329 String toString() => Maps.mapToString(this); |
| 330 |
| 331 Set<K> _newKeySet() => new _CustomHashSet<K>(_equals, _hashCode, _validKey); |
| 326 } | 332 } |
| 327 | 333 |
| 328 class _IdentityHashMap<K, V> extends _HashMap<K, V> { | 334 class _IdentityHashMap<K, V> extends _HashMap<K, V> { |
| 329 | 335 |
| 330 bool containsKey(Object key) { | 336 bool containsKey(Object key) { |
| 331 int hashCode = identityHashCode(key); | 337 int hashCode = identityHashCode(key); |
| 332 List buckets = _buckets; | 338 List buckets = _buckets; |
| 333 int index = hashCode & (buckets.length - 1); | 339 int index = hashCode & (buckets.length - 1); |
| 334 _HashMapEntry entry = buckets[index]; | 340 _HashMapEntry entry = buckets[index]; |
| 335 while (entry != null) { | 341 while (entry != null) { |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 406 (_modificationCount + 1) & _MODIFICATION_COUNT_MASK; | 412 (_modificationCount + 1) & _MODIFICATION_COUNT_MASK; |
| 407 return entry.value; | 413 return entry.value; |
| 408 } | 414 } |
| 409 previous = entry; | 415 previous = entry; |
| 410 entry = next; | 416 entry = next; |
| 411 } | 417 } |
| 412 return null; | 418 return null; |
| 413 } | 419 } |
| 414 | 420 |
| 415 String toString() => Maps.mapToString(this); | 421 String toString() => Maps.mapToString(this); |
| 422 |
| 423 Set<K> _newKeySet() => new _IdentityHashSet<K>(); |
| 416 } | 424 } |
| 417 | 425 |
| 418 | 426 |
| 419 class _HashMapEntry { | 427 class _HashMapEntry { |
| 420 final key; | 428 final key; |
| 421 var value; | 429 var value; |
| 422 final int hashCode; | 430 final int hashCode; |
| 423 _HashMapEntry next; | 431 _HashMapEntry next; |
| 424 _HashMapEntry(this.key, this.value, this.hashCode, this.next); | 432 _HashMapEntry(this.key, this.value, this.hashCode, this.next); |
| 425 } | 433 } |
| 426 | 434 |
| 427 abstract class _HashMapIterable<E> extends IterableBase<E> | 435 abstract class _HashMapIterable<E> extends IterableBase<E> |
| 428 implements EfficientLength { | 436 implements EfficientLength { |
| 429 final HashMap _map; | 437 final HashMap _map; |
| 430 _HashMapIterable(this._map); | 438 _HashMapIterable(this._map); |
| 431 int get length => _map.length; | 439 int get length => _map.length; |
| 432 bool get isEmpty => _map.isEmpty; | 440 bool get isEmpty => _map.isEmpty; |
| 433 bool get isNotEmpty => _map.isNotEmpty; | 441 bool get isNotEmpty => _map.isNotEmpty; |
| 434 } | 442 } |
| 435 | 443 |
| 436 class _HashMapKeyIterable<K> extends _HashMapIterable<K> { | 444 class _HashMapKeyIterable<K> extends _HashMapIterable<K> { |
| 437 _HashMapKeyIterable(HashMap map) : super(map); | 445 _HashMapKeyIterable(HashMap map) : super(map); |
| 438 Iterator<K> get iterator => new _HashMapKeyIterator<K>(_map); | 446 Iterator<K> get iterator => new _HashMapKeyIterator<K>(_map); |
| 439 bool contains(Object key) => _map.containsKey(key); | 447 bool contains(Object key) => _map.containsKey(key); |
| 440 void forEach(void action(K key)) { | 448 void forEach(void action(K key)) { |
| 441 _map.forEach((K key, _) { | 449 _map.forEach((K key, _) { |
| 442 action(key); | 450 action(key); |
| 443 }); | 451 }); |
| 444 } | 452 } |
| 453 Set<K> toSet() => _map._newKeySet()..addAll(this); |
| 445 } | 454 } |
| 446 | 455 |
| 447 class _HashMapValueIterable<V> extends _HashMapIterable<V> { | 456 class _HashMapValueIterable<V> extends _HashMapIterable<V> { |
| 448 _HashMapValueIterable(HashMap map) : super(map); | 457 _HashMapValueIterable(HashMap map) : super(map); |
| 449 Iterator<V> get iterator => new _HashMapValueIterator<V>(_map); | 458 Iterator<V> get iterator => new _HashMapValueIterator<V>(_map); |
| 450 bool contains(Object value) => _map.containsValue(value); | 459 bool contains(Object value) => _map.containsValue(value); |
| 451 void forEach(void action(V value)) { | 460 void forEach(void action(V value)) { |
| 452 _map.forEach((_, V value) { | 461 _map.forEach((_, V value) { |
| 453 action(value); | 462 action(value); |
| 454 }); | 463 }); |
| (...skipping 373 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 828 | 837 |
| 829 class _LinkedHashMapKeyIterable<K> extends IterableBase<K> | 838 class _LinkedHashMapKeyIterable<K> extends IterableBase<K> |
| 830 implements EfficientLength { | 839 implements EfficientLength { |
| 831 LinkedHashMap<K, dynamic> _map; | 840 LinkedHashMap<K, dynamic> _map; |
| 832 _LinkedHashMapKeyIterable(this._map); | 841 _LinkedHashMapKeyIterable(this._map); |
| 833 Iterator<K> get iterator => new _LinkedHashMapKeyIterator<K>(_map); | 842 Iterator<K> get iterator => new _LinkedHashMapKeyIterator<K>(_map); |
| 834 bool contains(Object key) => _map.containsKey(key); | 843 bool contains(Object key) => _map.containsKey(key); |
| 835 bool get isEmpty => _map.isEmpty; | 844 bool get isEmpty => _map.isEmpty; |
| 836 bool get isNotEmpty => _map.isNotEmpty; | 845 bool get isNotEmpty => _map.isNotEmpty; |
| 837 int get length => _map.length; | 846 int get length => _map.length; |
| 847 Set<K> toSet() => _map._newKeySet()..addAll(this); |
| 838 } | 848 } |
| 839 | 849 |
| 840 class _LinkedHashMapValueIterable<V> extends IterableBase<V> | 850 class _LinkedHashMapValueIterable<V> extends IterableBase<V> |
| 841 implements EfficientLength { | 851 implements EfficientLength { |
| 842 LinkedHashMap<dynamic, V> _map; | 852 LinkedHashMap<dynamic, V> _map; |
| 843 _LinkedHashMapValueIterable(this._map); | 853 _LinkedHashMapValueIterable(this._map); |
| 844 Iterator<V> get iterator => new _LinkedHashMapValueIterator<V>(_map); | 854 Iterator<V> get iterator => new _LinkedHashMapValueIterator<V>(_map); |
| 845 bool contains(Object value) => _map.containsValue(value); | 855 bool contains(Object value) => _map.containsValue(value); |
| 846 bool get isEmpty => _map.isEmpty; | 856 bool get isEmpty => _map.isEmpty; |
| 847 bool get isNotEmpty => _map.isNotEmpty; | 857 bool get isNotEmpty => _map.isNotEmpty; |
| (...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1013 | 1023 |
| 1014 Iterable<K> get keys => new _LinkedHashMapKeyIterable<K>(this); | 1024 Iterable<K> get keys => new _LinkedHashMapKeyIterable<K>(this); |
| 1015 Iterable<V> get values => new _LinkedHashMapValueIterable<V>(this); | 1025 Iterable<V> get values => new _LinkedHashMapValueIterable<V>(this); |
| 1016 } | 1026 } |
| 1017 | 1027 |
| 1018 class _LinkedHashMap<K, V> extends _HashMap<K, V> | 1028 class _LinkedHashMap<K, V> extends _HashMap<K, V> |
| 1019 with _LinkedHashMapMixin<K, V> { | 1029 with _LinkedHashMapMixin<K, V> { |
| 1020 _LinkedHashMap() { | 1030 _LinkedHashMap() { |
| 1021 _nextEntry = _previousEntry = this; | 1031 _nextEntry = _previousEntry = this; |
| 1022 } | 1032 } |
| 1033 |
| 1034 Set<K> _newKeySet() => new _LinkedHashSet<K>(); |
| 1023 } | 1035 } |
| 1024 | 1036 |
| 1025 class _LinkedIdentityHashMap<K, V> extends _IdentityHashMap<K, V> | 1037 class _LinkedIdentityHashMap<K, V> extends _IdentityHashMap<K, V> |
| 1026 with _LinkedHashMapMixin<K, V> { | 1038 with _LinkedHashMapMixin<K, V> { |
| 1027 _LinkedIdentityHashMap() { | 1039 _LinkedIdentityHashMap() { |
| 1028 _nextEntry = _previousEntry = this; | 1040 _nextEntry = _previousEntry = this; |
| 1029 } | 1041 } |
| 1042 |
| 1043 Set<K> _newKeySet() => new _LinkedIdentityHashSet<K>(); |
| 1030 } | 1044 } |
| 1031 | 1045 |
| 1032 class _LinkedCustomHashMap<K, V> extends _CustomHashMap<K, V> | 1046 class _LinkedCustomHashMap<K, V> extends _CustomHashMap<K, V> |
| 1033 with _LinkedHashMapMixin<K, V> { | 1047 with _LinkedHashMapMixin<K, V> { |
| 1034 _LinkedCustomHashMap(bool equals(K key1, K key2), | 1048 _LinkedCustomHashMap(bool equals(K key1, K key2), |
| 1035 int hashCode(K key), | 1049 int hashCode(K key), |
| 1036 bool isValidKey(potentialKey)) | 1050 bool isValidKey(potentialKey)) |
| 1037 : super(equals, hashCode, isValidKey) { | 1051 : super(equals, hashCode, isValidKey) { |
| 1038 _nextEntry = _previousEntry = this; | 1052 _nextEntry = _previousEntry = this; |
| 1039 } | 1053 } |
| 1054 Set<K> _newKeySet() => |
| 1055 new _LinkedCustomHashSet<K>(_equals, _hashCode, _validKey); |
| 1040 } | 1056 } |
| 1041 | 1057 |
| 1042 | 1058 |
| 1043 patch class LinkedHashSet<E> { | 1059 patch class LinkedHashSet<E> { |
| 1044 /* patch */ factory LinkedHashSet({ bool equals(E e1, E e2), | 1060 /* patch */ factory LinkedHashSet({ bool equals(E e1, E e2), |
| 1045 int hashCode(E e), | 1061 int hashCode(E e), |
| 1046 bool isValidKey(potentialKey) }) { | 1062 bool isValidKey(potentialKey) }) { |
| 1047 if (isValidKey == null) { | 1063 if (isValidKey == null) { |
| 1048 if (hashCode == null) { | 1064 if (hashCode == null) { |
| 1049 if (equals == null) { | 1065 if (equals == null) { |
| (...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1253 return false; | 1269 return false; |
| 1254 } | 1270 } |
| 1255 _LinkedHashSetEntry entry = _next; | 1271 _LinkedHashSetEntry entry = _next; |
| 1256 _current = entry.key; | 1272 _current = entry.key; |
| 1257 _next = entry._nextEntry; | 1273 _next = entry._nextEntry; |
| 1258 return true; | 1274 return true; |
| 1259 } | 1275 } |
| 1260 | 1276 |
| 1261 E get current => _current; | 1277 E get current => _current; |
| 1262 } | 1278 } |
| OLD | NEW |