Chromium Code Reviews| Index: sdk/lib/collection/hash_map.dart |
| diff --git a/sdk/lib/collection/hash_map.dart b/sdk/lib/collection/hash_map.dart |
| index ab1f09cbdfc6b2e0c3c413bac73465a75e44ec56..e425b668f3d468a2bdafbb66ce2bf6b4dd4a6a73 100644 |
| --- a/sdk/lib/collection/hash_map.dart |
| +++ b/sdk/lib/collection/hash_map.dart |
| @@ -18,10 +18,47 @@ part of dart.collection; |
| class HashMap<K, V> implements Map<K, V> { |
| external HashMap(); |
| + |
| factory HashMap.from(Map<K, V> other) { |
| return new HashMap<K, V>()..addAll(other); |
| } |
| + factory HashMap.fromIterable(Iterable iterable, |
| + {K key(element), V value(element)}) { |
| + HashMap<K, V> map = new HashMap<K, V>(); |
| + |
| + var keyFun = (key != null) ? key : (x) => x; |
|
floitsch
2013/06/27 11:45:04
In "dart:" libraries we type local variables too.
zarah
2013/06/27 13:02:43
Done.
|
| + var valueFun = (value != null) ? value : (x) => x; |
| + |
| + iterable.forEach((e) { |
|
floitsch
2013/06/27 11:45:04
personally I prefer
for (var element in iterable)
zarah
2013/06/27 13:02:43
Done.
|
| + map[keyFun(e)] = valueFun(e); |
| + }); |
| + |
| + return map; |
| + } |
| + |
| + factory HashMap.fromIterables(Iterable<K> keys, Iterable<V> values) { |
| + HashMap<K, V> map = new HashMap<K, V>(); |
| + |
| + var keyIterator = keys.iterator; |
|
floitsch
2013/06/27 11:45:04
Use type.
zarah
2013/06/27 13:02:43
Done.
|
| + var valueIterator = values.iterator; |
| + |
| + bool hasNextKey = keyIterator.moveNext(); |
| + bool hasNextValue = valueIterator.moveNext(); |
| + |
| + while (hasNextKey && hasNextValue) { |
| + map[keyIterator.current] = valueIterator.current; |
| + hasNextKey = keyIterator.moveNext(); |
| + hasNextValue = valueIterator.moveNext(); |
| + } |
| + |
| + if (hasNextKey != hasNextValue) { |
|
floitsch
2013/06/27 11:45:04
if (hasNextKey || hasNextValue) {
It has the same
zarah
2013/06/27 13:02:43
Done.
|
| + throw new ArgumentError("Iterables do not have same length."); |
| + } |
| + |
| + return map; |
| + } |
| + |
| external int get length; |
| external bool get isEmpty; |
| external bool get isNotEmpty; |