| 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 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 49 * key's own [Object.hashCode] is used. | 49 * key's own [Object.hashCode] is used. |
| 50 * | 50 * |
| 51 * If using methods like [operator[]], [remove] and [containsKey] together | 51 * If using methods like [operator[]], [remove] and [containsKey] together |
| 52 * with a custom equality and hashcode, an extra `isValidKey` function | 52 * with a custom equality and hashcode, an extra `isValidKey` function |
| 53 * can be supplied. This function is called before calling [equals] or | 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 | 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. | 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 | 56 * The [isValidKey] function defaults to just testing if the object is a |
| 57 * [K] instance. | 57 * [K] instance. |
| 58 * | 58 * |
| 59 * Example: |
| 60 * |
| 61 * new HashMap<int,int>(equals: (int a, int b) => (b - a) % 5 == 0, |
| 62 * hashCode: (int e) => e % 5) |
| 63 * |
| 64 * This example map does not need an `isValidKey` function to be passed. |
| 65 * The default function accepts only `int` values, which can safely be |
| 66 * passed to both the `equals` and `hashCode` functions. |
| 67 * |
| 68 * If neither `equals`, `hashCode`, nor `isValidKey` is provided, |
| 69 * the default `isValidKey` instead accepts all keys. |
| 70 * The default equality and hashcode operations are assumed to work on all |
| 71 * objects. |
| 72 * |
| 73 * Likewise, if `equals` is [identical], `hashCode` is [identityHashCode] |
| 74 * and `isValidKey` is omitted, the resulting map is identity based, |
| 75 * and the `isValidKey` defaults to accepting all keys. |
| 76 * Such a map can be created directly using [HashMap.identity]. |
| 77 * |
| 59 * The used `equals` and `hashCode` method should always be consistent, | 78 * The used `equals` and `hashCode` method should always be consistent, |
| 60 * so that if `equals(a, b)` then `hashCode(a) == hashCode(b)`. The hash | 79 * 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 | 80 * 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. | 81 * object is a key in the map. If it does change, the result is unpredictable. |
| 63 * | 82 * |
| 64 * If you supply one of [equals] and [hashCode], | 83 * If you supply one of [equals] and [hashCode], |
| 65 * you should generally also to supply the other. | 84 * 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 */ | 85 */ |
| 69 external factory HashMap({bool equals(K key1, K key2), | 86 external factory HashMap({bool equals(K key1, K key2), |
| 70 int hashCode(K key), | 87 int hashCode(K key), |
| 71 bool isValidKey(potentialKey)}); | 88 bool isValidKey(potentialKey)}); |
| 72 | 89 |
| 73 /** | 90 /** |
| 74 * Creates an unordered identity-based map. | 91 * Creates an unordered identity-based map. |
| 75 * | 92 * |
| 76 * Effectively a shorthand for: | 93 * Effectively a shorthand for: |
| 77 * | 94 * |
| 78 * new HashMap(equals: identical, hashCode: identityHashCodeOf) | 95 * new HashMap(equals: identical, |
| 96 * hashCode: identityHashCodeOf) |
| 79 */ | 97 */ |
| 80 external factory HashMap.identity(); | 98 external factory HashMap.identity(); |
| 81 | 99 |
| 82 /** | 100 /** |
| 83 * Creates a [HashMap] that contains all key/value pairs of [other]. | 101 * Creates a [HashMap] that contains all key/value pairs of [other]. |
| 84 */ | 102 */ |
| 85 factory HashMap.from(Map other) { | 103 factory HashMap.from(Map other) { |
| 86 HashMap<K, V> result = new HashMap<K, V>(); | 104 HashMap<K, V> result = new HashMap<K, V>(); |
| 87 other.forEach((k, v) { result[k] = v; }); | 105 other.forEach((k, v) { result[k] = v; }); |
| 88 return result; | 106 return result; |
| (...skipping 29 matching lines...) Expand all Loading... |
| 118 * overwrites the previous value. | 136 * overwrites the previous value. |
| 119 * | 137 * |
| 120 * It is an error if the two [Iterable]s don't have the same length. | 138 * It is an error if the two [Iterable]s don't have the same length. |
| 121 */ | 139 */ |
| 122 factory HashMap.fromIterables(Iterable<K> keys, Iterable<V> values) { | 140 factory HashMap.fromIterables(Iterable<K> keys, Iterable<V> values) { |
| 123 HashMap<K, V> map = new HashMap<K, V>(); | 141 HashMap<K, V> map = new HashMap<K, V>(); |
| 124 Maps._fillMapWithIterables(map, keys, values); | 142 Maps._fillMapWithIterables(map, keys, values); |
| 125 return map; | 143 return map; |
| 126 } | 144 } |
| 127 } | 145 } |
| OLD | NEW |