Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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.core; | 5 part of dart.core; |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * An unordered collection of key-value pairs, | 8 * An unordered collection of key-value pairs, from which you retrieve a value |
| 9 * from which you retrieve a value by using its associated key. | 9 * by using its associated key. |
|
Lasse Reichstein Nielsen
2013/09/24 08:29:08
Do we want a single-line summary for classes as we
Kathy Walrath
2013/09/25 18:11:09
Yes, everything needs a single-line summary.
| |
| 10 * | 10 * |
| 11 * Each key can occur at most once in a map. | 11 * Each key can occur at most once in a map. |
| 12 */ | 12 */ |
| 13 abstract class Map<K, V> { | 13 abstract class Map<K, V> { |
| 14 /** | 14 /** |
| 15 * Creates a Map instance with the default implementation. | 15 * Creates a Map instance with the default implementation. |
| 16 */ | 16 */ |
| 17 factory Map() = LinkedHashMap<K, V>; | 17 factory Map() = LinkedHashMap<K, V>; |
| 18 | 18 |
| 19 /** | 19 /** |
| 20 * Creates a Map instance that contains all key-value pairs of [other]. | 20 * Creates a Map instance that contains all key-value pairs of [other]. |
| 21 */ | 21 */ |
| 22 factory Map.from(Map<K, V> other) = LinkedHashMap<K, V>.from; | 22 factory Map.from(Map<K, V> other) = LinkedHashMap<K, V>.from; |
| 23 | 23 |
| 24 /** | 24 /** |
| 25 * Creates a Map instance | 25 * Creates a Map instance in which the keys and values are computed from the |
| 26 * where the keys and values are computed from the [iterable]. | 26 * [iterable]. |
| 27 * | 27 * |
| 28 * For each element of the [iterable] this constructor computes a key-value | 28 * For each element of the [iterable] this constructor computes a key-value |
| 29 * pair, by applying [key] and [value] respectively. | 29 * pair, by applying [key] and [value] respectively. |
| 30 * | 30 * |
| 31 * The keys computed by the source [iterable] | 31 * The example below creates a new Map from a List. The keys of `map` are |
| 32 * do not need to be unique. The last | 32 * `list` values converted to strings, and the values of the `map` are the |
| 33 * occurrence of a key will simply overwrite any previous value. | 33 * squares of the `list` values: |
| 34 * | |
| 35 * List<int> list = [1, 2, 3]; | |
| 36 * Map<String, int> map = new Map.fromIterable(list, | |
| 37 * key: (item) => item.toString(), | |
| 38 * value: (item) => item * item)); | |
| 39 * | |
| 40 * map['1'] + map['2']; // 1 + 4 | |
| 41 * map['3'] - map['2']; // 9 - 4 | |
| 34 * | 42 * |
| 35 * If no values are specified for [key] and [value] the default is the | 43 * If no values are specified for [key] and [value] the default is the |
|
floitsch
2013/09/23 12:16:07
or [value]
| |
| 36 * identity function. | 44 * identity function. |
| 45 * | |
| 46 * In the following example, the keys and corresponding values of `map` | |
| 47 * are `list` values: | |
| 48 * | |
| 49 * map = new Map.fromIterable(list); | |
|
floitsch
2013/09/23 12:16:07
Maybe:
Since this is not really a useful example:
shailentuli
2013/09/27 14:27:49
The point of the example is to show what happens i
| |
| 50 * map[1] + map[2]; // 1 + 2 | |
| 51 * map[3] - map[2]; // 3 - 2 | |
| 52 * | |
| 53 * The keys computed by the source [iterable] do not need to be unique. The | |
| 54 * last occurrence of a key will simply overwrite any previous value. | |
| 37 */ | 55 */ |
| 38 factory Map.fromIterable(Iterable iterable, | 56 factory Map.fromIterable(Iterable iterable, |
| 39 {K key(element), V value(element)}) = LinkedHashMap<K, V>.fromIterable; | 57 {K key(element), V value(element)}) = LinkedHashMap<K, V>.fromIterable; |
| 40 | 58 |
| 41 /** | 59 /** |
| 42 * Creates a Map instance associating the given [keys] to [values]. | 60 * Creates a Map instance associating the given [keys] to [values]. |
| 43 * | 61 * |
| 44 * This constructor iterates over [keys] and [values] and maps each element of | 62 * This constructor iterates over [keys] and [values] and maps each element of |
| 45 * [keys] to the corresponding element of [values]. | 63 * [keys] to the corresponding element of [values]. |
| 46 * | 64 * |
| 65 * List<String> letters = ['b', 'c']; | |
| 66 * List<String> words = ['bad', 'cat']; | |
| 67 * Map<String, String> map = new Map.fromIterables(letters, words); | |
| 68 * map['b'] == 'bad' && map['c'] == 'cat'; // true | |
|
floitsch
2013/09/23 12:16:07
I would prefer:
map['b']; // 'bad'
map['c']; //
shailentuli
2013/09/27 14:27:49
I've changed this. I generally try to avoid extrem
| |
| 69 * | |
| 47 * If [keys] contains the same object multiple times, the last occurrence | 70 * If [keys] contains the same object multiple times, the last occurrence |
| 48 * overwrites the previous value. | 71 * overwrites the previous value. |
| 49 * | 72 * |
| 50 * It is an error if the two [Iterable]s don't have the same length. | 73 * It is an error if the two [Iterable]s don't have the same length. |
| 51 */ | 74 */ |
| 52 factory Map.fromIterables(Iterable<K> keys, Iterable<V> values) | 75 factory Map.fromIterables(Iterable<K> keys, Iterable<V> values) |
| 53 = LinkedHashMap<K, V>.fromIterables; | 76 = LinkedHashMap<K, V>.fromIterables; |
| 54 | 77 |
| 55 /** | 78 /** |
| 56 * Returns true if this map contains the given value. | 79 * Returns true if this map contains the given value. |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 73 /** | 96 /** |
| 74 * Associates the [key] with the given [value]. | 97 * Associates the [key] with the given [value]. |
| 75 */ | 98 */ |
| 76 void operator []=(K key, V value); | 99 void operator []=(K key, V value); |
| 77 | 100 |
| 78 /** | 101 /** |
| 79 * If [key] is not associated to a value, calls [ifAbsent] and | 102 * If [key] is not associated to a value, calls [ifAbsent] and |
| 80 * updates the map by mapping [key] to the value returned by | 103 * updates the map by mapping [key] to the value returned by |
| 81 * [ifAbsent]. Returns the value in the map. | 104 * [ifAbsent]. Returns the value in the map. |
| 82 * | 105 * |
| 83 * It is an error to add or remove keys from the map during the call to | 106 * Map<String, int> scores = {'Bob': 36}; |
| 84 * [ifAbsent]. | 107 * for (var key in ['Bob', 'Rohan', 'Sophena']) { |
| 108 * scores.putIfAbsent(key, () => 25); | |
|
floitsch
2013/09/23 12:16:07
Maybe put an example that allocates something (or
shailentuli
2013/09/27 14:27:49
Done.
| |
| 109 * } | |
| 110 * scores['Bob']; // 36 | |
| 111 * scores['Rohan']; // 25 | |
| 112 * scores['Sophena']; // 25 | |
| 113 * | |
| 114 * An error occurs if you try to add or remove keys from the map during the | |
|
floitsch
2013/09/23 12:16:07
We generally write "it is an error". Why do you pr
Lasse Reichstein Nielsen
2013/09/24 08:29:08
I agree with Florian.
We write "It is an error" bu
Kathy Walrath
2013/09/25 18:11:09
I talked with Florian about this on another thread
shailentuli
2013/09/27 14:27:49
I'm using Kathy's version, but I'm not deeply atta
| |
| 115 * call to [ifAbsent]. | |
| 85 */ | 116 */ |
| 86 V putIfAbsent(K key, V ifAbsent()); | 117 V putIfAbsent(K key, V ifAbsent()); |
| 87 | 118 |
| 88 /** | 119 /** |
| 89 * Adds all key-value pairs of [other] to this map. | 120 * Adds all key-value pairs of [other] to this map. |
| 90 * | 121 * |
| 91 * If a key of [other] is already in this map, its value is overwritten. | 122 * If a key of [other] is already in this map, its value is overwritten. |
| 92 * | 123 * |
| 93 * The operation is equivalent to doing `this[key] = value` for each key | 124 * The operation is equivalent to doing `this[key] = value` for each key |
| 94 * and associated value in other. It iterates over [other], which must | 125 * and associated value in other. It iterates over [other], which must |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 134 /** | 165 /** |
| 135 * Returns true if there is no {key, value} pair in the map. | 166 * Returns true if there is no {key, value} pair in the map. |
| 136 */ | 167 */ |
| 137 bool get isEmpty; | 168 bool get isEmpty; |
| 138 | 169 |
| 139 /** | 170 /** |
| 140 * Returns true if there is at least one {key, value} pair in the map. | 171 * Returns true if there is at least one {key, value} pair in the map. |
| 141 */ | 172 */ |
| 142 bool get isNotEmpty; | 173 bool get isNotEmpty; |
| 143 } | 174 } |
| OLD | NEW |