Implement TypedArray#reverse (#532)

This commit is contained in:
jtenner 2019-03-08 12:48:06 -05:00 committed by Daniel Wirtz
parent cdec865bef
commit d411415060
6 changed files with 6862 additions and 218 deletions

View File

@ -1139,6 +1139,8 @@ declare abstract class TypedArray<T> implements ArrayBufferView<T> {
every(callbackfn: (value: T, index: i32, self: this) => bool): bool;
/** The forEach() method executes a provided function once per array element. This method has the same algorithm as Array.prototype.forEach().*/
forEach(callbackfn: (value: T, index: i32, self: this) => void): void;
/** The reverse() method reverses a typed array in place. The first typed array element becomes the last and the last becomes the first. This method has the same algorithm as Array.prototype.reverse(). */
reverse(): this;
}
/** An array of twos-complement 8-bit signed integers. */

View File

@ -244,3 +244,16 @@ export function FOREACH<TArray extends TypedArray<T>, T>(
callbackfn(LOAD<T>(buffer, i, byteOffset), i, array);
}
}
@inline
export function REVERSE<TArray extends TypedArray<T>, T>(array: TArray): TArray {
var buffer = array.buffer;
var byteOffset = array.byteOffset;
for (let front = 0, back = array.length - 1; front < back; ++front, --back) {
let temp = LOAD<T>(buffer, front, byteOffset);
STORE<T>(buffer, front, LOAD<T>(buffer, back, byteOffset), byteOffset);
STORE<T>(buffer, back, temp, byteOffset);
}
return array;
}

View File

