| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 /** Common parts of [HashSet] and [LinkedHashSet] implementations. */ | 7 class HashSet<E> extends Collection<E> implements Set<E> { |
| 8 abstract class _HashSetBase<E> extends Collection<E> implements Set<E> { | |
| 9 // Set. | |
| 10 bool isSubsetOf(Collection<E> other) { | |
| 11 // Deprecated, and using old signature. | |
| 12 Set otherSet; | |
| 13 if (other is Set) { | |
| 14 otherSet = other; | |
| 15 } else { | |
| 16 otherSet = other.toSet(); | |
| 17 } | |
| 18 return otherSet.containsAll(this); | |
| 19 } | |
| 20 | |
| 21 bool containsAll(Iterable<E> other) { | |
| 22 for (E object in other) { | |
| 23 if (!this.contains(object)) return false; | |
| 24 } | |
| 25 return true; | |
| 26 } | |
| 27 | |
| 28 Set<E> intersection(Set<E> other) { | |
| 29 Set<E> result = _newSet(); | |
| 30 if (other.length < this.length) { | |
| 31 for (E element in other) { | |
| 32 if (this.contains(element)) result.add(element); | |
| 33 } | |
| 34 } else { | |
| 35 for (E element in this) { | |
| 36 if (other.contains(element)) result.add(element); | |
| 37 } | |
| 38 } | |
| 39 return result; | |
| 40 } | |
| 41 | |
| 42 Set<E> union(Set<E> other) { | |
| 43 return _newSet()..addAll(this)..addAll(other); | |
| 44 } | |
| 45 | |
| 46 Set<E> difference(Set<E> other) { | |
| 47 HashSet<E> result = _newSet(); | |
| 48 for (E element in this) { | |
| 49 if (!other.contains(element)) result.add(element); | |
| 50 } | |
| 51 return result; | |
| 52 } | |
| 53 | |
| 54 String toString() => Collections.collectionToString(this); | |
| 55 } | |
| 56 | |
| 57 class HashSet<E> extends _HashSetBase<E> { | |
| 58 external HashSet(); | 8 external HashSet(); |
| 59 | 9 |
| 60 factory HashSet.from(Iterable<E> iterable) { | 10 factory HashSet.from(Iterable<E> iterable) { |
| 61 return new HashSet<E>()..addAll(iterable); | 11 return new HashSet<E>()..addAll(iterable); |
| 62 } | 12 } |
| 63 | 13 |
| 64 // Iterable. | 14 // Iterable. |
| 65 external Iterator<E> get iterator; | 15 external Iterator<E> get iterator; |
| 66 | 16 |
| 67 external int get length; | 17 external int get length; |
| 68 | 18 |
| 69 external bool get isEmpty; | 19 external bool get isEmpty; |
| 70 | 20 |
| 71 external bool contains(Object object); | 21 external bool contains(Object object); |
| 72 | 22 |
| 73 // Collection. | 23 // Collection. |
| 74 external void add(E element); | 24 external void add(E element); |
| 75 | 25 |
| 76 external void addAll(Iterable<E> objects); | 26 external void addAll(Iterable<E> objects); |
| 77 | 27 |
| 78 external bool remove(Object object); | 28 external bool remove(Object object); |
| 79 | 29 |
| 80 external void removeAll(Iterable objectsToRemove); | 30 external void removeAll(Iterable objectsToRemove); |
| 81 | 31 |
| 82 void retainAll(Iterable objectsToRetain) { | 32 void retainAll(Iterable objectsToRetain) { |
| 83 Set retainSet; | 33 IterableMixinWorkaround.retainAll(this, objectsToRetain); |
| 84 if (objectsToRetain is Set) { | |
| 85 retainSet = objectsToRetain; | |
| 86 } else { | |
| 87 retainSet = objectsToRetain.toSet(); | |
| 88 } | |
| 89 retainWhere(retainSet.contains); | |
| 90 } | 34 } |
| 91 | 35 |
| 92 external void removeWhere(bool test(E element)); | 36 external void removeWhere(bool test(E element)); |
| 93 | 37 |
| 94 external void retainWhere(bool test(E element)); | 38 external void retainWhere(bool test(E element)); |
| 95 | 39 |
| 96 external void clear(); | 40 external void clear(); |
| 97 | 41 |
| 98 // Set. | 42 // Set. |
| 99 Set<E> _newSet() => new HashSet<E>(); | 43 bool isSubsetOf(Collection<E> other) { |
| 44 // Deprecated, and using old signature. |
| 45 Set otherSet; |
| 46 if (other is Set) { |
| 47 otherSet = other; |
| 48 } else { |
| 49 otherSet = other.toSet(); |
| 50 } |
| 51 return IterableMixinWorkaround.setContainsAll(otherSet, this); |
| 52 } |
| 53 |
| 54 bool containsAll(Iterable<E> other) { |
| 55 return IterableMixinWorkaround.setContainsAll(this, other); |
| 56 } |
| 57 |
| 58 Set<E> intersection(Set<E> other) { |
| 59 return IterableMixinWorkaround.setIntersection( |
| 60 this, other, new HashSet<E>()); |
| 61 } |
| 62 |
| 63 Set<E> union(Set<E> other) { |
| 64 return IterableMixinWorkaround.setUnion(this, other, new HashSet<E>()); |
| 65 } |
| 66 |
| 67 Set<E> difference(Set<E> other) { |
| 68 return IterableMixinWorkaround.setDifference(this, other, new HashSet<E>()); |
| 69 } |
| 70 |
| 71 String toString() => Collections.collectionToString(this); |
| 100 } | 72 } |
| OLD | NEW |