| 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 _HashSetBase<E> { | 7 class LinkedHashSet<E> extends Collection<E> implements Set<E> { |
| 8 static const int _INITIAL_CAPACITY = 8; | 8 static const int _INITIAL_CAPACITY = 8; |
| 9 _LinkedHashTable<E> _table; | 9 _LinkedHashTable<E> _table; |
| 10 | 10 |
| 11 LinkedHashSet() : _table = new _LinkedHashTable(_INITIAL_CAPACITY) { | 11 LinkedHashSet() : _table = new _LinkedHashTable(_INITIAL_CAPACITY) { |
| 12 _table._container = this; | 12 _table._container = this; |
| 13 } | 13 } |
| 14 | 14 |
| 15 factory LinkedHashSet.from(Iterable<E> iterable) { | 15 factory LinkedHashSet.from(Iterable<E> iterable) { |
| 16 return new LinkedHashSet<E>()..addAll(iterable); | 16 return new LinkedHashSet<E>()..addAll(iterable); |
| 17 } | 17 } |
| 18 | 18 |
| 19 // Iterable. | 19 // Iterable. |
| 20 Iterator<E> get iterator => new _LinkedHashTableKeyIterator<E>(_table); | 20 Iterator<E> get iterator => new _LinkedHashTableKeyIterator<E>(_table); |
| 21 | 21 |
| 22 int get length => _table._elementCount; | |
| 23 | |
| 24 bool get isEmpty => _table._elementCount == 0; | |
| 25 | |
| 26 bool contains(Object object) => _table._get(object) >= 0; | |
| 27 | |
| 28 void forEach(void action(E element)) { | 22 void forEach(void action(E element)) { |
| 29 int offset = _table._next(_LinkedHashTable._HEAD_OFFSET); | 23 int offset = _table._next(_LinkedHashTable._HEAD_OFFSET); |
| 30 int modificationCount = _table._modificationCount; | 24 int modificationCount = _table._modificationCount; |
| 31 while (offset != _LinkedHashTable._HEAD_OFFSET) { | 25 while (offset != _LinkedHashTable._HEAD_OFFSET) { |
| 32 E key = _table._key(offset); | 26 E key = _table._key(offset); |
| 33 action(key); | 27 action(key); |
| 34 _table._checkModification(modificationCount); | 28 _table._checkModification(modificationCount); |
| 35 offset = _table._next(offset); | 29 offset = _table._next(offset); |
| 36 } | 30 } |
| 37 } | 31 } |
| 38 | 32 |
| 33 int get length => _table._elementCount; |
| 34 |
| 35 bool get isEmpty => _table._elementCount == 0; |
| 36 |
| 37 bool contains(Object object) => _table._get(object) >= 0; |
| 38 |
| 39 E get first { | 39 E get first { |
| 40 int firstOffset = _table._next(_LinkedHashTable._HEAD_OFFSET); | 40 int firstOffset = _table._next(_LinkedHashTable._HEAD_OFFSET); |
| 41 if (firstOffset == _LinkedHashTable._HEAD_OFFSET) { | 41 if (firstOffset == _LinkedHashTable._HEAD_OFFSET) { |
| 42 throw new StateError("No elements"); | 42 throw new StateError("No elements"); |
| 43 } | 43 } |
| 44 return _table._key(firstOffset); | 44 return _table._key(firstOffset); |
| 45 } | 45 } |
| 46 | 46 |
| 47 E get last { | 47 E get last { |
| 48 int lastOffset = _table._prev(_LinkedHashTable._HEAD_OFFSET); | 48 int lastOffset = _table._prev(_LinkedHashTable._HEAD_OFFSET); |
| 49 if (lastOffset == _LinkedHashTable._HEAD_OFFSET) { | 49 if (lastOffset == _LinkedHashTable._HEAD_OFFSET) { |
| 50 throw new StateError("No elements"); | 50 throw new StateError("No elements"); |
| 51 } | 51 } |
| 52 return _table._key(lastOffset); | 52 return _table._key(lastOffset); |
| 53 } | 53 } |
| 54 | 54 |
| 55 E get single { | 55 E get single { |
| 56 int firstOffset = _table._next(_LinkedHashTable._HEAD_OFFSET); | 56 int firstOffset = _table._next(_LinkedHashTable._HEAD_OFFSET); |
| 57 if (firstOffset == _LinkedHashTable._HEAD_OFFSET) { | 57 if (firstOffset == _LinkedHashTable._HEAD_OFFSET) { |
| 58 throw new StateError("No elements"); | 58 throw new StateError("No elements"); |
| 59 } | 59 } |
| 60 int lastOffset = _table._prev(_LinkedHashTable._HEAD_OFFSET); | 60 int lastOffset = _table._prev(_LinkedHashTable._HEAD_OFFSET); |
| 61 if (lastOffset != firstOffset) { | 61 if (lastOffset != firstOffset) { |
| 62 throw new StateError("Too many elements"); | 62 throw new StateError("Too many elements"); |
| 63 } | 63 } |
| 64 return _table._key(firstOffset); | 64 return _table._key(firstOffset); |
| 65 } | 65 } |
| 66 | 66 |
| 67 // Collection. | 67 // Collection. |
| 68 void add(E element) { |
| 69 _table._put(element); |
| 70 _table._checkCapacity(); |
| 71 } |
| 72 |
| 73 void addAll(Iterable<E> objects) { |
| 74 for (E object in objects) { |
| 75 _table._put(object); |
| 76 _table._checkCapacity(); |
| 77 } |
| 78 } |
| 79 |
| 80 bool remove(Object object) { |
| 81 int offset = _table._remove(object); |
| 82 if (offset >= 0) { |
| 83 _table._checkCapacity(); |
| 84 return true; |
| 85 } |
| 86 return false; |
| 87 } |
| 88 |
| 89 void removeAll(Iterable objectsToRemove) { |
| 90 for (Object object in objectsToRemove) { |
| 91 _table._remove(object); |
| 92 _table._checkCapacity(); |
| 93 } |
| 94 } |
| 95 |
| 96 void retainAll(Iterable objectsToRemove) { |
| 97 IterableMixinWorkaround.retainAll(this, objectsToRemove); |
| 98 } |
| 99 |
| 68 void _filterWhere(bool test(E element), bool removeMatching) { | 100 void _filterWhere(bool test(E element), bool removeMatching) { |
| 69 int entrySize = _table._entrySize; | 101 int entrySize = _table._entrySize; |
| 70 int length = _table._table.length; | 102 int length = _table._table.length; |
| 71 int offset = _table._next(_LinkedHashTable._HEAD_OFFSET); | 103 int offset = _table._next(_LinkedHashTable._HEAD_OFFSET); |
| 72 while (offset != _LinkedHashTable._HEAD_OFFSET) { | 104 while (offset != _LinkedHashTable._HEAD_OFFSET) { |
| 73 E key = _table._key(offset); | 105 E key = _table._key(offset); |
| 74 int nextOffset = _table._next(offset); | 106 int nextOffset = _table._next(offset); |
| 75 int modificationCount = _table._modificationCount; | 107 int modificationCount = _table._modificationCount; |
| 76 bool shouldRemove = (removeMatching == test(key)); | 108 bool shouldRemove = (removeMatching == test(key)); |
| 77 _table._checkModification(modificationCount); | 109 _table._checkModification(modificationCount); |
| 78 if (shouldRemove) { | 110 if (shouldRemove) { |
| 79 _table._deleteEntry(offset); | 111 _table._deleteEntry(offset); |
| 80 } | 112 } |
| 81 offset = nextOffset; | 113 offset = nextOffset; |
| 82 } | 114 } |
| 83 _table._checkCapacity(); | 115 _table._checkCapacity(); |
| 84 } | 116 } |
| 85 | 117 |
| 86 void add(E element) { | |
| 87 _table._put(element); | |
| 88 _table._checkCapacity(); | |
| 89 } | |
| 90 | |
| 91 bool remove(Object object) { | |
| 92 int offset = _table._remove(object); | |
| 93 if (offset >= 0) { | |
| 94 _table._checkCapacity(); | |
| 95 return true; | |
| 96 } | |
| 97 return false; | |
| 98 } | |
| 99 | |
| 100 void removeAll(Iterable objectsToRemove) { | |
| 101 for (Object object in objectsToRemove) { | |
| 102 if (_table.remove(object) >= 0) { | |
| 103 _table._checkCapacity(); | |
| 104 } | |
| 105 } | |
| 106 } | |
| 107 | |
| 108 void retainAll(Iterable objectsToRetain) { | |
| 109 Set retainSet; | |
| 110 if (objectsToRetain is Set) { | |
| 111 retainSet = objectsToRetain; | |
| 112 } else { | |
| 113 retainSet = objectsToRetain.toSet(); | |
| 114 } | |
| 115 _filterWhere(retainSet.contains, false); | |
| 116 } | |
| 117 | |
| 118 void removeWhere(bool test(E element)) { | 118 void removeWhere(bool test(E element)) { |
| 119 _filterWhere(test, true); | 119 _filterWhere(test, true); |
| 120 } | 120 } |
| 121 | 121 |
| 122 retianWhere(bool test(E element)) { | 122 void retainWhere(bool test(E element)) { |
| 123 _filterWhere(test, false); | 123 _filterWhere(test, false); |
| 124 } | 124 } |
| 125 | 125 |
| 126 // Set | 126 void clear() { |
| 127 Set<E> _newSet() => new LinkedHashSet<E>(); | 127 _table._clear(); |
| 128 } |
| 129 |
| 130 // Set. |
| 131 bool isSubsetOf(Collection<E> other) { |
| 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 |
| 161 String toString() => Collections.collectionToString(this); |
| 128 } | 162 } |
| OLD | NEW |