| 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 IterableBase<E> implements Set<E> { |
| 9 | 9 |
| 10 // Set. | 10 // Set. |
| 11 bool containsAll(Iterable<Object> other) { | 11 bool containsAll(Iterable<Object> other) { |
| 12 for (Object object in other) { | 12 for (Object object in other) { |
| 13 if (!this.contains(object)) return false; | 13 if (!this.contains(object)) return false; |
| 14 } | 14 } |
| 15 return true; | 15 return true; |
| 16 } | 16 } |
| 17 | 17 |
| 18 /** Create a new Set of the same type as this. */ | 18 /** Create a new Set of the same type as this. */ |
| 19 HashSet<E> _newSet(); | 19 HashSet<E> _newSet(); |
| 20 | 20 |
| 21 Set<E> intersection(Set<Object> other) { | 21 Set<E> intersection(Set<Object> other) { |
| 22 Set<E> result = _newSet(); | 22 Set<E> result = _newSet(); |
| 23 if (other.length < this.length) { | 23 for (var element in this) { |
| 24 for (var element in other) { | 24 if (other.contains(element)) result.add(element); |
| 25 if (this.contains(element)) result.add(element); | |
| 26 } | |
| 27 } else { | |
| 28 for (E element in this) { | |
| 29 if (other.contains(element)) result.add(element); | |
| 30 } | |
| 31 } | 25 } |
| 32 return result; | 26 return result; |
| 33 } | 27 } |
| 34 | 28 |
| 35 Set<E> union(Set<E> other) { | 29 Set<E> union(Set<E> other) { |
| 36 return _newSet()..addAll(this)..addAll(other); | 30 return _newSet()..addAll(this)..addAll(other); |
| 37 } | 31 } |
| 38 | 32 |
| 39 Set<E> difference(Set<E> other) { | 33 Set<E> difference(Set<E> other) { |
| 40 HashSet<E> result = _newSet(); | 34 HashSet<E> result = _newSet(); |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 /** | 116 /** |
| 123 * Create a hash set containing the elements of [iterable]. | 117 * Create a hash set containing the elements of [iterable]. |
| 124 * | 118 * |
| 125 * Creates a hash set as by `new HashSet<E>()` and adds each element of | 119 * Creates a hash set as by `new HashSet<E>()` and adds each element of |
| 126 * `iterable` to this set in the order they are iterated. | 120 * `iterable` to this set in the order they are iterated. |
| 127 */ | 121 */ |
| 128 factory HashSet.from(Iterable<E> iterable) { | 122 factory HashSet.from(Iterable<E> iterable) { |
| 129 return new HashSet<E>()..addAll(iterable); | 123 return new HashSet<E>()..addAll(iterable); |
| 130 } | 124 } |
| 131 } | 125 } |
| OLD | NEW |