forked from AssemblyScript/assemblyscript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypedarray.ts
More file actions
179 lines (164 loc) Β· 4.8 KB
/
typedarray.ts
File metadata and controls
179 lines (164 loc) Β· 4.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import {
HEADER_SIZE as AB_HEADER_SIZE,
MAX_BLENGTH as AB_MAX_BLENGTH,
allocateUnsafe,
LOAD,
STORE
} from "./arraybuffer";
import {
insertionSort,
weakHeapSort
} from "./array";
/** Typed array base class. Not a global object. */
export abstract class TypedArray<T> {
[key: number]: T; // compatibility only
readonly buffer: ArrayBuffer;
readonly byteOffset: i32;
readonly byteLength: i32;
constructor(length: i32) {
const MAX_LENGTH = <u32>AB_MAX_BLENGTH / sizeof<T>();
if (<u32>length > MAX_LENGTH) throw new RangeError("Invalid typed array length");
var byteLength = length << alignof<T>();
var buffer = allocateUnsafe(byteLength);
memory.fill(changetype<usize>(buffer) + AB_HEADER_SIZE, 0, <usize>byteLength);
this.buffer = buffer;
this.byteOffset = 0;
this.byteLength = byteLength;
}
@inline
get length(): i32 {
return this.byteLength >>> alignof<T>();
}
@operator("[]")
protected __get(index: i32): T {
if (<u32>index >= <u32>(this.byteLength >>> alignof<T>())) throw new Error("Index out of bounds");
return LOAD<T>(this.buffer, index, this.byteOffset);
}
@inline @operator("{}")
protected __unchecked_get(index: i32): T {
return LOAD<T>(this.buffer, index, this.byteOffset);
}
@operator("[]=")
protected __set(index: i32, value: NATIVE<T>): void {
if (<u32>index >= <u32>(this.byteLength >>> alignof<T>())) throw new Error("Index out of bounds");
STORE<T,NATIVE<T>>(this.buffer, index, value, this.byteOffset);
}
@inline @operator("{}=")
protected __unchecked_set(index: i32, value: NATIVE<T>): void {
STORE<T,NATIVE<T>>(this.buffer, index, value, this.byteOffset);
}
// copyWithin(target: i32, start: i32, end: i32 = this.length): this
}
@inline
export function FILL<TArray extends TypedArray<T>, T>(
array: TArray,
value: NATIVE<T>,
start: i32,
end: i32
): TArray {
var buffer = array.buffer;
var byteOffset = array.byteOffset;
var len = array.length;
start = start < 0 ? max(len + start, 0) : min(start, len);
end = end < 0 ? max(len + end, 0) : min(end, len);
if (sizeof<T>() == 1) {
if (start < end) {
memory.fill(
changetype<usize>(buffer) + start + byteOffset + AB_HEADER_SIZE,
<u8>value,
<usize>(end - start)
);
}
} else {
for (; start < end; ++start) {
STORE<T,NATIVE<T>>(buffer, start, value, byteOffset);
}
}
return array;
}
@inline
export function SORT<TArray extends TypedArray<T>, T>(
array: TArray,
comparator: (a: T, b: T) => i32
): TArray {
var byteOffset = array.byteOffset;
var length = array.length;
if (length <= 1) return array;
var buffer = array.buffer;
if (length == 2) {
let a = LOAD<T>(buffer, 1, byteOffset);
let b = LOAD<T>(buffer, 0, byteOffset);
if (comparator(a, b) < 0) {
STORE<T>(buffer, 1, b, byteOffset);
STORE<T>(buffer, 0, a, byteOffset);
}
return array;
}
if (isReference<T>()) {
// TODO replace this to faster stable sort (TimSort) when it implemented
insertionSort<T>(buffer, byteOffset, length, comparator);
return array;
} else {
if (length < 256) {
insertionSort<T>(buffer, byteOffset, length, comparator);
} else {
weakHeapSort<T>(buffer, byteOffset, length, comparator);
}
return array;
}
}
@inline
export function SUBARRAY<TArray extends TypedArray<T>, T>(
array: TArray,
begin: i32,
end: i32
): TArray {
var length = <i32>array.length;
if (begin < 0) begin = max(length + begin, 0);
else begin = min(begin, length);
if (end < 0) end = max(length + end, begin);
else end = max(min(end, length), begin);
var slice = memory.allocate(offsetof<TArray>());
store<usize>(slice, array.buffer, offsetof<TArray>("buffer"));
store<i32>(slice, <i32>array.byteOffset + (begin << alignof<T>()), offsetof<TArray>("byteOffset"));
store<i32>(slice, (end - begin) << alignof<T>(), offsetof<TArray>("byteLength"));
return changetype<TArray>(slice);
}
@inline
export function REDUCE<TArray extends TypedArray<T>, T, TRet>(
array: TArray,
callbackfn: (accumulator: TRet, value: T, index: i32, array: TArray) => TRet,
initialValue: TRet
): TRet {
var index = 0;
var length = <i32>array.length;
while (index != length) {
initialValue = callbackfn(
initialValue,
unchecked(array[index]),
index,
array,
);
++index;
}
return initialValue;
}
@inline
export function REDUCE_RIGHT<TArray extends TypedArray<T>, T, TRet>(
array: TArray,
callbackfn: (accumulator: TRet, value: T, index: i32, array: TArray) => TRet,
initialValue: TRet
): TRet {
var index = <i32>array.length - 1;
var length = -1;
while (index != length) {
initialValue = callbackfn(
initialValue,
unchecked(array[index]),
index,
array,
);
--index;
}
return initialValue;
}