-
Notifications
You must be signed in to change notification settings - Fork 30
/
tree.go
177 lines (144 loc) · 3.44 KB
/
tree.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
package golf
import (
"fmt"
"sort"
"strings"
)
type node struct {
text string
names map[string]int
handler HandlerFunc
parent *node
colon *node
children nodes
start byte
max byte
indices []uint8
}
type nodes []*node
func (s nodes) Len() int {
return len(s)
}
func (s nodes) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s nodes) Less(i, j int) bool {
return s[i].text[0] < s[j].text[0]
}
func (n *node) matchNode(path string) (*node, int8, int) {
if path == ":" {
if n.colon == nil {
n.colon = &node{text: ":"}
}
return n.colon, 0, 0
}
for i, child := range n.children {
if child.text[0] == path[0] {
maxLength := len(child.text)
pathLength := len(path)
var pathCompare int8
if pathLength > maxLength {
pathCompare = 1
} else if pathLength < maxLength {
maxLength = pathLength
pathCompare = -1
}
for j := 0; j < maxLength; j++ {
if path[j] != child.text[j] {
ccNode := &node{text: path[0:j], children: nodes{child, &node{text: path[j:]}}}
child.text = child.text[j:]
n.children[i] = ccNode
return ccNode.children[1], 0, i
}
}
return child, pathCompare, i
}
}
return nil, 0, 0
}
func (n *node) addRoute(parts []string, names map[string]int, handler HandlerFunc) {
var (
tmpNode *node
currentNode *node
)
currentNode, result, i := n.matchNode(parts[0])
for {
if currentNode == nil {
currentNode = &node{text: parts[0]}
n.children = append(n.children, currentNode)
} else if result == 1 {
parts[0] = parts[0][len(currentNode.text):]
tmpNode, result, i = currentNode.matchNode(parts[0])
n = currentNode
currentNode = tmpNode
continue
} else if result == -1 {
tmpNode := &node{text: parts[0]}
currentNode.text = currentNode.text[len(tmpNode.text):]
tmpNode.children = nodes{currentNode}
n.children[i] = tmpNode
currentNode = tmpNode
}
break
}
if len(parts) == 1 {
currentNode.handler = handler
currentNode.names = names
return
}
currentNode.addRoute(parts[1:], names, handler)
}
func (n *node) findRoute(urlPath string) (*node, error) {
urlByte := urlPath[0]
pathLen := len(urlPath)
if urlByte >= n.start && urlByte <= n.max {
if i := n.indices[urlByte-n.start]; i != 0 {
matched := n.children[i-1]
nodeLen := len(matched.text)
if nodeLen < pathLen {
if matched.text == urlPath[:nodeLen] {
if matched, _ := matched.findRoute(urlPath[nodeLen:]); matched != nil {
return matched, nil
}
}
} else if matched.text == urlPath {
return matched, nil
}
}
}
if n.colon != nil && pathLen != 0 {
i := strings.IndexByte(urlPath, '/')
if i > 0 {
if cNode, err := n.colon.findRoute(urlPath[i:]); cNode != nil {
return cNode, err
}
} else if n.colon.handler != nil {
return n.colon, nil
}
}
return nil, fmt.Errorf("Can not find route")
}
func (n *node) optimizeRoutes() {
if len(n.children) > 0 {
sort.Sort(n.children)
for i := 0; i < len(n.indices); i++ {
n.indices[i] = 0
}
n.start = n.children[0].text[0]
n.max = n.children[len(n.children)-1].text[0]
for i := 0; i < len(n.children); i++ {
cNode := n.children[i]
cNode.parent = n
cByte := int(cNode.text[0] - n.start)
if cByte >= len(n.indices) {
n.indices = append(n.indices, make([]uint8, cByte+1-len(n.indices))...)
}
n.indices[cByte] = uint8(i + 1)
cNode.optimizeRoutes()
}
}
if n.colon != nil {
n.colon.parent = n
n.colon.optimizeRoutes()
}
}