-
Notifications
You must be signed in to change notification settings - Fork 0
/
trie.go
70 lines (63 loc) · 1.89 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
package axisapi
import "strings"
// node using prefix tree to match routes
type node struct {
pattern string //route to be matched(要匹配的目标路由)
part string //part of route(路由的一部分)
children []*node //children node
isWild bool // if part is contain * or :that is universal matching(泛匹配)
}
// 获取第一个成功匹配的节点 get the first successful matched node
func (n *node) matchChild(part string) *node {
for _, child := range n.children {
if child.part == part || child.isWild {
return child
}
}
return nil
}
// 获取所有匹配成功的节点 get the all successful matched node
func (n *node) matchChildren(part string) []*node {
nodes := make([]*node, 0)
for _, child := range n.children {
if child.part == part || child.isWild {
nodes = append(nodes, child)
}
}
return nodes
}
// 构造动态路由 Constructing dynamic routes
func (n *node) insert(pattern string, parts []string, height int) {
// 只有匹配到最终点时pattern才会赋值,祖先节点如果不是路由规则则都是""
if len(parts) == height {
n.pattern = pattern
return
}
part := parts[height]
// 深度一样而且同名或者泛匹配的不生成新节点
child := n.matchChild(part)
if child == nil {
child = &node{part: part, isWild: part[0] == ':' || part[0] == '*'}
n.children = append(n.children, child)
}
child.insert(pattern, parts, height+1)
}
func (n *node) search(parts []string, height int) *node {
if len(parts) == height || strings.HasPrefix(n.part, "*") {
// 证明没有以parts[height]结束的路由规则
if n.pattern == "" {
return nil
}
return n
}
part := parts[height]
// 获取前缀树当前一层所有符合条件的孩子节点
children := n.matchChildren(part)
for _, child := range children {
result := child.search(parts, height+1)
if result != nil {
return result
}
}
return nil
}