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