Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TypedArray#filter #818

Merged
merged 7 commits into from
Sep 6, 2019
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions std/assembly/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1149,6 +1149,8 @@ declare abstract class TypedArray<T> implements ArrayBufferView<T> {
some(callbackfn: (value: T, index: i32, self: this) => bool): bool;
/** The map() method creates a new typed array with the results of calling a provided function on every element in this typed array. This method has the same algorithm as Array.prototype.map().*/
map(callbackfn: (value: T, index: i32, self: this) => T): TypedArray<T>;
/** The filter() method creates a new typed array with all elements that pass the test implemented by the provided function. This method has the same algorithm as Array.prototype.filter(). */
filter(callbackfn: (value: T, index: i32, self: this) => bool): TypedArray<T>;
/** The sort() method sorts the elements of a typed array numerically in place and returns the typed array. This method has the same algorithm as Array.prototype.sort(), except that sorts the values numerically instead of as strings. TypedArray is one of the typed array types here. */
sort(callback?: (a: T, b: T) => i32): this;
/** The fill() method fills all the elements of a typed array from a start index to an end index with a static value. This method has the same algorithm as Array.prototype.fill(). */
Expand Down
74 changes: 74 additions & 0 deletions std/assembly/typedarray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ export class Int8Array extends ArrayBufferView {
return MAP<Int8Array, i8>(this, fn);
}

filter(fn: (value: i8, index: i32, self: Int8Array) => bool): Int8Array {
return FILTER<Int8Array, i8>(this, fn);
}

findIndex(fn: (value: i8, index: i32, self: Int8Array) => bool): i32 {
return FIND_INDEX<Int8Array, i8>(this, fn);
}
Expand Down Expand Up @@ -202,6 +206,10 @@ export class Uint8Array extends ArrayBufferView {
return MAP<Uint8Array, u8>(this, fn);
}

filter(fn: (value: u8, index: i32, self: Uint8Array) => bool): Uint8Array {
return FILTER<Uint8Array, u8>(this, fn);
}

findIndex(fn: (value: u8, index: i32, self: Uint8Array) => bool): i32 {
return FIND_INDEX<Uint8Array, u8>(this, fn);
}
Expand Down Expand Up @@ -314,6 +322,10 @@ export class Uint8ClampedArray extends ArrayBufferView {
return MAP<Uint8ClampedArray, u8>(this, fn);
}

filter(fn: (value: u8, index: i32, self: Uint8ClampedArray) => bool): Uint8ClampedArray {
return FILTER<Uint8ClampedArray, u8>(this, fn);
}

findIndex(fn: (value: u8, index: i32, self: Uint8ClampedArray) => bool): i32 {
return FIND_INDEX<Uint8ClampedArray, u8>(this, fn);
}
Expand Down Expand Up @@ -426,6 +438,10 @@ export class Int16Array extends ArrayBufferView {
return MAP<Int16Array, i16>(this, fn);
}

filter(fn: (value: i16, index: i32, self: Int16Array) => bool): Int16Array {
return FILTER<Int16Array, i16>(this, fn);
}

findIndex(fn: (value: i16, index: i32, self: Int16Array) => bool): i32 {
return FIND_INDEX<Int16Array, i16>(this, fn);
}
Expand Down Expand Up @@ -538,6 +554,10 @@ export class Uint16Array extends ArrayBufferView {
return MAP<Uint16Array, u16>(this, fn);
}

filter(fn: (value: u16, index: i32, self: Uint16Array) => bool): Uint16Array {
return FILTER<Uint16Array, u16>(this, fn);
}

findIndex(fn: (value: u16, index: i32, self: Uint16Array) => bool): i32 {
return FIND_INDEX<Uint16Array, u16>(this, fn);
}
Expand Down Expand Up @@ -650,6 +670,10 @@ export class Int32Array extends ArrayBufferView {
return MAP<Int32Array, i32>(this, fn);
}

filter(fn: (value: i32, index: i32, self: Int32Array) => bool): Int32Array {
return FILTER<Int32Array, i32>(this, fn);
}

findIndex(fn: (value: i32, index: i32, self: Int32Array) => bool): i32 {
return FIND_INDEX<Int32Array, i32>(this, fn);
}
Expand Down Expand Up @@ -762,6 +786,10 @@ export class Uint32Array extends ArrayBufferView {
return MAP<Uint32Array, u32>(this, fn);
}

filter(fn: (value: u32, index: i32, self: Uint32Array) => bool): Uint32Array {
return FILTER<Uint32Array, u32>(this, fn);
}

findIndex(fn: (value: u32, index: i32, self: Uint32Array) => bool): i32 {
return FIND_INDEX<Uint32Array, u32>(this, fn);
}
Expand Down Expand Up @@ -874,6 +902,10 @@ export class Int64Array extends ArrayBufferView {
return MAP<Int64Array, i64>(this, fn);
}

filter(fn: (value: i64, index: i32, self: Int64Array) => bool): Int64Array {
return FILTER<Int64Array, i64>(this, fn);
}

findIndex(fn: (value: i64, index: i32, self: Int64Array) => bool): i32 {
return FIND_INDEX<Int64Array, i64>(this, fn);
}
Expand Down Expand Up @@ -986,6 +1018,10 @@ export class Uint64Array extends ArrayBufferView {
return MAP<Uint64Array, u64>(this, fn);
}

filter(fn: (value: u64, index: i32, self: Uint64Array) => bool): Uint64Array {
return FILTER<Uint64Array, u64>(this, fn);
}

findIndex(fn: (value: u64, index: i32, self: Uint64Array) => bool): i32 {
return FIND_INDEX<Uint64Array, u64>(this, fn);
}
Expand Down Expand Up @@ -1098,6 +1134,10 @@ export class Float32Array extends ArrayBufferView {
return MAP<Float32Array, f32>(this, fn);
}

filter(fn: (value: f32, index: i32, self: Float32Array) => bool): Float32Array {
return FILTER<Float32Array, f32>(this, fn);
}

findIndex(fn: (value: f32, index: i32, self: Float32Array) => bool): i32 {
return FIND_INDEX<Float32Array, f32>(this, fn);
}
Expand Down Expand Up @@ -1210,6 +1250,10 @@ export class Float64Array extends ArrayBufferView {
return MAP<Float64Array, f64>(this, fn);
}

filter(fn: (value: f64, index: i32, self: Float64Array) => bool): Float64Array {
return FILTER<Float64Array, f64>(this, fn);
}

findIndex(fn: (value: f64, index: i32, self: Float64Array) => bool): i32 {
return FIND_INDEX<Float64Array, f64>(this, fn);
}
Expand Down Expand Up @@ -1364,6 +1408,36 @@ function MAP<TArray extends ArrayBufferView, T>(
return out;
}

// @ts-ignore: decorator
@inline
function FILTER<TArray extends ArrayBufferView, T>(
array: TArray,
fn: (value: T, index: i32, self: TArray) => bool,
): TArray {
var len = array.length;
var byteLength = len << alignof<T>();
MaxGraey marked this conversation as resolved.
Show resolved Hide resolved
var out = changetype<TArray>(__alloc(offsetof<TArray>(), idof<TArray>()));
var buffer = __alloc(byteLength, idof<ArrayBuffer>());
var dataStart = array.dataStart;
var j: usize = 0;
for (let i = 0; i < len; i++) {
let value = load<T>(dataStart + (<usize>i << alignof<T>()));
if (fn(value, i, array)) {
store<T>(
buffer + (j++ << alignof<T>()),
value
);
}
}
// shrink output buffer
var length = j << alignof<T>();
var data = __realloc(buffer, length);
out.data = changetype<ArrayBuffer>(data); // retain
out.dataStart = data;
out.dataLength = length;
return out;
}

// @ts-ignore: decorator
@inline
function FIND_INDEX<TArray extends ArrayBufferView, T>(
Expand Down
2 changes: 1 addition & 1 deletion tests/compiler/std/dataview.optimized.wat
Original file line number Diff line number Diff line change
Expand Up @@ -1634,7 +1634,7 @@
if
i32.const 280
i32.const 376
i32.const 150
i32.const 154
i32.const 44
call $~lib/builtins/abort
unreachable
Expand Down
2 changes: 1 addition & 1 deletion tests/compiler/std/dataview.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -3327,7 +3327,7 @@
if
i32.const 280
i32.const 376
i32.const 150
i32.const 154
i32.const 44
call $~lib/builtins/abort
unreachable
Expand Down
4,240 changes: 3,347 additions & 893 deletions tests/compiler/std/typedarray.optimized.wat

Large diffs are not rendered by default.

27 changes: 27 additions & 0 deletions tests/compiler/std/typedarray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,33 @@ testArrayMap<Uint64Array, u64>();
testArrayMap<Float32Array, f32>();
testArrayMap<Float64Array, f64>();

function testArrayFilter<ArrayType extends TypedArray<T>, T extends number>(): void {
var source: ArrayType = instantiate<ArrayType>(6);
source[0] = <T>1;
source[1] = <T>2;
source[2] = <T>3;
source[3] = <T>4;
source[5] = <T>5;
var result = source.filter((value: T): bool => value > 2);
assert(result.byteOffset == 0);
assert(result.length == 3);
assert(result[0] == <T>3);
assert(result[1] == <T>4);
assert(result[2] == <T>5);
}

testArrayFilter<Int8Array, i8>();
testArrayFilter<Uint8Array, u8>();
testArrayFilter<Uint8ClampedArray, u8>();
testArrayFilter<Int16Array, i16>();
testArrayFilter<Uint16Array, u16>();
testArrayFilter<Int32Array, i32>();
testArrayFilter<Uint32Array, u32>();
testArrayFilter<Int64Array, i64>();
testArrayFilter<Uint64Array, u64>();
testArrayFilter<Float32Array, f32>();
testArrayFilter<Float64Array, f64>();

function testArraySome<ArrayType extends TypedArray<T>, T extends number>(): void {
var source: ArrayType = instantiate<ArrayType>(3);
source[0] = <T>2;
Expand Down
5,134 changes: 4,007 additions & 1,127 deletions tests/compiler/std/typedarray.untouched.wat

Large diffs are not rendered by default.