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) { | |
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); | |
43 return offset >= 0; | |
44 } | |
45 | |
46 void removeAll(Iterable objectsToRemove) { | |
47 for (Object object in objectsToRemove) { | |
48 _table._remove(object); | |
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 remove = (removeMatching == test(key)); | |
65 _table._checkModification(modificationCount); | |
66 if (remove) { | |
67 _table._deleteEntry(offset); | |
68 _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.
| |
69 } | |
70 } | |
71 } | |
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 |