-
Notifications
You must be signed in to change notification settings - Fork 55
/
tree_dump.go
563 lines (473 loc) · 12.8 KB
/
tree_dump.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
package art
import (
"bytes"
"fmt"
"strings"
)
const (
printValuesAsChar = 1 << iota
printValuesAsDecimal
printValuesAsHex
printValueDefault = printValuesAsChar
)
// refFormatter is a function that formats an artNodeRef.
type refFormatter func(*dumpNodeRef) string
// RefFullFormatter returns the full address of the node, including the ID and the pointer.
func RefFullFormatter(a *dumpNodeRef) string {
if a.ptr == nil {
return "-"
}
return fmt.Sprintf("#%d/%p", a.id, a.ptr)
}
// RefShortFormatter returns only the ID of the node.
func RefShortFormatter(a *dumpNodeRef) string {
if a.ptr == nil {
return "-"
}
return fmt.Sprintf("#%d", a.id)
}
// RefAddrFormatter returns only the pointer address of the node (legacy).
func RefAddrFormatter(a *dumpNodeRef) string {
if a.ptr == nil {
return "-"
}
return fmt.Sprintf("%p", a.ptr)
}
// dumpNodeRef represents the address of a nodeRef in the tree,
// composed of a unique, sequential ID and a pointer to the node.
// The ID remains consistent for trees built with the same keys
// while the pointer may change with each build.
// This structure helps identify and compare nodes across different tree instances.
// It is also helpful for debugging and testing.
//
// For example: if you inserted the same keys in two different trees (or rerun the same test),
// you can compare the nodes of the two trees by their IDs.
// The IDs will be the same for the same keys, but the pointers will be different.
type dumpNodeRef struct {
id int // unique ID
ptr *nodeRef // pointer to the node
fmt refFormatter // function to format the address
}
// String returns the string representation of the address.
func (a dumpNodeRef) String() string {
if a.fmt == nil {
return RefFullFormatter(&a)
}
return a.fmt(&a)
}
// NodeRegistry maintains a mapping between nodeRef pointers and their unique IDs.
type nodeRegistry struct {
ptrToID map[*nodeRef]int // Maps a node pointer to its unique ID
addresses []dumpNodeRef // List of node references
formatter refFormatter // Function to format node references
}
// register adds a nodeRef to the registry and returns its reference.
func (nr *nodeRegistry) register(node *nodeRef) dumpNodeRef {
// Check if the node is already registered.
if id, exists := nr.ptrToID[node]; exists {
return nr.addresses[id]
}
// Create a new reference for the node.
id := len(nr.addresses)
ref := dumpNodeRef{
id: id,
ptr: node,
fmt: nr.formatter,
}
// Register the node and its reference.
nr.ptrToID[node] = id
nr.addresses = append(nr.addresses, ref)
return ref
}
// depthStorage stores information about the depth of the tree.
type depthStorage struct {
childNum int
childrenTotal int
}
// treeStringer is a helper struct for generating a human-readable representation of the tree.
type treeStringer struct {
storage []depthStorage // Storage for depth information
buf *bytes.Buffer // Buffer for building the string representation
nodeRegistry *nodeRegistry // Registry for node references
}
// String returns the string representation of the tree.
func (ts *treeStringer) String() string {
s := ts.buf.String()
// trim trailing whitespace and newlines.
s = strings.TrimRight(s, "\n")
s = strings.TrimRight(s, " ")
return s
}
// regNode registers a nodeRef and returns its reference.
func (ts *treeStringer) regNode(node *nodeRef) dumpNodeRef {
addr := ts.nodeRegistry.register(node)
return addr
}
// regNodes registers a slice of artNodes and returns their references.
func (ts *treeStringer) regNodes(nodes []*nodeRef) []dumpNodeRef {
if nodes == nil {
return nil
}
addrs := make([]dumpNodeRef, 0, len(nodes))
for _, n := range nodes {
addrs = append(addrs, ts.nodeRegistry.register(n))
}
return addrs
}
// generatePads generates padding strings for the tree representation.
func (ts *treeStringer) generatePads(depth int, childNum int, childrenTotal int) (pad0, pad string) {
ts.storage[depth] = depthStorage{childNum, childrenTotal}
for d := 0; d <= depth; d++ {
if d < depth {
if ts.storage[d].childNum+1 < ts.storage[d].childrenTotal {
pad0 += "│ "
} else {
pad0 += " "
}
} else {
if childrenTotal == 0 {
pad0 += "─"
} else if ts.storage[d].childNum+1 < ts.storage[d].childrenTotal {
pad0 += "├"
} else {
pad0 += "└"
}
pad0 += "──"
}
}
pad0 += " "
for d := 0; d <= depth; d++ {
if childNum+1 < childrenTotal && childrenTotal > 0 {
if ts.storage[d].childNum+1 < ts.storage[d].childrenTotal {
pad += "│ "
} else {
pad += " "
}
} else if d < depth && ts.storage[d].childNum+1 < ts.storage[d].childrenTotal {
pad += "│ "
} else {
pad += " "
}
}
return
}
// append adds a string representation of a value to the buffer.
// opts is a list of options for formatting the value.
// If no options are provided, the default is to print the value as a character.
// The available options are:
// - printValuesAsChar: print values as characters
// - printValuesAsDecimal: print values as decimal numbers
// - printValuesAsHex: print values as hexadecimal numbers
func (ts *treeStringer) append(v interface{}, opts ...int) *treeStringer {
options := 0
for _, opt := range opts {
options |= opt
}
if options == 0 {
options = printValueDefault
}
switch v := v.(type) {
case string:
ts.buf.WriteString(v)
case []byte:
ts.append("[")
for i, b := range v {
if (options & printValuesAsChar) != 0 {
if b > 0 {
ts.append(fmt.Sprintf("%c", b))
} else {
ts.append("·")
}
} else if (options & printValuesAsDecimal) != 0 {
ts.append(fmt.Sprintf("%d", b))
}
if (options&printValuesAsDecimal) != 0 && i+1 < len(v) {
ts.append(" ")
}
}
ts.append("]")
case Key:
ts.append([]byte(v))
default:
ts.append("[")
ts.append(fmt.Sprintf("%#v", v))
ts.append("]")
}
return ts
}
// appendKey adds a string representation of a nodeRef's key to the buffer.
// see append for the list of available options.
func (ts *treeStringer) appendKey(keys []byte, present []byte, opts ...int) *treeStringer {
options := 0
for _, opt := range opts {
options |= opt
}
if options == 0 {
options = printValueDefault
}
ts.append("[")
for i, b := range keys {
if (options & printValuesAsChar) != 0 {
if present[i] != 0 {
ts.append(fmt.Sprintf("%c", b))
} else {
ts.append("·")
}
} else if (options & printValuesAsDecimal) != 0 {
if present[i] != 0 {
ts.append(fmt.Sprintf("%2d", b))
} else {
ts.append("·")
}
} else if (options & printValuesAsHex) != 0 {
if present[i] != 0 {
ts.append(fmt.Sprintf("%2x", b))
} else {
ts.append("·")
}
}
if (options&(printValuesAsDecimal|printValuesAsHex)) != 0 && i+1 < len(keys) {
ts.append(" ")
}
}
ts.append("]")
return ts
}
// children generates a string representation of the children of a nodeRef.
func (ts *treeStringer) children(children []*nodeRef, _ /*numChildred*/ uint16, keyOffset int, zeroChild *nodeRef) {
for i, child := range children {
ts.baseNode(child, keyOffset, i, len(children)+1)
}
ts.baseNode(zeroChild, keyOffset, len(children)+1, len(children)+1)
}
// node generates a string representation of a nodeRef.
func (ts *treeStringer) node(pad string, prefixLen uint16, prefix []byte, keys []byte, present []byte, children []*nodeRef, numChildren uint16, keyOffset int, zeroChild *nodeRef) {
if prefix != nil {
ts.append(pad).
append(fmt.Sprintf("prefix(%x): ", prefixLen)).
append(prefix).
append(" ").
append(fmt.Sprintf("%v", prefix)).
append("\n")
}
if keys != nil {
ts.append(pad).
append("keys: ").
appendKey(keys, present, printValuesAsChar).
append(" ").
appendKey(keys, present, printValuesAsDecimal).
append("\n")
}
ts.append(pad).
append(fmt.Sprintf("children(%v): %+v <%v>\n",
numChildren,
ts.regNodes(children),
ts.regNode(zeroChild)))
ts.children(children, numChildren, keyOffset+1, zeroChild)
}
func (ts *treeStringer) baseNode(an *nodeRef, depth int, childNum int, childrenTotal int) {
padHeader, pad := ts.generatePads(depth, childNum, childrenTotal)
if an == nil {
ts.append(padHeader).
append("nil").
append("\n")
return
}
ts.append(padHeader).
append(fmt.Sprintf("%v (%v)\n",
an.kind,
ts.regNode(an)))
switch an.kind {
case Node4:
nn := an.node4()
ts.node(pad,
nn.prefixLen,
nn.prefix[:],
nn.keys[:],
nn.present[:],
nn.children[:node4Max],
nn.childrenLen,
depth,
nn.children[node4Max])
case Node16:
nn := an.node16()
var present []byte
for i := 0; i < len(nn.keys); i++ {
var b byte
if nn.hasChild(i) {
b = 1
}
present = append(present, b)
}
ts.node(pad,
nn.prefixLen,
nn.prefix[:],
nn.keys[:],
present,
nn.children[:node16Max],
nn.childrenLen,
depth,
nn.children[node16Max])
case Node48:
nn := an.node48()
var present []byte
for i := 0; i < len(nn.keys); i++ {
var b byte
if nn.hasChild(i) {
b = 1
}
present = append(present, b)
}
ts.node(pad,
nn.prefixLen,
nn.prefix[:],
nn.keys[:],
present,
nn.children[:node48Max],
nn.childrenLen,
depth,
nn.children[node48Max])
case Node256:
nn := an.node256()
ts.node(pad,
nn.prefixLen,
nn.prefix[:],
nil,
nil,
nn.children[:node256Max],
nn.childrenLen,
depth,
nn.children[node256Max])
case Leaf:
n := an.leaf()
ts.append(pad).
append(fmt.Sprintf("key(%d): ", len(n.key))).
append(n.key).
append(" ").
append(fmt.Sprintf("%v", n.key)).
append("\n")
if s, ok := n.value.(string); ok {
ts.append(pad).
append(fmt.Sprintf("val: %v\n",
s))
} else if b, ok := n.value.([]byte); ok {
ts.append(pad).
append(fmt.Sprintf("val: %v\n",
string(b)))
} else {
ts.append(pad).
append(fmt.Sprintf("val: %v\n",
n.value))
}
}
ts.append(pad).
append("\n")
}
func (ts *treeStringer) startFromNode(an *nodeRef) {
ts.baseNode(an, 0, 0, 0)
}
/*
DumpNode returns Tree in the human readable format:
--8<-- // main.go
package main
import (
"fmt"
art "github.com/plar/go-adaptive-radix-tree"
)
func main() {
tree := art.New()
terms := []string{"A", "a", "aa"}
for _, term := range terms {
tree.Insert(art.Key(term), term)
}
fmt.Println(tree)
}
--8<--
$ go run main.go
─── Node4 (0xc00011c2d0)
prefix(0): [··········] [0 0 0 0 0 0 0 0 0 0]
keys: [Aa··] [65 97 · ·]
children(2): [0xc00011c2a0 0xc00011c300 - -] <->
├── Leaf (0xc00011c2a0)
│ key(1): [A] [65]
│ val: A
│
├── Node4 (0xc00011c300)
│ prefix(0): [··········] [0 0 0 0 0 0 0 0 0 0]
│ keys: [a···] [97 · · ·]
│ children(1): [0xc00011c2f0 - - -] <0xc00011c2c0>
│ ├── Leaf (0xc00011c2f0)
│ │ key(2): [aa] [97 97]
│ │ val: aa
│ │
│ ├── nil
│ ├── nil
│ ├── nil
│ └── Leaf (0xc00011c2c0)
│ key(1): [a] [97]
│ val: a
│
│
├── nil
├── nil
└── nil
*/
func DumpNode(root *nodeRef) string {
opts := createTreeStringerOptions(WithRefFormatter(RefAddrFormatter))
trs := newTreeStringer(opts)
trs.startFromNode(root)
return trs.String()
}
// treeStringerOptions contains options for DumpTree function.
type treeStringerOptions struct {
storageSize int
formatter refFormatter
}
// treeStringerOption is a function that sets an option for DumpTree.
type treeStringerOption func(opts *treeStringerOptions)
// WithStorageSize sets the size of the storage for depth information.
func WithStorageSize(size int) treeStringerOption {
return func(opts *treeStringerOptions) {
opts.storageSize = size
}
}
// WithRefFormatter sets the formatter for node references.
func WithRefFormatter(formatter refFormatter) treeStringerOption {
return func(opts *treeStringerOptions) {
opts.formatter = formatter
}
}
// TreeStringer returns the string representation of the tree.
// The tree must be of type *art.tree.
func TreeStringer(t Tree, opts ...treeStringerOption) string {
tr, ok := t.(*tree)
if !ok {
return "expected *art.tree"
}
trs := newTreeStringer(createTreeStringerOptions(opts...))
trs.startFromNode(tr.root)
return trs.String()
}
func createTreeStringerOptions(opts ...treeStringerOption) treeStringerOptions {
defOpts := treeStringerOptions{
storageSize: 4096,
formatter: RefShortFormatter,
}
for _, opt := range opts {
opt(&defOpts)
}
return defOpts
}
func newTreeStringer(opts treeStringerOptions) *treeStringer {
return &treeStringer{
storage: make([]depthStorage, opts.storageSize),
buf: bytes.NewBufferString(""),
nodeRegistry: &nodeRegistry{
ptrToID: make(map[*nodeRef]int),
formatter: opts.formatter,
},
}
}
func defaultTreeStringer() *treeStringer {
return newTreeStringer(createTreeStringerOptions())
}