Skip to content

Commit d7f4874

Browse files
jtennerdcodeIO
authored andcommitted
Implement TypedArray#reduce/reduceRight (AssemblyScript#352)
1 parent ced0121 commit d7f4874

File tree

7 files changed

+5079
-8
lines changed

7 files changed

+5079
-8
lines changed

NOTICE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ under the licensing terms detailed in LICENSE:
1010
* Alan Pierce <alangpierce@gmail.com>
1111
* Palmer <pengliao@live.cn>
1212
* Linus Unnebäck <linus@folkdatorn.se>
13+
* Joshua Tenner <tenner.joshua@gmail.com>
1314

1415
Portions of this software are derived from third-party works licensed under
1516
the following terms:

std/assembly/index.d.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,16 @@ declare abstract class TypedArray<T> implements ArrayBufferView<T> {
520520
readonly length: i32;
521521
/** Returns a new TypedArray of this type on the same ArrayBuffer from begin inclusive to end exclusive. */
522522
subarray(begin?: i32, end?: i32): this;
523+
/** The reduce() method applies a function against an accumulator and each value of the typed array (from left-to-right) has to reduce it to a single value. This method has the same algorithm as Array.prototype.reduce(). */
524+
reduce<W>(
525+
callbackfn: (accumulator: W, value: T, index: i32, self: this) => W,
526+
initialValue: W,
527+
): W;
528+
/** The reduceRight() method applies a function against an accumulator and each value of the typed array (from left-to-right) has to reduce it to a single value, starting from the end of the array. This method has the same algorithm as Array.prototype.reduceRight(). */
529+
reduceRight<W>(
530+
callbackfn: (accumulator: W, value: T, index: i32, self: this) => W,
531+
initialValue: W,
532+
): W;
523533
}
524534

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

std/assembly/internal/typedarray.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,31 @@ export abstract class TypedArray<T,TNative> {
126126
return this;
127127
}
128128
}
129+
130+
/**
131+
* TypedArray reduce implementation. This is a method that will be called from the parent,
132+
* passing types down from the child class using the typed parameters TypedArrayType and
133+
* ReturnType respectively. This implementation requires an initial value, and the direction.
134+
* When direction is true, reduce will reduce from the right side.
135+
*/
136+
@inline
137+
protected reduce_internal<TypedArrayType, ReturnType>(
138+
callbackfn: (accumulator: ReturnType, value: T, index: i32, array: TypedArrayType) => ReturnType,
139+
array: TypedArrayType,
140+
initialValue: ReturnType,
141+
direction: bool = false,
142+
): ReturnType {
143+
var index: i32 = direction ? this.length - 1 : 0;
144+
var length: i32 = direction ? -1 : this.length;
145+
while (index != length) {
146+
initialValue = callbackfn(
147+
initialValue,
148+
this.__unchecked_get(index),
149+
index,
150+
array,
151+
);
152+
index = direction ? index - 1 : index + 1;
153+
}
154+
return initialValue;
155+
}
129156
}

std/assembly/typedarray.ts

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,20 @@ export class Int8Array extends TypedArray<i8,i32> {
88
subarray(begin: i32 = 0, end: i32 = 0x7fffffff): Int8Array {
99
return changetype<Int8Array>(super.subarray(begin, end));
1010
}
11+
12+
reduce<ReturnType>(
13+
callbackfn: (accumulator: ReturnType, value: i8, index: i32, array: Int8Array) => ReturnType,
14+
initialValue: ReturnType,
15+
): ReturnType {
16+
return super.reduce_internal<Int8Array, ReturnType>(callbackfn, this, initialValue);
17+
}
18+
19+
reduceRight<ReturnType>(
20+
callbackfn: (accumulator: ReturnType, value: i8, index: i32, array: Int8Array) => ReturnType,
21+
initialValue: ReturnType,
22+
): ReturnType {
23+
return super.reduce_internal<Int8Array, ReturnType>(callbackfn, this, initialValue, true);
24+
}
1125
}
1226

