-
Notifications
You must be signed in to change notification settings - Fork 0
/
trie.go
85 lines (77 loc) · 1.21 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
package main
const eLen int = 4
const eLst int = eLen - 1
const wc rune = '`'
type node struct {
c []*node
w []string
}
func newN() *node {
return &node{c: make([]*node, 26)}
}
func newT() *node {
ret := newN()
for _, w := range rawin {
ret.insert(w, 0)
}
return ret
}
func (n *node) insert(w string, l int) {
if l == eLen {
n.w = append(n.w, w)
return
}
i := w[l] - 'a'
child := n.c[i]
if child == nil {
if l == eLst {
child = &node{}
} else {
child = newN()
}
n.c[i] = child
}
child.insert(w, l+1)
}
func (n *node) kj() []string {
if n == nil {
return nil
}
if len(n.w) != 0 {
return n.w
} else {
var ret []string
for _, c := range n.c {
if c != nil {
ret = append(ret, c.kj()...)
}
}
return ret
}
}
func searchT0(n *node, r rune) []*node {
if r == wc {
return n.c
}
return []*node{n.c[r-'a']}
}
func searchT(n []*node, r rune) []*node {
var ret []*node
if r == wc {
for _, e := range n {
if e != nil {
ret = append(ret, e.c...)
// when n is small, faster than
// `for ec in e.c if ec != nil`
}
}
} else {
idx := r - 'a'
for _, e := range n {
if e != nil && e.c[idx] != nil {
ret = append(ret, e.c[idx])
}
}
}
return ret
}