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

Side by Side Diff: test/generated_sdk/lib/core/iterable.dart

Issue 1162723007: remove generated_sdk from checked in code (Closed) Base URL: git@github.com:dart-lang/dev_compiler.git@master
Patch Set: Created 5 years, 6 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
« no previous file with comments | « test/generated_sdk/lib/core/invocation.dart ('k') | test/generated_sdk/lib/core/iterator.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.core;
6
7 /**
8 * An object that uses an [Iterator] to serve objects one at a time.
9 *
10 * You can iterate over all objects served by an Iterable object
11 * using the for-in loop construct.
12 * For example, you can iterate over all of the keys in a [Map],
13 * because Map keys are iterable.
14 *
15 * Map kidsBooks = {'Matilda': 'Roald Dahl',
16 * 'Green Eggs and Ham': 'Dr Seuss',
17 * 'Where the Wild Things Are': 'Maurice Sendak'};
18 * for (var book in kidsBooks.keys) {
19 * print('$book was written by ${kidsBooks[book]}');
20 * }
21 *
22 * The [List] class and the [Set] class implement this interface,
23 * as do classes in the [dart:collection](#dart-collection) library.
24 *
25 * You can implement Iterable in your own class.
26 * If you do, then an instance of your Iterable class
27 * can be the right-hand side of a for-in construct.
28 *
29 * Some subclasss of [Iterable] can be modified. It is generally not allowed
30 * to modify such collections while they are being iterated. Doing so will break
31 * the iteration, which is typically signalled by throwing a
32 * [ConcurrentModificationError] when it is detected.
33 */
34 @SupportJsExtensionMethods()
35 abstract class Iterable<E> {
36 const Iterable();
37
38 /**
39 * Creates an Iterable that generates its elements dynamically.
40 *
41 * The Iterators created by the Iterable count from
42 * zero to [:count - 1:] while iterating, and call [generator]
43 * with that index to create the next value.
44 *
45 * If [generator] is omitted, it defaults to an identity function
46 * on integers `(int x) => x`, so it should only be omitted if the type
47 * parameter allows integer values.
48 *
49 * As an Iterable, [:new Iterable.generate(n, generator)):] is equivalent to
50 * [:const [0, ..., n - 1].map(generator):]
51 */
52 factory Iterable.generate(int count, [E generator(int index)]) {
53 if (count <= 0) return new EmptyIterable<E>();
54 return new _GeneratorIterable<E>(count, generator);
55 }
56
57 /**
58 * Returns a new `Iterator` that allows iterating the elements of this
59 * `Iterable`.
60 *
61 * Modifying the underlying data after creating the new iterator
62 * may cause an error the next time [Iterator.moveNext] is called.
63 */
64 Iterator<E> get iterator;
65
66 /**
67 * Returns a new lazy [Iterable] with elements that are created by
68 * calling `f` on the elements of this `Iterable`.
69 *
70 * This method returns a view of the mapped elements. As long as the
71 * returned [Iterable] is not iterated over, the supplied function [f] will
72 * not be invoked. The transformed elements will not be cached. Iterating
73 * multiple times over the the returned [Iterable] will invoke the supplied
74 * function [f] multiple times on the same element.
75 */
76 Iterable map(f(E element));
77
78 /**
79 * Returns a new lazy [Iterable] with all elements that satisfy the
80 * predicate [test].
81 *
82 * This method returns a view of the mapped elements. As long as the
83 * returned [Iterable] is not iterated over, the supplied function [test] will
84 * not be invoked. Iterating will not cache results, and thus iterating
85 * multiple times over the returned [Iterable] will invoke the supplied
86 * function [test] multiple times on the same element.
87 */
88 Iterable<E> where(bool test(E element));
89
90 /**
91 * Expands each element of this [Iterable] into zero or more elements.
92 *
93 * The resulting Iterable runs through the elements returned
94 * by [f] for each element of this, in order.
95 *
96 * The returned [Iterable] is lazy, and calls [f] for each element
97 * of this every time it's iterated.
98 */
99 Iterable expand(Iterable f(E element));
100
101 /**
102 * Returns true if the collection contains an element equal to [element].
103 *
104 * The equality used to determine whether [element] is equal to an element of
105 * the iterable, depends on the type of iterable.
106 * For example, a [Set] may have a custom equality
107 * (see, e.g., [Set.identical]) that its `contains` uses.
108 * Likewise the `Iterable` returned by a [Map.keys] call
109 * will likely use the same equality that the `Map` uses for keys.
110 */
111 bool contains(Object element);
112
113 /**
114 * Applies the function [f] to each element of this collection.
115 */
116 void forEach(void f(E element));
117
118 /**
119 * Reduces a collection to a single value by iteratively combining elements
120 * of the collection using the provided function.
121 *
122 * Example of calculating the sum of an iterable:
123 *
124 * iterable.reduce((value, element) => value + element);
125 *
126 */
127 E reduce(E combine(E value, E element));
128
129 /**
130 * Reduces a collection to a single value by iteratively combining each
131 * element of the collection with an existing value using the provided
132 * function.
133 *
134 * Use [initialValue] as the initial value, and the function [combine] to
135 * create a new value from the previous one and an element.
136 *
137 * Example of calculating the sum of an iterable:
138 *
139 * iterable.fold(0, (prev, element) => prev + element);
140 *
141 */
142 dynamic fold(var initialValue,
143 dynamic combine(var previousValue, E element));
144
145 /**
146 * Returns true if every elements of this collection satisify the
147 * predicate [test]. Returns `false` otherwise.
148 */
149 bool every(bool test(E element));
150
151 /**
152 * Converts each element to a [String] and concatenates the strings.
153 *
154 * Converts each element to a [String] by calling [Object.toString] on it.
155 * Then concatenates the strings, optionally separated by the [separator]
156 * string.
157 */
158 String join([String separator = ""]) {
159 StringBuffer buffer = new StringBuffer();
160 buffer.writeAll(this, separator);
161 return buffer.toString();
162 }
163
164 /**
165 * Returns true if one element of this collection satisfies the
166 * predicate [test]. Returns false otherwise.
167 */
168 bool any(bool test(E element));
169
170 /**
171 * Creates a [List] containing the elements of this [Iterable].
172 *
173 * The elements are in iteration order. The list is fixed-length
174 * if [growable] is false.
175 */
176 List<E> toList({ bool growable: true });
177
178 /**
179 * Creates a [Set] containing the same elements as this iterable.
180 *
181 * The set may contain fewer elements than the iterable,
182 * if the iterable contains the an element more than once,
183 * or it contains one or more elements that are equal.
184 * The order of the elements in the set is not guaranteed to be the same
185 * as for the iterable.
186 */
187 Set<E> toSet();
188
189 /**
190 * Returns the number of elements in [this].
191 *
192 * Counting all elements may be involve running through all elements and can
193 * therefore be slow.
194 */
195 int get length;
196
197 /**
198 * Returns true if there is no element in this collection.
199 */
200 bool get isEmpty;
201
202 /**
203 * Returns true if there is at least one element in this collection.
204 */
205 bool get isNotEmpty;
206
207 /**
208 * Returns an [Iterable] with at most [count] elements.
209 *
210 * The returned `Iterable` may contain fewer than `count` elements, if `this`
211 * contains fewer than `count` elements.
212 *
213 * It is an error if `count` is negative.
214 */
215 Iterable<E> take(int count);
216
217 /**
218 * Returns an Iterable that stops once [test] is not satisfied anymore.
219 *
220 * The filtering happens lazily. Every new Iterator of the returned
221 * Iterable starts iterating over the elements of `this`.
222 *
223 * When the iterator encounters an element `e` that does not satisfy [test],
224 * it discards `e` and moves into the finished state. That is, it does not
225 * get or provide any more elements.
226 */
227 Iterable<E> takeWhile(bool test(E value));
228
229 /**
230 * Returns an Iterable that skips the first [count] elements.
231 *
232 * If `this` has fewer than `count` elements, then the resulting Iterable is
233 * empty.
234 *
235 * It is an error if `count` is negative.
236 */
237 Iterable<E> skip(int count);
238
239 /**
240 * Returns an Iterable that skips elements while [test] is satisfied.
241 *
242 * The filtering happens lazily. Every new Iterator of the returned
243 * Iterable iterates over all elements of `this`.
244 *
245 * As long as the iterator's elements satisfy [test] they are
246 * discarded. Once an element does not satisfy the [test] the iterator stops
247 * testing and uses every later element unconditionally. That is, the elements
248 * of the returned Iterable are the elements of `this` starting from the
249 * first element that does not satisfy [test].
250 */
251 Iterable<E> skipWhile(bool test(E value));
252
253 /**
254 * Returns the first element.
255 *
256 * If `this` is empty throws a [StateError]. Otherwise this method is
257 * equivalent to [:this.elementAt(0):]
258 */
259 E get first;
260
261 /**
262 * Returns the last element.
263 *
264 * If `this` is empty throws a [StateError].
265 */
266 E get last;
267
268 /**
269 * Returns the single element in `this`.
270 *
271 * If `this` is empty or has more than one element throws a [StateError].
272 */
273 E get single;
274
275 /**
276 * Returns the first element that satisfies the given predicate [test].
277 *
278 * If none matches, the result of invoking the [orElse] function is
279 * returned. By default, when [orElse] is `null`, a [StateError] is
280 * thrown.
281 */
282 E firstWhere(bool test(E element), { E orElse() });
283
284 /**
285 * Returns the last element that satisfies the given predicate [test].
286 *
287 * If none matches, the result of invoking the [orElse] function is
288 * returned. By default, when [orElse] is `null`, a [StateError] is
289 * thrown.
290 */
291 E lastWhere(bool test(E element), {E orElse()});
292
293 /**
294 * Returns the single element that satisfies [test]. If no or more than one
295 * element match then a [StateError] is thrown.
296 */
297 E singleWhere(bool test(E element));
298
299 /**
300 * Returns the [index]th element.
301 *
302 * The [index] must be non-negative and less than [length].
303 *
304 * Note: if `this` does not have a deterministic iteration order then the
305 * function may simply return any element without any iteration if there are
306 * at least [index] elements in `this`.
307 */
308 E elementAt(int index);
309 }
310
311 typedef E _Generator<E>(int index);
312
313 class _GeneratorIterable<E> extends IterableBase<E>
314 implements EfficientLength {
315 final int _start;
316 final int _end;
317 final _Generator<E> _generator;
318 _GeneratorIterable(this._end, E generator(int n))
319 : _start = 0,
320 _generator = (generator != null) ? generator : _id;
321
322 _GeneratorIterable.slice(this._start, this._end, this._generator);
323
324 Iterator<E> get iterator =>
325 new _GeneratorIterator<E>(_start, _end, _generator);
326 int get length => _end - _start;
327
328 Iterable<E> skip(int count) {
329 RangeError.checkNotNegative(count, "count");
330 if (count == 0) return this;
331 int newStart = _start + count;
332 if (newStart >= _end) return new EmptyIterable<E>();
333 return new _GeneratorIterable<E>.slice(newStart, _end, _generator);
334 }
335
336 Iterable<E> take(int count) {
337 RangeError.checkNotNegative(count, "count");
338 if (count == 0) return new EmptyIterable<E>();
339 int newEnd = _start + count;
340 if (newEnd >= _end) return this;
341 return new _GeneratorIterable<E>.slice(_start, newEnd, _generator);
342 }
343
344 static int _id(int n) => n;
345 }
346
347 class _GeneratorIterator<E> implements Iterator<E> {
348 final int _end;
349 final _Generator<E> _generator;
350 int _index;
351 E _current;
352
353 _GeneratorIterator(this._index, this._end, this._generator);
354
355 bool moveNext() {
356 if (_index < _end) {
357 _current = _generator(_index);
358 _index++;
359 return true;
360 } else {
361 _current = null;
362 return false;
363 }
364 }
365
366 E get current => _current;
367 }
368
369 /**
370 * An Iterator that allows moving backwards as well as forwards.
371 */
372 abstract class BidirectionalIterator<E> implements Iterator<E> {
373 /**
374 * Move back to the previous element.
375 *
376 * Returns true and updates [current] if successful. Returns false
377 * and sets [current] to null if there is no previous element.
378 */
379 bool movePrevious();
380 }
OLDNEW
« no previous file with comments | « test/generated_sdk/lib/core/invocation.dart ('k') | test/generated_sdk/lib/core/iterator.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698