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