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

Side by Side Diff: compiler/lib/implementation/array.dart

Issue 9702034: Removes dartc reliance on its own libraries, now can be targeted at any implementation's libraries (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: junit tests fixed Created 8 years, 9 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
« no previous file with comments | « compiler/lib/error.dart ('k') | compiler/lib/implementation/array.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 class ListFactory<E> {
6 factory List.from(Iterable<E> other) {
7 if (other == null) {
8 throw const NullPointerException();
9 }
10 List<E> list = new List<E>();
11 for (final e in other) {
12 list.add(e);
13 }
14 return list;
15 }
16
17 factory List([int length = null]) {
18 bool isFixed = true;
19 if (length === null) {
20 length = 0;
21 isFixed = false;
22 } else if (length < 0) {
23 throw new IllegalArgumentException("negative length $length");
24 }
25
26 ListImplementation<E> list = new ListImplementation<E>(length);
27 list._isFixed = isFixed;
28 return list;
29 }
30 }
31
32
33 class ListImplementation<T> implements List<T> native "Array" {
34 // ListImplementation maps directly to a JavaScript array. If the list is
35 // constructed by the ListFactory.List constructor, it has an
36 // additional named property for '_isFixed'. If it is a literal, the
37 // code generator will not add the property. It will be 'undefined'
38 // and coerce to false.
39 bool _isFixed;
40
41 ListImplementation(int length);
42
43 T operator[](int index) native;
44 void operator[]=(int index, T value) native;
45
46 Iterator<T> iterator() {
47 if (_isFixed) {
48 return new FixedSizeListIterator<T>(this);
49 } else {
50 return new VariableSizeListIterator<T>(this);
51 }
52 }
53
54 int get length() native;
55 void _setLength(int length) native;
56 void _add(T value) native;
57 void _removeRange(int start, int length) native;
58 void _insertRange(int start, int length, T initialValue) native;
59
60 void forEach(void f(T element)) {
61 Collections.forEach(this, f);
62 }
63
64 Collection map(f(T element)) {
65 return Collections.map(this, new List(), f);
66 }
67
68 Collection<T> filter(bool f(T element)) {
69 return Collections.filter(this, new List<T>(), f);
70 }
71
72 bool every(bool f(T element)) {
73 return Collections.every(this, f);
74 }
75
76 bool some(bool f(T element)) {
77 return Collections.some(this, f);
78 }
79
80 bool isEmpty() {
81 return this.length == 0;
82 }
83
84 void sort(int compare(T a, T b)) {
85 DualPivotQuicksort.sort(this, compare);
86 }
87
88 void copyFrom(List<Object> src, int srcStart, int dstStart, int count) {
89 Arrays.copy(src, srcStart, this, dstStart, count);
90 }
91
92 void setRange(int start, int length, List<T> from, [int startFrom = 0]) {
93 if (_isFixed) {
94 throw const UnsupportedOperationException(
95 "Cannot remove range of a non-extendable list");
96 }
97 if (length == 0) {
98 return;
99 }
100 Arrays.rangeCheck(this, start, length);
101 Arrays.copy(from, startFrom, this, start, length);
102 }
103
104 void removeRange(int start, int length) {
105 if (_isFixed) {
106 throw const UnsupportedOperationException(
107 "Cannot remove range of a non-extendable list");
108 }
109 if (length == 0) {
110 return;
111 }
112 Arrays.rangeCheck(this, start, length);
113 _removeRange(start, length);
114 }
115
116 void insertRange(int start, int length, [T initialValue = null]) {
117 if (_isFixed) {
118 throw const UnsupportedOperationException(
119 "Cannot insert range in a non-extendable list");
120 }
121 if (length == 0) {
122 return;
123 }
124 if (length < 0) {
125 throw new IllegalArgumentException("negative length $length");
126 }
127 if (start < 0 || start > this.length) {
128 throw new IndexOutOfRangeException(start);
129 }
130 _insertRange(start, length, initialValue);
131 }
132
133 List<T> getRange(int start, int length) {
134 if (length == 0) return [];
135 Arrays.rangeCheck(this, start, length);
136 List list = new List<T>();
137 list.length = length;
138 Arrays.copy(this, start, list, 0, length);
139 return list;
140 }
141
142 int indexOf(T element, [int start = 0]) {
143 return Arrays.indexOf(this, element, start, this.length);
144 }
145
146 int lastIndexOf(T element, [int start = null]) {
147 if (start === null) start = length - 1;
148 return Arrays.lastIndexOf(this, element, start);
149 }
150
151 void add(T element) {
152 if (_isFixed) {
153 throw const UnsupportedOperationException(
154 "Cannot add to a non-extendable list");
155 } else {
156 _add(element);
157 }
158 }
159
160 void addLast(T element) {
161 add(element);
162 }
163
164 void addAll(Collection<T> elements) {
165 if (_isFixed) {
166 throw const UnsupportedOperationException(
167 "Cannot add to a non-extendable list");
168 } else {
169 if (elements == null) {
170 throw const NullPointerException();
171 }
172 for (final e in elements) {
173 _add(e);
174 }
175 }
176 }
177
178 void clear() {
179 if (_isFixed) {
180 throw const UnsupportedOperationException(
181 "Cannot clear a non-extendable list");
182 } else {
183 length = 0;
184 }
185 }
186
187 void set length(int length) {
188 if (_isFixed) {
189 throw const UnsupportedOperationException(
190 "Cannot change the length of a non-extendable list");
191 } else {
192 _setLength(length);
193 }
194 }
195
196 T removeLast() {
197 if (_isFixed) {
198 throw const UnsupportedOperationException(
199 "Cannot remove in a non-extendable list");
200 } else {
201 T element = last();
202 length = length - 1;
203 return element;
204 }
205 }
206
207 T last() {
208 return this[length - 1];
209 }
210 }
211
212
213 // Iterator for lists with fixed size.
214 class FixedSizeListIterator<T> extends VariableSizeListIterator<T> {
215 FixedSizeListIterator(List list)
216 : super(list),
217 _length = list.length {
218 }
219
220 bool hasNext() {
221 return _length > _pos;
222 }
223
224 final int _length; // Cache list length for faster access.
225 }
226
227
228 // Iterator for lists with variable size.
229 class VariableSizeListIterator<T> implements Iterator<T> {
230 VariableSizeListIterator(List<T> list)
231 : _list = list,
232 _pos = 0 {
233 }
234
235 bool hasNext() {
236 return _list.length > _pos;
237 }
238
239 T next() {
240 if (!hasNext()) {
241 throw const NoMoreElementsException();
242 }
243 return _list[_pos++];
244 }
245
246 final List<T> _list;
247 int _pos;
248 }
249
250
251 class _ListJsUtil {
252 static int _listLength(List list) native {
253 return list.length;
254 }
255
256 static List _newList(int len) native {
257 return new List(len);
258 }
259
260 static void _throwIndexOutOfRangeException(int index) native {
261 throw new IndexOutOfRangeException(index);
262 }
263 }
OLDNEW
« no previous file with comments | « compiler/lib/error.dart ('k') | compiler/lib/implementation/array.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698