Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(230)

Side by Side Diff: sdk/lib/collection/collections.dart

Issue 14175013: Add setAll, insertAll, replaceRange and fillRange. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 part of dart.collection; 5 part of dart.collection;
6 6
7 /** 7 /**
8 * This class provides default implementations for Iterables (including Lists). 8 * This class provides default implementations for Iterables (including Lists).
9 * 9 *
10 * Once Dart receives Mixins it will be replaced with mixin classes. 10 * Once Dart receives Mixins it will be replaced with mixin classes.
(...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after
251 251
252 static int indexOfList(List list, var element, int start) { 252 static int indexOfList(List list, var element, int start) {
253 return Arrays.indexOf(list, element, start, list.length); 253 return Arrays.indexOf(list, element, start, list.length);
254 } 254 }
255 255
256 static int lastIndexOfList(List list, var element, int start) { 256 static int lastIndexOfList(List list, var element, int start) {
257 if (start == null) start = list.length - 1; 257 if (start == null) start = list.length - 1;
258 return Arrays.lastIndexOf(list, element, start); 258 return Arrays.lastIndexOf(list, element, start);
259 } 259 }
260 260
261 static Iterable getRangeList(List list, int start, int end) { 261 static void _rangeCheck(List list, int start, int end) {
262 if (start < 0 || start > list.length) { 262 if (start < 0 || start > list.length) {
263 throw new RangeError.range(start, 0, list.length); 263 throw new RangeError.range(start, 0, list.length);
264 } 264 }
265 if (end < start || end > list.length) { 265 if (end < start || end > list.length) {
266 throw new RangeError.range(end, start, list.length); 266 throw new RangeError.range(end, start, list.length);
267 } 267 }
268 }
269
270 static Iterable getRangeList(List list, int start, int end) {
271 _rangeCheck(list, start, end);
268 // The generic type is currently lost. It will be fixed with mixins. 272 // The generic type is currently lost. It will be fixed with mixins.
269 return new SubListIterable(list, start, end); 273 return new SubListIterable(list, start, end);
270 } 274 }
271 275
272 static void setRangeList(List list, int start, int end, 276 static void setRangeList(List list, int start, int end,
273 Iterable from, int skipCount) { 277 Iterable from, int skipCount) {
274 if (start < 0 || start > list.length) { 278 _rangeCheck(list, start, end);
275 throw new RangeError.range(start, 0, list.length);
276 }
277 if (end < start || end > list.length) {
278 throw new RangeError.range(end, start, list.length);
279 }
280 int length = end - start; 279 int length = end - start;
281 if (length == 0) return; 280 if (length == 0) return;
282 281
283 if (skipCount < 0) throw new ArgumentError(skipCount); 282 if (skipCount < 0) throw new ArgumentError(skipCount);
284 283
285 // TODO(floitsch): Make this accept more. 284 // TODO(floitsch): Make this accept more.
286 List otherList; 285 List otherList;
287 int otherStart; 286 int otherStart;
288 if (from is List) { 287 if (from is List) {
289 otherList = from; 288 otherList = from;
290 otherStart = skipCount; 289 otherStart = skipCount;
291 } else { 290 } else {
292 otherList = from.skip(skipCount).toList(growable: false); 291 otherList = from.skip(skipCount).toList(growable: false);
293 otherStart = 0; 292 otherStart = 0;
294 } 293 }
295 if (otherStart + length > otherList.length) { 294 if (otherStart + length > otherList.length) {
296 throw new StateError("Not enough elements"); 295 throw new StateError("Not enough elements");
297 } 296 }
298 Arrays.copy(otherList, otherStart, list, start, length); 297 Arrays.copy(otherList, otherStart, list, start, length);
299 } 298 }
300 299
300 static void replaceRangeList(List list, int start, int end,
301 Iterable iterable) {
302 _rangeCheck(list, start, end);
303 // TODO(floitsch): optimize this.
304 list.removeRange(start, end);
305 list.insertAll(start, iterable);
306 }
307
308 static void fillRangeList(List list, int start, int end, fillValue) {
309 _rangeCheck(list, start, end);
310 for (int i = start; i < end; i++) {
311 list[i] = fillValue;
312 }
313 }
314
315 static void insertAllList(List list, int index, Iterable iterable) {
316 if (index < 0 || index > list.length) {
317 throw new RangeError.range(index, 0, list.length);
318 }
319 if (iterable is! List && iterable is! Set) {
320 iterable = iterable.toList(growable: false);
321 }
322 int insertionLength = iterable.length;
323 list.length += insertionLength;
324 list.setRange(index + insertionLength, list.length, list, index);
325 for (var element in iterable) {
326 list[index++] = element;
327 }
328 }
329
330 static void setAllList(List list, int index, Iterable iterable) {
331 if (index < 0 || index > list.length) {
332 throw new RangeError.range(index, 0, list.length);
333 }
334 for (var element in iterable) {
335 list[index++] = element;
336 }
337 }
338
301 static Map<int, dynamic> asMapList(List l) { 339 static Map<int, dynamic> asMapList(List l) {
302 return new ListMapView(l); 340 return new ListMapView(l);
303 } 341 }
304 342
305 static bool setContainsAll(Set set, Iterable other) { 343 static bool setContainsAll(Set set, Iterable other) {
306 for (var element in other) { 344 for (var element in other) {
307 if (!set.contains(element)) return false; 345 if (!set.contains(element)) return false;
308 } 346 }
309 return true; 347 return true;
310 } 348 }
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
349 * The source of the elements may be a [List] or any [Iterable] with 387 * The source of the elements may be a [List] or any [Iterable] with
350 * efficient [Iterable.length] and [Iterable.elementAt]. 388 * efficient [Iterable.length] and [Iterable.elementAt].
351 */ 389 */
352 class UnmodifiableListView<E> extends UnmodifiableListBase<E> { 390 class UnmodifiableListView<E> extends UnmodifiableListBase<E> {
353 Iterable<E> _source; 391 Iterable<E> _source;
354 /** Create an unmodifiable list backed by [source]. */ 392 /** Create an unmodifiable list backed by [source]. */
355 UnmodifiableListView(Iterable<E> source) : _source = source; 393 UnmodifiableListView(Iterable<E> source) : _source = source;
356 int get length => _source.length; 394 int get length => _source.length;
357 E operator[](int index) => _source.elementAt(index); 395 E operator[](int index) => _source.elementAt(index);
358 } 396 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698