-
Notifications
You must be signed in to change notification settings - Fork 26
/
node.go
85 lines (71 loc) · 1.5 KB
/
node.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
package fibtree
import (
"github.com/usnistgov/ndn-dpdk/container/fib/fibdef"
"github.com/usnistgov/ndn-dpdk/ndn"
)
type component struct {
typ uint16
value string
}
func (c component) NameComponent() (nc ndn.NameComponent) {
nc.Type = uint32(c.typ)
nc.Value = []byte(c.value)
return
}
type node struct {
fibdef.EntryBody
height int
parent *node
comp component
children map[component]*node
}
func (n *node) AddChild(comp component, child *node) {
child.comp = comp
child.parent = n
n.children[comp] = child
}
func (n *node) RemoveChild(child *node) {
delete(n.children, child.comp)
child.parent = nil
}
func (n *node) UpdateHeight() {
h := -1
for _, child := range n.children {
h = max(h, child.height)
}
n.height = h + 1
}
func (n *node) Name() (name ndn.Name) {
if n.parent == nil {
return make(ndn.Name, 0, n.height)
}
return append(n.parent.Name(), n.comp.NameComponent())
}
func (n *node) IsEntry() bool {
return len(n.Nexthops) > 0
}
func (n *node) AppendListTo(parentName ndn.Name, list *[]fibdef.Entry) {
var name ndn.Name
if n.parent == nil {
name = ndn.Name{}
} else {
name = make(ndn.Name, len(parentName)+1)
copy(name, parentName)
name[len(parentName)] = n.comp.NameComponent()
}
if n.IsEntry() {
entry := fibdef.Entry{
EntryBody: n.EntryBody,
Name: name,
}
*list = append(*list, entry)
}
for _, child := range n.children {
child.AppendListTo(name, list)
}
}
func newNode() *node {
return &node{
children: map[component]*node{},
}
}