OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 part of dart.collection; | |
6 | |
7 /** Default function for equality comparison in customized HashMaps */ | |
8 bool _defaultEquals(a, b) => a == b; | |
9 /** Default function for hash-code computation in customized HashMaps */ | |
10 int _defaultHashCode(a) => a.hashCode; | |
11 | |
12 /** Type of custom equality function */ | |
13 typedef bool _Equality<K>(K a, K b); | |
14 /** Type of custom hash code function. */ | |
15 typedef int _Hasher<K>(K object); | |
16 | |
17 /** | |
18 * A hash-table based implementation of [Map]. | |
19 * | |
20 * The keys of a `HashMap` must have consistent [Object.operator==] | |
21 * and [Object.hashCode] implementations. This means that the `==` operator | |
22 * must define a stable equivalence relation on the keys (reflexive, | |
23 * symmetric, transitive, and consistent over time), and that `hashCode` | |
24 * must be the same for objects that are considered equal by `==`. | |
25 * | |
26 * The map allows `null` as a key. | |
27 * | |
28 * Iterating the map's keys, values or entries (through [forEach]) | |
29 * may happen in any order. | |
30 * The iteration order only changes when the map is modified. | |
31 * Values are iterated in the same order as their associated keys, | |
32 * so iterating the [keys] and [values] in parallel | |
33 * will give matching key and value pairs. | |
34 */ | |
35 abstract class HashMap<K, V> implements Map<K, V> { | |
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 * 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 * | |
78 * The used `equals` and `hashCode` method should always be consistent, | |
79 * so that if `equals(a, b)` then `hashCode(a) == hashCode(b)`. The hash | |
80 * of an object, or what it compares equal to, should not change while the | |
81 * object is a key in the map. If it does change, the result is unpredictable. | |
82 * | |
83 * If you supply one of [equals] and [hashCode], | |
84 * you should generally also to supply the other. | |
85 */ | |
86 external factory HashMap({bool equals(K key1, K key2), | |
87 int hashCode(K key), | |
88 bool isValidKey(potentialKey)}); | |
89 | |
90 /** | |
91 * Creates an unordered identity-based map. | |
92 * | |
93 * Effectively a shorthand for: | |
94 * | |
95 * new HashMap(equals: identical, | |
96 * hashCode: identityHashCode) | |
97 */ | |
98 external factory HashMap.identity(); | |
99 | |
100 /** | |
101 * Creates a [HashMap] that contains all key/value pairs of [other]. | |
102 */ | |
103 factory HashMap.from(Map other) { | |
104 HashMap<K, V> result = new HashMap<K, V>(); | |
105 other.forEach((k, v) { result[k as Object/*=K*/] = v as Object/*=V*/; }); | |
106 return result; | |
107 } | |
108 | |
109 /** | |
110 * Creates a [HashMap] where the keys and values are computed from the | |
111 * [iterable]. | |
112 * | |
113 * For each element of the [iterable] this constructor computes a key/value | |
114 * pair, by applying [key] and [value] respectively. | |
115 * | |
116 * The keys of the key/value pairs do not need to be unique. The last | |
117 * occurrence of a key will simply overwrite any previous value. | |
118 * | |
119 * If no values are specified for [key] and [value] the default is the | |
120 * identity function. | |
121 */ | |
122 factory HashMap.fromIterable(Iterable iterable, | |
123 {K key(element), V value(element)}) { | |
124 HashMap<K, V> map = new HashMap<K, V>(); | |
125 Maps._fillMapWithMappedIterable(map, iterable, key, value); | |
126 return map; | |
127 } | |
128 | |
129 /** | |
130 * Creates a [HashMap] associating the given [keys] to [values]. | |
131 * | |
132 * This constructor iterates over [keys] and [values] and maps each element of | |
133 * [keys] to the corresponding element of [values]. | |
134 * | |
135 * If [keys] contains the same object multiple times, the last occurrence | |
136 * overwrites the previous value. | |
137 * | |
138 * It is an error if the two [Iterable]s don't have the same length. | |
139 */ | |
140 factory HashMap.fromIterables(Iterable<K> keys, Iterable<V> values) { | |
141 HashMap<K, V> map = new HashMap<K, V>(); | |
142 Maps._fillMapWithIterables(map, keys, values); | |
143 return map; | |
144 } | |
145 } | |
OLD | NEW |