| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 class ArrayFactory { | |
| 6 factory Array<E>.from(Iterable<E> other) { | |
| 7 Array<E> array = new Array<E>(); | |
| 8 for (final e in other) { | |
| 9 array.add(e); | |
| 10 } | |
| 11 return array; | |
| 12 } | |
| 13 | |
| 14 factory Array<E>.fromArray(Array<E> other, int startIndex, int endIndex) { | |
| 15 Array array = new Array<E>(); | |
| 16 if (endIndex > other.length) endIndex = other.length; | |
| 17 if (startIndex < 0) startIndex = 0; | |
| 18 int count = endIndex - startIndex; | |
| 19 if (count > 0) { | |
| 20 array.length = count; | |
| 21 Arrays.copy(other, startIndex, array, 0, count); | |
| 22 } | |
| 23 return array; | |
| 24 } | |
| 25 | |
| 26 factory Array<E>([int length = null]) { | |
| 27 bool isFixed = true; | |
| 28 if (length === null) { | |
| 29 length = 0; | |
| 30 isFixed = false; | |
| 31 } else if (length < 0) { | |
| 32 throw new IllegalArgumentException("negative length $length"); | |
| 33 } | |
| 34 // TODO(floitsch): make array creation more efficient. Currently we allocate | |
| 35 // a new TypeToken at every allocation. Either we can optimize them away, | |
| 36 // or we need to find other ways to pass type-information from Dart to JS. | |
| 37 ObjectArray array = _new(new TypeToken<E>(), length); | |
| 38 array._isFixed = isFixed; | |
| 39 return array; | |
| 40 } | |
| 41 | |
| 42 static ObjectArray _new(TypeToken typeToken, int length) native; | |
| 43 } | |
| 44 | |
| 45 | |
| 46 class ListFactory { | 5 class ListFactory { |
| 47 factory List<E>.from(Iterable<E> other) { | 6 factory List<E>.from(Iterable<E> other) { |
| 48 List<E> list = new List<E>(); | 7 List<E> list = new List<E>(); |
| 49 for (final e in other) { | 8 for (final e in other) { |
| 50 list.add(e); | 9 list.add(e); |
| 51 } | 10 } |
| 52 return list; | 11 return list; |
| 53 } | 12 } |
| 54 | 13 |
| 55 // TODO(bak): Until the final transition Array type is needed for other | 14 factory List<E>.fromList(List<E> other, int startIndex, int endIndex) { |
| 56 factory List<E>.fromList(Array<E> other, int startIndex, int endIndex) { | |
| 57 List list = new List<E>(); | 15 List list = new List<E>(); |
| 58 if (endIndex > other.length) endIndex = other.length; | 16 if (endIndex > other.length) endIndex = other.length; |
| 59 if (startIndex < 0) startIndex = 0; | 17 if (startIndex < 0) startIndex = 0; |
| 60 int count = endIndex - startIndex; | 18 int count = endIndex - startIndex; |
| 61 if (count > 0) { | 19 if (count > 0) { |
| 62 list.length = count; | 20 list.length = count; |
| 63 Arrays.copy(other, startIndex, list, 0, count); | 21 Arrays.copy(other, startIndex, list, 0, count); |
| 64 } | 22 } |
| 65 return list; | 23 return list; |
| 66 } | 24 } |
| 67 | 25 |
| 68 factory List<E>([int length = null]) { | 26 factory List<E>([int length = null]) { |
| 69 bool isFixed = true; | 27 bool isFixed = true; |
| 70 if (length === null) { | 28 if (length === null) { |
| 71 length = 0; | 29 length = 0; |
| 72 isFixed = false; | 30 isFixed = false; |
| 73 } else if (length < 0) { | 31 } else if (length < 0) { |
| 74 throw new IllegalArgumentException("negative length $length"); | 32 throw new IllegalArgumentException("negative length $length"); |
| 75 } | 33 } |
| 76 // TODO(floitsch): make array creation more efficient. Currently we allocate | 34 // TODO(floitsch): make list creation more efficient. Currently we allocate |
| 77 // a new TypeToken at every allocation. Either we can optimize them away, | 35 // a new TypeToken at every allocation. Either we can optimize them away, |
| 78 // or we need to find other ways to pass type-information from Dart to JS. | 36 // or we need to find other ways to pass type-information from Dart to JS. |
| 79 ObjectArray list = _new(new TypeToken<E>(), length); | 37 ListImplementation list = _new(new TypeToken<E>(), length); |
| 80 list._isFixed = isFixed; | 38 list._isFixed = isFixed; |
| 81 return list; | 39 return list; |
| 82 } | 40 } |
| 83 | 41 |
| 84 static ObjectArray _new(TypeToken typeToken, int length) native; | 42 static ListImplementation _new(TypeToken typeToken, int length) native; |
| 85 } | 43 } |
| 86 | 44 |
| 87 | 45 |
| 88 class ObjectArray<T> implements Array<T> native "Array" { | 46 class ListImplementation<T> implements List<T> native "Array" { |
| 89 // ObjectArray maps directly to a JavaScript array. If the array is | 47 // ListImplementation maps directly to a JavaScript array. If the list is |
| 90 // constructed by the ArrayFactory.Array constructor, it has an | 48 // constructed by the ListFactory.List constructor, it has an |
| 91 // additional named property for '_isFixed'. If it is a literal, the | 49 // additional named property for '_isFixed'. If it is a literal, the |
| 92 // code generator will not add the property. It will be 'undefined' | 50 // code generator will not add the property. It will be 'undefined' |
| 93 // and coerce to false. | 51 // and coerce to false. |
| 94 bool _isFixed; | 52 bool _isFixed; |
| 95 | 53 |
| 96 T operator[](int index) { | 54 T operator[](int index) { |
| 97 if (0 <= index && index < length) { | 55 if (0 <= index && index < length) { |
| 98 return _indexOperator(index); | 56 return _indexOperator(index); |
| 99 } | 57 } |
| 100 throw new IndexOutOfRangeException(index); | 58 throw new IndexOutOfRangeException(index); |
| 101 } | 59 } |
| 102 | 60 |
| 103 void operator[]=(int index, T value) { | 61 void operator[]=(int index, T value) { |
| 104 if (index < 0 || length <= index) { | 62 if (index < 0 || length <= index) { |
| 105 throw new IndexOutOfRangeException(index); | 63 throw new IndexOutOfRangeException(index); |
| 106 } | 64 } |
| 107 _indexAssignOperator(index, value); | 65 _indexAssignOperator(index, value); |
| 108 } | 66 } |
| 109 | 67 |
| 110 Iterator<T> iterator() { | 68 Iterator<T> iterator() { |
| 111 if (_isFixed) { | 69 if (_isFixed) { |
| 112 return new FixedSizeArrayIterator<T>(this); | 70 return new FixedSizeListIterator<T>(this); |
| 113 } else { | 71 } else { |
| 114 return new VariableSizeArrayIterator<T>(this); | 72 return new VariableSizeListIterator<T>(this); |
| 115 } | 73 } |
| 116 } | 74 } |
| 117 | 75 |
| 118 T _indexOperator(int index) native; | 76 T _indexOperator(int index) native; |
| 119 void _indexAssignOperator(int index, T value) native; | 77 void _indexAssignOperator(int index, T value) native; |
| 120 int get length() native; | 78 int get length() native; |
| 121 void _setLength(int length) native; | 79 void _setLength(int length) native; |
| 122 void _add(T value) native; | 80 void _add(T value) native; |
| 123 void _removeRange(int start, int length) native; | 81 void _removeRange(int start, int length) native; |
| 124 void _insertRange(int start, int length, T initialValue) native; | 82 void _insertRange(int start, int length, T initialValue) native; |
| 125 | 83 |
| 126 void forEach(void f(T element)) { | 84 void forEach(void f(T element)) { |
| 127 Collections.forEach(this, f); | 85 Collections.forEach(this, f); |
| 128 } | 86 } |
| 129 | 87 |
| 130 Collection<T> filter(bool f(T element)) { | 88 Collection<T> filter(bool f(T element)) { |
| 131 return Collections.filter(this, new Array<T>(), f); | 89 return Collections.filter(this, new List<T>(), f); |
| 132 } | 90 } |
| 133 | 91 |
| 134 bool every(bool f(T element)) { | 92 bool every(bool f(T element)) { |
| 135 return Collections.every(this, f); | 93 return Collections.every(this, f); |
| 136 } | 94 } |
| 137 | 95 |
| 138 bool some(bool f(T element)) { | 96 bool some(bool f(T element)) { |
| 139 return Collections.some(this, f); | 97 return Collections.some(this, f); |
| 140 } | 98 } |
| 141 | 99 |
| 142 bool isEmpty() { | 100 bool isEmpty() { |
| 143 return this.length == 0; | 101 return this.length == 0; |
| 144 } | 102 } |
| 145 | 103 |
| 146 void sort(int compare(T a, T b)) { | 104 void sort(int compare(T a, T b)) { |
| 147 DualPivotQuicksort.sort(this, compare); | 105 DualPivotQuicksort.sort(this, compare); |
| 148 } | 106 } |
| 149 | 107 |
| 150 void copyFrom(Array<Object> src, int srcStart, int dstStart, int count) { | 108 void copyFrom(List<Object> src, int srcStart, int dstStart, int count) { |
| 151 Arrays.copy(src, srcStart, this, dstStart, count); | 109 Arrays.copy(src, srcStart, this, dstStart, count); |
| 152 } | 110 } |
| 153 | 111 |
| 154 void setRange(int start, int length, List<T> from, [int startFrom = 0]) { | 112 void setRange(int start, int length, List<T> from, [int startFrom = 0]) { |
| 155 if (_isFixed) { | 113 if (_isFixed) { |
| 156 throw const UnsupportedOperationException( | 114 throw const UnsupportedOperationException( |
| 157 "Cannot remove range of a non-extendable array"); | 115 "Cannot remove range of a non-extendable list"); |
| 158 } | 116 } |
| 159 if (length == 0) { | 117 if (length == 0) { |
| 160 return; | 118 return; |
| 161 } | 119 } |
| 162 Arrays.rangeCheck(this, start, length); | 120 Arrays.rangeCheck(this, start, length); |
| 163 Arrays.copy(from, startFrom, this, start, length); | 121 Arrays.copy(from, startFrom, this, start, length); |
| 164 } | 122 } |
| 165 | 123 |
| 166 void removeRange(int start, int length) { | 124 void removeRange(int start, int length) { |
| 167 if (_isFixed) { | 125 if (_isFixed) { |
| 168 throw const UnsupportedOperationException( | 126 throw const UnsupportedOperationException( |
| 169 "Cannot remove range of a non-extendable array"); | 127 "Cannot remove range of a non-extendable list"); |
| 170 } | 128 } |
| 171 if (length == 0) { | 129 if (length == 0) { |
| 172 return; | 130 return; |
| 173 } | 131 } |
| 174 Arrays.rangeCheck(this, start, length); | 132 Arrays.rangeCheck(this, start, length); |
| 175 _removeRange(start, length); | 133 _removeRange(start, length); |
| 176 } | 134 } |
| 177 | 135 |
| 178 void insertRange(int start, int length, [T initialValue = null]) { | 136 void insertRange(int start, int length, [T initialValue = null]) { |
| 179 if (_isFixed) { | 137 if (_isFixed) { |
| 180 throw const UnsupportedOperationException( | 138 throw const UnsupportedOperationException( |
| 181 "Cannot insert range in a non-extendable array"); | 139 "Cannot insert range in a non-extendable list"); |
| 182 } | 140 } |
| 183 if (length == 0) { | 141 if (length == 0) { |
| 184 return; | 142 return; |
| 185 } | 143 } |
| 186 if (length < 0) { | 144 if (length < 0) { |
| 187 throw new IllegalArgumentException("negative length $length"); | 145 throw new IllegalArgumentException("negative length $length"); |
| 188 } | 146 } |
| 189 if (start < 0 || start > this.length) { | 147 if (start < 0 || start > this.length) { |
| 190 throw new IndexOutOfRangeException(start); | 148 throw new IndexOutOfRangeException(start); |
| 191 } | 149 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 202 return Arrays.indexOf(this, element, startIndex, this.length); | 160 return Arrays.indexOf(this, element, startIndex, this.length); |
| 203 } | 161 } |
| 204 | 162 |
| 205 int lastIndexOf(T element, int startIndex) { | 163 int lastIndexOf(T element, int startIndex) { |
| 206 return Arrays.lastIndexOf(this, element, startIndex); | 164 return Arrays.lastIndexOf(this, element, startIndex); |
| 207 } | 165 } |
| 208 | 166 |
| 209 void add(T element) { | 167 void add(T element) { |
| 210 if (_isFixed) { | 168 if (_isFixed) { |
| 211 throw const UnsupportedOperationException( | 169 throw const UnsupportedOperationException( |
| 212 "Cannot add to a non-extendable array"); | 170 "Cannot add to a non-extendable list"); |
| 213 } else { | 171 } else { |
| 214 _add(element); | 172 _add(element); |
| 215 } | 173 } |
| 216 } | 174 } |
| 217 | 175 |
| 218 void addLast(T element) { | 176 void addLast(T element) { |
| 219 add(element); | 177 add(element); |
| 220 } | 178 } |
| 221 | 179 |
| 222 void addAll(Collection<T> elements) { | 180 void addAll(Collection<T> elements) { |
| 223 if (_isFixed) { | 181 if (_isFixed) { |
| 224 throw const UnsupportedOperationException( | 182 throw const UnsupportedOperationException( |
| 225 "Cannot add to a non-extendable array"); | 183 "Cannot add to a non-extendable list"); |
| 226 } else { | 184 } else { |
| 227 for (final e in elements) { | 185 for (final e in elements) { |
| 228 _add(e); | 186 _add(e); |
| 229 } | 187 } |
| 230 } | 188 } |
| 231 } | 189 } |
| 232 | 190 |
| 233 void clear() { | 191 void clear() { |
| 234 if (_isFixed) { | 192 if (_isFixed) { |
| 235 throw const UnsupportedOperationException( | 193 throw const UnsupportedOperationException( |
| 236 "Cannot clear a non-extendable array"); | 194 "Cannot clear a non-extendable list"); |
| 237 } else { | 195 } else { |
| 238 length = 0; | 196 length = 0; |
| 239 } | 197 } |
| 240 } | 198 } |
| 241 | 199 |
| 242 void set length(int length) { | 200 void set length(int length) { |
| 243 if (_isFixed) { | 201 if (_isFixed) { |
| 244 throw const UnsupportedOperationException( | 202 throw const UnsupportedOperationException( |
| 245 "Cannot change the length of a non-extendable array"); | 203 "Cannot change the length of a non-extendable list"); |
| 246 } else { | 204 } else { |
| 247 _setLength(length); | 205 _setLength(length); |
| 248 } | 206 } |
| 249 } | 207 } |
| 250 | 208 |
| 251 T removeLast() { | 209 T removeLast() { |
| 252 if (_isFixed) { | 210 if (_isFixed) { |
| 253 throw const UnsupportedOperationException( | 211 throw const UnsupportedOperationException( |
| 254 "Cannot remove in a non-extendable array"); | 212 "Cannot remove in a non-extendable list"); |
| 255 } else { | 213 } else { |
| 256 T element = last(); | 214 T element = last(); |
| 257 length = length - 1; | 215 length = length - 1; |
| 258 return element; | 216 return element; |
| 259 } | 217 } |
| 260 } | 218 } |
| 261 | 219 |
| 262 T last() { | 220 T last() { |
| 263 return this[length - 1]; | 221 return this[length - 1]; |
| 264 } | 222 } |
| 265 } | 223 } |
| 266 | 224 |
| 267 | 225 |
| 268 // Iterator for arrays with fixed size. | 226 // Iterator for lists with fixed size. |
| 269 class FixedSizeArrayIterator<T> extends VariableSizeArrayIterator<T> { | 227 class FixedSizeListIterator<T> extends VariableSizeListIterator<T> { |
| 270 FixedSizeArrayIterator(Array array) | 228 FixedSizeListIterator(List list) |
| 271 : super(array), | 229 : super(list), |
| 272 _length = array.length { | 230 _length = list.length { |
| 273 } | 231 } |
| 274 | 232 |
| 275 bool hasNext() { | 233 bool hasNext() { |
| 276 return _length > _pos; | 234 return _length > _pos; |
| 277 } | 235 } |
| 278 | 236 |
| 279 final int _length; // Cache array length for faster access. | 237 final int _length; // Cache list length for faster access. |
| 280 } | 238 } |
| 281 | 239 |
| 282 | 240 |
| 283 // Iterator for arrays with variable size. | 241 // Iterator for lists with variable size. |
| 284 class VariableSizeArrayIterator<T> implements Iterator<T> { | 242 class VariableSizeListIterator<T> implements Iterator<T> { |
| 285 VariableSizeArrayIterator(Array<T> array) | 243 VariableSizeListIterator(List<T> list) |
| 286 : _array = array, | 244 : _list = list, |
| 287 _pos = 0 { | 245 _pos = 0 { |
| 288 } | 246 } |
| 289 | 247 |
| 290 bool hasNext() { | 248 bool hasNext() { |
| 291 return _array.length > _pos; | 249 return _list.length > _pos; |
| 292 } | 250 } |
| 293 | 251 |
| 294 T next() { | 252 T next() { |
| 295 if (!hasNext()) { | 253 if (!hasNext()) { |
| 296 throw const NoMoreElementsException(); | 254 throw const NoMoreElementsException(); |
| 297 } | 255 } |
| 298 return _array[_pos++]; | 256 return _list[_pos++]; |
| 299 } | 257 } |
| 300 | 258 |
| 301 final Array<T> _array; | 259 final List<T> _list; |
| 302 int _pos; | 260 int _pos; |
| 303 } | 261 } |
| 304 | 262 |
| 305 | 263 |
| 306 class _ArrayJsUtil { | 264 class _ListJsUtil { |
| 307 static int _arrayLength(Array array) native { | 265 static int _listLength(List list) native { |
| 308 return array.length; | 266 return list.length; |
| 309 } | 267 } |
| 310 | 268 |
| 311 static Array _newArray(int len) native { | 269 static List _newList(int len) native { |
| 312 return new Array(len); | 270 return new List(len); |
| 313 } | 271 } |
| 314 | 272 |
| 315 static void _throwIndexOutOfRangeException(int index) native { | 273 static void _throwIndexOutOfRangeException(int index) native { |
| 316 throw new IndexOutOfRangeException(index); | 274 throw new IndexOutOfRangeException(index); |
| 317 } | 275 } |
| 318 } | 276 } |
| OLD | NEW |