Implement TypedArray#reduce/reduceRight (#352)

This commit is contained in:
jtenner 2018-12-05 11:53:31 -05:00 committed by Daniel Wirtz
parent ced01216f8
commit d7f4874650
7 changed files with 5079 additions and 8 deletions

1
NOTICE
View File

@ -10,6 +10,7 @@ under the licensing terms detailed in LICENSE:
* Alan Pierce <alangpierce@gmail.com>
* Palmer <pengliao@live.cn>
* Linus Unnebäck <linus@folkdatorn.se>
* Joshua Tenner <tenner.joshua@gmail.com>
Portions of this software are derived from third-party works licensed under
the following terms:

View File

@ -520,6 +520,16 @@ declare abstract class TypedArray<T> implements ArrayBufferView<T> {
readonly length: i32;
/** Returns a new TypedArray of this type on the same ArrayBuffer from begin inclusive to end exclusive. */
subarray(begin?: i32, end?: i32): this;
/** The reduce() method applies a function against an accumulator and each value of the typed array (from left-to-right) has to reduce it to a single value. This method has the same algorithm as Array.prototype.reduce(). */
reduce<W>(
callbackfn: (accumulator: W, value: T, index: i32, self: this) => W,
initialValue: W,
): W;
/** The reduceRight() method applies a function against an accumulator and each value of the typed array (from left-to-right) has to reduce it to a single value, starting from the end of the array. This method has the same algorithm as Array.prototype.reduceRight(). */
reduceRight<W>(
callbackfn: (accumulator: W, value: T, index: i32, self: this) => W,
initialValue: W,
): W;
}
/** An array of twos-complement 8-bit signed integers. */

View File

@ -126,4 +126,31 @@ export abstract class TypedArray<T,TNative> {
return this;
}
}
/**
* TypedArray reduce implementation. This is a method that will be called from the parent,
* passing types down from the child class using the typed parameters TypedArrayType and
* ReturnType respectively. This implementation requires an initial value, and the direction.
* When direction is true, reduce will reduce from the right side.
*/
@inline
protected reduce_internal<TypedArrayType, ReturnType>(
callbackfn: (accumulator: ReturnType, value: T, index: i32, array: TypedArrayType) => ReturnType,
array: TypedArrayType,
initialValue: ReturnType,
direction: bool = false,
): ReturnType {
var index: i32 = direction ? this.length - 1 : 0;
var length: i32 = direction ? -1 : this.length;
while (index != length) {
initialValue = callbackfn(
initialValue,
this.__unchecked_get(index),
index,
array,
);
index = direction ? index - 1 : index + 1;
}
return initialValue;
}
}

View File

