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

Unified Diff: sdk/lib/collection/splay_tree.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
Index: sdk/lib/collection/splay_tree.dart
diff --git a/sdk/lib/collection/splay_tree.dart b/sdk/lib/collection/splay_tree.dart
index 84e61e10d4386e01a8e85514a643374b22ad59d9..a49164ff87b86d282cff005818efa1490eaa2ebb 100644
--- a/sdk/lib/collection/splay_tree.dart
+++ b/sdk/lib/collection/splay_tree.dart
@@ -245,12 +245,51 @@ class SplayTreeMap<K, V> extends _SplayTree<K> implements Map<K, V> {
// checked mode. http://dartbug.com/7733
Function /* Comparator<K> */_comparator;
+ static _id(x) => x;
+
SplayTreeMap([int compare(K key1, K key2)])
: _comparator = (compare == null) ? Comparable.compare : compare;
factory SplayTreeMap.from(Map<K, V> other, [int compare(K key1, K key2)]) =>
new SplayTreeMap(compare)..addAll(other);
+ factory SplayTreeMap.fromIterable(Iterable iterable,
+ {int compare(K key1, K key2), K key(element), V value(element)}) {
floitsch 2013/06/27 15:21:38 move compare last. It doesn't make any semantic di
zarah 2013/06/27 15:58:52 Done.
+ SplayTreeMap<K, V> map = new SplayTreeMap<K, V>(compare);
+
+ if (key == null) key = _id;
+ if (value == null) value = _id;
+
+ for (var element in iterable) {
+ map[key(element)] = value(element);
+ }
+
+ return map;
+ }
+
+ factory SplayTreeMap.fromIterables(Iterable<K> keys, Iterable<V> values,
+ [int compare(K key1, K key2)]) {
+ SplayTreeMap<K, V> map = new SplayTreeMap<K, V>(compare);
+
+ 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;
+ }
+
int _compare(K key1, K key2) => _comparator(key1, key2);
SplayTreeMap._internal();

Powered by Google App Engine
This is Rietveld 408576698