forked from AssemblyScript/assemblyscript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstaticarray.ts
More file actions
400 lines (362 loc) · 14.5 KB
/
staticarray.ts
File metadata and controls
400 lines (362 loc) · 14.5 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/// <reference path="./rt/index.d.ts" />
import { OBJECT, BLOCK_MAXSIZE, TOTAL_OVERHEAD } from "./rt/common";
import { Runtime } from "shared/runtime";
import { COMPARATOR, SORT } from "./util/sort";
import { REVERSE } from "./util/bytes";
import { idof } from "./builtins";
import { Array } from "./array";
import { E_INDEXOUTOFRANGE, E_INVALIDLENGTH, E_HOLEYARRAY } from "./util/error";
import { joinBooleanArray, joinIntegerArray, joinFloatArray, joinStringArray, joinReferenceArray } from "./util/string";
@final
export class StaticArray<T> {
[key: number]: T;
// Note that the interface of StaticArray instances must be a semantically
// compatible subset of Array<T> in order for syntax highlighting to work
// properly, for instance when creating static arrays from array literals.
// The additionally provided static methods take care of dealing with static
// arrays exclusively, without having to convert to Array<T> first.
static fromArray<T>(source: Array<T>): StaticArray<T> {
var length = source.length;
var outSize = <usize>length << alignof<T>();
var out = changetype<StaticArray<T>>(__new(outSize, idof<StaticArray<T>>()));
if (isManaged<T>()) {
let sourcePtr = source.dataStart;
for (let i = 0; i < length; ++i) {
let off = <usize>i << alignof<T>();
let ref = load<usize>(sourcePtr + off);
store<usize>(changetype<usize>(out) + off, ref);
__link(changetype<usize>(out), ref, true);
}
} else {
memory.copy(changetype<usize>(out), source.dataStart, outSize);
}
return out;
}
static concat<T>(source: StaticArray<T>, other: StaticArray<T>): StaticArray<T> {
var sourceLen = source.length;
var otherLen = other.length;
var outLen = sourceLen + otherLen;
if (<u32>outLen > <u32>BLOCK_MAXSIZE >>> alignof<T>()) throw new Error(E_INVALIDLENGTH);
var out = changetype<StaticArray<T>>(__new(<usize>outLen << alignof<T>(), idof<StaticArray<T>>()));
var outStart = changetype<usize>(out);
var sourceSize = <usize>sourceLen << alignof<T>();
if (isManaged<T>()) {
for (let offset: usize = 0; offset < sourceSize; offset += sizeof<T>()) {
let ref = load<usize>(changetype<usize>(source) + offset);
store<usize>(outStart + offset, ref);
__link(changetype<usize>(out), ref, true);
}
outStart += sourceSize;
let otherSize = <usize>otherLen << alignof<T>();
for (let offset: usize = 0; offset < otherSize; offset += sizeof<T>()) {
let ref = load<usize>(changetype<usize>(other) + offset);
store<usize>(outStart + offset, ref);
__link(changetype<usize>(out), ref, true);
}
} else {
memory.copy(outStart, changetype<usize>(source), sourceSize);
memory.copy(outStart + sourceSize, changetype<usize>(other), <usize>otherLen << alignof<T>());
}
return out;
}
static slice<T>(source: StaticArray<T>, start: i32 = 0, end: i32 = i32.MAX_VALUE): StaticArray<T> {
var length = source.length;
start = start < 0 ? max(start + length, 0) : min(start, length);
end = end < 0 ? max(end + length, 0) : min(end , length);
length = max(end - start, 0);
var sliceSize = <usize>length << alignof<T>();
var slice = changetype<StaticArray<T>>(__new(sliceSize, idof<StaticArray<T>>()));
var sourcePtr = changetype<usize>(source) + (<usize>start << alignof<T>());
if (isManaged<T>()) {
let off: usize = 0;
while (off < sliceSize) {
let ref = load<usize>(sourcePtr + off);
store<usize>(changetype<usize>(slice) + off, ref);
__link(changetype<usize>(slice), ref, true);
off += sizeof<usize>();
}
} else {
memory.copy(changetype<usize>(slice), sourcePtr, sliceSize);
}
return slice;
}
constructor(length: i32) {
if (<u32>length > <u32>BLOCK_MAXSIZE >>> alignof<T>()) throw new RangeError(E_INVALIDLENGTH);
var outSize = <usize>length << alignof<T>();
var out = changetype<StaticArray<T>>(__new(outSize, idof<StaticArray<T>>()));
if (ASC_RUNTIME != Runtime.Incremental) {
memory.fill(changetype<usize>(out), 0, outSize);
}
return out;
}
get length(): i32 {
return changetype<OBJECT>(changetype<usize>(this) - TOTAL_OVERHEAD).rtSize >>> alignof<T>();
}
at(index: i32): T {
var len = this.length;
index += select(0, len, index >= 0);
if (<u32>index >= <u32>len) throw new RangeError(E_INDEXOUTOFRANGE);
var value = load<T>(changetype<usize>(this) + (<usize>index << alignof<T>()));
if (isReference<T>()) {
if (!isNullable<T>()) {
if (!changetype<usize>(value)) throw new Error(E_HOLEYARRAY);
}
}
return value;
}
@operator("[]") private __get(index: i32): T {
if (<u32>index >= <u32>this.length) throw new RangeError(E_INDEXOUTOFRANGE);
var value = load<T>(changetype<usize>(this) + (<usize>index << alignof<T>()));
if (isReference<T>()) {
if (!isNullable<T>()) {
if (!changetype<usize>(value)) throw new Error(E_HOLEYARRAY);
}
}
return value;
}
@unsafe @operator("{}") private __uget(index: i32): T {
return load<T>(changetype<usize>(this) + (<usize>index << alignof<T>()));
}
@operator("[]=") private __set(index: i32, value: T): void {
if (<u32>index >= <u32>this.length) throw new RangeError(E_INDEXOUTOFRANGE);
this.__uset(index, value);
}
@unsafe @operator("{}=") private __uset(index: i32, value: T): void {
store<T>(changetype<usize>(this) + (<usize>index << alignof<T>()), value);
if (isManaged<T>()) {
__link(changetype<usize>(this), changetype<usize>(value), true);
}
}
fill(value: T, start: i32 = 0, end: i32 = i32.MAX_VALUE): this {
var ptr = changetype<usize>(this);
var len = this.length;
start = start < 0 ? max(len + start, 0) : min(start, len);
end = end < 0 ? max(len + end, 0) : min(end, len);
if (isManaged<T>()) {
for (; start < end; ++start) {
store<usize>(ptr + (<usize>start << alignof<T>()), changetype<usize>(value));
__link(changetype<usize>(this), changetype<usize>(value), true);
}
} else if (sizeof<T>() == 1) {
if (start < end) {
memory.fill(
ptr + <usize>start,
u8(value),
<usize>(end - start)
);
}
} else {
for (; start < end; ++start) {
store<T>(ptr + (<usize>start << alignof<T>()), value);
}
}
return this;
}
copyWithin(target: i32, start: i32, end: i32 = i32.MAX_VALUE): this {
var ptr = changetype<usize>(this);
var len = this.length;
end = min<i32>(end, len);
var to = target < 0 ? max(len + target, 0) : min(target, len);
var from = start < 0 ? max(len + start, 0) : min(start, len);
var last = end < 0 ? max(len + end, 0) : min(end, len);
var count = min(last - from, len - to);
memory.copy( // is memmove
ptr + (<usize>to << alignof<T>()),
ptr + (<usize>from << alignof<T>()),
<usize>count << alignof<T>()
);
return this;
}
includes(value: T, fromIndex: i32 = 0): bool {
if (isFloat<T>()) {
let length = this.length;
if (length == 0 || fromIndex >= length) return false;
if (fromIndex < 0) fromIndex = max(length + fromIndex, 0);
while (fromIndex < length) {
let elem = load<T>(changetype<usize>(this) + (<usize>fromIndex << alignof<T>()));
// @ts-ignore
if (elem == value || isNaN(elem) & isNaN(value)) return true;
++fromIndex;
}
return false;
} else {
return this.indexOf(value, fromIndex) >= 0;
}
}
indexOf(value: T, fromIndex: i32 = 0): i32 {
var length = this.length;
if (length == 0 || fromIndex >= length) return -1;
if (fromIndex < 0) fromIndex = max(length + fromIndex, 0);
while (fromIndex < length) {
if (load<T>(changetype<usize>(this) + (<usize>fromIndex << alignof<T>())) == value) return fromIndex;
++fromIndex;
}
return -1;
}
lastIndexOf(value: T, fromIndex: i32 = this.length): i32 {
var length = this.length;
if (length == 0) return -1;
if (fromIndex < 0) fromIndex = length + fromIndex;
else if (fromIndex >= length) fromIndex = length - 1;
while (fromIndex >= 0) {
if (load<T>(changetype<usize>(this) + (<usize>fromIndex << alignof<T>())) == value) return fromIndex;
--fromIndex;
}
return -1;
}
concat(other: Array<T>): Array<T> {
var thisLen = this.length;
var otherLen = other.length;
var outLen = thisLen + otherLen;
if (<u32>outLen > <u32>BLOCK_MAXSIZE >>> alignof<T>()) throw new Error(E_INVALIDLENGTH);
var out = changetype<Array<T>>(__newArray(outLen, alignof<T>(), idof<Array<T>>()));
var outStart = out.dataStart;
var thisSize = <usize>thisLen << alignof<T>();
if (isManaged<T>()) {
let thisStart = changetype<usize>(this);
for (let offset: usize = 0; offset < thisSize; offset += sizeof<T>()) {
let ref = load<usize>(thisStart + offset);
store<usize>(outStart + offset, ref);
__link(changetype<usize>(out), ref, true);
}
outStart += thisSize;
let otherStart = other.dataStart;
let otherSize = <usize>otherLen << alignof<T>();
for (let offset: usize = 0; offset < otherSize; offset += sizeof<T>()) {
let ref = load<usize>(otherStart + offset);
store<usize>(outStart + offset, ref);
__link(changetype<usize>(out), ref, true);
}
} else {
memory.copy(outStart, changetype<usize>(this), thisSize);
memory.copy(outStart + thisSize, other.dataStart, <usize>otherLen << alignof<T>());
}
return out;
}
slice(start: i32 = 0, end: i32 = i32.MAX_VALUE): Array<T> {
var length = this.length;
start = start < 0 ? max(start + length, 0) : min(start, length);
end = end < 0 ? max(end + length, 0) : min(end , length);
length = max(end - start, 0);
var slice = changetype<Array<T>>(__newArray(length, alignof<T>(), idof<Array<T>>()));
var sliceBase = slice.dataStart;
var thisBase = changetype<usize>(this) + (<usize>start << alignof<T>());
if (isManaged<T>()) {
let off = <usize>0;
let end = <usize>length << alignof<usize>();
while (off < end) {
let ref = load<usize>(thisBase + off);
store<usize>(sliceBase + off, ref);
__link(changetype<usize>(slice), ref, true);
off += sizeof<usize>();
}
} else {
memory.copy(sliceBase, thisBase, length << alignof<T>());
}
return slice;
}
findIndex(fn: (value: T, index: i32, array: StaticArray<T>) => bool): i32 {
for (let i = 0, len = this.length; i < len; ++i) {
if (fn(load<T>(changetype<usize>(this) + (<usize>i << alignof<T>())), i, this)) return i;
}
return -1;
}
findLastIndex(fn: (value: T, index: i32, array: StaticArray<T>) => bool): i32 {
for (let i = this.length - 1; i >= 0; --i) {
if (fn(load<T>(changetype<usize>(this) + (<usize>i << alignof<T>())), i, this)) return i;
}
return -1;
}
forEach(fn: (value: T, index: i32, array: StaticArray<T>) => void): void {
for (let i = 0, len = this.length; i < len; ++i) {
fn(load<T>(changetype<usize>(this) + (<usize>i << alignof<T>())), i, this);
}
}
map<U>(fn: (value: T, index: i32, array: StaticArray<T>) => U): Array<U> {
var len = this.length;
var out = changetype<Array<U>>(__newArray(len, alignof<U>(), idof<Array<U>>()));
var outStart = out.dataStart;
for (let i = 0; i < len; ++i) {
let result = fn(load<T>(changetype<usize>(this) + (<usize>i << alignof<T>())), i, this);
store<U>(outStart + (<usize>i << alignof<U>()), result);
if (isManaged<U>()) {
__link(changetype<usize>(out), changetype<usize>(result), true);
}
}
return out;
}
filter(fn: (value: T, index: i32, array: StaticArray<T>) => bool): Array<T> {
var result = changetype<Array<T>>(__newArray(0, alignof<T>(), idof<Array<T>>()));
for (let i = 0, len = this.length; i < len; ++i) {
let value = load<T>(changetype<usize>(this) + (<usize>i << alignof<T>()));
if (fn(value, i, this)) result.push(value);
}
return result;
}
reduce<U>(
fn: (previousValue: U, currentValue: T, currentIndex: i32, array: StaticArray<T>) => U,
initialValue: U
): U {
var acc = initialValue;
for (let i = 0, len = this.length; i < len; ++i) {
acc = fn(acc, load<T>(changetype<usize>(this) + (<usize>i << alignof<T>())), i, this);
}
return acc;
}
reduceRight<U>(
fn: (previousValue: U, currentValue: T, currentIndex: i32, array: StaticArray<T>) => U,
initialValue: U
): U {
var acc = initialValue;
for (let i = this.length - 1; i >= 0; --i) {
acc = fn(acc, load<T>(changetype<usize>(this) + (<usize>i << alignof<T>())), i, this);
}
return acc;
}
every(fn: (value: T, index: i32, array: StaticArray<T>) => bool): bool {
for (let i = 0, len = this.length; i < len; ++i) {
if (!fn(load<T>(changetype<usize>(this) + (<usize>i << alignof<T>())), i, this)) return false;
}
return true;
}
some(fn: (value: T, index: i32, array: StaticArray<T>) => bool): bool {
for (let i = 0, len = this.length; i < len; ++i) {
if (fn(load<T>(changetype<usize>(this) + (<usize>i << alignof<T>())), i, this)) return true;
}
return false;
}
sort(comparator: (a: T, b: T) => i32 = COMPARATOR<T>()): this {
SORT<T>(changetype<usize>(this), this.length, comparator);
return this;
}
join(separator: string = ","): string {
if (isBoolean<T>()) return joinBooleanArray(changetype<usize>(this), this.length, separator);
if (isInteger<T>()) return joinIntegerArray<T>(changetype<usize>(this), this.length, separator);
if (isFloat<T>()) return joinFloatArray<T>(changetype<usize>(this), this.length, separator);
if (ASC_SHRINK_LEVEL < 1) {
if (isString<T>()) return joinStringArray(changetype<usize>(this), this.length, separator);
}
if (isReference<T>()) return joinReferenceArray<T>(changetype<usize>(this), this.length, separator);
ERROR("unspported element type");
return <string>unreachable();
}
reverse(): this {
REVERSE<T>(changetype<usize>(this), this.length);
return this;
}
toString(): string {
return this.join();
}
// RT integration
@unsafe private __visit(cookie: u32): void {
if (isManaged<T>()) {
let cur = changetype<usize>(this);
let end = cur + changetype<OBJECT>(changetype<usize>(this) - TOTAL_OVERHEAD).rtSize;
while (cur < end) {
let val = load<usize>(cur);
if (val) __visit(val, cookie);
cur += sizeof<usize>();
}
}
}
}