OLD | NEW |
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 * All operations are defined in terms of `length`, `operator[]`, | 10 * All operations are defined in terms of `length`, `operator[]`, |
(...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
294 } | 294 } |
295 | 295 |
296 void sort([int compare(E a, E b)]) { | 296 void sort([int compare(E a, E b)]) { |
297 if (compare == null) { | 297 if (compare == null) { |
298 var defaultCompare = Comparable.compare; | 298 var defaultCompare = Comparable.compare; |
299 compare = defaultCompare; | 299 compare = defaultCompare; |
300 } | 300 } |
301 Sort.sort(this, compare); | 301 Sort.sort(this, compare); |
302 } | 302 } |
303 | 303 |
| 304 void shuffle() { |
| 305 Random random = new Random(); |
| 306 int length = this.length; |
| 307 while (length > 1) { |
| 308 int pos = random.nextInt(length); |
| 309 length -= 1; |
| 310 var tmp = this[length]; |
| 311 this[length] = this[pos]; |
| 312 this[pos] = tmp; |
| 313 } |
| 314 } |
| 315 |
304 Map<int, E> asMap() { | 316 Map<int, E> asMap() { |
305 return new ListMapView(this); | 317 return new ListMapView(this); |
306 } | 318 } |
307 | 319 |
308 void _rangeCheck(int start, int end) { | 320 void _rangeCheck(int start, int end) { |
309 if (start < 0 || start > this.length) { | 321 if (start < 0 || start > this.length) { |
310 throw new RangeError.range(start, 0, this.length); | 322 throw new RangeError.range(start, 0, this.length); |
311 } | 323 } |
312 if (end < start || end > this.length) { | 324 if (end < start || end > this.length) { |
313 throw new RangeError.range(end, start, this.length); | 325 throw new RangeError.range(end, start, this.length); |
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
486 result.writeAll(this, ', '); | 498 result.writeAll(this, ', '); |
487 result.write(']'); | 499 result.write(']'); |
488 } finally { | 500 } finally { |
489 assert(identical(_toStringList.last, this)); | 501 assert(identical(_toStringList.last, this)); |
490 _toStringList.removeLast(); | 502 _toStringList.removeLast(); |
491 } | 503 } |
492 | 504 |
493 return result.toString(); | 505 return result.toString(); |
494 } | 506 } |
495 } | 507 } |
OLD | NEW |