-
Notifications
You must be signed in to change notification settings - Fork 2
/
cedar.go
620 lines (517 loc) · 15 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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
// Copyright 2016 Evans. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package cedar
// NInfo stores the information about the trie
type NInfo struct {
sibling, child byte // uint8
}
// Node contains the array of `base` and `check` as specified in the paper:
// "An efficient implementation of trie structures"
// https://dl.acm.org/citation.cfm?id=146691
type Node struct {
baseV, check int // int32
}
func (n *Node) base(reduced ...bool) int {
if !isReduced(reduced...) {
return n.baseV
}
return -(n.baseV + 1)
}
// Block stores the linked-list pointers and the stats info for blocks.
//
// Because of type conversion, this version all int16 and int32 uses int,
// witch will be optimized in the next version.
type Block struct {
prev int // int32 // previous block's index, 3 bytes width
next int // next block's index, 3 bytes width
num int // the number of slots that is free, the range is 0-256
reject int // a heuristic number to make the search for free space faster...
trial int // the number of times this block has been probed by `find_places` for the free block.
eHead int // the index of the first empty elemenet in this block
}
func (b *Block) init() {
b.num = 256 // each of block has 256 free slots at the beginning
b.reject = 257 // initially every block need to be fully iterated through so that we can reject it to be unusable.
}
// Cedar holds all of the information about double array trie.
type Cedar struct {
// Reduced option the reduced trie
Reduced bool
array []Node // storing the `base` and `check` info from the original paper.
nInfos []NInfo
blocks []Block
reject [257]int
blocksHeadFull int // the index of the first 'Full' block, 0 means no 'Full' block
blocksHeadClosed int // the index of the first 'Closed' block, 0 means no ' Closed' block
blocksHeadOpen int // the index of the first 'Open' block, 0 means no 'Open' block
capacity int
size int
ordered bool
maxTrial int // the parameter for cedar, it could be tuned for more, but the default is 1.
}
const (
// ValLimit cedar value limit
ValLimit = int(^uint(0) >> 1)
// NoVal not have value
NoVal = -1
)
// type PrefixIter struct {
// }
// New initialize the Cedar for further use
func New(reduced ...bool) *Cedar {
cd := Cedar{
Reduced: isReduced(reduced...),
array: make([]Node, 256),
nInfos: make([]NInfo, 256),
blocks: make([]Block, 1),
capacity: 256,
size: 256,
ordered: true,
maxTrial: 1,
}
if !cd.Reduced {
cd.array[0] = Node{baseV: 0, check: -1}
} else {
cd.array[0] = Node{baseV: -1, check: -1}
}
// make `baseV` point to the previous element, and make `check` point to the next element
for i := 1; i < 256; i++ {
cd.array[i] = Node{baseV: -(i - 1), check: -(i + 1)}
}
// make them link as a cyclic doubly-linked list
cd.array[1].baseV = -255
cd.array[255].check = -1
cd.blocks[0].eHead = 1
cd.blocks[0].init()
for i := 0; i <= 256; i++ {
cd.reject[i] = i + 1
}
return &cd
}
// follow To move in the trie by following the `label`, and insert the node if the node is not there,
// it is used by the `update` to populate the trie.
func (cd *Cedar) follow(from int, label byte) (to int) {
base := cd.array[from].base(cd.Reduced)
// the node is not there
to = base ^ int(label)
if base < 0 || cd.array[to].check < 0 {
// allocate a e node
to = cd.popENode(base, from, label)
branch := to ^ int(label)
// maintain the info in ninfo
cd.pushSibling(from, branch, label, base >= 0)
return
}
// the node is already there and the ownership is not `from`,
// therefore a conflict.
if cd.array[to].check != from {
// call `resolve` to relocate.
to = cd.resolve(from, base, label)
}
return
}
// Mark an edge `e` as used in a trie node.
// pop empty node from block; never transfer the special block (idx = 0)
func (cd *Cedar) popENode(base, from int, label byte) int {
e := base ^ int(label)
if base < 0 {
e = cd.findPlace()
}
idx := e >> 8
arr := &cd.array[e]
b := &cd.blocks[idx]
b.num--
// move the block at idx to the correct linked-list depending the free slots it still have.
if b.num == 0 {
if idx != 0 {
// Closed to Full
cd.transferBlock(idx, &cd.blocksHeadClosed, &cd.blocksHeadFull)
}
} else {
// release empty node from empty ring
cd.array[-arr.baseV].check = arr.check
cd.array[-arr.check].baseV = arr.baseV
if e == b.eHead {
b.eHead = -arr.check
}
if idx != 0 && b.num == 1 && b.trial != cd.maxTrial {
// Open to Closed
cd.transferBlock(idx, &cd.blocksHeadOpen, &cd.blocksHeadClosed)
}
}
// initialize the released node
if !cd.Reduced {
if label != 0 {
cd.array[e].baseV = -1
} else {
cd.array[e].baseV = 0
}
cd.array[e].check = from
if base < 0 {
cd.array[from].baseV = e ^ int(label)
}
return e
}
cd.array[e].baseV = ValLimit
cd.array[e].check = from
if base < 0 {
cd.array[from].baseV = -(e ^ int(label)) - 1
}
return e
}
// Mark an edge `e` as free in a trie node.
// push empty node into empty ring
func (cd *Cedar) pushENode(e int) {
idx := e >> 8
b := &cd.blocks[idx]
b.num++
if b.num == 1 {
b.eHead = e
cd.array[e] = Node{baseV: -e, check: -e}
if idx != 0 {
// Move the block from 'Full' to 'Closed' since it has one free slot now.
cd.transferBlock(idx, &cd.blocksHeadFull, &cd.blocksHeadClosed)
}
} else {
prev := b.eHead
next := -cd.array[prev].check
// Insert to the edge immediately after the e_head
cd.array[e] = Node{baseV: -prev, check: -next}
cd.array[prev].check = -e
cd.array[next].baseV = -e
// Move the block from 'Closed' to 'Open' since it has more than one free slot now.
if b.num == 2 || b.trial == cd.maxTrial {
if idx != 0 {
// Closed to Open
cd.transferBlock(idx, &cd.blocksHeadClosed, &cd.blocksHeadOpen)
}
}
// Reset the trial stats
b.trial = 0
}
if b.reject < cd.reject[b.num] {
b.reject = cd.reject[b.num]
}
// reset ninfo; no child, no sibling
cd.nInfos[e] = NInfo{}
}
// push the `label` into the sibling chain
// to from's child
func (cd *Cedar) pushSibling(from, base int, label byte, hasChild bool) {
c := &cd.nInfos[from].child
keepOrder := *c == 0
if cd.ordered {
keepOrder = label > *c
}
if hasChild && keepOrder {
c = &cd.nInfos[base^int(*c)].sibling
for cd.ordered && *c != 0 && *c < label {
c = &cd.nInfos[base^int(*c)].sibling
}
// for {
// c = &cd.nInfos[base^int(*c)].sibling
// if cd.ordered && *c != 0 && *c < label {
// break
// }
// }
}
cd.nInfos[base^int(label)].sibling = *c
*c = label
}
// remove the `label` from the sibling chain.
func (cd *Cedar) popSibling(from, base int, label byte) {
c := &cd.nInfos[from].child
for *c != label {
c = &cd.nInfos[base^int(*c)].sibling
}
*c = cd.nInfos[base^int(*c)].sibling
}
// Loop through the siblings to see which one reached the end first, which means
// it is the one with smaller in children size, and we should try ti relocate the smaller one.
// check whether to replace branching w/ the newly added node
func (cd *Cedar) consult(baseN, baseP int, cN, cP byte) bool {
cN = cd.nInfos[baseN^int(cN)].sibling
cP = cd.nInfos[baseP^int(cP)].sibling
for cN != 0 && cP != 0 {
cN = cd.nInfos[baseN^int(cN)].sibling
cP = cd.nInfos[baseP^int(cP)].sibling
}
return cP != 0
}
// Collect the list of the children, and push the label as well if it is not terminal node.
// enumerate (equal to or more than one) child nodes
func (cd *Cedar) setChild(base int, c, label byte, flag bool) []byte {
child := make([]byte, 0, 257)
// 0: terminal
if c == 0 {
child = append(child, c)
c = cd.nInfos[base^int(c)].sibling
}
if cd.ordered {
for c != 0 && c <= label {
child = append(child, c)
c = cd.nInfos[base^int(c)].sibling
}
}
if flag {
child = append(child, label)
}
for c != 0 {
child = append(child, c)
c = cd.nInfos[base^int(c)].sibling
}
return child
}
// For the case where only one free slot is needed
func (cd *Cedar) findPlace() int {
if cd.blocksHeadClosed != 0 {
return cd.blocks[cd.blocksHeadClosed].eHead
}
if cd.blocksHeadOpen != 0 {
return cd.blocks[cd.blocksHeadOpen].eHead
}
// the block is not enough, resize it and allocate it.
return cd.addBlock() << 8
}
// For the case where multiple free slots are needed.
func (cd *Cedar) findPlaces(child []byte) int {
idx := cd.blocksHeadOpen
// still have available 'Open' blocks.
if idx != 0 {
e := cd.listIdx(idx, child)
if e > 0 {
return e
}
}
return cd.addBlock() << 8
}
func (cd *Cedar) listIdx(idx int, child []byte) int {
n := len(child)
bo := cd.blocks[cd.blocksHeadOpen].prev
// only proceed if the free slots are more than the number of children. Also, we
// save the minimal number of attempts to fail in the `reject`, it only worths to
// try out this block if the number of children is less than that number.
for {
b := &cd.blocks[idx]
if b.num >= n && n < b.reject {
e := cd.listEHead(b, child)
if e > 0 {
return e
}
}
// we broke out of the loop, that means we failed. We save the information in
// `reject` for future pruning.
b.reject = n
if b.reject < cd.reject[b.num] {
// put this stats into the global array of information as well.
cd.reject[b.num] = b.reject
}
idxN := b.next
b.trial++
// move this block to the 'Closed' block list since it has reached the max_trial
if b.trial == cd.maxTrial {
cd.transferBlock(idx, &cd.blocksHeadOpen, &cd.blocksHeadClosed)
}
// we have finsihed one round of this cyclic doubly-linked-list.
if idx == bo {
break
}
// going to the next in this linked list group
idx = idxN
}
return 0
}
func (cd *Cedar) listEHead(b *Block, child []byte) int {
for e := b.eHead; ; {
base := e ^ int(child[0])
// iterate through the children to see if they are available: (check < 0)
for i := 0; cd.array[base^int(child[i])].check < 0; i++ {
if i == len(child)-1 {
// we have found the available block.
b.eHead = e
return e
}
}
// save the next free block's information in `check`
e = -cd.array[e].check
if e == b.eHead {
break
}
}
return 0
}
// resolve the conflict by moving one of the the nodes to a free block.
// resolve conflict on base_n ^ label_n = base_p ^ label_p
func (cd *Cedar) resolve(fromN, baseN int, labelN byte) int {
toPn := baseN ^ int(labelN)
// the `base` and `from` for the conflicting one.
fromP := cd.array[toPn].check
baseP := cd.array[fromP].base(cd.Reduced)
// whether to replace siblings of newly added
flag := cd.consult(
baseN, baseP,
cd.nInfos[fromN].child,
cd.nInfos[fromP].child,
)
// collect the list of children for the block that we are going to relocate.
var children []byte
if flag {
children = cd.setChild(baseN, cd.nInfos[fromN].child, labelN, true)
} else {
children = cd.setChild(baseP, cd.nInfos[fromP].child, 255, false)
}
// decide which algorithm to allocate free block depending on the number of children
// we have.
base := 0
if len(children) == 1 {
base = cd.findPlace()
} else {
base = cd.findPlaces(children)
}
base ^= int(children[0])
var from, nbase int
if flag {
from = fromN
nbase = baseN
} else {
from = fromP
nbase = baseP
}
if flag && children[0] == labelN {
cd.nInfos[from].child = labelN
}
// #[cfg(feature != "reduced-trie")]
if !cd.Reduced {
cd.array[from].baseV = base
} else {
cd.array[from].baseV = -base - 1
}
base, labelN, toPn = cd.listN(base, from, nbase, fromN, toPn,
labelN, children, flag)
// return the position that is free now.
if flag {
return base ^ int(labelN)
}
return toPn
}
func (cd *Cedar) listN(base, from, nbase, fromN, toPn int,
labelN byte, children []byte, flag bool) (int, byte, int) {
// the actual work for relocating the chilren
for i := 0; i < len(children); i++ {
to := cd.popENode(base, from, children[i])
newTo := nbase ^ int(children[i])
if i == len(children)-1 {
cd.nInfos[to].sibling = 0
} else {
cd.nInfos[to].sibling = children[i+1]
}
// new node has no children
if flag && newTo == toPn {
continue
}
arr := &cd.array[to]
arrs := &cd.array[newTo]
arr.baseV = arrs.baseV
condition := false
if !cd.Reduced {
condition = arr.baseV > 0 && children[i] != 0
} else {
condition = arr.baseV < 0 && children[i] != 0
}
if condition {
// this node has children, fix their check
c := cd.nInfos[newTo].child
cd.nInfos[to].child = c
cd.array[arr.base(cd.Reduced)^int(c)].check = to
c = cd.nInfos[arr.base(cd.Reduced)^int(c)].sibling
for c != 0 {
cd.array[arr.base(cd.Reduced)^int(c)].check = to
c = cd.nInfos[arr.base(cd.Reduced)^int(c)].sibling
}
}
// the parent node is moved
if !flag && newTo == fromN {
fromN = to
}
// clean up the space that was moved away from.
if !flag && newTo == toPn {
cd.pushSibling(fromN, toPn^int(labelN), labelN, true)
cd.nInfos[newTo].child = 0
if !cd.Reduced {
if labelN != 0 {
arrs.baseV = -1
} else {
arrs.baseV = 0
}
} else {
arrs.baseV = ValLimit
}
arrs.check = fromN
} else {
cd.pushENode(newTo)
}
}
return base, labelN, toPn
}
// pop a block at idx from the linked-list of type `from`, specially handled if it is the last
// one in the linked-list.
func (cd *Cedar) popBlock(idx int, from *int, last bool) {
if last {
*from = 0
return
}
b := &cd.blocks[idx]
cd.blocks[b.prev].next = b.next
cd.blocks[b.next].prev = b.prev
if idx == *from {
*from = b.next
}
}
// return the block at idx to the linked-list of `to`, specially handled
// if the linked-list is empty
func (cd *Cedar) pushBlock(idx int, to *int, empty bool) {
b := &cd.blocks[idx]
if empty {
*to, b.prev, b.next = idx, idx, idx
return
}
tailTo := &cd.blocks[*to].prev
b.prev = *tailTo
b.next = *to
*to, *tailTo, cd.blocks[*tailTo].next = idx, idx, idx
}
// Reallocate more spaces so that we have more free blocks.
func (cd *Cedar) addBlock() int {
if cd.size == cd.capacity {
cd.capacity += cd.capacity
array := cd.array
cd.array = make([]Node, cd.capacity)
copy(cd.array, array)
nInfos := cd.nInfos
cd.nInfos = make([]NInfo, cd.capacity)
copy(cd.nInfos, nInfos)
blocks := cd.blocks
cd.blocks = make([]Block, cd.capacity>>8)
copy(cd.blocks, blocks)
}
cd.blocks[cd.size>>8].init()
cd.blocks[cd.size>>8].eHead = cd.size
// make it a doubley linked list
cd.array[cd.size] = Node{baseV: -(cd.size + 255), check: -(cd.size + 1)}
for i := cd.size + 1; i < cd.size+255; i++ {
cd.array[i] = Node{baseV: -(i - 1), check: -(i + 1)}
}
cd.array[cd.size+255] = Node{baseV: -(cd.size + 254), check: -cd.size}
// append to block Open
cd.pushBlock(cd.size>>8, &cd.blocksHeadOpen, cd.blocksHeadOpen == 0)
cd.size += 256
return cd.size>>8 - 1
}
// transfer the block at idx from the linked-list of `from` to the linked-list of `to`,
// specially handle the case where the destination linked-list is empty.
func (cd *Cedar) transferBlock(idx int, from, to *int) {
b := cd.blocks[idx]
cd.popBlock(idx, from, idx == b.next) // b.next it's the last one if the next points to itself
cd.pushBlock(idx, to, *to == 0 && b.num != 0)
}