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