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 class HashSet<E> extends Collection<E> implements Set<E> { | 7 /** Common parts of [HashSet] and [LinkedHashSet] implementations. */ |
| 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> { |
8 external HashSet(); | 58 external HashSet(); |
9 | 59 |
10 factory HashSet.from(Iterable<E> iterable) { | 60 factory HashSet.from(Iterable<E> iterable) { |
11 return new HashSet<E>()..addAll(iterable); | 61 return new HashSet<E>()..addAll(iterable); |
12 } | 62 } |
13 | 63 |
14 // Iterable. | 64 // Iterable. |
15 external Iterator<E> get iterator; | 65 external Iterator<E> get iterator; |
16 | 66 |
17 external int get length; | 67 external int get length; |
18 | 68 |
19 external bool get isEmpty; | 69 external bool get isEmpty; |
20 | 70 |
21 external bool contains(Object object); | 71 external bool contains(Object object); |
22 | 72 |
23 // Collection. | 73 // Collection. |
24 external void add(E element); | 74 external void add(E element); |
25 | 75 |
26 external void addAll(Iterable<E> objects); | 76 external void addAll(Iterable<E> objects); |
27 | 77 |
28 external bool remove(Object object); | 78 external bool remove(Object object); |
29 | 79 |
30 external void removeAll(Iterable objectsToRemove); | 80 external void removeAll(Iterable objectsToRemove); |
31 | 81 |
32 void retainAll(Iterable objectsToRetain) { | 82 void retainAll(Iterable objectsToRetain) { |
33 IterableMixinWorkaround.retainAll(this, objectsToRetain); | 83 Set retainSet; |
| 84 if (objectsToRetain is Set) { |
| 85 retainSet = objectsToRetain; |
| 86 } else { |
| 87 retainSet = objectsToRetain.toSet(); |
| 88 } |
| 89 retainWhere(retainSet.contains); |
34 } | 90 } |
35 | 91 |
36 external void removeWhere(bool test(E element)); | 92 external void removeWhere(bool test(E element)); |
37 | 93 |
38 external void retainWhere(bool test(E element)); | 94 external void retainWhere(bool test(E element)); |
39 | 95 |
40 external void clear(); | 96 external void clear(); |
41 | 97 |
42 // Set. | 98 // Set. |
43 bool isSubsetOf(Collection<E> other) { | 99 Set<E> _newSet() => new HashSet<E>(); |
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); | |
72 } | 100 } |
OLD | NEW |