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

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

Issue 12646005: Add Set.union, Set.difference. Change Set methods to expect Set. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 9 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
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 Collection<E> implements Set<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
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
121 121
122 void retainMatching(bool test(E element)) { 122 void retainMatching(bool test(E element)) {
123 _filterMatching(test, false); 123 _filterMatching(test, false);
124 } 124 }
125 125
126 void clear() { 126 void clear() {
127 _table._clear(); 127 _table._clear();
128 } 128 }
129 129
130 // Set. 130 // Set.
131 bool isSubsetOf(Collection<E> collection) { 131 bool isSubsetOf(Set<E> other) {
floitsch 2013/03/12 16:25:00 ditto. isSubsetOf(other) => other.containsAll(this
floitsch 2013/03/12 16:25:00 move all these methods to IterableMixinWorkaround?
Lasse Reichstein Nielsen 2013/03/13 13:01:42 Done.
132 Set otherSet; 132 if (length > other.length) return false;
133 if (collection is Set) { 133 for (E element in this) {
134 otherSet = collection; 134 if (!other.contains(element)) return false;
135 } else {
136 otherSet = collection.toSet();
137 } 135 }
138 return otherSet.containsAll(this); 136 return true;
139 } 137 }
140 138
141 bool containsAll(Collection<E> collection) { 139 bool containsAll(Set<E> other) {
142 for (E element in collection) { 140 if (other.length > length) return false;
141 for (E element in other) {
143 if (!this.contains(element)) return false; 142 if (!this.contains(element)) return false;
144 } 143 }
145 return true; 144 return true;
146 } 145 }
147 146
148 Set<E> intersection(Collection<E> other) { 147 Set<E> intersection(Set<E> other) {
148 Set<E> smaller;
149 Set<E> larger;
150 if (length < other.length) {
151 smaller = this;
152 larger = other;
153 } else {
154 smaller = other;
155 larger = this;
156 }
149 Set<E> result = new LinkedHashSet<E>(); 157 Set<E> result = new LinkedHashSet<E>();
150 for (E element in other) { 158 for (E element in smaller) {
151 if (this.contains(element)) { 159 if (larger.contains(element)) {
152 result.add(element); 160 result.add(element);
153 } 161 }
154 } 162 }
163 return result;
164 }
165
166 Set<E> union(Set<E> other) {
167 Set<E> result = new LinkedHashSet<E>();
168 result.addAll(this);
169 result.addAll(other);
170 return result;
171 }
172
173 Set<E> difference(Set<E> other) {
174 Set<E> result = new LinkedHashSet<E>();
175 for (E element in this) {
176 if (!other.contains(element)) {
177 result.add(element);
178 }
179 }
155 return result; 180 return result;
156 } 181 }
157 182
158 String toString() => Collections.collectionToString(this); 183 String toString() => Collections.collectionToString(this);
159 } 184 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698