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

Side by Side Diff: runtime/lib/byte_array.dart

Issue 9195031: Add ByteArray interface and provide internal and external implementations. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: address remaining review comments Created 8 years, 11 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 | « runtime/lib/byte_array.cc ('k') | runtime/lib/byte_buffer.cc » ('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) 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
3 // BSD-style license that can be found in the LICENSE file.
4
5 interface ByteArray extends List default _InternalByteArray {
6 ByteArray(int length);
7
8 int get length();
9
10 int getInt8(int byteOffset);
11
12 void setInt8(int byteOffset, int value);
13
14 int getUint8(int byteOffset);
15
16 void setUint8(int byteOffset, int value);
17
18 int getInt16(int byteOffset);
19
20 void setInt16(int byteOffset, int value);
21
22 int getUint16(int byteOffset);
23
24 void setUint16(int byteOffset, int value);
25
26 int getInt32(int byteOffset);
27
28 void setInt32(int byteOffset, int value);
29
30 int getUint32(int byteOffset);
31
32 void setUint32(int byteOffset, int value);
33
34 int getInt64(int byteOffset);
35
36 void setInt64(int byteOffset, int value);
37
38 int getUint64(int byteOffset);
39
40 void setUint64(int byteOffset, int value);
41
42 double getFloat32(int byteOffset);
43
44 void setFloat32(int byteOffset, double value);
45
46 double getFloat64(int byteOffset);
47
48 void setFloat64(int byteOffset, double value);
49 }
50
51
52 class _ByteArrayBase {
53
54 // Iterable interface
55
56 Iterator iterator() {
57 return new _ByteArrayIterator(this);
58 }
59
60 // Collection interface
61
62 int get length() {
63 return _length();
64 }
65
66 bool every(bool f(int element)) {
67 return Collections.every(this, f);
68 }
69
70 Collection map(f(int element)) {
71 return Collections.map(this,
72 new GrowableObjectArray.withCapacity(length),
73 f);
74 }
75
76 Collection filter(bool f(int element)) {
77 return Collections.filter(this, new GrowableObjectArray(), f);
78 }
79
80 void forEach(f(int element)) {
81 Collections.forEach(this, f);
82 }
83
84 bool isEmpty() {
85 return this.length === 0;
86 }
87
88 bool some(bool f(int element)) {
89 return Collections.some(this, f);
90 }
91
92 // List interface
93
94 int operator[](int index) {
95 return getUint8(index);
96 }
97
98 void operator[]=(int index, int value) {
99 setUint8(index, value);
100 }
101
102 void set length(int newLength) {
103 throw const UnsupportedOperationException("Cannot add to a byte array");
104 }
105
106 void add(int element) {
107 throw const UnsupportedOperationException("Cannot add to a byte array");
108 }
109
110 void addLast(int element) {
111 add(element);
112 }
113
114 void addAll(Collection elements) {
115 throw const UnsupportedOperationException("Cannot add to a byte array");
116 }
117
118 void sort(int compare(int a, b)) {
119 DualPivotQuicksort.sort(this, compare);
120 }
121
122 int indexOf(int element, [int start = 0]) {
123 return Arrays.indexOf(this, element, start, this.length);
124 }
125
126 int lastIndexOf(int element, [int start = null]) {
127 if (start === null) start = length - 1;
128 return Arrays.lastIndexOf(this, element, start);
129 }
130
131 void clear() {
132 throw const UnsupportedOperationException("Cannot clear a byte array");
133 }
134
135 int removeLast() {
136 throw const UnsupportedOperationException(
137 "Cannot remove from a byte array");
138 }
139
140 int last() {
141 return this[length - 1];
142 }
143
144 // Implementation
145
146 int _length() native "ByteArray_getLength";
147 }
148
149
150 class _ByteArrayIterator implements Iterator {
151 _ByteArrayIterator(List byteArray)
152 : _byteArray = byteArray, _length = byteArray.length, _pos = 0 {
153 assert(byteArray is ByteArray);
154 }
155
156 bool hasNext() {
157 return _length > _pos;
158 }
159
160 int next() {
161 if (!hasNext()) {
162 throw const NoMoreElementsException();
163 }
164 return _byteArray[_pos++];
165 }
166
167 final List _byteArray;
168
169 final int _length;
170
171 int _pos;
172 }
173
174
175 class _InternalByteArray extends _ByteArrayBase implements ByteArray {
176 factory _InternalByteArray(int length) {
177 return _allocate(length);
178 }
179
180 // ByteArray interface
181
182 int getInt8(int byteOffset) {
183 return _getInt8(byteOffset);
184 }
185
186 void setInt8(int byteOffset, int value) {
187 _setInt8(byteOffset, value);
188 }
189
190 int getUint8(int byteOffset) {
191 return _getUint8(byteOffset);
192 }
193
194 void setUint8(int byteOffset, int value) {
195 _setUint8(byteOffset, value);
196 }
197
198 int getInt16(int byteOffset) {
199 return _getInt16(byteOffset);
200 }
201
202 void setInt16(int byteOffset, int value) {
203 _setInt16(byteOffset, value);
204 }
205
206 int getUint16(int byteOffset) {
207 return _getInt16(byteOffset);
208 }
209
210 void setUint16(int byteOffset, int value) {
211 _setUint16(byteOffset, value);
212 }
213
214 int getInt32(int byteOffset) {
215 return _getInt32(byteOffset);
216 }
217
218 void setInt32(int byteOffset, int value) {
219 _setInt32(byteOffset, value);
220 }
221
222 int getUint32(int byteOffset) {
223 return _getUint32(byteOffset);
224 }
225
226 void setUint32(int byteOffset, int value) {
227 _setUint32(byteOffset, value);
228 }
229
230 int getInt64(int byteOffset) {
231 return _getInt64(byteOffset);
232 }
233
234 void setInt64(int byteOffset, int value) {
235 _setInt64(byteOffset, value);
236 }
237
238 int getUint64(int byteOffset) {
239 return _getUint64(byteOffset);
240 }
241
242 void setUint64(int byteOffset, int value) {
243 _setUint64(byteOffset, value);
244 }
245
246 double getFloat32(int byteOffset) {
247 return _getFloat32(byteOffset);
248 }
249
250 void setFloat32(int byteOffset, double value) {
251 _setFloat32(byteOffset, value);
252 }
253
254 double getFloat64(int byteOffset) {
255 return _getFloat64(byteOffset);
256 }
257
258 void setFloat64(int byteOffset, double value) {
259 _setFloat64(byteOffset, value);
260 }
261
262 // Implementation
263
264 static _InternalByteArray _allocate(int length)
265 native "InternalByteArray_allocate";
266
267 int _getInt8(int byteOffset)
268 native "InternalByteArray_getInt8";
269
270 void _setInt8(int byteOffset, int value)
271 native "InternalByteArray_setInt8";
272
273 int _getUint8(int byteOffset)
274 native "InternalByteArray_getUint8";
275
276 void _setUint8(int byteOffset, int value)
277 native "InternalByteArray_setUint8";
278
279 int _getInt16(int byteOffset)
280 native "InternalByteArray_getInt16";
281
282 void _setInt16(int byteOffset, int value)
283 native "InternalByteArray_setInt16";
284
285 int _getUint16(int byteOffset)
286 native "InternalByteArray_getUint16";
287
288 void _setUint16(int byteOffset, int value)
289 native "InternalByteArray_setUint16";
290
291 int _getInt32(int byteOffset)
292 native "InternalByteArray_getInt32";
293
294 void _setInt32(int byteOffset, int value)
295 native "InternalByteArray_setInt32";
296
297 int _getUint32(int byteOffset)
298 native "InternalByteArray_getUint32";
299
300 void _setUint32(int byteOffset, int value)
301 native "InternalByteArray_setUint32";
302
303 int _getInt64(int byteOffset)
304 native "InternalByteArray_getInt64";
305
306 void _setInt64(int byteOffset, int value)
307 native "InternalByteArray_setInt64";
308
309 int _getUint64(int byteOffset)
310 native "InternalByteArray_getUint64";
311
312 void _setUint64(int byteOffset, int value)
313 native "InternalByteArray_setUint64";
314
315 double _getFloat32(int byteOffset)
316 native "InternalByteArray_getFloat32";
317
318 void _setFloat32(int byteOffset, double value)
319 native "InternalByteArray_setFloat32";
320
321 double _getFloat64(int byteOffset)
322 native "InternalByteArray_getFloat64";
323
324 void _setFloat64(int byteOffset, double value)
325 native "InternalByteArray_setFloat64";
326 }
327
328
329 class _ExternalByteArray extends _ByteArrayBase implements ByteArray {
330 // Collection interface
331
332 int get length() {
333 return _length();
334 }
335
336 // List interface
337
338 int operator[](int index) {
339 return _getUint8(index);
340 }
341
342 void operator[]=(int index, int value) {
343 _setUint8(index, value);
344 }
345
346 // ByteArray interface
347
348 int getInt8(int byteOffset) {
349 return _getInt8(byteOffset);
350 }
351
352 void setInt8(int byteOffset, int value) {
353 _setInt8(byteOffset, value);
354 }
355
356 int getUint8(int byteOffset) {
357 return _getUint8(byteOffset);
358 }
359
360 void setUint8(int byteOffset, int value) {
361 _setUint8(byteOffset, value);
362 }
363
364 int getInt16(int byteOffset) {
365 return _getInt16(byteOffset);
366 }
367
368 void setInt16(int byteOffset, int value) {
369 _setInt16(byteOffset, value);
370 }
371
372 int getUint16(int byteOffset) {
373 return _getInt16(byteOffset);
374 }
375
376 void setUint16(int byteOffset, int value) {
377 _setUint16(byteOffset, value);
378 }
379
380 int getInt32(int byteOffset) {
381 return _getInt32(byteOffset);
382 }
383
384 void setInt32(int byteOffset, int value) {
385 _setInt32(byteOffset, value);
386 }
387
388 int getUint32(int byteOffset) {
389 return _getUint32(byteOffset);
390 }
391
392 void setUint32(int byteOffset, int value) {
393 _setUint32(byteOffset, value);
394 }
395
396 int getInt64(int byteOffset) {
397 return _getInt64(byteOffset);
398 }
399
400 void setInt64(int byteOffset, int value) {
401 _setInt64(byteOffset, value);
402 }
403
404 int getUint64(int byteOffset) {
405 return _getUint64(byteOffset);
406 }
407
408 void setUint64(int byteOffset, int value) {
409 _setUint64(byteOffset, value);
410 }
411
412 double getFloat32(int byteOffset) {
413 return _getFloat32(byteOffset);
414 }
415
416 void setFloat32(int byteOffset, double value) {
417 _setFloat32(byteOffset, value);
418 }
419
420 double getFloat64(int byteOffset) {
421 return _getFloat64(byteOffset);
422 }
423
424 void setFloat64(int byteOffset, double value) {
425 _setFloat64(byteOffset, value);
426 }
427
428 // Implementation
429
430 int _getInt8(int byteOffset)
431 native "ExternalByteArray_getInt8";
432
433 void _setInt8(int byteOffset, int value)
434 native "ExternalByteArray_setInt8";
435
436 int _getUint8(int byteOffset)
437 native "ExternalByteArray_getUint8";
438
439 void _setUint8(int byteOffset, int value)
440 native "ExternalByteArray_setUint8";
441
442 int _getInt16(int byteOffset)
443 native "ExternalByteArray_getInt16";
444
445 void _setInt16(int byteOffset, int value)
446 native "ExternalByteArray_setInt16";
447
448 int _getUint16(int byteOffset)
449 native "ExternalByteArray_getUint16";
450
451 void _setUint16(int byteOffset, int value)
452 native "ExternalByteArray_setUint16";
453
454 int _getInt32(int byteOffset)
455 native "ExternalByteArray_getInt32";
456
457 void _setInt32(int byteOffset, int value)
458 native "ExternalByteArray_setInt32";
459
460 int _getUint32(int byteOffset)
461 native "ExternalByteArray_getUint32";
462
463 void _setUint32(int byteOffset, int value)
464 native "ExternalByteArray_setUint32";
465
466 int _getInt64(int byteOffset)
467 native "ExternalByteArray_getInt64";
468
469 void _setInt64(int byteOffset, int value)
470 native "ExternalByteArray_setInt64";
471
472 int _getUint64(int byteOffset)
473 native "ExternalByteArray_getUint64";
474
475 void _setUint64(int byteOffset, int value)
476 native "ExternalByteArray_setUint64";
477
478 double _getFloat32(int byteOffset)
479 native "ExternalByteArray_getFloat32";
480
481 void _setFloat32(int byteOffset, double value)
482 native "ExternalByteArray_setFloat32";
483
484 double _getFloat64(int byteOffset)
485 native "ExternalByteArray_getFloat64";
486
487 void _setFloat64(int byteOffset, double value)
488 native "ExternalByteArray_setFloat64";
489 }
OLDNEW
« no previous file with comments | « runtime/lib/byte_array.cc ('k') | runtime/lib/byte_buffer.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698