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

Side by Side Diff: sdk/lib/collection_dev/iterable.dart

Issue 11938036: Hide collection-dev library. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Allow dart:_collection-dev in dartc. Other fixes. Created 7 years, 10 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/collection_dev/collection_dev_sources.gypi ('k') | sdk/lib/collection_dev/list.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 part of dart.collection.dev;
6
7 typedef T _Transformation<S, T>(S value);
8
9 class MappedIterable<S, T> extends Iterable<T> {
10 final Iterable<S> _iterable;
11 // TODO(ahe): Restore type when feature is implemented in dart2js
12 // checked mode. http://dartbug.com/7733
13 final /* _Transformation<S, T> */ _f;
14
15 MappedIterable(this._iterable, T this._f(S element));
16
17 Iterator<T> get iterator => new MappedIterator<S, T>(_iterable.iterator, _f);
18
19 // Length related functions are independent of the mapping.
20 int get length => _iterable.length;
21 bool get isEmpty => _iterable.isEmpty;
22 }
23
24
25 class MappedIterator<S, T> extends Iterator<T> {
26 T _current;
27 final Iterator<S> _iterator;
28 // TODO(ahe): Restore type when feature is implemented in dart2js
29 // checked mode. http://dartbug.com/7733
30 final /* _Transformation<S, T> */ _f;
31
32 MappedIterator(this._iterator, T this._f(S element));
33
34 bool moveNext() {
35 if (_iterator.moveNext()) {
36 _current = _f(_iterator.current);
37 return true;
38 } else {
39 _current = null;
40 return false;
41 }
42 }
43
44 T get current => _current;
45 }
46
47 /** Specialized alternative to [MappedIterable] for mapped [List]s. */
48 class MappedListIterable<S, T> extends Iterable<T> {
49 final List<S> _list;
50 /**
51 * Start index of the part of the list to map.
52 *
53 * Allows mapping only a sub-list of an existing list.
54 *
55 * Used to implement lazy skip/take on a [MappedListIterable].
56 */
57 final int _start;
58
59 /**
60 * End index of the part of the list to map.
61 *
62 * If null, always use the length of the list.
63 */
64 final int _end;
65
66 // TODO(ahe): Restore type when feature is implemented in dart2js
67 // checked mode. http://dartbug.com/7733
68 final /* _Transformation<S, T> */ _f;
69
70 MappedListIterable(this._list, T this._f(S element), this._start, this._end) {
71 if (_end != null && _end < _start) {
72 throw new ArgumentError("End ($_end) before start ($_start)");
73 }
74 }
75
76 /** The start index, limited to the current length of the list. */
77 int get _startIndex {
78 if (_start <= _list.length) return _start;
79 return _list.length;
80 }
81
82 /** The end index, if given, limited to the current length of the list. */
83 int get _endIndex {
84 if (_end == null || _end > _list.length) return _list.length;
85 return _end;
86 }
87
88 Iterator<T> get iterator =>
89 new MappedListIterator<S, T>(_list, _f, _startIndex, _endIndex);
90
91 void forEach(void action(T element)) {
92 int length = _list.length;
93 for (int i = _startIndex, n = _endIndex; i < n; i++) {
94 action(_f(_list[i]));
95 if (_list.length != length) {
96 throw new ConcurrentModificationError(_list);
97 }
98 }
99 }
100
101 bool get isEmpty => _startIndex == _endIndex;
102
103 int get length => _endIndex - _startIndex;
104
105 T get first {
106 int start = _startIndex;
107 if (start == _endIndex) {
108 throw new StateError("No elements");
109 }
110 return _f(_list.elementAt(start));
111 }
112
113 T get last {
114 int end = _endIndex;
115 if (end == _startIndex) {
116 throw new StateError("No elements");
117 }
118 return _f(_list.elementAt(end - 1));
119 }
120
121 T get single {
122 int start = _startIndex;
123 int end = _endIndex;
124 if (start != end - 1) {
125 if (start == end) {
126 throw new StateError("No elements");
127 }
128 throw new StateError("Too many elements");
129 }
130 return _f(_list[start]);
131 }
132
133 T elementAt(int index) {
134 index += _startIndex;
135 if (index >= _endIndex) {
136 throw new StateError("No matching element");
137 }
138 return _f(_list.elementAt(index));
139 }
140
141 bool contains(T element) {
142 int length = _list.length;
143 for (int i = _startIndex, n = _endIndex; i < n; i++) {
144 if (_f(_list[i]) == element) {
145 return true;
146 }
147 if (_list.length != length) {
148 throw new ConcurrentModificationError(_list);
149 }
150 }
151 return false;
152 }
153
154 bool every(bool test(T element)) {
155 int length = _list.length;
156 for (int i = _startIndex, n = _endIndex; i < n; i++) {
157 if (!test(_f(_list[i]))) return false;
158 if (_list.length != length) {
159 throw new ConcurrentModificationError(_list);
160 }
161 }
162 return true;
163 }
164
165 bool any(bool test(T element)) {
166 int length = _list.length;
167 for (int i = _startIndex, n = _endIndex; i < n; i++) {
168 if (test(_f(_list[i]))) return true;
169 if (_list.length != length) {
170 throw new ConcurrentModificationError(_list);
171 }
172 }
173 return false;
174 }
175
176 T firstMatching(bool test(T element), { T orElse() }) {
177 int length = _list.length;
178 for (int i = _startIndex, n = _endIndex; i < n; i++) {
179 T value = _f(_list[i]);
180 if (test(value)) return value;
181 if (_list.length != length) {
182 throw new ConcurrentModificationError(_list);
183 }
184 }
185 if (orElse != null) return orElse();
186 throw new StateError("No matching element");
187 }
188
189 T lastMatching(bool test(T element), { T orElse() }) {
190 int length = _list.length;
191 for (int i = _endIndex - 1, start = _startIndex; i >= start; i++) {
192 T value = _f(_list[i]);
193 if (test(value)) return value;
194 if (_list.length != length) {
195 throw new ConcurrentModificationError(_list);
196 }
197 }
198 if (orElse != null) return orElse();
199 throw new StateError("No matching element");
200 }
201
202 T singleMatching(bool test(T element)) {
203 int length = _list.length;
204 T match;
205 bool matchFound = false;
206 for (int i = _startIndex, n = _endIndex; i < n; i++) {
207 T value = _f(_list[i]);
208 if (test(value)) {
209 if (matchFound) {
210 throw new StateError("More than one matching element");
211 }
212 matchFound = true;
213 match = value;
214 }
215 if (_list.length != length) {
216 throw new ConcurrentModificationError(_list);
217 }
218 }
219 if (matchFound) return match;
220 throw new StateError("No matching element");
221 }
222
223 T min([int compare(T a, T b)]) {
224 if (compare == null) {
225 var defaultCompare = Comparable.compare;
226 compare = defaultCompare;
227 }
228 int length = _list.length;
229 int start = _startIndex;
230 int end = _endIndex;
231 if (start == end) return null;
232 T value = _f(_list[start]);
233 if (_list.length != length) {
234 throw new ConcurrentModificationError(_list);
235 }
236 for (int i = start + 1; i < end; i++) {
237 T nextValue = _f(_list[i]);
238 if (compare(value, nextValue) > 0) {
239 value = nextValue;
240 }
241 if (_list.length != length) {
242 throw new ConcurrentModificationError(_list);
243 }
244 }
245 return value;
246 }
247
248 T max([int compare(T a, T b)]) {
249 if (compare == null) {
250 var defaultCompare = Comparable.compare;
251 compare = defaultCompare;
252 }
253 int length = _list.length;
254 int start = _startIndex;
255 int end = _endIndex;
256 if (start == end) return null;
257 T value = _f(_list[start]);
258 if (_list.length != length) {
259 throw new ConcurrentModificationError(_list);
260 }
261 for (int i = start + 1; i < end; i++) {
262 T nextValue = _f(_list[i]);
263 if (compare(value, nextValue) < 0) {
264 value = nextValue;
265 }
266 if (_list.length != length) {
267 throw new ConcurrentModificationError(_list);
268 }
269 }
270 return value;
271 }
272
273 String join([String separator]) {
274 int start = _startIndex;
275 int end = _endIndex;
276 if (start == end) return "";
277 StringBuffer buffer = new StringBuffer("${_f(_list[start])}");
278 if (_list.length != length) {
279 throw new ConcurrentModificationError(_list);
280 }
281 for (int i = start + 1; i < end; i++) {
282 buffer.add(separator);
283 buffer.add("${_f(_list[i])}");
284 if (_list.length != length) {
285 throw new ConcurrentModificationError(_list);
286 }
287 }
288 return buffer.toString();
289 }
290
291 Iterable<T> where(bool test(T element)) => super.where(test);
292
293 Iterable map(f(T element)) {
294 return new MappedListIterable(_list, (S v) => f(_f(v)), _start, _end);
295 }
296
297 Iterable mappedBy(f(T element)) => map(f);
298
299 reduce(var initialValue, combine(var previousValue, T element)) {
300 return _list.reduce(initialValue, (v, S e) => combine(v, _f(e)));
301 }
302
303 Iterable<T> skip(int count) {
304 int start = _startIndex + count;
305 if (_end != null && start >= _end) {
306 return new EmptyIterable<T>();
307 }
308 return new MappedListIterable(_list, _f, start, _end);
309 }
310
311 Iterable<T> skipWhile(bool test(T element)) => super.skipWhile(test);
312
313 Iterable<T> take(int count) {
314 int newEnd = _start + count;
315 if (_end == null || newEnd < _end) {
316 return new MappedListIterable(_list, _f, _start, newEnd);
317 }
318 // Equivalent to "this".
319 return new MappedListIterable(_list, _f, _start, _end);
320 }
321
322 Iterable<T> takeWhile(bool test(T element)) => super.takeWhile(test);
323
324 List<T> toList() {
325 List<T> result = new List<T>();
326 forEach(result.add);
327 return result;
328 }
329
330 Set<T> toSet() {
331 Set<T> result = new Set<T>();
332 forEach(result.add);
333 return result;
334 }
335 }
336
337 /**
338 * Iterator for [MappedListIterable].
339 *
340 * A list iterator that iterates over (a sublist of) a list and
341 * returns the values transformed by a function.
342 *
343 * As a list iterator, it throws if the length of the list has
344 * changed during iteration.
345 */
346 class MappedListIterator<S, T> implements Iterator<T> {
347 List<S> _list;
348 // TODO(ahe): Restore type when feature is implemented in dart2js
349 // checked mode. http://dartbug.com/7733
350 final /* _Transformation<S, T> */ _f;
351 final int _endIndex;
352 final int _length;
353 int _index;
354 T _current;
355
356 MappedListIterator(List<S> list, this._f, int start, this._endIndex)
357 : _list = list, _length = list.length, _index = start;
358
359 T get current => _current;
360
361 bool moveNext() {
362 if (_list.length != _length) {
363 throw new ConcurrentModificationError(_list);
364 }
365 if (_index >= _endIndex) {
366 _current = null;
367 return false;
368 }
369 _current = _f(_list[_index]);
370 _index++;
371 return true;
372 }
373 }
374
375 typedef bool _ElementPredicate<E>(E element);
376
377 class WhereIterable<E> extends Iterable<E> {
378 final Iterable<E> _iterable;
379 // TODO(ahe): Restore type when feature is implemented in dart2js
380 // checked mode. http://dartbug.com/7733
381 final /* _ElementPredicate */ _f;
382
383 WhereIterable(this._iterable, bool this._f(E element));
384
385 Iterator<E> get iterator => new WhereIterator<E>(_iterable.iterator, _f);
386 }
387
388 class WhereIterator<E> extends Iterator<E> {
389 final Iterator<E> _iterator;
390 // TODO(ahe): Restore type when feature is implemented in dart2js
391 // checked mode. http://dartbug.com/7733
392 final /* _ElementPredicate */ _f;
393
394 WhereIterator(this._iterator, bool this._f(E element));
395
396 bool moveNext() {
397 while (_iterator.moveNext()) {
398 if (_f(_iterator.current)) {
399 return true;
400 }
401 }
402 return false;
403 }
404
405 E get current => _iterator.current;
406 }
407
408 typedef Iterable<T> _ExpandFunction<S, T>(S sourceElement);
409
410 class ExpandIterable<S, T> extends Iterable<T> {
411 final Iterable<S> _iterable;
412 // TODO(ahe): Restore type when feature is implemented in dart2js
413 // checked mode. http://dartbug.com/7733
414 final /* _ExpandFunction */ _f;
415
416 ExpandIterable(this._iterable, Iterable<T> this._f(S element));
417
418 Iterator<T> get iterator => new ExpandIterator<S, T>(_iterable.iterator, _f);
419 }
420
421 class ExpandIterator<S, T> implements Iterator<T> {
422 final Iterator<S> _iterator;
423 // TODO(ahe): Restore type when feature is implemented in dart2js
424 // checked mode. http://dartbug.com/7733
425 final /* _ExpandFunction */ _f;
426 // Initialize _currentExpansion to an empty iterable. A null value
427 // marks the end of iteration, and we don't want to call _f before
428 // the first moveNext call.
429 Iterator<T> _currentExpansion = const EmptyIterator();
430 T _current;
431
432 ExpandIterator(this._iterator, Iterable<T> this._f(S element));
433
434 void _nextExpansion() {
435 }
436
437 T get current => _current;
438
439 bool moveNext() {
440 if (_currentExpansion == null) return false;
441 while (!_currentExpansion.moveNext()) {
442 _current = null;
443 if (_iterator.moveNext()) {
444 // If _f throws, this ends iteration. Otherwise _currentExpansion and
445 // _current will be set again below.
446 _currentExpansion = null;
447 _currentExpansion = _f(_iterator.current).iterator;
448 } else {
449 return false;
450 }
451 }
452 _current = _currentExpansion.current;
453 return true;
454 }
455 }
456
457 class TakeIterable<E> extends Iterable<E> {
458 final Iterable<E> _iterable;
459 final int _takeCount;
460
461 TakeIterable(this._iterable, this._takeCount) {
462 if (_takeCount is! int || _takeCount < 0) {
463 throw new ArgumentError(_takeCount);
464 }
465 }
466
467 Iterator<E> get iterator {
468 return new TakeIterator<E>(_iterable.iterator, _takeCount);
469 }
470 }
471
472 class TakeIterator<E> extends Iterator<E> {
473 final Iterator<E> _iterator;
474 int _remaining;
475
476 TakeIterator(this._iterator, this._remaining) {
477 assert(_remaining is int && _remaining >= 0);
478 }
479
480 bool moveNext() {
481 _remaining--;
482 if (_remaining >= 0) {
483 return _iterator.moveNext();
484 }
485 _remaining = -1;
486 return false;
487 }
488
489 E get current {
490 if (_remaining < 0) return null;
491 return _iterator.current;
492 }
493 }
494
495 class TakeWhileIterable<E> extends Iterable<E> {
496 final Iterable<E> _iterable;
497 // TODO(ahe): Restore type when feature is implemented in dart2js
498 // checked mode. http://dartbug.com/7733
499 final /* _ElementPredicate */ _f;
500
501 TakeWhileIterable(this._iterable, bool this._f(E element));
502
503 Iterator<E> get iterator {
504 return new TakeWhileIterator<E>(_iterable.iterator, _f);
505 }
506 }
507
508 class TakeWhileIterator<E> extends Iterator<E> {
509 final Iterator<E> _iterator;
510 // TODO(ahe): Restore type when feature is implemented in dart2js
511 // checked mode. http://dartbug.com/7733
512 final /* _ElementPredicate */ _f;
513 bool _isFinished = false;
514
515 TakeWhileIterator(this._iterator, bool this._f(E element));
516
517 bool moveNext() {
518 if (_isFinished) return false;
519 if (!_iterator.moveNext() || !_f(_iterator.current)) {
520 _isFinished = true;
521 return false;
522 }
523 return true;
524 }
525
526 E get current {
527 if (_isFinished) return null;
528 return _iterator.current;
529 }
530 }
531
532 class SkipIterable<E> extends Iterable<E> {
533 final Iterable<E> _iterable;
534 final int _skipCount;
535
536 SkipIterable(this._iterable, this._skipCount) {
537 if (_skipCount is! int || _skipCount < 0) {
538 throw new ArgumentError(_skipCount);
539 }
540 }
541
542 Iterable<E> skip(int n) {
543 if (n is! int || n < 0) {
544 throw new ArgumentError(n);
545 }
546 return new SkipIterable<E>(_iterable, _skipCount + n);
547 }
548
549 Iterator<E> get iterator {
550 return new SkipIterator<E>(_iterable.iterator, _skipCount);
551 }
552 }
553
554 class SkipIterator<E> extends Iterator<E> {
555 final Iterator<E> _iterator;
556 int _skipCount;
557
558 SkipIterator(this._iterator, this._skipCount) {
559 assert(_skipCount is int && _skipCount >= 0);
560 }
561
562 bool moveNext() {
563 for (int i = 0; i < _skipCount; i++) _iterator.moveNext();
564 _skipCount = 0;
565 return _iterator.moveNext();
566 }
567
568 E get current => _iterator.current;
569 }
570
571 class SkipWhileIterable<E> extends Iterable<E> {
572 final Iterable<E> _iterable;
573 // TODO(ahe): Restore type when feature is implemented in dart2js
574 // checked mode. http://dartbug.com/7733
575 final /* _ElementPredicate */ _f;
576
577 SkipWhileIterable(this._iterable, bool this._f(E element));
578
579 Iterator<E> get iterator {
580 return new SkipWhileIterator<E>(_iterable.iterator, _f);
581 }
582 }
583
584 class SkipWhileIterator<E> extends Iterator<E> {
585 final Iterator<E> _iterator;
586 // TODO(ahe): Restore type when feature is implemented in dart2js
587 // checked mode. http://dartbug.com/7733
588 final /* _ElementPredicate */ _f;
589 bool _hasSkipped = false;
590
591 SkipWhileIterator(this._iterator, bool this._f(E element));
592
593 bool moveNext() {
594 if (!_hasSkipped) {
595 _hasSkipped = true;
596 while (_iterator.moveNext()) {
597 if (!_f(_iterator.current)) return true;
598 }
599 }
600 return _iterator.moveNext();
601 }
602
603 E get current => _iterator.current;
604 }
605
606 /**
607 * The always empty [Iterable].
608 */
609 class EmptyIterable<E> extends Iterable<E> {
610 const EmptyIterable();
611
612 Iterator<E> get iterator => const EmptyIterator();
613
614 void forEach(void action(E element)) {}
615
616 bool get isEmpty => true;
617
618 int get length => 0;
619
620 E get first { throw new StateError("No elements"); }
621
622 E get last { throw new StateError("No elements"); }
623
624 E get single { throw new StateError("No elements"); }
625
626 E elementAt(int index) { throw new RangeError.value(index); }
627
628 bool contains(E element) => false;
629
630 bool every(bool test(E element)) => true;
631
632 bool any(bool test(E element)) => false;
633
634 E firstMatching(bool test(E element), { E orElse() }) {
635 if (orElse != null) return orElse();
636 throw new StateError("No matching element");
637 }
638
639 E lastMatching(bool test(E element), { E orElse() }) {
640 if (orElse != null) return orElse();
641 throw new StateError("No matching element");
642 }
643
644 E singleMatching(bool test(E element), { E orElse() }) {
645 if (orElse != null) return orElse();
646 throw new StateError("No matching element");
647 }
648
649 E min([int compare(E a, E b)]) => null;
650
651 E max([int compare(E a, E b)]) => null;
652
653 String join([String separator]) => "";
654
655 Iterable<E> where(bool test(E element)) => this;
656
657 Iterable map(f(E element)) => const EmptyIterable();
658
659 Iterable mappedBy(f(E element)) => const EmptyIterable();
660
661 reduce(var initialValue, combine(var previousValue, E element)) {
662 return initialValue;
663 }
664
665 Iterable<E> skip(int count) => this;
666
667 Iterable<E> skipWhile(bool test(E element)) => this;
668
669 Iterable<E> take(int count) => this;
670
671 Iterable<E> takeWhile(bool test(E element)) => this;
672
673 List toList() => <E>[];
674
675 Set toSet() => new Set<E>();
676 }
677
678 /** The always empty iterator. */
679 class EmptyIterator<E> implements Iterator<E> {
680 const EmptyIterator();
681 bool moveNext() => false;
682 E get current => null;
683 }
OLDNEW
« no previous file with comments | « sdk/lib/collection_dev/collection_dev_sources.gypi ('k') | sdk/lib/collection_dev/list.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698