| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 /** | 5 /** |
| 6 * This class is the public interface of a set. A set is a collection | 6 * This class is the public interface of a set. A set is a collection |
| 7 * without duplicates. | 7 * without duplicates. |
| 8 */ | 8 */ |
| 9 abstract class Set<E> extends Collection<E> { | 9 abstract class Set<E> extends Collection<E> { |
| 10 factory Set() => new _HashSetImpl<E>(); | 10 factory Set() => new _HashSetImpl<E>(); |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 abstract class HashSet<E> extends Set<E> { | 70 abstract class HashSet<E> extends Set<E> { |
| 71 factory HashSet() => new _HashSetImpl<E>(); | 71 factory HashSet() => new _HashSetImpl<E>(); |
| 72 | 72 |
| 73 /** | 73 /** |
| 74 * Creates a [Set] that contains all elements of [other]. | 74 * Creates a [Set] that contains all elements of [other]. |
| 75 */ | 75 */ |
| 76 factory HashSet.from(Iterable<E> other) => new _HashSetImpl<E>.from(other); | 76 factory HashSet.from(Iterable<E> other) => new _HashSetImpl<E>.from(other); |
| 77 } | 77 } |
| 78 | 78 |
| 79 | 79 |
| 80 class _HashSetImpl<E> implements HashSet<E> { | 80 class _HashSetImpl<E> extends Iterable<E> implements HashSet<E> { |
| 81 | 81 |
| 82 _HashSetImpl() { | 82 _HashSetImpl() { |
| 83 _backingMap = new _HashMapImpl<E, E>(); | 83 _backingMap = new _HashMapImpl<E, E>(); |
| 84 } | 84 } |
| 85 | 85 |
| 86 factory _HashSetImpl.from(Iterable<E> other) { | 86 factory _HashSetImpl.from(Iterable<E> other) { |
| 87 Set<E> set = new _HashSetImpl<E>(); | 87 Set<E> set = new _HashSetImpl<E>(); |
| 88 for (final e in other) { | 88 for (final e in other) { |
| 89 set.add(e); | 89 set.add(e); |
| 90 } | 90 } |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 138 return contains(value); | 138 return contains(value); |
| 139 }); | 139 }); |
| 140 } | 140 } |
| 141 | 141 |
| 142 void forEach(void f(E element)) { | 142 void forEach(void f(E element)) { |
| 143 _backingMap.forEach((E key, E value) { | 143 _backingMap.forEach((E key, E value) { |
| 144 f(key); | 144 f(key); |
| 145 }); | 145 }); |
| 146 } | 146 } |
| 147 | 147 |
| 148 Set mappedBy(f(E element)) { | |
| 149 Set result = new Set(); | |
| 150 _backingMap.forEach((E key, E value) { | |
| 151 result.add(f(key)); | |
| 152 }); | |
| 153 return result; | |
| 154 } | |
| 155 | |
| 156 Dynamic reduce(Dynamic initialValue, | |
| 157 Dynamic combine(Dynamic previousValue, E element)) { | |
| 158 return Collections.reduce(this, initialValue, combine); | |
| 159 } | |
| 160 | |
| 161 Set<E> where(bool f(E element)) { | |
| 162 Set<E> result = new Set<E>(); | |
| 163 _backingMap.forEach((E key, E value) { | |
| 164 if (f(key)) result.add(key); | |
| 165 }); | |
| 166 return result; | |
| 167 } | |
| 168 | |
| 169 bool every(bool f(E element)) { | |
| 170 Collection<E> keys = _backingMap.keys; | |
| 171 return keys.every(f); | |
| 172 } | |
| 173 | |
| 174 bool some(bool f(E element)) { | |
| 175 Collection<E> keys = _backingMap.keys; | |
| 176 return keys.some(f); | |
| 177 } | |
| 178 | |
| 179 bool get isEmpty { | 148 bool get isEmpty { |
| 180 return _backingMap.isEmpty; | 149 return _backingMap.isEmpty; |
| 181 } | 150 } |
| 182 | 151 |
| 183 int get length { | 152 int get length { |
| 184 return _backingMap.length; | 153 return _backingMap.length; |
| 185 } | 154 } |
| 186 | 155 |
| 187 Iterator<E> get iterator => new _HashSetIterator<E>(this); | 156 Iterator<E> get iterator => new _HashSetIterator<E>(this); |
| 188 | 157 |
| 189 String toString() { | 158 String toString() { |
| 190 return Collections.collectionToString(this); | 159 return Collections.collectionToString(this); |
| 191 } | 160 } |
| 192 | 161 |
| 193 List<E> toList() { | |
| 194 return new List<E>.from(this); | |
| 195 } | |
| 196 | |
| 197 Set<E> toSet() { | |
| 198 return new Set<E>.from(this); | |
| 199 } | |
| 200 | |
| 201 // The map backing this set. The associations in this map are all | 162 // The map backing this set. The associations in this map are all |
| 202 // of the form element -> element. If a value is not in the map, | 163 // of the form element -> element. If a value is not in the map, |
| 203 // then it is not in the set. | 164 // then it is not in the set. |
| 204 _HashMapImpl<E, E> _backingMap; | 165 _HashMapImpl<E, E> _backingMap; |
| 205 } | 166 } |
| 206 | 167 |
| 207 class _HashSetIterator<E> implements Iterator<E> { | 168 class _HashSetIterator<E> implements Iterator<E> { |
| 208 | 169 |
| 209 _HashSetIterator(_HashSetImpl<E> set) | 170 _HashSetIterator(_HashSetImpl<E> set) |
| 210 : _keysIterator = set._backingMap._keys.iterator; | 171 : _keysIterator = set._backingMap._keys.iterator; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 223 do { | 184 do { |
| 224 result = _keysIterator.moveNext(); | 185 result = _keysIterator.moveNext(); |
| 225 } while (result && | 186 } while (result && |
| 226 (_keysIterator.current == null || | 187 (_keysIterator.current == null || |
| 227 identical(_keysIterator.current, _HashMapImpl._DELETED_KEY))); | 188 identical(_keysIterator.current, _HashMapImpl._DELETED_KEY))); |
| 228 return result; | 189 return result; |
| 229 } | 190 } |
| 230 | 191 |
| 231 Iterator _keysIterator; | 192 Iterator _keysIterator; |
| 232 } | 193 } |
| OLD | NEW |