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