Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(219)

Unified Diff: sdk/lib/collection/hash_map.dart

Issue 18023003: Add Map constructors taking iterables. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Removed newline. Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | sdk/lib/core/map.dart » ('j') | sdk/lib/core/map.dart » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..2d08e35890696589b494681de77adc10dd5cd36c 100644
--- a/sdk/lib/collection/hash_map.dart
+++ b/sdk/lib/collection/hash_map.dart
@@ -18,10 +18,48 @@ part of dart.collection;
class HashMap<K, V> implements Map<K, V> {
external HashMap();
+ static _id(x) => x;
+
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>();
+
+ if (key == null) key = _id;
+ if (value == null) value = _id;
+
+ for (var element in iterable) {
+ map[key(element)] = value(element);
+ }
+
+ return map;
+ }
+
+ factory HashMap.fromIterables(Iterable<K> keys, Iterable<V> values) {
+ HashMap<K, V> map = new HashMap<K, V>();
+
+ Iterator<K> keyIterator = keys.iterator;
+ Iterator<V> 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) {
+ throw new ArgumentError("Iterables do not have same length.");
+ }
+
+ return map;
+ }
+
external int get length;
external bool get isEmpty;
external bool get isNotEmpty;
« no previous file with comments | « no previous file | sdk/lib/core/map.dart » ('j') | sdk/lib/core/map.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698