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

Side by Side 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 part of dart.collection; 5 part of dart.collection;
6 6
7 typedef bool _Predicate<T>(T value); 7 typedef bool _Predicate<T>(T value);
8 8
9 /** 9 /**
10 * A node in a splay tree. It holds the sorting key and the left 10 * A node in a splay tree. It holds the sorting key and the left
(...skipping 253 matching lines...) Expand 10 before | Expand all | Expand 10 after
264 Comparator<K> _comparator; 264 Comparator<K> _comparator;
265 _Predicate _validKey; 265 _Predicate _validKey;
266 266
267 SplayTreeMap([int compare(K key1, K key2), bool isValidKey(potentialKey)]) 267 SplayTreeMap([int compare(K key1, K key2), bool isValidKey(potentialKey)])
268 : _comparator = (compare == null) ? Comparable.compare : compare, 268 : _comparator = (compare == null) ? Comparable.compare : compare,
269 _validKey = (isValidKey != null) ? isValidKey : ((v) => v is K); 269 _validKey = (isValidKey != null) ? isValidKey : ((v) => v is K);
270 270
271 /** 271 /**
272 * Creates a [SplayTreeMap] that contains all key value pairs of [other]. 272 * Creates a [SplayTreeMap] that contains all key value pairs of [other].
273 */ 273 */
274 factory SplayTreeMap.from(Map<K, V> other, 274 factory SplayTreeMap.from(Map other,
275 [ int compare(K key1, K key2), 275 [ 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.
276 bool isValidKey(potentialKey)]) => 276 bool isValidKey(potentialKey)]) {
277 new SplayTreeMap(compare, isValidKey)..addAll(other); 277 SplayTreeMap<K, V> result = new SplayTreeMap<K, V>();
278 other.forEach((k, v) { result[k] = v; });
279 return result;
280 }
278 281
279 /** 282 /**
280 * Creates a [SplayTreeMap] where the keys and values are computed from the 283 * Creates a [SplayTreeMap] where the keys and values are computed from the
281 * [iterable]. 284 * [iterable].
282 * 285 *
283 * For each element of the [iterable] this constructor computes a key/value 286 * For each element of the [iterable] this constructor computes a key/value
284 * pair, by applying [key] and [value] respectively. 287 * pair, by applying [key] and [value] respectively.
285 * 288 *
286 * The keys of the key/value pairs do not need to be unique. The last 289 * The keys of the key/value pairs do not need to be unique. The last
287 * occurrence of a key will simply overwrite any previous value. 290 * occurrence of a key will simply overwrite any previous value.
(...skipping 393 matching lines...) Expand 10 before | Expand all | Expand 10 after
681 * If the `isValidKey` function returns false for an object, it is assumed to 684 * If the `isValidKey` function returns false for an object, it is assumed to
682 * not be in the set. 685 * not be in the set.
683 * 686 *
684 * If omitted, the `isValidKey` function defaults to checking against the 687 * If omitted, the `isValidKey` function defaults to checking against the
685 * type parameter: `other is E`. 688 * type parameter: `other is E`.
686 */ 689 */
687 SplayTreeSet([int compare(E key1, E key2), bool isValidKey(potentialKey)]) 690 SplayTreeSet([int compare(E key1, E key2), bool isValidKey(potentialKey)])
688 : _comparator = (compare == null) ? Comparable.compare : compare, 691 : _comparator = (compare == null) ? Comparable.compare : compare,
689 _validKey = (isValidKey != null) ? isValidKey : ((v) => v is E); 692 _validKey = (isValidKey != null) ? isValidKey : ((v) => v is E);
690 693
694 /**
695 * Creates a [SplayTreeSet] that contains all of [elements].
696 *
697 * The set works as if created by `new SplayTreeSet<E>(compare, isValidKey)`.
698 *
699 * All the [elements] should be valid as arguments to the [compare] function.
700 */
701 factory SplayTreeSet.from(Iterable elements,
702 [int compare(E key1, E key2),
703 bool isValidKey(potentialKey)]) {
704 SplayTreeSet<E> result = new SplayTreeSet<E>(compare, isValidKey);
705 for (final E element in elements) {
706 result.add(element);
707 }
708 return result;
709 }
710
691 int _compare(E e1, E e2) => _comparator(e1, e2); 711 int _compare(E e1, E e2) => _comparator(e1, e2);
692 712
693 // From Iterable. 713 // From Iterable.
694 714
695 Iterator<E> get iterator => new _SplayTreeKeyIterator<E>(this); 715 Iterator<E> get iterator => new _SplayTreeKeyIterator<E>(this);
696 716
697 int get length => _count; 717 int get length => _count;
698 bool get isEmpty => _root == null; 718 bool get isEmpty => _root == null;
699 bool get isNotEmpty => _root != null; 719 bool get isNotEmpty => _root != null;
700 720
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
807 return new _SplayTreeNode<E>(node.key)..left = _copyNode(node.left) 827 return new _SplayTreeNode<E>(node.key)..left = _copyNode(node.left)
808 ..right = _copyNode(node.right); 828 ..right = _copyNode(node.right);
809 } 829 }
810 830
811 void clear() { _clear(); } 831 void clear() { _clear(); }
812 832
813 Set<E> toSet() => _clone(); 833 Set<E> toSet() => _clone();
814 834
815 String toString() => IterableBase.iterableToFullString(this, '{', '}'); 835 String toString() => IterableBase.iterableToFullString(this, '{', '}');
816 } 836 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698