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; |
11 | 11 |
12 /** | 12 /** |
13 * A hash-table based implementation of [Map]. | 13 * A hash-table based implementation of [Map]. |
14 * | 14 * |
15 * The keys of a `HashMap` must have consistent [Object.operator==] | 15 * The keys of a `HashMap` must have consistent [Object.operator==] |
16 * and [Object.hashCode] implementations. This means that the `==` operator | 16 * and [Object.hashCode] implementations. This means that the `==` operator |
17 * must define a stable equivalence relation on the keys (reflexive, | 17 * must define a stable equivalence relation on the keys (reflexive, |
18 * anti-symmetric, transitive, and consistent over time), and that `hashCode` | 18 * anti-symmetric, transitive, and consistent over time), and that `hashCode` |
19 * must be the same for objects that are considered equal by `==`. | 19 * must be the same for objects that are considered equal by `==`. |
20 * | 20 * |
21 * The map allows `null` as a key. | 21 * The map allows `null` as a key. |
22 */ | 22 */ |
23 abstract class HashMap<K, V> implements Map<K, V> { | 23 class HashMap<K, V> implements Map<K, V> { |
24 /** | 24 /** |
25 * Creates a hash-table based [Map]. | 25 * Creates a hash-table based [Map]. |
26 * | 26 * |
27 * The created map is not ordered in any way. When iterating the keys or | 27 * The created map is not ordered in any way. When iterating the keys or |
28 * values, the iteration order is unspecified except that it will stay the | 28 * values, the iteration order is unspecified except that it will stay the |
29 * same as long as the map isn't changed. | 29 * same as long as the map isn't changed. |
30 * | 30 * |
31 * If [equals] is provided, it is used to compare the keys in the table with | 31 * If [equals] is provided, it is used to compare the keys in the table with |
32 * new keys. If [equals] is omitted, the key's own [Object.operator==] is used | 32 * new keys. If [equals] is omitted, the key's own [Object.operator==] is used |
33 * instead. | 33 * instead. |
34 * | 34 * |
35 * Similar, if [hashCode] is provided, it is used to produce a hash value | 35 * Similar, if [hashCode] is provided, it is used to produce a hash value |
36 * for keys in order to place them in the hash table. If it is omitted, the | 36 * for keys in order to place them in the hash table. If it is omitted, the |
37 * key's own [Object.hashCode] is used. | 37 * key's own [Object.hashCode] is used. |
38 * | 38 * |
39 * The used `equals` and `hashCode` method should always be consistent, | 39 * The used `equals` and `hashCode` method should always be consistent, |
40 * so that if `equals(a, b)` then `hashCode(a) == hashCode(b)`. The hash | 40 * so that if `equals(a, b)` then `hashCode(a) == hashCode(b)`. The hash |
41 * of an object, or what it compares equal to, should not change while the | 41 * of an object, or what it compares equal to, should not change while the |
42 * object is in the table. If it does change, the result is unpredictable. | 42 * object is in the table. If it does change, the result is unpredictable. |
43 * | |
44 * It is generally the case that if you supply one of [equals] and [hashCode], | |
45 * you also want to supply the other. The only common exception is to pass | |
46 * [identical] as the equality and use the default hash code. | |
47 */ | 43 */ |
48 external factory HashMap({bool equals(K key1, K key2), int hashCode(K key)}); | 44 factory HashMap({bool equals(K key1, K key2), int hashCode(K key)}) { |
| 45 if (equals != null || hashCode != null) { |
| 46 if (equals == null) { |
| 47 equals = _defaultEquals; |
| 48 } else if (hashCode == null) { |
| 49 hashCode = _defaultHashCode; |
| 50 } |
| 51 // Create new CustomHashMap<K, V>(equals, hashCode). |
| 52 throw new UnimplementedError("Not implemented yet"); |
| 53 } |
| 54 return new HashMap<K, V>._internal(); |
| 55 } |
| 56 |
| 57 /** Creates an empty `HashMap` with the default equals and hashCode. */ |
| 58 external HashMap._internal(); |
49 | 59 |
50 /** | 60 /** |
51 * Creates a [HashMap] that contains all key value pairs of [other]. | 61 * Creates a [HashMap] that contains all key value pairs of [other]. |
52 */ | 62 */ |
53 factory HashMap.from(Map<K, V> other) { | 63 factory HashMap.from(Map<K, V> other) { |
54 return new HashMap<K, V>()..addAll(other); | 64 return new HashMap<K, V>()..addAll(other); |
55 } | 65 } |
56 | 66 |
57 /** | 67 /** |
58 * Creates a [HashMap] where the keys and values are computed from the | 68 * Creates a [HashMap] where the keys and values are computed from the |
(...skipping 24 matching lines...) Expand all Loading... |
83 * If [keys] contains the same object multiple times, the last occurrence | 93 * If [keys] contains the same object multiple times, the last occurrence |
84 * overwrites the previous value. | 94 * overwrites the previous value. |
85 * | 95 * |
86 * It is an error if the two [Iterable]s don't have the same length. | 96 * It is an error if the two [Iterable]s don't have the same length. |
87 */ | 97 */ |
88 factory HashMap.fromIterables(Iterable<K> keys, Iterable<V> values) { | 98 factory HashMap.fromIterables(Iterable<K> keys, Iterable<V> values) { |
89 HashMap<K, V> map = new HashMap<K, V>(); | 99 HashMap<K, V> map = new HashMap<K, V>(); |
90 Maps._fillMapWithIterables(map, keys, values); | 100 Maps._fillMapWithIterables(map, keys, values); |
91 return map; | 101 return map; |
92 } | 102 } |
| 103 |
| 104 external int get length; |
| 105 external bool get isEmpty; |
| 106 external bool get isNotEmpty; |
| 107 |
| 108 external Iterable<K> get keys; |
| 109 external Iterable<V> get values; |
| 110 |
| 111 external bool containsKey(Object key); |
| 112 external bool containsValue(Object value); |
| 113 |
| 114 external void addAll(Map<K, V> other); |
| 115 |
| 116 external V operator [](Object key); |
| 117 external void operator []=(K key, V value); |
| 118 |
| 119 external V putIfAbsent(K key, V ifAbsent()); |
| 120 |
| 121 external V remove(Object key); |
| 122 external void clear(); |
| 123 |
| 124 external void forEach(void action(K key, V value)); |
| 125 |
| 126 String toString() => Maps.mapToString(this); |
93 } | 127 } |
OLD | NEW |