-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector.go
393 lines (328 loc) · 7.82 KB
/
vector.go
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
package vector
const (
bits = 5 // number of bits needed to represent the range (0 32].
width = 32
mask = width - 1 // 0x1f
)
// Vector is an immutable vector implementation with O(1) lookup,
// insertion, appending, and deletion.
type Vector[T any] struct {
cnt, shift int
root, tail *node[T]
}
func New[T any](items ...T) (vec Vector[T]) {
if len(items) > 0 {
trans := vec.transient()
trans.Append(items...)
vec = trans.Vector()
}
return
}
func newVector[T any]() Vector[T] {
node := &node[T]{}
return Vector[T]{
shift: bits,
root: node,
tail: node,
}
}
// Transient returns a *Transient with the same value as v.
// The transient vector is mutable, making suitable for optimizing
// tight loops where intermediate values of the transient are not shared.
// When the transient vector has reached the desired state, it should be
// persisted with a call to Persistent() prior to sharing.
func (v Vector[T]) transient() *Builder[T] {
if v == (Vector[T]{}) {
v = newVector[T]()
}
return &Builder[T]{
cnt: v.cnt,
shift: v.shift,
root: v.root.clone(),
tail: v.tail.clone(),
}
}
// Len returns the number of elements contained in the Vector.
func (v Vector[T]) Len() int {
return v.cnt
}
func (v Vector[T]) tailoff() int {
if v.cnt < width {
return 0
}
return ((v.cnt - 1) >> bits) << bits
}
func (v Vector[T]) nodeFor(i int) *node[T] {
if i >= 0 && i < v.cnt {
if i >= v.tailoff() {
return v.tail
}
n := v.root
for level := v.shift; level > 0; level -= bits {
n = n.array[(i>>level)&mask].(*node[T])
}
return n
}
panic("index out of bounds")
}
// At i returns the ith entry in the Vector
func (v Vector[T]) At(i int) T {
t, _ := v.nodeFor(i).array[i&mask].(T)
return t
}
// Set takes a value and "associates" it to the Vector,
// assigning it to the index.
func (v Vector[T]) Set(index int, t T) Vector[T] {
if index >= 0 && index < v.cnt {
if index >= v.tailoff() {
newTail := v.tail.clone()
newTail.array[index&mask] = t
return Vector[T]{
cnt: v.cnt,
shift: v.shift,
root: v.root,
tail: newTail,
}
}
return Vector[T]{
cnt: v.cnt,
shift: v.shift,
root: v.doAssoc(v.shift, v.root, index, t),
tail: v.tail,
}
}
if index == v.cnt {
return v.cons(t)
}
panic("index out of bounds")
}
func (v Vector[T]) doAssoc(level int, n *node[T], i int, t T) *node[T] {
ret := n
if level == 0 {
ret.array[i&mask] = t
} else {
subidx := (i >> level) & mask
ret.array[subidx] = v.doAssoc(level-bits, n.array[subidx].(*node[T]), i, t)
}
return ret
}
// Append values to the Vector.
func (v Vector[T]) Append(ts ...T) Vector[T] {
switch len(ts) {
case 0:
return v
case 1:
return v.cons(ts[0])
default:
head, ts := ts[0], ts[1:]
b := v.cons(head).transient()
b.Append(ts...)
return b.Vector()
}
}
func (v Vector[T]) cons(t T) Vector[T] {
if v == (Vector[T]{}) {
v = newVector[T]()
}
// room in tail?
if v.cnt-v.tailoff() < 32 {
newTail := v.tail.clone()
newTail.len++
newTail.array[v.tail.len] = t
return Vector[T]{
cnt: v.cnt + 1,
shift: v.shift,
root: v.root,
tail: newTail,
}
}
// full tail; push into trie
newRoot := &node[T]{}
tailNode := v.tail.clone()
newShift := v.shift
// overflow root?
if (v.cnt >> bits) > (1 << v.shift) {
newRoot.len += 2
newRoot.array[0] = v.root
newRoot.array[1] = newPath(v.shift, tailNode)
newShift += bits
} else {
newRoot = v.pushTail(v.shift, v.root, tailNode)
}
return Vector[T]{
cnt: v.cnt + 1,
shift: newShift,
root: newRoot,
tail: newValueNode(t),
}
}
func newPath[T any](level int, n *node[T]) *node[T] {
if level <= 0 {
return n
}
return newPathNode(newPath(level-bits, n))
}
func (v Vector[T]) pushTail(level int, parent, tailNode *node[T]) *node[T] {
//if parent is leaf, insert node,
// else does it map to an existing child? -> nodeToInsert = pushNode one more level
// else alloc new path
//return nodeToInsert placed in copy of parent
subidx := ((v.cnt - 1) >> level) & mask
ret := parent.clone()
var nodeToInsert *node[T]
if level == bits {
nodeToInsert = tailNode
} else {
if child := parent.array[subidx]; child != nil {
nodeToInsert = v.pushTail(level-bits, child.(*node[T]), tailNode)
} else {
nodeToInsert = newPath(level-bits, tailNode)
}
}
ret.array[subidx] = nodeToInsert
return ret
}
// Pop returns a copy of the Vector without its last element.
func (v Vector[T]) Pop() Vector[T] {
if v.cnt <= 1 {
return Vector[T]{}
}
// len(tail) > 1 ?
if v.cnt-v.tailoff() > 1 {
newTail := &node[T]{len: v.tail.len - 1}
copy(newTail.array[:newTail.len], v.tail.array[:])
return Vector[T]{
cnt: v.cnt - 1,
shift: v.shift,
root: v.root,
tail: newTail,
}
}
newTail := v.nodeFor(v.cnt - 2)
newRoot := v.popTail(v.shift, v.root)
newShift := v.shift
if newRoot == nil {
newRoot = &node[T]{}
}
if v.shift > bits && newRoot.array[1] == nil {
if newRoot.array[0] == nil {
newRoot = &node[T]{}
}
newShift -= bits
}
return Vector[T]{
cnt: v.cnt - 1,
shift: newShift,
root: newRoot,
tail: newTail,
}
}
func (v Vector[T]) popTail(level int, n *node[T]) *node[T] {
subidx := ((v.cnt - 2) >> level) & mask
if level > bits {
newChild := v.popTail(level-bits, n.array[subidx].(*node[T]))
if newChild == nil && subidx == 0 {
return nil
}
ret := n.clone()
ret.array[subidx] = newChild
// ret.len++
return ret
} else if subidx == 0 {
return nil
}
ret := n.clone()
ret.array[subidx] = node[T]{}
return ret
}
// Builder is a mutable Vector that minimizes allocation for all operations.
// Callers MUST NOT share transient objects, nor convert shared Vectors into
// Builders.
type Builder[T any] struct {
cnt, shift int
root, tail *node[T]
}
func NewBuilder[T any]() *Builder[T] {
vec := newVector[T]()
return (*Builder[T])(&vec)
}
// Vector finalizes the builder into a Vector.
// Users MUST NOT mutate t after a call to Vector.
func (t Builder[T]) Vector() Vector[T] { return (Vector[T])(t) }
func (t Builder[T]) tailoff() int { return (Vector[T])(t).tailoff() }
// Count the number of elements in the vector.
func (t *Builder[T]) Len() int { return t.cnt }
// Append values to the vector
func (t *Builder[T]) Append(ts ...T) {
for _, val := range ts {
t.Cons(val)
}
}
func (t *Builder[T]) Cons(val T) {
// room in tail?
if t.cnt-t.tailoff() < 32 {
t.tail.array[t.cnt&mask] = val
t.tail.len++
t.cnt++
return
}
// full tail; push into trie
newRoot := &node[T]{}
tailNode := t.tail.clone()
t.tail = newValueNode(val)
newShift := t.shift
// overflow root?
if (t.cnt >> bits) > (1 << t.shift) {
newRoot.len += 2
newRoot.array[0] = t.root
newRoot.array[1] = newPath(t.shift, tailNode)
newShift += 5
} else {
newRoot = t.pushTail(t.shift, t.root, tailNode)
}
t.root = newRoot
t.shift = newShift
t.cnt++
}
func (t *Builder[T]) pushTail(level int, parent, tailNode *node[T]) *node[T] {
//if parent is leaf, insert node,
// else does it map to an existing child? -> nodeToInsert = pushNode one more level
// else alloc new path
//return nodeToInsert placed in parent
subidx := ((t.cnt - 1) >> level) & mask
ret := parent // mutable; don't clone
var nodeToInsert *node[T]
if level == bits {
nodeToInsert = tailNode
} else {
if child := parent.array[subidx]; child != nil {
nodeToInsert = t.pushTail(level-bits, child.(*node[T]), tailNode)
} else {
nodeToInsert = newPath(level-bits, tailNode)
}
}
ret.array[subidx] = nodeToInsert
return ret
}
type node[T any] struct {
len int
array [width]any
}
func newValueNode[T any](vs ...T) *node[T] {
n := &node[T]{len: len(vs)}
for i, v := range vs {
n.array[i] = v
}
return n
}
func newPathNode[T any](n *node[T]) *node[T] {
out := &node[T]{len: 1}
out.array[0] = n
return out
}
func (n *node[T]) clone() *node[T] {
return &node[T]{
len: n.len,
array: n.array,
}
}