| OLD | NEW |
| 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 typedef T _Transformation<S, T>(S value); | 7 typedef T _Transformation<S, T>(S value); |
| 8 | 8 |
| 9 class MappedIterable<S, T> extends Iterable<T> { | 9 class MappedIterable<S, T> extends Iterable<T> { |
| 10 final Iterable<S> _iterable; | 10 final Iterable<S> _iterable; |
| 11 // TODO(ahe): Restore type when feature is implemented in dart2js | 11 // TODO(ahe): Restore type when feature is implemented in dart2js |
| 12 // checked mode. http://dartbug.com/7733 | 12 // checked mode. http://dartbug.com/7733 |
| 13 final /* _Transformation<S, T> */ _f; | 13 final /* _Transformation<S, T> */ _f; |
| 14 | 14 |
| 15 MappedIterable(this._iterable, T this._f(S element)); | 15 MappedIterable(this._iterable, T this._f(S element)); |
| 16 | 16 |
| 17 Iterator<T> get iterator => new MappedIterator<S, T>(_iterable.iterator, _f); | 17 Iterator<T> get iterator => new MappedIterator<S, T>(_iterable.iterator, _f); |
| 18 | 18 |
| 19 // Length related functions are independent of the mapping. | 19 // Length related functions are independent of the mapping. |
| 20 int get length => _iterable.length; | 20 int get length => _iterable.length; |
| 21 bool get isEmpty => _iterable.isEmpty; | 21 bool get isEmpty => _iterable.isEmpty; |
| 22 } | 22 } |
| 23 | 23 |
| 24 |
| 24 class MappedIterator<S, T> extends Iterator<T> { | 25 class MappedIterator<S, T> extends Iterator<T> { |
| 25 T _current; | 26 T _current; |
| 26 final Iterator<S> _iterator; | 27 final Iterator<S> _iterator; |
| 27 // TODO(ahe): Restore type when feature is implemented in dart2js | 28 // TODO(ahe): Restore type when feature is implemented in dart2js |
| 28 // checked mode. http://dartbug.com/7733 | 29 // checked mode. http://dartbug.com/7733 |
| 29 final /* _Transformation<S, T> */ _f; | 30 final /* _Transformation<S, T> */ _f; |
| 30 | 31 |
| 31 MappedIterator(this._iterator, T this._f(S element)); | 32 MappedIterator(this._iterator, T this._f(S element)); |
| 32 | 33 |
| 33 bool moveNext() { | 34 bool moveNext() { |
| 34 if (_iterator.moveNext()) { | 35 if (_iterator.moveNext()) { |
| 35 _current = _f(_iterator.current); | 36 _current = _f(_iterator.current); |
| 36 return true; | 37 return true; |
| 37 } else { | 38 } else { |
| 38 _current = null; | 39 _current = null; |
| 39 return false; | 40 return false; |
| 40 } | 41 } |
| 41 } | 42 } |
| 42 | 43 |
| 43 T get current => _current; | 44 T get current => _current; |
| 44 } | 45 } |
| 45 | 46 |
| 47 /** Specialized alternative to [MappedIterable] for mapped [List]s. */ |
| 48 class MappedListIterable<S, T> extends Iterable<T> { |
| 49 final Iterable<S> _list; |
| 50 /** |
| 51 * Start index of the part of the list to map. |
| 52 * |
| 53 * Allows mapping only a sub-list of an existing list. |
| 54 * |
| 55 * Used to implement lazy skip/take on a [MappedListIterable]. |
| 56 */ |
| 57 final int _start; |
| 58 |
| 59 /** |
| 60 * End index of the part of the list to map. |
| 61 * |
| 62 * If null, always use the length of the list. |
| 63 */ |
| 64 final int _end; |
| 65 |
| 66 // TODO(ahe): Restore type when feature is implemented in dart2js |
| 67 // checked mode. http://dartbug.com/7733 |
| 68 final /* _Transformation<S, T> */ _f; |
| 69 |
| 70 MappedListIterable(this._list, T this._f(S element), this._start, this._end) { |
| 71 if (_end != null && _end < _start) { |
| 72 throw new ArgumentError("End ($end) before start ($start)"); |
| 73 } |
| 74 } |
| 75 |
| 76 /** The start index, limited to the current length of the list. */ |
| 77 int get _startIndex { |
| 78 if (_start <= _list.length) return _start; |
| 79 return _list.length; |
| 80 } |
| 81 |
| 82 /** The end index, if given, limited to the current length of the list. */ |
| 83 int get _endIndex { |
| 84 if (_end == null || _end > _list.length) return _list.length; |
| 85 return _end; |
| 86 } |
| 87 |
| 88 Iterator<T> get iterator => |
| 89 new MappedListIterator<S, T>(_list, _f, _startIndex, _endIndex); |
| 90 |
| 91 void forEach(void action(T element)) { |
| 92 int length = _list.length; |
| 93 for (int i = _startIndex, n = _endIndex; i < n; i++) { |
| 94 action(_f(_list[i])); |
| 95 if (_list.length != length) { |
| 96 throw new ConcurrentModificationError(_list); |
| 97 } |
| 98 } |
| 99 } |
| 100 |
| 101 bool get isEmpty => _startIndex == _endIndex; |
| 102 |
| 103 int get length => _endIndex - _startIndex; |
| 104 |
| 105 T get first { |
| 106 int start = _startIndex; |
| 107 if (start == _endIndex) { |
| 108 throw new StateError("No elements"); |
| 109 } |
| 110 return _f(_list.elementAt(start)); |
| 111 } |
| 112 |
| 113 T get last { |
| 114 int end = _endIndex; |
| 115 if (end == _startIndex) { |
| 116 throw new StateError("No elements"); |
| 117 } |
| 118 return _f(_list.elementAt(end - 1)); |
| 119 } |
| 120 |
| 121 T get single { |
| 122 int start = _startIndex; |
| 123 int end = _endIndex; |
| 124 if (start != end - 1) { |
| 125 if (start == end) { |
| 126 throw new StateError("No elements"); |
| 127 } |
| 128 throw new StateError("Too many elements"); |
| 129 } |
| 130 return _f(_list[start]); |
| 131 } |
| 132 |
| 133 T elementAt(int index) { |
| 134 index += _startIndex; |
| 135 if (index >= _endIndex) { |
| 136 throw new StateError("No matching element"); |
| 137 } |
| 138 return _f(_list.elementAt(index)); |
| 139 } |
| 140 |
| 141 bool contains(T element) { |
| 142 int length = _list.length; |
| 143 for (int i = _startIndex, n = _endIndex; i < n; i++) { |
| 144 if (_f(_list[i]) == element) { |
| 145 return true; |
| 146 } |
| 147 if (_list.length != length) { |
| 148 throw new ConcurrentModificationError(_list); |
| 149 } |
| 150 } |
| 151 return false; |
| 152 } |
| 153 |
| 154 bool every(bool test(T element)) { |
| 155 int length = _list.length; |
| 156 for (int i = _startIndex, n = _endIndex; i < n; i++) { |
| 157 if (!test(_f(_list[i]))) return false; |
| 158 if (_list.length != length) { |
| 159 throw new ConcurrentModificationError(_list); |
| 160 } |
| 161 } |
| 162 return true; |
| 163 } |
| 164 |
| 165 bool any(bool test(T element)) { |
| 166 int length = _list.length; |
| 167 for (int i = _startIndex, n = _endIndex; i < n; i++) { |
| 168 if (test(_f(_list[i]))) return true; |
| 169 if (_list.length != length) { |
| 170 throw new ConcurrentModificationError(_list); |
| 171 } |
| 172 } |
| 173 return false; |
| 174 } |
| 175 |
| 176 T firstMatching(bool test(T element), { T orElse() }) { |
| 177 int length = _list.length; |
| 178 for (int i = _startIndex, n = _endIndex; i < n; i++) { |
| 179 T value = _f(_list[i]); |
| 180 if (test(value)) return value; |
| 181 if (_list.length != length) { |
| 182 throw new ConcurrentModificationError(_list); |
| 183 } |
| 184 } |
| 185 if (orElse != null) return orElse(); |
| 186 throw new StateError("No matching element"); |
| 187 } |
| 188 |
| 189 T lastMatching(bool test(T element), { T orElse() }) { |
| 190 int length = _list.length; |
| 191 for (int i = _endIndex - 1, start = _startIndex; i >= start; i++) { |
| 192 T value = _f(_list[i]); |
| 193 if (test(value)) return value; |
| 194 if (_list.length != length) { |
| 195 throw new ConcurrentModificationError(_list); |
| 196 } |
| 197 } |
| 198 if (orElse != null) return orElse(); |
| 199 throw new StateError("No matching element"); |
| 200 } |
| 201 |
| 202 T singleMatching(bool test(T element)) { |
| 203 int length = _list.length; |
| 204 T match; |
| 205 bool matchFound = false; |
| 206 for (int i = _startIndex, n = _endIndex; i < n; i++) { |
| 207 T value = _f(_list[i]); |
| 208 if (test(value)) { |
| 209 if (matchFound) { |
| 210 throw new StateError("More than one matching element"); |
| 211 } |
| 212 matchFound = true; |
| 213 match = value; |
| 214 } |
| 215 if (_list.length != length) { |
| 216 throw new ConcurrentModificationError(_list); |
| 217 } |
| 218 } |
| 219 if (matchFound) return match; |
| 220 throw new StateError("No matching element"); |
| 221 } |
| 222 |
| 223 int min([int compare(T a, T b)]) { |
| 224 if (compare == null) { |
| 225 var defaultCompare = Comparable.compare; |
| 226 compare = defaultCompare; |
| 227 } |
| 228 int length = _list.length; |
| 229 int start = _startIndex; |
| 230 int end = _endIndex; |
| 231 if (start == end) return null; |
| 232 int value = _f(_list[start]); |
| 233 if (_list.length != length) { |
| 234 throw new ConcurrentModificationError(_list); |
| 235 } |
| 236 for (int i = start + 1; i < end; i++) { |
| 237 int nextValue = _f(_list[i]); |
| 238 if (compare(value, nextValue) > 0) { |
| 239 value = nextValue; |
| 240 } |
| 241 if (_list.length != length) { |
| 242 throw new ConcurrentModificationError(_list); |
| 243 } |
| 244 } |
| 245 return value; |
| 246 } |
| 247 |
| 248 int max([int compare(T a, T b)]) { |
| 249 if (compare == null) { |
| 250 var defaultCompare = Comparable.compare; |
| 251 compare = defaultCompare; |
| 252 } |
| 253 int length = _list.length; |
| 254 int start = _startIndex; |
| 255 int end = _endIndex; |
| 256 if (start == end) return null; |
| 257 int value = _f(_list[start]); |
| 258 if (_list.length != length) { |
| 259 throw new ConcurrentModificationError(_list); |
| 260 } |
| 261 for (int i = start + 1; i < end; i++) { |
| 262 int nextValue = _f(_list[i]); |
| 263 if (compare(value, nextValue) < 0) { |
| 264 value = nextValue; |
| 265 } |
| 266 if (_list.length != length) { |
| 267 throw new ConcurrentModificationError(_list); |
| 268 } |
| 269 } |
| 270 return value; |
| 271 } |
| 272 |
| 273 String join([String separator]) { |
| 274 int start = _startIndex; |
| 275 int end = _endIndex; |
| 276 if (start == end) return ""; |
| 277 StringBuffer buffer = new StringBuffer("${_f(_list[start])}"); |
| 278 if (_list.length != length) { |
| 279 throw new ConcurrentModificationError(_list); |
| 280 } |
| 281 for (int i = start + 1; i < end; i++) { |
| 282 buffer.add(separator); |
| 283 buffer.add("${_f(_list[i])}"); |
| 284 if (_list.length != length) { |
| 285 throw new ConcurrentModificationError(_list); |
| 286 } |
| 287 } |
| 288 return buffer.toString(); |
| 289 } |
| 290 |
| 291 Iterable<T> where(bool test(T element)) => super.where(test); |
| 292 |
| 293 Iterable map(f(T element)) { |
| 294 return new MappedListIterable(_list, (S v) => f(_f(v)), _start, _end); |
| 295 } |
| 296 |
| 297 Iterable mappedBy(f(T element)) => map(f); |
| 298 |
| 299 reduce(var initialValue, combine(var previousValue, T element)) { |
| 300 return _list.reduce(initialValue, (v, S e) => combine(v, _f(e))); |
| 301 } |
| 302 |
| 303 Iterable<T> skip(int count) { |
| 304 int start = _startIndex + count; |
| 305 if (_end != null && start >= _end) { |
| 306 return new EmptyIterable<T>(); |
| 307 } |
| 308 return new MappedListIterable(_list, _f, start, _end); |
| 309 } |
| 310 |
| 311 Iterable<T> skipWhile(bool test(T element)) => super.skipWhile(test); |
| 312 |
| 313 Iterable<T> take(int count) { |
| 314 int newEnd = _start + count; |
| 315 if (_end == null || newEnd < _end) { |
| 316 return new MappedListIterable(_list, _f, _start, newEnd); |
| 317 } |
| 318 // Equivalent to "this". |
| 319 return new MappedListIterable(_list, _f, _start, _end); |
| 320 } |
| 321 |
| 322 Iterable<T> takeWhile(bool test(T element)) => super.takeWhile(test); |
| 323 |
| 324 List<T> toList() { |
| 325 List<T> result = new List<T>(); |
| 326 forEach(result.add); |
| 327 return result; |
| 328 } |
| 329 |
| 330 Set<T> toSet() { |
| 331 List result = new Set<T>(); |
| 332 forEach(result.add); |
| 333 return result; |
| 334 } |
| 335 } |
| 336 |
| 337 /** |
| 338 * Iterator for [MappedListIterable]. |
| 339 * |
| 340 * A list iterator that iterates over (a sublist of) a list and |
| 341 * returns the values transformed by a function. |
| 342 * |
| 343 * As a list iterator, it throws if the length of the list has |
| 344 * changed during iteration. |
| 345 */ |
| 346 class MappedListIterator<S, T> implements Iterator<T> { |
| 347 List<S> _list; |
| 348 // TODO(ahe): Restore type when feature is implemented in dart2js |
| 349 // checked mode. http://dartbug.com/7733 |
| 350 final /* _Transformation<S, T> */ _f; |
| 351 final int _endIndex; |
| 352 final int _length; |
| 353 int _index; |
| 354 T _current; |
| 355 |
| 356 MappedListIterator(List<S> list, this._f, int start, this._endIndex) |
| 357 : _list = list, _length = list.length, _index = start; |
| 358 |
| 359 T get current => _current; |
| 360 |
| 361 bool moveNext() { |
| 362 if (_list.length != _length) { |
| 363 throw new ConcurrentModificationError(_list); |
| 364 } |
| 365 if (_index >= _endIndex) { |
| 366 _current = null; |
| 367 return false; |
| 368 } |
| 369 _current = _f(_list[_index]); |
| 370 _index++; |
| 371 return true; |
| 372 } |
| 373 } |
| 374 |
| 46 typedef bool _ElementPredicate<E>(E element); | 375 typedef bool _ElementPredicate<E>(E element); |
| 47 | 376 |
| 48 class WhereIterable<E> extends Iterable<E> { | 377 class WhereIterable<E> extends Iterable<E> { |
| 49 final Iterable<E> _iterable; | 378 final Iterable<E> _iterable; |
| 50 // TODO(ahe): Restore type when feature is implemented in dart2js | 379 // TODO(ahe): Restore type when feature is implemented in dart2js |
| 51 // checked mode. http://dartbug.com/7733 | 380 // checked mode. http://dartbug.com/7733 |
| 52 final /* _ElementPredicate */ _f; | 381 final /* _ElementPredicate */ _f; |
| 53 | 382 |
| 54 WhereIterable(this._iterable, bool this._f(E element)); | 383 WhereIterable(this._iterable, bool this._f(E element)); |
| 55 | 384 |
| (...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 217 _hasSkipped = true; | 546 _hasSkipped = true; |
| 218 while (_iterator.moveNext()) { | 547 while (_iterator.moveNext()) { |
| 219 if (!_f(_iterator.current)) return true; | 548 if (!_f(_iterator.current)) return true; |
| 220 } | 549 } |
| 221 } | 550 } |
| 222 return _iterator.moveNext(); | 551 return _iterator.moveNext(); |
| 223 } | 552 } |
| 224 | 553 |
| 225 E get current => _iterator.current; | 554 E get current => _iterator.current; |
| 226 } | 555 } |
| 556 |
| 557 /** |
| 558 * The always empty [Iterable]. |
| 559 */ |
| 560 class EmptyIterable<E> extends Iterable<E> { |
| 561 const EmptyIterable(); |
| 562 |
| 563 Iterator<E> get iterator => const EmptyIterator(); |
| 564 |
| 565 void forEach(void action(E element)) {} |
| 566 |
| 567 bool get isEmpty => true; |
| 568 |
| 569 int get length => 0; |
| 570 |
| 571 E get first { throw new StateError("No elements"); } |
| 572 |
| 573 E get last { throw new StateError("No elements"); } |
| 574 |
| 575 E get single { throw new StateError("No elements"); } |
| 576 |
| 577 E elementAt(int index) { throw new RangeError.value(index); } |
| 578 |
| 579 bool contains(E element) => false; |
| 580 |
| 581 bool every(bool test(E element)) => true; |
| 582 |
| 583 bool any(bool test(E element)) => false; |
| 584 |
| 585 E firstMatching(bool test(E element), { E orElse() }) { |
| 586 if (orElse != null) return orElse(); |
| 587 throw new StateError("No matching element"); |
| 588 } |
| 589 |
| 590 E lastMatching(bool test(E element), { E orElse() }) { |
| 591 return _source.lastMatching(test, orElse: orElse); |
| 592 } |
| 593 |
| 594 E singleMatching(bool test(E element), { E orElse() }) { |
| 595 return _source.singleMatching(test, orElse: orElse); |
| 596 } |
| 597 |
| 598 int min([int compare(E a, E b)]) => null; |
| 599 |
| 600 int max([int compare(E a, E b)]) => null; |
| 601 |
| 602 String join([String separator]) => ""; |
| 603 |
| 604 Iterable<E> where(bool test(E element)) => this; |
| 605 |
| 606 Iterable map(f(E element)) => const Iterable(); |
| 607 |
| 608 Iterable mappedBy(f(E element)) => const Iterable(); |
| 609 |
| 610 reduce(var initialValue, combine(var previousValue, E element)) { |
| 611 return initialValue; |
| 612 } |
| 613 |
| 614 Iterable<E> skip(int count) => this; |
| 615 |
| 616 Iterable<E> skipWhile(bool test(E element)) => this; |
| 617 |
| 618 Iterable<E> take(int count) => this; |
| 619 |
| 620 Iterable<E> takeWhile(bool test(E element)) => this; |
| 621 |
| 622 List toList() => <E>[]; |
| 623 |
| 624 Set toSet() => new Set<E>(); |
| 625 } |
| 626 |
| 627 /** The always empty iterator. */ |
| 628 class EmptyIterator<E> implements Iterator<E> { |
| 629 const EmptyIterator(); |
| 630 bool moveNext() => false; |
| 631 E get current => null; |
| 632 } |
| OLD | NEW |