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

Side by Side Diff: quiver/lib/src/iterables/enumerate.dart

Issue 1400473008: Roll Observatory packages and add a roll script (Closed) Base URL: git@github.com:dart-lang/observatory_pub_packages.git@master
Patch Set: Created 5 years, 2 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 | « quiver/lib/src/iterables/cycle.dart ('k') | quiver/lib/src/iterables/generating_iterable.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 2013 Google Inc. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 part of quiver.iterables;
16
17 /**
18 * Returns an [Iterable] of [IndexedValue]s where the nth value holds the nth
19 * element of [iterable] and its index.
20 */
21 Iterable<IndexedValue> enumerate(Iterable iterable) =>
22 new EnumerateIterable(iterable);
23
24 class IndexedValue<V> {
25 final int index;
26 final V value;
27
28 IndexedValue(this.index, this.value);
29
30 operator ==(o) => o is IndexedValue && o.index == index && o.value == value;
31 int get hashCode => index * 31 + value.hashCode;
32 String toString() => '($index, $value)';
33 }
34
35 /**
36 * An [Iterable] of [IndexedValue]s where the nth value holds the nth
37 * element of [iterable] and its index. See [enumerate].
38 */
39 // This was inspired by MappedIterable internal to Dart collections.
40 class EnumerateIterable<V> extends IterableBase<IndexedValue<V>> {
41 final Iterable<V> _iterable;
42
43 EnumerateIterable(this._iterable);
44
45 Iterator<IndexedValue<V>> get iterator =>
46 new EnumerateIterator<V>(_iterable.iterator);
47
48 // Length related functions are independent of the mapping.
49 int get length => _iterable.length;
50 bool get isEmpty => _iterable.isEmpty;
51
52 // Index based lookup can be done before transforming.
53 IndexedValue<V> get first => new IndexedValue<V>(0, _iterable.first);
54 IndexedValue<V> get last => new IndexedValue<V>(length - 1, _iterable.last);
55 IndexedValue<V> get single => new IndexedValue<V>(0, _iterable.single);
56 IndexedValue<V> elementAt(int index) =>
57 new IndexedValue<V>(index, _iterable.elementAt(index));
58 }
59
60 /** The [Iterator] returned by [EnumerateIterable.iterator]. */
61 class EnumerateIterator<V> extends Iterator<IndexedValue<V>> {
62 final Iterator<V> _iterator;
63 int _index = 0;
64 IndexedValue<V> _current;
65
66 EnumerateIterator(this._iterator);
67
68 IndexedValue<V> get current => _current;
69
70 bool moveNext() {
71 if (_iterator.moveNext()) {
72 _current = new IndexedValue(_index++, _iterator.current);
73 return true;
74 }
75 _current = null;
76 return false;
77 }
78 }
OLDNEW
« no previous file with comments | « quiver/lib/src/iterables/cycle.dart ('k') | quiver/lib/src/iterables/generating_iterable.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698