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