@ -10,6 +10,7 @@ import {
SOME,
EVERY,
FOREACH,
REVERSE,
} from "./internal/typedarray";
import {
@ -68,6 +69,10 @@ export class Int8Array extends TypedArray<i8> {
forEach(callbackfn: (value: i8, index: i32, self: Int8Array) => void): void {
FOREACH<Int8Array, i8>(this, callbackfn);
}
reverse(): this {
return REVERSE<this, i8>(this);
}
}
export class Uint8Array extends TypedArray<u8> {
@ -118,6 +123,10 @@ export class Uint8Array extends TypedArray<u8> {
forEach(callbackfn: (value: u8, index: i32, self: Uint8Array) => void): void {
FOREACH<Uint8Array, u8>(this, callbackfn);
}
reverse(): this {
return REVERSE<this, u8>(this);
}
}
export class Uint8ClampedArray extends Uint8Array {
@ -178,6 +187,10 @@ export class Uint8ClampedArray extends Uint8Array {
forEach(callbackfn: (value: u8, index: i32, self: Uint8ClampedArray) => void): void {
FOREACH<Uint8ClampedArray, u8>(this, callbackfn);
}
reverse(): this {
return REVERSE<this, u8>(this);
}
}
export class Int16Array extends TypedArray<i16> {
@ -228,6 +241,10 @@ export class Int16Array extends TypedArray<i16> {
forEach(callbackfn: (value: i16, index: i32, self: Int16Array) => void): void {
FOREACH<Int16Array, i16>(this, callbackfn);
}
reverse(): this {
return REVERSE<this, i16>(this);
}
}
export class Uint16Array extends TypedArray<u16> {
@ -278,6 +295,10 @@ export class Uint16Array extends TypedArray<u16> {
forEach(callbackfn: (value: u16, index: i32, self: Uint16Array) => void): void {
FOREACH<Uint16Array, u16>(this, callbackfn);
}
reverse(): this {
return REVERSE<this, u16>(this);
}
}
export class Int32Array extends TypedArray<i32> {
@ -328,6 +349,10 @@ export class Int32Array extends TypedArray<i32> {
forEach(callbackfn: (value: i32, index: i32, self: Int32Array) => void): void {
FOREACH<Int32Array, i32>(this, callbackfn);
}
reverse(): this {
return REVERSE<this, i32>(this);
}
}
export class Uint32Array extends TypedArray<u32> {
@ -378,6 +403,10 @@ export class Uint32Array extends TypedArray<u32> {
forEach(callbackfn: (value: u32, index: i32, self: Uint32Array) => void): void {
FOREACH<Uint32Array, u32>(this, callbackfn);
}
reverse(): this {
return REVERSE<this, u32>(this);
}
}
export class Int64Array extends TypedArray<i64> {
@ -428,6 +457,10 @@ export class Int64Array extends TypedArray<i64> {
forEach(callbackfn: (value: i64, index: i32, self: Int64Array) => void): void {
FOREACH<Int64Array, i64>(this, callbackfn);
}
reverse(): this {
return REVERSE<this, i64>(this);
}
}
export class Uint64Array extends TypedArray<u64> {
@ -478,6 +511,10 @@ export class Uint64Array extends TypedArray<u64> {
forEach(callbackfn: (value: u64, index: i32, self: Uint64Array) => void): void {
FOREACH<Uint64Array, u64>(this, callbackfn);
}
reverse(): this {
return REVERSE<this, u64>(this);
}
}
export class Float32Array extends TypedArray<f32> {
@ -528,6 +565,10 @@ export class Float32Array extends TypedArray<f32> {
forEach(callbackfn: (value: f32, index: i32, self: Float32Array) => void): void {
FOREACH<Float32Array, f32>(this, callbackfn);
}
reverse(): this {
return REVERSE<this, f32>(this);
}
}
export class Float64Array extends TypedArray<f64> {
@ -578,4 +619,8 @@ export class Float64Array extends TypedArray<f64> {
forEach(callbackfn: (value: f64, index: i32, self: Float64Array) => void): void {
FOREACH<Float64Array, f64>(this, callbackfn);
}
reverse(): this {
return REVERSE<this, f64>(this);
}
}

File diff suppressed because one or more lines are too long

View File

@ -362,10 +362,10 @@ function testArrayFindIndex<ArrayType extends TypedArray<T>, T extends number>()
// testIndex++;
return value == <T>2;
});
assert(result == 1);
assert(result == 1, "result mismatch");
var failResult = source.findIndex((value: T, index: i32, self: ArrayType): bool => value == <T>4);
assert(failResult == -1);
assert(failResult == -1, "fail result mismatch");
}
testArrayFindIndex<Int8Array, i8>();
@ -441,3 +441,42 @@ testArrayForEach<Int64Array, i64>();
testArrayForEach<Uint64Array, u64>();
testArrayForEach<Float32Array, f32>();
testArrayForEach<Float64Array, f64>();
var testArrayReverseValues: i32[] = [1, 2, 3, 4, 5, 6, 7, 8, 9];
function testArrayReverse<TArray extends TypedArray<T>, T extends number>(): void {
var values = testArrayReverseValues;
var array = instantiate<TArray>(9);
var arrayWithOffset = instantiate<TArray>(9);
var i: i32 = 0;
for (i = 0; i < 9; i++) {
array[i] = <T>values[i];
arrayWithOffset[i] = <T>values[i];
}
array.reverse();
for (i = 0; i < 9; i++) {
assert(array[i] == <T>values[8 - i], "TypedArray reverse value mismatch");
}
var reversedSlice = arrayWithOffset.subarray(4, 8).reverse();
assert(reversedSlice[0] == <T>8, "TypedArray reverse with byteOffset mismatch");
assert(reversedSlice[1] == <T>7, "TypedArray reverse with byteOffset mismatch");
assert(reversedSlice[2] == <T>6, "TypedArray reverse with byteOffset mismatch");
assert(reversedSlice[3] == <T>5, "TypedArray reverse with byteOffset mismatch");
}
testArrayReverse<Int8Array, i8>();
testArrayReverse<Uint8Array, u8>();
testArrayReverse<Uint8ClampedArray, u8>();
testArrayReverse<Int16Array, i16>();
testArrayReverse<Uint16Array, u16>();
testArrayReverse<Int32Array, i32>();
testArrayReverse<Uint32Array, u32>();
testArrayReverse<Int64Array, i64>();
testArrayReverse<Uint64Array, u64>();
testArrayReverse<Float32Array, f32>();
testArrayReverse<Float64Array, f64>();

File diff suppressed because one or more lines are too long