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 23 matching lines...) Expand all Loading... |
34 * same as long as the map isn't changed. | 34 * same as long as the map isn't changed. |
35 * | 35 * |
36 * If [equals] is provided, it is used to compare the keys in the table with | 36 * If [equals] is provided, it is used to compare the keys in the table with |
37 * new keys. If [equals] is omitted, the key's own [Object.operator==] is used | 37 * new keys. If [equals] is omitted, the key's own [Object.operator==] is used |
38 * instead. | 38 * instead. |
39 * | 39 * |
40 * Similar, if [hashCode] is provided, it is used to produce a hash value | 40 * Similar, if [hashCode] is provided, it is used to produce a hash value |
41 * for keys in order to place them in the hash table. If it is omitted, the | 41 * for keys in order to place them in the hash table. If it is omitted, the |
42 * key's own [Object.hashCode] is used. | 42 * key's own [Object.hashCode] is used. |
43 * | 43 * |
44 * If using methods like [operator[]], [remove] and [containsKey] together | |
45 * with a custom equality and hashcode, an extra `isValidKey` function | |
46 * can be supplied. This function is called before calling [equals] or | |
47 * [hashCode] with an argument that may not be a [K] instance, and if the | |
48 * call returns false, the key is assumed to not be in the set. | |
49 * The [isValidKey] function defaults to just testing if the object is a | |
50 * [K] instance. | |
51 * | |
52 * The used `equals` and `hashCode` method should always be consistent, | 44 * The used `equals` and `hashCode` method should always be consistent, |
53 * so that if `equals(a, b)` then `hashCode(a) == hashCode(b)`. The hash | 45 * so that if `equals(a, b)` then `hashCode(a) == hashCode(b)`. The hash |
54 * of an object, or what it compares equal to, should not change while the | 46 * of an object, or what it compares equal to, should not change while the |
55 * object is in the table. If it does change, the result is unpredictable. | 47 * object is in the table. If it does change, the result is unpredictable. |
56 * | 48 * |
57 * It is generally the case that if you supply one of [equals] and [hashCode], | 49 * It is generally the case that if you supply one of [equals] and [hashCode], |
58 * you also want to supply the other. The only common exception is to pass | 50 * you also want to supply the other. The only common exception is to pass |
59 * [identical] as the equality and use the default hash code. | 51 * [identical] as the equality and use the default hash code. |
60 */ | 52 */ |
61 external factory HashMap({bool equals(K key1, K key2), | 53 external factory HashMap({bool equals(K key1, K key2), int hashCode(K key)}); |
62 int hashCode(K key), | |
63 bool isValidKey(potentialKey)}); | |
64 | 54 |
65 /** | 55 /** |
66 * Creates a [HashMap] that contains all key value pairs of [other]. | 56 * Creates a [HashMap] that contains all key value pairs of [other]. |
67 */ | 57 */ |
68 factory HashMap.from(Map<K, V> other) { | 58 factory HashMap.from(Map<K, V> other) { |
69 return new HashMap<K, V>()..addAll(other); | 59 return new HashMap<K, V>()..addAll(other); |
70 } | 60 } |
71 | 61 |
72 /** | 62 /** |
73 * Creates a [HashMap] where the keys and values are computed from the | 63 * Creates a [HashMap] where the keys and values are computed from the |
(...skipping 25 matching lines...) Expand all Loading... |
99 * overwrites the previous value. | 89 * overwrites the previous value. |
100 * | 90 * |
101 * It is an error if the two [Iterable]s don't have the same length. | 91 * It is an error if the two [Iterable]s don't have the same length. |
102 */ | 92 */ |
103 factory HashMap.fromIterables(Iterable<K> keys, Iterable<V> values) { | 93 factory HashMap.fromIterables(Iterable<K> keys, Iterable<V> values) { |
104 HashMap<K, V> map = new HashMap<K, V>(); | 94 HashMap<K, V> map = new HashMap<K, V>(); |
105 Maps._fillMapWithIterables(map, keys, values); | 95 Maps._fillMapWithIterables(map, keys, values); |
106 return map; | 96 return map; |
107 } | 97 } |
108 } | 98 } |
OLD | NEW |