-
-
Notifications
You must be signed in to change notification settings - Fork 647
/
typedarray.ts
365 lines (333 loc) · 10.6 KB
/
typedarray.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
import {
HEADER_SIZE as AB_HEADER_SIZE,
MAX_BLENGTH as AB_MAX_BLENGTH,
allocateUnsafe,
LOAD,
STORE
} from "./arraybuffer";
import {
SORT as SORT_IMPL
} from "./sort";
/** Typed array base class. Not a global object. */
export abstract class TypedArray<T> {
[key: number]: T; // compatibility only
readonly buffer: ArrayBuffer;
readonly byteOffset: i32;
readonly byteLength: i32;
constructor(length: i32) {
const MAX_LENGTH = <u32>AB_MAX_BLENGTH / sizeof<T>();
if (<u32>length > MAX_LENGTH) throw new RangeError("Invalid typed array length");
var byteLength = length << alignof<T>();
var buffer = allocateUnsafe(byteLength);
memory.fill(changetype<usize>(buffer) + AB_HEADER_SIZE, 0, <usize>byteLength);
this.buffer = buffer;
this.byteOffset = 0;
this.byteLength = byteLength;
}
@inline
get length(): i32 {
return this.byteLength >>> alignof<T>();
}
@operator("[]")
protected __get(index: i32): T {
if (<u32>index >= <u32>(this.byteLength >>> alignof<T>())) throw new Error("Index out of bounds");
return LOAD<T>(this.buffer, index, this.byteOffset);
}
@inline @operator("{}")
protected __unchecked_get(index: i32): T {
return LOAD<T>(this.buffer, index, this.byteOffset);
}
@operator("[]=")
protected __set(index: i32, value: native<T>): void {
if (<u32>index >= <u32>(this.byteLength >>> alignof<T>())) throw new Error("Index out of bounds");
STORE<T,native<T>>(this.buffer, index, value, this.byteOffset);
}
@inline @operator("{}=")
protected __unchecked_set(index: i32, value: native<T>): void {
STORE<T,native<T>>(this.buffer, index, value, this.byteOffset);
}
// copyWithin(target: i32, start: i32, end: i32 = this.length): this
}
@inline
export function FILL<TArray extends TypedArray<T>, T extends number>(
array: TArray,
value: native<T>,
start: i32,
end: i32
): TArray {
var buffer = array.buffer;
var byteOffset = array.byteOffset;
var len = array.length;
start = start < 0 ? max(len + start, 0) : min(start, len);
end = end < 0 ? max(len + end, 0) : min(end, len);
if (sizeof<T>() == 1) {
if (start < end) {
memory.fill(
changetype<usize>(buffer) + start + byteOffset + AB_HEADER_SIZE,
<u8>value,
<usize>(end - start)
);
}
} else {
for (; start < end; ++start) {
STORE<T,native<T>>(buffer, start, value, byteOffset);
}
}
return array;
}
@inline
export function SORT<TArray extends TypedArray<T>, T>(
array: TArray,
comparator: (a: T, b: T) => i32
): TArray {
var byteOffset = array.byteOffset;
var length = array.length;
if (length <= 1) return array;
var buffer = array.buffer;
if (length == 2) {
let a = LOAD<T>(buffer, 1, byteOffset);
let b = LOAD<T>(buffer, 0, byteOffset);
if (comparator(a, b) < 0) {
STORE<T>(buffer, 1, b, byteOffset);
STORE<T>(buffer, 0, a, byteOffset);
}
return array;
}
SORT_IMPL<T>(buffer, byteOffset, length, comparator);
return array;
}
@inline
export function SUBARRAY<TArray extends TypedArray<T>, T>(
array: TArray,
begin: i32,
end: i32
): TArray {
var length = <i32>array.length;
if (begin < 0) begin = max(length + begin, 0);
else begin = min(begin, length);
if (end < 0) end = max(length + end, begin);
else end = max(min(end, length), begin);
var slice = memory.allocate(offsetof<TArray>());
store<usize>(slice, array.buffer, offsetof<TArray>("buffer"));
store<i32>(slice, <i32>array.byteOffset + (begin << alignof<T>()), offsetof<TArray>("byteOffset"));
store<i32>(slice, (end - begin) << alignof<T>(), offsetof<TArray>("byteLength"));
return changetype<TArray>(slice);
}
@inline
export function REDUCE<TArray extends TypedArray<T>, T, TRet>(
array: TArray,
callbackfn: (accumulator: TRet, value: T, index: i32, array: TArray) => TRet,
initialValue: TRet
): TRet {
var length = array.length;
var buffer = array.buffer;
var byteOffset = array.byteOffset;
for (let i = 0; i < length; i++) {
initialValue = callbackfn(
initialValue,
LOAD<T>(buffer, i, byteOffset),
i,
array,
);
}
return initialValue;
}
@inline
export function REDUCE_RIGHT<TArray extends TypedArray<T>, T, TRet>(
array: TArray,
callbackfn: (accumulator: TRet, value: T, index: i32, array: TArray) => TRet,
initialValue: TRet
): TRet {
var buffer = array.buffer;
var byteOffset = array.byteOffset;
for (let i = array.length - 1; i >= 0; i--) {
initialValue = callbackfn(
initialValue,
LOAD<T>(buffer, i, byteOffset),
i,
array,
);
}
return initialValue;
}
@inline
export function MAP<TArray extends TypedArray<T>, T>(
array: TArray,
callbackfn: (value: T, index: i32, self: TArray) => T,
): TArray {
var length = array.length;
var buffer = array.buffer;
var byteOffset = array.byteOffset;
var result = instantiate<TArray>(length);
var resultBuffer = result.buffer;
for (let i = 0; i < length; i++) {
STORE<T, native<T>>(resultBuffer, i, <native<T>>callbackfn(LOAD<T>(buffer, i, byteOffset), i, array));
}
return result;
}
@inline
export function FIND_INDEX<TArray extends TypedArray<T>, T>(
array: TArray,
callbackfn: (value: T, index: i32, array: TArray) => bool,
): i32 {
var length = array.length;
var buffer = array.buffer;
var byteOffset = array.byteOffset;
for (let i = 0; i < length; i++) {
if (callbackfn(LOAD<T>(buffer, i, byteOffset), i, array)) {
return i;
}
}
return -1;
}
@inline
export function SOME<TArray extends TypedArray<T>, T>(
array: TArray,
callbackfn: (value: T, index: i32, array: TArray) => bool,
): bool {
var length = array.length;
var buffer = array.buffer;
var byteOffset = array.byteOffset;
for (let i = 0; i < length; i++) {
if (callbackfn(LOAD<T>(buffer, i, byteOffset), i, array)) {
return true;
}
}
return false;
}
@inline
export function EVERY<TArray extends TypedArray<T>, T>(
array: TArray,
callbackfn: (value: T, index: i32, array: TArray) => bool,
): bool {
var length = array.length;
var buffer = array.buffer;
var byteOffset = array.byteOffset;
for (let i = 0; i < length; i++) {
if (callbackfn(LOAD<T>(buffer, i, byteOffset), i, array)) {
continue;
}
return false;
}
return true;
}
@inline
export function SET<T extends TypedArray<U>, U extends number, SourceT, SourceU extends number>(
target: T,
source: SourceT,
offset: i32): void {
// assert target and source are not null
if (isReference<T>(target)) {
assert(target != null, "TypeError: Target is null.");
} else {
assert(false, "TypeError: T is not a reference.");
}
if (isReference<SourceT>(source)) {
assert(source != null, "TypeError: Source is null.");
} else {
assert(false, "TypeError: SourceT is not a reference.");
}
if (isArray<SourceT>(source)) {
// check to see if the offsets are in range
let sourceLength = source.length;
let targetLength = target.length;
assert((sourceLength + offset) <= targetLength, "RangeError: Offset is too large.");
// cache the buffer and the byteOffset
let targetBuffer = target.buffer;
let targetByteOffset = target.byteOffset;
// for each source value, write it to the ArrayBuffer
for (let i = 0; i < sourceLength; i++) {
STORE<U>(
targetBuffer,
i + offset,
<U>unchecked(source[i]),
targetByteOffset,
);
}
// fast path: source has the same backing type as targe
} else if (ArrayBuffer.isView<SourceT>(source)) {
// validate the lengths are within range
let sourceLength = source.length;
let targetLength = target.length;
assert((sourceLength + offset) <= targetLength, "RangeError: Offset is too large.");
if (isFloat<U>()) {
if (isFloat<SourceU>()) {
if (sizeof<U>() == sizeof<SourceU>()) {
SET_SAME<T, U>(target, <T>source, offset);
} else {
SET_DIFFERENT<T, U, SourceT, SourceU>(target, source, sourceLength, offset);
}
} else {
SET_DIFFERENT<T, U, SourceT, SourceU>(target, source, sourceLength, offset);
}
} else if (isInteger<U>()) {
if (isInteger<SourceU>()) {
if (sizeof<U>() == sizeof<SourceU>()) {
SET_SAME<T, U>(target, <T>source, offset);
} else {
SET_DIFFERENT<T, U, SourceT, SourceU>(target, source, sourceLength, offset);
}
} else {
SET_DIFFERENT<T, U, SourceT, SourceU>(target, source, sourceLength, offset);
}
}
} else {
// validate the lengths are within range
// @ts-ignore: source is assumed to have a length property
let sourceLength = source.length;
let targetLength = target.length;
assert((sourceLength + offset) <= targetLength, "RangeError: Offset is too large.");
// cache the buffer and the offset
let targetBuffer = target.buffer;
let targetByteOffset = target.byteOffset;
/**
* In order for the source to be ArrayLike, it has to have a length property, and a
* `@operator("[]=")` getter. This is very slow because it doesn't allow for unchecked gets,
* but it is as standard compliant as we can make it.
*/
// @ts-ignore: Source is expected to have a length property
for (let i = source.length - 1; i >= 0; i--) {
STORE<U>(
targetBuffer,
i + offset,
// @ts-ignore: Source is expected to have a getter signature
<U>source[i], // if the object does not have a getter this throws a compiler error
targetByteOffset,
);
}
}
}
@inline
function SET_SAME<T extends TypedArray<U>, U extends number>(target: T, source: T, offset: i32): void {
// perform a memory.copy
memory.copy(
// store the data at the target pointer + byteOFfset + offset << alignOf<U>()
target.buffer.data + target.byteOffset + (offset << alignof<U>()),
// read the data from source pointer + byteOffset
// @ts-ignore: source has a buffer and a byteOffset property because it's instanceof T
source.buffer.data + source.byteOffset,
// @ts-ignore: store source.buffer.byteLength number of bytes
source.buffer.byteLength,
);
}
@inline
function SET_DIFFERENT<
T extends TypedArray<U>,
U extends number,
SourceT extends TypedArray<SourceU>,
SourceU extends number
>(target: T, source: SourceT, sourceLength: i32, offset: i32): void {
let sourceBuffer = source.buffer;
let targetBuffer = target.buffer;
let sourceOffset = source.byteOffset;
let targetOffset = target.byteOffset;
for (let i = 0; i < sourceLength; i++) {
STORE<U>(
targetBuffer,
i + offset,
// @ts-ignore: Number values can be cast to each other
<U>LOAD<SourceU>(sourceBuffer, i, sourceOffset),
targetOffset,
);
}
}