| 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 part of dart.collection; | 5 part of dart.collection; |
| 6 | 6 |
| 7 /** Default function for equality comparison in customized HashMaps */ | 7 /** Default function for equality comparison in customized HashMaps */ |
| 8 bool _defaultEquals(a, b) => a == b; | 8 bool _defaultEquals(a, b) => a == b; |
| 9 /** Default function for hash-code computation in customized HashMaps */ | 9 /** Default function for hash-code computation in customized HashMaps */ |
| 10 int _defaultHashCode(a) => a.hashCode; | 10 int _defaultHashCode(a) => a.hashCode; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 * The map allows `null` as a key. | 26 * The map allows `null` as a key. |
| 27 * | 27 * |
| 28 * Iterating the map's keys, values or entries (through [forEach]) | 28 * Iterating the map's keys, values or entries (through [forEach]) |
| 29 * may happen in any order. | 29 * may happen in any order. |
| 30 * The itearation order only changes when the map is modified. | 30 * The itearation order only changes when the map is modified. |
| 31 * Values are iterated in the same order as their associated keys, | 31 * Values are iterated in the same order as their associated keys, |
| 32 * so iterating the [keys] and [values] in parallel | 32 * so iterating the [keys] and [values] in parallel |
| 33 * will give matching key and value pairs. | 33 * will give matching key and value pairs. |
| 34 */ | 34 */ |
| 35 abstract class HashMap<K, V> implements Map<K, V> { | 35 abstract class HashMap<K, V> implements Map<K, V> { |
| 36 @patch | 36 /** |
| 37 * Creates an unordered hash-table based [Map]. |
| 38 * |
| 39 * The created map is not ordered in any way. When iterating the keys or |
| 40 * values, the iteration order is unspecified except that it will stay the |
| 41 * same as long as the map isn't changed. |
| 42 * |
| 43 * If [equals] is provided, it is used to compare the keys in the table with |
| 44 * new keys. If [equals] is omitted, the key's own [Object.operator==] is used |
| 45 * instead. |
| 46 * |
| 47 * Similar, if [hashCode] is provided, it is used to produce a hash value |
| 48 * for keys in order to place them in the hash table. If it is omitted, the |
| 49 * key's own [Object.hashCode] is used. |
| 50 * |
| 51 * If using methods like [operator[]], [remove] and [containsKey] together |
| 52 * with a custom equality and hashcode, an extra `isValidKey` function |
| 53 * can be supplied. This function is called before calling [equals] or |
| 54 * [hashCode] with an argument that may not be a [K] instance, and if the |
| 55 * call returns false, the key is assumed to not be in the set. |
| 56 * The [isValidKey] function defaults to just testing if the object is a |
| 57 * [K] instance. |
| 58 * |
| 59 * The used `equals` and `hashCode` method should always be consistent, |
| 60 * so that if `equals(a, b)` then `hashCode(a) == hashCode(b)`. The hash |
| 61 * of an object, or what it compares equal to, should not change while the |
| 62 * object is in the table. If it does change, the result is unpredictable. |
| 63 * |
| 64 * If you supply one of [equals] and [hashCode], |
| 65 * you should generally also to supply the other. |
| 66 * An example would be using [identical] and [identityHashCode], |
| 67 * which is equivalent to using the shorthand [HashMap.identity]). |
| 68 */ |
| 37 factory HashMap({ bool equals(K key1, K key2), | 69 factory HashMap({ bool equals(K key1, K key2), |
| 38 int hashCode(K key), | 70 int hashCode(K key), |
| 39 bool isValidKey(potentialKey) }) { | 71 bool isValidKey(potentialKey) }) { |
| 40 if (isValidKey == null) { | 72 if (isValidKey == null) { |
| 41 if (hashCode == null) { | 73 if (hashCode == null) { |
| 42 if (equals == null) { | 74 if (equals == null) { |
| 43 return new _HashMap<K, V>(); | 75 return new _HashMap<K, V>(); |
| 44 } | 76 } |
| 45 hashCode = _defaultHashCode; | 77 hashCode = _defaultHashCode; |
| 46 } else { | 78 } else { |
| 47 if (identical(identityHashCode, hashCode) && | 79 if (identical(identityHashCode, hashCode) && |
| 48 identical(identical, equals)) { | 80 identical(identical, equals)) { |
| 49 return new _IdentityHashMap<K, V>(); | 81 return new _IdentityHashMap<K, V>(); |
| 50 } | 82 } |
| 51 if (equals == null) { | 83 if (equals == null) { |
| 52 equals = _defaultEquals; | 84 equals = _defaultEquals; |
| 53 } | 85 } |
| 54 } | 86 } |
| 55 } else { | 87 } else { |
| 56 if (hashCode == null) { | 88 if (hashCode == null) { |
| 57 hashCode = _defaultHashCode; | 89 hashCode = _defaultHashCode; |
| 58 } | 90 } |
| 59 if (equals == null) { | 91 if (equals == null) { |
| 60 equals = _defaultEquals; | 92 equals = _defaultEquals; |
| 61 } | 93 } |
| 62 } | 94 } |
| 63 return new _CustomHashMap<K, V>(equals, hashCode, isValidKey); | 95 return new _CustomHashMap<K, V>(equals, hashCode, isValidKey); |
| 64 } | 96 } |
| 65 | 97 |
| 66 @patch | 98 /** |
| 99 * Creates an unordered identity-based map. |
| 100 * |
| 101 * Effectively a shorthand for: |
| 102 * |
| 103 * new HashMap(equals: identical, hashCode: identityHashCodeOf) |
| 104 */ |
| 67 factory HashMap.identity() = _IdentityHashMap<K, V>; | 105 factory HashMap.identity() = _IdentityHashMap<K, V>; |
| 68 | 106 |
| 69 /** | 107 /** |
| 70 * Creates a [HashMap] that contains all key/value pairs of [other]. | 108 * Creates a [HashMap] that contains all key/value pairs of [other]. |
| 71 */ | 109 */ |
| 72 factory HashMap.from(Map other) { | 110 factory HashMap.from(Map other) { |
| 73 HashMap<K, V> result = new HashMap<K, V>(); | 111 HashMap<K, V> result = new HashMap<K, V>(); |
| 74 other.forEach((k, v) { result[k] = v; }); | 112 other.forEach((k, v) { result[k] = v; }); |
| 75 return result; | 113 return result; |
| 76 } | 114 } |
| (...skipping 28 matching lines...) Expand all Loading... |
| 105 * overwrites the previous value. | 143 * overwrites the previous value. |
| 106 * | 144 * |
| 107 * It is an error if the two [Iterable]s don't have the same length. | 145 * It is an error if the two [Iterable]s don't have the same length. |
| 108 */ | 146 */ |
| 109 factory HashMap.fromIterables(Iterable<K> keys, Iterable<V> values) { | 147 factory HashMap.fromIterables(Iterable<K> keys, Iterable<V> values) { |
| 110 HashMap<K, V> map = new HashMap<K, V>(); | 148 HashMap<K, V> map = new HashMap<K, V>(); |
| 111 Maps._fillMapWithIterables(map, keys, values); | 149 Maps._fillMapWithIterables(map, keys, values); |
| 112 return map; | 150 return map; |
| 113 } | 151 } |
| 114 } | 152 } |
| OLD | NEW |