Skip to content

Commit 117cc16

Browse files
jtennerdcodeIO
authored andcommitted
Implement TypedArray#map (AssemblyScript#365)
* Also cleans up TypedArray#reduce/reduceRight test cases * Also fixes the (what needs to be) overloads on Uint8ClampedArray
1 parent 86c084a commit 117cc16

File tree

5 files changed

+3637
-515
lines changed

5 files changed

+3637
-515
lines changed

std/assembly/internal/typedarray.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,3 +177,18 @@ export function REDUCE_RIGHT<TArray extends TypedArray<T>, T, TRet>(
177177
}
178178
return initialValue;
179179
}
180+
181+
@inline
182+
export function MAP<TArray extends TypedArray<T>, T>(
183+
array: TArray,
184+
callbackfn: (value: T, index: i32, self: TArray) => T,
185+
): TArray {
186+
var length: i32 = array.length;
187+
var result = instantiate<TArray>(length);
188+
var i: i32 = 0;
189+
while (i < length) {
190+
unchecked(result[i] = callbackfn(array[i], i, <TArray>array));
191+
++i;
192+
}
193+
return result;
194+
}

std/assembly/typedarray.ts

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import {
44
SORT,
55
SUBARRAY,
66
REDUCE,
7-
REDUCE_RIGHT
7+
REDUCE_RIGHT,
8+
MAP,
89
} from "./internal/typedarray";
910

1011
import {
@@ -39,6 +40,10 @@ export class Int8Array extends TypedArray<i8> {
3940
): T {
4041
return REDUCE_RIGHT<Int8Array, i8, T>(this, callbackfn, initialValue);
4142
}
43+
44+
map(callbackfn: (value: i8, index: i32, self: Int8Array) => i8): Int8Array {
45+
return MAP<Int8Array, i8>(this, callbackfn);
46+
}
4247
}
4348

4449
export class Uint8Array extends TypedArray<u8> {
@@ -69,6 +74,10 @@ export class Uint8Array extends TypedArray<u8> {
6974
): T {
7075
return REDUCE_RIGHT<Uint8Array, u8, T>(this, callbackfn, initialValue);
7176
}
77+
78+
map(callbackfn: (value: u8, index: i32, self: Uint8Array) => u8): Uint8Array {
79+
return MAP<Uint8Array, u8>(this, callbackfn);
80+
}
7281
}
7382

7483
export class Uint8ClampedArray extends Uint8Array {
@@ -83,6 +92,22 @@ export class Uint8ClampedArray extends Uint8Array {
8392
protected __unchecked_set(index: i32, value: i32): void {
8493
super.__unchecked_set(index, max(min(value, 255), 0));
8594
}
95+
96+
fill(value: u32, start: i32 = 0, end: i32 = i32.MAX_VALUE): Uint8ClampedArray {
97+
return changetype<Uint8ClampedArray>(super.fill(value, start, end)); // safe because '.fill' reuses 'this'
98+
}
99+
100+
sort(comparator: (a: u8, b: u8) => i32 = defaultComparator<u8>()): Uint8ClampedArray {
101+
return changetype<Uint8ClampedArray>(super.sort(comparator)); // safe because '.sort' reuses 'this'
102+
}
103+
104+
subarray(begin: i32 = 0, end: i32 = 0x7fffffff): Uint8ClampedArray {
105+
return SUBARRAY<Uint8ClampedArray, u8>(this, begin, end);
106+
}
107+
108+
map(callbackfn: (value: u8, index: i32, self: Uint8ClampedArray) => u8): Uint8ClampedArray {
109+
return MAP<Uint8ClampedArray, u8>(this, callbackfn);
110+
}
86111
}
87112

88113
export class Int16Array extends TypedArray<i16> {
@@ -113,6 +138,10 @@ export class Int16Array extends TypedArray<i16> {
113138
): T {
114139
return REDUCE_RIGHT<Int16Array, i16, T>(this, callbackfn, initialValue);
115140
}
141+
142+
map(callbackfn: (value: i16, index: i32, self: Int16Array) => i16): Int16Array {
143+
return MAP<Int16Array, i16>(this, callbackfn);
144+
}
116145
}
117146

118147
export class Uint16Array extends TypedArray<u16> {
@@ -143,6 +172,10 @@ export class Uint16Array extends TypedArray<u16> {
143172
): T {
144173
return REDUCE_RIGHT<Uint16Array, u16, T>(this, callbackfn, initialValue);
145174
}
175+
176+
map(callbackfn: (value: u16, index: i32, self: Uint16Array) => u16): Uint16Array {
177+
return MAP<Uint16Array, u16>(this, callbackfn);
178+
}
146179
}
147180

148181
export class Int32Array extends TypedArray<i32> {
@@ -173,6 +206,10 @@ export class Int32Array extends TypedArray<i32> {
173206
): T {
174207
return REDUCE_RIGHT<Int32Array, i32, T>(this, callbackfn, initialValue);
175208
}
209+
210+
map(callbackfn: (value: i32, index: i32, self: Int32Array) => i32): Int32Array {
211+
return MAP<Int32Array, i32>(this, callbackfn);
212+
}
176213
}
177214

178215
export class Uint32Array extends TypedArray<u32> {
@@ -203,6 +240,10 @@ export class Uint32Array extends TypedArray<u32> {
203240
): T {
204241
return REDUCE_RIGHT<Uint32Array, u32, T>(this, callbackfn, initialValue);
205242
}
243+
244+
map(callbackfn: (value: u32, index: i32, self: Uint32Array) => u32): Uint32Array {
245+
return MAP<Uint32Array, u32>(this, callbackfn);
246+
}
206247
}
207248

208249
export class Int64Array extends TypedArray<i64> {
@@ -233,6 +274,10 @@ export class Int64Array extends TypedArray<i64> {
233274
): T {
234275
return REDUCE_RIGHT<Int64Array, i64, T>(this, callbackfn, initialValue);
235276
}
277+
278+
map(callbackfn: (value: i64, index: i32, self: Int64Array) => i64): Int64Array {
279+
return MAP<Int64Array, i64>(this, callbackfn);
280+
}
236281
}
237282

238283
export class Uint64Array extends TypedArray<u64> {
@@ -263,6 +308,10 @@ export class Uint64Array extends TypedArray<u64> {
263308
): T {
264309
return REDUCE_RIGHT<Uint64Array, u64, T>(this, callbackfn, initialValue);
265310
}
311+
312+
map(callbackfn: (value: u64, index: i32, self: Uint64Array) => u64): Uint64Array {
313+
return MAP<Uint64Array, u64>(this, callbackfn);
314+
}
266315
}
267316

268317
export class Float32Array extends TypedArray<f32> {
@@ -293,6 +342,10 @@ export class Float32Array extends TypedArray<f32> {
293342
): T {
294343
return REDUCE_RIGHT<Float32Array, f32, T>(this, callbackfn, initialValue);
295344
}
345+
346+
map(callbackfn: (value: f32, index: i32, self: Float32Array) => f32): Float32Array {
347+
return MAP<Float32Array, f32>(this, callbackfn);
348+
}
296349
}
297350

298351
export class Float64Array extends TypedArray<f64> {
@@ -323,4 +376,8 @@ export class Float64Array extends TypedArray<f64> {
323376
): T {
324377
return REDUCE_RIGHT<Float64Array, f64, T>(this, callbackfn, initialValue);
325378
}
379+
380+
map(callbackfn: (value: f64, index: i32, self: Float64Array) => f64): Float64Array {
381+
return MAP<Float64Array, f64>(this, callbackfn);
382+
}
326383
}

0 commit comments

Comments
 (0)