-
Notifications
You must be signed in to change notification settings - Fork 2
/
trie.go
226 lines (172 loc) · 3.55 KB
/
trie.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
package trie
import (
"bytes"
"cmp"
"fmt"
"io"
"slices"
"strings"
)
const rootNode = rune(0)
// Trie represents generic prefix-tree.
type Trie[T any] struct {
root *node[T]
}
// New creates empty Trie.
func New[T any]() *Trie[T] {
return &Trie[T]{
root: makeNode[T](rootNode),
}
}
// Add inserts new key/value pair.
func (t *Trie[T]) Add(key string, value T) {
n := t.root
for _, r := range key {
if c, ok := n.GetChild(r); ok {
n = c
continue
}
n = n.AddChild(r)
}
n.SetValue(value)
}
func (t *Trie[T]) find(key string) (n *node[T], ok bool) {
n = t.root
for _, r := range key {
if n, ok = n.GetChild(r); ok {
continue
}
return
}
return n, true
}
// Del removes node by key.
func (t *Trie[T]) Del(key string) (ok bool) {
var n, p *node[T]
if n, ok = t.find(key); !ok {
return
}
for ; n != nil; n = p {
if p = n.Parent(); p == nil {
break
}
if n.HasValue() {
n.DropValue()
}
if n.HasChildren() {
break
}
p.DelChild(n.key)
}
return true
}
// Find does tree-lookup in order to find value associated with given key.
func (t *Trie[T]) Find(key string) (value T, ok bool) {
var n *node[T]
if n, ok = t.find(key); !ok {
return
}
return n.Value(), n.HasValue()
}
// Suggest returns slice of existing keys with matching prefix.
func (t *Trie[T]) Suggest(prefix string) (rv []string, ok bool) {
add := func(v string, _ T) {
rv = append(rv, v)
}
t.Iter(prefix, add)
return rv, len(rv) > 0
}
// Iter iterates over trie by prefix using dfs.
// Pass prefix="" to iterate over whole trie.
func (t *Trie[T]) Iter(prefix string, walker func(key string, value T)) {
n, ok := t.find(prefix)
if !ok {
return
}
if n.HasValue() {
walker(prefix, n.value)
}
dfsValues(n, prefix, walker)
}
// Common returns slice of common keys with at least `minLength` of their length
// Pass prefix="" to find all commons whithin given length
// Resulting slice is sorted by overall matching keys count, key with most goes first.
func (t *Trie[T]) Common(prefix string, minLength int) (rv []string) {
n, ok := t.find(prefix)
if !ok {
return
}
minLength -= len(prefix)
dfsKeys(n, prefix, func(k string, n *node[T]) bool {
if len(k) < minLength {
return true
}
if n.HasValue() || len(n.childs) > 1 {
rv = append(rv, k)
return false
}
return true
})
slices.SortFunc(rv, func(a, b string) int {
sa, _ := t.Suggest(a)
sb, _ := t.Suggest(b)
return cmp.Compare(len(sb), len(sa))
})
return rv
}
// String implements fmt.Stringer interface.
func (t *Trie[T]) String() string {
var b bytes.Buffer
writeNode(&b, t.root, 0)
return b.String()
}
func writeNode[T any](
w io.Writer,
n *node[T],
level int,
) {
template := "key: '%c'"
if level > 0 {
template = strings.Repeat("\t", level) + template
}
switch {
case n.HasValue():
_, _ = fmt.Fprintf(w, template+" value: '%v'", n.key, n.Value())
case n.key == rootNode:
_, _ = fmt.Fprint(w, "root")
default:
_, _ = fmt.Fprintf(w, template+":", n.key)
}
_, _ = fmt.Fprintln(w)
for _, c := range n.childs {
writeNode(w, c, level+1)
}
}
func dfsValues[T any](
n *node[T],
prefix string,
handler func(key string, value T),
) {
for _, r := range n.order {
c := n.childs[r]
key := prefix + string(r)
if c.HasValue() {
handler(key, c.value)
}
dfsValues(c, key, handler)
}
}
func dfsKeys[T any](
n *node[T],
prefix string,
handler func(string, *node[T]) bool,
) {
for _, r := range n.order {
c := n.childs[r]
key := prefix + string(r)
if !handler(key, c) {
continue
}
dfsKeys(c, key, handler)
}
}