Chromium Code Reviews| 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 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 98 | 98 |
| 99 int indexOf(E element, [int start = 0]) { | 99 int indexOf(E element, [int start = 0]) { |
| 100 return Arrays.indexOf(this, element, start, this.length); | 100 return Arrays.indexOf(this, element, start, this.length); |
| 101 } | 101 } |
| 102 | 102 |
| 103 int lastIndexOf(E element, [int start = null]) { | 103 int lastIndexOf(E element, [int start = null]) { |
| 104 if (start == null) start = length - 1; | 104 if (start == null) start = length - 1; |
| 105 return Arrays.lastIndexOf(this, element, start); | 105 return Arrays.lastIndexOf(this, element, start); |
| 106 } | 106 } |
| 107 | 107 |
| 108 Iterator<E> iterator() { | 108 Iterator<E> get iterator => new _FixedSizeArrayIterator<E>(this); |
| 109 return new _FixedSizeArrayIterator<E>(this); | |
| 110 } | |
| 111 | 109 |
| 112 void add(E element) { | 110 void add(E element) { |
| 113 throw new UnsupportedError( | 111 throw new UnsupportedError( |
| 114 "Cannot add to a non-extendable array"); | 112 "Cannot add to a non-extendable array"); |
| 115 } | 113 } |
| 116 | 114 |
| 117 void addLast(E element) { | 115 void addLast(E element) { |
| 118 add(element); | 116 add(element); |
| 119 } | 117 } |
| 120 | 118 |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 245 | 243 |
| 246 int indexOf(E element, [int start = 0]) { | 244 int indexOf(E element, [int start = 0]) { |
| 247 return Arrays.indexOf(this, element, start, this.length); | 245 return Arrays.indexOf(this, element, start, this.length); |
| 248 } | 246 } |
| 249 | 247 |
| 250 int lastIndexOf(E element, [int start = null]) { | 248 int lastIndexOf(E element, [int start = null]) { |
| 251 if (start == null) start = length - 1; | 249 if (start == null) start = length - 1; |
| 252 return Arrays.lastIndexOf(this, element, start); | 250 return Arrays.lastIndexOf(this, element, start); |
| 253 } | 251 } |
| 254 | 252 |
| 255 Iterator<E> iterator() { | 253 Iterator<E> get iterator => new _FixedSizeArrayIterator<E>(this); |
| 256 return new _FixedSizeArrayIterator<E>(this); | |
| 257 } | |
| 258 | 254 |
| 259 void add(E element) { | 255 void add(E element) { |
| 260 throw new UnsupportedError( | 256 throw new UnsupportedError( |
| 261 "Cannot add to an immutable array"); | 257 "Cannot add to an immutable array"); |
| 262 } | 258 } |
| 263 | 259 |
| 264 void addLast(E element) { | 260 void addLast(E element) { |
| 265 add(element); | 261 add(element); |
| 266 } | 262 } |
| 267 | 263 |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 287 | 283 |
| 288 E get last { | 284 E get last { |
| 289 return this[length - 1]; | 285 return this[length - 1]; |
| 290 } | 286 } |
| 291 } | 287 } |
| 292 | 288 |
| 293 | 289 |
| 294 // Iterator for arrays with fixed size. | 290 // Iterator for arrays with fixed size. |
| 295 class _FixedSizeArrayIterator<E> implements Iterator<E> { | 291 class _FixedSizeArrayIterator<E> implements Iterator<E> { |
| 296 _FixedSizeArrayIterator(List array) | 292 _FixedSizeArrayIterator(List array) |
| 297 : _array = array, _length = array.length, _pos = 0 { | 293 : _array = array, _length = array.length, _pos = -1 { |
| 298 assert(array is _ObjectArray || array is _ImmutableArray); | 294 assert(array is _ObjectArray || array is _ImmutableArray); |
| 299 } | 295 } |
| 300 | 296 |
| 297 bool moveNext() { | |
| 298 _pos++; | |
| 299 if (_pos < _length) return true; | |
| 300 _pos = _length; | |
| 301 return false; | |
| 302 } | |
| 301 bool get hasNext { | 303 bool get hasNext { |
| 302 return _length > _pos; | 304 return _length > _pos; |
| 303 } | 305 } |
| 304 | 306 |
| 305 E next() { | 307 E get current { |
| 306 if (!hasNext) { | 308 if (0 <= _pos && _pos < _length) { |
| 307 throw new StateError("No more elements"); | 309 return _array[_pos]; |
| 308 } | 310 } |
| 309 return _array[_pos++]; | 311 throw new StateError("No more elements"); |
|
Lasse Reichstein Nielsen
2012/11/15 10:24:37
Wrong message if _pos < 0 and length > 0.
floitsch
2012/11/16 17:51:58
added TODO. this will go away with the new approac
| |
| 310 } | 312 } |
| 311 | 313 |
| 312 final List<E> _array; | 314 final List<E> _array; |
| 313 final int _length; // Cache array length for faster access. | 315 final int _length; // Cache array length for faster access. |
| 314 int _pos; | 316 int _pos; |
| 315 } | 317 } |
| OLD | NEW |