Chromium Code Reviews| Index: sdk/lib/collection/hash_set.dart |
| diff --git a/sdk/lib/collection/hash_set.dart b/sdk/lib/collection/hash_set.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..9d723ae9abe312d47296d1fa46dde862c666c944 |
| --- /dev/null |
| +++ b/sdk/lib/collection/hash_set.dart |
| @@ -0,0 +1,115 @@ |
| +part of dart.collection; |
| + |
| +class HashSet<E> extends Collection<E> implements Set<E> { |
| + static const int _INITIAL_CAPACITY = 8; |
| + final _HashTable<E> _table; |
| + |
| + HashSet() : _table = new _HashTable(_INITIAL_CAPACITY); |
| + |
| + factory HashSet.from(Iterable<E> iterable) { |
| + return new HashSet<E>()..addAll(iterable); |
| + } |
| + |
| + // Iterable. |
| + Iterator<E> get iterator => new _HashTableKeyIterator<E>(_table); |
| + |
| + bool get isEmpty => _table._elementCount == 0; |
| + |
| + bool contains(Object object) => _table._get(object) >= 0; |
| + |
| + // Collection. |
| + void add(E element) { |
| + _table._ensureCapacity(1); |
| + _table._put(element); |
| + } |
| + |
| + void addAll(Iterable<E> objects) { |
| + if (objects is Set || objects is List) { |
| + int length = objects.length; |
| + _table._ensureCapacity(length); |
| + for (E object in objects) { |
| + _table._put(object); |
| + } |
| + } else { |
| + for (E object in objects) { |
| + _table._ensureCapacity(1); |
| + _table._put(object); |
| + } |
| + } |
| + } |
| + |
| + bool remove(Object object) { |
| + int offset = _table._remove(object); |
| + return offset >= 0; |
| + } |
| + |
| + void removeAll(Iterable objectsToRemove) { |
| + for (Object object in objectsToRemove) { |
| + _table._remove(object); |
| + } |
| + } |
| + |
| + void retainAll(Iterable objectsToRetain) { |
| + IterableMixinWorkaround.retainAll(this, objectsToRetain); |
| + } |
| + |
| + void _filterMatching(bool test(E element), bool removeMatching) { |
| + int entrySize = _table._entrySize; |
| + int length = _table._table.length; |
| + for (int offset = 0; offset < length; offset += entrySize) { |
| + Object entry = _table._table[offset]; |
| + if (!_table._isFree(entry)) { |
| + E key = identical(entry, _NULL) ? null : entry; |
| + int modificationCount = _table._modificationCount; |
| + bool remove = (removeMatching == test(key)); |
| + _table._checkModification(modificationCount); |
| + if (remove) { |
| + _table._deleteEntry(offset); |
| + _table._recordModification(); |
|
floitsch
2013/02/08 16:48:35
shouldn't be necessary. _deleteEntry records the m
Lasse Reichstein Nielsen
2013/02/11 14:07:37
True.
|
| + } |
| + } |
| + } |
| + } |
| + |
| + void removeMatching(bool test(E element)) { |
| + _filterMatching(test, true); |
| + } |
| + |
| + void retainMatching(bool test(E element)) { |
| + _filterMatching(test, false); |
| + } |
| + |
| + void clear() { |
| + _table._clear(); |
| + } |
| + |
| + // Set. |
| + bool isSubsetOf(Collection<E> collection) { |
| + Set otherSet; |
| + if (collection is Set) { |
| + otherSet = collection; |
| + } else { |
| + otherSet = collection.toSet(); |
| + } |
| + return otherSet.containsAll(this); |
| + } |
| + |
| + bool containsAll(Collection<E> collection) { |
| + for (E element in collection) { |
| + if (!this.contains(element)) return false; |
| + } |
| + return true; |
| + } |
| + |
| + Set<E> intersection(Collection<E> other) { |
| + Set<E> result = new HashSet<E>(); |
| + for (E element in other) { |
| + if (this.contains(element)) { |
| + result.add(element); |
| + } |
| + } |
| + return result; |
| + } |
| + |
| + String toString() => Collections.collectionToString(this); |
| +} |