mirror of
https://github.com/fluencelabs/assemblyscript
synced 2025-06-16 00:11:28 +00:00
Implement TypedArray#reverse (#532)
This commit is contained in:
2
std/assembly/index.d.ts
vendored
2
std/assembly/index.d.ts
vendored
@ -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. */
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user