fix: use same strClone function

This commit is contained in:
wwqgtxx 2022-11-30 20:38:03 +08:00
parent df8e129fc6
commit bc5ab3120f

View File

@ -4,21 +4,21 @@ import "strings"
// Node is the trie's node
type Node[T any] struct {
childMap map[string]*Node[T]
childNode *Node[T] // optimize for only one child
childStr string
children map[string]*Node[T]
inited bool
data T
}
func (n *Node[T]) getChild(s string) *Node[T] {
if n.children == nil {
if n.childMap == nil {
if n.childNode != nil && n.childStr == s {
return n.childNode
}
return nil
}
return n.children[s]
return n.childMap[s]
}
func (n *Node[T]) hasChild(s string) bool {
@ -26,21 +26,21 @@ func (n *Node[T]) hasChild(s string) bool {
}
func (n *Node[T]) addChild(s string, child *Node[T]) {
if n.children == nil {
if n.childMap == nil {
if n.childNode == nil {
n.childStr = s
n.childNode = child
return
}
n.children = map[string]*Node[T]{}
n.childMap = map[string]*Node[T]{}
if n.childNode != nil {
n.children[n.childStr] = n.childNode
n.childMap[n.childStr] = n.childNode
}
n.childStr = ""
n.childNode = nil
}
n.children[s] = child
n.childMap[s] = child
}
func (n *Node[T]) getOrNewChild(s string) *Node[T] {
@ -54,45 +54,54 @@ func (n *Node[T]) getOrNewChild(s string) *Node[T] {
func (n *Node[T]) optimize() {
if len(n.childStr) > 0 {
n.childStr = strings.Clone(n.childStr)
n.childStr = strClone(n.childStr)
}
if n.childNode != nil {
n.childNode.optimize()
}
if n.children == nil {
if n.childMap == nil {
return
}
switch len(n.children) {
switch len(n.childMap) {
case 0:
n.children = nil
n.childMap = nil
return
case 1:
for key := range n.children {
for key := range n.childMap {
n.childStr = key
n.childNode = n.children[key]
n.childNode = n.childMap[key]
}
n.children = nil
n.childMap = nil
n.optimize()
return
}
children := make(map[string]*Node[T], len(n.children)) // avoid map reallocate memory
for key := range n.children {
child := n.children[key]
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
}
switch key { // try to save string's memory
case wildcard:
key = wildcard
case dotWildcard:
key = dotWildcard
default:
key = strings.Clone(key)
}
key = strClone(key)
children[key] = child
child.optimize()
}
n.children = children
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 {