| 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._internal; | |
| 6 | |
| 7 /** | |
| 8 * Marker interface for [Iterable] subclasses that have an efficient | |
| 9 * [length] implementation. | |
| 10 */ | |
| 11 abstract class EfficientLength { | |
| 12 /** | |
| 13 * Returns the number of elements in the iterable. | |
| 14 * | |
| 15 * This is an efficient operation that doesn't require iterating through | |
| 16 * the elements. | |
| 17 */ | |
| 18 int get length; | |
| 19 } | |
| 20 | |
| 21 /** | |
| 22 * An [Iterable] for classes that have efficient [length] and [elementAt]. | |
| 23 * | |
| 24 * All other methods are implemented in terms of [length] and [elementAt], | |
| 25 * including [iterator]. | |
| 26 */ | |
| 27 abstract class ListIterable<E> extends IterableBase<E> | |
| 28 implements EfficientLength { | |
| 29 int get length; | |
| 30 E elementAt(int i); | |
| 31 | |
| 32 const ListIterable(); | |
| 33 | |
| 34 Iterator<E> get iterator => new ListIterator<E>(this); | |
| 35 | |
| 36 void forEach(void action(E element)) { | |
| 37 int length = this.length; | |
| 38 for (int i = 0; i < length; i++) { | |
| 39 action(elementAt(i)); | |
| 40 if (length != this.length) { | |
| 41 throw new ConcurrentModificationError(this); | |
| 42 } | |
| 43 } | |
| 44 } | |
| 45 | |
| 46 bool get isEmpty => length == 0; | |
| 47 | |
| 48 E get first { | |
| 49 if (length == 0) throw IterableElementError.noElement(); | |
| 50 return elementAt(0); | |
| 51 } | |
| 52 | |
| 53 E get last { | |
| 54 if (length == 0) throw IterableElementError.noElement(); | |
| 55 return elementAt(length - 1); | |
| 56 } | |
| 57 | |
| 58 E get single { | |
| 59 if (length == 0) throw IterableElementError.noElement(); | |
| 60 if (length > 1) throw IterableElementError.tooMany(); | |
| 61 return elementAt(0); | |
| 62 } | |
| 63 | |
| 64 bool contains(Object element) { | |
| 65 int length = this.length; | |
| 66 for (int i = 0; i < length; i++) { | |
| 67 if (elementAt(i) == element) return true; | |
| 68 if (length != this.length) { | |
| 69 throw new ConcurrentModificationError(this); | |
| 70 } | |
| 71 } | |
| 72 return false; | |
| 73 } | |
| 74 | |
| 75 bool every(bool test(E element)) { | |
| 76 int length = this.length; | |
| 77 for (int i = 0; i < length; i++) { | |
| 78 if (!test(elementAt(i))) return false; | |
| 79 if (length != this.length) { | |
| 80 throw new ConcurrentModificationError(this); | |
| 81 } | |
| 82 } | |
| 83 return true; | |
| 84 } | |
| 85 | |
| 86 bool any(bool test(E element)) { | |
| 87 int length = this.length; | |
| 88 for (int i = 0; i < length; i++) { | |
| 89 if (test(elementAt(i))) return true; | |
| 90 if (length != this.length) { | |
| 91 throw new ConcurrentModificationError(this); | |
| 92 } | |
| 93 } | |
| 94 return false; | |
| 95 } | |
| 96 | |
| 97 E firstWhere(bool test(E element), { E orElse() }) { | |
| 98 int length = this.length; | |
| 99 for (int i = 0; i < length; i++) { | |
| 100 E element = elementAt(i); | |
| 101 if (test(element)) return element; | |
| 102 if (length != this.length) { | |
| 103 throw new ConcurrentModificationError(this); | |
| 104 } | |
| 105 } | |
| 106 if (orElse != null) return orElse(); | |
| 107 throw IterableElementError.noElement(); | |
| 108 } | |
| 109 | |
| 110 E lastWhere(bool test(E element), { E orElse() }) { | |
| 111 int length = this.length; | |
| 112 for (int i = length - 1; i >= 0; i--) { | |
| 113 E element = elementAt(i); | |
| 114 if (test(element)) return element; | |
| 115 if (length != this.length) { | |
| 116 throw new ConcurrentModificationError(this); | |
| 117 } | |
| 118 } | |
| 119 if (orElse != null) return orElse(); | |
| 120 throw IterableElementError.noElement(); | |
| 121 } | |
| 122 | |
| 123 E singleWhere(bool test(E element)) { | |
| 124 int length = this.length; | |
| 125 E match = null; | |
| 126 bool matchFound = false; | |
| 127 for (int i = 0; i < length; i++) { | |
| 128 E element = elementAt(i); | |
| 129 if (test(element)) { | |
| 130 if (matchFound) { | |
| 131 throw IterableElementError.tooMany(); | |
| 132 } | |
| 133 matchFound = true; | |
| 134 match = element; | |
| 135 } | |
| 136 if (length != this.length) { | |
| 137 throw new ConcurrentModificationError(this); | |
| 138 } | |
| 139 } | |
| 140 if (matchFound) return match; | |
| 141 throw IterableElementError.noElement(); | |
| 142 } | |
| 143 | |
| 144 String join([String separator = ""]) { | |
| 145 int length = this.length; | |
| 146 if (!separator.isEmpty) { | |
| 147 if (length == 0) return ""; | |
| 148 String first = "${elementAt(0)}"; | |
| 149 if (length != this.length) { | |
| 150 throw new ConcurrentModificationError(this); | |
| 151 } | |
| 152 StringBuffer buffer = new StringBuffer(first); | |
| 153 for (int i = 1; i < length; i++) { | |
| 154 buffer.write(separator); | |
| 155 buffer.write(elementAt(i)); | |
| 156 if (length != this.length) { | |
| 157 throw new ConcurrentModificationError(this); | |
| 158 } | |
| 159 } | |
| 160 return buffer.toString(); | |
| 161 } else { | |
| 162 StringBuffer buffer = new StringBuffer(); | |
| 163 for (int i = 0; i < length; i++) { | |
| 164 buffer.write(elementAt(i)); | |
| 165 if (length != this.length) { | |
| 166 throw new ConcurrentModificationError(this); | |
| 167 } | |
| 168 } | |
| 169 return buffer.toString(); | |
| 170 } | |
| 171 } | |
| 172 | |
| 173 Iterable<E> where(bool test(E element)) => super.where(test); | |
| 174 | |
| 175 Iterable map(f(E element)) => new MappedListIterable(this, f); | |
| 176 | |
| 177 E reduce(E combine(var value, E element)) { | |
| 178 int length = this.length; | |
| 179 if (length == 0) throw IterableElementError.noElement(); | |
| 180 E value = elementAt(0); | |
| 181 for (int i = 1; i < length; i++) { | |
| 182 value = combine(value, elementAt(i)); | |
| 183 if (length != this.length) { | |
| 184 throw new ConcurrentModificationError(this); | |
| 185 } | |
| 186 | |
| 187 } | |
| 188 return value; | |
| 189 } | |
| 190 | |
| 191 fold(var initialValue, combine(var previousValue, E element)) { | |
| 192 var value = initialValue; | |
| 193 int length = this.length; | |
| 194 for (int i = 0; i < length; i++) { | |
| 195 value = combine(value, elementAt(i)); | |
| 196 if (length != this.length) { | |
| 197 throw new ConcurrentModificationError(this); | |
| 198 } | |
| 199 } | |
| 200 return value; | |
| 201 } | |
| 202 | |
| 203 Iterable<E> skip(int count) => new SubListIterable<E>(this, count, null); | |
| 204 | |
| 205 Iterable<E> skipWhile(bool test(E element)) => super.skipWhile(test); | |
| 206 | |
| 207 Iterable<E> take(int count) => new SubListIterable<E>(this, 0, count); | |
| 208 | |
| 209 Iterable<E> takeWhile(bool test(E element)) => super.takeWhile(test); | |
| 210 | |
| 211 List<E> toList({ bool growable: true }) { | |
| 212 List<E> result; | |
| 213 if (growable) { | |
| 214 result = new List<E>()..length = length; | |
| 215 } else { | |
| 216 result = new List<E>(length); | |
| 217 } | |
| 218 for (int i = 0; i < length; i++) { | |
| 219 result[i] = elementAt(i); | |
| 220 } | |
| 221 return result; | |
| 222 } | |
| 223 | |
| 224 Set<E> toSet() { | |
| 225 Set<E> result = new Set<E>(); | |
| 226 for (int i = 0; i < length; i++) { | |
| 227 result.add(elementAt(i)); | |
| 228 } | |
| 229 return result; | |
| 230 } | |
| 231 } | |
| 232 | |
| 233 class SubListIterable<E> extends ListIterable<E> { | |
| 234 final Iterable<E> _iterable; // Has efficient length and elementAt. | |
| 235 final int _start; | |
| 236 /** If null, represents the length of the iterable. */ | |
| 237 final int _endOrLength; | |
| 238 | |
| 239 SubListIterable(this._iterable, this._start, this._endOrLength) { | |
| 240 RangeError.checkNotNegative(_start, "start"); | |
| 241 if (_endOrLength != null) { | |
| 242 RangeError.checkNotNegative(_endOrLength, "end"); | |
| 243 if (_start > _endOrLength) { | |
| 244 throw new RangeError.range(_start, 0, _endOrLength, "start"); | |
| 245 } | |
| 246 } | |
| 247 } | |
| 248 | |
| 249 int get _endIndex { | |
| 250 int length = _iterable.length; | |
| 251 if (_endOrLength == null || _endOrLength > length) return length; | |
| 252 return _endOrLength; | |
| 253 } | |
| 254 | |
| 255 int get _startIndex { | |
| 256 int length = _iterable.length; | |
| 257 if (_start > length) return length; | |
| 258 return _start; | |
| 259 } | |
| 260 | |
| 261 int get length { | |
| 262 int length = _iterable.length; | |
| 263 if (_start >= length) return 0; | |
| 264 if (_endOrLength == null || _endOrLength >= length) { | |
| 265 return length - _start; | |
| 266 } | |
| 267 return _endOrLength - _start; | |
| 268 } | |
| 269 | |
| 270 E elementAt(int index) { | |
| 271 int realIndex = _startIndex + index; | |
| 272 if (index < 0 || realIndex >= _endIndex) { | |
| 273 throw new RangeError.index(index, this, "index"); | |
| 274 } | |
| 275 return _iterable.elementAt(realIndex); | |
| 276 } | |
| 277 | |
| 278 Iterable<E> skip(int count) { | |
| 279 RangeError.checkNotNegative(count, "count"); | |
| 280 int newStart = _start + count; | |
| 281 if (_endOrLength != null && newStart >= _endOrLength) { | |
| 282 return new EmptyIterable<E>(); | |
| 283 } | |
| 284 return new SubListIterable<E>(_iterable, newStart, _endOrLength); | |
| 285 } | |
| 286 | |
| 287 Iterable<E> take(int count) { | |
| 288 RangeError.checkNotNegative(count, "count"); | |
| 289 if (_endOrLength == null) { | |
| 290 return new SubListIterable<E>(_iterable, _start, _start + count); | |
| 291 } else { | |
| 292 int newEnd = _start + count; | |
| 293 if (_endOrLength < newEnd) return this; | |
| 294 return new SubListIterable<E>(_iterable, _start, newEnd); | |
| 295 } | |
| 296 } | |
| 297 | |
| 298 List<E> toList({bool growable: true}) { | |
| 299 int start = _start; | |
| 300 int end = _iterable.length; | |
| 301 if (_endOrLength != null && _endOrLength < end) end = _endOrLength; | |
| 302 int length = end - start; | |
| 303 if (length < 0) length = 0; | |
| 304 List result = growable ? (new List<E>()..length = length) | |
| 305 : new List<E>(length); | |
| 306 for (int i = 0; i < length; i++) { | |
| 307 result[i] = _iterable.elementAt(start + i); | |
| 308 if (_iterable.length < end) throw new ConcurrentModificationError(this); | |
| 309 } | |
| 310 return result; | |
| 311 } | |
| 312 } | |
| 313 | |
| 314 /** | |
| 315 * An [Iterator] that iterates a list-like [Iterable]. | |
| 316 * | |
| 317 * All iterations is done in terms of [Iterable.length] and | |
| 318 * [Iterable.elementAt]. These operations are fast for list-like | |
| 319 * iterables. | |
| 320 */ | |
| 321 class ListIterator<E> implements Iterator<E> { | |
| 322 final Iterable<E> _iterable; | |
| 323 final int _length; | |
| 324 int _index; | |
| 325 E _current; | |
| 326 | |
| 327 ListIterator(Iterable<E> iterable) | |
| 328 : _iterable = iterable, _length = iterable.length, _index = 0; | |
| 329 | |
| 330 E get current => _current; | |
| 331 | |
| 332 bool moveNext() { | |
| 333 int length = _iterable.length; | |
| 334 if (_length != length) { | |
| 335 throw new ConcurrentModificationError(_iterable); | |
| 336 } | |
| 337 if (_index >= length) { | |
| 338 _current = null; | |
| 339 return false; | |
| 340 } | |
| 341 _current = _iterable.elementAt(_index); | |
| 342 _index++; | |
| 343 return true; | |
| 344 } | |
| 345 } | |
| 346 | |
| 347 typedef T _Transformation<S, T>(S value); | |
| 348 | |
| 349 class MappedIterable<S, T> extends IterableBase<T> { | |
| 350 final Iterable<S> _iterable; | |
| 351 final _Transformation<S, T> _f; | |
| 352 | |
| 353 factory MappedIterable(Iterable iterable, T function(S value)) { | |
| 354 if (iterable is EfficientLength) { | |
| 355 return new EfficientLengthMappedIterable<S, T>(iterable, function); | |
| 356 } | |
| 357 return new MappedIterable<S, T>._(iterable, function); | |
| 358 } | |
| 359 | |
| 360 MappedIterable._(this._iterable, T this._f(S element)); | |
| 361 | |
| 362 Iterator<T> get iterator => new MappedIterator<S, T>(_iterable.iterator, _f); | |
| 363 | |
| 364 // Length related functions are independent of the mapping. | |
| 365 int get length => _iterable.length; | |
| 366 bool get isEmpty => _iterable.isEmpty; | |
| 367 | |
| 368 // Index based lookup can be done before transforming. | |
| 369 T get first => _f(_iterable.first); | |
| 370 T get last => _f(_iterable.last); | |
| 371 T get single => _f(_iterable.single); | |
| 372 T elementAt(int index) => _f(_iterable.elementAt(index)); | |
| 373 } | |
| 374 | |
| 375 class EfficientLengthMappedIterable<S, T> extends MappedIterable<S, T> | |
| 376 implements EfficientLength { | |
| 377 EfficientLengthMappedIterable(Iterable iterable, T function(S value)) | |
| 378 : super._(iterable, function); | |
| 379 } | |
| 380 | |
| 381 class MappedIterator<S, T> extends Iterator<T> { | |
| 382 T _current; | |
| 383 final Iterator<S> _iterator; | |
| 384 final _Transformation<S, T> _f; | |
| 385 | |
| 386 MappedIterator(this._iterator, T this._f(S element)); | |
| 387 | |
| 388 bool moveNext() { | |
| 389 if (_iterator.moveNext()) { | |
| 390 _current = _f(_iterator.current); | |
| 391 return true; | |
| 392 } | |
| 393 _current = null; | |
| 394 return false; | |
| 395 } | |
| 396 | |
| 397 T get current => _current; | |
| 398 } | |
| 399 | |
| 400 /** | |
| 401 * Specialized alternative to [MappedIterable] for mapped [List]s. | |
| 402 * | |
| 403 * Expects efficient `length` and `elementAt` on the source iterable. | |
| 404 */ | |
| 405 class MappedListIterable<S, T> extends ListIterable<T> | |
| 406 implements EfficientLength { | |
| 407 final Iterable<S> _source; | |
| 408 final _Transformation<S, T> _f; | |
| 409 | |
| 410 MappedListIterable(this._source, T this._f(S value)); | |
| 411 | |
| 412 int get length => _source.length; | |
| 413 T elementAt(int index) => _f(_source.elementAt(index)); | |
| 414 } | |
| 415 | |
| 416 | |
| 417 typedef bool _ElementPredicate<E>(E element); | |
| 418 | |
| 419 class WhereIterable<E> extends IterableBase<E> { | |
| 420 final Iterable<E> _iterable; | |
| 421 final _ElementPredicate<E> _f; | |
| 422 | |
| 423 WhereIterable(this._iterable, bool this._f(E element)); | |
| 424 | |
| 425 Iterator<E> get iterator => new WhereIterator<E>(_iterable.iterator, _f); | |
| 426 } | |
| 427 | |
| 428 class WhereIterator<E> extends Iterator<E> { | |
| 429 final Iterator<E> _iterator; | |
| 430 final _ElementPredicate<E> _f; | |
| 431 | |
| 432 WhereIterator(this._iterator, bool this._f(E element)); | |
| 433 | |
| 434 bool moveNext() { | |
| 435 while (_iterator.moveNext()) { | |
| 436 if (_f(_iterator.current)) { | |
| 437 return true; | |
| 438 } | |
| 439 } | |
| 440 return false; | |
| 441 } | |
| 442 | |
| 443 E get current => _iterator.current; | |
| 444 } | |
| 445 | |
| 446 typedef Iterable<T> _ExpandFunction<S, T>(S sourceElement); | |
| 447 | |
| 448 class ExpandIterable<S, T> extends IterableBase<T> { | |
| 449 final Iterable<S> _iterable; | |
| 450 final _ExpandFunction _f; | |
| 451 | |
| 452 ExpandIterable(this._iterable, Iterable<T> this._f(S element)); | |
| 453 | |
| 454 Iterator<T> get iterator => new ExpandIterator<S, T>(_iterable.iterator, _f); | |
| 455 } | |
| 456 | |
| 457 class ExpandIterator<S, T> implements Iterator<T> { | |
| 458 final Iterator<S> _iterator; | |
| 459 final _ExpandFunction _f; | |
| 460 // Initialize _currentExpansion to an empty iterable. A null value | |
| 461 // marks the end of iteration, and we don't want to call _f before | |
| 462 // the first moveNext call. | |
| 463 Iterator<T> _currentExpansion = const EmptyIterator(); | |
| 464 T _current; | |
| 465 | |
| 466 ExpandIterator(this._iterator, Iterable<T> this._f(S element)); | |
| 467 | |
| 468 void _nextExpansion() { | |
| 469 } | |
| 470 | |
| 471 T get current => _current; | |
| 472 | |
| 473 bool moveNext() { | |
| 474 if (_currentExpansion == null) return false; | |
| 475 while (!_currentExpansion.moveNext()) { | |
| 476 _current = null; | |
| 477 if (_iterator.moveNext()) { | |
| 478 // If _f throws, this ends iteration. Otherwise _currentExpansion and | |
| 479 // _current will be set again below. | |
| 480 _currentExpansion = null; | |
| 481 _currentExpansion = _f(_iterator.current).iterator; | |
| 482 } else { | |
| 483 return false; | |
| 484 } | |
| 485 } | |
| 486 _current = _currentExpansion.current; | |
| 487 return true; | |
| 488 } | |
| 489 } | |
| 490 | |
| 491 class TakeIterable<E> extends IterableBase<E> { | |
| 492 final Iterable<E> _iterable; | |
| 493 final int _takeCount; | |
| 494 | |
| 495 factory TakeIterable(Iterable<E> iterable, int takeCount) { | |
| 496 if (takeCount is! int || takeCount < 0) { | |
| 497 throw new ArgumentError(takeCount); | |
| 498 } | |
| 499 if (iterable is EfficientLength) { | |
| 500 return new EfficientLengthTakeIterable<E>(iterable, takeCount); | |
| 501 } | |
| 502 return new TakeIterable<E>._(iterable, takeCount); | |
| 503 } | |
| 504 | |
| 505 TakeIterable._(this._iterable, this._takeCount); | |
| 506 | |
| 507 Iterator<E> get iterator { | |
| 508 return new TakeIterator<E>(_iterable.iterator, _takeCount); | |
| 509 } | |
| 510 } | |
| 511 | |
| 512 class EfficientLengthTakeIterable<E> extends TakeIterable<E> | |
| 513 implements EfficientLength { | |
| 514 EfficientLengthTakeIterable(Iterable<E> iterable, int takeCount) | |
| 515 : super._(iterable, takeCount); | |
| 516 | |
| 517 int get length { | |
| 518 int iterableLength = _iterable.length; | |
| 519 if (iterableLength > _takeCount) return _takeCount; | |
| 520 return iterableLength; | |
| 521 } | |
| 522 } | |
| 523 | |
| 524 | |
| 525 class TakeIterator<E> extends Iterator<E> { | |
| 526 final Iterator<E> _iterator; | |
| 527 int _remaining; | |
| 528 | |
| 529 TakeIterator(this._iterator, this._remaining) { | |
| 530 assert(_remaining is int && _remaining >= 0); | |
| 531 } | |
| 532 | |
| 533 bool moveNext() { | |
| 534 _remaining--; | |
| 535 if (_remaining >= 0) { | |
| 536 return _iterator.moveNext(); | |
| 537 } | |
| 538 _remaining = -1; | |
| 539 return false; | |
| 540 } | |
| 541 | |
| 542 E get current { | |
| 543 if (_remaining < 0) return null; | |
| 544 return _iterator.current; | |
| 545 } | |
| 546 } | |
| 547 | |
| 548 class TakeWhileIterable<E> extends IterableBase<E> { | |
| 549 final Iterable<E> _iterable; | |
| 550 final _ElementPredicate _f; | |
| 551 | |
| 552 TakeWhileIterable(this._iterable, bool this._f(E element)); | |
| 553 | |
| 554 Iterator<E> get iterator { | |
| 555 return new TakeWhileIterator<E>(_iterable.iterator, _f); | |
| 556 } | |
| 557 } | |
| 558 | |
| 559 class TakeWhileIterator<E> extends Iterator<E> { | |
| 560 final Iterator<E> _iterator; | |
| 561 final _ElementPredicate _f; | |
| 562 bool _isFinished = false; | |
| 563 | |
| 564 TakeWhileIterator(this._iterator, bool this._f(E element)); | |
| 565 | |
| 566 bool moveNext() { | |
| 567 if (_isFinished) return false; | |
| 568 if (!_iterator.moveNext() || !_f(_iterator.current)) { | |
| 569 _isFinished = true; | |
| 570 return false; | |
| 571 } | |
| 572 return true; | |
| 573 } | |
| 574 | |
| 575 E get current { | |
| 576 if (_isFinished) return null; | |
| 577 return _iterator.current; | |
| 578 } | |
| 579 } | |
| 580 | |
| 581 class SkipIterable<E> extends IterableBase<E> { | |
| 582 final Iterable<E> _iterable; | |
| 583 final int _skipCount; | |
| 584 | |
| 585 factory SkipIterable(Iterable<E> iterable, int count) { | |
| 586 if (iterable is EfficientLength) { | |
| 587 return new EfficientLengthSkipIterable<E>(iterable, count); | |
| 588 } | |
| 589 return new SkipIterable<E>._(iterable, count); | |
| 590 } | |
| 591 | |
| 592 SkipIterable._(this._iterable, this._skipCount) { | |
| 593 if (_skipCount is! int) { | |
| 594 throw new ArgumentError.value(_skipCount, "count is not an integer"); | |
| 595 } | |
| 596 RangeError.checkNotNegative(_skipCount, "count"); | |
| 597 } | |
| 598 | |
| 599 Iterable<E> skip(int count) { | |
| 600 if (_skipCount is! int) { | |
| 601 throw new ArgumentError.value(_skipCount, "count is not an integer"); | |
| 602 } | |
| 603 RangeError.checkNotNegative(_skipCount, "count"); | |
| 604 return new SkipIterable<E>._(_iterable, _skipCount + count); | |
| 605 } | |
| 606 | |
| 607 Iterator<E> get iterator { | |
| 608 return new SkipIterator<E>(_iterable.iterator, _skipCount); | |
| 609 } | |
| 610 } | |
| 611 | |
| 612 class EfficientLengthSkipIterable<E> extends SkipIterable<E> | |
| 613 implements EfficientLength { | |
| 614 EfficientLengthSkipIterable(Iterable<E> iterable, int skipCount) | |
| 615 : super._(iterable, skipCount); | |
| 616 | |
| 617 int get length { | |
| 618 int length = _iterable.length - _skipCount; | |
| 619 if (length >= 0) return length; | |
| 620 return 0; | |
| 621 } | |
| 622 } | |
| 623 | |
| 624 class SkipIterator<E> extends Iterator<E> { | |
| 625 final Iterator<E> _iterator; | |
| 626 int _skipCount; | |
| 627 | |
| 628 SkipIterator(this._iterator, this._skipCount) { | |
| 629 assert(_skipCount is int && _skipCount >= 0); | |
| 630 } | |
| 631 | |
| 632 bool moveNext() { | |
| 633 for (int i = 0; i < _skipCount; i++) _iterator.moveNext(); | |
| 634 _skipCount = 0; | |
| 635 return _iterator.moveNext(); | |
| 636 } | |
| 637 | |
| 638 E get current => _iterator.current; | |
| 639 } | |
| 640 | |
| 641 class SkipWhileIterable<E> extends IterableBase<E> { | |
| 642 final Iterable<E> _iterable; | |
| 643 final _ElementPredicate _f; | |
| 644 | |
| 645 SkipWhileIterable(this._iterable, bool this._f(E element)); | |
| 646 | |
| 647 Iterator<E> get iterator { | |
| 648 return new SkipWhileIterator<E>(_iterable.iterator, _f); | |
| 649 } | |
| 650 } | |
| 651 | |
| 652 class SkipWhileIterator<E> extends Iterator<E> { | |
| 653 final Iterator<E> _iterator; | |
| 654 final _ElementPredicate _f; | |
| 655 bool _hasSkipped = false; | |
| 656 | |
| 657 SkipWhileIterator(this._iterator, bool this._f(E element)); | |
| 658 | |
| 659 bool moveNext() { | |
| 660 if (!_hasSkipped) { | |
| 661 _hasSkipped = true; | |
| 662 while (_iterator.moveNext()) { | |
| 663 if (!_f(_iterator.current)) return true; | |
| 664 } | |
| 665 } | |
| 666 return _iterator.moveNext(); | |
| 667 } | |
| 668 | |
| 669 E get current => _iterator.current; | |
| 670 } | |
| 671 | |
| 672 /** | |
| 673 * The always empty [Iterable]. | |
| 674 */ | |
| 675 class EmptyIterable<E> extends IterableBase<E> implements EfficientLength { | |
| 676 const EmptyIterable(); | |
| 677 | |
| 678 Iterator<E> get iterator => const EmptyIterator(); | |
| 679 | |
| 680 void forEach(void action(E element)) {} | |
| 681 | |
| 682 bool get isEmpty => true; | |
| 683 | |
| 684 int get length => 0; | |
| 685 | |
| 686 E get first { throw IterableElementError.noElement(); } | |
| 687 | |
| 688 E get last { throw IterableElementError.noElement(); } | |
| 689 | |
| 690 E get single { throw IterableElementError.noElement(); } | |
| 691 | |
| 692 E elementAt(int index) { throw new RangeError.range(index, 0, 0, "index"); } | |
| 693 | |
| 694 bool contains(Object element) => false; | |
| 695 | |
| 696 bool every(bool test(E element)) => true; | |
| 697 | |
| 698 bool any(bool test(E element)) => false; | |
| 699 | |
| 700 E firstWhere(bool test(E element), { E orElse() }) { | |
| 701 if (orElse != null) return orElse(); | |
| 702 throw IterableElementError.noElement(); | |
| 703 } | |
| 704 | |
| 705 E lastWhere(bool test(E element), { E orElse() }) { | |
| 706 if (orElse != null) return orElse(); | |
| 707 throw IterableElementError.noElement(); | |
| 708 } | |
| 709 | |
| 710 E singleWhere(bool test(E element), { E orElse() }) { | |
| 711 if (orElse != null) return orElse(); | |
| 712 throw IterableElementError.noElement(); | |
| 713 } | |
| 714 | |
| 715 String join([String separator = ""]) => ""; | |
| 716 | |
| 717 Iterable<E> where(bool test(E element)) => this; | |
| 718 | |
| 719 Iterable map(f(E element)) => const EmptyIterable(); | |
| 720 | |
| 721 E reduce(E combine(E value, E element)) { | |
| 722 throw IterableElementError.noElement(); | |
| 723 } | |
| 724 | |
| 725 fold(var initialValue, combine(var previousValue, E element)) { | |
| 726 return initialValue; | |
| 727 } | |
| 728 | |
| 729 Iterable<E> skip(int count) { | |
| 730 RangeError.checkNotNegative(count, "count"); | |
| 731 return this; | |
| 732 } | |
| 733 | |
| 734 Iterable<E> skipWhile(bool test(E element)) => this; | |
| 735 | |
| 736 Iterable<E> take(int count) { | |
| 737 RangeError.checkNotNegative(count, "count"); | |
| 738 return this; | |
| 739 } | |
| 740 | |
| 741 Iterable<E> takeWhile(bool test(E element)) => this; | |
| 742 | |
| 743 List<E> toList({ bool growable: true }) => growable ? <E>[] : new List<E>(0); | |
| 744 | |
| 745 Set<E> toSet() => new Set<E>(); | |
| 746 } | |
| 747 | |
| 748 /** The always empty iterator. */ | |
| 749 class EmptyIterator<E> implements Iterator<E> { | |
| 750 const EmptyIterator(); | |
| 751 bool moveNext() => false; | |
| 752 E get current => null; | |
| 753 } | |
| 754 | |
| 755 /** An [Iterator] that can move in both directions. */ | |
| 756 abstract class BidirectionalIterator<T> implements Iterator<T> { | |
| 757 bool movePrevious(); | |
| 758 } | |
| 759 | |
| 760 /** | |
| 761 * This class provides default implementations for Iterables (including Lists). | |
| 762 * | |
| 763 * The uses of this class will be replaced by mixins. | |
| 764 */ | |
| 765 class IterableMixinWorkaround<T> { | |
| 766 static bool contains(Iterable iterable, var element) { | |
| 767 for (final e in iterable) { | |
| 768 if (e == element) return true; | |
| 769 } | |
| 770 return false; | |
| 771 } | |
| 772 | |
| 773 static void forEach(Iterable iterable, void f(o)) { | |
| 774 for (final e in iterable) { | |
| 775 f(e); | |
| 776 } | |
| 777 } | |
| 778 | |
| 779 static bool any(Iterable iterable, bool f(o)) { | |
| 780 for (final e in iterable) { | |
| 781 if (f(e)) return true; | |
| 782 } | |
| 783 return false; | |
| 784 } | |
| 785 | |
| 786 static bool every(Iterable iterable, bool f(o)) { | |
| 787 for (final e in iterable) { | |
| 788 if (!f(e)) return false; | |
| 789 } | |
| 790 return true; | |
| 791 } | |
| 792 | |
| 793 static dynamic reduce(Iterable iterable, | |
| 794 dynamic combine(previousValue, element)) { | |
| 795 Iterator iterator = iterable.iterator; | |
| 796 if (!iterator.moveNext()) throw IterableElementError.noElement(); | |
| 797 var value = iterator.current; | |
| 798 while (iterator.moveNext()) { | |
| 799 value = combine(value, iterator.current); | |
| 800 } | |
| 801 return value; | |
| 802 } | |
| 803 | |
| 804 static dynamic fold(Iterable iterable, | |
| 805 dynamic initialValue, | |
| 806 dynamic combine(dynamic previousValue, element)) { | |
| 807 for (final element in iterable) { | |
| 808 initialValue = combine(initialValue, element); | |
| 809 } | |
| 810 return initialValue; | |
| 811 } | |
| 812 | |
| 813 /** | |
| 814 * Removes elements matching [test] from [list]. | |
| 815 * | |
| 816 * This is performed in two steps, to avoid exposing an inconsistent state | |
| 817 * to the [test] function. First the elements to retain are found, and then | |
| 818 * the original list is updated to contain those elements. | |
| 819 */ | |
| 820 static void removeWhereList(List list, bool test(var element)) { | |
| 821 List retained = []; | |
| 822 int length = list.length; | |
| 823 for (int i = 0; i < length; i++) { | |
| 824 var element = list[i]; | |
| 825 if (!test(element)) { | |
| 826 retained.add(element); | |
| 827 } | |
| 828 if (length != list.length) { | |
| 829 throw new ConcurrentModificationError(list); | |
| 830 } | |
| 831 } | |
| 832 if (retained.length == length) return; | |
| 833 list.length = retained.length; | |
| 834 for (int i = 0; i < retained.length; i++) { | |
| 835 list[i] = retained[i]; | |
| 836 } | |
| 837 } | |
| 838 | |
| 839 static bool isEmpty(Iterable iterable) { | |
| 840 return !iterable.iterator.moveNext(); | |
| 841 } | |
| 842 | |
| 843 static dynamic first(Iterable iterable) { | |
| 844 Iterator it = iterable.iterator; | |
| 845 if (!it.moveNext()) { | |
| 846 throw IterableElementError.noElement(); | |
| 847 } | |
| 848 return it.current; | |
| 849 } | |
| 850 | |
| 851 static dynamic last(Iterable iterable) { | |
| 852 Iterator it = iterable.iterator; | |
| 853 if (!it.moveNext()) { | |
| 854 throw IterableElementError.noElement(); | |
| 855 } | |
| 856 dynamic result; | |
| 857 do { | |
| 858 result = it.current; | |
| 859 } while(it.moveNext()); | |
| 860 return result; | |
| 861 } | |
| 862 | |
| 863 static dynamic single(Iterable iterable) { | |
| 864 Iterator it = iterable.iterator; | |
| 865 if (!it.moveNext()) throw IterableElementError.noElement(); | |
| 866 dynamic result = it.current; | |
| 867 if (it.moveNext()) throw IterableElementError.tooMany(); | |
| 868 return result; | |
| 869 } | |
| 870 | |
| 871 static dynamic firstWhere(Iterable iterable, | |
| 872 bool test(dynamic value), | |
| 873 dynamic orElse()) { | |
| 874 for (dynamic element in iterable) { | |
| 875 if (test(element)) return element; | |
| 876 } | |
| 877 if (orElse != null) return orElse(); | |
| 878 throw IterableElementError.noElement(); | |
| 879 } | |
| 880 | |
| 881 static dynamic lastWhere(Iterable iterable, | |
| 882 bool test(dynamic value), | |
| 883 dynamic orElse()) { | |
| 884 dynamic result = null; | |
| 885 bool foundMatching = false; | |
| 886 for (dynamic element in iterable) { | |
| 887 if (test(element)) { | |
| 888 result = element; | |
| 889 foundMatching = true; | |
| 890 } | |
| 891 } | |
| 892 if (foundMatching) return result; | |
| 893 if (orElse != null) return orElse(); | |
| 894 throw IterableElementError.noElement(); | |
| 895 } | |
| 896 | |
| 897 static dynamic lastWhereList(List list, | |
| 898 bool test(dynamic value), | |
| 899 dynamic orElse()) { | |
| 900 // TODO(floitsch): check that arguments are of correct type? | |
| 901 for (int i = list.length - 1; i >= 0; i--) { | |
| 902 dynamic element = list[i]; | |
| 903 if (test(element)) return element; | |
| 904 } | |
| 905 if (orElse != null) return orElse(); | |
| 906 throw IterableElementError.noElement(); | |
| 907 } | |
| 908 | |
| 909 static dynamic singleWhere(Iterable iterable, bool test(dynamic value)) { | |
| 910 dynamic result = null; | |
| 911 bool foundMatching = false; | |
| 912 for (dynamic element in iterable) { | |
| 913 if (test(element)) { | |
| 914 if (foundMatching) { | |
| 915 throw IterableElementError.tooMany(); | |
| 916 } | |
| 917 result = element; | |
| 918 foundMatching = true; | |
| 919 } | |
| 920 } | |
| 921 if (foundMatching) return result; | |
| 922 throw IterableElementError.noElement(); | |
| 923 } | |
| 924 | |
| 925 static elementAt(Iterable iterable, int index) { | |
| 926 if (index is! int) throw new ArgumentError.notNull("index"); | |
| 927 RangeError.checkNotNegative(index, "index"); | |
| 928 int elementIndex = 0; | |
| 929 for (var element in iterable) { | |
| 930 if (index == elementIndex) return element; | |
| 931 elementIndex++; | |
| 932 } | |
| 933 throw new RangeError.index(index, iterable, "index", null, elementIndex); | |
| 934 } | |
| 935 | |
| 936 static String join(Iterable iterable, [String separator]) { | |
| 937 StringBuffer buffer = new StringBuffer(); | |
| 938 buffer.writeAll(iterable, separator); | |
| 939 return buffer.toString(); | |
| 940 } | |
| 941 | |
| 942 static String joinList(List list, [String separator]) { | |
| 943 if (list.isEmpty) return ""; | |
| 944 if (list.length == 1) return "${list[0]}"; | |
| 945 StringBuffer buffer = new StringBuffer(); | |
| 946 if (separator.isEmpty) { | |
| 947 for (int i = 0; i < list.length; i++) { | |
| 948 buffer.write(list[i]); | |
| 949 } | |
| 950 } else { | |
| 951 buffer.write(list[0]); | |
| 952 for (int i = 1; i < list.length; i++) { | |
| 953 buffer.write(separator); | |
| 954 buffer.write(list[i]); | |
| 955 } | |
| 956 } | |
| 957 return buffer.toString(); | |
| 958 } | |
| 959 | |
| 960 Iterable<T> where(Iterable iterable, bool f(var element)) { | |
| 961 return new WhereIterable<T>(iterable, f); | |
| 962 } | |
| 963 | |
| 964 static Iterable map(Iterable iterable, f(var element)) { | |
| 965 return new MappedIterable(iterable, f); | |
| 966 } | |
| 967 | |
| 968 static Iterable mapList(List list, f(var element)) { | |
| 969 return new MappedListIterable(list, f); | |
| 970 } | |
| 971 | |
| 972 static Iterable expand(Iterable iterable, Iterable f(var element)) { | |
| 973 return new ExpandIterable(iterable, f); | |
| 974 } | |
| 975 | |
| 976 Iterable<T> takeList(List list, int n) { | |
| 977 // The generic type is currently lost. It will be fixed with mixins. | |
| 978 return new SubListIterable<T>(list, 0, n); | |
| 979 } | |
| 980 | |
| 981 Iterable<T> takeWhile(Iterable iterable, bool test(var value)) { | |
| 982 // The generic type is currently lost. It will be fixed with mixins. | |
| 983 return new TakeWhileIterable<T>(iterable, test); | |
| 984 } | |
| 985 | |
| 986 Iterable<T> skipList(List list, int n) { | |
| 987 // The generic type is currently lost. It will be fixed with mixins. | |
| 988 return new SubListIterable<T>(list, n, null); | |
| 989 } | |
| 990 | |
| 991 Iterable<T> skipWhile(Iterable iterable, bool test(var value)) { | |
| 992 // The generic type is currently lost. It will be fixed with mixins. | |
| 993 return new SkipWhileIterable<T>(iterable, test); | |
| 994 } | |
| 995 | |
| 996 Iterable<T> reversedList(List list) { | |
| 997 return new ReversedListIterable<T>(list); | |
| 998 } | |
| 999 | |
| 1000 static void sortList(List list, int compare(a, b)) { | |
| 1001 if (compare == null) compare = Comparable.compare; | |
| 1002 Sort.sort(list, compare); | |
| 1003 } | |
| 1004 | |
| 1005 static void shuffleList(List list, Random random) { | |
| 1006 if (random == null) random = new Random(); | |
| 1007 int length = list.length; | |
| 1008 while (length > 1) { | |
| 1009 int pos = random.nextInt(length); | |
| 1010 length -= 1; | |
| 1011 var tmp = list[length]; | |
| 1012 list[length] = list[pos]; | |
| 1013 list[pos] = tmp; | |
| 1014 } | |
| 1015 } | |
| 1016 | |
| 1017 static int indexOfList(List list, var element, int start) { | |
| 1018 return Lists.indexOf(list, element, start, list.length); | |
| 1019 } | |
| 1020 | |
| 1021 static int lastIndexOfList(List list, var element, int start) { | |
| 1022 if (start == null) start = list.length - 1; | |
| 1023 return Lists.lastIndexOf(list, element, start); | |
| 1024 } | |
| 1025 | |
| 1026 static void _rangeCheck(List list, int start, int end) { | |
| 1027 RangeError.checkValidRange(start, end, list.length); | |
| 1028 } | |
| 1029 | |
| 1030 Iterable<T> getRangeList(List list, int start, int end) { | |
| 1031 _rangeCheck(list, start, end); | |
| 1032 // The generic type is currently lost. It will be fixed with mixins. | |
| 1033 return new SubListIterable<T>(list, start, end); | |
| 1034 } | |
| 1035 | |
| 1036 static void setRangeList(List list, int start, int end, | |
| 1037 Iterable from, int skipCount) { | |
| 1038 _rangeCheck(list, start, end); | |
| 1039 int length = end - start; | |
| 1040 if (length == 0) return; | |
| 1041 | |
| 1042 if (skipCount < 0) throw new ArgumentError(skipCount); | |
| 1043 | |
| 1044 // TODO(floitsch): Make this accept more. | |
| 1045 List otherList; | |
| 1046 int otherStart; | |
| 1047 if (from is List) { | |
| 1048 otherList = from; | |
| 1049 otherStart = skipCount; | |
| 1050 } else { | |
| 1051 otherList = from.skip(skipCount).toList(growable: false); | |
| 1052 otherStart = 0; | |
| 1053 } | |
| 1054 if (otherStart + length > otherList.length) { | |
| 1055 throw IterableElementError.tooFew(); | |
| 1056 } | |
| 1057 Lists.copy(otherList, otherStart, list, start, length); | |
| 1058 } | |
| 1059 | |
| 1060 static void replaceRangeList(List list, int start, int end, | |
| 1061 Iterable iterable) { | |
| 1062 _rangeCheck(list, start, end); | |
| 1063 if (iterable is! EfficientLength) { | |
| 1064 iterable = iterable.toList(); | |
| 1065 } | |
| 1066 int removeLength = end - start; | |
| 1067 int insertLength = iterable.length; | |
| 1068 if (removeLength >= insertLength) { | |
| 1069 int delta = removeLength - insertLength; | |
| 1070 int insertEnd = start + insertLength; | |
| 1071 int newEnd = list.length - delta; | |
| 1072 list.setRange(start, insertEnd, iterable); | |
| 1073 if (delta != 0) { | |
| 1074 list.setRange(insertEnd, newEnd, list, end); | |
| 1075 list.length = newEnd; | |
| 1076 } | |
| 1077 } else { | |
| 1078 int delta = insertLength - removeLength; | |
| 1079 int newLength = list.length + delta; | |
| 1080 int insertEnd = start + insertLength; // aka. end + delta. | |
| 1081 list.length = newLength; | |
| 1082 list.setRange(insertEnd, newLength, list, end); | |
| 1083 list.setRange(start, insertEnd, iterable); | |
| 1084 } | |
| 1085 } | |
| 1086 | |
| 1087 static void fillRangeList(List list, int start, int end, fillValue) { | |
| 1088 _rangeCheck(list, start, end); | |
| 1089 for (int i = start; i < end; i++) { | |
| 1090 list[i] = fillValue; | |
| 1091 } | |
| 1092 } | |
| 1093 | |
| 1094 static void insertAllList(List list, int index, Iterable iterable) { | |
| 1095 RangeError.checkValueInInterval(index, 0, list.length, "index"); | |
| 1096 if (iterable is! EfficientLength) { | |
| 1097 iterable = iterable.toList(growable: false); | |
| 1098 } | |
| 1099 int insertionLength = iterable.length; | |
| 1100 list.length += insertionLength; | |
| 1101 list.setRange(index + insertionLength, list.length, list, index); | |
| 1102 for (var element in iterable) { | |
| 1103 list[index++] = element; | |
| 1104 } | |
| 1105 } | |
| 1106 | |
| 1107 static void setAllList(List list, int index, Iterable iterable) { | |
| 1108 RangeError.checkValueInInterval(index, 0, list.length, "index"); | |
| 1109 for (var element in iterable) { | |
| 1110 list[index++] = element; | |
| 1111 } | |
| 1112 } | |
| 1113 | |
| 1114 Map<int, T> asMapList(List l) { | |
| 1115 return new ListMapView<T>(l); | |
| 1116 } | |
| 1117 | |
| 1118 static bool setContainsAll(Set set, Iterable other) { | |
| 1119 for (var element in other) { | |
| 1120 if (!set.contains(element)) return false; | |
| 1121 } | |
| 1122 return true; | |
| 1123 } | |
| 1124 | |
| 1125 static Set setIntersection(Set set, Set other, Set result) { | |
| 1126 Set smaller; | |
| 1127 Set larger; | |
| 1128 if (set.length < other.length) { | |
| 1129 smaller = set; | |
| 1130 larger = other; | |
| 1131 } else { | |
| 1132 smaller = other; | |
| 1133 larger = set; | |
| 1134 } | |
| 1135 for (var element in smaller) { | |
| 1136 if (larger.contains(element)) { | |
| 1137 result.add(element); | |
| 1138 } | |
| 1139 } | |
| 1140 return result; | |
| 1141 } | |
| 1142 | |
| 1143 static Set setUnion(Set set, Set other, Set result) { | |
| 1144 result.addAll(set); | |
| 1145 result.addAll(other); | |
| 1146 return result; | |
| 1147 } | |
| 1148 | |
| 1149 static Set setDifference(Set set, Set other, Set result) { | |
| 1150 for (var element in set) { | |
| 1151 if (!other.contains(element)) { | |
| 1152 result.add(element); | |
| 1153 } | |
| 1154 } | |
| 1155 return result; | |
| 1156 } | |
| 1157 } | |
| 1158 | |
| 1159 /** | |
| 1160 * Creates errors throw by [Iterable] when the element count is wrong. | |
| 1161 */ | |
| 1162 abstract class IterableElementError { | |
| 1163 /** Error thrown thrown by, e.g., [Iterable.first] when there is no result. */ | |
| 1164 static StateError noElement() => new StateError("No element"); | |
| 1165 /** Error thrown by, e.g., [Iterable.single] if there are too many results. */ | |
| 1166 static StateError tooMany() => new StateError("Too many elements"); | |
| 1167 /** Error thrown by, e.g., [List.setRange] if there are too few elements. */ | |
| 1168 static StateError tooFew() => new StateError("Too few elements"); | |
| 1169 } | |
| OLD | NEW |