| 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 keys of a `HashMap` must have consistent [Object.operator==] | 10 * The keys of a `HashMap` must have consistent [Object.operator==] |
| 11 * and [Object.hashCode] implementations. This means that the `==` operator | 11 * and [Object.hashCode] implementations. This means that the `==` operator |
| 12 * must define a stable equivalence relation on the keys (reflexive, | 12 * must define a stable equivalence relation on the keys (reflexive, |
| 13 * anti-symmetric, transitive, and consistent over time), and that `hashCode` | 13 * anti-symmetric, transitive, and consistent over time), and that `hashCode` |
| 14 * must be the same for objects that are considered equal by `==`. | 14 * must be the same for objects that are considered equal by `==`. |
| 15 * | 15 * |
| 16 * The map allows `null` as a key. | 16 * The map allows `null` as a key. |
| 17 */ | 17 */ |
| 18 class HashMap<K, V> implements Map<K, V> { | 18 class HashMap<K, V> implements Map<K, V> { |
| 19 external HashMap(); | 19 external HashMap(); |
| 20 | 20 |
| 21 static _id(x) => x; | 21 /** |
| 22 | 22 * Creates a [HashMap] that contains all key value pairs of [other]. |
| 23 */ |
| 23 factory HashMap.from(Map<K, V> other) { | 24 factory HashMap.from(Map<K, V> other) { |
| 24 return new HashMap<K, V>()..addAll(other); | 25 return new HashMap<K, V>()..addAll(other); |
| 25 } | 26 } |
| 26 | 27 |
| 27 factory HashMap.fromIterable(Iterable iterable, | 28 /** |
| 29 * Creates a [HashMap] where the keys and values are computed from the |
| 30 * [iterable]. |
| 31 * |
| 32 * For each element of the [iterable] this constructor computes a key/value |
| 33 * pair, by applying [key] and [value] respectively. |
| 34 * |
| 35 * The keys of the key/value pairs do not need to be unique. The last |
| 36 * occurrence of a key will simply overwrite any previous value. |
| 37 * |
| 38 * If no values are specified for [key] and [value] the default is the |
| 39 * identity function. |
| 40 */ |
| 41 factory HashMap.fromIterable(Iterable<K> iterable, |
| 28 {K key(element), V value(element)}) { | 42 {K key(element), V value(element)}) { |
| 29 HashMap<K, V> map = new HashMap<K, V>(); | 43 HashMap<K, V> map = new HashMap<K, V>(); |
| 30 | 44 Maps._fillMapWithMappedIterable(map, iterable, key, value); |
| 31 if (key == null) key = _id; | |
| 32 if (value == null) value = _id; | |
| 33 | |
| 34 for (var element in iterable) { | |
| 35 map[key(element)] = value(element); | |
| 36 } | |
| 37 | |
| 38 return map; | 45 return map; |
| 39 } | 46 } |
| 40 | 47 |
| 48 /** |
| 49 * Creates a [HashMap] associating the given [keys] to [values]. |
| 50 * |
| 51 * This constructor iterates over [keys] and [values] and maps each element of |
| 52 * [keys] to the corresponding element of [values]. |
| 53 * |
| 54 * If [keys] contains the same object multiple times, the last occurrence |
| 55 * overwrites the previous value. |
| 56 * |
| 57 * It is an error if the two [Iterable]s don't have the same length. |
| 58 */ |
| 41 factory HashMap.fromIterables(Iterable<K> keys, Iterable<V> values) { | 59 factory HashMap.fromIterables(Iterable<K> keys, Iterable<V> values) { |
| 42 HashMap<K, V> map = new HashMap<K, V>(); | 60 HashMap<K, V> map = new HashMap<K, V>(); |
| 43 | 61 Maps._fillMapWithIterables(map, keys, values); |
| 44 Iterator<K> keyIterator = keys.iterator; | |
| 45 Iterator<V> valueIterator = values.iterator; | |
| 46 | |
| 47 bool hasNextKey = keyIterator.moveNext(); | |
| 48 bool hasNextValue = valueIterator.moveNext(); | |
| 49 | |
| 50 while (hasNextKey && hasNextValue) { | |
| 51 map[keyIterator.current] = valueIterator.current; | |
| 52 hasNextKey = keyIterator.moveNext(); | |
| 53 hasNextValue = valueIterator.moveNext(); | |
| 54 } | |
| 55 | |
| 56 if (hasNextKey || hasNextValue) { | |
| 57 throw new ArgumentError("Iterables do not have same length."); | |
| 58 } | |
| 59 | |
| 60 return map; | 62 return map; |
| 61 } | 63 } |
| 62 | 64 |
| 63 external int get length; | 65 external int get length; |
| 64 external bool get isEmpty; | 66 external bool get isEmpty; |
| 65 external bool get isNotEmpty; | 67 external bool get isNotEmpty; |
| 66 | 68 |
| 67 external Iterable<K> get keys; | 69 external Iterable<K> get keys; |
| 68 external Iterable<V> get values; | 70 external Iterable<V> get values; |
| 69 | 71 |
| 70 external bool containsKey(Object key); | 72 external bool containsKey(Object key); |
| 71 external bool containsValue(Object value); | 73 external bool containsValue(Object value); |
| 72 | 74 |
| 73 external void addAll(Map<K, V> other); | 75 external void addAll(Map<K, V> other); |
| 74 | 76 |
| 75 external V operator [](Object key); | 77 external V operator [](Object key); |
| 76 external void operator []=(K key, V value); | 78 external void operator []=(K key, V value); |
| 77 | 79 |
| 78 external V putIfAbsent(K key, V ifAbsent()); | 80 external V putIfAbsent(K key, V ifAbsent()); |
| 79 | 81 |
| 80 external V remove(Object key); | 82 external V remove(Object key); |
| 81 external void clear(); | 83 external void clear(); |
| 82 | 84 |
| 83 external void forEach(void action(K key, V value)); | 85 external void forEach(void action(K key, V value)); |
| 84 | 86 |
| 85 String toString() => Maps.mapToString(this); | 87 String toString() => Maps.mapToString(this); |
| 86 } | 88 } |
| OLD | NEW |