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 * 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 * | |
65 * The used `equals` and `hashCode` method should always be consistent, | |
66 * so that if `equals(a, b)` then `hashCode(a) == hashCode(b)`. The hash | |
67 * of an object, or what it compares equal to, should not change while the | |
68 * object is in the table. If it does change, the result is unpredictable. | |
69 * | |
70 * If you supply one of [equals] and [hashCode], | |
71 * you should generally also to supply the other. | |
72 */ | |
73 external factory LinkedHashMap({bool equals(K key1, K key2), | |
74 int hashCode(K key), | |
75 bool isValidKey(Object potentialKey)}); | |
76 | |
77 /** | |
78 * Creates an insertion-ordered identity-based map. | |
79 * | |
80 * Effectively a shorthand for: | |
81 * | |
82 * new LinkedHashMap(equals: identical, | |
83 * hashCode: identityHashCode) | |
84 */ | |
85 external factory LinkedHashMap.identity(); | |
86 | |
87 /** | |
88 * Creates a [LinkedHashMap] that contains all key value pairs of [other]. | |
89 */ | |
90 factory LinkedHashMap.from(Map other) { | |
91 LinkedHashMap<K, V> result = new LinkedHashMap<K, V>(); | |
92 other.forEach((k, v) { result[k as Object/*=K*/] = v as Object/*=V*/; }); | |
93 return result; | |
94 } | |
95 | |
96 /** | |
97 * Creates a [LinkedHashMap] where the keys and values are computed from the | |
98 * [iterable]. | |
99 * | |
100 * For each element of the [iterable] this constructor computes a key/value | |
101 * pair, by applying [key] and [value] respectively. | |
102 * | |
103 * The keys of the key/value pairs do not need to be unique. The last | |
104 * occurrence of a key will simply overwrite any previous value. | |
105 * | |
106 * If no values are specified for [key] and [value] the default is the | |
107 * identity function. | |
108 */ | |
109 factory LinkedHashMap.fromIterable(Iterable iterable, | |
110 {K key(element), V value(element)}) { | |
111 LinkedHashMap<K, V> map = new LinkedHashMap<K, V>(); | |
112 Maps._fillMapWithMappedIterable(map, iterable, key, value); | |
113 return map; | |
114 } | |
115 | |
116 /** | |
117 * Creates a [LinkedHashMap] associating the given [keys] to [values]. | |
118 * | |
119 * This constructor iterates over [keys] and [values] and maps each element of | |
120 * [keys] to the corresponding element of [values]. | |
121 * | |
122 * If [keys] contains the same object multiple times, the last occurrence | |
123 * overwrites the previous value. | |
124 * | |
125 * It is an error if the two [Iterable]s don't have the same length. | |
126 */ | |
127 factory LinkedHashMap.fromIterables(Iterable<K> keys, Iterable<V> values) { | |
128 LinkedHashMap<K, V> map = new LinkedHashMap<K, V>(); | |
129 Maps._fillMapWithIterables(map, keys, values); | |
130 return map; | |
131 } | |
132 } | |
OLD | NEW |