| 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; |
| 22 |
| 21 factory HashMap.from(Map<K, V> other) { | 23 factory HashMap.from(Map<K, V> other) { |
| 22 return new HashMap<K, V>()..addAll(other); | 24 return new HashMap<K, V>()..addAll(other); |
| 23 } | 25 } |
| 24 | 26 |
| 27 factory HashMap.fromIterable(Iterable iterable, |
| 28 {K key(element), V value(element)}) { |
| 29 HashMap<K, V> map = new HashMap<K, V>(); |
| 30 |
| 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; |
| 39 } |
| 40 |
| 41 factory HashMap.fromIterables(Iterable<K> keys, Iterable<V> values) { |
| 42 HashMap<K, V> map = new HashMap<K, V>(); |
| 43 |
| 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; |
| 61 } |
| 62 |
| 25 external int get length; | 63 external int get length; |
| 26 external bool get isEmpty; | 64 external bool get isEmpty; |
| 27 external bool get isNotEmpty; | 65 external bool get isNotEmpty; |
| 28 | 66 |
| 29 external Iterable<K> get keys; | 67 external Iterable<K> get keys; |
| 30 external Iterable<V> get values; | 68 external Iterable<V> get values; |
| 31 | 69 |
| 32 external bool containsKey(Object key); | 70 external bool containsKey(Object key); |
| 33 external bool containsValue(Object value); | 71 external bool containsValue(Object value); |
| 34 | 72 |
| 35 external void addAll(Map<K, V> other); | 73 external void addAll(Map<K, V> other); |
| 36 | 74 |
| 37 external V operator [](Object key); | 75 external V operator [](Object key); |
| 38 external void operator []=(K key, V value); | 76 external void operator []=(K key, V value); |
| 39 | 77 |
| 40 external V putIfAbsent(K key, V ifAbsent()); | 78 external V putIfAbsent(K key, V ifAbsent()); |
| 41 | 79 |
| 42 external V remove(Object key); | 80 external V remove(Object key); |
| 43 external void clear(); | 81 external void clear(); |
| 44 | 82 |
| 45 external void forEach(void action(K key, V value)); | 83 external void forEach(void action(K key, V value)); |
| 46 | 84 |
| 47 String toString() => Maps.mapToString(this); | 85 String toString() => Maps.mapToString(this); |
| 48 } | 86 } |
| OLD | NEW |