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 pathexpandingIntArray.mini
More file actions
193 lines (174 loc) · 5.08 KB
/
expandingIntArray.mini
File metadata and controls
193 lines (174 loc) · 5.08 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
//
// Copyright 2020, Offchain Labs, Inc. All rights reserved.
//
use std::bytearray::opClosure;
type ExpandingIntArray = struct {
size: uint,
chunk: uint,
contents: [8]any,
};
public func expandingIntArray_new() -> ExpandingIntArray {
struct {
size: 8,
chunk: 1,
contents: unsafecast<[8]any>((0,0,0,0,0,0,0,0)),
}
}
public func expandingIntArray_size(arr: ExpandingIntArray) -> uint {
arr.size
}
public func expandingIntArray_get(arr: ExpandingIntArray, index: uint) -> uint {
if index >= arr.size {
return 0;
}
let chunk = arr.chunk;
let tree = arr.contents;
while chunk > 1 {
tree = unsafecast<[8]any>(tree[index / chunk]);
index = index % chunk;
chunk = chunk / 8;
}
unsafecast<uint>(tree[index])
}
public func expandingIntArray_getConsecutive(arr: ExpandingIntArray, index: uint) -> (uint, uint) {
if index+1 >= arr.size {
return (expandingIntArray_get(arr, index), 0);
}
let tree = arr.contents;
let chunk = arr.chunk;
while chunk > 1 {
let offset = index % chunk;
if (offset+1) == chunk {
let loSlot = index / chunk;
return (
ear_get_2(unsafecast<[8]any>(tree[loSlot]), chunk/8, chunk-1),
ear_get_2(unsafecast<[8]any>(tree[loSlot+1]), chunk/8, 0)
);
}
tree = unsafecast<[8]any>(tree[index/chunk]);
index = offset;
chunk = chunk / 8;
}
(unsafecast<uint>(tree[index]), unsafecast<uint>(tree[index+1]))
}
func ear_get_2(tree: [8]any, chunk: uint, index: uint) -> uint {
while chunk > 1 {
tree = unsafecast<[8]any>(tree[index/chunk]);
index = index % chunk;
chunk = chunk / 8;
}
unsafecast<uint>(tree[index])
}
type Unwinder = struct {
tree: [8]any,
slot: uint,
next: option<Unwinder>,
};
public func expandingIntArray_set(arr: ExpandingIntArray, index: uint, value: uint) -> ExpandingIntArray {
expandingIntArray_setN(arr, 1, index, value)
}
public func expandingIntArray_setN(
arr: ExpandingIntArray,
inChunk: uint, // must be a power of 8
index: uint, // must be a multiple of inChunk
value: any
) -> ExpandingIntArray {
while (index+inChunk) > arr.size {
arr = expandingIntArray_grow(arr);
}
if (inChunk == arr.size) && (index == 0) {
return arr with { contents: unsafecast<[8]any>(value) };
}
let chunk = arr.chunk;
let tree = arr.contents;
let unwinder = None<Unwinder>;
loop {
if chunk <= inChunk {
tree = tree with { [index/chunk] = value };
loop {
if let Some(unw) = unwinder {
tree = unw.tree with { [unw.slot] = tree };
unwinder = unw.next;
} else {
return arr with { contents: tree };
}
};
}
let slot = index/chunk;
unwinder = Some(struct {
tree: tree,
slot: slot,
next: unwinder,
});
tree = unsafecast<[8]any>(tree[slot]);
index = index % chunk;
chunk = chunk / 8;
}
}
public throw func expandingIntArray_op(
arr: ExpandingIntArray,
index: uint,
applicator: opClosure
) -> (ExpandingIntArray, any) {
while index >= arr.size {
arr = expandingIntArray_grow(arr);
}
let (tree, retVal) = ear_op2(arr.contents, arr.chunk, index, applicator);
(
arr with { contents: tree },
retVal,
)
}
throw func ear_op2(
tree: [8]any,
chunk: uint,
index: uint,
applicator: opClosure
) -> ([8]any, any) {
if chunk == 1 {
let (newSlotContents, retVal) = applicator.f(applicator.val, unsafecast<uint>(tree[index]));
(
tree with { [index] = newSlotContents },
retVal,
)
} else {
let slot = index / chunk;
let (newSlotContents, retVal) = ear_op2(
unsafecast<[8]any>(tree[slot]),
chunk/8,
index%chunk,
applicator
);
(
tree with { [slot] = newSlotContents },
retVal,
)
}
}
public throw func expandingIntArray_opConsecutive(
arr: ExpandingIntArray,
index: uint,
loClosure: opClosure,
hiClosure: opClosure
) -> (ExpandingIntArray, any, any) {
// TODO: make this more efficient
let (arr1, val1) = expandingIntArray_op(arr, index, loClosure);
let (arr2, val2) = expandingIntArray_op(arr1, index+1, hiClosure);
(arr2, val1, val2)
}
func expandingIntArray_grow(arr: ExpandingIntArray) -> ExpandingIntArray {
let newContents = unsafecast<[8]any>((0,0,0,0,0,0,0,0));
let newChunk = 1;
while newChunk <= arr.chunk {
newContents = unsafecast<[8]any>(
(newContents, newContents, newContents, newContents, newContents, newContents, newContents, newContents)
);
newChunk = newChunk * 8;
}
newContents = newContents with { [0] = arr.contents };
struct {
size: 8*newChunk,
chunk: newChunk,
contents: newContents,
}
}