nf/tree.go

82 lines
1.5 KiB
Go
Raw Normal View History

2024-01-10 20:26:19 +08:00
package nf
2024-01-11 18:16:41 +08:00
import (
"fmt"
"strings"
)
2024-01-10 20:26:19 +08:00
2024-01-11 18:16:41 +08:00
type _node struct {
pattern string
part string
children []*_node
isWild bool
2024-01-10 20:26:19 +08:00
}
2024-01-11 18:16:41 +08:00
func (n *_node) String() string {
return fmt.Sprintf("_node{pattern=%s, part=%s, isWild=%t}", n.pattern, n.part, n.isWild)
2024-01-10 20:26:19 +08:00
}
2024-01-11 18:16:41 +08:00
func (n *_node) insert(pattern string, parts []string, height int) {
2024-01-10 20:26:19 +08:00
if len(parts) == height {
n.pattern = pattern
return
}
part := parts[height]
child := n.matchChild(part)
if child == nil {
2024-01-11 18:16:41 +08:00
child = &_node{part: part, isWild: part[0] == ':' || part[0] == '*'}
2024-01-10 20:26:19 +08:00
n.children = append(n.children, child)
}
child.insert(pattern, parts, height+1)
}
2024-01-11 18:16:41 +08:00
func (n *_node) search(parts []string, height int) *_node {
2024-01-10 20:26:19 +08:00
if len(parts) == height || strings.HasPrefix(n.part, "*") {
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
}
2024-01-11 18:16:41 +08:00
func (n *_node) travel(list *([]*_node)) {
if n.pattern != "" {
*list = append(*list, n)
}
for _, child := range n.children {
child.travel(list)
}
}
func (n *_node) matchChild(part string) *_node {
for _, child := range n.children {
if child.part == part || child.isWild {
return child
}
}
return nil
}
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
}