Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(985)

Side by Side Diff: sdk/lib/collection/linked_hash_set.dart

Issue 13496014: Reapply "Reduce usage of IterableMixinWorkaround" (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « sdk/lib/collection/hash_set.dart ('k') | sdk/lib/collection/queue.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 Collection<E> implements Set<E> { 7 class LinkedHashSet<E> extends _HashSetBase<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
22 void forEach(void action(E element)) { 28 void forEach(void action(E element)) {
23 int offset = _table._next(_LinkedHashTable._HEAD_OFFSET); 29 int offset = _table._next(_LinkedHashTable._HEAD_OFFSET);
24 int modificationCount = _table._modificationCount; 30 int modificationCount = _table._modificationCount;
25 while (offset != _LinkedHashTable._HEAD_OFFSET) { 31 while (offset != _LinkedHashTable._HEAD_OFFSET) {
26 E key = _table._key(offset); 32 E key = _table._key(offset);
27 action(key); 33 action(key);
28 _table._checkModification(modificationCount); 34 _table._checkModification(modificationCount);
29 offset = _table._next(offset); 35 offset = _table._next(offset);
30 } 36 }
31 } 37 }
32 38
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
100 void _filterWhere(bool test(E element), bool removeMatching) { 68 void _filterWhere(bool test(E element), bool removeMatching) {
101 int entrySize = _table._entrySize; 69 int entrySize = _table._entrySize;
102 int length = _table._table.length; 70 int length = _table._table.length;
103 int offset = _table._next(_LinkedHashTable._HEAD_OFFSET); 71 int offset = _table._next(_LinkedHashTable._HEAD_OFFSET);
104 while (offset != _LinkedHashTable._HEAD_OFFSET) { 72 while (offset != _LinkedHashTable._HEAD_OFFSET) {
105 E key = _table._key(offset); 73 E key = _table._key(offset);
106 int nextOffset = _table._next(offset); 74 int nextOffset = _table._next(offset);
107 int modificationCount = _table._modificationCount; 75 int modificationCount = _table._modificationCount;
108 bool shouldRemove = (removeMatching == test(key)); 76 bool shouldRemove = (removeMatching == test(key));
109 _table._checkModification(modificationCount); 77 _table._checkModification(modificationCount);
110 if (shouldRemove) { 78 if (shouldRemove) {
111 _table._deleteEntry(offset); 79 _table._deleteEntry(offset);
112 } 80 }
113 offset = nextOffset; 81 offset = nextOffset;
114 } 82 }
115 _table._checkCapacity(); 83 _table._checkCapacity();
116 } 84 }
117 85
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 void retainWhere(bool test(E element)) { 122 retianWhere(bool test(E element)) {
123 _filterWhere(test, false); 123 _filterWhere(test, false);
124 } 124 }
125 125
126 void clear() { 126 // Set
127 _table._clear(); 127 Set<E> _newSet() => new LinkedHashSet<E>();
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);
162 } 128 }
OLDNEW
« no previous file with comments | « sdk/lib/collection/hash_set.dart ('k') | sdk/lib/collection/queue.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698