| 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 /** | |
| 8 * A hash-table based implementation of [Map]. | |
| 9 * | |
| 10 * The insertion order of keys is remembered, | |
| 11 * and keys are iterated in the order they were inserted into the map. | |
| 12 * Values are iterated in their corresponding key's order. | |
| 13 * Changing a key's value, when the key is already in the map, | |
| 14 * does not change the iteration order, | |
| 15 * but removing the key and adding it again | |
| 16 * will make it be last in the iteration order. | |
| 17 * | |
| 18 * The keys of a `LinkedHashMap` must have consistent [Object.operator==] | |
| 19 * and [Object.hashCode] implementations. This means that the `==` operator | |
| 20 * must define a stable equivalence relation on the keys (reflexive, | |
| 21 * symmetric, transitive, and consistent over time), and that `hashCode` | |
| 22 * must be the same for objects that are considered equal by `==`. | |
| 23 * | |
| 24 * The map allows `null` as a key. | |
| 25 */ | |
| 26 abstract class LinkedHashMap<K, V> implements HashMap<K, V> { | |
| 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 */ | |
| 56 factory LinkedHashMap({ bool equals(K key1, K key2), | |
| 57 int hashCode(K key), | |
| 58 bool isValidKey(Object potentialKey) }) { | |
| 59 if (isValidKey == null) { | |
| 60 if (hashCode == null) { | |
| 61 if (equals == null) { | |
| 62 return new _LinkedHashMap<K, V>(); | |
| 63 } | |
| 64 hashCode = _defaultHashCode; | |
| 65 } else { | |
| 66 if (identical(identityHashCode, hashCode) && | |
| 67 identical(identical, equals)) { | |
| 68 return new _LinkedIdentityHashMap<K, V>(); | |
| 69 } | |
| 70 if (equals == null) { | |
| 71 equals = _defaultEquals; | |
| 72 } | |
| 73 } | |
| 74 } else { | |
| 75 if (hashCode == null) { | |
| 76 hashCode = _defaultHashCode; | |
| 77 } | |
| 78 if (equals == null) { | |
| 79 equals = _defaultEquals; | |
| 80 } | |
| 81 } | |
| 82 return new _LinkedCustomHashMap<K, V>(equals, hashCode, isValidKey); | |
| 83 } | |
| 84 | |
| 85 /** | |
| 86 * Creates an insertion-ordered identity-based map. | |
| 87 * | |
| 88 * Effectively a shorthand for: | |
| 89 * | |
| 90 * new LinkedHashMap(equals: identical, hashCode: identityHashCodeOf) | |
| 91 */ | |
| 92 factory LinkedHashMap.identity() = _LinkedIdentityHashMap<K, V>; | |
| 93 | |
| 94 /** | |
| 95 * Creates a [LinkedHashMap] that contains all key value pairs of [other]. | |
| 96 */ | |
| 97 factory LinkedHashMap.from(Map other) { | |
| 98 LinkedHashMap<K, V> result = new LinkedHashMap<K, V>(); | |
| 99 other.forEach((k, v) { result[k] = v; }); | |
| 100 return result; | |
| 101 } | |
| 102 | |
| 103 /** | |
| 104 * Creates a [LinkedHashMap] where the keys and values are computed from the | |
| 105 * [iterable]. | |
| 106 * | |
| 107 * For each element of the [iterable] this constructor computes a key/value | |
| 108 * pair, by applying [key] and [value] respectively. | |
| 109 * | |
| 110 * The keys of the key/value pairs do not need to be unique. The last | |
| 111 * occurrence of a key will simply overwrite any previous value. | |
| 112 * | |
| 113 * If no values are specified for [key] and [value] the default is the | |
| 114 * identity function. | |
| 115 */ | |
| 116 factory LinkedHashMap.fromIterable(Iterable iterable, | |
| 117 {K key(element), V value(element)}) { | |
| 118 LinkedHashMap<K, V> map = new LinkedHashMap<K, V>(); | |
| 119 Maps._fillMapWithMappedIterable(map, iterable, key, value); | |
| 120 return map; | |
| 121 } | |
| 122 | |
| 123 /** | |
| 124 * Creates a [LinkedHashMap] associating the given [keys] to [values]. | |
| 125 * | |
| 126 * This constructor iterates over [keys] and [values] and maps each element of | |
| 127 * [keys] to the corresponding element of [values]. | |
| 128 * | |
| 129 * If [keys] contains the same object multiple times, the last occurrence | |
| 130 * overwrites the previous value. | |
| 131 * | |
| 132 * It is an error if the two [Iterable]s don't have the same length. | |
| 133 */ | |
| 134 factory LinkedHashMap.fromIterables(Iterable<K> keys, Iterable<V> values) { | |
| 135 LinkedHashMap<K, V> map = new LinkedHashMap<K, V>(); | |
| 136 Maps._fillMapWithIterables(map, keys, values); | |
| 137 return map; | |
| 138 } | |
| 139 | |
| 140 @NoInline() | |
| 141 factory LinkedHashMap._literal(List keyValuePairs) { | |
| 142 return fillLiteralMap(keyValuePairs, new _LinkedHashMap<K, V>()); | |
| 143 } | |
| 144 | |
| 145 @NoThrows() @NoInline() | |
| 146 factory LinkedHashMap._empty() { | |
| 147 return new _LinkedHashMap<K, V>(); | |
| 148 } | |
| 149 } | |
| OLD | NEW |