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

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

Issue 838463002: Change List/Set/Map/Queue.from constructrs to accept any iterable. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 5 years, 11 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 0edd58deaac79d7094125e24d501aa8475bdc97c..8d05ca36ad6706d7faf03c7c1348d426e43617cf 100644
--- a/sdk/lib/collection/splay_tree.dart
+++ b/sdk/lib/collection/splay_tree.dart
@@ -271,10 +271,13 @@ class SplayTreeMap<K, V> extends _SplayTree<K> implements Map<K, V> {
/**
* Creates a [SplayTreeMap] that contains all key value pairs of [other].
*/
- factory SplayTreeMap.from(Map<K, V> other,
+ factory SplayTreeMap.from(Map other,
[ int compare(K key1, K key2),
floitsch 2015/01/05 14:45:30 remove space.
Lasse Reichstein Nielsen 2015/01/06 10:14:45 Done.
- bool isValidKey(potentialKey)]) =>
- new SplayTreeMap(compare, isValidKey)..addAll(other);
+ bool isValidKey(potentialKey)]) {
+ SplayTreeMap<K, V> result = new SplayTreeMap<K, V>();
+ other.forEach((k, v) { result[k] = v; });
+ return result;
+ }
/**
* Creates a [SplayTreeMap] where the keys and values are computed from the
@@ -688,6 +691,23 @@ class SplayTreeSet<E> extends _SplayTree<E> with IterableMixin<E>, SetMixin<E> {
: _comparator = (compare == null) ? Comparable.compare : compare,
_validKey = (isValidKey != null) ? isValidKey : ((v) => v is E);
+ /**
+ * Creates a [SplayTreeSet] that contains all of [elements].
+ *
+ * The set works as if created by `new SplayTreeSet<E>(compare, isValidKey)`.
+ *
+ * All the [elements] should be valid as arguments to the [compare] function.
+ */
+ factory SplayTreeSet.from(Iterable elements,
+ [int compare(E key1, E key2),
+ bool isValidKey(potentialKey)]) {
+ SplayTreeSet<E> result = new SplayTreeSet<E>(compare, isValidKey);
+ for (final E element in elements) {
+ result.add(element);
+ }
+ return result;
+ }
+
int _compare(E e1, E e2) => _comparator(e1, e2);
// From Iterable.

Powered by Google App Engine
This is Rietveld 408576698