OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 part of dart.collection; |
| 6 |
| 7 |
| 8 |
| 9 class HashSet<E> extends Collection<E> implements Set<E> { |
| 10 // The map backing this set. The associations in this map are all |
| 11 // of the form element -> element. If a value is not in the map, |
| 12 // then it is not in the set. |
| 13 final _HashMapImpl<E, E> _backingMap; |
| 14 |
| 15 HashSet() : _backingMap = new _HashMapImpl<E, E>(); |
| 16 |
| 17 /** |
| 18 * Creates a [Set] that contains all elements of [other]. |
| 19 */ |
| 20 factory HashSet.from(Iterable<E> other) { |
| 21 Set<E> set = new HashSet<E>(); |
| 22 for (final e in other) { |
| 23 set.add(e); |
| 24 } |
| 25 return set; |
| 26 } |
| 27 |
| 28 void clear() { |
| 29 _backingMap.clear(); |
| 30 } |
| 31 |
| 32 void add(E value) { |
| 33 _backingMap[value] = value; |
| 34 } |
| 35 |
| 36 bool remove(Object value) { |
| 37 if (!_backingMap.containsKey(value)) return false; |
| 38 _backingMap.remove(value); |
| 39 return true; |
| 40 } |
| 41 |
| 42 bool contains(E value) { |
| 43 return _backingMap.containsKey(value); |
| 44 } |
| 45 |
| 46 Set<E> intersection(Collection<E> collection) { |
| 47 Set<E> result = new Set<E>(); |
| 48 collection.forEach((E value) { |
| 49 if (contains(value)) result.add(value); |
| 50 }); |
| 51 return result; |
| 52 } |
| 53 |
| 54 bool isSubsetOf(Collection<E> other) { |
| 55 return new Set<E>.from(other).containsAll(this); |
| 56 } |
| 57 |
| 58 bool containsAll(Collection<E> collection) { |
| 59 return collection.every((E value) { |
| 60 return contains(value); |
| 61 }); |
| 62 } |
| 63 |
| 64 void forEach(void f(E element)) { |
| 65 _backingMap.forEach((E key, E value) { |
| 66 f(key); |
| 67 }); |
| 68 } |
| 69 |
| 70 bool get isEmpty { |
| 71 return _backingMap.isEmpty; |
| 72 } |
| 73 |
| 74 int get length { |
| 75 return _backingMap.length; |
| 76 } |
| 77 |
| 78 Iterator<E> get iterator => new _HashSetIterator<E>(this); |
| 79 |
| 80 String toString() { |
| 81 return Collections.collectionToString(this); |
| 82 } |
| 83 } |
| 84 |
| 85 class _HashSetIterator<E> implements Iterator<E> { |
| 86 |
| 87 _HashSetIterator(HashSet<E> set) |
| 88 : _keysIterator = set._backingMap._keys.iterator; |
| 89 |
| 90 E get current { |
| 91 var result = _keysIterator.current; |
| 92 if (identical(result, _HashMapImpl._DELETED_KEY)) { |
| 93 // TODO(floitsch): improve the error reporting. |
| 94 throw new StateError("Concurrent modification."); |
| 95 } |
| 96 return result; |
| 97 } |
| 98 |
| 99 bool moveNext() { |
| 100 bool result; |
| 101 do { |
| 102 result = _keysIterator.moveNext(); |
| 103 } while (result && |
| 104 (_keysIterator.current == null || |
| 105 identical(_keysIterator.current, _HashMapImpl._DELETED_KEY))); |
| 106 return result; |
| 107 } |
| 108 |
| 109 Iterator _keysIterator; |
| 110 } |
OLD | NEW |