1327
export class Uint8Array extends TypedArray<u8,u32> {
@@ -16,6 +30,20 @@ export class Uint8Array extends TypedArray<u8,u32> {
1630
subarray(begin: i32 = 0, end: i32 = 0x7fffffff): Uint8Array {
1731
return changetype<Uint8Array>(super.subarray(begin, end));
1832
}
33+
34+
reduce<ReturnType>(
35+
callbackfn: (accumulator: ReturnType, value: u8, index: i32, array: Uint8Array) => ReturnType,
36+
initialValue: ReturnType,
37+
): ReturnType {
38+
return super.reduce_internal<Uint8Array, ReturnType>(callbackfn, this, initialValue);
39+
}
40+
41+
reduceRight<ReturnType>(
42+
callbackfn: (accumulator: ReturnType, value: u8, index: i32, array: Uint8Array) => ReturnType,
43+
initialValue: ReturnType,
44+
): ReturnType {
45+
return super.reduce_internal<Uint8Array, ReturnType>(callbackfn, this, initialValue, true);
46+
}
1947
}
2048

2149
export class Uint8ClampedArray extends TypedArray<u8,u32> {
@@ -34,6 +62,20 @@ export class Uint8ClampedArray extends TypedArray<u8,u32> {
3462
subarray(begin: i32 = 0, end: i32 = 0x7fffffff): Uint8ClampedArray {
3563
return changetype<Uint8ClampedArray>(super.subarray(begin, end));
3664
}
65+
66+
reduce<ReturnType>(
67+
callbackfn: (accumulator: ReturnType, value: u8, index: i32, array: Uint8ClampedArray) => ReturnType,
68+
initialValue: ReturnType,
69+
): ReturnType {
70+
return super.reduce_internal<Uint8ClampedArray, ReturnType>(callbackfn, this, initialValue);
71+
}
72+
73+
reduceRight<ReturnType>(
74+
callbackfn: (accumulator: ReturnType, value: u8, index: i32, array: Uint8ClampedArray) => ReturnType,
75+
initialValue: ReturnType,
76+
): ReturnType {
77+
return super.reduce_internal<Uint8ClampedArray, ReturnType>(callbackfn, this, initialValue, true);
78+
}
3779
}
3880

3981
export class Int16Array extends TypedArray<i16,i32> {
@@ -42,6 +84,20 @@ export class Int16Array extends TypedArray<i16,i32> {
4284
subarray(begin: i32 = 0, end: i32 = 0x7fffffff): Int16Array {
4385
return changetype<Int16Array>(super.subarray(begin, end));
4486
}
87+
88+
reduce<ReturnType>(
89+
callbackfn: (accumulator: ReturnType, value: i16, index: i32, array: Int16Array) => ReturnType,
90+
initialValue: ReturnType,
91+
): ReturnType {
92+
return super.reduce_internal<Int16Array, ReturnType>(callbackfn, this, initialValue);
93+
}
94+
95+
reduceRight<ReturnType>(
96+
callbackfn: (accumulator: ReturnType, value: i16, index: i32, array: Int16Array) => ReturnType,
97+
initialValue: ReturnType,
98+
): ReturnType {
99+
return super.reduce_internal<Int16Array, ReturnType>(callbackfn, this, initialValue, true);
100+
}
45101
}
46102

47103
export class Uint16Array extends TypedArray<u16,u32> {
@@ -50,6 +106,20 @@ export class Uint16Array extends TypedArray<u16,u32> {
50106
subarray(begin: i32 = 0, end: i32 = 0x7fffffff): Uint16Array {
51107
return changetype<Uint16Array>(super.subarray(begin, end));
52108
}
109+
110+
reduce<ReturnType>(
111+
callbackfn: (accumulator: ReturnType, value: u16, index: i32, array: Uint16Array) => ReturnType,
112+
initialValue: ReturnType,
113+
): ReturnType {
114+
return super.reduce_internal<Uint16Array, ReturnType>(callbackfn, this, initialValue);
115+
}
116+
117+
reduceRight<ReturnType>(
118+
callbackfn: (accumulator: ReturnType, value: u16, index: i32, array: Uint16Array) => ReturnType,
119+
initialValue: ReturnType,
120+
): ReturnType {
121+
return super.reduce_internal<Uint16Array, ReturnType>(callbackfn, this, initialValue, true);
122+
}
53123
}
54124

55125
export class Int32Array extends TypedArray<i32,i32> {
@@ -58,6 +128,24 @@ export class Int32Array extends TypedArray<i32,i32> {
58128
subarray(begin: i32 = 0, end: i32 = 0x7fffffff): Int32Array {
59129
return changetype<Int32Array>(super.subarray(begin, end));
60130
}
131+
132+
/**
133+
* @param callbackfn {function} - a function that reduces each value to a ReturnType
134+
* @param initialValue {ReturnType} - the initial ReturnType value to be passed to the callbackfn
135+
*/
136+
reduce<ReturnType>(
137+
callbackfn: (accumulator: ReturnType, value: i32, index: i32, array: Int32Array) => ReturnType,
138+
initialValue: ReturnType,
139+
): ReturnType {
140+
return super.reduce_internal<Int32Array, ReturnType>(callbackfn, this, initialValue);
141+
}
142+
143+
reduceRight<ReturnType>(
144+
callbackfn: (accumulator: ReturnType, value: i32, index: i32, array: Int32Array) => ReturnType,
145+
initialValue: ReturnType,
146+
): ReturnType {
147+
return super.reduce_internal<Int32Array, ReturnType>(callbackfn, this, initialValue, true);
148+
}
61149
}
62150

63151
export class Uint32Array extends TypedArray<u32,u32> {
@@ -66,6 +154,20 @@ export class Uint32Array extends TypedArray<u32,u32> {
66154
subarray(begin: i32 = 0, end: i32 = 0x7fffffff): Uint32Array {
67155
return changetype<Uint32Array>(super.subarray(begin, end));
68156
}
157+
158+
reduce<ReturnType>(
159+
callbackfn: (accumulator: ReturnType, value: u32, index: i32, array: Uint32Array) => ReturnType,
160+
initialValue: ReturnType,
161+
): ReturnType {
162+
return super.reduce_internal<Uint32Array, ReturnType>(callbackfn, this, initialValue);
163+
}
164+
165+
reduceRight<ReturnType>(
166+
callbackfn: (accumulator: ReturnType, value: u32, index: i32, array: Uint32Array) => ReturnType,
167+
initialValue: ReturnType,
168+
): ReturnType {
169+
return super.reduce_internal<Uint32Array, ReturnType>(callbackfn, this, initialValue, true);
170+
}
69171
}
70172

71173
export class Int64Array extends TypedArray<i64,i64> {
@@ -74,6 +176,20 @@ export class Int64Array extends TypedArray<i64,i64> {
74176
subarray(begin: i32 = 0, end: i32 = 0x7fffffff): Int64Array {
75177
return changetype<Int64Array>(super.subarray(begin, end));
76178
}
179+
180+
reduce<ReturnType>(
181+
callbackfn: (accumulator: ReturnType, value: i64, index: i32, array: Int64Array) => ReturnType,
182+
initialValue: ReturnType,
183+
): ReturnType {
184+
return super.reduce_internal<Int64Array, ReturnType>(callbackfn, this, initialValue);
185+
}
186+
187+
reduceRight<ReturnType>(
188+
callbackfn: (accumulator: ReturnType, value: i64, index: i32, array: Int64Array) => ReturnType,
189+
initialValue: ReturnType,
190+
): ReturnType {
191+
return super.reduce_internal<Int64Array, ReturnType>(callbackfn, this, initialValue, true);
192+
}
77193
}
78194

79195
export class Uint64Array extends TypedArray<u64,u64> {
@@ -82,6 +198,20 @@ export class Uint64Array extends TypedArray<u64,u64> {
82198
subarray(begin: i32 = 0, end: i32 = 0x7fffffff): Uint64Array {
83199
return changetype<Uint64Array>(super.subarray(begin, end));
84200
}
201+
202+
reduce<ReturnType>(
203+
callbackfn: (accumulator: ReturnType, value: u64, index: i32, array: Uint64Array) => ReturnType,
204+
initialValue: ReturnType,
205+
): ReturnType {
206+
return super.reduce_internal<Uint64Array, ReturnType>(callbackfn, this, initialValue);
207+
}
208+
209+
reduceRight<ReturnType>(
210+
callbackfn: (accumulator: ReturnType, value: u64, index: i32, array: Uint64Array) => ReturnType,
211+
initialValue: ReturnType,
212+
): ReturnType {
213+
return super.reduce_internal<Uint64Array, ReturnType>(callbackfn, this, initialValue, true);
214+
}
85215
}
86216

87217
export class Float32Array extends TypedArray<f32,f32> {
@@ -90,6 +220,20 @@ export class Float32Array extends TypedArray<f32,f32> {
90220
subarray(begin: i32 = 0, end: i32 = 0x7fffffff): Float32Array {
91221
return changetype<Float32Array>(super.subarray(begin, end));
92222
}
223+
224+
reduce<ReturnType>(
225+
callbackfn: (accumulator: ReturnType, value: f32, index: i32, array: Float32Array) => ReturnType,
226+
initialValue: ReturnType,
227+
): ReturnType {
228+
return super.reduce_internal<Float32Array, ReturnType>(callbackfn, this, initialValue);
229+
}
230+
231+
reduceRight<ReturnType>(
232+
callbackfn: (accumulator: ReturnType, value: f32, index: i32, array: Float32Array) => ReturnType,
233+
initialValue: ReturnType,
234+
): ReturnType {
235+
return super.reduce_internal<Float32Array, ReturnType>(callbackfn, this, initialValue, true);
236+
}
93237
}
94238

95239
export class Float64Array extends TypedArray<f64,f64> {
@@ -98,4 +242,18 @@ export class Float64Array extends TypedArray<f64,f64> {
98242
subarray(begin: i32 = 0, end: i32 = 0x7fffffff): Float64Array {
99243
return changetype<Float64Array>(super.subarray(begin, end));
100244
}
245+
246+
reduce<ReturnType>(
247+
callbackfn: (accumulator: ReturnType, value: f64, index: i32, array: Float64Array) => ReturnType,
248+
initialValue: ReturnType,
249+
): ReturnType {
250+
return super.reduce_internal<Float64Array, ReturnType>(callbackfn, this, initialValue);
251+
}
252+
253+
reduceRight<ReturnType>(
254+
callbackfn: (accumulator: ReturnType, value: f64, index: i32, array: Float64Array) => ReturnType,
255+
initialValue: ReturnType,
256+
): ReturnType {
257+
return super.reduce_internal<Float64Array, ReturnType>(callbackfn, this, initialValue, true);
258+
}
101259
}

0 commit comments

Comments
 (0)