Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(34)

Unified Diff: runtime/lib/collection_patch.dart

Issue 13715002: Add a dart2js specific HashSet implementation that follows the HashMap implementation fairly closel… (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Address feedback. Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | sdk/lib/_internal/compiler/implementation/lib/collection_patch.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: runtime/lib/collection_patch.dart
diff --git a/runtime/lib/collection_patch.dart b/runtime/lib/collection_patch.dart
index a9b74b7b4dc33b13cd702f65bbf547b8171f66df..2d4881ed7303163134cfdeb8932e9996538b2a85 100644
--- a/runtime/lib/collection_patch.dart
+++ b/runtime/lib/collection_patch.dart
@@ -111,3 +111,81 @@ patch class HashMap<K, V> {
/* patch */ bool get isEmpty => _hashTable._elementCount == 0;
}
+
+patch class HashSet<E> {
+ static const int _INITIAL_CAPACITY = 8;
+ final _HashTable<E> _table;
+
+ /* patch */ HashSet() : _table = new _HashTable(_INITIAL_CAPACITY) {
+ _table._container = this;
+ }
+
+ factory HashSet.from(Iterable<E> iterable) {
+ return new HashSet<E>()..addAll(iterable);
+ }
+
+ // Iterable.
+ /* patch */ Iterator<E> get iterator => new _HashTableKeyIterator<E>(_table);
+
+ /* patch */ int get length => _table._elementCount;
+
+ /* patch */ bool get isEmpty => _table._elementCount == 0;
+
+ /* patch */ bool contains(Object object) => _table._get(object) >= 0;
+
+ // Collection.
+ /* patch */ void add(E element) {
+ _table._put(element);
+ _table._checkCapacity();
+ }
+
+ /* patch */ void addAll(Iterable<E> objects) {
+ for (E object in objects) {
+ _table._put(object);
+ _table._checkCapacity();
+ }
+ }
+
+ /* patch */ bool remove(Object object) {
+ int offset = _table._remove(object);
+ _table._checkCapacity();
+ return offset >= 0;
+ }
+
+ /* patch */ void removeAll(Iterable objectsToRemove) {
+ for (Object object in objectsToRemove) {
+ _table._remove(object);
+ _table._checkCapacity();
+ }
+ }
+
+ void _filterWhere(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 shouldRemove = (removeMatching == test(key));
+ _table._checkModification(modificationCount);
+ if (shouldRemove) {
+ _table._deleteEntry(offset);
+ }
+ }
+ }
+ _table._checkCapacity();
+ }
+
+ /* patch */ void removeWhere(bool test(E element)) {
+ _filterWhere(test, true);
+ }
+
+ /* patch */ void retainWhere(bool test(E element)) {
+ _filterWhere(test, false);
+ }
+
+ /* patch */ void clear() {
+ _table._clear();
+ }
+}
« no previous file with comments | « no previous file | sdk/lib/_internal/compiler/implementation/lib/collection_patch.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698