OLD | NEW |
---|---|
(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._ensureCapacity(1); | |
23 _table._put(element); | |
24 } | |
25 | |
26 void addAll(Iterable<E> objects) { | |
erikcorry
2013/02/05 16:25:05
I can't see where this increments the modification
Lasse Reichstein Nielsen
2013/02/05 19:47:40
True. I was probably assuming that it was done in
| |
27 if (objects is Set || objects is List) { | |
28 int length = objects.length; | |
29 _table._ensureCapacity(length); | |
30 for (E object in objects) { | |
31 _table._put(object); | |
32 } | |
33 } else { | |
34 for (E object in objects) { | |
35 _table._ensureCapacity(1); | |
36 _table._put(object); | |
37 } | |
38 } | |
39 } | |
40 | |
41 bool remove(Object object) { | |
42 int offset = _table._remove(object); | |
floitsch
2013/02/06 10:43:58
modification count here too.
Lasse Reichstein Nielsen
2013/02/08 13:53:01
Added to _remove.
| |
43 return offset >= 0; | |
44 } | |
45 | |
46 void removeAll(Iterable objectsToRemove) { | |
47 for (Object object in objectsToRemove) { | |
floitsch
2013/02/06 10:43:58
ditto.
Lasse Reichstein Nielsen
2013/02/08 13:53:01
Et ditto.
| |
48 _table._remove(object); | |
49 } | |
50 } | |
51 | |
52 void retainAll(Iterable objectsToRemove) { | |
erikcorry
2013/02/05 16:25:05
Parameter is misnamed.
Lasse Reichstein Nielsen
2013/02/05 19:47:40
Whoops.
| |
53 IterableMixinWorkaround.retainAll(this, objectsToRemove); | |
54 } | |
55 | |
56 void removeMatching(bool test(E element)) { | |
57 int entrySize = _table._entrySize; | |
58 int length = _table._table.length; | |
59 for (int offset = 0; offset < length; offset += entrySize) { | |
60 Object entry = _table._key(offset); | |
61 if (!_table._isFree(entry)) { | |
62 E key = entry; | |
63 int modificationCount = _table._modificationCount; | |
64 bool matches = test(key); | |
65 _table._checkModification(modificationCount); | |
66 if (matches) { | |
67 _table._deleteEntry(offset); | |
68 _table._modificationCount++; | |
69 } | |
70 } | |
71 } | |
72 } | |
73 | |
74 void retainMatching(bool test(E element)) { | |
75 int entrySize = _table._entrySize; | |
76 int length = _table._table.length; | |
77 for (int offset = 0; offset < length; offset += entrySize) { | |
78 Object entry = _table._key(offset); | |
79 if (!_table._isFree(entry)) { | |
80 E key = entry; | |
81 int modificationCount = _table._modificationCount; | |
82 bool matches = test(key); | |
83 _table._checkModification(modificationCount); | |
84 if (!matches) { | |
85 _table._deleteEntry(offset); | |
86 _table._modificationCount++; | |
87 } | |
88 } | |
89 } | |
90 } | |
91 | |
92 void clear() { | |
93 _table._clear(); | |
94 } | |
95 | |
96 // Set. | |
97 bool isSubsetOf(Collection<E> collection) { | |
98 Set otherSet; | |
99 if (collection is Set) { | |
100 otherSet = collection; | |
101 } else { | |
102 otherSet = collection.toSet(); | |
103 } | |
104 return otherSet.containsAll(this); | |
105 } | |
106 | |
107 bool containsAll(Collection<E> collection) { | |
floitsch
2013/02/06 10:43:58
We should change this to be an Iterable.
Lasse Reichstein Nielsen
2013/02/08 13:53:01
Or a Set. I think I would prefer that for intersec
| |
108 for (E element in collection) { | |
109 if (!this.contains(element)) return false; | |
110 } | |
111 return true; | |
112 } | |
113 | |
114 Set<E> intersection(Collection<E> other) { | |
floitsch
2013/02/06 10:43:58
ditto.
Lasse Reichstein Nielsen
2013/02/08 13:53:01
Same.
| |
115 Set<E> result = new HashSet<E>(); | |
floitsch
2013/02/06 10:43:58
new Set<E>(). It should be the default Set.
Lasse Reichstein Nielsen
2013/02/08 13:53:01
Let's keep this the same type as the set creating
| |
116 for (E element in other) { | |
117 if (this.contains(element)) { | |
118 result.add(element); | |
119 } | |
120 } | |
121 return result; | |
122 } | |
123 | |
124 String toString() => Collections.collectionToString(this); | |
125 } | |
OLD | NEW |