mihomo/component/trie/node.go

138 lines
2.4 KiB
Go
Raw Permalink Normal View History

2019-07-14 19:29:58 +08:00
package trie
import "strings"
2019-07-14 19:29:58 +08:00
// Node is the trie's node
type Node[T any] struct {
2022-11-30 20:38:03 +08:00
childMap map[string]*Node[T]
childNode *Node[T] // optimize for only one child
childStr string
inited bool
data T
2019-07-14 19:29:58 +08:00
}
2022-04-06 04:25:53 +08:00
func (n *Node[T]) getChild(s string) *Node[T] {
2022-11-30 20:38:03 +08:00
if n.childMap == nil {
if n.childNode != nil && n.childStr == s {
return n.childNode
}
return nil
}
2022-11-30 20:38:03 +08:00
return n.childMap[s]
2019-07-14 19:29:58 +08:00
}
2022-04-06 04:25:53 +08:00
func (n *Node[T]) hasChild(s string) bool {
2019-07-14 19:29:58 +08:00
return n.getChild(s) != nil
}
2022-04-06 04:25:53 +08:00
func (n *Node[T]) addChild(s string, child *Node[T]) {
2022-11-30 20:38:03 +08:00
if n.childMap == nil {
if n.childNode == nil {
n.childStr = s
n.childNode = child
return
}
2022-11-30 20:38:03 +08:00
n.childMap = map[string]*Node[T]{}
if n.childNode != nil {
2022-11-30 20:38:03 +08:00
n.childMap[n.childStr] = n.childNode
}
n.childStr = ""
n.childNode = nil
}
2022-11-30 20:38:03 +08:00
n.childMap[s] = child
2019-07-14 19:29:58 +08:00
}
func (n *Node[T]) getOrNewChild(s string) *Node[T] {
node := n.getChild(s)
if node == nil {
node = newNode[T]()
n.addChild(s, node)
}
return node
}
func (n *Node[T]) optimize() {
if len(n.childStr) > 0 {
2022-11-30 20:38:03 +08:00
n.childStr = strClone(n.childStr)
}
if n.childNode != nil {
n.childNode.optimize()
}
2022-11-30 20:38:03 +08:00
if n.childMap == nil {
return
}
2022-11-30 20:38:03 +08:00
switch len(n.childMap) {
case 0:
2022-11-30 20:38:03 +08:00
n.childMap = nil
return
case 1:
2022-11-30 20:38:03 +08:00
for key := range n.childMap {
n.childStr = key
2022-11-30 20:38:03 +08:00
n.childNode = n.childMap[key]
}
2022-11-30 20:38:03 +08:00
n.childMap = nil
n.optimize()
return
}
2022-11-30 20:38:03 +08:00
children := make(map[string]*Node[T], len(n.childMap)) // avoid map reallocate memory
for key := range n.childMap {
child := n.childMap[key]
if child == nil {
continue
}
2022-11-30 20:38:03 +08:00
key = strClone(key)
children[key] = child
child.optimize()
}
2022-11-30 20:38:03 +08:00
n.childMap = children
}
func strClone(key string) string {
switch key { // try to save string's memory
case wildcard:
key = wildcard
case dotWildcard:
key = dotWildcard
case complexWildcard:
key = complexWildcard
case domainStep:
key = domainStep
default:
key = strings.Clone(key)
}
return key
}
func (n *Node[T]) isEmpty() bool {
if n == nil || n.inited == false {
return true
}
return false
}
func (n *Node[T]) setData(data T) {
n.data = data
n.inited = true
}
func (n *Node[T]) getChildren() map[string]*Node[T] {
if n.childMap == nil {
if n.childNode != nil {
m := make(map[string]*Node[T])
m[n.childStr] = n.childNode
return m
}
} else {
return n.childMap
}
return nil
}
func (n *Node[T]) Data() T {
return n.data
}
func newNode[T any]() *Node[T] {
return &Node[T]{}
2019-07-14 19:29:58 +08:00
}