-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathradix.go
177 lines (130 loc) · 2.98 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
171
172
173
174
175
176
177
package unimorph
import (
"fmt"
"github.com/jonasknobloch/jinn/pkg/tree"
"os"
"path/filepath"
"strings"
)
type Radix tree.Tree
func NewRadix() *Radix {
return (*Radix)(&tree.Tree{
Label: "ROOT", // empty word
Children: []*tree.Tree{},
})
}
func (r *Radix) Insert(s string) []int {
rs := []rune(s + "$")
return insert((*tree.Tree)(r), rs, make([]int, len(rs)))
}
func (r *Radix) Split(s string, f func(t *tree.Tree) bool) []string {
rs := []rune(s + "$")
result := make([]string, 0)
for _, rs := range split((*tree.Tree)(r), rs, make([]rune, 0), make([][]rune, 0), f) {
result = append(result, string(rs))
}
return result
}
func insert(t *tree.Tree, rs []rune, p []int) []int {
if len(rs) == 0 {
return p
}
for i, c := range t.Children {
if c.Label == string(rs[0]) {
return insert(c, rs[1:], append(p, i))
}
}
if t.Children == nil {
t.Children = make([]*tree.Tree, 0, 1)
}
t.Children = append(t.Children, &tree.Tree{Label: string(rs[0])})
return insert(t, rs, p)
}
func split(t *tree.Tree, rs []rune, s []rune, r [][]rune, f func(t *tree.Tree) bool) [][]rune {
if len(rs) == 1 {
if rs[0] != '$' {
panic("invalid end of word")
}
ok := false
for _, c := range t.Children {
if c.Label == "$" {
ok = true
break
}
}
if !ok {
panic("unexpected end of word")
}
if len(s) != 0 {
r = append(r, s)
}
return r
}
if len(rs) > 1 && len(t.Children) == 0 {
panic("unexpected end of path")
}
for _, c := range t.Children {
if c.Label == string(rs[0]) {
s = append(s, rs[0])
if f(c) {
r = append(r, s) // add substring to result
s = make([]rune, 0) // reset substring
}
return split(c, rs[1:], s, r, f)
}
}
return split(t, rs, s, r, f)
}
func (r *Radix) Compress() {
t := (*tree.Tree)(r)
if len(t.Children) == 0 {
return
}
compress(t.Children[0])
}
func compress(t *tree.Tree) {
if len(t.Children) == 0 {
return
}
if len(t.Children) == 1 {
if t.Children[0].Label == "$" {
return
}
t.Label = string(append([]rune(t.Label), []rune(t.Children[0].Label)[0]))
t.Children = t.Children[0].Children
compress(t)
}
for _, c := range t.Children {
compress(c)
}
}
func (r *Radix) Draw(stubs ...string) (int, error) {
t := (*tree.Tree)(r)
name := fmt.Sprintf("radix_%s.dot", strings.Join(stubs, "-"))
f, err := os.Create(filepath.Join(name))
if err != nil {
return 0, err
}
defer f.Close()
sb := strings.Builder{}
sb.WriteString("digraph D {\n")
sb.WriteString(" node [shape=record]\n")
t.Walk(func(st *tree.Tree) {
sb.WriteString(fmt.Sprintf(" PTR%p", st))
sb.WriteString(" [")
if len(st.Children) > 0 {
sb.WriteString("color=blue label=\"")
} else {
sb.WriteString("color=red label=\"")
}
sb.WriteString(fmt.Sprintf("%s ", st.Label))
sb.WriteString("\"]\n")
})
for k, cs := range t.Edges() {
for _, c := range cs {
sb.WriteString(fmt.Sprintf(" PTR%p -> PTR%p\n", k, c))
}
}
sb.WriteString("}\n")
return f.WriteString(sb.String())
}