This repository has been archived by the owner on Oct 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
cedar.go
521 lines (427 loc) · 9.52 KB
/
cedar.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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
package cedar
const (
// ValueLimit limit value
ValueLimit = int(^uint(0) >> 1)
)
type node struct {
Value int
Check int
}
func (n *node) base() int {
return -(n.Value + 1)
}
type nvalue struct {
Len int
Value interface{}
}
type ninfo struct {
Sibling, Child byte
End bool
}
type block struct {
Prev, Next, Num, Reject, Trial, Ehead int
}
func (b *block) init() {
b.Num = 256
b.Reject = 257
}
// Cedar cedar struct
type Cedar struct {
Array []node
Ninfos []ninfo
Blocks []block
Reject [257]int
vals map[int]nvalue
vkey int
BheadF, BheadC, BheadO int
Capacity int
Size int
Ordered bool
MaxTrial int
}
// New new Cedar
func New() *Cedar {
da := Cedar{
Array: make([]node, 256),
Ninfos: make([]ninfo, 256),
Blocks: make([]block, 1),
vals: make(map[int]nvalue),
vkey: 1,
Capacity: 256,
Size: 256,
Ordered: true,
MaxTrial: 1,
}
da.Array[0] = node{-2, 0}
for i := 1; i < 256; i++ {
da.Array[i] = node{-(i - 1), -(i + 1)}
}
da.Array[1].Value = -255
da.Array[255].Check = -1
da.Blocks[0].Ehead = 1
da.Blocks[0].init()
for i := 0; i <= 256; i++ {
da.Reject[i] = i + 1
}
return &da
}
// Get value by key, insert the key if not exist
func (da *Cedar) get(key []byte, from, pos int) *int {
to := da.getV(key, from, pos)
return &da.Array[to].Value
}
// GetV value by key, insert the key if not exist
func (da *Cedar) getV(key []byte, from, pos int) int {
for ; pos < len(key); pos++ {
if value := da.Array[from].Value; value >= 0 && value != ValueLimit {
to := da.follow(from, 0)
da.Array[to].Value = value
}
from = da.follow(from, key[pos])
}
to := from
if da.Array[from].Value < 0 {
to = da.follow(from, 0)
}
return to
}
func (da *Cedar) vKey() int {
k := da.vkey
for {
k = (k + 1) % da.Capacity
if _, ok := da.vals[k]; !ok {
break
}
}
da.vkey = k
return k
}
func (da *Cedar) follow(from int, label byte) int {
base := da.Array[from].base()
to := base ^ int(label)
if base < 0 || da.Array[to].Check < 0 {
hasChild := false
if base >= 0 {
hasChild = (da.Array[base^int(da.Ninfos[from].Child)].Check == from)
}
to = da.popEnode(base, from, label)
da.pushSibling(from, to^int(label), label, hasChild)
return to
}
if da.Array[to].Check != from {
to = da.resolve(from, base, label)
return to
}
if da.Array[to].Check == from {
return to
}
panic("Cedar: internal error, should not be here")
// return to
}
func (da *Cedar) popBlock(bi int, headIn *int, last bool) {
if last {
*headIn = 0
return
}
b := &da.Blocks[bi]
da.Blocks[b.Prev].Next = b.Next
da.Blocks[b.Next].Prev = b.Prev
if bi == *headIn {
*headIn = b.Next
}
}
func (da *Cedar) pushBlock(bi int, headOut *int, empty bool) {
b := &da.Blocks[bi]
if empty {
*headOut, b.Prev, b.Next = bi, bi, bi
} else {
tailOut := &da.Blocks[*headOut].Prev
b.Prev = *tailOut
b.Next = *headOut
*headOut, *tailOut, da.Blocks[*tailOut].Next = bi, bi, bi
}
}
func (da *Cedar) addBlock() int {
if da.Size == da.Capacity {
da.Capacity *= 2
oldArray := da.Array
da.Array = make([]node, da.Capacity)
copy(da.Array, oldArray)
oldNinfo := da.Ninfos
da.Ninfos = make([]ninfo, da.Capacity)
copy(da.Ninfos, oldNinfo)
oldBlock := da.Blocks
da.Blocks = make([]block, da.Capacity>>8)
copy(da.Blocks, oldBlock)
}
da.Blocks[da.Size>>8].init()
da.Blocks[da.Size>>8].Ehead = da.Size
da.Array[da.Size] = node{-(da.Size + 255), -(da.Size + 1)}
for i := da.Size + 1; i < da.Size+255; i++ {
da.Array[i] = node{-(i - 1), -(i + 1)}
}
da.Array[da.Size+255] = node{-(da.Size + 254), -da.Size}
da.pushBlock(da.Size>>8, &da.BheadO, da.BheadO == 0)
da.Size += 256
return da.Size>>8 - 1
}
func (da *Cedar) transferBlock(bi int, headIn, headOut *int) {
da.popBlock(bi, headIn, bi == da.Blocks[bi].Next)
da.pushBlock(bi, headOut, *headOut == 0 && da.Blocks[bi].Num != 0)
}
func (da *Cedar) popEnode(base, from int, label byte) int {
e := base ^ int(label)
if base < 0 {
e = da.findPlace()
}
bi := e >> 8
n := &da.Array[e]
b := &da.Blocks[bi]
b.Num--
if b.Num == 0 {
if bi != 0 {
da.transferBlock(bi, &da.BheadC, &da.BheadF)
}
} else {
da.Array[-n.Value].Check = n.Check
da.Array[-n.Check].Value = n.Value
if e == b.Ehead {
b.Ehead = -n.Check
}
if bi != 0 && b.Num == 1 && b.Trial != da.MaxTrial {
da.transferBlock(bi, &da.BheadO, &da.BheadC)
}
}
n.Value = ValueLimit
n.Check = from
if base < 0 {
da.Array[from].Value = -(e ^ int(label)) - 1
}
return e
}
func (da *Cedar) pushEnode(e int) {
bi := e >> 8
b := &da.Blocks[bi]
b.Num++
if b.Num == 1 {
b.Ehead = e
da.Array[e] = node{-e, -e}
if bi != 0 {
da.transferBlock(bi, &da.BheadF, &da.BheadC)
}
} else {
prev := b.Ehead
next := -da.Array[prev].Check
da.Array[e] = node{-prev, -next}
da.Array[prev].Check = -e
da.Array[next].Value = -e
if b.Num == 2 || b.Trial == da.MaxTrial {
if bi != 0 {
da.transferBlock(bi, &da.BheadC, &da.BheadO)
}
}
b.Trial = 0
}
if b.Reject < da.Reject[b.Num] {
b.Reject = da.Reject[b.Num]
}
da.Ninfos[e] = ninfo{}
}
// hasChild: wherether the `from` node has children
func (da *Cedar) pushSibling(from, base int, label byte, hasChild bool) {
c := &da.Ninfos[from].Child
keepOrder := *c == 0
if da.Ordered {
keepOrder = label > *c
}
if hasChild && keepOrder {
c = &da.Ninfos[base^int(*c)].Sibling
for da.Ordered && *c != 0 && *c < label {
c = &da.Ninfos[base^int(*c)].Sibling
}
}
da.Ninfos[base^int(label)].Sibling = *c
*c = label
}
func (da *Cedar) popSibling(from, base int, label byte) {
c := &da.Ninfos[from].Child
for *c != label {
c = &da.Ninfos[base^int(*c)].Sibling
}
*c = da.Ninfos[base^int(*c)].Sibling
}
func (da *Cedar) consult(baseN, baseP int, cN, cP byte) bool {
cN = da.Ninfos[baseN^int(cN)].Sibling
cP = da.Ninfos[baseP^int(cP)].Sibling
for cN != 0 && cP != 0 {
cN = da.Ninfos[baseN^int(cN)].Sibling
cP = da.Ninfos[baseP^int(cP)].Sibling
}
return cP != 0
}
func (da *Cedar) setChild(base int, c, label byte, flag bool) []byte {
child := make([]byte, 0, 257)
if c == 0 {
child = append(child, c)
c = da.Ninfos[base^int(c)].Sibling
}
if da.Ordered {
for c != 0 && c <= label {
child = append(child, c)
c = da.Ninfos[base^int(c)].Sibling
}
}
if flag {
child = append(child, label)
}
for c != 0 {
child = append(child, c)
c = da.Ninfos[base^int(c)].Sibling
}
return child
}
func (da *Cedar) findPlace() int {
if da.BheadC != 0 {
return da.Blocks[da.BheadC].Ehead
}
if da.BheadO != 0 {
return da.Blocks[da.BheadO].Ehead
}
return da.addBlock() << 8
}
func (da *Cedar) findPlaces(child []byte) int {
bi := da.BheadO
if bi != 0 {
e := da.listBi(bi, child)
if e > 0 {
return e
}
}
return da.addBlock() << 8
}
func (da *Cedar) listBi(bi int, child []byte) int {
nc := len(child)
bz := da.Blocks[da.BheadO].Prev
for {
b := &da.Blocks[bi]
if b.Num >= nc && nc < b.Reject {
e := da.listEhead(b, child)
if e > 0 {
return e
}
}
b.Reject = nc
if b.Reject < da.Reject[b.Num] {
da.Reject[b.Num] = b.Reject
}
biN := b.Next
b.Trial++
if b.Trial == da.MaxTrial {
da.transferBlock(bi, &da.BheadO, &da.BheadC)
}
if bi == bz {
break
}
bi = biN
}
return 0
}
func (da *Cedar) listEhead(b *block, child []byte) int {
for e := b.Ehead; ; {
base := e ^ int(child[0])
for i := 0; da.Array[base^int(child[i])].Check < 0; i++ {
if i == len(child)-1 {
b.Ehead = e
// if e == 0 {
// }
return e
}
}
e = -da.Array[e].Check
if e == b.Ehead {
break
}
}
return 0
}
func (da *Cedar) resolve(fromN, baseN int, labelN byte) int {
toPn := baseN ^ int(labelN)
fromP := da.Array[toPn].Check
baseP := da.Array[fromP].base()
flag := da.consult(baseN, baseP, da.Ninfos[fromN].Child, da.Ninfos[fromP].Child)
var children []byte
if flag {
children = da.setChild(baseN, da.Ninfos[fromN].Child, labelN, true)
} else {
children = da.setChild(baseP, da.Ninfos[fromP].Child, 255, false)
}
var base int
if len(children) == 1 {
base = da.findPlace()
} else {
base = da.findPlaces(children)
}
base ^= int(children[0])
var (
from int
nbase int
)
if flag {
from = fromN
nbase = baseN
} else {
from = fromP
nbase = baseP
}
if flag && children[0] == labelN {
da.Ninfos[from].Child = labelN
}
da.Array[from].Value = -base - 1
base, labelN, toPn = da.list(base, from, nbase, fromN, toPn,
labelN, children, flag)
if flag {
return base ^ int(labelN)
}
return toPn
}
func (da *Cedar) list(base, from, nbase, fromN, toPn int,
labelN byte, children []byte, flag bool) (int, byte, int) {
for i := 0; i < len(children); i++ {
to := da.popEnode(base, from, children[i])
newTo := nbase ^ int(children[i])
if i == len(children)-1 {
da.Ninfos[to].Sibling = 0
} else {
da.Ninfos[to].Sibling = children[i+1]
}
if flag && newTo == toPn { // new node has no child
continue
}
n := &da.Array[to]
ns := &da.Array[newTo]
n.Value = ns.Value
if n.Value < 0 && children[i] != 0 {
// this node has children, fix their check
c := da.Ninfos[newTo].Child
da.Ninfos[to].Child = c
da.Array[n.base()^int(c)].Check = to
c = da.Ninfos[n.base()^int(c)].Sibling
for c != 0 {
da.Array[n.base()^int(c)].Check = to
c = da.Ninfos[n.base()^int(c)].Sibling
}
}
if !flag && newTo == fromN { // parent node moved
fromN = to
}
if !flag && newTo == toPn {
da.pushSibling(fromN, toPn^int(labelN), labelN, true)
da.Ninfos[newTo].Child = 0
ns.Value = ValueLimit
ns.Check = fromN
} else {
da.pushEnode(newTo)
}
}
return base, labelN, toPn
}