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 class HashSet<E> extends Collection<E> implements Set<E> { |
| 8 static const int _INITIAL_CAPACITY = 8; |
| 9 final _HashTable<E> _table; |
| 10 |
| 11 HashSet() : _table = new _HashTable(_INITIAL_CAPACITY) { |
| 12 _table._container = this; |
| 13 } |
| 14 |
| 15 factory HashSet.from(Iterable<E> iterable) { |
| 16 return new HashSet<E>()..addAll(iterable); |
| 17 } |
| 18 |
| 19 // Iterable. |
| 20 Iterator<E> get iterator => new _HashTableKeyIterator<E>(_table); |
| 21 |
| 22 bool get isEmpty => _table._elementCount == 0; |
| 23 |
| 24 bool contains(Object object) => _table._get(object) >= 0; |
| 25 |
| 26 // Collection. |
| 27 void add(E element) { |
| 28 _table._put(element); |
| 29 _table._checkCapacity(); |
| 30 } |
| 31 |
| 32 void addAll(Iterable<E> objects) { |
| 33 for (E object in objects) { |
| 34 _table._put(object); |
| 35 _table._checkCapacity(); |
| 36 } |
| 37 } |
| 38 |
| 39 bool remove(Object object) { |
| 40 int offset = _table._remove(object); |
| 41 _table._checkCapacity(); |
| 42 return offset >= 0; |
| 43 } |
| 44 |
| 45 void removeAll(Iterable objectsToRemove) { |
| 46 for (Object object in objectsToRemove) { |
| 47 _table._remove(object); |
| 48 _table._checkCapacity(); |
| 49 } |
| 50 } |
| 51 |
| 52 void retainAll(Iterable objectsToRetain) { |
| 53 IterableMixinWorkaround.retainAll(this, objectsToRetain); |
| 54 } |
| 55 |
| 56 void _filterMatching(bool test(E element), bool removeMatching) { |
| 57 int entrySize = _table._entrySize; |
| 58 int length = _table._table.length; |
| 59 for (int offset = 0; offset < length; offset += entrySize) { |
| 60 Object entry = _table._table[offset]; |
| 61 if (!_table._isFree(entry)) { |
| 62 E key = identical(entry, _NULL) ? null : entry; |
| 63 int modificationCount = _table._modificationCount; |
| 64 bool shouldRemove = (removeMatching == test(key)); |
| 65 _table._checkModification(modificationCount); |
| 66 if (shouldRemove) { |
| 67 _table._deleteEntry(offset); |
| 68 } |
| 69 } |
| 70 } |
| 71 _table._checkCapacity(); |
| 72 } |
| 73 |
| 74 void removeMatching(bool test(E element)) { |
| 75 _filterMatching(test, true); |
| 76 } |
| 77 |
| 78 void retainMatching(bool test(E element)) { |
| 79 _filterMatching(test, false); |
| 80 } |
| 81 |
| 82 void clear() { |
| 83 _table._clear(); |
| 84 } |
| 85 |
| 86 // Set. |
| 87 bool isSubsetOf(Collection<E> collection) { |
| 88 Set otherSet; |
| 89 if (collection is Set) { |
| 90 otherSet = collection; |
| 91 } else { |
| 92 otherSet = collection.toSet(); |
| 93 } |
| 94 return otherSet.containsAll(this); |
| 95 } |
| 96 |
| 97 bool containsAll(Collection<E> collection) { |
| 98 for (E element in collection) { |
| 99 if (!this.contains(element)) return false; |
| 100 } |
| 101 return true; |
| 102 } |
| 103 |
| 104 Set<E> intersection(Collection<E> other) { |
| 105 Set<E> result = new HashSet<E>(); |
| 106 for (E element in other) { |
| 107 if (this.contains(element)) { |
| 108 result.add(element); |
| 109 } |
| 110 } |
| 111 return result; |
| 112 } |
| 113 |
| 114 String toString() => Collections.collectionToString(this); |
| 115 } |
OLD | NEW |