This repository was archived by the owner on Nov 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy patharray.mini
More file actions
167 lines (151 loc) · 4.52 KB
/
array.mini
File metadata and controls
167 lines (151 loc) · 4.52 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
/*
* Copyright 2020, Offchain Labs, Inc. All rights reserved.
*/
type block<T> = [8]T; // this is basically a maximum-size AVM tuple
type array<T> = struct {
size: uint,
topstep: uint,
contents: block<T>,
access: func(array<T>, uint) -> T,
};
public func array_new<T>(size: uint, base_val: T) -> array<T> {
let chunk = 1;
while 8*chunk < size {
chunk = 8*chunk;
base_val = unsafecast<T>(newfixedarray(8, base_val));
}
struct {
size: size,
topstep: chunk,
contents: newfixedarray(8, base_val),
access: array_get::<T>,
}
}
public throw func array_get<T>(arr: array<T>, index: uint) -> T {
if index >= arr.size {
error; // out of bounds access
}
let res = arr.contents;
let chunk = arr.topstep;
while 1 <= chunk {
res = unsafecast<block<T> >(res[index/chunk]);
index = index % chunk;
chunk = chunk / 8;
}
unsafecast<T>(res)
}
public throw func array_get_consecutive<T>(arr: array<T>, index: uint) -> (T, T) {
// Get two consecutive items at [index] and [index+1]
// This will be faster than two separate arrayGets, in the common case.
if index+1 >= arr.size {
error;
}
if index % 8 == 7 {
// leaves are not in same bottom-level block, do it the slow way
(array_get::<T>(arr, index), array_get::<T>(arr, index+1))
} else {
let res = arr.contents;
let chunk = arr.topstep;
while 8 <= chunk {
res = unsafecast<block<T> >(res[index/chunk]);
index = index % chunk;
chunk = chunk / 8;
}
(res[index], res[index+1])
}
}
public throw func array_set<T>(arr: array<T>, index: uint, value: T) -> array<T> {
if index >= arr.size {
error;
}
arr with { contents: arraySetInternal::<T>(arr.contents, arr.topstep, index, value) }
}
throw func arraySetInternal<T>(b: block<T>, chunkSize: uint, index: uint, value: T) -> block<T> {
if 1 == chunkSize {
// at a leaf of the tree
b with {[index] = value}
} else {
// at an internal node of the tree
b with {[index/chunkSize] = unsafecast<T>(arraySetInternal::<T>(
unsafecast<block<T> >(b[index/chunkSize]),
chunkSize / 8,
index % chunkSize,
value
))}
}
}
public throw func array_swap<T>(a: array<T>, index: uint, value: T) -> (array<T>, T) {
// Write a new value into a slot of the array, returning the old value that was in that slot
if index >= a.size {
error;
}
let sub = arraySwapInternal::<T>(a.contents, a.topstep, index, value);
(
a with { contents: sub.bloc },
sub.val,
)
}
type arraySwapSubResult<T> = struct {
bloc: block<T>,
val: T,
};
throw func arraySwapInternal<T>(b: block<T>, chunk: uint, index: uint, value: T) -> arraySwapSubResult<T> {
if 1 == chunk {
struct {
bloc: b with {[index] = value},
val: b[index],
}
} else {
let subIndex = index/chunk;
let sub = arraySwapInternal::<T>(
unsafecast<block<T> >(b[subIndex]),
chunk / 8,
index % chunk,
value
);
sub with { bloc: b with {[subIndex] = unsafecast<T>(sub.bloc) } }
}
}
public throw func array_resize<T>(a: array<T>, newSize: uint, baseVal: T) -> array<T> {
// Resize an array.
// If the new size is larger, copy over all of the old contents, and fill the added elements with baseVal.
// If the new size is smaller, copy over all of the old contents that will fit.
// TODO: make this more efficient by reusing more of the tree-structure of a
let minSize = a.size;
if minSize > newSize {
minSize = newSize;
}
let ret = array_new::<T>(newSize, baseVal);
let i = 0;
while i < minSize {
ret = array_set::<T>(ret, i, array_get::<T>(a, i));
i = i+1;
}
ret
}
public throw func array_get_safe<T>(arr: array<T>, index: uint) -> option<T> {
if index >= arr.size {
return None;
}
Some(array_get::<T>(arr, index)) // won't error, because we bounds-checked
}
public throw func array_set_safe<T>(arr: array<T>, index: uint, value: T) -> option<array<T> > {
if index >= arr.size {
return None;
}
Some(array_set::<T>(arr, index, value))
}
public throw func array_get_consecutive_safe<T>(arr: array<T>, index: uint) -> option<(T, T)> {
// Return Non if access is out-of-bounds, otherwise return Some((result, result'))
if index+1 >= arr.size {
return None;
}
Some(array_get_consecutive::<T>(arr, index))
}
public throw func array_swap_safe<T>(a: array<T>, index: uint, value: T) -> option<(array<T>, T)> {
if index >= a.size {
return None;
}
let (aOut, valOut) = array_swap::<T>(a, index, value);
Some((aOut, valOut))
}