-
Notifications
You must be signed in to change notification settings - Fork 31
/
PersistentList.go
397 lines (331 loc) · 8.99 KB
/
PersistentList.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
394
395
396
397
// 基于 VectorTrie 实现的 PersistentList/PersistentVector.
// https://github.com/shanzi/algo-ds/tree/master/vector_trie
// !关注tail优化和transient
package main
import (
"fmt"
"runtime/debug"
"time"
)
func main() {
demo()
}
func init() {
debug.SetGCPercent(-1)
}
// 1146. 快照数组
// https://leetcode.cn/problems/snapshot-array/
type SnapshotArray struct {
arr IPersistentList
git []IPersistentList
}
func Constructor(length int) SnapshotArray {
arr := NewPersistentList().WithMutations(func(t ITransientList) {
for i := 0; i < length; i++ {
t.Push(0)
}
})
return SnapshotArray{arr: arr}
}
func (this *SnapshotArray) Set(index int, val int) {
this.arr, _ = this.arr.Set(index, val)
}
func (this *SnapshotArray) Snap() int {
this.git = append(this.git, this.arr)
return len(this.git) - 1
}
func (this *SnapshotArray) Get(index int, snap_id int) int {
res, _ := this.git[snap_id].Get(index)
return res.(int)
}
func demo() {
list0 := NewPersistentList()
list1 := list0.Push(1)
list2 := list1.Push(2)
list3, _ := list2.Set(0, 3)
list4, _ := list3.Pop()
fmt.Println(list0, list1, list2, list3, list4)
time1 := time.Now()
list5 := NewPersistentList().WithMutations(func(t ITransientList) {
for i := 0; i < 1e6; i++ {
t.Push(i)
}
})
time2 := time.Now()
fmt.Println(time2.Sub(time1)) // 18ms
_ = list5
// fmt.Println(list5)
}
type Value = int
const (
SHIFT = 5
NODE_SIZE = (1 << SHIFT)
MASK = NODE_SIZE - 1
)
var globalOwnerId int32
func nextId() int32 {
globalOwnerId++
return globalOwnerId
// return atomic.AddInt32(&id, 1) // concurrent safe
}
type IPersistentList interface {
Get(index int) (interface{}, bool)
Set(index int, value interface{}) (IPersistentList, bool)
Push(value interface{}) IPersistentList
Pop() (IPersistentList, interface{})
Len() int
// immutable.js style api
AsMutable() ITransientList
WithMutations(f func(ITransientList)) IPersistentList
}
// The difference is that Transient has an ID while List does not (0).
type PersistentList TransitentList
var EMPTY_PERSISTENT_LIST = &PersistentList{}
func NewPersistentList() IPersistentList {
return EMPTY_PERSISTENT_LIST
}
func (head *PersistentList) Get(index int) (interface{}, bool) {
return (*TransitentList)(head).Get(index)
}
func (head *PersistentList) Set(index int, value interface{}) (IPersistentList, bool) {
t := head.AsMutable()
if t.Set(index, value) {
return t.AsImmutable(), true
} else {
return head, false
}
}
func (head *PersistentList) Push(value interface{}) IPersistentList {
t := head.AsMutable()
t.Push(value)
return t.AsImmutable()
}
func (head *PersistentList) Pop() (IPersistentList, interface{}) {
if head.len == 1 {
value, _ := head.Get(0)
return EMPTY_PERSISTENT_LIST, value
} else {
t := head.AsMutable()
value := t.Pop()
return t.AsImmutable(), value
}
}
func (head *PersistentList) AsMutable() ITransientList {
id := nextId()
return &TransitentList{id, head.len, head.level, head.offset, head.root, head.tail}
}
func (head *PersistentList) WithMutations(f func(ITransientList)) IPersistentList {
t := head.AsMutable()
f(t)
return t.AsImmutable()
}
func (head *PersistentList) Len() int {
return int(head.len)
}
func (head *PersistentList) String() string {
res := make([]string, head.len)
for i := 0; i < int(head.len); i++ {
value, _ := head.Get(i)
res[i] = fmt.Sprintf("%v", value)
}
return fmt.Sprintf("%v", res)
}
type ITransientList interface {
Get(n int) (interface{}, bool)
Set(n int, value interface{}) bool
Push(value interface{})
Pop() interface{}
Len() int
// !dont modify previous transient list after `AsImmutable`
AsImmutable() IPersistentList
}
type TransitentList struct {
ownerId int32
len int32
level int8
offset int32 // 列表中在 tail 节点之前的节点当中储存的元素的数量,同时也是 tail 节点中下标0的元素在整个 List 当中的 Index
root *trieNode
tail *trieNode
}
func (head *TransitentList) Get(n int) (interface{}, bool) {
n32 := int32(n)
if n32 < 0 || n32 >= head.len {
return nil, false
}
if n32 >= head.offset {
return head.tail.getChildValue(n32 - head.offset), true
}
root := head.root
for lv := head.level - 1; ; lv-- {
index := (n32 >> (lv * SHIFT)) & MASK
if lv <= 0 {
// Arrived at leaves node, return value
return root.getChildValue(index), true
} else {
// Update root node
root = root.getChildNode(index)
}
}
}
func (head *TransitentList) Set(n int, value interface{}) bool {
n32 := int32(n)
if n32 < 0 || n32 >= head.len {
panic("Index out of bound")
}
ok := false
if n32 >= head.offset {
head.tail, ok = setTail(head.ownerId, head.tail, n32-head.offset, value)
} else {
head.root, ok = setInNode(head.ownerId, head.root, n32, head.level, value)
}
return ok
}
func (head *TransitentList) Push(value interface{}) {
if head.len-head.offset < NODE_SIZE {
// Tail node has free space
head.tail, _ = setTail(head.ownerId, head.tail, head.len-head.offset, value)
} else {
// Tail node is full
n := head.offset
lv := head.level
root := head.root
// Increase the depth of tree while the capacity is not enough
for lv == 0 || (n>>(lv*SHIFT)) > 0 {
parent := newTrieNode(head.ownerId)
parent.children[0] = root
root = parent
lv++
}
head.root = putTail(head.ownerId, root, head.tail, n, lv)
head.tail, _ = setTail(head.ownerId, nil, 0, value)
head.level = lv
head.offset += NODE_SIZE
}
head.len++
}
func (head *TransitentList) Pop() interface{} {
if head.len == 0 {
panic("Remove from empty list")
}
value := head.tail.getChildValue(head.len - head.offset - 1)
head.tail, _ = setTail(head.ownerId, head.tail, head.len-head.offset-1, nil) // clear reference to release memory
head.len--
if head.len == 0 {
head.level = 0
head.offset = 0
head.root = nil
head.tail = nil
} else {
if head.len <= head.offset {
// tail is empty, retrieve new tail from root
head.root, head.tail = getTail(head.ownerId, head.root, head.len-1, head.level)
head.offset -= NODE_SIZE
}
// Reduce the depth of tree if root only have one child
n := head.offset - 1
lv := head.level
root := head.root
for lv > 1 && (n>>((lv-1)*SHIFT)) == 0 {
// if root only have one child, it must be zero
root = root.getChildNode(0)
lv--
}
head.root = root
head.level = lv
}
return value
}
func (head *TransitentList) AsImmutable() IPersistentList {
perisitHead := (*PersistentList)(head)
perisitHead.ownerId = 0
return perisitHead
}
func (head *TransitentList) Len() int {
return int(head.len)
}
func setInNode(id int32, root *trieNode, n int32, level int8, value interface{}) (*trieNode, bool) {
index := (n >> ((level - 1) * SHIFT)) & MASK
if level == 1 {
if root.getChildValue(index) == value {
return root, false
}
return root.setChildValue(id, index, value), true
} else {
child := root.getChildNode(index)
newChild, wasAltered := setInNode(id, child, n, level-1, value)
if wasAltered {
return root.setChildValue(id, index, newChild), true
}
return root, false
}
}
func setTail(id int32, tail *trieNode, index int32, value interface{}) (*trieNode, bool) {
if tail == nil {
tail = newTrieNode(id)
}
if tail.getChildValue(index) == value {
return tail, false
}
return tail.setChildValue(id, index, value), true
}
func getTail(id int32, root *trieNode, n int32, level int8) (*trieNode, *trieNode) {
index := int32((n >> ((level - 1) * SHIFT)) & MASK)
if level == 1 {
return nil, root
} else {
child, tail := getTail(id, root.getChildNode(index), n, level-1)
if index == 0 && child == nil {
// The first element has been removed, which means current node
// becomes empty, remove current node by returning nil
return nil, tail
} else {
// Current node is not empty
root = root.setChildValue(id, index, child)
return root, tail
}
}
}
// find the tail node and put it into the correct position
func putTail(id int32, root *trieNode, tail *trieNode, n int32, level int8) *trieNode {
index := int32((n >> ((level - 1) * SHIFT)) & MASK)
if level == 1 {
return tail
} else {
if root == nil {
root = newTrieNode(id)
}
return root.setChildValue(id, index, putTail(id, root.getChildNode(index), tail, n, level-1))
}
}
type trieNode struct {
id int32
children []interface{}
}
func newTrieNode(id int32) *trieNode {
return &trieNode{id, make([]interface{}, NODE_SIZE)}
}
func (node *trieNode) getChildNode(index int32) *trieNode {
if child := node.children[index]; child != nil {
return child.(*trieNode)
} else {
return nil
}
}
func (node *trieNode) getChildValue(index int32) interface{} {
return node.children[index]
}
func (node *trieNode) setChildValue(id int32, index int32, value interface{}) *trieNode {
if node.id == id {
node.children[index] = value // transient
return node
} else {
newNode := node.clone(id)
newNode.children[index] = value
return newNode
}
}
func (node *trieNode) clone(id int32) *trieNode {
children := make([]interface{}, NODE_SIZE)
copy(children, node.children)
return &trieNode{id, children}
}