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 /** | 7 /** |
8 * A hash-table based implementation of [Map]. | 8 * A hash-table based implementation of [Map]. |
9 * | 9 * |
10 * The insertion order of keys is remembered, | 10 * The insertion order of keys is remembered, |
(...skipping 25 matching lines...) Expand all Loading... |
36 * key's own [Object.hashCode] is used. | 36 * key's own [Object.hashCode] is used. |
37 * | 37 * |
38 * If using methods like [operator[]], [remove] and [containsKey] together | 38 * If using methods like [operator[]], [remove] and [containsKey] together |
39 * with a custom equality and hashcode, an extra `isValidKey` function | 39 * with a custom equality and hashcode, an extra `isValidKey` function |
40 * can be supplied. This function is called before calling [equals] or | 40 * can be supplied. This function is called before calling [equals] or |
41 * [hashCode] with an argument that may not be a [K] instance, and if the | 41 * [hashCode] with an argument that may not be a [K] instance, and if the |
42 * call returns false, the key is assumed to not be in the set. | 42 * call returns false, the key is assumed to not be in the set. |
43 * The [isValidKey] function defaults to just testing if the object is a | 43 * The [isValidKey] function defaults to just testing if the object is a |
44 * [K] instance. | 44 * [K] instance. |
45 * | 45 * |
| 46 * Example: |
| 47 * |
| 48 * new LinkedHashMap<int,int>(equals: (int a, int b) => (b - a) % 5 == 0, |
| 49 * hashCode: (int e) => e % 5) |
| 50 * |
| 51 * This example map does not need an `isValidKey` function to be passed. |
| 52 * The default function accepts only `int` values, which can safely be |
| 53 * passed to both the `equals` and `hashCode` functions. |
| 54 * |
| 55 * If neither `equals`, `hashCode`, nor `isValidKey` is provided, |
| 56 * the default `isValidKey` instead accepts all keys. |
| 57 * The default equality and hashcode operations are assumed to work on all |
| 58 * objects. |
| 59 * |
| 60 * Likewise, if `equals` is [identical], `hashCode` is [identityHashCode] |
| 61 * and `isValidKey` is omitted, the resulting map is identity based, |
| 62 * and the `isValidKey` defaults to accepting all keys. |
| 63 * Such a map can be created directly using [LinkedHashMap.identity]. |
| 64 * |
46 * The used `equals` and `hashCode` method should always be consistent, | 65 * The used `equals` and `hashCode` method should always be consistent, |
47 * so that if `equals(a, b)` then `hashCode(a) == hashCode(b)`. The hash | 66 * so that if `equals(a, b)` then `hashCode(a) == hashCode(b)`. The hash |
48 * of an object, or what it compares equal to, should not change while the | 67 * of an object, or what it compares equal to, should not change while the |
49 * object is in the table. If it does change, the result is unpredictable. | 68 * object is in the table. If it does change, the result is unpredictable. |
50 * | 69 * |
51 * If you supply one of [equals] and [hashCode], | 70 * If you supply one of [equals] and [hashCode], |
52 * you should generally also to supply the other. | 71 * you should generally also to supply the other. |
53 * An example would be using [identical] and [identityHashCode], | |
54 * which is equivalent to using the shorthand [LinkedHashMap.identity]). | |
55 */ | 72 */ |
56 external factory LinkedHashMap({ bool equals(K key1, K key2), | 73 external factory LinkedHashMap({bool equals(K key1, K key2), |
57 int hashCode(K key), | 74 int hashCode(K key), |
58 bool isValidKey(potentialKey) }); | 75 bool isValidKey(potentialKey)}); |
59 | 76 |
60 /** | 77 /** |
61 * Creates an insertion-ordered identity-based map. | 78 * Creates an insertion-ordered identity-based map. |
62 * | 79 * |
63 * Effectively a shorthand for: | 80 * Effectively a shorthand for: |
64 * | 81 * |
65 * new LinkedHashMap(equals: identical, hashCode: identityHashCodeOf) | 82 * new LinkedHashMap(equals: identical, |
| 83 * hashCode: identityHashCodeOf) |
66 */ | 84 */ |
67 external factory LinkedHashMap.identity(); | 85 external factory LinkedHashMap.identity(); |
68 | 86 |
69 /** | 87 /** |
70 * Creates a [LinkedHashMap] that contains all key value pairs of [other]. | 88 * Creates a [LinkedHashMap] that contains all key value pairs of [other]. |
71 */ | 89 */ |
72 factory LinkedHashMap.from(Map other) { | 90 factory LinkedHashMap.from(Map other) { |
73 LinkedHashMap<K, V> result = new LinkedHashMap<K, V>(); | 91 LinkedHashMap<K, V> result = new LinkedHashMap<K, V>(); |
74 other.forEach((k, v) { result[k] = v; }); | 92 other.forEach((k, v) { result[k] = v; }); |
75 return result; | 93 return result; |
(...skipping 29 matching lines...) Expand all Loading... |
105 * overwrites the previous value. | 123 * overwrites the previous value. |
106 * | 124 * |
107 * It is an error if the two [Iterable]s don't have the same length. | 125 * It is an error if the two [Iterable]s don't have the same length. |
108 */ | 126 */ |
109 factory LinkedHashMap.fromIterables(Iterable<K> keys, Iterable<V> values) { | 127 factory LinkedHashMap.fromIterables(Iterable<K> keys, Iterable<V> values) { |
110 LinkedHashMap<K, V> map = new LinkedHashMap<K, V>(); | 128 LinkedHashMap<K, V> map = new LinkedHashMap<K, V>(); |
111 Maps._fillMapWithIterables(map, keys, values); | 129 Maps._fillMapWithIterables(map, keys, values); |
112 return map; | 130 return map; |
113 } | 131 } |
114 } | 132 } |
OLD | NEW |