OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 part of dart.collection; | 5 part of dart.collection; |
6 | 6 |
7 class LinkedHashSet<E> extends Collection<E> implements Set<E> { | 7 class LinkedHashSet<E> extends Collection<E> with _SetMixin<E> |
| 8 implements Set<E> { |
8 static const int _INITIAL_CAPACITY = 8; | 9 static const int _INITIAL_CAPACITY = 8; |
9 _LinkedHashTable<E> _table; | 10 _LinkedHashTable<E> _table; |
10 | 11 |
11 LinkedHashSet() : _table = new _LinkedHashTable(_INITIAL_CAPACITY) { | 12 LinkedHashSet() : _table = new _LinkedHashTable(_INITIAL_CAPACITY) { |
12 _table._container = this; | 13 _table._container = this; |
13 } | 14 } |
14 | 15 |
15 factory LinkedHashSet.from(Iterable<E> iterable) { | 16 factory LinkedHashSet.from(Iterable<E> iterable) { |
16 return new LinkedHashSet<E>()..addAll(iterable); | 17 return new LinkedHashSet<E>()..addAll(iterable); |
17 } | 18 } |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 return false; | 87 return false; |
87 } | 88 } |
88 | 89 |
89 void removeAll(Iterable objectsToRemove) { | 90 void removeAll(Iterable objectsToRemove) { |
90 for (Object object in objectsToRemove) { | 91 for (Object object in objectsToRemove) { |
91 _table._remove(object); | 92 _table._remove(object); |
92 _table._checkCapacity(); | 93 _table._checkCapacity(); |
93 } | 94 } |
94 } | 95 } |
95 | 96 |
96 void retainAll(Iterable objectsToRemove) { | |
97 IterableMixinWorkaround.retainAll(this, objectsToRemove); | |
98 } | |
99 | |
100 void _filterWhere(bool test(E element), bool removeMatching) { | 97 void _filterWhere(bool test(E element), bool removeMatching) { |
101 int entrySize = _table._entrySize; | 98 int entrySize = _table._entrySize; |
102 int length = _table._table.length; | 99 int length = _table._table.length; |
103 int offset = _table._next(_LinkedHashTable._HEAD_OFFSET); | 100 int offset = _table._next(_LinkedHashTable._HEAD_OFFSET); |
104 while (offset != _LinkedHashTable._HEAD_OFFSET) { | 101 while (offset != _LinkedHashTable._HEAD_OFFSET) { |
105 E key = _table._key(offset); | 102 E key = _table._key(offset); |
106 int nextOffset = _table._next(offset); | 103 int nextOffset = _table._next(offset); |
107 int modificationCount = _table._modificationCount; | 104 int modificationCount = _table._modificationCount; |
108 bool shouldRemove = (removeMatching == test(key)); | 105 bool shouldRemove = (removeMatching == test(key)); |
109 _table._checkModification(modificationCount); | 106 _table._checkModification(modificationCount); |
(...skipping 10 matching lines...) Expand all Loading... |
120 } | 117 } |
121 | 118 |
122 void retainWhere(bool test(E element)) { | 119 void retainWhere(bool test(E element)) { |
123 _filterWhere(test, false); | 120 _filterWhere(test, false); |
124 } | 121 } |
125 | 122 |
126 void clear() { | 123 void clear() { |
127 _table._clear(); | 124 _table._clear(); |
128 } | 125 } |
129 | 126 |
130 // Set. | 127 // Set interface from _SetMixin<E>. |
131 bool isSubsetOf(Collection<E> other) { | 128 Set<E> _newSet() => new LinkedHashSet<E>(); |
132 // Deprecated, and using old signature. | |
133 Set otherSet; | |
134 if (other is Set) { | |
135 otherSet = other; | |
136 } else { | |
137 otherSet = other.toSet(); | |
138 } | |
139 return IterableMixinWorkaround.setContainsAll(otherSet, this); | |
140 } | |
141 | |
142 bool containsAll(Iterable<E> other) { | |
143 return IterableMixinWorkaround.setContainsAll(this, other); | |
144 } | |
145 | |
146 Set<E> intersection(Set<E> other) { | |
147 return IterableMixinWorkaround.setIntersection( | |
148 this, other, new LinkedHashSet<E>()); | |
149 } | |
150 | |
151 Set<E> union(Set<E> other) { | |
152 return IterableMixinWorkaround.setUnion( | |
153 this, other, new LinkedHashSet<E>()); | |
154 } | |
155 | |
156 Set<E> difference(Set<E> other) { | |
157 return IterableMixinWorkaround.setDifference( | |
158 this, other, new LinkedHashSet<E>()); | |
159 } | |
160 | 129 |
161 String toString() => Collections.collectionToString(this); | 130 String toString() => Collections.collectionToString(this); |
162 } | 131 } |
OLD | NEW |