-
Notifications
You must be signed in to change notification settings - Fork 0
/
trie.go
266 lines (234 loc) · 5.46 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// Package router trie based on a 256-way trie expressed on the textbook
// Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne
// A string symbol table for extended ASCII strings, implemented
// using a 256-way trie.
// http://algs4.cs.princeton.edu/code/edu/princeton/cs/algs4/TrieST.java.html
//
package router
import "bytes"
// R extended ASCII
const R = 256
//
// TRIE
//
// trie struct
type trie struct {
// root of trie
root *node
// number of keys in trie
n int
}
// newTrie creates new instace trie
func newTrie() *trie {
return &trie{root: newNode()}
}
// Get returns the value associated with the given key
func (t *trie) Get(key string) *data {
n := get(t.root, key, 0)
if n == nil {
return nil
}
return n.data
}
// get
func get(n *node, key string, index int) *node {
if n == nil {
// n = newNode()
return nil
}
if index == len(key) {
return n
}
ascii := key[index]
return get(n.next[ascii], key, index+1)
}
// Contains verify if a key exists in the Trie
func (t *trie) Contains(key string) bool {
return t.Get(key).value != ""
}
// Put Inserts the key-value pair into the symbol table, overwriting the old value
// with the new value if the key is already in the symbol table.
// If the value is nil, this effectively deletes the key from the symbol table.
func (t *trie) Put(key string, data *data) {
if data.value == "" {
t.Remove(key)
}
var ok bool
_, ok = put(t.root, key, data, 0)
if ok {
t.n++
}
}
// put
func put(n *node, key string, data *data, index int) (*node, bool) {
if n == nil {
n = newNode()
}
var ok bool
if index == len(key) {
if n.data == nil {
ok = true
}
n.data = data
return n, ok
}
ascii := key[index]
n.next[ascii], ok = put(n.next[ascii], key, data, index+1)
return n, ok
}
// Remove the key from the trie if present
func (t *trie) Remove(key string) {
t.root = t.remove(t.root, key, 0)
}
func (t *trie) remove(n *node, key string, d int) *node {
if n == nil {
return nil
}
if d == len(key) {
if n.data.value != "" {
t.n--
}
n.data.value = ""
//n.data = &data{}
} else {
c := key[d]
n.next[c] = t.remove(n.next[c], key, d+1)
}
// remove sub-trie rooted at n if its completely empty
//if !reflect.DeepEqual(n.data, &data{}) {
if n.data.value != "" {
return n
}
for i := 0; i < R; i++ {
if n.next[i] != nil {
return n
}
}
return nil
}
// Size returns the number of key-value pairs in this trie
func (t *trie) Size() int {
return t.n
}
// IsEmpty returns the number of key-value pairs in this trie
func (t *trie) IsEmpty() bool {
return t.n == 0
}
// Keys returns all keys in the trie
func (t *trie) Keys() []string {
return (t.KeysWithPrefix(""))
}
// KeysWithPrefix returns all keys in the trie
// that start with a prefix
func (t *trie) KeysWithPrefix(prefix string) []string {
n := get(t.root, prefix, 0)
results := &[]string{}
collectKeysWithPrefix(n, prefix, results)
return *results
}
func collectKeysWithPrefix(n *node, prefix string, results *[]string) {
if n == nil {
return
}
if n.data != nil {
*results = append(*results, prefix)
}
for c := 0; c < R; c++ {
buffer := bytes.NewBufferString(prefix)
buffer.WriteByte(byte(c))
collectKeysWithPrefix(n.next[c], buffer.String(), results)
}
}
// KeysThatMatch returns all keys in the trie that match a pattern
// where . symbol is treated as a wildcard character
func (t *trie) KeysThatMatch(pattern string) []string {
results := &[]string{}
collectKeysThatMatch(t.root, "", pattern, results)
return *results
}
func collectKeysThatMatch(n *node, prefix, pattern string, results *[]string) {
if n == nil {
return
}
if len(prefix) == len(pattern) && n.data != nil {
*results = append(*results, prefix)
}
if len(prefix) == len(pattern) {
return
}
p := pattern[len(prefix)]
if p == '.' {
for c := 0; c < R; c++ {
buffer := bytes.NewBufferString(prefix)
buffer.WriteByte(byte(c))
collectKeysThatMatch(n.next[c], buffer.String(), pattern, results)
}
} else {
buffer := bytes.NewBufferString(prefix)
buffer.WriteByte(byte(p))
collectKeysThatMatch(n.next[p], buffer.String(), pattern, results)
}
}
// LongestPrefixOf returns the string that is the longest prefix
// or null, if no such string
func (t *trie) LongestPrefixOf(query string) string {
length := longestPrefixOf(t.root, query, 0, -1)
if length == -1 {
return ""
}
return query[0:length]
}
// longestPrefixOf returns the length of the longest string key in the subtrie
// rooted at x that is a prefix of the query string,
// assuming the first d character match and we have already
// found a prefix match of given length (-1 if no such match)
func longestPrefixOf(n *node, query string, d, length int) int {
if n == nil {
return length
}
if n.data != nil {
length = d
}
if d == len(query) {
return length
}
c := query[d]
return longestPrefixOf(n.next[c], query, d+1, length)
}
//
// NODE
//
// node a representation of each trie node
type node struct {
data *data
next [R]*node
}
// NewNode creates new instace node
func newNode() *node {
return &node{}
}
//
// DATA
//
// Data a data struct that each node can handle
type data struct {
value string
prefix string
vars []string
methods map[string]HandlerFunc
}
// newData returns a new data instance
func newData() *data {
return &data{
vars: []string{},
methods: make(map[string]HandlerFunc),
}
}
// GetValue returns data value
func (d *data) Value() string {
return d.value
}
// SetValue sets data value
func (d *data) SetValue(val string) {
d.value = val
}