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 class HashSet<E> extends Collection<E> implements Set<E> { | 7 class HashSet<E> extends Collection<E> implements Set<E> { |
| 8 static const int _INITIAL_CAPACITY = 8; | 8 static const int _INITIAL_CAPACITY = 8; |
| 9 final _HashTable<E> _table; | 9 final _HashTable<E> _table; |
| 10 | 10 |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 79 | 79 |
| 80 void retainMatching(bool test(E element)) { | 80 void retainMatching(bool test(E element)) { |
| 81 _filterMatching(test, false); | 81 _filterMatching(test, false); |
| 82 } | 82 } |
| 83 | 83 |
| 84 void clear() { | 84 void clear() { |
| 85 _table._clear(); | 85 _table._clear(); |
| 86 } | 86 } |
| 87 | 87 |
| 88 // Set. | 88 // Set. |
| 89 bool isSubsetOf(Collection<E> collection) { | 89 bool isSubsetOf(Set<E> other) { |
|
floitsch
2013/03/12 16:25:00
bool isSubsetOf(Set<E> other) => other.containsAll
Lasse Reichstein Nielsen
2013/03/13 13:01:42
The resason is the usual one: I don't want public,
| |
| 90 Set otherSet; | 90 if (length > other.length) return false; |
| 91 if (collection is Set) { | 91 for (E element in this) { |
| 92 otherSet = collection; | 92 if (!other.contains(element)) return false; |
| 93 } else { | |
| 94 otherSet = collection.toSet(); | |
| 95 } | 93 } |
| 96 return otherSet.containsAll(this); | 94 return true; |
| 97 } | 95 } |
| 98 | 96 |
| 99 bool containsAll(Collection<E> collection) { | 97 bool containsAll(Set<E> other) { |
| 100 for (E element in collection) { | 98 if (other.length > length) return false; |
| 99 for (E element in other) { | |
| 101 if (!this.contains(element)) return false; | 100 if (!this.contains(element)) return false; |
| 102 } | 101 } |
| 103 return true; | 102 return true; |
| 104 } | 103 } |
| 105 | 104 |
| 106 Set<E> intersection(Collection<E> other) { | 105 Set<E> intersection(Set<E> other) { |
| 106 Set<E> smaller; | |
| 107 Set<E> larger; | |
| 108 if (length < other.length) { | |
| 109 smaller = this; | |
| 110 larger = other; | |
| 111 } else { | |
| 112 smaller = other; | |
| 113 larger = this; | |
| 114 } | |
| 107 Set<E> result = new HashSet<E>(); | 115 Set<E> result = new HashSet<E>(); |
| 108 for (E element in other) { | 116 for (E element in smaller) { |
| 109 if (this.contains(element)) { | 117 if (larger.contains(element)) { |
| 110 result.add(element); | 118 result.add(element); |
| 111 } | 119 } |
| 112 } | 120 } |
| 121 return result; | |
| 122 } | |
| 123 | |
| 124 Set<E> union(Set<E> other) { | |
| 125 Set<E> result = new HashSet<E>(); | |
| 126 result.addAll(this); | |
| 127 result.addAll(other); | |
| 128 return result; | |
| 129 } | |
| 130 | |
| 131 Set<E> difference(Set<E> other) { | |
| 132 Set<E> result = new HashSet<E>(); | |
| 133 for (E element in this) { | |
| 134 if (!other.contains(element)) { | |
| 135 result.add(element); | |
| 136 } | |
| 137 } | |
| 113 return result; | 138 return result; |
| 114 } | 139 } |
| 115 | 140 |
| 116 String toString() => Collections.collectionToString(this); | 141 String toString() => Collections.collectionToString(this); |
| 117 } | 142 } |
| OLD | NEW |