Chromium Code Reviews| 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 | 9 |
| 10 // Set. | 10 Set<E> difference(Set<Object> other) { |
| 11 bool containsAll(Iterable<Object> other) { | 11 // Start from an empty set and add elements, |
| 12 for (Object object in other) { | 12 // unlike SetBase which starts from toSet() and removes elements. |
|
Søren Gjesse
2014/05/22 09:25:52
Move the comment to before the method, and say thi
Lasse Reichstein Nielsen
2014/05/22 09:41:37
Done.
| |
| 13 if (!this.contains(object)) return false; | 13 Set<E> result = _newSet(); |
| 14 for (var element in this) { | |
| 15 if (!other.contains(element)) result.add(element); | |
| 14 } | 16 } |
| 15 return true; | 17 return result; |
| 16 } | 18 } |
| 17 | 19 |
| 18 /** Create a new Set of the same type as this. */ | |
| 19 HashSet<E> _newSet(); | |
| 20 | |
| 21 Set<E> intersection(Set<Object> other) { | 20 Set<E> intersection(Set<Object> other) { |
| 21 // Start from an empty set and add elements, | |
| 22 // unlike SetBase which starts from toSet() and removes elements. | |
| 22 Set<E> result = _newSet(); | 23 Set<E> result = _newSet(); |
| 23 for (var element in this) { | 24 for (var element in this) { |
| 24 if (other.contains(element)) result.add(element); | 25 if (other.contains(element)) result.add(element); |
| 25 } | 26 } |
| 26 return result; | 27 return result; |
| 27 } | 28 } |
| 28 | 29 |
| 29 Set<E> union(Set<E> other) { | 30 Set<E> _newSet(); |
| 30 return _newSet()..addAll(this)..addAll(other); | |
| 31 } | |
| 32 | 31 |
| 33 Set<E> difference(Set<E> other) { | 32 // Subclasses can optimize this further. |
| 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); | 33 Set<E> toSet() => _newSet()..addAll(this); |
| 62 | 34 |
| 63 String toString() => IterableMixinWorkaround.toStringIterable(this, '{', '}'); | 35 String toString() => IterableMixinWorkaround.toStringIterable(this, '{', '}'); |
| 64 } | 36 } |
| 65 | 37 |
| 66 /** | 38 /** |
| 67 * An unordered hash-table based [Set] implementation. | 39 * An unordered hash-table based [Set] implementation. |
| 68 * | 40 * |
| 69 * The elements of a `HashSet` must have consistent equality | 41 * The elements of a `HashSet` must have consistent equality |
| 70 * and hashCode implementations. This means that the equals operation | 42 * and hashCode implementations. This means that the equals operation |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 125 } | 97 } |
| 126 | 98 |
| 127 /** | 99 /** |
| 128 * Provides an iterator that iterates over the elements of this set. | 100 * Provides an iterator that iterates over the elements of this set. |
| 129 * | 101 * |
| 130 * The order of iteration is unspecified, | 102 * The order of iteration is unspecified, |
| 131 * but consistent between changes to the set. | 103 * but consistent between changes to the set. |
| 132 */ | 104 */ |
| 133 Iterator<E> get iterator; | 105 Iterator<E> get iterator; |
| 134 } | 106 } |
| OLD | NEW |