| 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.dev; | 5 part of dart._collection.dev; |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * Class implementing the read-operations on [List]. | 8 * Class implementing the read-operations on [List]. |
| 9 * | 9 * |
| 10 * Implements all read-only operations, except [:operator[]:] and [:length:], | 10 * Implements all read-only operations, except [:operator[]:] and [:length:], |
| 11 * in terms of those two operations. | 11 * in terms of those two operations. |
| 12 */ | 12 */ |
| 13 abstract class ListBase<E> extends ListIterable<E> implements List<E> { | 13 abstract class ListBase<E> extends ListIterable<E> implements List<E> { |
| 14 // List interface. | 14 // List interface. |
| 15 int get length; | 15 int get length; |
| 16 E operator[](int index); | 16 E operator[](int index); |
| 17 | 17 |
| 18 // Collection interface. | 18 // Collection interface. |
| 19 // Implement in a fully mutable specialized class if necessary. | 19 // Implement in a fully mutable specialized class if necessary. |
| 20 // The fixed-length and unmodifiable lists throw on all members | 20 // The fixed-length and unmodifiable lists throw on all members |
| 21 // of the collection interface. | 21 // of the collection interface. |
| 22 | 22 |
| 23 // Iterable interface. | 23 // Iterable interface. |
| 24 E elementAt(int index) { | 24 E elementAt(int index) { |
| 25 return this[index]; | 25 return this[index]; |
| 26 } | 26 } |
| 27 | |
| 28 Map<int, E> asMap() { | 27 Map<int, E> asMap() { |
| 29 return new ListMapView(this); | 28 return new ListMapView(this); |
| 30 } | 29 } |
| 31 | 30 |
| 32 List<E> getRange(int start, int length) { | |
| 33 if (start < 0 || start > this.length) { | |
| 34 throw new RangeError.range(start, 0, this.length); | |
| 35 } | |
| 36 if (length < 0 || start + length > this.length) { | |
| 37 throw new RangeError.range(length, 0, this.length - start); | |
| 38 } | |
| 39 List<E> result = new List<E>(length); | |
| 40 for (int i = 0; i < length; i++) { | |
| 41 result[i] = this[start + i]; | |
| 42 } | |
| 43 return result; | |
| 44 } | |
| 45 | |
| 46 int indexOf(E element, [int start = 0]) { | |
| 47 return Arrays.indexOf(this, element, start, this.length); | |
| 48 } | |
| 49 | |
| 50 int lastIndexOf(E element, [int start = null]) { | |
| 51 if (start == null) start = length - 1; | |
| 52 return Arrays.lastIndexOf(this, element, start); | |
| 53 } | |
| 54 | |
| 55 Iterable<E> get reversed => new ReversedListIterable(this); | |
| 56 } | 31 } |
| 57 | 32 |
| 58 /** | 33 /** |
| 59 * Abstract class implementing the non-length changing operations of [List]. | 34 * Abstract class implementing the non-length changing operations of [List]. |
| 60 * | 35 * |
| 61 * All modifications are performed using [[]=]. | 36 * All modifications are performed using [[]=]. |
| 62 */ | 37 */ |
| 63 abstract class FixedLengthListBase<E> extends ListBase<E> { | 38 abstract class FixedLengthListBase<E> extends ListBase<E> { |
| 64 void operator[]=(int index, E value); | 39 void operator[]=(int index, E value); |
| 65 | 40 |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 108 void retainAll(Iterable elements) { | 83 void retainAll(Iterable elements) { |
| 109 throw new UnsupportedError( | 84 throw new UnsupportedError( |
| 110 "Cannot remove from a fixed-length list"); | 85 "Cannot remove from a fixed-length list"); |
| 111 } | 86 } |
| 112 | 87 |
| 113 void removeMatching(bool test(E element)) { | 88 void removeMatching(bool test(E element)) { |
| 114 throw new UnsupportedError( | 89 throw new UnsupportedError( |
| 115 "Cannot remove from a fixed-length list"); | 90 "Cannot remove from a fixed-length list"); |
| 116 } | 91 } |
| 117 | 92 |
| 118 void retainMatching(bool test(E element)) { | |
| 119 throw new UnsupportedError( | |
| 120 "Cannot remove from a fixed-length list"); | |
| 121 } | |
| 122 | |
| 123 void clear() { | 93 void clear() { |
| 124 throw new UnsupportedError( | 94 throw new UnsupportedError( |
| 125 "Cannot clear a fixed-length list"); | 95 "Cannot clear a fixed-length list"); |
| 126 } | 96 } |
| 127 | 97 |
| 128 E removeAt(int index) { | 98 E removeAt(int index) { |
| 129 throw new UnsupportedError( | 99 throw new UnsupportedError( |
| 130 "Cannot remove from a fixed-length list"); | 100 "Cannot remove from a fixed-length list"); |
| 131 } | 101 } |
| 132 | 102 |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 189 void retainAll(Iterable elements) { | 159 void retainAll(Iterable elements) { |
| 190 throw new UnsupportedError( | 160 throw new UnsupportedError( |
| 191 "Cannot remove from an unmodifiable list"); | 161 "Cannot remove from an unmodifiable list"); |
| 192 } | 162 } |
| 193 | 163 |
| 194 void removeMatching(bool test(E element)) { | 164 void removeMatching(bool test(E element)) { |
| 195 throw new UnsupportedError( | 165 throw new UnsupportedError( |
| 196 "Cannot remove from an unmodifiable list"); | 166 "Cannot remove from an unmodifiable list"); |
| 197 } | 167 } |
| 198 | 168 |
| 199 void retainMatching(bool test(E element)) { | |
| 200 throw new UnsupportedError( | |
| 201 "Cannot remove from an unmodifiable list"); | |
| 202 } | |
| 203 | |
| 204 void sort([Comparator<E> compare]) { | 169 void sort([Comparator<E> compare]) { |
| 205 throw new UnsupportedError( | 170 throw new UnsupportedError( |
| 206 "Cannot modify an unmodifiable list"); | 171 "Cannot modify an unmodifiable list"); |
| 207 } | 172 } |
| 208 | 173 |
| 209 void clear() { | 174 void clear() { |
| 210 throw new UnsupportedError( | 175 throw new UnsupportedError( |
| 211 "Cannot clear an unmodifiable list"); | 176 "Cannot clear an unmodifiable list"); |
| 212 } | 177 } |
| 213 | 178 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 228 | 193 |
| 229 void removeRange(int start, int length) { | 194 void removeRange(int start, int length) { |
| 230 throw new UnsupportedError( | 195 throw new UnsupportedError( |
| 231 "Cannot remove from an unmodifiable list"); | 196 "Cannot remove from an unmodifiable list"); |
| 232 } | 197 } |
| 233 | 198 |
| 234 void insertRange(int start, int length, [E initialValue]) { | 199 void insertRange(int start, int length, [E initialValue]) { |
| 235 throw new UnsupportedError( | 200 throw new UnsupportedError( |
| 236 "Cannot insert range in an unmodifiable list"); | 201 "Cannot insert range in an unmodifiable list"); |
| 237 } | 202 } |
| 203 |
| 204 List<E> getRange(int start, int length) { |
| 205 if (start < 0 || start > this.length) { |
| 206 throw new RangeError.range(start, 0, this.length); |
| 207 } |
| 208 if (length < 0 || start + length > this.length) { |
| 209 throw new RangeError.range(length, 0, this.length - start); |
| 210 } |
| 211 List<E> result = new List<E>(length); |
| 212 for (int i = 0; i < length; i++) { |
| 213 result[i] = this[start + i]; |
| 214 } |
| 215 return result; |
| 216 } |
| 238 } | 217 } |
| 239 | 218 |
| 240 /** An empty fixed-length list. */ | 219 /** An empty fixed-length list. */ |
| 241 class EmptyList<E> extends FixedLengthListBase<E> { | 220 class EmptyList<E> extends FixedLengthListBase<E> { |
| 242 int get length => 0; | 221 int get length => 0; |
| 243 E operator[](int index) { throw new RangeError.value(index); } | 222 E operator[](int index) { throw new RangeError.value(index); } |
| 244 void operator []=(int index, E value) { throw new RangeError.value(index); } | 223 void operator []=(int index, E value) { throw new RangeError.value(index); } |
| 245 Iterable<E> skip(int count) => const EmptyIterable(); | 224 Iterable<E> skip(int count) => const EmptyIterable(); |
| 246 Iterable<E> take(int count) => const EmptyIterable(); | 225 Iterable<E> take(int count) => const EmptyIterable(); |
| 247 Iterable<E> get reversed => const EmptyIterable(); | 226 Iterable<E> get reversed => const EmptyIterable(); |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 316 } | 295 } |
| 317 | 296 |
| 318 E remove(int key) { | 297 E remove(int key) { |
| 319 throw new UnsupportedError("Cannot modify an unmodifiable map"); | 298 throw new UnsupportedError("Cannot modify an unmodifiable map"); |
| 320 } | 299 } |
| 321 | 300 |
| 322 void clear() { | 301 void clear() { |
| 323 throw new UnsupportedError("Cannot modify an unmodifiable map"); | 302 throw new UnsupportedError("Cannot modify an unmodifiable map"); |
| 324 } | 303 } |
| 325 } | 304 } |
| OLD | NEW |