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

Side by Side Diff: runtime/lib/growable_array.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 class _GrowableObjectArray<T> implements List<T> { 5 class _GrowableObjectArray<T> implements List<T> {
6 factory _GrowableObjectArray._uninstantiable() { 6 factory _GrowableObjectArray._uninstantiable() {
7 throw new UnsupportedError( 7 throw new UnsupportedError(
8 "GrowableObjectArray can only be allocated by the VM"); 8 "GrowableObjectArray can only be allocated by the VM");
9 } 9 }
10 10
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
45 45
46 void remove(Object element) { 46 void remove(Object element) {
47 for (int i = 0; i < this.length; i++) { 47 for (int i = 0; i < this.length; i++) {
48 if (this[i] == element) { 48 if (this[i] == element) {
49 removeAt(i); 49 removeAt(i);
50 return; 50 return;
51 } 51 }
52 } 52 }
53 } 53 }
54 54
55 void insertAll(int index, Iterable<T> iterable) {
56 if (index < 0 || index > length) {
57 throw new RangeError.range(index, 0, length);
58 }
59 // TODO(floitsch): we can probably detect more cases.
60 if (iterable is! List && iterable is! Set && iterable is! SubListIterable) {
61 iterable = iterable.toList();
62 }
63 int insertionLength = iterable.length;
64 // There might be errors after the length change, in which case the list
65 // will end up being modified but the operation not complete. Unless we
66 // always go through a "toList" we can't really avoid that.
67 this.length += insertionLength;
68 setRange(index + insertionLength, this.length, this, index);
69 setAll(index, iterable);
70 }
71
72 void setAll(int index, Iterable<T> iterable) {
73 if (iterable is List) {
74 setRange(index, index + iterable.length, iterable);
Lasse Reichstein Nielsen 2013/04/15 12:11:31 Notice how the other parameter structure for setRa
75 } else {
76 for (T element in iterable) {
77 this[index++] = element;
78 }
79 }
80 }
81
55 void removeWhere(bool test(T element)) { 82 void removeWhere(bool test(T element)) {
56 IterableMixinWorkaround.removeWhereList(this, test); 83 IterableMixinWorkaround.removeWhereList(this, test);
57 } 84 }
58 85
59 void retainWhere(bool test(T element)) { 86 void retainWhere(bool test(T element)) {
60 IterableMixinWorkaround.removeWhereList(this, 87 IterableMixinWorkaround.removeWhereList(this,
61 (T element) => !test(element)); 88 (T element) => !test(element));
62 } 89 }
63 90
64 Iterable<T> getRange(int start, int end) { 91 Iterable<T> getRange(int start, int end) {
65 return IterableMixinWorkaround.getRangeList(this, start, end); 92 return IterableMixinWorkaround.getRangeList(this, start, end);
66 } 93 }
67 94
68 void setRange(int start, int end, Iterable<T> iterable, [int skipCount = 0]) { 95 void setRange(int start, int end, Iterable<T> iterable, [int skipCount = 0]) {
69 IterableMixinWorkaround.setRangeList(this, start, end, iterable, skipCount); 96 IterableMixinWorkaround.setRangeList(this, start, end, iterable, skipCount);
70 } 97 }
71 98
72 void removeRange(int start, int end) { 99 void removeRange(int start, int end) {
73 Arrays.indicesCheck(this, start, end); 100 Arrays.indicesCheck(this, start, end);
74 Arrays.copy(this, 101 Arrays.copy(this,
75 end, 102 end,
76 this, 103 this,
77 start, 104 start,
78 this.length - end); 105 this.length - end);
79 this.length = this.length - (end - start); 106 this.length = this.length - (end - start);
80 } 107 }
81 108
109 void replaceRange(int start, int end, Iterable<T> iterable) {
110 IterableMixinWorkaround.replaceRangeList(this, start, end, iterable);
111 }
112
113 void fillRange(int start, int end, [T fillValue]) {
114 IterableMixinWorkaround.fillRangeList(this, start, end, fillValue);
115 }
116
82 List<T> sublist(int start, [int end]) { 117 List<T> sublist(int start, [int end]) {
83 Arrays.indicesCheck(this, start, end); 118 Arrays.indicesCheck(this, start, end);
84 if (end == null) end = length; 119 if (end == null) end = length;
85 int length = end - start; 120 int length = end - start;
86 if (start == end) return <T>[]; 121 if (start == end) return <T>[];
87 List list = new _GrowableObjectArray<T>.withCapacity(length); 122 List list = new _GrowableObjectArray<T>.withCapacity(length);
88 list.length = length; 123 list.length = length;
89 Arrays.copy(this, start, list, 0, length); 124 Arrays.copy(this, start, list, 0, length);
90 return list; 125 return list;
91 } 126 }
(...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after
312 } 347 }
313 348
314 Set<T> toSet() { 349 Set<T> toSet() {
315 return new Set<T>.from(this); 350 return new Set<T>.from(this);
316 } 351 }
317 352
318 Map<int, T> asMap() { 353 Map<int, T> asMap() {
319 return IterableMixinWorkaround.asMapList(this); 354 return IterableMixinWorkaround.asMapList(this);
320 } 355 }
321 } 356 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698