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

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

Issue 2715833004: fix ListMixin _filter and sort to work in strong mode (Closed)
Patch Set: introduce _compareAny Created 3 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
« no previous file with comments | « no previous file | no next file » | 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 /** 7 /**
8 * Abstract implementation of a list. 8 * Abstract implementation of a list.
9 * 9 *
10 * `ListBase` can be used as a base class for implementing the `List` interface. 10 * `ListBase` can be used as a base class for implementing the `List` interface.
(...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after
267 } 267 }
268 268
269 void removeWhere(bool test(E element)) { 269 void removeWhere(bool test(E element)) {
270 _filter(this, test, false); 270 _filter(this, test, false);
271 } 271 }
272 272
273 void retainWhere(bool test(E element)) { 273 void retainWhere(bool test(E element)) {
274 _filter(this, test, true); 274 _filter(this, test, true);
275 } 275 }
276 276
277 static void _filter(List source, 277 void _filter(List source, bool test(var element), bool retainMatching) {
Jennifer Messerly 2017/02/28 18:36:34 I also made the change you suggested here: https:/
278 bool test(var element), 278 List<E> retained = <E>[];
279 bool retainMatching) {
280 List retained = [];
281 int length = source.length; 279 int length = source.length;
282 for (int i = 0; i < length; i++) { 280 for (int i = 0; i < length; i++) {
283 var element = source[i]; 281 var element = source[i];
284 if (test(element) == retainMatching) { 282 if (test(element) == retainMatching) {
285 retained.add(element); 283 retained.add(element);
286 } 284 }
287 if (length != source.length) { 285 if (length != source.length) {
288 throw new ConcurrentModificationError(source); 286 throw new ConcurrentModificationError(source);
289 } 287 }
290 } 288 }
(...skipping 10 matching lines...) Expand all
301 E removeLast() { 299 E removeLast() {
302 if (length == 0) { 300 if (length == 0) {
303 throw IterableElementError.noElement(); 301 throw IterableElementError.noElement();
304 } 302 }
305 E result = this[length - 1]; 303 E result = this[length - 1];
306 length--; 304 length--;
307 return result; 305 return result;
308 } 306 }
309 307
310 void sort([int compare(E a, E b)]) { 308 void sort([int compare(E a, E b)]) {
311 if (compare == null) { 309 Sort.sort(this, compare ?? _compareAny);
312 Sort.sort(this, Comparable.compare); 310 }
313 } else { 311
314 Sort.sort(this, compare); 312 static int _compareAny(a, b) {
315 } 313 // In strong mode this requires a cast to ensure `a` and `b` are Comparable.
Lasse Reichstein Nielsen 2017/02/28 07:05:56 Confusing comment - I don't see any cast?
Jennifer Messerly 2017/02/28 18:36:35 Ah thanks! will clarify. The cast is added implici
314 return Comparable.compare(a, b);
316 } 315 }
317 316
318 void shuffle([Random random]) { 317 void shuffle([Random random]) {
319 if (random == null) random = new Random(); 318 if (random == null) random = new Random();
320 int length = this.length; 319 int length = this.length;
321 while (length > 1) { 320 while (length > 1) {
322 int pos = random.nextInt(length); 321 int pos = random.nextInt(length);
323 length -= 1; 322 length -= 1;
324 var tmp = this[length]; 323 var tmp = this[length];
325 this[length] = this[pos]; 324 this[length] = this[pos];
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after
507 for (E element in iterable) { 506 for (E element in iterable) {
508 this[index++] = element; 507 this[index++] = element;
509 } 508 }
510 } 509 }
511 } 510 }
512 511
513 Iterable<E> get reversed => new ReversedListIterable<E>(this); 512 Iterable<E> get reversed => new ReversedListIterable<E>(this);
514 513
515 String toString() => IterableBase.iterableToFullString(this, '[', ']'); 514 String toString() => IterableBase.iterableToFullString(this, '[', ']');
516 } 515 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698