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

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

Issue 14071002: Added new version of reduce. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 8 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 /** 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[]`,
11 * `operator[]=` and `length=`, which need to be implemented. 11 * `operator[]=` and `length=`, which need to be implemented.
12 */ 12 */
13 typedef ListBase<E> = Object with ListMixin<E>; 13 typedef ListBase<E> = Object with ListMixin<E>;
14 14
15 /** 15 /**
16 * Abstract implementation of a fixed-length list.
floitsch 2013/04/10 15:14:07 I thought we agreed that these classes are not in
Lasse Reichstein Nielsen 2013/04/11 07:54:04 Yes, yet another bad merge. Seems I was working on
17 *
18 * All operations are defined in terms of `length`, `operator[]` and
19 * `operator[]=`, which need to be implemented.
20 */
21 typedef FixedLengthListBase<E> = ListBase<E> with FixedLengthListMixin<E>;
22
23 /**
24 * Abstract implementation of an unmodifiable list.
25 *
26 * All operations are defined in terms of `length` and `operator[]`,
27 * which need to be implemented.
28 */
29 typedef UnmodifiableListBase<E> = ListBase<E> with UnmodifiableListMixin<E>;
30
31 /**
16 * Base implementation of a [List] class. 32 * Base implementation of a [List] class.
17 * 33 *
18 * This class can be used as a mixin. 34 * This class can be used as a mixin.
19 * 35 *
20 * This implements all read operations using only the `length` and 36 * This implements all read operations using only the `length` and
21 * `operator[]` members. It implements write operations using those and 37 * `operator[]` members. It implements write operations using those and
22 * `length=` and `operator[]=` 38 * `length=` and `operator[]=`
23 * 39 *
24 * A fixed-length list should mix this class in, and the [FixedLengthListMixin] 40 * A fixed-length list should mix this class in, and the [FixedLengthListMixin]
25 * as well, in that order, to overwrite the methods that modify the length. 41 * as well, in that order, to overwrite the methods that modify the length.
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
134 match = element; 150 match = element;
135 } 151 }
136 if (length != this.length) { 152 if (length != this.length) {
137 throw new ConcurrentModificationError(this); 153 throw new ConcurrentModificationError(this);
138 } 154 }
139 } 155 }
140 if (matchFound) return match; 156 if (matchFound) return match;
141 throw new StateError("No matching element"); 157 throw new StateError("No matching element");
142 } 158 }
143 159
144 E min([int compare(E a, E b)]) { 160 String join([String separator]) {
floitsch 2013/04/10 15:14:07 bad merge.
Lasse Reichstein Nielsen 2013/04/11 08:00:23 Done.
145 if (length == 0) return null;
146 if (compare == null) {
147 var defaultCompare = Comparable.compare;
148 compare = defaultCompare;
149 }
150 E min = this[0];
151 int length = this.length; 161 int length = this.length;
152 for (int i = 1; i < length; i++) { 162 if (separator != null && !separator.isEmpty) {
floitsch 2013/04/10 15:14:07 bad merge.
Lasse Reichstein Nielsen 2013/04/11 08:00:23 Done.
153 E element = this[i];
154 if (compare(min, element) > 0) {
155 min = element;
156 }
157 if (length != this.length) {
158 throw new ConcurrentModificationError(this);
159 }
160 }
161 return min;
162 }
163
164 E max([int compare(E a, E b)]) {
165 if (length == 0) return null;
166 if (compare == null) {
167 var defaultCompare = Comparable.compare;
168 compare = defaultCompare;
169 }
170 E max = this[0];
171 int length = this.length;
172 for (int i = 1; i < length; i++) {
173 E element = this[i];
174 if (compare(max, element) < 0) {
175 max = element;
176 }
177 if (length != this.length) {
178 throw new ConcurrentModificationError(this);
179 }
180 }
181 return max;
182 }
183
184 String join([String separator = ""]) {
185 int length = this.length;
186 if (!separator.isEmpty) {
187 if (length == 0) return ""; 163 if (length == 0) return "";
188 String first = "${this[0]}"; 164 String first = "${this[0]}";
189 if (length != this.length) { 165 if (length != this.length) {
190 throw new ConcurrentModificationError(this); 166 throw new ConcurrentModificationError(this);
191 } 167 }
192 StringBuffer buffer = new StringBuffer(first); 168 StringBuffer buffer = new StringBuffer(first);
193 for (int i = 1; i < length; i++) { 169 for (int i = 1; i < length; i++) {
194 buffer.write(separator); 170 buffer.write(separator);
195 buffer.write(this[i]); 171 buffer.write("${this[i]}");
floitsch 2013/04/10 15:14:07 bad merge.
196 if (length != this.length) { 172 if (length != this.length) {
197 throw new ConcurrentModificationError(this); 173 throw new ConcurrentModificationError(this);
198 } 174 }
199 } 175 }
200 return buffer.toString(); 176 return buffer.toString();
201 } else { 177 } else {
202 StringBuffer buffer = new StringBuffer(); 178 StringBuffer buffer = new StringBuffer();
203 for (int i = 0; i < length; i++) { 179 for (int i = 0; i < length; i++) {
204 buffer.write(this[i]); 180 buffer.write("${this[i]}");
floitsch 2013/04/10 15:14:07 bad merge.
205 if (length != this.length) { 181 if (length != this.length) {
206 throw new ConcurrentModificationError(this); 182 throw new ConcurrentModificationError(this);
207 } 183 }
208 } 184 }
209 return buffer.toString(); 185 return buffer.toString();
210 } 186 }
211 } 187 }
212 188
213 Iterable<E> where(bool test(E element)) => new WhereIterable<E>(this, test); 189 Iterable<E> where(bool test(E element)) => new WhereIterable<E>(this, test);
214 190
215 Iterable map(f(E element)) => new MappedListIterable(this, f); 191 Iterable map(f(E element)) => new MappedListIterable(this, f);
216 192
217 reduce(var initialValue, combine(var previousValue, E element)) { 193 E reduce(E combine(E previousValue, E element)) {
218 return fold(initialValue, combine); 194 if (length == 0) throw new StateError("No elements");
195 E value = this[0];
196 for (int i = 1; i < length; i++) {
197 value = combine(value, this[i]);
198 }
199 return value;
219 } 200 }
220 201
221 fold(var initialValue, combine(var previousValue, E element)) { 202 fold(var initialValue, combine(var previousValue, E element)) {
222 var value = initialValue; 203 var value = initialValue;
223 int length = this.length; 204 int length = this.length;
224 for (int i = 0; i < length; i++) { 205 for (int i = 0; i < length; i++) {
225 value = combine(value, this[i]); 206 value = combine(value, this[i]);
226 if (length != this.length) { 207 if (length != this.length) {
227 throw new ConcurrentModificationError(this); 208 throw new ConcurrentModificationError(this);
228 } 209 }
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
285 } 266 }
286 267
287 void removeAll(Iterable<Object> elements) { 268 void removeAll(Iterable<Object> elements) {
288 if (elements is! Set) { 269 if (elements is! Set) {
289 elements = elements.toSet(); 270 elements = elements.toSet();
290 } 271 }
291 _filter(this, elements.contains, false); 272 _filter(this, elements.contains, false);
292 } 273 }
293 274
294 275
295 void retainAll(Iterable<E> elements) { 276 void retainAll(Iterable<E> iterable) {
296 if (elements is! Set) { 277 if (elements is! Set) {
297 elements = elements.toSet(); 278 elements = elements.toSet();
298 } 279 }
299 _filter(this, elements.contains, true); 280 _filter(this, elements.contains, true);
300 } 281 }
301 282
302 void removeWhere(bool test(E element)) { 283 void removeWhere(bool test(E element)) {
303 _filter(this, test, false); 284 _filter(this, test, false);
304 } 285 }
305 286
(...skipping 14 matching lines...) Expand all
320 if (length != source.length) { 301 if (length != source.length) {
321 throw new ConcurrentModificationError(source); 302 throw new ConcurrentModificationError(source);
322 } 303 }
323 } 304 }
324 if (retained.length != source.length) { 305 if (retained.length != source.length) {
325 source.setRange(0, retained.length, retained); 306 source.setRange(0, retained.length, retained);
326 source.length = retained.length; 307 source.length = retained.length;
327 } 308 }
328 } 309 }
329 310
330 void clear() { this.length = 0; }
floitsch 2013/04/10 15:14:07 Didn't you just add them? bad merge?
331
332 // List interface. 311 // List interface.
333 312
334 E removeLast() {
335 if (length == 0) {
336 throw new StateError("No elements");
337 }
338 E result = this[length - 1];
339 length--;
340 return result;
341 }
342
343 void sort([Comparator<E> compare]) { 313 void sort([Comparator<E> compare]) {
344 Sort.sort(this, compare); 314 Sort.sort(this, compare);
345 } 315 }
346 316
347 Map<int, E> asMap() { 317 Map<int, E> asMap() {
348 return new ListMapView(this); 318 return new ListMapView(this);
349 } 319 }
350 320
351 List<E> sublist(int start, [int end]) { 321 List<E> sublist(int start, [int end]) {
352 if (end == null) end = length; 322 if (end == null) end = length;
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
444 * the search at index [startIndex] to 0. 414 * the search at index [startIndex] to 0.
445 * Returns -1 if [element] is not found. 415 * Returns -1 if [element] is not found.
446 */ 416 */
447 int lastIndexOf(E element, [int startIndex]) { 417 int lastIndexOf(E element, [int startIndex]) {
448 if (startIndex == null) { 418 if (startIndex == null) {
449 startIndex = this.length - 1; 419 startIndex = this.length - 1;
450 } else { 420 } else {
451 if (startIndex < 0) { 421 if (startIndex < 0) {
452 return -1; 422 return -1;
453 } 423 }
454 if (startIndex >= this.length) { 424 if (startIndex >= a.length) {
floitsch 2013/04/10 15:14:07 I guess bad merge.
455 startIndex = this.length - 1; 425 startIndex = a.length - 1;
456 } 426 }
457 } 427 }
458 for (int i = startIndex; i >= 0; i--) { 428 for (int i = startIndex; i >= 0; i--) {
459 if (this[i] == element) { 429 if (this[i] == element) {
460 return i; 430 return i;
461 } 431 }
462 } 432 }
463 return -1; 433 return -1;
464 } 434 }
465 435
466 Iterable<E> get reversed => new ReversedListIterable(this); 436 Iterable<E> get reversed => new _ReversedListIterable(this);
floitsch 2013/04/10 15:14:07 bad merge.
467 } 437 }
438
439 /**
440 * Mixin that throws on the length changing operations of [List].
441 *
442 * Intended to mix-in on top of [ListMixin] for fixed-length lists.
443 */
444 abstract class FixedLengthListMixin<E> {
floitsch 2013/04/10 15:14:07 This shouldn't be here either.
445 void set length(int newLength) {
446 throw new UnsupportedError(
447 "Cannot change the length of a fixed-length list");
448 }
449
450 void add(E value) {
451 throw new UnsupportedError(
452 "Cannot add to a fixed-length list");
453 }
454
455 void insert(int index, E value) {
456 throw new UnsupportedError(
457 "Cannot add to a fixed-length list");
458 }
459
460 void addAll(Iterable<E> iterable) {
461 throw new UnsupportedError(
462 "Cannot add to a fixed-length list");
463 }
464
465 void remove(E element) {
466 throw new UnsupportedError(
467 "Cannot remove from a fixed-length list");
468 }
469
470 void removeAll(Iterable elements) {
471 throw new UnsupportedError(
472 "Cannot remove from a fixed-length list");
473 }
474
475 void retainAll(Iterable elements) {
476 throw new UnsupportedError(
477 "Cannot remove from a fixed-length list");
478 }
479
480 void removeWhere(bool test(E element)) {
481 throw new UnsupportedError(
482 "Cannot remove from a fixed-length list");
483 }
484
485 void retainWhere(bool test(E element)) {
486 throw new UnsupportedError(
487 "Cannot remove from a fixed-length list");
488 }
489
490 void clear() {
491 throw new UnsupportedError(
492 "Cannot clear a fixed-length list");
493 }
494
495 E removeAt(int index) {
496 throw new UnsupportedError(
497 "Cannot remove from a fixed-length list");
498 }
499
500 E removeLast() {
501 throw new UnsupportedError(
502 "Cannot remove from a fixed-length list");
503 }
504
505 void removeRange(int start, int length) {
506 throw new UnsupportedError(
507 "Cannot remove from a fixed-length list");
508 }
509
510 void insertRange(int start, int length, [E initialValue]) {
511 throw new UnsupportedError(
512 "Cannot insert range in a fixed-length list");
513 }
514 }
515
516 /**
517 * Mixin for an unmodifiable [List] class.
518 *
519 * This overrides all mutating methods with methods that throw.
520 * This mixin is intended to be mixed in on top of [ListMixin] on
521 * unmodifiable lists.
522 */
523 abstract class UnmodifiableListMixin<E> {
floitsch 2013/04/10 15:14:07 ditto.
524
525 void operator []=(int index, E value) {
526 throw new UnsupportedError(
527 "Cannot modify an unmodifiable list");
528 }
529
530 void set length(int newLength) {
531 throw new UnsupportedError(
532 "Cannot change the length of an unmodifiable list");
533 }
534
535 void add(E value) {
536 throw new UnsupportedError(
537 "Cannot add to an unmodifiable list");
538 }
539
540 E insert(int index, E value) {
541 throw new UnsupportedError(
542 "Cannot add to an unmodifiable list");
543 }
544
545 void addAll(Iterable<E> iterable) {
546 throw new UnsupportedError(
547 "Cannot add to an unmodifiable list");
548 }
549
550 void remove(E element) {
551 throw new UnsupportedError(
552 "Cannot remove from an unmodifiable list");
553 }
554
555 void removeAll(Iterable elements) {
556 throw new UnsupportedError(
557 "Cannot remove from an unmodifiable list");
558 }
559
560 void retainAll(Iterable elements) {
561 throw new UnsupportedError(
562 "Cannot remove from an unmodifiable list");
563 }
564
565 void removeWhere(bool test(E element)) {
566 throw new UnsupportedError(
567 "Cannot remove from an unmodifiable list");
568 }
569
570 void retainWhere(bool test(E element)) {
571 throw new UnsupportedError(
572 "Cannot remove from an unmodifiable list");
573 }
574
575 void sort([Comparator<E> compare]) {
576 throw new UnsupportedError(
577 "Cannot modify an unmodifiable list");
578 }
579
580 void clear() {
581 throw new UnsupportedError(
582 "Cannot clear an unmodifiable list");
583 }
584
585 E removeAt(int index) {
586 throw new UnsupportedError(
587 "Cannot remove from an unmodifiable list");
588 }
589
590 E removeLast() {
591 throw new UnsupportedError(
592 "Cannot remove from an unmodifiable list");
593 }
594
595 void setRange(int start, int length, List<E> from, [int startFrom]) {
596 throw new UnsupportedError(
597 "Cannot modify an unmodifiable list");
598 }
599
600 void removeRange(int start, int length) {
601 throw new UnsupportedError(
602 "Cannot remove from an unmodifiable list");
603 }
604
605 void insertRange(int start, int length, [E initialValue]) {
606 throw new UnsupportedError(
607 "Cannot insert range in an unmodifiable list");
608 }
609 }
610
611 class _ReversedListIterable<E> extends ListIterable<E> {
612 Iterable<E> _source;
613 _ReversedListIterable(this._source);
614
615 int get length => _source.length;
616
617 E elementAt(int index) => _source.elementAt(_source.length - 1 - index);
618 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698