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

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

Issue 11931034: Add methods to Collection. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Address comments. Created 7 years, 11 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/core/queue.dart ('k') | sdk/lib/html/dart2js/html_dart2js.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 */
11 abstract class Set<E> extends Collection<E> { 11 abstract class Set<E> extends Collection<E> {
12 factory Set() => new _HashSetImpl<E>(); 12 factory Set() => new HashSet<E>();
13 13
14 /** 14 /**
15 * Creates a [Set] that contains all elements of [other]. 15 * Creates a [Set] that contains all elements of [other].
16 */ 16 */
17 factory Set.from(Iterable<E> other) => new _HashSetImpl<E>.from(other); 17 factory Set.from(Iterable<E> other) => new HashSet<E>.from(other);
18 18
19 /** 19 /**
20 * Returns true if [value] is in the set. 20 * Returns true if [value] is in the set.
21 */ 21 */
22 bool contains(E value); 22 bool contains(E value);
23 23
24 /** 24 /**
25 * Adds [value] into the set. The method has no effect if 25 * Adds [value] into the set. The method has no effect if
26 * [value] was already in the set. 26 * [value] was already in the set.
27 */ 27 */
28 void add(E value); 28 void add(E value);
29 29
30 /** 30 /**
31 * Removes [value] from the set. Returns true if [value] was 31 * Removes [value] from the set. Returns true if [value] was
32 * in the set. Returns false otherwise. The method has no effect 32 * in the set. Returns false otherwise. The method has no effect
33 * if [value] value was not in the set. 33 * if [value] value was not in the set.
34 */ 34 */
35 bool remove(E value); 35 bool remove(Object value);
36
37 /**
38 * Adds all the elements of the given [iterable] to the set.
39 */
40 void addAll(Iterable<E> iterable);
41
42 /**
43 * Removes all the elements of the given collection from the set.
44 */
45 void removeAll(Iterable<E> iterable);
46 36
47 /** 37 /**
48 * Returns true if [collection] contains all the elements of this 38 * Returns true if [collection] contains all the elements of this
49 * collection. 39 * collection.
50 */ 40 */
51 bool isSubsetOf(Collection<E> collection); 41 bool isSubsetOf(Collection<E> collection);
52 42
53 /** 43 /**
54 * Returns true if this collection contains all the elements of 44 * Returns true if this collection contains all the elements of
55 * [collection]. 45 * [collection].
56 */ 46 */
57 bool containsAll(Collection<E> collection); 47 bool containsAll(Collection<E> collection);
58 48
59 /** 49 /**
60 * Returns a new set which is the intersection between this set and 50 * Returns a new set which is the intersection between this set and
61 * the given collection. 51 * the given collection.
62 */ 52 */
63 Set<E> intersection(Collection<E> other); 53 Set<E> intersection(Collection<E> other);
64 54
65 /** 55 /**
66 * Removes all elements in the set. 56 * Removes all elements in the set.
67 */ 57 */
68 void clear(); 58 void clear();
69
70 } 59 }
71 60
72 abstract class HashSet<E> extends Set<E> { 61
73 factory HashSet() => new _HashSetImpl<E>(); 62 class HashSet<E> extends Collection<E> implements Set<E> {
63 // The map backing this set. The associations in this map are all
64 // of the form element -> element. If a value is not in the map,
65 // then it is not in the set.
66 final _HashMapImpl<E, E> _backingMap;
67
68 HashSet() : _backingMap = new _HashMapImpl<E, E>();
74 69
75 /** 70 /**
76 * Creates a [Set] that contains all elements of [other]. 71 * Creates a [Set] that contains all elements of [other].
77 */ 72 */
78 factory HashSet.from(Iterable<E> other) => new _HashSetImpl<E>.from(other); 73 factory HashSet.from(Iterable<E> other) {
79 } 74 Set<E> set = new HashSet<E>();
80
81
82 class _HashSetImpl<E> extends Iterable<E> implements HashSet<E> {
83
84 _HashSetImpl() {
85 _backingMap = new _HashMapImpl<E, E>();
86 }
87
88 factory _HashSetImpl.from(Iterable<E> other) {
89 Set<E> set = new _HashSetImpl<E>();
90 for (final e in other) { 75 for (final e in other) {
91 set.add(e); 76 set.add(e);
92 } 77 }
93 return set; 78 return set;
94 } 79 }
95 80
96 void clear() { 81 void clear() {
97 _backingMap.clear(); 82 _backingMap.clear();
98 } 83 }
99 84
100 void add(E value) { 85 void add(E value) {
101 _backingMap[value] = value; 86 _backingMap[value] = value;
102 } 87 }
103 88
89 bool remove(Object value) {
90 if (!_backingMap.containsKey(value)) return false;
91 _backingMap.remove(value);
92 return true;
93 }
94
104 bool contains(E value) { 95 bool contains(E value) {
105 return _backingMap.containsKey(value); 96 return _backingMap.containsKey(value);
106 } 97 }
107 98
108 bool remove(E value) {
109 if (!_backingMap.containsKey(value)) return false;
110 _backingMap.remove(value);
111 return true;
112 }
113
114 void addAll(Iterable<E> iterable) {
115 for (E element in iterable) {
116 add(element);
117 }
118 }
119
120 Set<E> intersection(Collection<E> collection) { 99 Set<E> intersection(Collection<E> collection) {
121 Set<E> result = new Set<E>(); 100 Set<E> result = new Set<E>();
122 collection.forEach((E value) { 101 collection.forEach((E value) {
123 if (contains(value)) result.add(value); 102 if (contains(value)) result.add(value);
124 }); 103 });
125 return result; 104 return result;
126 } 105 }
127 106
128 bool isSubsetOf(Collection<E> other) { 107 bool isSubsetOf(Collection<E> other) {
129 return new Set<E>.from(other).containsAll(this); 108 return new Set<E>.from(other).containsAll(this);
130 } 109 }
131 110
132 void removeAll(Iterable<E> iterable) {
133 for (E value in iterable) {
134 remove(value);
135 }
136 }
137
138 bool containsAll(Collection<E> collection) { 111 bool containsAll(Collection<E> collection) {
139 return collection.every((E value) { 112 return collection.every((E value) {
140 return contains(value); 113 return contains(value);
141 }); 114 });
142 } 115 }
143 116
144 void forEach(void f(E element)) { 117 void forEach(void f(E element)) {
145 _backingMap.forEach((E key, E value) { 118 _backingMap.forEach((E key, E value) {
146 f(key); 119 f(key);
147 }); 120 });
148 } 121 }
149 122
150 bool get isEmpty { 123 bool get isEmpty {
151 return _backingMap.isEmpty; 124 return _backingMap.isEmpty;
152 } 125 }
153 126
154 int get length { 127 int get length {
155 return _backingMap.length; 128 return _backingMap.length;
156 } 129 }
157 130
158 Iterator<E> get iterator => new _HashSetIterator<E>(this); 131 Iterator<E> get iterator => new _HashSetIterator<E>(this);
159 132
160 String toString() { 133 String toString() {
161 return Collections.collectionToString(this); 134 return Collections.collectionToString(this);
162 } 135 }
163
164 // 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,
166 // then it is not in the set.
167 _HashMapImpl<E, E> _backingMap;
168 } 136 }
169 137
170 class _HashSetIterator<E> implements Iterator<E> { 138 class _HashSetIterator<E> implements Iterator<E> {
171 139
172 _HashSetIterator(_HashSetImpl<E> set) 140 _HashSetIterator(HashSet<E> set)
173 : _keysIterator = set._backingMap._keys.iterator; 141 : _keysIterator = set._backingMap._keys.iterator;
174 142
175 E get current { 143 E get current {
176 var result = _keysIterator.current; 144 var result = _keysIterator.current;
177 if (identical(result, _HashMapImpl._DELETED_KEY)) { 145 if (identical(result, _HashMapImpl._DELETED_KEY)) {
178 // TODO(floitsch): improve the error reporting. 146 // TODO(floitsch): improve the error reporting.
179 throw new StateError("Concurrent modification."); 147 throw new StateError("Concurrent modification.");
180 } 148 }
181 return result; 149 return result;
182 } 150 }
183 151
184 bool moveNext() { 152 bool moveNext() {
185 bool result; 153 bool result;
186 do { 154 do {
187 result = _keysIterator.moveNext(); 155 result = _keysIterator.moveNext();
188 } while (result && 156 } while (result &&
189 (_keysIterator.current == null || 157 (_keysIterator.current == null ||
190 identical(_keysIterator.current, _HashMapImpl._DELETED_KEY))); 158 identical(_keysIterator.current, _HashMapImpl._DELETED_KEY)));
191 return result; 159 return result;
192 } 160 }
193 161
194 Iterator _keysIterator; 162 Iterator _keysIterator;
195 } 163 }
OLDNEW
« no previous file with comments | « sdk/lib/core/queue.dart ('k') | sdk/lib/html/dart2js/html_dart2js.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698