| OLD | NEW |
| 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 226 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 237 _root = null; | 237 _root = null; |
| 238 _count = 0; | 238 _count = 0; |
| 239 _modificationCount++; | 239 _modificationCount++; |
| 240 } | 240 } |
| 241 } | 241 } |
| 242 | 242 |
| 243 class _TypeTest<T> { | 243 class _TypeTest<T> { |
| 244 bool test(v) => v is T; | 244 bool test(v) => v is T; |
| 245 } | 245 } |
| 246 | 246 |
| 247 int _dynamicCompare(dynamic a, dynamic b) => Comparable.compare(a, b); |
| 248 |
| 249 Comparator<K> _defaultCompare<K>() { |
| 250 // If K <: Comparable, then we can just use Comparable.compare |
| 251 // with no casts. |
| 252 Object compare = Comparable.compare; |
| 253 if (compare is Comparator<K>) { |
| 254 return compare; |
| 255 } |
| 256 // Otherwise wrap and cast the arguments on each call. |
| 257 return _dynamicCompare; |
| 258 } |
| 259 |
| 247 /** | 260 /** |
| 248 * A [Map] of objects that can be ordered relative to each other. | 261 * A [Map] of objects that can be ordered relative to each other. |
| 249 * | 262 * |
| 250 * The map is based on a self-balancing binary tree. It allows most operations | 263 * The map is based on a self-balancing binary tree. It allows most operations |
| 251 * in amortized logarithmic time. | 264 * in amortized logarithmic time. |
| 252 * | 265 * |
| 253 * Keys of the map are compared using the `compare` function passed in | 266 * Keys of the map are compared using the `compare` function passed in |
| 254 * the constructor, both for ordering and for equality. | 267 * the constructor, both for ordering and for equality. |
| 255 * If the map contains only the key `a`, then `map.containsKey(b)` | 268 * If the map contains only the key `a`, then `map.containsKey(b)` |
| 256 * will return `true` if and only if `compare(a, b) == 0`, | 269 * will return `true` if and only if `compare(a, b) == 0`, |
| (...skipping 13 matching lines...) Expand all Loading... |
| 270 class SplayTreeMap<K, V> extends _SplayTree<K, _SplayTreeMapNode<K, V>> | 283 class SplayTreeMap<K, V> extends _SplayTree<K, _SplayTreeMapNode<K, V>> |
| 271 implements Map<K, V> { | 284 implements Map<K, V> { |
| 272 _SplayTreeMapNode<K, V> _root; | 285 _SplayTreeMapNode<K, V> _root; |
| 273 final _SplayTreeMapNode<K, V> _dummy = | 286 final _SplayTreeMapNode<K, V> _dummy = |
| 274 new _SplayTreeMapNode<K, V>(null, null); | 287 new _SplayTreeMapNode<K, V>(null, null); |
| 275 | 288 |
| 276 Comparator<K> _comparator; | 289 Comparator<K> _comparator; |
| 277 _Predicate _validKey; | 290 _Predicate _validKey; |
| 278 | 291 |
| 279 SplayTreeMap([int compare(K key1, K key2), bool isValidKey(potentialKey)]) | 292 SplayTreeMap([int compare(K key1, K key2), bool isValidKey(potentialKey)]) |
| 280 : _comparator = compare ?? Comparable.compare as Comparator<K>, | 293 : _comparator = compare ?? _defaultCompare<K>(), |
| 281 _validKey = isValidKey ?? ((v) => v is K); | 294 _validKey = isValidKey ?? ((v) => v is K); |
| 282 | 295 |
| 283 /** | 296 /** |
| 284 * Creates a [SplayTreeMap] that contains all key/value pairs of [other]. | 297 * Creates a [SplayTreeMap] that contains all key/value pairs of [other]. |
| 285 */ | 298 */ |
| 286 factory SplayTreeMap.from(Map other, | 299 factory SplayTreeMap.from(Map other, |
| 287 [int compare(K key1, K key2), bool isValidKey(potentialKey)]) { | 300 [int compare(K key1, K key2), bool isValidKey(potentialKey)]) { |
| 288 SplayTreeMap<K, V> result = new SplayTreeMap<K, V>(compare, isValidKey); | 301 SplayTreeMap<K, V> result = new SplayTreeMap<K, V>(compare, isValidKey); |
| 289 other.forEach((k, v) { | 302 other.forEach((k, v) { |
| 290 result[k as Object/*=K*/] = v as Object/*=V*/; | 303 result[k as Object/*=K*/] = v as Object/*=V*/; |
| (...skipping 407 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 698 * | 711 * |
| 699 * If [isValidKey] is provided, only values satisfying `isValidKey(other)` | 712 * If [isValidKey] is provided, only values satisfying `isValidKey(other)` |
| 700 * are compared using the `compare` method in the methods mentioned above. | 713 * are compared using the `compare` method in the methods mentioned above. |
| 701 * If the `isValidKey` function returns false for an object, it is assumed to | 714 * If the `isValidKey` function returns false for an object, it is assumed to |
| 702 * not be in the set. | 715 * not be in the set. |
| 703 * | 716 * |
| 704 * If omitted, the `isValidKey` function defaults to checking against the | 717 * If omitted, the `isValidKey` function defaults to checking against the |
| 705 * type parameter: `other is E`. | 718 * type parameter: `other is E`. |
| 706 */ | 719 */ |
| 707 SplayTreeSet([int compare(E key1, E key2), bool isValidKey(potentialKey)]) | 720 SplayTreeSet([int compare(E key1, E key2), bool isValidKey(potentialKey)]) |
| 708 : _comparator = | 721 : _comparator = compare ?? _defaultCompare<E>(), |
| 709 compare ?? Comparable.compare as dynamic/*=Comparator<E>*/, | |
| 710 _validKey = isValidKey ?? ((v) => v is E); | 722 _validKey = isValidKey ?? ((v) => v is E); |
| 711 | 723 |
| 712 /** | 724 /** |
| 713 * Creates a [SplayTreeSet] that contains all [elements]. | 725 * Creates a [SplayTreeSet] that contains all [elements]. |
| 714 * | 726 * |
| 715 * The set works as if created by `new SplayTreeSet<E>(compare, isValidKey)`. | 727 * The set works as if created by `new SplayTreeSet<E>(compare, isValidKey)`. |
| 716 * | 728 * |
| 717 * All the [elements] should be valid as arguments to the [compare] function. | 729 * All the [elements] should be valid as arguments to the [compare] function. |
| 718 */ | 730 */ |
| 719 factory SplayTreeSet.from(Iterable elements, | 731 factory SplayTreeSet.from(Iterable elements, |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 850 } | 862 } |
| 851 | 863 |
| 852 void clear() { | 864 void clear() { |
| 853 _clear(); | 865 _clear(); |
| 854 } | 866 } |
| 855 | 867 |
| 856 Set<E> toSet() => _clone(); | 868 Set<E> toSet() => _clone(); |
| 857 | 869 |
| 858 String toString() => IterableBase.iterableToFullString(this, '{', '}'); | 870 String toString() => IterableBase.iterableToFullString(this, '{', '}'); |
| 859 } | 871 } |
| OLD | NEW |