-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrie.go
173 lines (150 loc) · 3.65 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
package trie
import (
"fmt"
"sort"
"golang.org/x/exp/constraints"
)
// Algorithm Explanation: https://engineering.linecorp.com/ja/blog/simple-tries/
// Tree implements an immutable trie tree.
type Tree[K constraints.Ordered, V any] []node[K, V]
type node[K constraints.Ordered, V any] struct {
value V
next int
label K
match bool
leaf bool
}
type kv[K constraints.Ordered, V any] struct {
k []K
v V
}
// New builds new Tree from keys and values. The len(keys) should equal to
// len(values).
func New[K constraints.Ordered, V any](keys [][]K, values []V) Tree[K, V] {
if len(keys) != len(values) {
panic("length mismatch of keys and values")
}
kvs := make([]kv[K, V], 0, len(keys))
for i, k := range keys {
kvs = append(kvs, kv[K, V]{k, values[i]})
}
sort.Slice(kvs, func(i, j int) bool {
a, b := kvs[i].k, kvs[j].k
for i := 0; i < len(a) && i < len(b); i++ {
if a[i] == b[i] {
continue
}
return a[i] < b[i]
}
if len(a) == len(b) {
panic(fmt.Sprintf("2 same key is passed: %v", kvs[i].k))
}
return len(a) < len(b)
})
t := Tree[K, V]{node[K, V]{next: 1}}
t = t.construct(kvs, 0, 0)
return t
}
func (t Tree[K, V]) construct(kvs []kv[K, V], depth, current int) Tree[K, V] {
if depth == len(kvs[0].k) {
t[current].match = true
t[current].value = kvs[0].v
kvs = kvs[1:]
if len(kvs) == 0 {
t[current].leaf = true
return t
}
}
p := []int{0}
for i := 0; i < len(kvs); {
t = append(t, node[K, V]{
label: kvs[i].k[depth],
})
for c := kvs[i].k[depth]; i < len(kvs) && kvs[i].k[depth] == c; i++ {
}
p = append(p, i)
}
for i := 0; i < len(p)-1; i++ {
t[t.nextOf(current)+i].next = len(t) - t.nextOf(current) - i
t = t.construct(kvs[p[i]:p[i+1]], depth+1, t.nextOf(current)+i)
}
return t
}
// Trace returns the subtree of t whose root is the node traced from the root
// of t by path. It doesn't modify t itself, but returns the subtree.
func (t Tree[K, V]) Trace(path []K) Tree[K, V] {
if t == nil {
return nil
}
var u int
for _, c := range path {
if t[u].leaf {
return nil
}
u = t.nextOf(u)
v := t.nextOf(u)
if v-u > 40 {
// Binary Search
u += sort.Search(v-u, func(m int) bool {
return t[u+m].label >= c
})
} else {
// Linear Search
for ; u != v-1 && t[u].label < c; u++ {
}
}
if u > v || t[u].label != c {
return nil
}
}
return t[u:]
}
// TraceOne is shorthand for Trace([]K{c}).
func (t Tree[K, V]) TraceOne(c K) Tree[K, V] {
return t.Trace([]K{c})
}
// Terminal returns the value of the root of t. The second return value
// indicates whether the node has a value; if it is false, the first return
// value is zero value.
func (t Tree[K, V]) Terminal() (V, bool) {
var zero V
if len(t) == 0 {
return zero, false
}
return t[0].value, t[0].match
}
// Predict returns the all values in the tree, t. The complexity is proportional
// to the number of nodes in t(it's not equal to len(t)).
func (t Tree[K, V]) Predict() []V {
if len(t) == 0 || t[0].leaf {
return nil
}
// Search linearly all of the child.
var end int
for !t[end].leaf {
end = t.nextOf(t.nextOf(end)) - 1
}
var values []V
for i := t.nextOf(0); i <= end; i++ {
if t[i].match {
values = append(values, t[i].value)
}
}
return values
}
// Children returns the bytes of all direct children of the root of t. The result
// is sorted in ascending order.
func (t Tree[K, V]) Children() []K {
if len(t) == 0 || t[0].leaf {
return nil
}
var children []K
for _, c := range t[t.nextOf(0):t.nextOf(t.nextOf(0))] {
children = append(children, c.label)
}
return children
}
// nextOf returns the index of the next node of t[i].
func (t Tree[K, V]) nextOf(i int) int {
return i + t[i].next
}