| OLD | NEW |
| (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 class MappedIterator<S, T> extends Iterator<T> { | |
| 25 T _current; | |
| 26 final Iterator<S> _iterator; | |
| 27 // TODO(ahe): Restore type when feature is implemented in dart2js | |
| 28 // checked mode. http://dartbug.com/7733 | |
| 29 final /* _Transformation<S, T> */ _f; | |
| 30 | |
| 31 MappedIterator(this._iterator, T this._f(S element)); | |
| 32 | |
| 33 bool moveNext() { | |
| 34 if (_iterator.moveNext()) { | |
| 35 _current = _f(_iterator.current); | |
| 36 return true; | |
| 37 } else { | |
| 38 _current = null; | |
| 39 return false; | |
| 40 } | |
| 41 } | |
| 42 | |
| 43 T get current => _current; | |
| 44 } | |
| 45 | |
| 46 typedef bool _ElementPredicate<E>(E element); | |
| 47 | |
| 48 class WhereIterable<E> extends Iterable<E> { | |
| 49 final Iterable<E> _iterable; | |
| 50 // TODO(ahe): Restore type when feature is implemented in dart2js | |
| 51 // checked mode. http://dartbug.com/7733 | |
| 52 final /* _ElementPredicate */ _f; | |
| 53 | |
| 54 WhereIterable(this._iterable, bool this._f(E element)); | |
| 55 | |
| 56 Iterator<E> get iterator => new WhereIterator<E>(_iterable.iterator, _f); | |
| 57 } | |
| 58 | |
| 59 class WhereIterator<E> extends Iterator<E> { | |
| 60 final Iterator<E> _iterator; | |
| 61 // TODO(ahe): Restore type when feature is implemented in dart2js | |
| 62 // checked mode. http://dartbug.com/7733 | |
| 63 final /* _ElementPredicate */ _f; | |
| 64 | |
| 65 WhereIterator(this._iterator, bool this._f(E element)); | |
| 66 | |
| 67 bool moveNext() { | |
| 68 while (_iterator.moveNext()) { | |
| 69 if (_f(_iterator.current)) { | |
| 70 return true; | |
| 71 } | |
| 72 } | |
| 73 return false; | |
| 74 } | |
| 75 | |
| 76 E get current => _iterator.current; | |
| 77 } | |
| 78 | |
| 79 class TakeIterable<E> extends Iterable<E> { | |
| 80 final Iterable<E> _iterable; | |
| 81 final int _takeCount; | |
| 82 | |
| 83 TakeIterable(this._iterable, this._takeCount) { | |
| 84 if (_takeCount is! int || _takeCount < 0) { | |
| 85 throw new ArgumentError(_takeCount); | |
| 86 } | |
| 87 } | |
| 88 | |
| 89 Iterator<E> get iterator { | |
| 90 return new TakeIterator<E>(_iterable.iterator, _takeCount); | |
| 91 } | |
| 92 } | |
| 93 | |
| 94 class TakeIterator<E> extends Iterator<E> { | |
| 95 final Iterator<E> _iterator; | |
| 96 int _remaining; | |
| 97 | |
| 98 TakeIterator(this._iterator, this._remaining) { | |
| 99 assert(_remaining is int && _remaining >= 0); | |
| 100 } | |
| 101 | |
| 102 bool moveNext() { | |
| 103 _remaining--; | |
| 104 if (_remaining >= 0) { | |
| 105 return _iterator.moveNext(); | |
| 106 } | |
| 107 _remaining = -1; | |
| 108 return false; | |
| 109 } | |
| 110 | |
| 111 E get current { | |
| 112 if (_remaining < 0) return null; | |
| 113 return _iterator.current; | |
| 114 } | |
| 115 } | |
| 116 | |
| 117 class TakeWhileIterable<E> extends Iterable<E> { | |
| 118 final Iterable<E> _iterable; | |
| 119 // TODO(ahe): Restore type when feature is implemented in dart2js | |
| 120 // checked mode. http://dartbug.com/7733 | |
| 121 final /* _ElementPredicate */ _f; | |
| 122 | |
| 123 TakeWhileIterable(this._iterable, bool this._f(E element)); | |
| 124 | |
| 125 Iterator<E> get iterator { | |
| 126 return new TakeWhileIterator<E>(_iterable.iterator, _f); | |
| 127 } | |
| 128 } | |
| 129 | |
| 130 class TakeWhileIterator<E> extends Iterator<E> { | |
| 131 final Iterator<E> _iterator; | |
| 132 // TODO(ahe): Restore type when feature is implemented in dart2js | |
| 133 // checked mode. http://dartbug.com/7733 | |
| 134 final /* _ElementPredicate */ _f; | |
| 135 bool _isFinished = false; | |
| 136 | |
| 137 TakeWhileIterator(this._iterator, bool this._f(E element)); | |
| 138 | |
| 139 bool moveNext() { | |
| 140 if (_isFinished) return false; | |
| 141 if (!_iterator.moveNext() || !_f(_iterator.current)) { | |
| 142 _isFinished = true; | |
| 143 return false; | |
| 144 } | |
| 145 return true; | |
| 146 } | |
| 147 | |
| 148 E get current { | |
| 149 if (_isFinished) return null; | |
| 150 return _iterator.current; | |
| 151 } | |
| 152 } | |
| 153 | |
| 154 class SkipIterable<E> extends Iterable<E> { | |
| 155 final Iterable<E> _iterable; | |
| 156 final int _skipCount; | |
| 157 | |
| 158 SkipIterable(this._iterable, this._skipCount) { | |
| 159 if (_skipCount is! int || _skipCount < 0) { | |
| 160 throw new ArgumentError(_skipCount); | |
| 161 } | |
| 162 } | |
| 163 | |
| 164 Iterable<E> skip(int n) { | |
| 165 if (n is! int || n < 0) { | |
| 166 throw new ArgumentError(n); | |
| 167 } | |
| 168 return new SkipIterable<E>(_iterable, _skipCount + n); | |
| 169 } | |
| 170 | |
| 171 Iterator<E> get iterator { | |
| 172 return new SkipIterator<E>(_iterable.iterator, _skipCount); | |
| 173 } | |
| 174 } | |
| 175 | |
| 176 class SkipIterator<E> extends Iterator<E> { | |
| 177 final Iterator<E> _iterator; | |
| 178 int _skipCount; | |
| 179 | |
| 180 SkipIterator(this._iterator, this._skipCount) { | |
| 181 assert(_skipCount is int && _skipCount >= 0); | |
| 182 } | |
| 183 | |
| 184 bool moveNext() { | |
| 185 for (int i = 0; i < _skipCount; i++) _iterator.moveNext(); | |
| 186 _skipCount = 0; | |
| 187 return _iterator.moveNext(); | |
| 188 } | |
| 189 | |
| 190 E get current => _iterator.current; | |
| 191 } | |
| 192 | |
| 193 class SkipWhileIterable<E> extends Iterable<E> { | |
| 194 final Iterable<E> _iterable; | |
| 195 // TODO(ahe): Restore type when feature is implemented in dart2js | |
| 196 // checked mode. http://dartbug.com/7733 | |
| 197 final /* _ElementPredicate */ _f; | |
| 198 | |
| 199 SkipWhileIterable(this._iterable, bool this._f(E element)); | |
| 200 | |
| 201 Iterator<E> get iterator { | |
| 202 return new SkipWhileIterator<E>(_iterable.iterator, _f); | |
| 203 } | |
| 204 } | |
| 205 | |
| 206 class SkipWhileIterator<E> extends Iterator<E> { | |
| 207 final Iterator<E> _iterator; | |
| 208 // TODO(ahe): Restore type when feature is implemented in dart2js | |
| 209 // checked mode. http://dartbug.com/7733 | |
| 210 final /* _ElementPredicate */ _f; | |
| 211 bool _hasSkipped = false; | |
| 212 | |
| 213 SkipWhileIterator(this._iterator, bool this._f(E element)); | |
| 214 | |
| 215 bool moveNext() { | |
| 216 if (!_hasSkipped) { | |
| 217 _hasSkipped = true; | |
| 218 while (_iterator.moveNext()) { | |
| 219 if (!_f(_iterator.current)) return true; | |
| 220 } | |
| 221 } | |
| 222 return _iterator.moveNext(); | |
| 223 } | |
| 224 | |
| 225 E get current => _iterator.current; | |
| 226 } | |
| OLD | NEW |