| 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 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 251 * | 251 * |
| 252 * To allow calling [operator[]], [remove] or [containsKey] with objects | 252 * To allow calling [operator[]], [remove] or [containsKey] with objects |
| 253 * that are not supported by the `compare` function, an extra `isValidKey` | 253 * that are not supported by the `compare` function, an extra `isValidKey` |
| 254 * predicate function can be supplied. This function is tested before | 254 * predicate function can be supplied. This function is tested before |
| 255 * using the `compare` function on an argument value that may not be a [K] | 255 * using the `compare` function on an argument value that may not be a [K] |
| 256 * value. If omitted, the `isValidKey` function defaults to testing if the | 256 * value. If omitted, the `isValidKey` function defaults to testing if the |
| 257 * value is a [K]. | 257 * value is a [K]. |
| 258 */ | 258 */ |
| 259 class SplayTreeMap<K, V> extends _SplayTree<K> implements Map<K, V> { | 259 class SplayTreeMap<K, V> extends _SplayTree<K> implements Map<K, V> { |
| 260 Comparator<K> _comparator; | 260 Comparator<K> _comparator; |
| 261 _Predicate _validKey; | 261 _Predicate<Object> _validKey; |
| 262 | 262 |
| 263 SplayTreeMap([int compare(K key1, K key2), bool isValidKey(potentialKey)]) | 263 SplayTreeMap([int compare(K key1, K key2), |
| 264 bool isValidKey(Object potentialKey)]) |
| 264 : _comparator = (compare == null) ? Comparable.compare : compare, | 265 : _comparator = (compare == null) ? Comparable.compare : compare, |
| 265 _validKey = (isValidKey != null) ? isValidKey : ((v) => v is K); | 266 _validKey = (isValidKey != null) ? isValidKey : ((v) => v is K); |
| 266 | 267 |
| 267 /** | 268 /** |
| 268 * Creates a [SplayTreeMap] that contains all key/value pairs of [other]. | 269 * Creates a [SplayTreeMap] that contains all key/value pairs of [other]. |
| 269 */ | 270 */ |
| 270 factory SplayTreeMap.from(Map other, | 271 factory SplayTreeMap.from(Map other, |
| 271 [int compare(K key1, K key2), | 272 [int compare(K key1, K key2), |
| 272 bool isValidKey(potentialKey)]) { | 273 bool isValidKey(Object potentialKey)]) { |
| 273 SplayTreeMap<K, V> result = new SplayTreeMap<K, V>(); | 274 SplayTreeMap<K, V> result = new SplayTreeMap<K, V>(); |
| 274 other.forEach((k, v) { result[k] = v; }); | 275 other.forEach((k, v) { result[k] = v; }); |
| 275 return result; | 276 return result; |
| 276 } | 277 } |
| 277 | 278 |
| 278 /** | 279 /** |
| 279 * Creates a [SplayTreeMap] where the keys and values are computed from the | 280 * Creates a [SplayTreeMap] where the keys and values are computed from the |
| 280 * [iterable]. | 281 * [iterable]. |
| 281 * | 282 * |
| 282 * For each element of the [iterable] this constructor computes a key/value | 283 * For each element of the [iterable] this constructor computes a key/value |
| 283 * pair, by applying [key] and [value] respectively. | 284 * pair, by applying [key] and [value] respectively. |
| 284 * | 285 * |
| 285 * The keys of the key/value pairs do not need to be unique. The last | 286 * The keys of the key/value pairs do not need to be unique. The last |
| 286 * occurrence of a key will simply overwrite any previous value. | 287 * occurrence of a key will simply overwrite any previous value. |
| 287 * | 288 * |
| 288 * If no functions are specified for [key] and [value] the default is to | 289 * If no functions are specified for [key] and [value] the default is to |
| 289 * use the iterable value itself. | 290 * use the iterable value itself. |
| 290 */ | 291 */ |
| 291 factory SplayTreeMap.fromIterable(Iterable iterable, | 292 factory SplayTreeMap.fromIterable(Iterable iterable, |
| 292 {K key(element), | 293 {K key(element), |
| 293 V value(element), | 294 V value(element), |
| 294 int compare(K key1, K key2), | 295 int compare(K key1, K key2), |
| 295 bool isValidKey(potentialKey) }) { | 296 bool isValidKey(Object potentialKey) }) { |
| 296 SplayTreeMap<K, V> map = new SplayTreeMap<K, V>(compare, isValidKey); | 297 SplayTreeMap<K, V> map = new SplayTreeMap<K, V>(compare, isValidKey); |
| 297 Maps._fillMapWithMappedIterable(map, iterable, key, value); | 298 Maps._fillMapWithMappedIterable(map, iterable, key, value); |
| 298 return map; | 299 return map; |
| 299 } | 300 } |
| 300 | 301 |
| 301 /** | 302 /** |
| 302 * Creates a [SplayTreeMap] associating the given [keys] to [values]. | 303 * Creates a [SplayTreeMap] associating the given [keys] to [values]. |
| 303 * | 304 * |
| 304 * This constructor iterates over [keys] and [values] and maps each element of | 305 * This constructor iterates over [keys] and [values] and maps each element of |
| 305 * [keys] to the corresponding element of [values]. | 306 * [keys] to the corresponding element of [values]. |
| 306 * | 307 * |
| 307 * If [keys] contains the same object multiple times, the last occurrence | 308 * If [keys] contains the same object multiple times, the last occurrence |
| 308 * overwrites the previous value. | 309 * overwrites the previous value. |
| 309 * | 310 * |
| 310 * It is an error if the two [Iterable]s don't have the same length. | 311 * It is an error if the two [Iterable]s don't have the same length. |
| 311 */ | 312 */ |
| 312 factory SplayTreeMap.fromIterables(Iterable<K> keys, Iterable<V> values, | 313 factory SplayTreeMap.fromIterables(Iterable<K> keys, Iterable<V> values, |
| 313 [int compare(K key1, K key2), bool isValidKey(potentialKey)]) { | 314 [int compare(K key1, K key2), bool isValidKey(Object potentialKey)]) { |
| 314 SplayTreeMap<K, V> map = new SplayTreeMap<K, V>(compare, isValidKey); | 315 SplayTreeMap<K, V> map = new SplayTreeMap<K, V>(compare, isValidKey); |
| 315 Maps._fillMapWithIterables(map, keys, values); | 316 Maps._fillMapWithIterables(map, keys, values); |
| 316 return map; | 317 return map; |
| 317 } | 318 } |
| 318 | 319 |
| 319 int _compare(K key1, K key2) => _comparator(key1, key2); | 320 int _compare(K key1, K key2) => _comparator(key1, key2); |
| 320 | 321 |
| 321 SplayTreeMap._internal(); | 322 SplayTreeMap._internal(); |
| 322 | 323 |
| 323 V operator [](Object key) { | 324 V operator [](Object key) { |
| (...skipping 327 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 651 * the constructor, both for ordering and for equality. | 652 * the constructor, both for ordering and for equality. |
| 652 * If the set contains only an object `a`, then `set.contains(b)` | 653 * If the set contains only an object `a`, then `set.contains(b)` |
| 653 * will return `true` if and only if `compare(a, b) == 0`, | 654 * will return `true` if and only if `compare(a, b) == 0`, |
| 654 * and the value of `a == b` is not even checked. | 655 * and the value of `a == b` is not even checked. |
| 655 * If the compare function is omitted, the objects are assumed to be | 656 * If the compare function is omitted, the objects are assumed to be |
| 656 * [Comparable], and are compared using their [Comparable.compareTo] method. | 657 * [Comparable], and are compared using their [Comparable.compareTo] method. |
| 657 * Non-comparable objects (including `null`) will not work as an element | 658 * Non-comparable objects (including `null`) will not work as an element |
| 658 * in that case. | 659 * in that case. |
| 659 */ | 660 */ |
| 660 class SplayTreeSet<E> extends _SplayTree<E> with IterableMixin<E>, SetMixin<E> { | 661 class SplayTreeSet<E> extends _SplayTree<E> with IterableMixin<E>, SetMixin<E> { |
| 661 Comparator _comparator; | 662 Comparator<E> _comparator; |
| 662 _Predicate _validKey; | 663 _Predicate<Object> _validKey; |
| 663 | 664 |
| 664 /** | 665 /** |
| 665 * Create a new [SplayTreeSet] with the given compare function. | 666 * Create a new [SplayTreeSet] with the given compare function. |
| 666 * | 667 * |
| 667 * If the [compare] function is omitted, it defaults to [Comparable.compare], | 668 * If the [compare] function is omitted, it defaults to [Comparable.compare], |
| 668 * and the elements must be comparable. | 669 * and the elements must be comparable. |
| 669 * | 670 * |
| 670 * A provided `compare` function may not work on all objects. It may not even | 671 * A provided `compare` function may not work on all objects. It may not even |
| 671 * work on all `E` instances. | 672 * work on all `E` instances. |
| 672 * | 673 * |
| 673 * For operations that add elements to the set, the user is supposed to not | 674 * For operations that add elements to the set, the user is supposed to not |
| 674 * pass in objects that doesn't work with the compare function. | 675 * pass in objects that doesn't work with the compare function. |
| 675 * | 676 * |
| 676 * The methods [contains], [remove], [lookup], [removeAll] or [retainAll] | 677 * The methods [contains], [remove], [lookup], [removeAll] or [retainAll] |
| 677 * are typed to accept any object(s), and the [isValidKey] test can used to | 678 * are typed to accept any object(s), and the [isValidKey] test can used to |
| 678 * filter those objects before handing them to the `compare` function. | 679 * filter those objects before handing them to the `compare` function. |
| 679 * | 680 * |
| 680 * If [isValidKey] is provided, only values satisfying `isValidKey(other)` | 681 * If [isValidKey] is provided, only values satisfying `isValidKey(other)` |
| 681 * are compared using the `compare` method in the methods mentioned above. | 682 * are compared using the `compare` method in the methods mentioned above. |
| 682 * If the `isValidKey` function returns false for an object, it is assumed to | 683 * If the `isValidKey` function returns false for an object, it is assumed to |
| 683 * not be in the set. | 684 * not be in the set. |
| 684 * | 685 * |
| 685 * If omitted, the `isValidKey` function defaults to checking against the | 686 * If omitted, the `isValidKey` function defaults to checking against the |
| 686 * type parameter: `other is E`. | 687 * type parameter: `other is E`. |
| 687 */ | 688 */ |
| 688 SplayTreeSet([int compare(E key1, E key2), bool isValidKey(potentialKey)]) | 689 SplayTreeSet([int compare(E key1, E key2), |
| 690 bool isValidKey(Object potentialKey)]) |
| 689 : _comparator = (compare == null) ? Comparable.compare : compare, | 691 : _comparator = (compare == null) ? Comparable.compare : compare, |
| 690 _validKey = (isValidKey != null) ? isValidKey : ((v) => v is E); | 692 _validKey = (isValidKey != null) ? isValidKey : ((v) => v is E); |
| 691 | 693 |
| 692 /** | 694 /** |
| 693 * Creates a [SplayTreeSet] that contains all [elements]. | 695 * Creates a [SplayTreeSet] that contains all [elements]. |
| 694 * | 696 * |
| 695 * The set works as if created by `new SplayTreeSet<E>(compare, isValidKey)`. | 697 * The set works as if created by `new SplayTreeSet<E>(compare, isValidKey)`. |
| 696 * | 698 * |
| 697 * All the [elements] should be valid as arguments to the [compare] function. | 699 * All the [elements] should be valid as arguments to the [compare] function. |
| 698 */ | 700 */ |
| 699 factory SplayTreeSet.from(Iterable elements, | 701 factory SplayTreeSet.from(Iterable elements, |
| 700 [int compare(E key1, E key2), | 702 [int compare(E key1, E key2), |
| 701 bool isValidKey(potentialKey)]) { | 703 bool isValidKey(Object potentialKey)]) { |
| 702 SplayTreeSet<E> result = new SplayTreeSet<E>(compare, isValidKey); | 704 SplayTreeSet<E> result = new SplayTreeSet<E>(compare, isValidKey); |
| 703 for (final E element in elements) { | 705 for (final E element in elements) { |
| 704 result.add(element); | 706 result.add(element); |
| 705 } | 707 } |
| 706 return result; | 708 return result; |
| 707 } | 709 } |
| 708 | 710 |
| 709 int _compare(E e1, E e2) => _comparator(e1, e2); | 711 int _compare(E e1, E e2) => _comparator(e1, e2); |
| 710 | 712 |
| 711 // From Iterable. | 713 // From Iterable. |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 825 return new _SplayTreeNode<E>(node.key)..left = _copyNode(node.left) | 827 return new _SplayTreeNode<E>(node.key)..left = _copyNode(node.left) |
| 826 ..right = _copyNode(node.right); | 828 ..right = _copyNode(node.right); |
| 827 } | 829 } |
| 828 | 830 |
| 829 void clear() { _clear(); } | 831 void clear() { _clear(); } |
| 830 | 832 |
| 831 Set<E> toSet() => _clone(); | 833 Set<E> toSet() => _clone(); |
| 832 | 834 |
| 833 String toString() => IterableBase.iterableToFullString(this, '{', '}'); | 835 String toString() => IterableBase.iterableToFullString(this, '{', '}'); |
| 834 } | 836 } |
| OLD | NEW |