@ -8,6 +8,20 @@ export class Int8Array extends TypedArray<i8,i32> {
subarray(begin: i32 = 0, end: i32 = 0x7fffffff): Int8Array {
return changetype<Int8Array>(super.subarray(begin, end));
}
reduce<ReturnType>(
callbackfn: (accumulator: ReturnType, value: i8, index: i32, array: Int8Array) => ReturnType,
initialValue: ReturnType,
): ReturnType {
return super.reduce_internal<Int8Array, ReturnType>(callbackfn, this, initialValue);
}
reduceRight<ReturnType>(
callbackfn: (accumulator: ReturnType, value: i8, index: i32, array: Int8Array) => ReturnType,
initialValue: ReturnType,
): ReturnType {
return super.reduce_internal<Int8Array, ReturnType>(callbackfn, this, initialValue, true);
}
}
export class Uint8Array extends TypedArray<u8,u32> {
@ -16,6 +30,20 @@ export class Uint8Array extends TypedArray<u8,u32> {
subarray(begin: i32 = 0, end: i32 = 0x7fffffff): Uint8Array {
return changetype<Uint8Array>(super.subarray(begin, end));
}
reduce<ReturnType>(
callbackfn: (accumulator: ReturnType, value: u8, index: i32, array: Uint8Array) => ReturnType,
initialValue: ReturnType,
): ReturnType {
return super.reduce_internal<Uint8Array, ReturnType>(callbackfn, this, initialValue);
}
reduceRight<ReturnType>(
callbackfn: (accumulator: ReturnType, value: u8, index: i32, array: Uint8Array) => ReturnType,
initialValue: ReturnType,
): ReturnType {
return super.reduce_internal<Uint8Array, ReturnType>(callbackfn, this, initialValue, true);
}
}
export class Uint8ClampedArray extends TypedArray<u8,u32> {
@ -34,6 +62,20 @@ export class Uint8ClampedArray extends TypedArray<u8,u32> {
subarray(begin: i32 = 0, end: i32 = 0x7fffffff): Uint8ClampedArray {
return changetype<Uint8ClampedArray>(super.subarray(begin, end));
}
reduce<ReturnType>(
callbackfn: (accumulator: ReturnType, value: u8, index: i32, array: Uint8ClampedArray) => ReturnType,
initialValue: ReturnType,
): ReturnType {
return super.reduce_internal<Uint8ClampedArray, ReturnType>(callbackfn, this, initialValue);
}
reduceRight<ReturnType>(
callbackfn: (accumulator: ReturnType, value: u8, index: i32, array: Uint8ClampedArray) => ReturnType,
initialValue: ReturnType,
): ReturnType {
return super.reduce_internal<Uint8ClampedArray, ReturnType>(callbackfn, this, initialValue, true);
}
}
export class Int16Array extends TypedArray<i16,i32> {
@ -42,6 +84,20 @@ export class Int16Array extends TypedArray<i16,i32> {
subarray(begin: i32 = 0, end: i32 = 0x7fffffff): Int16Array {
return changetype<Int16Array>(super.subarray(begin, end));
}
reduce<ReturnType>(
callbackfn: (accumulator: ReturnType, value: i16, index: i32, array: Int16Array) => ReturnType,
initialValue: ReturnType,
): ReturnType {
return super.reduce_internal<Int16Array, ReturnType>(callbackfn, this, initialValue);
}
reduceRight<ReturnType>(
callbackfn: (accumulator: ReturnType, value: i16, index: i32, array: Int16Array) => ReturnType,
initialValue: ReturnType,
): ReturnType {
return super.reduce_internal<Int16Array, ReturnType>(callbackfn, this, initialValue, true);
}
}
export class Uint16Array extends TypedArray<u16,u32> {
@ -50,6 +106,20 @@ export class Uint16Array extends TypedArray<u16,u32> {
subarray(begin: i32 = 0, end: i32 = 0x7fffffff): Uint16Array {
return changetype<Uint16Array>(super.subarray(begin, end));
}
reduce<ReturnType>(
callbackfn: (accumulator: ReturnType, value: u16, index: i32, array: Uint16Array) => ReturnType,
initialValue: ReturnType,
): ReturnType {
return super.reduce_internal<Uint16Array, ReturnType>(callbackfn, this, initialValue);
}
reduceRight<ReturnType>(
callbackfn: (accumulator: ReturnType, value: u16, index: i32, array: Uint16Array) => ReturnType,
initialValue: ReturnType,
): ReturnType {
return super.reduce_internal<Uint16Array, ReturnType>(callbackfn, this, initialValue, true);
}
}
export class Int32Array extends TypedArray<i32,i32> {
@ -58,6 +128,24 @@ export class Int32Array extends TypedArray<i32,i32> {
subarray(begin: i32 = 0, end: i32 = 0x7fffffff): Int32Array {
return changetype<Int32Array>(super.subarray(begin, end));
}
/**
* @param callbackfn {function} - a function that reduces each value to a ReturnType
* @param initialValue {ReturnType} - the initial ReturnType value to be passed to the callbackfn
*/
reduce<ReturnType>(
callbackfn: (accumulator: ReturnType, value: i32, index: i32, array: Int32Array) => ReturnType,
initialValue: ReturnType,
): ReturnType {
return super.reduce_internal<Int32Array, ReturnType>(callbackfn, this, initialValue);
}
reduceRight<ReturnType>(
callbackfn: (accumulator: ReturnType, value: i32, index: i32, array: Int32Array) => ReturnType,
initialValue: ReturnType,
): ReturnType {
return super.reduce_internal<Int32Array, ReturnType>(callbackfn, this, initialValue, true);
}
}
export class Uint32Array extends TypedArray<u32,u32> {
@ -66,6 +154,20 @@ export class Uint32Array extends TypedArray<u32,u32> {
subarray(begin: i32 = 0, end: i32 = 0x7fffffff): Uint32Array {
return changetype<Uint32Array>(super.subarray(begin, end));
}
reduce<ReturnType>(
callbackfn: (accumulator: ReturnType, value: u32, index: i32, array: Uint32Array) => ReturnType,
initialValue: ReturnType,
): ReturnType {
return super.reduce_internal<Uint32Array, ReturnType>(callbackfn, this, initialValue);
}
reduceRight<ReturnType>(
callbackfn: (accumulator: ReturnType, value: u32, index: i32, array: Uint32Array) => ReturnType,
initialValue: ReturnType,
): ReturnType {
return super.reduce_internal<Uint32Array, ReturnType>(callbackfn, this, initialValue, true);
}
}
export class Int64Array extends TypedArray<i64,i64> {
@ -74,6 +176,20 @@ export class Int64Array extends TypedArray<i64,i64> {
subarray(begin: i32 = 0, end: i32 = 0x7fffffff): Int64Array {
return changetype<Int64Array>(super.subarray(begin, end));
}
reduce<ReturnType>(
callbackfn: (accumulator: ReturnType, value: i64, index: i32, array: Int64Array) => ReturnType,
initialValue: ReturnType,
): ReturnType {
return super.reduce_internal<Int64Array, ReturnType>(callbackfn, this, initialValue);
}
reduceRight<ReturnType>(
callbackfn: (accumulator: ReturnType, value: i64, index: i32, array: Int64Array) => ReturnType,
initialValue: ReturnType,
): ReturnType {
return super.reduce_internal<Int64Array, ReturnType>(callbackfn, this, initialValue, true);
}
}
export class Uint64Array extends TypedArray<u64,u64> {
@ -82,6 +198,20 @@ export class Uint64Array extends TypedArray<u64,u64> {
subarray(begin: i32 = 0, end: i32 = 0x7fffffff): Uint64Array {
return changetype<Uint64Array>(super.subarray(begin, end));
}
reduce<ReturnType>(
callbackfn: (accumulator: ReturnType, value: u64, index: i32, array: Uint64Array) => ReturnType,
initialValue: ReturnType,
): ReturnType {
return super.reduce_internal<Uint64Array, ReturnType>(callbackfn, this, initialValue);
}
reduceRight<ReturnType>(
callbackfn: (accumulator: ReturnType, value: u64, index: i32, array: Uint64Array) => ReturnType,
initialValue: ReturnType,
): ReturnType {
return super.reduce_internal<Uint64Array, ReturnType>(callbackfn, this, initialValue, true);
}
}
export class Float32Array extends TypedArray<f32,f32> {
@ -90,6 +220,20 @@ export class Float32Array extends TypedArray<f32,f32> {
subarray(begin: i32 = 0, end: i32 = 0x7fffffff): Float32Array {
return changetype<Float32Array>(super.subarray(begin, end));
}
reduce<ReturnType>(
callbackfn: (accumulator: ReturnType, value: f32, index: i32, array: Float32Array) => ReturnType,
initialValue: ReturnType,
): ReturnType {
return super.reduce_internal<Float32Array, ReturnType>(callbackfn, this, initialValue);
}
reduceRight<ReturnType>(
callbackfn: (accumulator: ReturnType, value: f32, index: i32, array: Float32Array) => ReturnType,
initialValue: ReturnType,
): ReturnType {
return super.reduce_internal<Float32Array, ReturnType>(callbackfn, this, initialValue, true);
}
}
export class Float64Array extends TypedArray<f64,f64> {
@ -98,4 +242,18 @@ export class Float64Array extends TypedArray<f64,f64> {
subarray(begin: i32 = 0, end: i32 = 0x7fffffff): Float64Array {
return changetype<Float64Array>(super.subarray(begin, end));
}
reduce<ReturnType>(
callbackfn: (accumulator: ReturnType, value: f64, index: i32, array: Float64Array) => ReturnType,
initialValue: ReturnType,
): ReturnType {
return super.reduce_internal<Float64Array, ReturnType>(callbackfn, this, initialValue);
}
reduceRight<ReturnType>(
callbackfn: (accumulator: ReturnType, value: f64, index: i32, array: Float64Array) => ReturnType,
initialValue: ReturnType,
): ReturnType {
return super.reduce_internal<Float64Array, ReturnType>(callbackfn, this, initialValue, true);
}
}

File diff suppressed because it is too large Load Diff

View File

@ -225,3 +225,379 @@ assert(multisubarr3[0] === 4);
assert(multisubarr3.length === 3);
assert(multisubarr3.byteOffset === 3);
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
* 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 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;
// var testindex: i32 = 2;
var result = array.reduceRight<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 reduceRightUint8ArrayTest(): void {
var array: Uint8Array = new Uint8Array(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: Uint8Array): u8 => {
// assert(testindex == index);
// assert(array == self);
// --testindex;
return acc + val;
}, 0);
assert(result == 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);
}
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();

File diff suppressed because it is too large Load Diff