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

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

Issue 18749002: Use typedefs in lib. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Updated cf. comment Created 7 years, 5 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 | « no previous file | sdk/lib/_internal/compiler/implementation/js_backend/emitter.dart » ('j') | 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) 2011, the Dart project authors. Please see the AUTHORS file 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 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.dev; 5 part of dart._collection.dev;
6 6
7 7
8 // This is a hack to make @deprecated work in dart:io. Don't remove or use this, 8 // This is a hack to make @deprecated work in dart:io. Don't remove or use this,
9 // unless coordinated with either me or the core library team. Thanks! 9 // unless coordinated with either me or the core library team. Thanks!
10 // TODO(ajohnsen): Remove at the 11th of Auguest 2013. 10 // TODO(ajohnsen): Remove at the 11th of Auguest 2013.
(...skipping 313 matching lines...) Expand 10 before | Expand all | Expand 10 after
324 _current = _iterable.elementAt(_index); 324 _current = _iterable.elementAt(_index);
325 _index++; 325 _index++;
326 return true; 326 return true;
327 } 327 }
328 } 328 }
329 329
330 typedef T _Transformation<S, T>(S value); 330 typedef T _Transformation<S, T>(S value);
331 331
332 class MappedIterable<S, T> extends IterableBase<T> { 332 class MappedIterable<S, T> extends IterableBase<T> {
333 final Iterable<S> _iterable; 333 final Iterable<S> _iterable;
334 // TODO(ahe): Restore type when feature is implemented in dart2js 334 final _Transformation<S, T> _f;
335 // checked mode. http://dartbug.com/7733
336 final /* _Transformation<S, T> */ _f;
337 335
338 MappedIterable(this._iterable, T this._f(S element)); 336 MappedIterable(this._iterable, T this._f(S element));
339 337
340 Iterator<T> get iterator => new MappedIterator<S, T>(_iterable.iterator, _f); 338 Iterator<T> get iterator => new MappedIterator<S, T>(_iterable.iterator, _f);
341 339
342 // Length related functions are independent of the mapping. 340 // Length related functions are independent of the mapping.
343 int get length => _iterable.length; 341 int get length => _iterable.length;
344 bool get isEmpty => _iterable.isEmpty; 342 bool get isEmpty => _iterable.isEmpty;
345 343
346 // Index based lookup can be done before transforming. 344 // Index based lookup can be done before transforming.
347 T get first => _f(_iterable.first); 345 T get first => _f(_iterable.first);
348 T get last => _f(_iterable.last); 346 T get last => _f(_iterable.last);
349 T get single => _f(_iterable.single); 347 T get single => _f(_iterable.single);
350 T elementAt(int index) => _f(_iterable.elementAt(index)); 348 T elementAt(int index) => _f(_iterable.elementAt(index));
351 } 349 }
352 350
353 class MappedIterator<S, T> extends Iterator<T> { 351 class MappedIterator<S, T> extends Iterator<T> {
354 T _current; 352 T _current;
355 final Iterator<S> _iterator; 353 final Iterator<S> _iterator;
356 // TODO(ahe): Restore type when feature is implemented in dart2js 354 final _Transformation<S, T> _f;
357 // checked mode. http://dartbug.com/7733
358 final /* _Transformation<S, T> */ _f;
359 355
360 MappedIterator(this._iterator, T this._f(S element)); 356 MappedIterator(this._iterator, T this._f(S element));
361 357
362 bool moveNext() { 358 bool moveNext() {
363 if (_iterator.moveNext()) { 359 if (_iterator.moveNext()) {
364 _current = _f(_iterator.current); 360 _current = _f(_iterator.current);
365 return true; 361 return true;
366 } 362 }
367 _current = null; 363 _current = null;
368 return false; 364 return false;
369 } 365 }
370 366
371 T get current => _current; 367 T get current => _current;
372 } 368 }
373 369
374 /** Specialized alternative to [MappedIterable] for mapped [List]s. */ 370 /** Specialized alternative to [MappedIterable] for mapped [List]s. */
375 class MappedListIterable<S, T> extends ListIterable<T> { 371 class MappedListIterable<S, T> extends ListIterable<T> {
376 final Iterable<S> _source; 372 final Iterable<S> _source;
377 // TODO(ahe): Restore type when feature is implemented in dart2js 373 final _Transformation<S, T> _f;
378 // checked mode. http://dartbug.com/7733
379 final /* _Transformation<S, T> */ _f;
380 374
381 MappedListIterable(this._source, T this._f(S value)); 375 MappedListIterable(this._source, T this._f(S value));
382 376
383 int get length => _source.length; 377 int get length => _source.length;
384 T elementAt(int index) => _f(_source.elementAt(index)); 378 T elementAt(int index) => _f(_source.elementAt(index));
385 } 379 }
386 380
387 381
388 typedef bool _ElementPredicate<E>(E element); 382 typedef bool _ElementPredicate<E>(E element);
389 383
390 class WhereIterable<E> extends IterableBase<E> { 384 class WhereIterable<E> extends IterableBase<E> {
391 final Iterable<E> _iterable; 385 final Iterable<E> _iterable;
392 // TODO(ahe): Restore type when feature is implemented in dart2js 386 final _ElementPredicate _f;
393 // checked mode. http://dartbug.com/7733
394 final /* _ElementPredicate */ _f;
395 387
396 WhereIterable(this._iterable, bool this._f(E element)); 388 WhereIterable(this._iterable, bool this._f(E element));
397 389
398 Iterator<E> get iterator => new WhereIterator<E>(_iterable.iterator, _f); 390 Iterator<E> get iterator => new WhereIterator<E>(_iterable.iterator, _f);
399 } 391 }
400 392
401 class WhereIterator<E> extends Iterator<E> { 393 class WhereIterator<E> extends Iterator<E> {
402 final Iterator<E> _iterator; 394 final Iterator<E> _iterator;
403 // TODO(ahe): Restore type when feature is implemented in dart2js 395 final _ElementPredicate _f;
404 // checked mode. http://dartbug.com/7733
405 final /* _ElementPredicate */ _f;
406 396
407 WhereIterator(this._iterator, bool this._f(E element)); 397 WhereIterator(this._iterator, bool this._f(E element));
408 398
409 bool moveNext() { 399 bool moveNext() {
410 while (_iterator.moveNext()) { 400 while (_iterator.moveNext()) {
411 if (_f(_iterator.current)) { 401 if (_f(_iterator.current)) {
412 return true; 402 return true;
413 } 403 }
414 } 404 }
415 return false; 405 return false;
416 } 406 }
417 407
418 E get current => _iterator.current; 408 E get current => _iterator.current;
419 } 409 }
420 410
421 typedef Iterable<T> _ExpandFunction<S, T>(S sourceElement); 411 typedef Iterable<T> _ExpandFunction<S, T>(S sourceElement);
422 412
423 class ExpandIterable<S, T> extends IterableBase<T> { 413 class ExpandIterable<S, T> extends IterableBase<T> {
424 final Iterable<S> _iterable; 414 final Iterable<S> _iterable;
425 // TODO(ahe): Restore type when feature is implemented in dart2js 415 final _ExpandFunction _f;
426 // checked mode. http://dartbug.com/7733
427 final /* _ExpandFunction */ _f;
428 416
429 ExpandIterable(this._iterable, Iterable<T> this._f(S element)); 417 ExpandIterable(this._iterable, Iterable<T> this._f(S element));
430 418
431 Iterator<T> get iterator => new ExpandIterator<S, T>(_iterable.iterator, _f); 419 Iterator<T> get iterator => new ExpandIterator<S, T>(_iterable.iterator, _f);
432 } 420 }
433 421
434 class ExpandIterator<S, T> implements Iterator<T> { 422 class ExpandIterator<S, T> implements Iterator<T> {
435 final Iterator<S> _iterator; 423 final Iterator<S> _iterator;
436 // TODO(ahe): Restore type when feature is implemented in dart2js 424 final _ExpandFunction _f;
437 // checked mode. http://dartbug.com/7733
438 final /* _ExpandFunction */ _f;
439 // Initialize _currentExpansion to an empty iterable. A null value 425 // Initialize _currentExpansion to an empty iterable. A null value
440 // marks the end of iteration, and we don't want to call _f before 426 // marks the end of iteration, and we don't want to call _f before
441 // the first moveNext call. 427 // the first moveNext call.
442 Iterator<T> _currentExpansion = const EmptyIterator(); 428 Iterator<T> _currentExpansion = const EmptyIterator();
443 T _current; 429 T _current;
444 430
445 ExpandIterator(this._iterator, Iterable<T> this._f(S element)); 431 ExpandIterator(this._iterator, Iterable<T> this._f(S element));
446 432
447 void _nextExpansion() { 433 void _nextExpansion() {
448 } 434 }
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
500 } 486 }
501 487
502 E get current { 488 E get current {
503 if (_remaining < 0) return null; 489 if (_remaining < 0) return null;
504 return _iterator.current; 490 return _iterator.current;
505 } 491 }
506 } 492 }
507 493
508 class TakeWhileIterable<E> extends IterableBase<E> { 494 class TakeWhileIterable<E> extends IterableBase<E> {
509 final Iterable<E> _iterable; 495 final Iterable<E> _iterable;
510 // TODO(ahe): Restore type when feature is implemented in dart2js 496 final _ElementPredicate _f;
511 // checked mode. http://dartbug.com/7733
512 final /* _ElementPredicate */ _f;
513 497
514 TakeWhileIterable(this._iterable, bool this._f(E element)); 498 TakeWhileIterable(this._iterable, bool this._f(E element));
515 499
516 Iterator<E> get iterator { 500 Iterator<E> get iterator {
517 return new TakeWhileIterator<E>(_iterable.iterator, _f); 501 return new TakeWhileIterator<E>(_iterable.iterator, _f);
518 } 502 }
519 } 503 }
520 504
521 class TakeWhileIterator<E> extends Iterator<E> { 505 class TakeWhileIterator<E> extends Iterator<E> {
522 final Iterator<E> _iterator; 506 final Iterator<E> _iterator;
523 // TODO(ahe): Restore type when feature is implemented in dart2js 507 final _ElementPredicate _f;
524 // checked mode. http://dartbug.com/7733
525 final /* _ElementPredicate */ _f;
526 bool _isFinished = false; 508 bool _isFinished = false;
527 509
528 TakeWhileIterator(this._iterator, bool this._f(E element)); 510 TakeWhileIterator(this._iterator, bool this._f(E element));
529 511
530 bool moveNext() { 512 bool moveNext() {
531 if (_isFinished) return false; 513 if (_isFinished) return false;
532 if (!_iterator.moveNext() || !_f(_iterator.current)) { 514 if (!_iterator.moveNext() || !_f(_iterator.current)) {
533 _isFinished = true; 515 _isFinished = true;
534 return false; 516 return false;
535 } 517 }
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
576 for (int i = 0; i < _skipCount; i++) _iterator.moveNext(); 558 for (int i = 0; i < _skipCount; i++) _iterator.moveNext();
577 _skipCount = 0; 559 _skipCount = 0;
578 return _iterator.moveNext(); 560 return _iterator.moveNext();
579 } 561 }
580 562
581 E get current => _iterator.current; 563 E get current => _iterator.current;
582 } 564 }
583 565
584 class SkipWhileIterable<E> extends IterableBase<E> { 566 class SkipWhileIterable<E> extends IterableBase<E> {
585 final Iterable<E> _iterable; 567 final Iterable<E> _iterable;
586 // TODO(ahe): Restore type when feature is implemented in dart2js 568 final _ElementPredicate _f;
587 // checked mode. http://dartbug.com/7733
588 final /* _ElementPredicate */ _f;
589 569
590 SkipWhileIterable(this._iterable, bool this._f(E element)); 570 SkipWhileIterable(this._iterable, bool this._f(E element));
591 571
592 Iterator<E> get iterator { 572 Iterator<E> get iterator {
593 return new SkipWhileIterator<E>(_iterable.iterator, _f); 573 return new SkipWhileIterator<E>(_iterable.iterator, _f);
594 } 574 }
595 } 575 }
596 576
597 class SkipWhileIterator<E> extends Iterator<E> { 577 class SkipWhileIterator<E> extends Iterator<E> {
598 final Iterator<E> _iterator; 578 final Iterator<E> _iterator;
599 // TODO(ahe): Restore type when feature is implemented in dart2js 579 final _ElementPredicate _f;
600 // checked mode. http://dartbug.com/7733
601 final /* _ElementPredicate */ _f;
602 bool _hasSkipped = false; 580 bool _hasSkipped = false;
603 581
604 SkipWhileIterator(this._iterator, bool this._f(E element)); 582 SkipWhileIterator(this._iterator, bool this._f(E element));
605 583
606 bool moveNext() { 584 bool moveNext() {
607 if (!_hasSkipped) { 585 if (!_hasSkipped) {
608 _hasSkipped = true; 586 _hasSkipped = true;
609 while (_iterator.moveNext()) { 587 while (_iterator.moveNext()) {
610 if (!_f(_iterator.current)) return true; 588 if (!_f(_iterator.current)) return true;
611 } 589 }
(...skipping 460 matching lines...) Expand 10 before | Expand all | Expand 10 after
1072 1050
1073 static Set setDifference(Set set, Set other, Set result) { 1051 static Set setDifference(Set set, Set other, Set result) {
1074 for (var element in set) { 1052 for (var element in set) {
1075 if (!other.contains(element)) { 1053 if (!other.contains(element)) {
1076 result.add(element); 1054 result.add(element);
1077 } 1055 }
1078 } 1056 }
1079 return result; 1057 return result;
1080 } 1058 }
1081 } 1059 }
OLDNEW
« no previous file with comments | « no previous file | sdk/lib/_internal/compiler/implementation/js_backend/emitter.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698