Repository navigation
Expand file tree
/
Copy patharray.ts
More file actions
110 lines (91 loc) · 3.01 KB
/
Copy patharray.ts
File metadata and controls
110 lines (91 loc) · 3.01 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
export class Array<T> implements IDisposable {
readonly capacity: i32; // @0 (4)
length: i32; // @4 (4)
readonly base: usize; // @8 (4/8)
constructor(capacity: i32) {
if (capacity < 0)
throw new Error("Invalid array length");
this.length = this.capacity = capacity;
const dataSize: usize = this.capacity as usize * sizeof<T>();
this.base = memset(malloc(dataSize), 0, dataSize);
}
indexOf(searchElement: T, fromIndex: i32 = 0): i32 {
let length: i32 = this.length;
if (length > this.capacity)
length = this.capacity;
// if negative, it is taken as the offset from the end of the array
if (fromIndex < 0) {
fromIndex = length + fromIndex;
// if the calculated index is less than 0, then the whole array will be searched
if (fromIndex < 0)
fromIndex = 0;
}
// implicit: if greater than or equal to the array's length, -1 is returned
while (fromIndex < length) {
if (this[fromIndex] == searchElement)
return fromIndex;
++fromIndex;
}
return -1;
}
lastIndexOf(searchElement: T, fromIndex: i32 = 0x7fffffff): i32 {
let length: i32 = this.length;
if (length > this.capacity)
length = this.capacity;
// if negative, it is taken as the offset from the end of the array
if (fromIndex < 0)
fromIndex = length + fromIndex;
// if greater than or equal to the length of the array, the whole array will be searched
else if (fromIndex >= length)
fromIndex = length - 1;
// implicit: if the calculated index is less than 0, -1 is returned
while (fromIndex >= 0) {
if (this[fromIndex] == searchElement)
return fromIndex;
--fromIndex;
}
return -1;
}
slice(begin: i32 = 0, end: i32 = 0x7fffffff): this {
let length: i32 = this.length;
if (length > this.capacity)
length = this.capacity;
if (begin < 0) {
begin = length + begin;
if (begin < 0)
begin = 0;
} else if (begin > length)
begin = length;
if (end < 0)
end = length + end;
else if (end > length)
end = length;
if (end < begin)
end = begin;
const capacity: i32 = end - begin;
const dataSize: usize = (capacity as usize) * sizeof<T>();
const slice: usize = malloc(sizeof<Array<T>>());
store<i32>(slice, capacity);
store<i32>(slice + 4, capacity);
store<usize>(slice + 8, this.base + begin * sizeof<T>());
return unsafe_cast<usize,this>(slice);
}
reverse(): this {
let length: i32 = this.length;
if (length > this.capacity)
length = this.capacity;
// transposes the elements of the calling array object in place, mutating the array
for (let i: i32 = 0, j: i32 = length - 1, t: i32; i < j; ++i, --j) {
t = this[i];
this[i] = this[j];
this[j] = t;
}
// and returning a reference to the array
return this;
}
dispose(disposeData: bool = true): void {
free(unsafe_cast<this,usize>(this));
if (disposeData)
free(this.base);
}
}