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

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

Issue 2877683002: Adjust types in SplayTree implementation and some strong tests to (Closed)
Patch Set: Bind to variable to enable promotion Created 3 years, 7 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 14cb33fde3c256c973e9a9e0cb9feea642de29fe..319b1af59d18a35ae35d4e24d2ff044683f78b9d 100644
--- a/sdk/lib/collection/splay_tree.dart
+++ b/sdk/lib/collection/splay_tree.dart
@@ -244,6 +244,19 @@ class _TypeTest<T> {
bool test(v) => v is T;
}
+int _dynamicCompare(dynamic a, dynamic b) => Comparable.compare(a, b);
+
+Comparator<K> _defaultCompare<K>() {
+ // If K <: Comparable, then we can just use Comparable.compare
+ // with no casts.
+ Object compare = Comparable.compare;
+ if (compare is Comparator<K>) {
+ return compare;
+ }
+ // Otherwise wrap and cast the arguments on each call.
+ return _dynamicCompare;
+}
+
/**
* A [Map] of objects that can be ordered relative to each other.
*
@@ -277,7 +290,7 @@ class SplayTreeMap<K, V> extends _SplayTree<K, _SplayTreeMapNode<K, V>>
_Predicate _validKey;
SplayTreeMap([int compare(K key1, K key2), bool isValidKey(potentialKey)])
- : _comparator = compare ?? Comparable.compare as Comparator<K>,
+ : _comparator = compare ?? _defaultCompare<K>(),
_validKey = isValidKey ?? ((v) => v is K);
/**
@@ -705,8 +718,7 @@ class SplayTreeSet<E> extends _SplayTree<E, _SplayTreeNode<E>>
* type parameter: `other is E`.
*/
SplayTreeSet([int compare(E key1, E key2), bool isValidKey(potentialKey)])
- : _comparator =
- compare ?? Comparable.compare as dynamic/*=Comparator<E>*/,
+ : _comparator = compare ?? _defaultCompare<E>(),
_validKey = isValidKey ?? ((v) => v is E);
/**

Powered by Google App Engine
This is Rietveld 408576698