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