| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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; | 5 part of dart.collection; |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * Abstract implementation of a list. | 8 * Abstract implementation of a list. |
| 9 * | 9 * |
| 10 * All operations are defined in terms of `length`, `operator[]`, | 10 * All operations are defined in terms of `length`, `operator[]`, |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 63 throw new ConcurrentModificationError(this); | 63 throw new ConcurrentModificationError(this); |
| 64 } | 64 } |
| 65 } | 65 } |
| 66 } | 66 } |
| 67 | 67 |
| 68 bool get isEmpty => length == 0; | 68 bool get isEmpty => length == 0; |
| 69 | 69 |
| 70 bool get isNotEmpty => !isEmpty; | 70 bool get isNotEmpty => !isEmpty; |
| 71 | 71 |
| 72 E get first { | 72 E get first { |
| 73 if (length == 0) throw new StateError("No elements"); | 73 if (length == 0) throw IterableElementError.noElement(); |
| 74 return this[0]; | 74 return this[0]; |
| 75 } | 75 } |
| 76 | 76 |
| 77 E get last { | 77 E get last { |
| 78 if (length == 0) throw new StateError("No elements"); | 78 if (length == 0) throw IterableElementError.noElement(); |
| 79 return this[length - 1]; | 79 return this[length - 1]; |
| 80 } | 80 } |
| 81 | 81 |
| 82 void set last(E value) { |
| 83 if (length == 0) throw IterableElementError.noElement(); |
| 84 this[length - 1] = value; |
| 85 } |
| 86 |
| 82 E get single { | 87 E get single { |
| 83 if (length == 0) throw new StateError("No elements"); | 88 if (length == 0) throw IterableElementError.noElement(); |
| 84 if (length > 1) throw new StateError("Too many elements"); | 89 if (length > 1) throw IterableElementError.tooMany(); |
| 85 return this[0]; | 90 return this[0]; |
| 86 } | 91 } |
| 87 | 92 |
| 88 bool contains(Object element) { | 93 bool contains(Object element) { |
| 89 int length = this.length; | 94 int length = this.length; |
| 90 for (int i = 0; i < this.length; i++) { | 95 for (int i = 0; i < this.length; i++) { |
| 91 if (this[i] == element) return true; | 96 if (this[i] == element) return true; |
| 92 if (length != this.length) { | 97 if (length != this.length) { |
| 93 throw new ConcurrentModificationError(this); | 98 throw new ConcurrentModificationError(this); |
| 94 } | 99 } |
| (...skipping 26 matching lines...) Expand all Loading... |
| 121 dynamic firstWhere(bool test(E element), { Object orElse() }) { | 126 dynamic firstWhere(bool test(E element), { Object orElse() }) { |
| 122 int length = this.length; | 127 int length = this.length; |
| 123 for (int i = 0; i < length; i++) { | 128 for (int i = 0; i < length; i++) { |
| 124 E element = this[i]; | 129 E element = this[i]; |
| 125 if (test(element)) return element; | 130 if (test(element)) return element; |
| 126 if (length != this.length) { | 131 if (length != this.length) { |
| 127 throw new ConcurrentModificationError(this); | 132 throw new ConcurrentModificationError(this); |
| 128 } | 133 } |
| 129 } | 134 } |
| 130 if (orElse != null) return orElse(); | 135 if (orElse != null) return orElse(); |
| 131 throw new StateError("No matching element"); | 136 throw IterableElementError.noElement(); |
| 132 } | 137 } |
| 133 | 138 |
| 134 dynamic lastWhere(bool test(E element), { Object orElse() }) { | 139 dynamic lastWhere(bool test(E element), { Object orElse() }) { |
| 135 int length = this.length; | 140 int length = this.length; |
| 136 for (int i = length - 1; i >= 0; i--) { | 141 for (int i = length - 1; i >= 0; i--) { |
| 137 E element = this[i]; | 142 E element = this[i]; |
| 138 if (test(element)) return element; | 143 if (test(element)) return element; |
| 139 if (length != this.length) { | 144 if (length != this.length) { |
| 140 throw new ConcurrentModificationError(this); | 145 throw new ConcurrentModificationError(this); |
| 141 } | 146 } |
| 142 } | 147 } |
| 143 if (orElse != null) return orElse(); | 148 if (orElse != null) return orElse(); |
| 144 throw new StateError("No matching element"); | 149 throw IterableElementError.noElement(); |
| 145 } | 150 } |
| 146 | 151 |
| 147 E singleWhere(bool test(E element)) { | 152 E singleWhere(bool test(E element)) { |
| 148 int length = this.length; | 153 int length = this.length; |
| 149 E match = null; | 154 E match = null; |
| 150 bool matchFound = false; | 155 bool matchFound = false; |
| 151 for (int i = 0; i < length; i++) { | 156 for (int i = 0; i < length; i++) { |
| 152 E element = this[i]; | 157 E element = this[i]; |
| 153 if (test(element)) { | 158 if (test(element)) { |
| 154 if (matchFound) { | 159 if (matchFound) { |
| 155 throw new StateError("More than one matching element"); | 160 throw IterableElementError.tooMany(); |
| 156 } | 161 } |
| 157 matchFound = true; | 162 matchFound = true; |
| 158 match = element; | 163 match = element; |
| 159 } | 164 } |
| 160 if (length != this.length) { | 165 if (length != this.length) { |
| 161 throw new ConcurrentModificationError(this); | 166 throw new ConcurrentModificationError(this); |
| 162 } | 167 } |
| 163 } | 168 } |
| 164 if (matchFound) return match; | 169 if (matchFound) return match; |
| 165 throw new StateError("No matching element"); | 170 throw IterableElementError.noElement(); |
| 166 } | 171 } |
| 167 | 172 |
| 168 String join([String separator = ""]) { | 173 String join([String separator = ""]) { |
| 169 if (length == 0) return ""; | 174 if (length == 0) return ""; |
| 170 StringBuffer buffer = new StringBuffer()..writeAll(this, separator); | 175 StringBuffer buffer = new StringBuffer()..writeAll(this, separator); |
| 171 return buffer.toString(); | 176 return buffer.toString(); |
| 172 } | 177 } |
| 173 | 178 |
| 174 Iterable<E> where(bool test(E element)) => new WhereIterable<E>(this, test); | 179 Iterable<E> where(bool test(E element)) => new WhereIterable<E>(this, test); |
| 175 | 180 |
| 176 Iterable map(f(E element)) => new MappedListIterable(this, f); | 181 Iterable map(f(E element)) => new MappedListIterable(this, f); |
| 177 | 182 |
| 178 Iterable expand(Iterable f(E element)) => | 183 Iterable expand(Iterable f(E element)) => |
| 179 new ExpandIterable<E, dynamic>(this, f); | 184 new ExpandIterable<E, dynamic>(this, f); |
| 180 | 185 |
| 181 E reduce(E combine(E previousValue, E element)) { | 186 E reduce(E combine(E previousValue, E element)) { |
| 182 if (length == 0) throw new StateError("No elements"); | 187 if (length == 0) throw IterableElementError.noElement(); |
| 183 E value = this[0]; | 188 E value = this[0]; |
| 184 for (int i = 1; i < length; i++) { | 189 for (int i = 1; i < length; i++) { |
| 185 value = combine(value, this[i]); | 190 value = combine(value, this[i]); |
| 186 } | 191 } |
| 187 return value; | 192 return value; |
| 188 } | 193 } |
| 189 | 194 |
| 190 fold(var initialValue, combine(var previousValue, E element)) { | 195 fold(var initialValue, combine(var previousValue, E element)) { |
| 191 var value = initialValue; | 196 var value = initialValue; |
| 192 int length = this.length; | 197 int length = this.length; |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 281 source.length = retained.length; | 286 source.length = retained.length; |
| 282 } | 287 } |
| 283 } | 288 } |
| 284 | 289 |
| 285 void clear() { this.length = 0; } | 290 void clear() { this.length = 0; } |
| 286 | 291 |
| 287 // List interface. | 292 // List interface. |
| 288 | 293 |
| 289 E removeLast() { | 294 E removeLast() { |
| 290 if (length == 0) { | 295 if (length == 0) { |
| 291 throw new StateError("No elements"); | 296 throw IterableElementError.noElement(); |
| 292 } | 297 } |
| 293 E result = this[length - 1]; | 298 E result = this[length - 1]; |
| 294 length--; | 299 length--; |
| 295 return result; | 300 return result; |
| 296 } | 301 } |
| 297 | 302 |
| 298 void sort([int compare(E a, E b)]) { | 303 void sort([int compare(E a, E b)]) { |
| 299 if (compare == null) { | 304 if (compare == null) { |
| 300 var defaultCompare = Comparable.compare; | 305 var defaultCompare = Comparable.compare; |
| 301 compare = defaultCompare; | 306 compare = defaultCompare; |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 369 int otherStart; | 374 int otherStart; |
| 370 // TODO(floitsch): Make this accept more. | 375 // TODO(floitsch): Make this accept more. |
| 371 if (iterable is List) { | 376 if (iterable is List) { |
| 372 otherList = iterable; | 377 otherList = iterable; |
| 373 otherStart = skipCount; | 378 otherStart = skipCount; |
| 374 } else { | 379 } else { |
| 375 otherList = iterable.skip(skipCount).toList(growable: false); | 380 otherList = iterable.skip(skipCount).toList(growable: false); |
| 376 otherStart = 0; | 381 otherStart = 0; |
| 377 } | 382 } |
| 378 if (otherStart + length > otherList.length) { | 383 if (otherStart + length > otherList.length) { |
| 379 throw new StateError("Not enough elements"); | 384 throw IterableElementError.noElement(); |
| 380 } | 385 } |
| 381 if (otherStart < start) { | 386 if (otherStart < start) { |
| 382 // Copy backwards to ensure correct copy if [from] is this. | 387 // Copy backwards to ensure correct copy if [from] is this. |
| 383 for (int i = length - 1; i >= 0; i--) { | 388 for (int i = length - 1; i >= 0; i--) { |
| 384 this[start + i] = otherList[otherStart + i]; | 389 this[start + i] = otherList[otherStart + i]; |
| 385 } | 390 } |
| 386 } else { | 391 } else { |
| 387 for (int i = 0; i < length; i++) { | 392 for (int i = 0; i < length; i++) { |
| 388 this[start + i] = otherList[otherStart + i]; | 393 this[start + i] = otherList[otherStart + i]; |
| 389 } | 394 } |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 502 for (E element in iterable) { | 507 for (E element in iterable) { |
| 503 this[index++] = element; | 508 this[index++] = element; |
| 504 } | 509 } |
| 505 } | 510 } |
| 506 } | 511 } |
| 507 | 512 |
| 508 Iterable<E> get reversed => new ReversedListIterable(this); | 513 Iterable<E> get reversed => new ReversedListIterable(this); |
| 509 | 514 |
| 510 String toString() => IterableBase.iterableToFullString(this, '[', ']'); | 515 String toString() => IterableBase.iterableToFullString(this, '[', ']'); |
| 511 } | 516 } |
| OLD | NEW |