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 * `ListBase` can be used as a base class for implementing the `List` interface. | 10 * `ListBase` can be used as a base class for implementing the `List` interface. |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
113 int length = this.length; | 113 int length = this.length; |
114 for (int i = 0; i < length; i++) { | 114 for (int i = 0; i < length; i++) { |
115 if (test(this[i])) return true; | 115 if (test(this[i])) return true; |
116 if (length != this.length) { | 116 if (length != this.length) { |
117 throw new ConcurrentModificationError(this); | 117 throw new ConcurrentModificationError(this); |
118 } | 118 } |
119 } | 119 } |
120 return false; | 120 return false; |
121 } | 121 } |
122 | 122 |
123 E firstWhere(bool test(E element), { E orElse() }) { | 123 E firstWhere(bool test(E element), {E orElse()}) { |
124 int length = this.length; | 124 int length = this.length; |
125 for (int i = 0; i < length; i++) { | 125 for (int i = 0; i < length; i++) { |
126 E element = this[i]; | 126 E element = this[i]; |
127 if (test(element)) return element; | 127 if (test(element)) return element; |
128 if (length != this.length) { | 128 if (length != this.length) { |
129 throw new ConcurrentModificationError(this); | 129 throw new ConcurrentModificationError(this); |
130 } | 130 } |
131 } | 131 } |
132 if (orElse != null) return orElse(); | 132 if (orElse != null) return orElse(); |
133 throw IterableElementError.noElement(); | 133 throw IterableElementError.noElement(); |
134 } | 134 } |
135 | 135 |
136 E lastWhere(bool test(E element), { E orElse() }) { | 136 E lastWhere(bool test(E element), {E orElse()}) { |
137 int length = this.length; | 137 int length = this.length; |
138 for (int i = length - 1; i >= 0; i--) { | 138 for (int i = length - 1; i >= 0; i--) { |
139 E element = this[i]; | 139 E element = this[i]; |
140 if (test(element)) return element; | 140 if (test(element)) return element; |
141 if (length != this.length) { | 141 if (length != this.length) { |
142 throw new ConcurrentModificationError(this); | 142 throw new ConcurrentModificationError(this); |
143 } | 143 } |
144 } | 144 } |
145 if (orElse != null) return orElse(); | 145 if (orElse != null) return orElse(); |
146 throw IterableElementError.noElement(); | 146 throw IterableElementError.noElement(); |
(...skipping 21 matching lines...) Expand all Loading... |
168 } | 168 } |
169 | 169 |
170 String join([String separator = ""]) { | 170 String join([String separator = ""]) { |
171 if (length == 0) return ""; | 171 if (length == 0) return ""; |
172 StringBuffer buffer = new StringBuffer()..writeAll(this, separator); | 172 StringBuffer buffer = new StringBuffer()..writeAll(this, separator); |
173 return buffer.toString(); | 173 return buffer.toString(); |
174 } | 174 } |
175 | 175 |
176 Iterable<E> where(bool test(E element)) => new WhereIterable<E>(this, test); | 176 Iterable<E> where(bool test(E element)) => new WhereIterable<E>(this, test); |
177 | 177 |
178 Iterable<T> map<T>(T f(E element)) => | 178 Iterable<T> map<T>(T f(E element)) => new MappedListIterable<E, T>(this, f); |
179 new MappedListIterable<E, T>(this, f); | |
180 | 179 |
181 Iterable<T> expand<T>(Iterable<T> f(E element)) => | 180 Iterable<T> |
182 new ExpandIterable<E, T>(this, f); | 181 expand<T>(Iterable<T> f(E element)) => new ExpandIterable<E, T>(this, f); |
183 | 182 |
184 E reduce(E combine(E previousValue, E element)) { | 183 E reduce(E combine(E previousValue, E element)) { |
185 int length = this.length; | 184 int length = this.length; |
186 if (length == 0) throw IterableElementError.noElement(); | 185 if (length == 0) throw IterableElementError.noElement(); |
187 E value = this[0]; | 186 E value = this[0]; |
188 for (int i = 1; i < length; i++) { | 187 for (int i = 1; i < length; i++) { |
189 value = combine(value, this[i]); | 188 value = combine(value, this[i]); |
190 if (length != this.length) { | 189 if (length != this.length) { |
191 throw new ConcurrentModificationError(this); | 190 throw new ConcurrentModificationError(this); |
192 } | 191 } |
193 } | 192 } |
194 return value; | 193 return value; |
195 } | 194 } |
196 | 195 |
197 T fold<T>(T initialValue, | 196 T fold<T>(T initialValue, T combine(T previousValue, E element)) { |
198 T combine(T previousValue, E element)) { | |
199 var value = initialValue; | 197 var value = initialValue; |
200 int length = this.length; | 198 int length = this.length; |
201 for (int i = 0; i < length; i++) { | 199 for (int i = 0; i < length; i++) { |
202 value = combine(value, this[i]); | 200 value = combine(value, this[i]); |
203 if (length != this.length) { | 201 if (length != this.length) { |
204 throw new ConcurrentModificationError(this); | 202 throw new ConcurrentModificationError(this); |
205 } | 203 } |
206 } | 204 } |
207 return value; | 205 return value; |
208 } | 206 } |
209 | 207 |
210 Iterable<E> skip(int count) => new SubListIterable<E>(this, count, null); | 208 Iterable<E> skip(int count) => new SubListIterable<E>(this, count, null); |
211 | 209 |
212 Iterable<E> skipWhile(bool test(E element)) { | 210 Iterable<E> skipWhile(bool test(E element)) { |
213 return new SkipWhileIterable<E>(this, test); | 211 return new SkipWhileIterable<E>(this, test); |
214 } | 212 } |
215 | 213 |
216 Iterable<E> take(int count) => new SubListIterable<E>(this, 0, count); | 214 Iterable<E> take(int count) => new SubListIterable<E>(this, 0, count); |
217 | 215 |
218 Iterable<E> takeWhile(bool test(E element)) { | 216 Iterable<E> takeWhile(bool test(E element)) { |
219 return new TakeWhileIterable<E>(this, test); | 217 return new TakeWhileIterable<E>(this, test); |
220 } | 218 } |
221 | 219 |
222 List<E> toList({ bool growable: true }) { | 220 List<E> toList({bool growable: true}) { |
223 List<E> result; | 221 List<E> result; |
224 if (growable) { | 222 if (growable) { |
225 result = new List<E>()..length = length; | 223 result = new List<E>()..length = length; |
226 } else { | 224 } else { |
227 result = new List<E>(length); | 225 result = new List<E>(length); |
228 } | 226 } |
229 for (int i = 0; i < length; i++) { | 227 for (int i = 0; i < length; i++) { |
230 result[i] = this[i]; | 228 result[i] = this[i]; |
231 } | 229 } |
232 return result; | 230 return result; |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
285 if (length != this.length) { | 283 if (length != this.length) { |
286 throw new ConcurrentModificationError(this); | 284 throw new ConcurrentModificationError(this); |
287 } | 285 } |
288 } | 286 } |
289 if (retained.length != this.length) { | 287 if (retained.length != this.length) { |
290 this.setRange(0, retained.length, retained); | 288 this.setRange(0, retained.length, retained); |
291 this.length = retained.length; | 289 this.length = retained.length; |
292 } | 290 } |
293 } | 291 } |
294 | 292 |
295 void clear() { this.length = 0; } | 293 void clear() { |
| 294 this.length = 0; |
| 295 } |
296 | 296 |
297 // List interface. | 297 // List interface. |
298 | 298 |
299 E removeLast() { | 299 E removeLast() { |
300 if (length == 0) { | 300 if (length == 0) { |
301 throw IterableElementError.noElement(); | 301 throw IterableElementError.noElement(); |
302 } | 302 } |
303 E result = this[length - 1]; | 303 E result = this[length - 1]; |
304 length--; | 304 length--; |
305 return result; | 305 return result; |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
405 int insertEnd = start + insertLength; | 405 int insertEnd = start + insertLength; |
406 int newLength = this.length - delta; | 406 int newLength = this.length - delta; |
407 this.setRange(start, insertEnd, newContents); | 407 this.setRange(start, insertEnd, newContents); |
408 if (delta != 0) { | 408 if (delta != 0) { |
409 this.setRange(insertEnd, newLength, this, end); | 409 this.setRange(insertEnd, newLength, this, end); |
410 this.length = newLength; | 410 this.length = newLength; |
411 } | 411 } |
412 } else { | 412 } else { |
413 int delta = insertLength - removeLength; | 413 int delta = insertLength - removeLength; |
414 int newLength = this.length + delta; | 414 int newLength = this.length + delta; |
415 int insertEnd = start + insertLength; // aka. end + delta. | 415 int insertEnd = start + insertLength; // aka. end + delta. |
416 this.length = newLength; | 416 this.length = newLength; |
417 this.setRange(insertEnd, newLength, this, end); | 417 this.setRange(insertEnd, newLength, this, end); |
418 this.setRange(start, insertEnd, newContents); | 418 this.setRange(start, insertEnd, newContents); |
419 } | 419 } |
420 } | 420 } |
421 | 421 |
422 int indexOf(Object element, [int startIndex = 0]) { | 422 int indexOf(Object element, [int startIndex = 0]) { |
423 if (startIndex >= this.length) { | 423 if (startIndex >= this.length) { |
424 return -1; | 424 return -1; |
425 } | 425 } |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
507 for (E element in iterable) { | 507 for (E element in iterable) { |
508 this[index++] = element; | 508 this[index++] = element; |
509 } | 509 } |
510 } | 510 } |
511 } | 511 } |
512 | 512 |
513 Iterable<E> get reversed => new ReversedListIterable<E>(this); | 513 Iterable<E> get reversed => new ReversedListIterable<E>(this); |
514 | 514 |
515 String toString() => IterableBase.iterableToFullString(this, '[', ']'); | 515 String toString() => IterableBase.iterableToFullString(this, '[', ']'); |
516 } | 516 } |
OLD | NEW |