Skip to content

Commit d411415

Browse files
jtennerdcodeIO
authored andcommitted
Implement TypedArray#reverse (AssemblyScript#532)
1 parent cdec865 commit d411415

File tree

6 files changed

+6923
-279
lines changed

6 files changed

+6923
-279
lines changed

std/assembly/index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1139,6 +1139,8 @@ declare abstract class TypedArray<T> implements ArrayBufferView<T> {
11391139
every(callbackfn: (value: T, index: i32, self: this) => bool): bool;
11401140
/** The forEach() method executes a provided function once per array element. This method has the same algorithm as Array.prototype.forEach().*/
11411141
forEach(callbackfn: (value: T, index: i32, self: this) => void): void;
1142+
/** 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(). */
1143+
reverse(): this;
11421144
}
11431145

11441146
/** An array of twos-complement 8-bit signed integers. */

std/assembly/internal/typedarray.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,3 +244,16 @@ export function FOREACH<TArray extends TypedArray<T>, T>(
244244
callbackfn(LOAD<T>(buffer, i, byteOffset), i, array);
245245
}
246246
}
247+
248+
@inline
249+
export function REVERSE<TArray extends TypedArray<T>, T>(array: TArray): TArray {
250+
var buffer = array.buffer;
251+
var byteOffset = array.byteOffset;
252+
253+
for (let front = 0, back = array.length - 1; front < back; ++front, --back) {
254+
let temp = LOAD<T>(buffer, front, byteOffset);
255+
STORE<T>(buffer, front, LOAD<T>(buffer, back, byteOffset), byteOffset);
256+
STORE<T>(buffer, back, temp, byteOffset);
257+
}
258+
return array;
259+
}

std/assembly/typedarray.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
SOME,
1111
EVERY,
1212
FOREACH,
13+
REVERSE,
1314
} from "./internal/typedarray";
1415

1516
import {
@@ -68,6 +69,10 @@ export class Int8Array extends TypedArray<i8> {
6869
forEach(callbackfn: (value: i8, index: i32, self: Int8Array) => void): void {
6970
FOREACH<Int8Array, i8>(this, callbackfn);
7071
}
72+
73+
reverse(): this {
74+
return REVERSE<this, i8>(this);
75+
}
7176
}
7277

7378
export class Uint8Array extends TypedArray<u8> {
@@ -118,6 +123,10 @@ export class Uint8Array extends TypedArray<u8> {
118123
forEach(callbackfn: (value: u8, index: i32, self: Uint8Array) => void): void {
119124
FOREACH<Uint8Array, u8>(this, callbackfn);
120125
}
126+
127+
reverse(): this {
128+
return REVERSE<this, u8>(this);
129+
}
121130
}
122131

123132
export class Uint8ClampedArray extends Uint8Array {
@@ -178,6 +187,10 @@ export class Uint8ClampedArray extends Uint8Array {
178187
forEach(callbackfn: (value: u8, index: i32, self: Uint8ClampedArray) => void): void {
179188
FOREACH<Uint8ClampedArray, u8>(this, callbackfn);
180189
}
190+
191+
reverse(): this {
192+
return REVERSE<this, u8>(this);
193+
}
181194
}
182195

183196
export class Int16Array extends TypedArray<i16> {
@@ -228,6 +241,10 @@ export class Int16Array extends TypedArray<i16> {
228241
forEach(callbackfn: (value: i16, index: i32, self: Int16Array) => void): void {
229242
FOREACH<Int16Array, i16>(this, callbackfn);
230243
}
244+
245+
reverse(): this {
246+
return REVERSE<this, i16>(this);
247+
}
231248
}
232249

233250
export class Uint16Array extends TypedArray<u16> {
@@ -278,6 +295,10 @@ export class Uint16Array extends TypedArray<u16> {
278295
forEach(callbackfn: (value: u16, index: i32, self: Uint16Array) => void): void {
279296
FOREACH<Uint16Array, u16>(this, callbackfn);
280297
}
298+
299+
reverse(): this {
300+
return REVERSE<this, u16>(this);
301+
}
281302
}
282303

283304
export class Int32Array extends TypedArray<i32> {
@@ -328,6 +349,10 @@ export class Int32Array extends TypedArray<i32> {
328349
forEach(callbackfn: (value: i32, index: i32, self: Int32Array) => void): void {
329350
FOREACH<Int32Array, i32>(this, callbackfn);
330351
}
352+
353+
reverse(): this {
354+
return REVERSE<this, i32>(this);
355+
}
331356
}
332357

333358
export class Uint32Array extends TypedArray<u32> {
@@ -378,6 +403,10 @@ export class Uint32Array extends TypedArray<u32> {
378403
forEach(callbackfn: (value: u32, index: i32, self: Uint32Array) => void): void {
379404
FOREACH<Uint32Array, u32>(this, callbackfn);
380405
}
406+
407+
reverse(): this {
408+
return REVERSE<this, u32>(this);
409+
}
381410
}
382411

383412
export class Int64Array extends TypedArray<i64> {
@@ -428,6 +457,10 @@ export class Int64Array extends TypedArray<i64> {
428457
forEach(callbackfn: (value: i64, index: i32, self: Int64Array) => void): void {
429458
FOREACH<Int64Array, i64>(this, callbackfn);
430459
}
460+
461+
reverse(): this {
462+
return REVERSE<this, i64>(this);
463+
}
431464
}
432465

433466
export class Uint64Array extends TypedArray<u64> {
@@ -478,6 +511,10 @@ export class Uint64Array extends TypedArray<u64> {
478511
forEach(callbackfn: (value: u64, index: i32, self: Uint64Array) => void): void {
479512
FOREACH<Uint64Array, u64>(this, callbackfn);
480513
}
514+
515+
reverse(): this {
516+
return REVERSE<this, u64>(this);
517+
}
481518
}
482519

483520
export class Float32Array extends TypedArray<f32> {
@@ -528,6 +565,10 @@ export class Float32Array extends TypedArray<f32> {
528565
forEach(callbackfn: (value: f32, index: i32, self: Float32Array) => void): void {
529566
FOREACH<Float32Array, f32>(this, callbackfn);
530567
}
568+
569+
reverse(): this {
570+
return REVERSE<this, f32>(this);
571+
}
531572
}
532573

533574
export class Float64Array extends TypedArray<f64> {
@@ -578,4 +619,8 @@ export class Float64Array extends TypedArray<f64> {
578619
forEach(callbackfn: (value: f64, index: i32, self: Float64Array) => void): void {
579620
FOREACH<Float64Array, f64>(this, callbackfn);
580621
}
622+
623+
reverse(): this {
624+
return REVERSE<this, f64>(this);
625+
}
581626
}

0 commit comments

Comments
 (0)