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

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

Issue 18072004: Add fromIterable(s) constructors in SplayTreeMap and LinkedHashMap. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: 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/collection/splay_tree.dart » ('j') | sdk/lib/collection/splay_tree.dart » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sdk/lib/collection/linked_hash_map.dart
diff --git a/sdk/lib/collection/linked_hash_map.dart b/sdk/lib/collection/linked_hash_map.dart
index 3082ce0881a8b99736968e9a37839bb375c80bbb..12695d43073b8c89ff8a80eeafa573d4c20d7b60 100644
--- a/sdk/lib/collection/linked_hash_map.dart
+++ b/sdk/lib/collection/linked_hash_map.dart
@@ -21,10 +21,49 @@ part of dart.collection;
class LinkedHashMap<K, V> implements Map<K, V> {
external LinkedHashMap();
+ static _id(x) => x;
+
factory LinkedHashMap.from(Map<K, V> other) {
return new LinkedHashMap<K, V>()..addAll(other);
}
+ factory LinkedHashMap.fromIterable(Iterable iterable,
+ {K key(element), V value(element)}) {
+
+ LinkedHashMap<K, V> map = new LinkedHashMap<K, V>();
floitsch 2013/06/27 15:21:38 We start to have too much code-duplication. Let's
zarah 2013/06/27 15:58:52 Agree! Done.
+
+ if (key == null) key = _id;
+ if (value == null) value = _id;
+
+ for (var element in iterable) {
+ map[key(element)] = value(element);
+ }
+
+ return map;
+ }
+
+ factory LinkedHashMap.fromIterables(Iterable<K> keys, Iterable<V> values) {
+ LinkedHashMap<K, V> map = new LinkedHashMap<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 bool containsKey(Object key);
external bool containsValue(Object value);
« no previous file with comments | « no previous file | sdk/lib/collection/splay_tree.dart » ('j') | sdk/lib/collection/splay_tree.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698