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 part of dart._collection.dev; | 5 part of dart._collection.dev; |
6 | 6 |
| 7 |
| 8 /** |
| 9 * An [Iterable] for classes that have efficient [length] and [elementAt]. |
| 10 * |
| 11 * All other methods are implemented in terms of [length] and [elementAt], |
| 12 * including [iterator]. |
| 13 */ |
| 14 abstract class ListIterable<E> extends Iterable<E> { |
| 15 int get length; |
| 16 E elementAt(int i); |
| 17 |
| 18 Iterator<E> get iterator => new ListIterableIterator<E>(this); |
| 19 |
| 20 void forEach(void action(E element)) { |
| 21 int length = this.length; |
| 22 for (int i = 0; i < length; i++) { |
| 23 action(elementAt(i)); |
| 24 if (length != this.length) { |
| 25 throw new ConcurrentModificationError(this); |
| 26 } |
| 27 } |
| 28 } |
| 29 |
| 30 bool get isEmpty => length != 0; |
| 31 |
| 32 E get first { |
| 33 if (length == 0) throw new StateError("No elements"); |
| 34 return elementAt(0); |
| 35 } |
| 36 |
| 37 E get last { |
| 38 if (length == 0) throw new StateError("No elements"); |
| 39 return elementAt(length - 1); |
| 40 } |
| 41 |
| 42 E get single { |
| 43 if (length == 0) throw new StateError("No elements"); |
| 44 if (length > 1) throw new StateError("Too many elements"); |
| 45 return elementAt(0); |
| 46 } |
| 47 |
| 48 bool contains(E element) { |
| 49 int length = this.length; |
| 50 for (int i = 0; i < length; i++) { |
| 51 if (elementAt(i) == element) return true; |
| 52 if (length != this.length) { |
| 53 throw new ConcurrentModificationError(this); |
| 54 } |
| 55 } |
| 56 return false; |
| 57 } |
| 58 |
| 59 bool every(bool test(E element)) { |
| 60 int length = this.length; |
| 61 for (int i = 0; i < length; i++) { |
| 62 if (!test(elementAt(i))) return false; |
| 63 if (length != this.length) { |
| 64 throw new ConcurrentModificationError(this); |
| 65 } |
| 66 } |
| 67 return true; |
| 68 } |
| 69 |
| 70 bool any(bool test(E element)) { |
| 71 int length = this.length; |
| 72 for (int i = 0; i < length; i++) { |
| 73 if (test(elementAt(i))) return true; |
| 74 if (length != this.length) { |
| 75 throw new ConcurrentModificationError(this); |
| 76 } |
| 77 } |
| 78 return false; |
| 79 } |
| 80 |
| 81 E firstMatching(bool test(E element), { E orElse() }) { |
| 82 int length = this.length; |
| 83 for (int i = 0; i < length; i++) { |
| 84 E element = elementAt(i); |
| 85 if (test(element)) return element; |
| 86 if (length != this.length) { |
| 87 throw new ConcurrentModificationError(this); |
| 88 } |
| 89 } |
| 90 if (orElse != null) return orElse(); |
| 91 throw new StateError("No matching element"); |
| 92 } |
| 93 |
| 94 E lastMatching(bool test(E element), { E orElse() }) { |
| 95 int length = this.length; |
| 96 for (int i = length - 1; i >= 0; i++) { |
| 97 E element = elementAt(i); |
| 98 if (test(element)) return element; |
| 99 if (length != this.length) { |
| 100 throw new ConcurrentModificationError(this); |
| 101 } |
| 102 } |
| 103 if (orElse != null) return orElse(); |
| 104 throw new StateError("No matching element"); |
| 105 } |
| 106 |
| 107 E singleMatching(bool test(E element)) { |
| 108 int length = this.length; |
| 109 E match = null; |
| 110 bool matchFound = false; |
| 111 for (int i = 0; i < length; i++) { |
| 112 E element = elementAt(i); |
| 113 if (test(element)) { |
| 114 if (matchFound) { |
| 115 throw new StateError("More than one matching element"); |
| 116 } |
| 117 matchFound = true; |
| 118 match = element; |
| 119 } |
| 120 if (length != this.length) { |
| 121 throw new ConcurrentModificationError(this); |
| 122 } |
| 123 } |
| 124 if (matchFound) return match; |
| 125 throw new StateError("No matching element"); |
| 126 } |
| 127 |
| 128 E min([int compare(E a, E b)]) { |
| 129 if (length == 0) return null; |
| 130 if (compare == null) { |
| 131 var defaultCompare = Comparable.compare; |
| 132 compare = defaultCompare; |
| 133 } |
| 134 E min = elementAt(0); |
| 135 int length = this.length; |
| 136 for (int i = 1; i < length; i++) { |
| 137 E element = elementAt(i); |
| 138 if (compare(min, element) > 0) { |
| 139 min = element; |
| 140 } |
| 141 if (length != this.length) { |
| 142 throw new ConcurrentModificationError(this); |
| 143 } |
| 144 } |
| 145 return min; |
| 146 } |
| 147 |
| 148 E max([int compare(E a, E b)]) { |
| 149 if (length == 0) return null; |
| 150 if (compare == null) { |
| 151 var defaultCompare = Comparable.compare; |
| 152 compare = defaultCompare; |
| 153 } |
| 154 E max = elementAt(0); |
| 155 int length = this.length; |
| 156 for (int i = 1; i < length; i++) { |
| 157 E element = elementAt(i); |
| 158 if (compare(max, element) < 0) { |
| 159 max = element; |
| 160 } |
| 161 if (length != this.length) { |
| 162 throw new ConcurrentModificationError(this); |
| 163 } |
| 164 } |
| 165 return max; |
| 166 } |
| 167 |
| 168 String join([String separator]) { |
| 169 int length = this.length; |
| 170 if (separator != null && !separator.isEmpty) { |
| 171 if (length == 0) return ""; |
| 172 String first = "${elementAt(0)}"; |
| 173 if (length != this.length) { |
| 174 throw new ConcurrentModificationError(this); |
| 175 } |
| 176 StringBuffer buffer = new StringBuffer(first); |
| 177 for (int i = 1; i < length; i++) { |
| 178 buffer.add(separator); |
| 179 buffer.add("${elementAt(i)}"); |
| 180 if (length != this.length) { |
| 181 throw new ConcurrentModificationError(this); |
| 182 } |
| 183 } |
| 184 return buffer.toString(); |
| 185 } else { |
| 186 StringBuffer buffer = new StringBuffer(); |
| 187 for (int i = 0; i < length; i++) { |
| 188 buffer.add("${elementAt(i)}"); |
| 189 if (length != this.length) { |
| 190 throw new ConcurrentModificationError(this); |
| 191 } |
| 192 } |
| 193 return buffer.toString(); |
| 194 } |
| 195 } |
| 196 |
| 197 Iterable<E> where(bool test(E element)) => super.where(test); |
| 198 |
| 199 Iterable map(f(E element)) => new MappedIterable(this, f); |
| 200 |
| 201 Iterable mappedBy(f(E element)) => super.mappedBy(f); |
| 202 |
| 203 reduce(var initialValue, combine(var previousValue, E element)) { |
| 204 var value = initialValue; |
| 205 int length = this.length; |
| 206 for (int i = 0; i < length; i++) { |
| 207 value = reduce(value, elementAt(i)); |
| 208 if (length != this.length) { |
| 209 throw new ConcurrentModificationError(this); |
| 210 } |
| 211 } |
| 212 return value; |
| 213 } |
| 214 |
| 215 Iterable<E> skip(int count) => new SubListIterable(this, count, null); |
| 216 |
| 217 Iterable<E> skipWhile(bool test(E element)) => super.skipWhile(test); |
| 218 |
| 219 Iterable<E> take(int count) => new SubListIterable(this, 0, count); |
| 220 |
| 221 Iterable<E> takeWhile(bool test(E element)) => super.takeWhile(test); |
| 222 |
| 223 List<E> toList() { |
| 224 List<E> result = new List(length); |
| 225 for (int i = 0; i < length; i++) { |
| 226 result[i] = elementAt(i); |
| 227 } |
| 228 return result; |
| 229 } |
| 230 |
| 231 Set<E> toSet() { |
| 232 Set<E> result = new Set<E>(); |
| 233 for (int i = 0; i < length; i++) { |
| 234 result.add(elementAt(i)); |
| 235 } |
| 236 return result; |
| 237 } |
| 238 } |
| 239 |
| 240 abstract class SubListIterable<E> extends ListIterable<E> { |
| 241 final Iterable<E> _iterable; |
| 242 final int _start; |
| 243 /** If null, represents the length of the iterable. */ |
| 244 final int _endOrLength; |
| 245 |
| 246 SubListIterable(this._iterable, this._start, this._endOrLength); |
| 247 |
| 248 int get _endIndex { |
| 249 int length = _iterable.length; |
| 250 if (_endOrLength == null || _endOrLength > length) return length; |
| 251 return _endOrLength; |
| 252 } |
| 253 |
| 254 int get _startIndex { |
| 255 int length = _iterable.length; |
| 256 if (_start > length) return length; |
| 257 return _start; |
| 258 } |
| 259 |
| 260 int get length { |
| 261 int length = _iterable.length; |
| 262 if (_start >= length) return 0; |
| 263 if (_endOrLength == null || _endOrLength >= length) { |
| 264 return length - _start; |
| 265 } |
| 266 return _endOrLength - _start; |
| 267 } |
| 268 |
| 269 E elementAt(int index) { |
| 270 int realIndex = _startIndex + index; |
| 271 if (index < 0 || realIndex >= _endIndex) { |
| 272 throw new RangeError.range(index, 0, length); |
| 273 } |
| 274 return _iterable.elementAt(realIndex); |
| 275 } |
| 276 |
| 277 Iterable<E> skip(int count) { |
| 278 if (count < 0) throw new ArgumentError(count); |
| 279 return new SubListIterable(_iterable, _start + count, _endOrLength); |
| 280 } |
| 281 |
| 282 Iterable<E> take(int count) { |
| 283 if (count < 0) throw new ArgumentError(count); |
| 284 if (_endOrLength == null) { |
| 285 return new SubListIterable(_iterable, _start, _start + count); |
| 286 } else { |
| 287 newEnd = _start + count; |
| 288 if (_endOrLength < newEnd) return this; |
| 289 return new SubListIterable(_iterable, _start, newEnd); |
| 290 } |
| 291 } |
| 292 } |
| 293 |
| 294 class ListIterableIterator<E> implements Iterator<E> { |
| 295 final Iterable<E> _iterable; |
| 296 final int _length; |
| 297 int _index; |
| 298 E _current; |
| 299 ListIterableIterator(Iterable<E> iterable) |
| 300 : _iterable = iterable, _length = iterable.length, _index = 0; |
| 301 |
| 302 E get current => _current; |
| 303 |
| 304 bool moveNext() { |
| 305 if (_length != _iterable.length) { |
| 306 throw new ConcurrentModificationError(_iterable); |
| 307 } |
| 308 if (_index == _length) { |
| 309 _current = null; |
| 310 return false; |
| 311 } |
| 312 _current = _iterable.elementAt(_index); |
| 313 _index++; |
| 314 return true; |
| 315 } |
| 316 } |
| 317 |
7 typedef T _Transformation<S, T>(S value); | 318 typedef T _Transformation<S, T>(S value); |
8 | 319 |
9 class MappedIterable<S, T> extends Iterable<T> { | 320 class MappedIterable<S, T> extends Iterable<T> { |
10 final Iterable<S> _iterable; | 321 final Iterable<S> _iterable; |
11 // TODO(ahe): Restore type when feature is implemented in dart2js | 322 // TODO(ahe): Restore type when feature is implemented in dart2js |
12 // checked mode. http://dartbug.com/7733 | 323 // checked mode. http://dartbug.com/7733 |
13 final /* _Transformation<S, T> */ _f; | 324 final /* _Transformation<S, T> */ _f; |
14 | 325 |
15 MappedIterable(this._iterable, T this._f(S element)); | 326 MappedIterable(this._iterable, T this._f(S element)); |
16 | 327 |
(...skipping 664 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
681 class EmptyIterator<E> implements Iterator<E> { | 992 class EmptyIterator<E> implements Iterator<E> { |
682 const EmptyIterator(); | 993 const EmptyIterator(); |
683 bool moveNext() => false; | 994 bool moveNext() => false; |
684 E get current => null; | 995 E get current => null; |
685 } | 996 } |
686 | 997 |
687 /** An [Iterator] that can move in both directions. */ | 998 /** An [Iterator] that can move in both directions. */ |
688 abstract class BiDirectionalIterator<T> implements Iterator<T> { | 999 abstract class BiDirectionalIterator<T> implements Iterator<T> { |
689 bool movePrevious(); | 1000 bool movePrevious(); |
690 } | 1001 } |
OLD | NEW |