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

Side by Side Diff: sdk/lib/core/sequences.dart

Issue 11410086: Use iterator, moveNext(), current. (Closed) Base URL: https://dart.googlecode.com/svn/experimental/lib_v2/dart
Patch Set: Address comments. Created 8 years, 1 month 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) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, 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 /** 5 /**
6 * An indexed sequence of elements of the same type. 6 * An indexed sequence of elements of the same type.
7 * 7 *
8 * This is a primitive interface that any finite integer-indexable 8 * This is a primitive interface that any finite integer-indexable
9 * sequence can implement. 9 * sequence can implement.
10 * It is intended for data structures where access by index is 10 * It is intended for data structures where access by index is
(...skipping 15 matching lines...) Expand all
26 */ 26 */
27 T operator[](int index); 27 T operator[](int index);
28 } 28 }
29 29
30 /** 30 /**
31 * A skeleton class for a [Collection] that is also a [Sequence]. 31 * A skeleton class for a [Collection] that is also a [Sequence].
32 */ 32 */
33 abstract class SequenceCollection<E> implements Collection<E>, Sequence<E> { 33 abstract class SequenceCollection<E> implements Collection<E>, Sequence<E> {
34 // The class is intended for use as a mixin as well. 34 // The class is intended for use as a mixin as well.
35 35
36 Iterator<E> iterator() => new SequenceIterator(sequence); 36 Iterator<E> get iterator => new SequenceIterator(sequence);
37 37
38 void forEach(f(E element)) { 38 void forEach(f(E element)) {
39 for (int i = 0; i < this.length; i++) f(this[i]); 39 for (int i = 0; i < this.length; i++) f(this[i]);
40 } 40 }
41 41
42 Collection mappedBy(f(E element)) { 42 Collection mappedBy(f(E element)) {
43 List result = new List(); 43 List result = new List();
44 for (int i = 0; i < this.length; i++) { 44 for (int i = 0; i < this.length; i++) {
45 result.add(f(this[i])); 45 result.add(f(this[i]));
46 } 46 }
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
187 "Cannot insert range in an unmodifiable list"); 187 "Cannot insert range in an unmodifiable list");
188 } 188 }
189 } 189 }
190 190
191 /** 191 /**
192 * Iterates over a [Sequence] in growing index order. 192 * Iterates over a [Sequence] in growing index order.
193 */ 193 */
194 class SequenceIterator<E> implements Iterator<E> { 194 class SequenceIterator<E> implements Iterator<E> {
195 Sequence<E> _sequence; 195 Sequence<E> _sequence;
196 int _position; 196 int _position;
197 SequenceIterator(this._sequence) : _position = 0; 197 SequenceIterator(this._sequence) : _position = -1;
198 bool get hasNext => _position < _sequence.length; 198 bool moveNext() {
199 E next() { 199 _position++;
200 if (hasNext) return _sequence[_position++]; 200 if (_position < _sequence.length) return true;
201 _position = _sequence.length;
202 return false;
203 }
204 E get current {
205 if (0 <= _position && _position < _sequence.length) {
206 return _sequence[_position];
207 }
208 // TODO(floitsch): adapt error-message.
201 throw new StateError("No more elements"); 209 throw new StateError("No more elements");
202 } 210 }
203 } 211 }
204 212
OLDNEW
« runtime/vm/intrinsifier.h ('K') | « sdk/lib/core/queue.dart ('k') | sdk/lib/core/set.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698