-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathradix.go
169 lines (156 loc) · 3.01 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
package radix
type Trie struct {
root *node
dump bool
}
type noneType int
const vNONE noneType = 0
type node struct {
child []*node
childidx []byte // first byte of each child
s string
v interface{}
}
// NewTrie creates a new trie.
func NewTrie(dump bool) *Trie {
return &Trie{
dump: dump,
}
}
func dump(s string) string {
ss := make([]byte, len(s))
copy(ss, s)
return string(ss)
}
func (t *Trie) newSubTree(s string, v interface{}) *node {
n := &node{
v: v,
}
t.setS(n, s)
return n
}
func (t *Trie) setS(n *node, s string) {
if t.dump {
n.s = dump(s)
} else {
n.s = s
}
}
func (n *node) setV(v interface{}) (ov interface{}, ok bool) {
ov = n.v
if _, ok = ov.(noneType); ok {
ov, ok = nil, false
}
n.v = v
return
}
// Add inserts a key-value pair into trie. If there is an old value for the
// key, old value will be returned and 'has' will be true.
func (t *Trie) Add(s string, v interface{}) (ov interface{}, has bool) {
if s == "" {
return
}
if t.root == nil {
t.root = t.newSubTree(s, v)
return
}
n := t.root
INSERT:
for {
var l int
min := len(s)
if min > len(n.s) {
min = len(n.s)
}
for ; l < min; l++ {
if s[l] != n.s[l] {
break
}
}
switch {
case l == len(n.s): // totally match this node
s = s[l:]
if len(s) == 0 { // end
return n.setV(v)
}
first := 0
if len(s[first:]) > 0 {
for i := 0; i < len(n.childidx); i++ {
if n.childidx[i] == s[first] {
n = n.child[i]
continue INSERT
}
}
}
default: // split
prefix, suffix := n.s[:l], n.s[l:]
child := &node{
child: n.child,
childidx: n.childidx,
v: n.v,
}
t.setS(child, suffix)
*n = node{}
t.setS(n, prefix)
n.child = []*node{child}
n.childidx = []byte{child.s[0]}
n.v = vNONE
s = s[l:]
if len(s) == 0 { // end
return n.setV(v)
}
}
// construct a new subtree using rest of pattern and
// append it to the child list of this node
child := t.newSubTree(s, v)
n.child = append(n.child, child)
n.childidx = append(n.childidx, child.s[0])
return
}
}
// Lookup searchs the trie to find an exact match and returns the
// associated value. If not found, ok will be false.
func (t *Trie) Lookup(s string) (v interface{}, ok bool) {
n := t.root.lookup(s)
if n == nil {
v, ok = nil, false
} else {
v, ok = n.v, true
if _, ok = v.(noneType); ok {
v, ok = nil, false
}
}
return
}
func (n *node) lookup(s string) *node {
if n == nil {
return nil
}
minLen := len(s)
if minLen > len(n.s) {
minLen = len(n.s)
}
var l int // length of longest common prefix
for l = 0; l < minLen && s[l] == n.s[l]; l++ {
} // at the end of loop: pattern[:l] == n.s[:l]
switch l {
case len(n.s): // totally match this node
s = s[l:]
if len(s) == 0 { // end
return n
}
// go down
var k int
for k = 0; k < len(n.childidx); k++ {
if n.childidx[k] == s[0] {
if end := n.child[k].lookup(s); end != nil {
return end
}
break
}
}
fallthrough
default:
return nil
}
}