| 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 266 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 277 _Predicate _validKey; | 277 _Predicate _validKey; |
| 278 | 278 |
| 279 SplayTreeMap([int compare(K key1, K key2), bool isValidKey(potentialKey)]) | 279 SplayTreeMap([int compare(K key1, K key2), bool isValidKey(potentialKey)]) |
| 280 : _comparator = compare ?? Comparable.compare as Comparator<K>, | 280 : _comparator = compare ?? Comparable.compare as Comparator<K>, |
| 281 _validKey = isValidKey ?? ((v) => v is K); | 281 _validKey = isValidKey ?? ((v) => v is K); |
| 282 | 282 |
| 283 /** | 283 /** |
| 284 * Creates a [SplayTreeMap] that contains all key/value pairs of [other]. | 284 * Creates a [SplayTreeMap] that contains all key/value pairs of [other]. |
| 285 */ | 285 */ |
| 286 factory SplayTreeMap.from(Map other, | 286 factory SplayTreeMap.from(Map other, |
| 287 [int compare(K key1, K key2), | 287 [int compare(K key1, K key2), bool isValidKey(potentialKey)]) { |
| 288 bool isValidKey(potentialKey)]) { | |
| 289 SplayTreeMap<K, V> result = new SplayTreeMap<K, V>(compare, isValidKey); | 288 SplayTreeMap<K, V> result = new SplayTreeMap<K, V>(compare, isValidKey); |
| 290 other.forEach((k, v) { result[k as Object/*=K*/] = v as Object/*=V*/; }); | 289 other.forEach((k, v) { |
| 290 result[k as Object/*=K*/] = v as Object/*=V*/; |
| 291 }); |
| 291 return result; | 292 return result; |
| 292 } | 293 } |
| 293 | 294 |
| 294 /** | 295 /** |
| 295 * Creates a [SplayTreeMap] where the keys and values are computed from the | 296 * Creates a [SplayTreeMap] where the keys and values are computed from the |
| 296 * [iterable]. | 297 * [iterable]. |
| 297 * | 298 * |
| 298 * For each element of the [iterable] this constructor computes a key/value | 299 * For each element of the [iterable] this constructor computes a key/value |
| 299 * pair, by applying [key] and [value] respectively. | 300 * pair, by applying [key] and [value] respectively. |
| 300 * | 301 * |
| 301 * The keys of the key/value pairs do not need to be unique. The last | 302 * The keys of the key/value pairs do not need to be unique. The last |
| 302 * occurrence of a key will simply overwrite any previous value. | 303 * occurrence of a key will simply overwrite any previous value. |
| 303 * | 304 * |
| 304 * If no functions are specified for [key] and [value] the default is to | 305 * If no functions are specified for [key] and [value] the default is to |
| 305 * use the iterable value itself. | 306 * use the iterable value itself. |
| 306 */ | 307 */ |
| 307 factory SplayTreeMap.fromIterable(Iterable iterable, | 308 factory SplayTreeMap.fromIterable(Iterable iterable, |
| 308 {K key(element), | 309 {K key(element), |
| 309 V value(element), | 310 V value(element), |
| 310 int compare(K key1, K key2), | 311 int compare(K key1, K key2), |
| 311 bool isValidKey(potentialKey) }) { | 312 bool isValidKey(potentialKey)}) { |
| 312 SplayTreeMap<K, V> map = new SplayTreeMap<K, V>(compare, isValidKey); | 313 SplayTreeMap<K, V> map = new SplayTreeMap<K, V>(compare, isValidKey); |
| 313 Maps._fillMapWithMappedIterable(map, iterable, key, value); | 314 Maps._fillMapWithMappedIterable(map, iterable, key, value); |
| 314 return map; | 315 return map; |
| 315 } | 316 } |
| 316 | 317 |
| 317 /** | 318 /** |
| 318 * Creates a [SplayTreeMap] associating the given [keys] to [values]. | 319 * Creates a [SplayTreeMap] associating the given [keys] to [values]. |
| 319 * | 320 * |
| 320 * This constructor iterates over [keys] and [values] and maps each element of | 321 * This constructor iterates over [keys] and [values] and maps each element of |
| 321 * [keys] to the corresponding element of [values]. | 322 * [keys] to the corresponding element of [values]. |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 359 // Splay on the key to move the last node on the search path for | 360 // Splay on the key to move the last node on the search path for |
| 360 // the key to the root of the tree. | 361 // the key to the root of the tree. |
| 361 int comp = _splay(key); | 362 int comp = _splay(key); |
| 362 if (comp == 0) { | 363 if (comp == 0) { |
| 363 _root.value = value; | 364 _root.value = value; |
| 364 return; | 365 return; |
| 365 } | 366 } |
| 366 _addNewRoot(new _SplayTreeMapNode(key, value), comp); | 367 _addNewRoot(new _SplayTreeMapNode(key, value), comp); |
| 367 } | 368 } |
| 368 | 369 |
| 369 | |
| 370 V putIfAbsent(K key, V ifAbsent()) { | 370 V putIfAbsent(K key, V ifAbsent()) { |
| 371 if (key == null) throw new ArgumentError(key); | 371 if (key == null) throw new ArgumentError(key); |
| 372 int comp = _splay(key); | 372 int comp = _splay(key); |
| 373 if (comp == 0) { | 373 if (comp == 0) { |
| 374 return _root.value; | 374 return _root.value; |
| 375 } | 375 } |
| 376 int modificationCount = _modificationCount; | 376 int modificationCount = _modificationCount; |
| 377 int splayCount = _splayCount; | 377 int splayCount = _splayCount; |
| 378 V value = ifAbsent(); | 378 V value = ifAbsent(); |
| 379 if (modificationCount != _modificationCount) { | 379 if (modificationCount != _modificationCount) { |
| 380 throw new ConcurrentModificationError(this); | 380 throw new ConcurrentModificationError(this); |
| 381 } | 381 } |
| 382 if (splayCount != _splayCount) { | 382 if (splayCount != _splayCount) { |
| 383 comp = _splay(key); | 383 comp = _splay(key); |
| 384 // Key is still not there, otherwise _modificationCount would be changed. | 384 // Key is still not there, otherwise _modificationCount would be changed. |
| 385 assert(comp != 0); | 385 assert(comp != 0); |
| 386 } | 386 } |
| 387 _addNewRoot(new _SplayTreeMapNode(key, value), comp); | 387 _addNewRoot(new _SplayTreeMapNode(key, value), comp); |
| 388 return value; | 388 return value; |
| 389 } | 389 } |
| 390 | 390 |
| 391 void addAll(Map<K, V> other) { | 391 void addAll(Map<K, V> other) { |
| 392 other.forEach((K key, V value) { this[key] = value; }); | 392 other.forEach((K key, V value) { |
| 393 this[key] = value; |
| 394 }); |
| 393 } | 395 } |
| 394 | 396 |
| 395 bool get isEmpty { | 397 bool get isEmpty { |
| 396 return (_root == null); | 398 return (_root == null); |
| 397 } | 399 } |
| 398 | 400 |
| 399 bool get isNotEmpty => !isEmpty; | 401 bool get isNotEmpty => !isEmpty; |
| 400 | 402 |
| 401 void forEach(void f(K key, V value)) { | 403 void forEach(void f(K key, V value)) { |
| 402 Iterator<_SplayTreeNode<K>> nodes = | 404 Iterator<_SplayTreeNode<K>> nodes = new _SplayTreeNodeIterator<K>(this); |
| 403 new _SplayTreeNodeIterator<K>(this); | |
| 404 while (nodes.moveNext()) { | 405 while (nodes.moveNext()) { |
| 405 _SplayTreeMapNode<K, V> node = nodes.current; | 406 _SplayTreeMapNode<K, V> node = nodes.current; |
| 406 f(node.key, node.value); | 407 f(node.key, node.value); |
| 407 } | 408 } |
| 408 } | 409 } |
| 409 | 410 |
| 410 int get length { | 411 int get length { |
| 411 return _count; | 412 return _count; |
| 412 } | 413 } |
| 413 | 414 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 426 while (node != null) { | 427 while (node != null) { |
| 427 if (node.value == value) return true; | 428 if (node.value == value) return true; |
| 428 if (initialSplayCount != _splayCount) { | 429 if (initialSplayCount != _splayCount) { |
| 429 throw new ConcurrentModificationError(this); | 430 throw new ConcurrentModificationError(this); |
| 430 } | 431 } |
| 431 if (node.right != null && visit(node.right)) return true; | 432 if (node.right != null && visit(node.right)) return true; |
| 432 node = node.left; | 433 node = node.left; |
| 433 } | 434 } |
| 434 return false; | 435 return false; |
| 435 } | 436 } |
| 437 |
| 436 return visit(_root); | 438 return visit(_root); |
| 437 } | 439 } |
| 438 | 440 |
| 439 Iterable<K> get keys => new _SplayTreeKeyIterable<K>(this); | 441 Iterable<K> get keys => new _SplayTreeKeyIterable<K>(this); |
| 440 | 442 |
| 441 Iterable<V> get values => new _SplayTreeValueIterable<K, V>(this); | 443 Iterable<V> get values => new _SplayTreeValueIterable<K, V>(this); |
| 442 | 444 |
| 443 String toString() { | 445 String toString() { |
| 444 return Maps.mapToString(this); | 446 return Maps.mapToString(this); |
| 445 } | 447 } |
| (...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 625 | 627 |
| 626 class _SplayTreeValueIterable<K, V> extends EfficientLengthIterable<V> { | 628 class _SplayTreeValueIterable<K, V> extends EfficientLengthIterable<V> { |
| 627 SplayTreeMap<K, V> _map; | 629 SplayTreeMap<K, V> _map; |
| 628 _SplayTreeValueIterable(this._map); | 630 _SplayTreeValueIterable(this._map); |
| 629 int get length => _map._count; | 631 int get length => _map._count; |
| 630 bool get isEmpty => _map._count == 0; | 632 bool get isEmpty => _map._count == 0; |
| 631 Iterator<V> get iterator => new _SplayTreeValueIterator<K, V>(_map); | 633 Iterator<V> get iterator => new _SplayTreeValueIterator<K, V>(_map); |
| 632 } | 634 } |
| 633 | 635 |
| 634 class _SplayTreeKeyIterator<K> extends _SplayTreeIterator<K, K> { | 636 class _SplayTreeKeyIterator<K> extends _SplayTreeIterator<K, K> { |
| 635 _SplayTreeKeyIterator(_SplayTree<K, _SplayTreeNode<K>> map): super(map); | 637 _SplayTreeKeyIterator(_SplayTree<K, _SplayTreeNode<K>> map) : super(map); |
| 636 K _getValue(_SplayTreeNode<K> node) => node.key; | 638 K _getValue(_SplayTreeNode<K> node) => node.key; |
| 637 } | 639 } |
| 638 | 640 |
| 639 class _SplayTreeValueIterator<K, V> extends _SplayTreeIterator<K, V> { | 641 class _SplayTreeValueIterator<K, V> extends _SplayTreeIterator<K, V> { |
| 640 _SplayTreeValueIterator(SplayTreeMap<K, V> map): super(map); | 642 _SplayTreeValueIterator(SplayTreeMap<K, V> map) : super(map); |
| 641 V _getValue(_SplayTreeNode<K> node) { | 643 V _getValue(_SplayTreeNode<K> node) { |
| 642 _SplayTreeMapNode<K, V> mapNode = | 644 _SplayTreeMapNode<K, V> mapNode = |
| 643 node as dynamic/*=_SplayTreeMapNode<K, V>*/; | 645 node as dynamic/*=_SplayTreeMapNode<K, V>*/; |
| 644 return mapNode.value; | 646 return mapNode.value; |
| 645 } | 647 } |
| 646 } | 648 } |
| 647 | 649 |
| 648 class _SplayTreeNodeIterator<K> | 650 class _SplayTreeNodeIterator<K> |
| 649 extends _SplayTreeIterator<K, _SplayTreeNode<K>> { | 651 extends _SplayTreeIterator<K, _SplayTreeNode<K>> { |
| 650 _SplayTreeNodeIterator(_SplayTree<K, _SplayTreeNode<K>> tree): super(tree); | 652 _SplayTreeNodeIterator(_SplayTree<K, _SplayTreeNode<K>> tree) : super(tree); |
| 651 _SplayTreeNodeIterator.startAt( | 653 _SplayTreeNodeIterator.startAt( |
| 652 _SplayTree<K, _SplayTreeNode<K>> tree, K startKey) | 654 _SplayTree<K, _SplayTreeNode<K>> tree, K startKey) |
| 653 : super.startAt(tree, startKey); | 655 : super.startAt(tree, startKey); |
| 654 _SplayTreeNode<K> _getValue(_SplayTreeNode<K> node) => node; | 656 _SplayTreeNode<K> _getValue(_SplayTreeNode<K> node) => node; |
| 655 } | 657 } |
| 656 | 658 |
| 657 | |
| 658 /** | 659 /** |
| 659 * A [Set] of objects that can be ordered relative to each other. | 660 * A [Set] of objects that can be ordered relative to each other. |
| 660 * | 661 * |
| 661 * The set is based on a self-balancing binary tree. It allows most operations | 662 * The set is based on a self-balancing binary tree. It allows most operations |
| 662 * in amortized logarithmic time. | 663 * in amortized logarithmic time. |
| 663 * | 664 * |
| 664 * Elements of the set are compared using the `compare` function passed in | 665 * Elements of the set are compared using the `compare` function passed in |
| 665 * the constructor, both for ordering and for equality. | 666 * the constructor, both for ordering and for equality. |
| 666 * If the set contains only an object `a`, then `set.contains(b)` | 667 * If the set contains only an object `a`, then `set.contains(b)` |
| 667 * will return `true` if and only if `compare(a, b) == 0`, | 668 * will return `true` if and only if `compare(a, b) == 0`, |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 709 _validKey = isValidKey ?? ((v) => v is E); | 710 _validKey = isValidKey ?? ((v) => v is E); |
| 710 | 711 |
| 711 /** | 712 /** |
| 712 * Creates a [SplayTreeSet] that contains all [elements]. | 713 * Creates a [SplayTreeSet] that contains all [elements]. |
| 713 * | 714 * |
| 714 * The set works as if created by `new SplayTreeSet<E>(compare, isValidKey)`. | 715 * The set works as if created by `new SplayTreeSet<E>(compare, isValidKey)`. |
| 715 * | 716 * |
| 716 * All the [elements] should be valid as arguments to the [compare] function. | 717 * All the [elements] should be valid as arguments to the [compare] function. |
| 717 */ | 718 */ |
| 718 factory SplayTreeSet.from(Iterable elements, | 719 factory SplayTreeSet.from(Iterable elements, |
| 719 [int compare(E key1, E key2), | 720 [int compare(E key1, E key2), bool isValidKey(potentialKey)]) { |
| 720 bool isValidKey(potentialKey)]) { | |
| 721 SplayTreeSet<E> result = new SplayTreeSet<E>(compare, isValidKey); | 721 SplayTreeSet<E> result = new SplayTreeSet<E>(compare, isValidKey); |
| 722 for (final element in elements) { | 722 for (final element in elements) { |
| 723 E e = element as Object/*=E*/; | 723 E e = element as Object/*=E*/; |
| 724 result.add(e); | 724 result.add(e); |
| 725 } | 725 } |
| 726 return result; | 726 return result; |
| 727 } | 727 } |
| 728 | 728 |
| 729 int _compare(E e1, E e2) => _comparator(e1, e2); | 729 int _compare(E e1, E e2) => _comparator(e1, e2); |
| 730 | 730 |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 837 var set = new SplayTreeSet<E>(_comparator, _validKey); | 837 var set = new SplayTreeSet<E>(_comparator, _validKey); |
| 838 set._count = _count; | 838 set._count = _count; |
| 839 set._root = _copyNode(_root); | 839 set._root = _copyNode(_root); |
| 840 return set; | 840 return set; |
| 841 } | 841 } |
| 842 | 842 |
| 843 // Copies the structure of a SplayTree into a new similar structure. | 843 // Copies the structure of a SplayTree into a new similar structure. |
| 844 // Works on _SplayTreeMapNode as well, but only copies the keys, | 844 // Works on _SplayTreeMapNode as well, but only copies the keys, |
| 845 _SplayTreeNode<E> _copyNode(_SplayTreeNode<E> node) { | 845 _SplayTreeNode<E> _copyNode(_SplayTreeNode<E> node) { |
| 846 if (node == null) return null; | 846 if (node == null) return null; |
| 847 return new _SplayTreeNode<E>(node.key)..left = _copyNode(node.left) | 847 return new _SplayTreeNode<E>(node.key) |
| 848 ..right = _copyNode(node.right); | 848 ..left = _copyNode(node.left) |
| 849 ..right = _copyNode(node.right); |
| 849 } | 850 } |
| 850 | 851 |
| 851 void clear() { _clear(); } | 852 void clear() { |
| 853 _clear(); |
| 854 } |
| 852 | 855 |
| 853 Set<E> toSet() => _clone(); | 856 Set<E> toSet() => _clone(); |
| 854 | 857 |
| 855 String toString() => IterableBase.iterableToFullString(this, '{', '}'); | 858 String toString() => IterableBase.iterableToFullString(this, '{', '}'); |
| 856 } | 859 } |
| OLD | NEW |