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

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

Issue 8429027: Add an external array type to support typed arrays. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Address final review comments Created 9 years, 1 month 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_buffer.cc ('k') | runtime/lib/lib_sources.gypi » ('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 ByteBuffer implements List {
6 factory ByteBuffer(int length) {
7 return _allocate(length);
8 }
9
10 int getInt8(int byteOffset) {
11 return _getInt8(byteOffset);
12 }
13 void setInt8(int byteOffset, int value) {
14 _setInt8(byteOffset, value);
15 }
16
17 int getUint8(int byteOffset) {
18 return _getUint8(byteOffset);
19 }
20 void setUint8(int byteOffset, int value) {
21 _setUint8(byteOffset, value);
22 }
23
24 int getInt16(int byteOffset) {
25 return _getInt16(byteOffset);
26 }
27 void setInt16(int byteOffset, int value) {
28 _setInt16(byteOffset, value);
29 }
30
31 int getUint16(int byteOffset) {
32 return _getInt16(byteOffset);
33 }
34 void setUint16(int byteOffset, int value) {
35 _setUint16(byteOffset, value);
36 }
37
38 int getInt32(int byteOffset) {
39 return _getInt32(byteOffset);
40 }
41 void setInt32(int byteOffset, int value) {
42 _setInt32(byteOffset, value);
43 }
44
45 int getUint32(int byteOffset) {
46 return _getUint32(byteOffset);
47 }
48 void setUint32(int byteOffset, int value) {
49 _setUint32(byteOffset, value);
50 }
51
52 int getInt64(int byteOffset) {
53 return _getInt64(byteOffset);
54 }
55 void setInt64(int byteOffset, int value) {
56 _setInt64(byteOffset, value);
57 }
58
59 int getUint64(int byteOffset) {
60 return _getUint64(byteOffset);
61 }
62 void setUint64(int byteOffset, int value) {
63 _setUint64(byteOffset, value);
64 }
65
66 double getFloat32(int byteOffset) {
67 return _getFloat32(byteOffset);
68 }
69 void setFloat32(int byteOffset, double value) {
70 _setFloat32(byteOffset, value);
71 }
72
73 double getFloat64(int byteOffset) {
74 return _getFloat64(byteOffset);
75 }
76 void setFloat64(int byteOffset, double value) {
77 _setFloat64(byteOffset, value);
78 }
79
80 // Iterable interface
81
82 Iterator iterator() {
83 return new ByteBufferIterator(this);
84 }
85
86 // Collection interface
87
88 int get length() {
89 return _length();
90 }
91
92 bool every(bool f(int element)) {
93 return Collections.every(this, f);
94 }
95
96 Collection filter(bool f(int element)) {
97 return Collections.filter(this, new GrowableObjectArray(), f);
98 }
99
100 void forEach(f(int element)) {
101 Collections.forEach(this, f);
102 }
103
104 bool isEmpty() {
105 return this.length === 0;
106 }
107
108 bool some(bool f(int element)) {
109 return Collections.some(this, f);
110 }
111
112 // List interface
113
114 int operator[](int index) {
115 return _getUint8(index);
116 }
117
118 void operator[]=(int index, int value) {
119 _setUint8(index, value);
120 }
121
122 void set length(int newLength) {
123 throw const UnsupportedOperationException("Cannot add to a byte buffer");
124 }
125
126 void add(int element) {
127 throw const UnsupportedOperationException("Cannot add to a byte buffer");
128 }
129
130 void addLast(int element) {
131 add(element);
132 }
133
134 void addAll(Collection elements) {
135 throw const UnsupportedOperationException("Cannot add to a byte buffer");
136 }
137
138 void sort(int compare(int a, b)) {
139 DualPivotQuicksort.sort(this, compare);
140 }
141
142 int indexOf(int element, [int start = 0]) {
143 return Arrays.indexOf(this, element, start, this.length);
144 }
145
146 int lastIndexOf(int element, [int start = null]) {
147 if (start === null) start = length - 1;
148 return Arrays.lastIndexOf(this, element, start);
149 }
150
151 void clear() {
152 throw const UnsupportedOperationException("Cannot clear a byte buffer");
153 }
154
155 int removeLast() {
156 throw const UnsupportedOperationException(
157 "Cannot remove from a byte buffer");
158 }
159
160 int last() {
161 return this[length - 1];
162 }
163
164 // Implementation
165
166 static ByteBuffer _allocate(int length) native "ByteBuffer_allocate";
167
168 int _length() native "ByteBuffer_getLength";
169
170 int _getInt8(int byteOffset) native "ByteBuffer_getInt8";
171 void _setInt8(int byteOffset, int value) native "ByteBuffer_setInt8";
172
173 int _getUint8(int byteOffset) native "ByteBuffer_getUint8";
174 void _setUint8(int byteOffset, int value) native "ByteBuffer_setUint8";
175
176 int _getInt16(int byteOffset) native "ByteBuffer_getInt16";
177 void _setInt16(int byteOffset, int value) native "ByteBuffer_setInt16";
178
179 int _getUint16(int byteOffset) native "ByteBuffer_getUint16";
180 void _setUint16(int byteOffset, int value) native "ByteBuffer_setUint16";
181
182 int _getInt32(int byteOffset) native "ByteBuffer_getInt32";
183 void _setInt32(int byteOffset, int value) native "ByteBuffer_setInt32";
184
185 int _getUint32(int byteOffset) native "ByteBuffer_getUint32";
186 void _setUint32(int byteOffset, int value) native "ByteBuffer_setUint32";
187
188 int _getInt64(int byteOffset) native "ByteBuffer_getInt64";
189 void _setInt64(int byteOffset, int value) native "ByteBuffer_setInt64";
190
191 int _getUint64(int byteOffset) native "ByteBuffer_getUint64";
192 void _setUint64(int byteOffset, int value) native "ByteBuffer_setUint64";
193
194 double _getFloat32(int byteOffset) native "ByteBuffer_getFloat32";
195 void _setFloat32(int byteOffset, double value) native "ByteBuffer_setFloat32";
196
197 double _getFloat64(int byteOffset) native "ByteBuffer_getFloat64";
198 void _setFloat64(int byteOffset, double value) native "ByteBuffer_setFloat64";
199 }
200
201
202 class ByteBufferIterator implements Iterator {
203 ByteBufferIterator(List byteBuffer)
204 : _byteBuffer = byteBuffer, _length = byteBuffer.length, _pos = 0 {
205 assert(byteBuffer is ByteBuffer);
206 }
207
208 bool hasNext() {
209 return _length > _pos;
210 }
211
212 int next() {
213 if (!hasNext()) {
214 throw const NoMoreElementsException();
215 }
216 return _byteBuffer[_pos++];
217 }
218
219 final List _byteBuffer;
220 final int _length; // Cache byte buffer length for faster access.
221 int _pos;
222 }
OLDNEW
« no previous file with comments | « runtime/lib/byte_buffer.cc ('k') | runtime/lib/lib_sources.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698