-
Notifications
You must be signed in to change notification settings - Fork 0
/
radix.go
170 lines (156 loc) · 3.8 KB
/
radix.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
package radix
import (
"sync"
)
// TODO: All non-edge nodes technically are associated or are the root of a
// subtree.
// TODO: Need a solid way to build a tree from any node
// TODO: Should probably have longest branch length
//LongestBranch int
type Tree struct {
mutex *sync.RWMutex
Root *Node
Children []*Node
Height int // Or MaxDepth
NodeCount int // Total
}
// TODO: Would prefer a proper build tree from node function
func New() *Tree {
root := &Node{
Type: Branch,
Depth: -1,
Children: []*Node{},
}
return &Tree{
Root: root,
mutex: new(sync.RWMutex),
Children: root.Children,
Height: 0,
NodeCount: 0,
}
}
func (self *Tree) Keys() (keys []string) {
for _, child := range self.Children {
keys = append(keys, child.Key)
}
return append(keys, self.Root.Key)
}
func (self *Tree) NodesAtDepth(depth int) []*Node {
nodes := []*Node{}
for _, node := range self.Children {
if node.Depth == depth {
nodes = append(nodes, node)
}
}
return nodes
}
func (self *Tree) KeyExists(key string) bool {
for _, child := range self.Children {
if child.Key == key {
return true
}
}
return false
}
func (self *Tree) Add(key string, value interface{}) *Node {
if len(key) == 0 {
return &Node{}
}
self.mutex.Lock()
defer self.mutex.Unlock()
node := self.Root.add(key, value)
if !self.KeyExists(key) && node != nil {
if len(node.Children) == 0 {
node.Type = Edge
if self.Height < node.Depth {
self.Height = node.Depth
}
} else {
for _, child := range node.Children {
child.Depth = child.Parent.Depth + 1
}
}
// NOTE: Cache
self.Children = append(self.Children, node)
return node
} else {
return nil
}
}
// TODO: Remaining issues:
// 1) Doesn't properly set depth in complex edge cases
func (self *Node) add(key string, value interface{}) *Node {
if len(self.Children) != 0 {
for _, child := range self.Children {
commonPrefixIndex := longestCommonPrefixIndex(child.Key, key)
if commonPrefixIndex == -1 {
continue
} else if commonPrefixIndex > 0 {
if len(child.Key) > commonPrefixIndex {
child.SplitKeyAtIndex(commonPrefixIndex)
} else if len(child.Key) == commonPrefixIndex {
return child.add(key[commonPrefixIndex:], value)
}
if len(key) == commonPrefixIndex {
child.Value = value
return child
} else {
return child.AddChild(key[commonPrefixIndex:], value)
}
}
}
}
if self.Parent != nil {
self.Depth = self.Parent.Depth + 1
}
return self.AddChild(key, value)
}
func (self *Tree) AddEdge(node *Node) {
self.Children = append(self.Children, node)
}
// TODO: This is a gross function, needs recursive
func (self *Tree) AddBranch(child *Node) (subtree *Tree) {
subtree = self.AddBranch(child)
for _, subchild := range child.Children {
if subchild.Type == Edge {
subtree.AddEdge(subchild)
} else {
subtree = subtree.AddBranch(subchild)
for _, subchild := range subchild.Children {
if subchild.Type == Edge {
subtree.AddEdge(subchild)
} else {
subtree = subtree.AddBranch(subchild)
for _, subchild := range subchild.Children {
if subchild.Type == Edge {
subtree.AddEdge(subchild)
} else {
subtree.AddBranch(subchild)
}
}
}
}
}
}
return subtree
}
func (self *Node) IsBranch() bool { return len(self.Children) != 0 }
func (self *Node) IsEdge() bool { return len(self.Children) == 0 }
// TODO: Need to actually write a recursive function, this was just used for
// development.
func (self *Tree) String() string {
tree := &Tree{}
for _, node := range self.Root.Children {
if node.IsBranch() {
tree = tree.AddBranch(node)
for _, child := range node.Children {
if child.IsEdge() {
tree.AddEdge(child)
}
}
} else {
tree.AddEdge(node)
}
}
return tree.String()
}