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

Side by Side Diff: sdk/lib/collection/hash_set.dart

Issue 12213010: New implementation of {,Linked}Hash{Set,Map}. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Made null elements work again, added tests for null. Created 7 years, 10 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 part of dart.collection;
2
3 class HashSet<E> extends Collection<E> implements Set<E> {
4 static const int _INITIAL_CAPACITY = 8;
5 final _HashTable<E> _table;
6
7 HashSet() : _table = new _HashTable(_INITIAL_CAPACITY);
8
9 factory HashSet.from(Iterable<E> iterable) {
10 return new HashSet<E>()..addAll(iterable);
11 }
12
13 // Iterable.
14 Iterator<E> get iterator => new _HashTableKeyIterator<E>(_table);
15
16 bool get isEmpty => _table._elementCount == 0;
17
18 bool contains(Object object) => _table._get(object) >= 0;
19
20 // Collection.
21 void add(E element) {
22 _table._put(element);
23 _table._checkCapacity();
24 }
25
26 void addAll(Iterable<E> objects) {
27 for (E object in objects) {
28 _table._put(object);
29 _table._checkCapacity();
30 }
31 }
32
33 bool remove(Object object) {
34 int offset = _table._remove(object);
35 _table._checkCapacity();
36 return offset >= 0;
37 }
38
39 void removeAll(Iterable objectsToRemove) {
40 for (Object object in objectsToRemove) {
41 _table._remove(object);
42 _table._checkCapacity();
43 }
44 }
45
46 void retainAll(Iterable objectsToRetain) {
47 IterableMixinWorkaround.retainAll(this, objectsToRetain);
48 }
49
50 void _filterMatching(bool test(E element), bool removeMatching) {
51 int entrySize = _table._entrySize;
52 int length = _table._table.length;
53 for (int offset = 0; offset < length; offset += entrySize) {
54 Object entry = _table._table[offset];
55 if (!_table._isFree(entry)) {
56 E key = identical(entry, _NULL) ? null : entry;
57 int modificationCount = _table._modificationCount;
58 bool remove = (removeMatching == test(key));
59 _table._checkModification(modificationCount);
60 if (remove) {
61 _table._deleteEntry(offset);
62 }
63 }
64 }
65 _table._checkCapacity();
66 }
67
68 void removeMatching(bool test(E element)) {
69 _filterMatching(test, true);
70 }
71
72 void retainMatching(bool test(E element)) {
73 _filterMatching(test, false);
74 }
75
76 void clear() {
77 _table._clear();
78 }
79
80 // Set.
81 bool isSubsetOf(Collection<E> collection) {
82 Set otherSet;
83 if (collection is Set) {
84 otherSet = collection;
85 } else {
86 otherSet = collection.toSet();
87 }
88 return otherSet.containsAll(this);
89 }
90
91 bool containsAll(Collection<E> collection) {
92 for (E element in collection) {
93 if (!this.contains(element)) return false;
94 }
95 return true;
96 }
97
98 Set<E> intersection(Collection<E> other) {
99 Set<E> result = new HashSet<E>();
100 for (E element in other) {
101 if (this.contains(element)) {
102 result.add(element);
103 }
104 }
105 return result;
106 }
107
108 String toString() => Collections.collectionToString(this);
109 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698