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

Side by Side Diff: tool/input_sdk/lib/collection/splay_tree.dart

Issue 1977003002: Update dart:collection. (Closed) Base URL: https://github.com/dart-lang/dev_compiler.git@master
Patch Set: Created 4 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 unified diff | Download patch
« no previous file with comments | « tool/input_sdk/lib/collection/queue.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 269 matching lines...) Expand 10 before | Expand all | Expand 10 after
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),
288 bool isValidKey(potentialKey)]) { 288 bool isValidKey(potentialKey)]) {
289 SplayTreeMap<K, V> result = new SplayTreeMap<K, V>(compare, isValidKey); 289 SplayTreeMap<K, V> result = new SplayTreeMap<K, V>(compare, isValidKey);
290 other.forEach((k, v) { result[k as K] = v as V; }); 290 other.forEach((k, v) { result[k as Object/*=K*/] = v as Object/*=V*/; });
291 return result; 291 return result;
292 } 292 }
293 293
294 /** 294 /**
295 * Creates a [SplayTreeMap] where the keys and values are computed from the 295 * Creates a [SplayTreeMap] where the keys and values are computed from the
296 * [iterable]. 296 * [iterable].
297 * 297 *
298 * For each element of the [iterable] this constructor computes a key/value 298 * For each element of the [iterable] this constructor computes a key/value
299 * pair, by applying [key] and [value] respectively. 299 * pair, by applying [key] and [value] respectively.
300 * 300 *
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
332 return map; 332 return map;
333 } 333 }
334 334
335 int _compare(K key1, K key2) => _comparator(key1, key2); 335 int _compare(K key1, K key2) => _comparator(key1, key2);
336 336
337 SplayTreeMap._internal(); 337 SplayTreeMap._internal();
338 338
339 V operator [](Object key) { 339 V operator [](Object key) {
340 if (!_validKey(key)) return null; 340 if (!_validKey(key)) return null;
341 if (_root != null) { 341 if (_root != null) {
342 int comp = _splay(key as K); 342 int comp = _splay(key as dynamic/*=K*/);
343 if (comp == 0) { 343 if (comp == 0) {
344 return _root.value; 344 return _root.value;
345 } 345 }
346 } 346 }
347 return null; 347 return null;
348 } 348 }
349 349
350 V remove(Object key) { 350 V remove(Object key) {
351 if (!_validKey(key)) return null; 351 if (!_validKey(key)) return null;
352 _SplayTreeMapNode<K, V> mapRoot = _remove(key as K); 352 _SplayTreeMapNode<K, V> mapRoot = _remove(key as dynamic/*=K*/);
353 if (mapRoot != null) return mapRoot.value; 353 if (mapRoot != null) return mapRoot.value;
354 return null; 354 return null;
355 } 355 }
356 356
357 void operator []=(K key, V value) { 357 void operator []=(K key, V value) {
358 if (key == null) throw new ArgumentError(key); 358 if (key == null) throw new ArgumentError(key);
359 // Splay on the key to move the last node on the search path for 359 // Splay on the key to move the last node on the search path for
360 // the key to the root of the tree. 360 // the key to the root of the tree.
361 int comp = _splay(key); 361 int comp = _splay(key);
362 if (comp == 0) { 362 if (comp == 0) {
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
409 409
410 int get length { 410 int get length {
411 return _count; 411 return _count;
412 } 412 }
413 413
414 void clear() { 414 void clear() {
415 _clear(); 415 _clear();
416 } 416 }
417 417
418 bool containsKey(Object key) { 418 bool containsKey(Object key) {
419 return _validKey(key) && _splay(key as K) == 0; 419 return _validKey(key) && _splay(key as dynamic/*=K*/) == 0;
420 } 420 }
421 421
422 bool containsValue(Object value) { 422 bool containsValue(Object value) {
423 bool found = false; 423 bool found = false;
424 int initialSplayCount = _splayCount; 424 int initialSplayCount = _splayCount;
425 bool visit(_SplayTreeMapNode node) { 425 bool visit(_SplayTreeMapNode node) {
426 while (node != null) { 426 while (node != null) {
427 if (node.value == value) return true; 427 if (node.value == value) return true;
428 if (initialSplayCount != _splayCount) { 428 if (initialSplayCount != _splayCount) {
429 throw new ConcurrentModificationError(this); 429 throw new ConcurrentModificationError(this);
(...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after
634 } 634 }
635 635
636 class _SplayTreeKeyIterator<K> extends _SplayTreeIterator<K, K> { 636 class _SplayTreeKeyIterator<K> extends _SplayTreeIterator<K, K> {
637 _SplayTreeKeyIterator(_SplayTree<K, _SplayTreeNode<K>> map): super(map); 637 _SplayTreeKeyIterator(_SplayTree<K, _SplayTreeNode<K>> map): super(map);
638 K _getValue(_SplayTreeNode<K> node) => node.key; 638 K _getValue(_SplayTreeNode<K> node) => node.key;
639 } 639 }
640 640
641 class _SplayTreeValueIterator<K, V> extends _SplayTreeIterator<K, V> { 641 class _SplayTreeValueIterator<K, V> extends _SplayTreeIterator<K, V> {
642 _SplayTreeValueIterator(SplayTreeMap<K, V> map): super(map); 642 _SplayTreeValueIterator(SplayTreeMap<K, V> map): super(map);
643 V _getValue(_SplayTreeNode<K> node) { 643 V _getValue(_SplayTreeNode<K> node) {
644 _SplayTreeMapNode<K, V> mapNode = node as _SplayTreeMapNode<K, V>; 644 _SplayTreeMapNode<K, V> mapNode =
645 node as dynamic/*=_SplayTreeMapNode<K, V>*/;
645 return mapNode.value; 646 return mapNode.value;
646 } 647 }
647 } 648 }
648 649
649 class _SplayTreeNodeIterator<K> 650 class _SplayTreeNodeIterator<K>
650 extends _SplayTreeIterator<K, _SplayTreeNode<K>> { 651 extends _SplayTreeIterator<K, _SplayTreeNode<K>> {
651 _SplayTreeNodeIterator(_SplayTree<K, _SplayTreeNode<K>> tree): super(tree); 652 _SplayTreeNodeIterator(_SplayTree<K, _SplayTreeNode<K>> tree): super(tree);
652 _SplayTreeNodeIterator.startAt( 653 _SplayTreeNodeIterator.startAt(
653 _SplayTree<K, _SplayTreeNode<K>> tree, K startKey) 654 _SplayTree<K, _SplayTreeNode<K>> tree, K startKey)
654 : super.startAt(tree, startKey); 655 : super.startAt(tree, startKey);
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
698 * 699 *
699 * If [isValidKey] is provided, only values satisfying `isValidKey(other)` 700 * If [isValidKey] is provided, only values satisfying `isValidKey(other)`
700 * are compared using the `compare` method in the methods mentioned above. 701 * 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 702 * If the `isValidKey` function returns false for an object, it is assumed to
702 * not be in the set. 703 * not be in the set.
703 * 704 *
704 * If omitted, the `isValidKey` function defaults to checking against the 705 * If omitted, the `isValidKey` function defaults to checking against the
705 * type parameter: `other is E`. 706 * type parameter: `other is E`.
706 */ 707 */
707 SplayTreeSet([int compare(E key1, E key2), bool isValidKey(potentialKey)]) 708 SplayTreeSet([int compare(E key1, E key2), bool isValidKey(potentialKey)])
708 : _comparator = compare ?? Comparable.compare as Comparator<E>, 709 : _comparator =
710 compare ?? Comparable.compare as dynamic/*=Comparator<E>*/,
709 _validKey = isValidKey ?? ((v) => v is E); 711 _validKey = isValidKey ?? ((v) => v is E);
710 712
711 /** 713 /**
712 * Creates a [SplayTreeSet] that contains all [elements]. 714 * Creates a [SplayTreeSet] that contains all [elements].
713 * 715 *
714 * The set works as if created by `new SplayTreeSet<E>(compare, isValidKey)`. 716 * The set works as if created by `new SplayTreeSet<E>(compare, isValidKey)`.
715 * 717 *
716 * All the [elements] should be valid as arguments to the [compare] function. 718 * All the [elements] should be valid as arguments to the [compare] function.
717 */ 719 */
718 factory SplayTreeSet.from(Iterable elements, 720 factory SplayTreeSet.from(Iterable elements,
719 [int compare(E key1, E key2), 721 [int compare(E key1, E key2),
720 bool isValidKey(potentialKey)]) { 722 bool isValidKey(potentialKey)]) {
721 SplayTreeSet<E> result = new SplayTreeSet<E>(compare, isValidKey); 723 SplayTreeSet<E> result = new SplayTreeSet<E>(compare, isValidKey);
722 for (final element in elements) { 724 for (final element in elements) {
723 result.add(element as E); 725 E e = element as Object/*=E*/;
726 result.add(e);
724 } 727 }
725 return result; 728 return result;
726 } 729 }
727 730
728 int _compare(E e1, E e2) => _comparator(e1, e2); 731 int _compare(E e1, E e2) => _comparator(e1, e2);
729 732
730 // From Iterable. 733 // From Iterable.
731 734
732 Iterator<E> get iterator => new _SplayTreeKeyIterator<E>(this); 735 Iterator<E> get iterator => new _SplayTreeKeyIterator<E>(this);
733 736
(...skipping 12 matching lines...) Expand all
746 } 749 }
747 750
748 E get single { 751 E get single {
749 if (_count == 0) throw IterableElementError.noElement(); 752 if (_count == 0) throw IterableElementError.noElement();
750 if (_count > 1) throw IterableElementError.tooMany(); 753 if (_count > 1) throw IterableElementError.tooMany();
751 return _root.key; 754 return _root.key;
752 } 755 }
753 756
754 // From Set. 757 // From Set.
755 bool contains(Object object) { 758 bool contains(Object object) {
756 return _validKey(object) && _splay(object as E) == 0; 759 return _validKey(object) && _splay(object as dynamic/*=E*/) == 0;
757 } 760 }
758 761
759 bool add(E element) { 762 bool add(E element) {
760 int compare = _splay(element); 763 int compare = _splay(element);
761 if (compare == 0) return false; 764 if (compare == 0) return false;
762 _addNewRoot(new _SplayTreeNode(element), compare); 765 _addNewRoot(new _SplayTreeNode(element), compare);
763 return true; 766 return true;
764 } 767 }
765 768
766 bool remove(Object object) { 769 bool remove(Object object) {
767 if (!_validKey(object)) return false; 770 if (!_validKey(object)) return false;
768 return _remove(object as E) != null; 771 return _remove(object as dynamic/*=E*/) != null;
769 } 772 }
770 773
771 void addAll(Iterable<E> elements) { 774 void addAll(Iterable<E> elements) {
772 for (E element in elements) { 775 for (E element in elements) {
773 int compare = _splay(element); 776 int compare = _splay(element);
774 if (compare != 0) { 777 if (compare != 0) {
775 _addNewRoot(new _SplayTreeNode(element), compare); 778 _addNewRoot(new _SplayTreeNode(element), compare);
776 } 779 }
777 } 780 }
778 } 781 }
779 782
780 void removeAll(Iterable<Object> elements) { 783 void removeAll(Iterable<Object> elements) {
781 for (Object element in elements) { 784 for (Object element in elements) {
782 if (_validKey(element)) _remove(element as E); 785 if (_validKey(element)) _remove(element as dynamic/*=E*/);
783 } 786 }
784 } 787 }
785 788
786 void retainAll(Iterable<Object> elements) { 789 void retainAll(Iterable<Object> elements) {
787 // Build a set with the same sense of equality as this set. 790 // Build a set with the same sense of equality as this set.
788 SplayTreeSet<E> retainSet = new SplayTreeSet<E>(_comparator, _validKey); 791 SplayTreeSet<E> retainSet = new SplayTreeSet<E>(_comparator, _validKey);
789 int modificationCount = _modificationCount; 792 int modificationCount = _modificationCount;
790 for (Object object in elements) { 793 for (Object object in elements) {
791 if (modificationCount != _modificationCount) { 794 if (modificationCount != _modificationCount) {
792 // The iterator should not have side effects. 795 // The iterator should not have side effects.
793 throw new ConcurrentModificationError(this); 796 throw new ConcurrentModificationError(this);
794 } 797 }
795 // Equivalent to this.contains(object). 798 // Equivalent to this.contains(object).
796 if (_validKey(object) && _splay(object as E) == 0) { 799 if (_validKey(object) && _splay(object as dynamic/*=E*/) == 0) {
797 retainSet.add(_root.key); 800 retainSet.add(_root.key);
798 } 801 }
799 } 802 }
800 // Take over the elements from the retained set, if it differs. 803 // Take over the elements from the retained set, if it differs.
801 if (retainSet._count != _count) { 804 if (retainSet._count != _count) {
802 _root = retainSet._root; 805 _root = retainSet._root;
803 _count = retainSet._count; 806 _count = retainSet._count;
804 _modificationCount++; 807 _modificationCount++;
805 } 808 }
806 } 809 }
807 810
808 E lookup(Object object) { 811 E lookup(Object object) {
809 if (!_validKey(object)) return null; 812 if (!_validKey(object)) return null;
810 int comp = _splay(object as E); 813 int comp = _splay(object as dynamic/*=E*/);
811 if (comp != 0) return null; 814 if (comp != 0) return null;
812 return _root.key; 815 return _root.key;
813 } 816 }
814 817
815 Set<E> intersection(Set<Object> other) { 818 Set<E> intersection(Set<Object> other) {
816 Set<E> result = new SplayTreeSet<E>(_comparator, _validKey); 819 Set<E> result = new SplayTreeSet<E>(_comparator, _validKey);
817 for (E element in this) { 820 for (E element in this) {
818 if (other.contains(element)) result.add(element); 821 if (other.contains(element)) result.add(element);
819 } 822 }
820 return result; 823 return result;
(...skipping 25 matching lines...) Expand all
846 return new _SplayTreeNode<E>(node.key)..left = _copyNode(node.left) 849 return new _SplayTreeNode<E>(node.key)..left = _copyNode(node.left)
847 ..right = _copyNode(node.right); 850 ..right = _copyNode(node.right);
848 } 851 }
849 852
850 void clear() { _clear(); } 853 void clear() { _clear(); }
851 854
852 Set<E> toSet() => _clone(); 855 Set<E> toSet() => _clone();
853 856
854 String toString() => IterableBase.iterableToFullString(this, '{', '}'); 857 String toString() => IterableBase.iterableToFullString(this, '{', '}');
855 } 858 }
OLDNEW
« no previous file with comments | « tool/input_sdk/lib/collection/queue.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698