| 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, |
| 11 * and keys are iterated in the order they were inserted into the map. | 11 * and keys are iterated in the order they were inserted into the map. |
| 12 * Values are iterated in their corresponding key's order. | 12 * Values are iterated in their corresponding key's order. |
| 13 * Changing a key's value, when the key is already in the map, | 13 * Changing a key's value, when the key is already in the map, |
| 14 * does not change the iteration order, | 14 * does not change the iteration order, |
| 15 * but removing the key and adding it again | 15 * but removing the key and adding it again |
| 16 * will make it be last in the iteration order. | 16 * will make it be last in the iteration order. |
| 17 * | 17 * |
| 18 * The keys of a `LinkedHashMap` must have consistent [Object.operator==] | 18 * The keys of a `LinkedHashMap` must have consistent [Object.operator==] |
| 19 * and [Object.hashCode] implementations. This means that the `==` operator | 19 * and [Object.hashCode] implementations. This means that the `==` operator |
| 20 * must define a stable equivalence relation on the keys (reflexive, | 20 * must define a stable equivalence relation on the keys (reflexive, |
| 21 * symmetric, transitive, and consistent over time), and that `hashCode` | 21 * symmetric, transitive, and consistent over time), and that `hashCode` |
| 22 * must be the same for objects that are considered equal by `==`. | 22 * must be the same for objects that are considered equal by `==`. |
| 23 * | 23 * |
| 24 * The map allows `null` as a key. | 24 * The map allows `null` as a key. |
| 25 */ | 25 */ |
| 26 abstract class LinkedHashMap<K, V> implements HashMap<K, V> { | 26 abstract class LinkedHashMap<K, V> implements HashMap<K, V> { |
| 27 @patch | 27 /** |
| 28 * Creates an insertion-ordered hash-table based [Map]. |
| 29 * |
| 30 * If [equals] is provided, it is used to compare the keys in the table with |
| 31 * new keys. If [equals] is omitted, the key's own [Object.operator==] is used |
| 32 * instead. |
| 33 * |
| 34 * Similar, if [hashCode] is provided, it is used to produce a hash value |
| 35 * for keys in order to place them in the hash table. If it is omitted, the |
| 36 * key's own [Object.hashCode] is used. |
| 37 * |
| 38 * If using methods like [operator[]], [remove] and [containsKey] together |
| 39 * with a custom equality and hashcode, an extra `isValidKey` function |
| 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 |
| 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 |
| 44 * [K] instance. |
| 45 * |
| 46 * The used `equals` and `hashCode` method should always be consistent, |
| 47 * 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 |
| 49 * object is in the table. If it does change, the result is unpredictable. |
| 50 * |
| 51 * If you supply one of [equals] and [hashCode], |
| 52 * 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 */ |
| 28 factory LinkedHashMap({ bool equals(K key1, K key2), | 56 factory LinkedHashMap({ bool equals(K key1, K key2), |
| 29 int hashCode(K key), | 57 int hashCode(K key), |
| 30 bool isValidKey(potentialKey) }) { | 58 bool isValidKey(potentialKey) }) { |
| 31 if (isValidKey == null) { | 59 if (isValidKey == null) { |
| 32 if (hashCode == null) { | 60 if (hashCode == null) { |
| 33 if (equals == null) { | 61 if (equals == null) { |
| 34 return new _LinkedHashMap<K, V>(); | 62 return new _LinkedHashMap<K, V>(); |
| 35 } | 63 } |
| 36 hashCode = _defaultHashCode; | 64 hashCode = _defaultHashCode; |
| 37 } else { | 65 } else { |
| 38 if (identical(identityHashCode, hashCode) && | 66 if (identical(identityHashCode, hashCode) && |
| 39 identical(identical, equals)) { | 67 identical(identical, equals)) { |
| 40 return new _LinkedIdentityHashMap<K, V>(); | 68 return new _LinkedIdentityHashMap<K, V>(); |
| 41 } | 69 } |
| 42 if (equals == null) { | 70 if (equals == null) { |
| 43 equals = _defaultEquals; | 71 equals = _defaultEquals; |
| 44 } | 72 } |
| 45 } | 73 } |
| 46 } else { | 74 } else { |
| 47 if (hashCode == null) { | 75 if (hashCode == null) { |
| 48 hashCode = _defaultHashCode; | 76 hashCode = _defaultHashCode; |
| 49 } | 77 } |
| 50 if (equals == null) { | 78 if (equals == null) { |
| 51 equals = _defaultEquals; | 79 equals = _defaultEquals; |
| 52 } | 80 } |
| 53 } | 81 } |
| 54 return new _LinkedCustomHashMap<K, V>(equals, hashCode, isValidKey); | 82 return new _LinkedCustomHashMap<K, V>(equals, hashCode, isValidKey); |
| 55 } | 83 } |
| 56 | 84 |
| 57 @patch | 85 /** |
| 86 * Creates an insertion-ordered identity-based map. |
| 87 * |
| 88 * Effectively a shorthand for: |
| 89 * |
| 90 * new LinkedHashMap(equals: identical, hashCode: identityHashCodeOf) |
| 91 */ |
| 58 factory LinkedHashMap.identity() = _LinkedIdentityHashMap<K, V>; | 92 factory LinkedHashMap.identity() = _LinkedIdentityHashMap<K, V>; |
| 59 | 93 |
| 60 /** | 94 /** |
| 61 * Creates a [LinkedHashMap] that contains all key value pairs of [other]. | 95 * Creates a [LinkedHashMap] that contains all key value pairs of [other]. |
| 62 */ | 96 */ |
| 63 factory LinkedHashMap.from(Map other) { | 97 factory LinkedHashMap.from(Map other) { |
| 64 LinkedHashMap<K, V> result = new LinkedHashMap<K, V>(); | 98 LinkedHashMap<K, V> result = new LinkedHashMap<K, V>(); |
| 65 other.forEach((k, v) { result[k] = v; }); | 99 other.forEach((k, v) { result[k] = v; }); |
| 66 return result; | 100 return result; |
| 67 } | 101 } |
| (...skipping 28 matching lines...) Expand all Loading... |
| 96 * overwrites the previous value. | 130 * overwrites the previous value. |
| 97 * | 131 * |
| 98 * It is an error if the two [Iterable]s don't have the same length. | 132 * It is an error if the two [Iterable]s don't have the same length. |
| 99 */ | 133 */ |
| 100 factory LinkedHashMap.fromIterables(Iterable<K> keys, Iterable<V> values) { | 134 factory LinkedHashMap.fromIterables(Iterable<K> keys, Iterable<V> values) { |
| 101 LinkedHashMap<K, V> map = new LinkedHashMap<K, V>(); | 135 LinkedHashMap<K, V> map = new LinkedHashMap<K, V>(); |
| 102 Maps._fillMapWithIterables(map, keys, values); | 136 Maps._fillMapWithIterables(map, keys, values); |
| 103 return map; | 137 return map; |
| 104 } | 138 } |
| 105 } | 139 } |
| OLD | NEW |