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

Side by Side Diff: sdk/lib/core/set.dart

Issue 11644072: Add ordered set. (Closed) Base URL: https://dart.googlecode.com/svn/experimental/lib_v2/dart
Patch Set: Created 7 years, 12 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 | « no previous file | tests/corelib/ordered_set_iterator_test.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) 2011, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2011, 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.core; 5 part of dart.core;
6 6
7 /** 7 /**
8 * This class is the public interface of a set. A set is a collection 8 * This class is the public interface of a set. A set is a collection
9 * without duplicates. 9 * without duplicates.
10 */ 10 */
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
71 71
72 abstract class HashSet<E> extends Set<E> { 72 abstract class HashSet<E> extends Set<E> {
73 factory HashSet() => new _HashSetImpl<E>(); 73 factory HashSet() => new _HashSetImpl<E>();
74 74
75 /** 75 /**
76 * Creates a [Set] that contains all elements of [other]. 76 * Creates a [Set] that contains all elements of [other].
77 */ 77 */
78 factory HashSet.from(Iterable<E> other) => new _HashSetImpl<E>.from(other); 78 factory HashSet.from(Iterable<E> other) => new _HashSetImpl<E>.from(other);
79 } 79 }
80 80
81 abstract class _MapBackedSet<E> extends Iterable<E> implements HashSet<E> {
Lasse Reichstein Nielsen 2013/01/04 09:39:06 Why implement HashSet? Being a HashSet is an imple
82 _MapBackedSet(this._backingMap);
81 83
82 class _HashSetImpl<E> extends Iterable<E> implements HashSet<E> { 84 _MapBackedSet.from(this._backingMap, Iterable<E> iterable) {
83 85 for (final e in iterable) {
84 _HashSetImpl() { 86 add(e);
85 _backingMap = new _HashMapImpl<E, E>(); 87 }
86 } 88 }
87 89
88 factory _HashSetImpl.from(Iterable<E> other) {
89 Set<E> set = new _HashSetImpl<E>();
90 for (final e in other) {
91 set.add(e);
92 }
93 return set;
94 }
95 90
96 void clear() { 91 void clear() {
97 _backingMap.clear(); 92 _backingMap.clear();
98 } 93 }
99 94
100 void add(E value) { 95 void add(E value) {
101 _backingMap[value] = value; 96 _backingMap[value] = value;
102 } 97 }
103 98
104 bool contains(E value) { 99 bool contains(E value) {
105 return _backingMap.containsKey(value); 100 return _backingMap.containsKey(value);
106 } 101 }
107 102
108 bool remove(E value) { 103 bool remove(E value) {
109 if (!_backingMap.containsKey(value)) return false; 104 if (!_backingMap.containsKey(value)) return false;
110 _backingMap.remove(value); 105 _backingMap.remove(value);
111 return true; 106 return true;
112 } 107 }
113 108
114 void addAll(Iterable<E> iterable) { 109 void addAll(Iterable<E> iterable) {
115 for (E element in iterable) { 110 for (E element in iterable) {
116 add(element); 111 add(element);
117 } 112 }
118 } 113 }
119 114
120 Set<E> intersection(Collection<E> collection) { 115 Set<E> intersection(Collection<E> collection) {
Lasse Reichstein Nielsen 2013/01/04 09:39:06 Why not Iterable?
121 Set<E> result = new Set<E>(); 116 Set<E> result = new Set<E>();
122 collection.forEach((E value) { 117 collection.forEach((E value) {
123 if (contains(value)) result.add(value); 118 if (contains(value)) result.add(value);
124 }); 119 });
125 return result; 120 return result;
126 } 121 }
127 122
128 bool isSubsetOf(Collection<E> other) { 123 bool isSubsetOf(Collection<E> other) {
129 return new Set<E>.from(other).containsAll(this); 124 return new Set<E>.from(other).containsAll(this);
Lasse Reichstein Nielsen 2013/01/04 09:39:06 ICK. Can't we really find a more efficient way to
sra1 2013/01/04 09:56:15 Would a check be good enough? if (other is Set) r
130 } 125 }
131 126
132 void removeAll(Iterable<E> iterable) { 127 void removeAll(Iterable<E> iterable) {
133 for (E value in iterable) { 128 for (E value in iterable) {
134 remove(value); 129 remove(value);
135 } 130 }
136 } 131 }
137 132
138 bool containsAll(Collection<E> collection) { 133 bool containsAll(Collection<E> collection) {
139 return collection.every((E value) { 134 return collection.every((E value) {
140 return contains(value); 135 return contains(value);
141 }); 136 });
142 } 137 }
143 138
144 void forEach(void f(E element)) { 139 void forEach(void f(E element)) {
145 _backingMap.forEach((E key, E value) { 140 _backingMap.forEach((E key, E value) {
146 f(key); 141 f(key);
147 }); 142 });
148 } 143 }
149 144
150 bool get isEmpty { 145 bool get isEmpty {
151 return _backingMap.isEmpty; 146 return _backingMap.isEmpty;
152 } 147 }
153 148
154 int get length { 149 int get length {
155 return _backingMap.length; 150 return _backingMap.length;
156 } 151 }
157 152
158 Iterator<E> get iterator => new _HashSetIterator<E>(this); 153 Iterator<E> get iterator => _backingMap.keys.iterator;
159 154
160 String toString() { 155 String toString() {
161 return Collections.collectionToString(this); 156 return Collections.collectionToString(this);
162 } 157 }
163 158
164 // The map backing this set. The associations in this map are all 159 // The map backing this set. The associations in this map are all
165 // of the form element -> element. If a value is not in the map, 160 // of the form element -> element. If a value is not in the map,
166 // then it is not in the set. 161 // then it is not in the set.
167 _HashMapImpl<E, E> _backingMap; 162 Map<E, E> _backingMap;
168 } 163 }
169 164
170 class _HashSetIterator<E> implements Iterator<E> { 165 class _HashSetImpl<E> extends _MapBackedSet<E> implements HashSet<E> {
166 _HashSetImpl.from(Iterable<E> iterable)
167 : super.from(new _HashMapImpl<E, E>(), iterable);
171 168
172 _HashSetIterator(_HashSetImpl<E> set) 169 _HashSetImpl() : super(new _HashMapImpl<E, E>());
173 : _keysIterator = set._backingMap._keys.iterator; 170 }
174 171
175 E get current { 172 class OrderedSet<E> extends _MapBackedSet<E> implements Set<E> {
Lasse Reichstein Nielsen 2013/01/04 09:39:06 I don't like "OrderedSet" as a name. It doesn't sa
sra1 2013/01/04 09:56:15 +1. From the CL title I thought at last we had a
sra1 2013/01/04 10:02:43 +1. From the CL title I thought at last we had a
176 var result = _keysIterator.current; 173 OrderedSet.from(Iterable<E> iterable)
177 if (identical(result, _HashMapImpl._DELETED_KEY)) { 174 : super.from(new _LinkedHashMapImpl<E, E>(), iterable);
178 // TODO(floitsch): improve the error reporting.
179 throw new StateError("Concurrent modification.");
180 }
181 return result;
182 }
183 175
184 bool moveNext() { 176 OrderedSet() : super(new _LinkedHashMapImpl<E, E>());
Lasse Reichstein Nielsen 2013/01/04 09:39:06 I still think it's silly to have a HashMap interfa
185 bool result;
186 do {
187 result = _keysIterator.moveNext();
188 } while (result &&
189 (_keysIterator.current == null ||
190 identical(_keysIterator.current, _HashMapImpl._DELETED_KEY)));
191 return result;
192 }
193
194 Iterator _keysIterator;
195 } 177 }
OLDNEW
« no previous file with comments | « no previous file | tests/corelib/ordered_set_iterator_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698