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 /** Common parts of [HashSet] and [LinkedHashSet] implementations. */ |
8 abstract class _HashSetBase<E> extends IterableBase<E> implements Set<E> { | 8 abstract class _HashSetBase<E> extends SetBase<E> { |
9 | |
10 // Set. | |
11 bool containsAll(Iterable<Object> other) { | |
12 for (Object object in other) { | |
13 if (!this.contains(object)) return false; | |
14 } | |
15 return true; | |
16 } | |
17 | |
18 /** Create a new Set of the same type as this. */ | |
19 HashSet<E> _newSet(); | |
20 | |
21 Set<E> intersection(Set<Object> other) { | |
22 Set<E> result = _newSet(); | |
23 for (var element in this) { | |
24 if (other.contains(element)) result.add(element); | |
25 } | |
26 return result; | |
27 } | |
28 | |
29 Set<E> union(Set<E> other) { | |
30 return _newSet()..addAll(this)..addAll(other); | |
31 } | |
32 | |
33 Set<E> difference(Set<E> other) { | |
34 HashSet<E> result = _newSet(); | |
35 for (E element in this) { | |
36 if (!other.contains(element)) result.add(element); | |
37 } | |
38 return result; | |
39 } | |
40 | |
41 void _retainAll(Iterable objectsToRetain, bool isValidKey(Object o)) { | |
42 // TODO(lrn): Consider optimizing table based versions by | |
43 // building a new table of the entries to retain. | |
44 Set retainSet = _newSet(); | |
45 for (Object o in objectsToRetain) { | |
46 if (isValidKey(o)) { | |
47 retainSet.add(o); | |
48 } | |
49 } | |
50 retainWhere(retainSet.contains); | |
51 } | |
52 | |
53 List<E> toList({bool growable: true}) { | |
54 List<E> result = growable ? (new List<E>()..length = this.length) | |
55 : new List<E>(this.length); | |
56 int i = 0; | |
57 for (E element in this) result[i++] = element; | |
58 return result; | |
59 } | |
60 | |
61 Set<E> toSet() => _newSet()..addAll(this); | 9 Set<E> toSet() => _newSet()..addAll(this); |
62 | 10 |
63 String toString() => IterableMixinWorkaround.toStringIterable(this, '{', '}'); | 11 String toString() => IterableMixinWorkaround.toStringIterable(this, '{', '}'); |
64 } | 12 } |
65 | 13 |
66 /** | 14 /** |
67 * An unordered hash-table based [Set] implementation. | 15 * An unordered hash-table based [Set] implementation. |
68 * | 16 * |
69 * The elements of a `HashSet` must have consistent equality | 17 * The elements of a `HashSet` must have consistent equality |
70 * and hashCode implementations. This means that the equals operation | 18 * and hashCode implementations. This means that the equals operation |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
125 } | 73 } |
126 | 74 |
127 /** | 75 /** |
128 * Provides an iterator that iterates over the elements of this set. | 76 * Provides an iterator that iterates over the elements of this set. |
129 * | 77 * |
130 * The order of iteration is unspecified, | 78 * The order of iteration is unspecified, |
131 * but consistent between changes to the set. | 79 * but consistent between changes to the set. |
132 */ | 80 */ |
133 Iterator<E> get iterator; | 81 Iterator<E> get iterator; |
134 } | 82 } |
OLD | NEW |