| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 | 5 |
| 6 // TODO(srdjan): Use shared array implementation. | 6 // TODO(srdjan): Use shared array implementation. |
| 7 class _ObjectArray<E> implements List<E> { | 7 class _ObjectArray<E> implements List<E> { |
| 8 | 8 |
| 9 factory _ObjectArray(int length) native "ObjectArray_allocate"; | 9 factory _ObjectArray(int length) native "ObjectArray_allocate"; |
| 10 | 10 |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 54 if (length == 0) return []; | 54 if (length == 0) return []; |
| 55 Arrays.rangeCheck(this, start, length); | 55 Arrays.rangeCheck(this, start, length); |
| 56 List list = new _GrowableObjectArray<E>.withCapacity(length); | 56 List list = new _GrowableObjectArray<E>.withCapacity(length); |
| 57 list.length = length; | 57 list.length = length; |
| 58 Arrays.copy(this, start, list, 0, length); | 58 Arrays.copy(this, start, list, 0, length); |
| 59 return list; | 59 return list; |
| 60 } | 60 } |
| 61 | 61 |
| 62 // Collection interface. | 62 // Collection interface. |
| 63 | 63 |
| 64 bool contains(E element) => Collections.contains(this, element); | 64 bool contains(E element) { |
| 65 return Collections.contains(this, element); |
| 66 } |
| 65 | 67 |
| 66 void forEach(f(E element)) { | 68 void forEach(f(E element)) { |
| 67 Collections.forEach(this, f); | 69 Collections.forEach(this, f); |
| 68 } | 70 } |
| 69 | 71 |
| 70 Collection map(f(E element)) { | 72 String join([String separator]) { |
| 71 return Collections.map( | 73 return Collections.join(this, separator); |
| 72 this, new _GrowableObjectArray.withCapacity(length), f); | 74 } |
| 75 |
| 76 List mappedBy(f(E element)) { |
| 77 return new MappedList<E, dynamic>(this, f); |
| 73 } | 78 } |
| 74 | 79 |
| 75 reduce(initialValue, combine(previousValue, E element)) { | 80 reduce(initialValue, combine(previousValue, E element)) { |
| 76 return Collections.reduce(this, initialValue, combine); | 81 return Collections.reduce(this, initialValue, combine); |
| 77 } | 82 } |
| 78 | 83 |
| 79 Collection<E> filter(bool f(E element)) { | 84 Iterable<E> where(bool f(E element)) { |
| 80 return Collections.filter(this, new _GrowableObjectArray<E>(), f); | 85 return new WhereIterable<E>(this, f); |
| 86 } |
| 87 |
| 88 List<E> take(int n) { |
| 89 return new ListView<E>(this, 0, n); |
| 90 } |
| 91 |
| 92 Iterable<E> takeWhile(bool test(E value)) { |
| 93 return new TakeWhileIterable<E>(this, test); |
| 94 } |
| 95 |
| 96 List<E> skip(int n) { |
| 97 return new ListView<E>(this, n, null); |
| 98 } |
| 99 |
| 100 Iterable<E> skipWhile(bool test(E value)) { |
| 101 return new SkipWhileIterable<E>(this, test); |
| 81 } | 102 } |
| 82 | 103 |
| 83 bool every(bool f(E element)) { | 104 bool every(bool f(E element)) { |
| 84 return Collections.every(this, f); | 105 return Collections.every(this, f); |
| 85 } | 106 } |
| 86 | 107 |
| 87 bool some(bool f(E element)) { | 108 bool any(bool f(E element)) { |
| 88 return Collections.some(this, f); | 109 return Collections.any(this, f); |
| 110 } |
| 111 |
| 112 E firstMatching(bool test(E value), {E orElse()}) { |
| 113 return Collections.firstMatching(this, test, orElse); |
| 114 } |
| 115 |
| 116 E lastMatching(bool test(E value), {E orElse()}) { |
| 117 return Collections.lastMatchingInList(this, test, orElse); |
| 118 } |
| 119 |
| 120 E singleMatching(bool test(E value)) { |
| 121 return Collections.singleMatching(this, test); |
| 122 } |
| 123 |
| 124 E elementAt(int index) { |
| 125 return this[index]; |
| 89 } | 126 } |
| 90 | 127 |
| 91 bool get isEmpty { | 128 bool get isEmpty { |
| 92 return this.length == 0; | 129 return this.length == 0; |
| 93 } | 130 } |
| 94 | 131 |
| 95 void sort([int compare(E a, E b)]) { | 132 void sort([int compare(E a, E b)]) { |
| 96 if (compare == null) compare = Comparable.compare; | 133 if (compare == null) compare = Comparable.compare; |
| 97 _Sort.sort(this, compare); | 134 _Sort.sort(this, compare); |
| 98 } | 135 } |
| 99 | 136 |
| 100 int indexOf(E element, [int start = 0]) { | 137 int indexOf(E element, [int start = 0]) { |
| 101 return Arrays.indexOf(this, element, start, this.length); | 138 return Arrays.indexOf(this, element, start, this.length); |
| 102 } | 139 } |
| 103 | 140 |
| 104 int lastIndexOf(E element, [int start = null]) { | 141 int lastIndexOf(E element, [int start = null]) { |
| 105 if (start == null) start = length - 1; | 142 if (start == null) start = length - 1; |
| 106 return Arrays.lastIndexOf(this, element, start); | 143 return Arrays.lastIndexOf(this, element, start); |
| 107 } | 144 } |
| 108 | 145 |
| 109 Iterator<E> iterator() { | 146 Iterator<E> get iterator { |
| 110 return new _FixedSizeArrayIterator<E>(this); | 147 return new _FixedSizeArrayIterator<E>(this); |
| 111 } | 148 } |
| 112 | 149 |
| 113 void add(E element) { | 150 void add(E element) { |
| 114 throw new UnsupportedError( | 151 throw new UnsupportedError( |
| 115 "Cannot add to a non-extendable array"); | 152 "Cannot add to a non-extendable array"); |
| 116 } | 153 } |
| 117 | 154 |
| 118 void addLast(E element) { | 155 void addLast(E element) { |
| 119 add(element); | 156 add(element); |
| 120 } | 157 } |
| 121 | 158 |
| 122 void addAll(Collection<E> elements) { | 159 void addAll(Iterable<E> iterable) { |
| 123 throw new UnsupportedError( | 160 throw new UnsupportedError( |
| 124 "Cannot add to a non-extendable array"); | 161 "Cannot add to a non-extendable array"); |
| 125 } | 162 } |
| 126 | 163 |
| 127 void clear() { | 164 void clear() { |
| 128 throw new UnsupportedError( | 165 throw new UnsupportedError( |
| 129 "Cannot clear a non-extendable array"); | 166 "Cannot clear a non-extendable array"); |
| 130 } | 167 } |
| 131 | 168 |
| 132 void set length(int length) { | 169 void set length(int length) { |
| 133 throw new UnsupportedError( | 170 throw new UnsupportedError( |
| 134 "Cannot change the length of a non-extendable array"); | 171 "Cannot change the length of a non-extendable array"); |
| 135 } | 172 } |
| 136 | 173 |
| 137 E removeLast() { | 174 E removeLast() { |
| 138 throw new UnsupportedError( | 175 throw new UnsupportedError( |
| 139 "Cannot remove in a non-extendable array"); | 176 "Cannot remove in a non-extendable array"); |
| 140 } | 177 } |
| 141 | 178 |
| 142 E get first { | 179 E get first { |
| 143 return this[0]; | 180 if (length > 0) return this[0]; |
| 181 throw new StateError("No elements"); |
| 144 } | 182 } |
| 145 | 183 |
| 146 E get last { | 184 E get last { |
| 147 return this[length - 1]; | 185 if (length > 0) return this[length - 1]; |
| 186 throw new StateError("No elements"); |
| 187 } |
| 188 |
| 189 E get single { |
| 190 if (length == 1) return this[0]; |
| 191 if (length == 0) throw new StateError("No elements"); |
| 192 throw new StateError("More than one element"); |
| 193 } |
| 194 |
| 195 E min([int compare(E a, E b)]) => Collections.min(this, compare); |
| 196 |
| 197 E max([int compare(E a, E b)]) => Collections.max(this, compare); |
| 198 |
| 199 List<E> toList() { |
| 200 return new List<E>.from(this); |
| 201 } |
| 202 |
| 203 Set<E> toSet() { |
| 204 return new Set<E>.from(this); |
| 148 } | 205 } |
| 149 } | 206 } |
| 150 | 207 |
| 151 | 208 |
| 152 // This is essentially the same class as _ObjectArray, but it does not | 209 // This is essentially the same class as _ObjectArray, but it does not |
| 153 // permit any modification of array elements from Dart code. We use | 210 // permit any modification of array elements from Dart code. We use |
| 154 // this class for arrays constructed from Dart array literals. | 211 // this class for arrays constructed from Dart array literals. |
| 155 // TODO(hausner): We should consider the trade-offs between two | 212 // TODO(hausner): We should consider the trade-offs between two |
| 156 // classes (and inline cache misses) versus a field in the native | 213 // classes (and inline cache misses) versus a field in the native |
| 157 // implementation (checks when modifying). We should keep watching | 214 // implementation (checks when modifying). We should keep watching |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 201 if (length == 0) return []; | 258 if (length == 0) return []; |
| 202 Arrays.rangeCheck(this, start, length); | 259 Arrays.rangeCheck(this, start, length); |
| 203 List list = new List<E>(); | 260 List list = new List<E>(); |
| 204 list.length = length; | 261 list.length = length; |
| 205 Arrays.copy(this, start, list, 0, length); | 262 Arrays.copy(this, start, list, 0, length); |
| 206 return list; | 263 return list; |
| 207 } | 264 } |
| 208 | 265 |
| 209 // Collection interface. | 266 // Collection interface. |
| 210 | 267 |
| 211 bool contains(E element) => Collections.contains(this, element); | 268 bool contains(E element) { |
| 269 return Collections.contains(this, element); |
| 270 } |
| 212 | 271 |
| 213 void forEach(f(E element)) { | 272 void forEach(f(E element)) { |
| 214 Collections.forEach(this, f); | 273 Collections.forEach(this, f); |
| 215 } | 274 } |
| 216 | 275 |
| 217 Collection map(f(E element)) { | 276 List mappedBy(f(E element)) { |
| 218 return Collections.map( | 277 return new MappedList<E, dynamic>(this, f); |
| 219 this, new _GrowableObjectArray.withCapacity(length), f); | 278 } |
| 279 |
| 280 String join([String separator]) { |
| 281 return Collections.join(this, separator); |
| 220 } | 282 } |
| 221 | 283 |
| 222 reduce(initialValue, combine(previousValue, E element)) { | 284 reduce(initialValue, combine(previousValue, E element)) { |
| 223 return Collections.reduce(this, initialValue, combine); | 285 return Collections.reduce(this, initialValue, combine); |
| 224 } | 286 } |
| 225 | 287 |
| 226 Collection<E> filter(bool f(E element)) { | 288 Iterable<E> where(bool f(E element)) { |
| 227 return Collections.filter(this, new _GrowableObjectArray<E>(), f); | 289 return new WhereIterable<E>(this, f); |
| 290 } |
| 291 |
| 292 List<E> take(int n) { |
| 293 return new ListView<E>(this, 0, n); |
| 294 } |
| 295 |
| 296 Iterable<E> takeWhile(bool test(E value)) { |
| 297 return new TakeWhileIterable<E>(this, test); |
| 298 } |
| 299 |
| 300 List<E> skip(int n) { |
| 301 return new ListView<E>(this, n, null); |
| 302 } |
| 303 |
| 304 Iterable<E> skipWhile(bool test(E value)) { |
| 305 return new SkipWhileIterable<E>(this, test); |
| 228 } | 306 } |
| 229 | 307 |
| 230 bool every(bool f(E element)) { | 308 bool every(bool f(E element)) { |
| 231 return Collections.every(this, f); | 309 return Collections.every(this, f); |
| 232 } | 310 } |
| 233 | 311 |
| 234 bool some(bool f(E element)) { | 312 bool any(bool f(E element)) { |
| 235 return Collections.some(this, f); | 313 return Collections.any(this, f); |
| 314 } |
| 315 |
| 316 E firstMatching(bool test(E value), {E orElse()}) { |
| 317 return Collections.firstMatching(this, test, orElse); |
| 318 } |
| 319 |
| 320 E lastMatching(bool test(E value), {E orElse()}) { |
| 321 return Collections.lastMatchingInList(this, test, orElse); |
| 322 } |
| 323 |
| 324 E singleMatching(bool test(E value)) { |
| 325 return Collections.singleMatching(this, test); |
| 326 } |
| 327 |
| 328 E elementAt(int index) { |
| 329 return this[index]; |
| 236 } | 330 } |
| 237 | 331 |
| 238 bool get isEmpty { | 332 bool get isEmpty { |
| 239 return this.length == 0; | 333 return this.length == 0; |
| 240 } | 334 } |
| 241 | 335 |
| 242 void sort([int compare(E a, E b)]) { | 336 void sort([int compare(E a, E b)]) { |
| 243 throw new UnsupportedError( | 337 throw new UnsupportedError( |
| 244 "Cannot modify an immutable array"); | 338 "Cannot modify an immutable array"); |
| 245 } | 339 } |
| 246 | 340 |
| 247 String toString() { | 341 String toString() { |
| 248 return Collections.collectionToString(this); | 342 return Collections.collectionToString(this); |
| 249 } | 343 } |
| 250 | 344 |
| 251 int indexOf(E element, [int start = 0]) { | 345 int indexOf(E element, [int start = 0]) { |
| 252 return Arrays.indexOf(this, element, start, this.length); | 346 return Arrays.indexOf(this, element, start, this.length); |
| 253 } | 347 } |
| 254 | 348 |
| 255 int lastIndexOf(E element, [int start = null]) { | 349 int lastIndexOf(E element, [int start = null]) { |
| 256 if (start == null) start = length - 1; | 350 if (start == null) start = length - 1; |
| 257 return Arrays.lastIndexOf(this, element, start); | 351 return Arrays.lastIndexOf(this, element, start); |
| 258 } | 352 } |
| 259 | 353 |
| 260 Iterator<E> iterator() { | 354 Iterator<E> get iterator { |
| 261 return new _FixedSizeArrayIterator<E>(this); | 355 return new _FixedSizeArrayIterator<E>(this); |
| 262 } | 356 } |
| 263 | 357 |
| 264 void add(E element) { | 358 void add(E element) { |
| 265 throw new UnsupportedError( | 359 throw new UnsupportedError( |
| 266 "Cannot add to an immutable array"); | 360 "Cannot add to an immutable array"); |
| 267 } | 361 } |
| 268 | 362 |
| 269 void addLast(E element) { | 363 void addLast(E element) { |
| 270 add(element); | 364 add(element); |
| 271 } | 365 } |
| 272 | 366 |
| 273 void addAll(Collection<E> elements) { | 367 void addAll(Iterable<E> elements) { |
| 274 throw new UnsupportedError( | 368 throw new UnsupportedError( |
| 275 "Cannot add to an immutable array"); | 369 "Cannot add to an immutable array"); |
| 276 } | 370 } |
| 277 | 371 |
| 278 void clear() { | 372 void clear() { |
| 279 throw new UnsupportedError( | 373 throw new UnsupportedError( |
| 280 "Cannot clear an immutable array"); | 374 "Cannot clear an immutable array"); |
| 281 } | 375 } |
| 282 | 376 |
| 283 void set length(int length) { | 377 void set length(int length) { |
| 284 throw new UnsupportedError( | 378 throw new UnsupportedError( |
| 285 "Cannot change the length of an immutable array"); | 379 "Cannot change the length of an immutable array"); |
| 286 } | 380 } |
| 287 | 381 |
| 288 E removeLast() { | 382 E removeLast() { |
| 289 throw new UnsupportedError( | 383 throw new UnsupportedError( |
| 290 "Cannot remove in a non-extendable array"); | 384 "Cannot remove in a non-extendable array"); |
| 291 } | 385 } |
| 292 | 386 |
| 293 E get first { | 387 E get first { |
| 294 return this[0]; | 388 if (length > 0) return this[0]; |
| 389 throw new StateError("No elements"); |
| 295 } | 390 } |
| 296 | 391 |
| 297 E get last { | 392 E get last { |
| 298 return this[length - 1]; | 393 if (length > 0) return this[length - 1]; |
| 394 throw new StateError("No elements"); |
| 395 } |
| 396 |
| 397 E get single { |
| 398 if (length == 1) return this[0]; |
| 399 if (length == 0) throw new StateError("No elements"); |
| 400 throw new StateError("More than one element"); |
| 401 } |
| 402 |
| 403 E min([int compare(E a, E b)]) => Collections.min(this, compare); |
| 404 |
| 405 E max([int compare(E a, E b)]) => Collections.max(this, compare); |
| 406 |
| 407 List<E> toList() { |
| 408 return new List<E>.from(this); |
| 409 } |
| 410 |
| 411 Set<E> toSet() { |
| 412 return new Set<E>.from(this); |
| 299 } | 413 } |
| 300 } | 414 } |
| 301 | 415 |
| 302 | 416 |
| 303 // Iterator for arrays with fixed size. | 417 // Iterator for arrays with fixed size. |
| 304 class _FixedSizeArrayIterator<E> implements Iterator<E> { | 418 class _FixedSizeArrayIterator<E> implements Iterator<E> { |
| 419 final List<E> _array; |
| 420 final int _length; // Cache array length for faster access. |
| 421 int _position; |
| 422 E _current; |
| 423 |
| 305 _FixedSizeArrayIterator(List array) | 424 _FixedSizeArrayIterator(List array) |
| 306 : _array = array, _length = array.length, _pos = 0 { | 425 : _array = array, _length = array.length, _position = -1 { |
| 307 assert(array is _ObjectArray || array is _ImmutableArray); | 426 assert(array is _ObjectArray || array is _ImmutableArray); |
| 308 } | 427 } |
| 309 | 428 |
| 310 bool get hasNext { | 429 bool moveNext() { |
| 311 return _length > _pos; | 430 int nextPosition = _position + 1; |
| 431 if (nextPosition < _length) { |
| 432 _current = _array[nextPosition]; |
| 433 _position = nextPosition; |
| 434 return true; |
| 435 } |
| 436 _position = _length; |
| 437 _current = null; |
| 438 return false; |
| 312 } | 439 } |
| 313 | 440 |
| 314 E next() { | 441 E get current { |
| 315 if (!hasNext) { | 442 return _current; |
| 316 throw new StateError("No more elements"); | |
| 317 } | |
| 318 return _array[_pos++]; | |
| 319 } | 443 } |
| 320 | |
| 321 final List<E> _array; | |
| 322 final int _length; // Cache array length for faster access. | |
| 323 int _pos; | |
| 324 } | 444 } |
| OLD | NEW |