mirror of
https://github.com/fluencelabs/assemblyscript
synced 2025-04-25 07:02:13 +00:00
Implement TypedArray#forEach (#530)
This commit is contained in:
parent
208dc2f1de
commit
783dd32c2e
4
std/assembly/index.d.ts
vendored
4
std/assembly/index.d.ts
vendored
@ -1136,7 +1136,9 @@ declare abstract class TypedArray<T> implements ArrayBufferView<T> {
|
|||||||
/** The findIndex() method returns an index in the typed array, if an element in the typed array satisfies the provided testing function. Otherwise -1 is returned. See also the find() [not implemented] method, which returns the value of a found element in the typed array instead of its index. */
|
/** The findIndex() method returns an index in the typed array, if an element in the typed array satisfies the provided testing function. Otherwise -1 is returned. See also the find() [not implemented] method, which returns the value of a found element in the typed array instead of its index. */
|
||||||
findIndex(callbackfn: (value: T, index: i32, self: this) => bool): i32;
|
findIndex(callbackfn: (value: T, index: i32, self: this) => bool): i32;
|
||||||
/** The every() method tests whether all elements in the typed array pass the test implemented by the provided function. This method has the same algorithm as Array.prototype.every(). */
|
/** The every() method tests whether all elements in the typed array pass the test implemented by the provided function. This method has the same algorithm as Array.prototype.every(). */
|
||||||
every(callbackfn: (value: T, index: i32, self: this) => bool): i32;
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** An array of twos-complement 8-bit signed integers. */
|
/** An array of twos-complement 8-bit signed integers. */
|
||||||
|
@ -231,3 +231,16 @@ export function EVERY<TArray extends TypedArray<T>, T>(
|
|||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@inline
|
||||||
|
export function FOREACH<TArray extends TypedArray<T>, T>(
|
||||||
|
array: TArray,
|
||||||
|
callbackfn: (value: T, index: i32, array: TArray) => void,
|
||||||
|
): void {
|
||||||
|
var length = array.length;
|
||||||
|
var buffer = array.buffer;
|
||||||
|
var byteOffset = array.byteOffset;
|
||||||
|
for (let i = 0; i < length; i++) {
|
||||||
|
callbackfn(LOAD<T>(buffer, i, byteOffset), i, array);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -9,6 +9,7 @@ import {
|
|||||||
FIND_INDEX,
|
FIND_INDEX,
|
||||||
SOME,
|
SOME,
|
||||||
EVERY,
|
EVERY,
|
||||||
|
FOREACH,
|
||||||
} from "./internal/typedarray";
|
} from "./internal/typedarray";
|
||||||
|
|
||||||
import {
|
import {
|
||||||
@ -63,6 +64,10 @@ export class Int8Array extends TypedArray<i8> {
|
|||||||
every(callbackfn: (value: i8, index: i32, self: Int8Array) => bool): bool {
|
every(callbackfn: (value: i8, index: i32, self: Int8Array) => bool): bool {
|
||||||
return EVERY<Int8Array, i8>(this, callbackfn);
|
return EVERY<Int8Array, i8>(this, callbackfn);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
forEach(callbackfn: (value: i8, index: i32, self: Int8Array) => void): void {
|
||||||
|
FOREACH<Int8Array, i8>(this, callbackfn);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Uint8Array extends TypedArray<u8> {
|
export class Uint8Array extends TypedArray<u8> {
|
||||||
@ -109,6 +114,10 @@ export class Uint8Array extends TypedArray<u8> {
|
|||||||
every(callbackfn: (value: u8, index: i32, self: Uint8Array) => bool): bool {
|
every(callbackfn: (value: u8, index: i32, self: Uint8Array) => bool): bool {
|
||||||
return EVERY<Uint8Array, u8>(this, callbackfn);
|
return EVERY<Uint8Array, u8>(this, callbackfn);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
forEach(callbackfn: (value: u8, index: i32, self: Uint8Array) => void): void {
|
||||||
|
FOREACH<Uint8Array, u8>(this, callbackfn);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Uint8ClampedArray extends Uint8Array {
|
export class Uint8ClampedArray extends Uint8Array {
|
||||||
@ -165,6 +174,10 @@ export class Uint8ClampedArray extends Uint8Array {
|
|||||||
every(callbackfn: (value: u8, index: i32, self: Uint8ClampedArray) => bool): bool {
|
every(callbackfn: (value: u8, index: i32, self: Uint8ClampedArray) => bool): bool {
|
||||||
return EVERY<Uint8ClampedArray, u8>(this, callbackfn);
|
return EVERY<Uint8ClampedArray, u8>(this, callbackfn);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
forEach(callbackfn: (value: u8, index: i32, self: Uint8ClampedArray) => void): void {
|
||||||
|
FOREACH<Uint8ClampedArray, u8>(this, callbackfn);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Int16Array extends TypedArray<i16> {
|
export class Int16Array extends TypedArray<i16> {
|
||||||
@ -211,6 +224,10 @@ export class Int16Array extends TypedArray<i16> {
|
|||||||
every(callbackfn: (value: i16, index: i32, self: Int16Array) => bool): bool {
|
every(callbackfn: (value: i16, index: i32, self: Int16Array) => bool): bool {
|
||||||
return EVERY<Int16Array, i16>(this, callbackfn);
|
return EVERY<Int16Array, i16>(this, callbackfn);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
forEach(callbackfn: (value: i16, index: i32, self: Int16Array) => void): void {
|
||||||
|
FOREACH<Int16Array, i16>(this, callbackfn);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Uint16Array extends TypedArray<u16> {
|
export class Uint16Array extends TypedArray<u16> {
|
||||||
@ -257,6 +274,10 @@ export class Uint16Array extends TypedArray<u16> {
|
|||||||
every(callbackfn: (value: u16, index: i32, self: Uint16Array) => bool): bool {
|
every(callbackfn: (value: u16, index: i32, self: Uint16Array) => bool): bool {
|
||||||
return EVERY<Uint16Array, u16>(this, callbackfn);
|
return EVERY<Uint16Array, u16>(this, callbackfn);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
forEach(callbackfn: (value: u16, index: i32, self: Uint16Array) => void): void {
|
||||||
|
FOREACH<Uint16Array, u16>(this, callbackfn);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Int32Array extends TypedArray<i32> {
|
export class Int32Array extends TypedArray<i32> {
|
||||||
@ -303,6 +324,10 @@ export class Int32Array extends TypedArray<i32> {
|
|||||||
every(callbackfn: (value: i32, index: i32, self: Int32Array) => bool): bool {
|
every(callbackfn: (value: i32, index: i32, self: Int32Array) => bool): bool {
|
||||||
return EVERY<Int32Array, i32>(this, callbackfn);
|
return EVERY<Int32Array, i32>(this, callbackfn);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
forEach(callbackfn: (value: i32, index: i32, self: Int32Array) => void): void {
|
||||||
|
FOREACH<Int32Array, i32>(this, callbackfn);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Uint32Array extends TypedArray<u32> {
|
export class Uint32Array extends TypedArray<u32> {
|
||||||
@ -349,6 +374,10 @@ export class Uint32Array extends TypedArray<u32> {
|
|||||||
every(callbackfn: (value: u32, index: i32, self: Uint32Array) => bool): bool {
|
every(callbackfn: (value: u32, index: i32, self: Uint32Array) => bool): bool {
|
||||||
return EVERY<Uint32Array, u32>(this, callbackfn);
|
return EVERY<Uint32Array, u32>(this, callbackfn);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
forEach(callbackfn: (value: u32, index: i32, self: Uint32Array) => void): void {
|
||||||
|
FOREACH<Uint32Array, u32>(this, callbackfn);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Int64Array extends TypedArray<i64> {
|
export class Int64Array extends TypedArray<i64> {
|
||||||
@ -395,6 +424,10 @@ export class Int64Array extends TypedArray<i64> {
|
|||||||
every(callbackfn: (value: i64, index: i32, self: Int64Array) => bool): bool {
|
every(callbackfn: (value: i64, index: i32, self: Int64Array) => bool): bool {
|
||||||
return EVERY<Int64Array, i64>(this, callbackfn);
|
return EVERY<Int64Array, i64>(this, callbackfn);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
forEach(callbackfn: (value: i64, index: i32, self: Int64Array) => void): void {
|
||||||
|
FOREACH<Int64Array, i64>(this, callbackfn);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Uint64Array extends TypedArray<u64> {
|
export class Uint64Array extends TypedArray<u64> {
|
||||||
@ -441,6 +474,10 @@ export class Uint64Array extends TypedArray<u64> {
|
|||||||
every(callbackfn: (value: u64, index: i32, self: Uint64Array) => bool): bool {
|
every(callbackfn: (value: u64, index: i32, self: Uint64Array) => bool): bool {
|
||||||
return EVERY<Uint64Array, u64>(this, callbackfn);
|
return EVERY<Uint64Array, u64>(this, callbackfn);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
forEach(callbackfn: (value: u64, index: i32, self: Uint64Array) => void): void {
|
||||||
|
FOREACH<Uint64Array, u64>(this, callbackfn);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Float32Array extends TypedArray<f32> {
|
export class Float32Array extends TypedArray<f32> {
|
||||||
@ -487,6 +524,10 @@ export class Float32Array extends TypedArray<f32> {
|
|||||||
every(callbackfn: (value: f32, index: i32, self: Float32Array) => bool): bool {
|
every(callbackfn: (value: f32, index: i32, self: Float32Array) => bool): bool {
|
||||||
return EVERY<Float32Array, f32>(this, callbackfn);
|
return EVERY<Float32Array, f32>(this, callbackfn);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
forEach(callbackfn: (value: f32, index: i32, self: Float32Array) => void): void {
|
||||||
|
FOREACH<Float32Array, f32>(this, callbackfn);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Float64Array extends TypedArray<f64> {
|
export class Float64Array extends TypedArray<f64> {
|
||||||
@ -533,4 +574,8 @@ export class Float64Array extends TypedArray<f64> {
|
|||||||
every(callbackfn: (value: f64, index: i32, self: Float64Array) => bool): bool {
|
every(callbackfn: (value: f64, index: i32, self: Float64Array) => bool): bool {
|
||||||
return EVERY<Float64Array, f64>(this, callbackfn);
|
return EVERY<Float64Array, f64>(this, callbackfn);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
forEach(callbackfn: (value: f64, index: i32, self: Float64Array) => void): void {
|
||||||
|
FOREACH<Float64Array, f64>(this, callbackfn);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
File diff suppressed because one or more lines are too long
@ -409,3 +409,35 @@ testArrayEvery<Int64Array, i64>();
|
|||||||
testArrayEvery<Uint64Array, u64>();
|
testArrayEvery<Uint64Array, u64>();
|
||||||
testArrayEvery<Float32Array, f32>();
|
testArrayEvery<Float32Array, f32>();
|
||||||
testArrayEvery<Float64Array, f64>();
|
testArrayEvery<Float64Array, f64>();
|
||||||
|
|
||||||
|
var forEachCallCount: i32 = 0;
|
||||||
|
var forEachSelf: usize;
|
||||||
|
var forEachValues: i32[] = [10, 12, 14];
|
||||||
|
function testArrayForEach<TArray extends TypedArray<T>, T extends number>(): void {
|
||||||
|
forEachCallCount = 0;
|
||||||
|
var array = instantiate<TArray>(3);
|
||||||
|
forEachSelf = changetype<usize>(array);
|
||||||
|
array[0] = <T>forEachValues[0];
|
||||||
|
array[1] = <T>forEachValues[1];
|
||||||
|
array[2] = <T>forEachValues[2];
|
||||||
|
array.forEach((value: T, index: i32, self: TArray): void => {
|
||||||
|
var matchedValue = forEachValues[index];
|
||||||
|
assert(value == <T>matchedValue, "forEach value mismatch");
|
||||||
|
assert(index == forEachCallCount, "forEach index mismatch");
|
||||||
|
assert(forEachSelf == changetype<usize>(self), "forEach self parameter mismatch");
|
||||||
|
forEachCallCount++;
|
||||||
|
});
|
||||||
|
assert(forEachCallCount == 3, "forEach call count mismatch");
|
||||||
|
}
|
||||||
|
|
||||||
|
testArrayForEach<Int8Array, i8>();
|
||||||
|
testArrayForEach<Uint8Array, u8>();
|
||||||
|
testArrayForEach<Uint8ClampedArray, u8>();
|
||||||
|
testArrayForEach<Int16Array, i16>();
|
||||||
|
testArrayForEach<Uint16Array, u16>();
|
||||||
|
testArrayForEach<Int32Array, i32>();
|
||||||
|
testArrayForEach<Uint32Array, u32>();
|
||||||
|
testArrayForEach<Int64Array, i64>();
|
||||||
|
testArrayForEach<Uint64Array, u64>();
|
||||||
|
testArrayForEach<Float32Array, f32>();
|
||||||
|
testArrayForEach<Float64Array, f64>();
|
||||||
|
File diff suppressed because one or more lines are too long
Loading…
x
Reference in New Issue
Block a user