Implement TypedArray#map (#365)

* Also cleans up TypedArray#reduce/reduceRight test cases
* Also fixes the (what needs to be) overloads on Uint8ClampedArray
This commit is contained in:
jtenner 2018-12-07 13:40:21 -05:00 committed by Daniel Wirtz
parent 86c084a519
commit 117cc1629b
5 changed files with 3636 additions and 514 deletions

View File

@ -177,3 +177,18 @@ export function REDUCE_RIGHT<TArray extends TypedArray<T>, T, TRet>(
}
return initialValue;
}
@inline
export function MAP<TArray extends TypedArray<T>, T>(
array: TArray,
callbackfn: (value: T, index: i32, self: TArray) => T,
): TArray {
var length: i32 = array.length;
var result = instantiate<TArray>(length);
var i: i32 = 0;
while (i < length) {
unchecked(result[i] = callbackfn(array[i], i, <TArray>array));
++i;
}
return result;
}

View File

@ -4,7 +4,8 @@ import {
SORT,
SUBARRAY,
REDUCE,
REDUCE_RIGHT
REDUCE_RIGHT,
MAP,
} from "./internal/typedarray";
import {
@ -39,6 +40,10 @@ export class Int8Array extends TypedArray<i8> {
): T {
return REDUCE_RIGHT<Int8Array, i8, T>(this, callbackfn, initialValue);
}
map(callbackfn: (value: i8, index: i32, self: Int8Array) => i8): Int8Array {
return MAP<Int8Array, i8>(this, callbackfn);
}
}
export class Uint8Array extends TypedArray<u8> {
@ -69,6 +74,10 @@ export class Uint8Array extends TypedArray<u8> {
): T {
return REDUCE_RIGHT<Uint8Array, u8, T>(this, callbackfn, initialValue);
}
map(callbackfn: (value: u8, index: i32, self: Uint8Array) => u8): Uint8Array {
return MAP<Uint8Array, u8>(this, callbackfn);
}
}
export class Uint8ClampedArray extends Uint8Array {
@ -83,6 +92,22 @@ export class Uint8ClampedArray extends Uint8Array {
protected __unchecked_set(index: i32, value: i32): void {
super.__unchecked_set(index, max(min(value, 255), 0));
}
fill(value: u32, start: i32 = 0, end: i32 = i32.MAX_VALUE): Uint8ClampedArray {
return changetype<Uint8ClampedArray>(super.fill(value, start, end)); // safe because '.fill' reuses 'this'
}
sort(comparator: (a: u8, b: u8) => i32 = defaultComparator<u8>()): Uint8ClampedArray {
return changetype<Uint8ClampedArray>(super.sort(comparator)); // safe because '.sort' reuses 'this'
}
subarray(begin: i32 = 0, end: i32 = 0x7fffffff): Uint8ClampedArray {
return SUBARRAY<Uint8ClampedArray, u8>(this, begin, end);
}
map(callbackfn: (value: u8, index: i32, self: Uint8ClampedArray) => u8): Uint8ClampedArray {
return MAP<Uint8ClampedArray, u8>(this, callbackfn);
}
}
export class Int16Array extends TypedArray<i16> {
@ -113,6 +138,10 @@ export class Int16Array extends TypedArray<i16> {
): T {
return REDUCE_RIGHT<Int16Array, i16, T>(this, callbackfn, initialValue);
}
map(callbackfn: (value: i16, index: i32, self: Int16Array) => i16): Int16Array {
return MAP<Int16Array, i16>(this, callbackfn);
}
}
export class Uint16Array extends TypedArray<u16> {
@ -143,6 +172,10 @@ export class Uint16Array extends TypedArray<u16> {
): T {
return REDUCE_RIGHT<Uint16Array, u16, T>(this, callbackfn, initialValue);
}
map(callbackfn: (value: u16, index: i32, self: Uint16Array) => u16): Uint16Array {
return MAP<Uint16Array, u16>(this, callbackfn);
}
}
export class Int32Array extends TypedArray<i32> {
@ -173,6 +206,10 @@ export class Int32Array extends TypedArray<i32> {
): T {
return REDUCE_RIGHT<Int32Array, i32, T>(this, callbackfn, initialValue);
}
map(callbackfn: (value: i32, index: i32, self: Int32Array) => i32): Int32Array {
return MAP<Int32Array, i32>(this, callbackfn);
}
}
export class Uint32Array extends TypedArray<u32> {
@ -203,6 +240,10 @@ export class Uint32Array extends TypedArray<u32> {
): T {
return REDUCE_RIGHT<Uint32Array, u32, T>(this, callbackfn, initialValue);
}
map(callbackfn: (value: u32, index: i32, self: Uint32Array) => u32): Uint32Array {
return MAP<Uint32Array, u32>(this, callbackfn);
}
}
export class Int64Array extends TypedArray<i64> {
@ -233,6 +274,10 @@ export class Int64Array extends TypedArray<i64> {
): T {
return REDUCE_RIGHT<Int64Array, i64, T>(this, callbackfn, initialValue);
}
map(callbackfn: (value: i64, index: i32, self: Int64Array) => i64): Int64Array {
return MAP<Int64Array, i64>(this, callbackfn);
}
}
export class Uint64Array extends TypedArray<u64> {
@ -263,6 +308,10 @@ export class Uint64Array extends TypedArray<u64> {
): T {
return REDUCE_RIGHT<Uint64Array, u64, T>(this, callbackfn, initialValue);
}
map(callbackfn: (value: u64, index: i32, self: Uint64Array) => u64): Uint64Array {
return MAP<Uint64Array, u64>(this, callbackfn);
}
}
export class Float32Array extends TypedArray<f32> {
@ -293,6 +342,10 @@ export class Float32Array extends TypedArray<f32> {
): T {
return REDUCE_RIGHT<Float32Array, f32, T>(this, callbackfn, initialValue);
}
map(callbackfn: (value: f32, index: i32, self: Float32Array) => f32): Float32Array {
return MAP<Float32Array, f32>(this, callbackfn);
}
}
export class Float64Array extends TypedArray<f64> {
@ -323,4 +376,8 @@ export class Float64Array extends TypedArray<f64> {
): T {
return REDUCE_RIGHT<Float64Array, f64, T>(this, callbackfn, initialValue);
}
map(callbackfn: (value: f64, index: i32, self: Float64Array) => f64): Float64Array {
return MAP<Float64Array, f64>(this, callbackfn);
}
}

File diff suppressed because it is too large Load Diff

View File

@ -227,8 +227,8 @@ assert(multisubarr3.byteLength === 3);
/**
* Reduce test suite:
* Each function is designed to test a simple sum reduction. In each test it initialized the tested
* typedarray, and sets the values manually. Then it calls `TypedArray.prototype.reduce` with a
* The reduce test is designed to test a simple sum reduction. In each test it instantiates the
* tested typedarray, and sets the values manually. Then it calls `TypedArray.prototype.reduce` with a
* single sum arrow function reduction. For each reduction, it verifies the `self` parameter is the
* instantiated array, the index is the correct index, and it increments the testIndex variable.
* Finally, it asserts the value is 6.
@ -236,367 +236,86 @@ assert(multisubarr3.byteLength === 3);
* TODO: When function closure support is added, remove the function comments to fully verify the
* tests work.
*/
function reduceInt8ArrayTest(): void {
var array: Int8Array = new Int8Array(3);
array[0] = 1;
array[1] = 2;
array[2] = 3;
// var testindex: i32 = 0;
var result = array.reduce<i8>((acc: i8, val: i8, index: i32, self: Int8Array): i8 => {
// assert(testindex == index);
// assert(array == self);
// ++testindex;
return acc + val;
}, 0);
assert(result == 6);
}
function reduceUint8ArrayTest(): void {
var array: Uint8Array = new Uint8Array(3);
array[0] = 1;
array[1] = 2;
array[2] = 3;
// var testindex: i32 = 0;
var result = array.reduce<u8>((acc: u8, val: u8, index: i32, self: Uint8Array): u8 => {
// assert(testindex == index);
// assert(array == self);
// ++testindex;
return acc + val;
}, 0);
assert(result == 6);
}
function reduceUint8ClampedArrayTest(): void {
var array: Uint8ClampedArray = new Uint8ClampedArray(3);
array[0] = 1;
array[1] = 2;
array[2] = 3;
// var testindex: i32 = 0;
var result = array.reduce<u8>((acc: u8, val: u8, index: i32, self: Uint8ClampedArray): u8 => {
// assert(testindex == index);
// assert(array == self);
// ++testindex;
return acc + val;
}, 0);
assert(result == 6);
}
function reduceInt16ArrayTest(): void {
var array: Int16Array = new Int16Array(3);
array[0] = 1;
array[1] = 2;
array[2] = 3;
// var testindex: i32 = 0;
var result = array.reduce<i16>((acc: i16, val: i16, index: i32, self: Int16Array): i16 => {
// assert(testindex == index);
// assert(array == self);
// ++testindex;
return acc + val;
}, 0);
assert(result == 6);
}
function reduceUint16ArrayTest(): void {
var array: Uint16Array = new Uint16Array(3);
array[0] = 1;
array[1] = 2;
array[2] = 3;
// var testindex: i32 = 0;
var result = array.reduce<u16>((acc: u16, val: u16, index: i32, self: Uint16Array): u16 => {
// assert(testindex == index);
// assert(array == self);
// ++testindex;
return acc + val;
}, 0);
assert(result == 6);
}
function reduceInt32ArrayTest(): void {
var array: Int32Array = new Int32Array(3);
array[0] = 1;
array[1] = 2;
array[2] = 3;
// var testindex: i32 = 0;
var result = array.reduce<i32>((acc: i32, val: i32, index: i32, self: Int32Array): i32 => {
// assert(testindex == index);
// assert(array == self);
// ++testindex;
return acc + val;
}, 0);
assert(result == 6);
}
function reduceUint32ArrayTest(): void {
var array: Uint32Array = new Uint32Array(3);
array[0] = 1;
array[1] = 2;
array[2] = 3;
// var testindex: i32 = 0;
var result = array.reduce<u32>((acc: u32, val: u32, index: i32, self: Uint32Array): u32 => {
// assert(testindex == index);
// assert(array == self);
// ++testindex;
return acc + val;
}, 0);
assert(result == 6);
}
function reduceInt64ArrayTest(): void {
var array: Int64Array = new Int64Array(3);
array[0] = 1;
array[1] = 2;
array[2] = 3;
// var testindex: i32 = 0;
var result = array.reduce<i64>((acc: i64, val: i64, index: i32, self: Int64Array): i64 => {
// assert(testindex == index);
// assert(array == self);
// ++testindex;
return acc + val;
}, 0);
assert(result == 6);
}
function reduceUint64ArrayTest(): void {
var array: Uint64Array = new Uint64Array(3);
array[0] = 1;
array[1] = 2;
array[2] = 3;
// var testindex: i32 = 0;
var result = array.reduce<u64>((acc: u64, val: u64, index: i32, self: Uint64Array): u64 => {
// assert(testindex == index);
// assert(array == self);
// ++testindex;
return acc + val;
}, 0);
assert(result == 6);
}
function reduceFloat32ArrayTest(): void {
var array: Float32Array = new Float32Array(3);
array[0] = 1;
array[1] = 2;
array[2] = 3;
// var testindex: i32 = 0;
var result = array.reduce<f32>((acc: f32, val: f32, index: i32, self: Float32Array): f32 => {
// assert(testindex == index);
// assert(array == self);
// ++testindex;
return acc + val;
}, 0);
assert(result == 6);
}
function reduceFloat64ArrayTest(): void {
var array: Float64Array = new Float64Array(3);
array[0] = 1;
array[1] = 2;
array[2] = 3;
// var testindex: i32 = 0;
var result = array.reduce<f64>((acc: f64, val: f64, index: i32, self: Float64Array): f64 => {
// assert(testindex == index);
// assert(array == self);
// ++testindex;
return acc + val;
}, 0);
assert(result == 6);
}
reduceInt8ArrayTest();
reduceUint8ArrayTest();
reduceUint8ClampedArrayTest();
reduceInt16ArrayTest();
reduceUint16ArrayTest();
reduceInt32ArrayTest();
reduceUint32ArrayTest();
reduceInt64ArrayTest();
reduceUint64ArrayTest();
reduceFloat32ArrayTest();
reduceFloat64ArrayTest();
/**
* ReduceRight test suite:
* Each function is designed to test a simple sum reduction. In each test it initialized the tested
* typedarray, and sets the values manually. Then it calls `TypedArray.prototype.reduceRight` with a
* single sum arrow function reduction. For each reduction, it verifies the `self` parameter is the
* instantiated array, the index is the correct index, and it increments the testIndex variable.
* Finally, it asserts the value is 6.
*
* TODO: When function closure support is added, remove the function comments to fully verify the
* tests work.
*/
function reduceRightInt8ArrayTest(): void {
var array: Int8Array = new Int8Array(3);
array[0] = 1;
array[1] = 2;
array[2] = 3;
function testReduce<ArrayType, T>(): void {
var array: ArrayType = instantiate<ArrayType>(3);
array[0] = <T>1;
array[1] = <T>2;
array[2] = <T>3;
// var testindex: i32 = 2;
var result = array.reduceRight<i8>((acc: i8, val: i8, index: i32, self: Int8Array): i8 => {
var result = array.reduce<T>((acc: T, val: T, index: i32, self: ArrayType): T => {
// assert(testindex == index);
// assert(array == self);
// --testindex;
return acc + val;
}, 0);
assert(result == 6);
assert(result == <T>6);
}
function reduceRightUint8ArrayTest(): void {
var array: Uint8Array = new Uint8Array(3);
array[0] = 1;
array[1] = 2;
array[2] = 3;
testReduce<Int8Array, i8>();
testReduce<Uint8Array, u8>();
testReduce<Uint8ClampedArray, u8>();
testReduce<Int16Array, i16>();
testReduce<Uint16Array, u16>();
testReduce<Int32Array, i32>();
testReduce<Uint32Array, u32>();
testReduce<Int64Array, i64>();
testReduce<Uint64Array, u64>();
testReduce<Float32Array, f32>();
testReduce<Float64Array, f64>();
function testReduceRight<ArrayType, T>(): void {
var array: ArrayType = instantiate<ArrayType>(3);
array[0] = <T>1;
array[1] = <T>2;
array[2] = <T>3;
// var testindex: i32 = 2;
var result = array.reduceRight<u8>((acc: u8, val: u8, index: i32, self: Uint8Array): u8 => {
var result = array.reduceRight<T>((acc: T, val: T, index: i32, self: ArrayType): T => {
// assert(testindex == index);
// assert(array == self);
// --testindex;
return acc + val;
}, 0);
assert(result == 6);
assert(result == <T>6);
}
function reduceRightUint8ClampedArrayTest(): void {
var array: Uint8ClampedArray = new Uint8ClampedArray(3);
array[0] = 1;
array[1] = 2;
array[2] = 3;
// var testindex: i32 = 2;
var result = array.reduceRight<u8>((acc: u8, val: u8, index: i32, self: Uint8ClampedArray): u8 => {
// assert(testindex == index);
// assert(array == self);
// --testindex;
return acc + val;
}, 0);
assert(result == 6);
testReduceRight<Int8Array, i8>();
testReduceRight<Uint8Array, u8>();
testReduceRight<Uint8ClampedArray, u8>();
testReduceRight<Int16Array, i16>();
testReduceRight<Uint16Array, u16>();
testReduceRight<Int32Array, i32>();
testReduceRight<Uint32Array, u32>();
testReduceRight<Int64Array, i64>();
testReduceRight<Uint64Array, u64>();
testReduceRight<Float32Array, f32>();
testReduceRight<Float64Array, f64>();
function testArrayMap<ArrayType extends TypedArray<T>, T>(): void {
var source: ArrayType = instantiate<ArrayType>(3);
source[0] = <T>1;
source[1] = <T>2;
source[2] = <T>3;
// var testIndex: i32 = 0;
var result = source.map((value: T, index: i32, self: ArrayType): T => {
// assert(self == source);
// assert(testIndex == testIndex);
// testIndex++;
return value * value;
});
assert(result[0] == <T>1);
assert(result[1] == <T>4);
assert(result[2] == <T>9);
}
function reduceRightInt16ArrayTest(): void {
var array: Int16Array = new Int16Array(3);
array[0] = 1;
array[1] = 2;
array[2] = 3;
// var testindex: i32 = 2;
var result = array.reduceRight<i16>((acc: i16, val: i16, index: i32, self: Int16Array): i16 => {
// assert(testindex == index);
// assert(array == self);
// --testindex;
return acc + val;
}, 0);
assert(result == 6);
}
function reduceRightUint16ArrayTest(): void {
var array: Uint16Array = new Uint16Array(3);
array[0] = 1;
array[1] = 2;
array[2] = 3;
// var testindex: i32 = 2;
var result = array.reduceRight<u16>((acc: u16, val: u16, index: i32, self: Uint16Array): u16 => {
// assert(testindex == index);
// assert(array == self);
// --testindex;
return acc + val;
}, 0);
assert(result == 6);
}
function reduceRightInt32ArrayTest(): void {
var array: Int32Array = new Int32Array(3);
array[0] = 1;
array[1] = 2;
array[2] = 3;
// var testindex: i32 = 2;
var result = array.reduceRight<i32>((acc: i32, val: i32, index: i32, self: Int32Array): i32 => {
// assert(testindex == index);
// assert(array == self);
// --testindex;
return acc + val;
}, 0);
assert(result == 6);
}
function reduceRightUint32ArrayTest(): void {
var array: Uint32Array = new Uint32Array(3);
array[0] = 1;
array[1] = 2;
array[2] = 3;
// var testindex: i32 = 2;
var result = array.reduceRight<u32>((acc: u32, val: u32, index: i32, self: Uint32Array): u32 => {
// assert(testindex == index);
// assert(array == self);
// --testindex;
return acc + val;
}, 0);
assert(result == 6);
}
function reduceRightInt64ArrayTest(): void {
var array: Int64Array = new Int64Array(3);
array[0] = 1;
array[1] = 2;
array[2] = 3;
// var testindex: i32 = 2;
var result = array.reduceRight<i64>((acc: i64, val: i64, index: i32, self: Int64Array): i64 => {
// assert(testindex == index);
// assert(array == self);
// --testindex;
return acc + val;
}, 0);
assert(result == 6);
}
function reduceRightUint64ArrayTest(): void {
var array: Uint64Array = new Uint64Array(3);
array[0] = 1;
array[1] = 2;
array[2] = 3;
// var testindex: i32 = 2;
var result = array.reduceRight<u64>((acc: u64, val: u64, index: i32, self: Uint64Array): u64 => {
// assert(testindex == index);
// assert(array == self);
// --testindex;
return acc + val;
}, 0);
assert(result == 6);
}
function reduceRightFloat32ArrayTest(): void {
var array: Float32Array = new Float32Array(3);
array[0] = 1;
array[1] = 2;
array[2] = 3;
// var testindex: i32 = 2;
var result = array.reduceRight<f32>((acc: f32, val: f32, index: i32, self: Float32Array): f32 => {
// assert(testindex == index);
// assert(array == self);
// --testindex;
return acc + val;
}, 0);
assert(result == 6);
}
function reduceRightFloat64ArrayTest(): void {
var array: Float64Array = new Float64Array(3);
array[0] = 1;
array[1] = 2;
array[2] = 3;
// var testindex: i32 = 2;
var result = array.reduceRight<f64>((acc: f64, val: f64, index: i32, self: Float64Array): f64 => {
// assert(testindex == index);
// assert(array == self);
// --testindex;
return acc + val;
}, 0);
assert(result == 6);
}
reduceRightInt8ArrayTest();
reduceRightUint8ArrayTest();
reduceRightUint8ClampedArrayTest();
reduceRightInt16ArrayTest();
reduceRightUint16ArrayTest();
reduceRightInt32ArrayTest();
reduceRightUint32ArrayTest();
reduceRightInt64ArrayTest();
reduceRightUint64ArrayTest();
reduceRightFloat32ArrayTest();
reduceRightFloat64ArrayTest();
testArrayMap<Int8Array, i8>();
testArrayMap<Uint8Array, u8>();
testArrayMap<Uint8ClampedArray, u8>();
testArrayMap<Int16Array, i16>();
testArrayMap<Uint16Array, u16>();
testArrayMap<Int32Array, i32>();
testArrayMap<Uint32Array, u32>();
testArrayMap<Int64Array, i64>();
testArrayMap<Uint64Array, u64>();
testArrayMap<Float32Array, f32>();
testArrayMap<Float64Array, f64>();

File diff suppressed because it is too large Load Diff