2019-07-14 19:29:58 +08:00
|
|
|
package trie
|
|
|
|
|
2022-11-30 18:50:46 +08:00
|
|
|
import "strings"
|
|
|
|
|
2019-07-14 19:29:58 +08:00
|
|
|
// Node is the trie's node
|
2022-11-02 22:28:18 +08:00
|
|
|
type Node[T any] struct {
|
2022-11-30 19:42:05 +08:00
|
|
|
childNode *Node[T] // optimize for only one child
|
|
|
|
childStr string
|
|
|
|
children map[string]*Node[T]
|
|
|
|
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 18:50:46 +08:00
|
|
|
if n.children == nil {
|
2022-11-30 19:42:05 +08:00
|
|
|
if n.childNode != nil && n.childStr == s {
|
|
|
|
return n.childNode
|
|
|
|
}
|
2022-11-30 18:50:46 +08:00
|
|
|
return nil
|
|
|
|
}
|
2019-07-14 19:29:58 +08:00
|
|
|
return n.children[s]
|
|
|
|
}
|
|
|
|
|
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 18:50:46 +08:00
|
|
|
if n.children == nil {
|
2022-11-30 19:42:05 +08:00
|
|
|
if n.childNode == nil {
|
|
|
|
n.childStr = s
|
|
|
|
n.childNode = child
|
|
|
|
return
|
|
|
|
}
|
2022-11-30 18:50:46 +08:00
|
|
|
n.children = map[string]*Node[T]{}
|
2022-11-30 19:42:05 +08:00
|
|
|
if n.childNode != nil {
|
|
|
|
n.children[n.childStr] = n.childNode
|
|
|
|
}
|
|
|
|
n.childStr = ""
|
|
|
|
n.childNode = nil
|
2022-11-30 18:50:46 +08:00
|
|
|
}
|
2022-11-30 19:42:05 +08:00
|
|
|
|
2019-07-14 19:29:58 +08:00
|
|
|
n.children[s] = child
|
|
|
|
}
|
|
|
|
|
2022-11-30 18:50:46 +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
|
|
|
|
}
|
|
|
|
|
2022-11-30 19:42:05 +08:00
|
|
|
func (n *Node[T]) optimize() {
|
|
|
|
if len(n.childStr) > 0 {
|
|
|
|
n.childStr = strings.Clone(n.childStr)
|
|
|
|
}
|
|
|
|
if n.childNode != nil {
|
|
|
|
n.childNode.optimize()
|
|
|
|
}
|
2022-11-30 18:50:46 +08:00
|
|
|
if n.children == nil {
|
|
|
|
return
|
|
|
|
}
|
2022-11-30 19:42:05 +08:00
|
|
|
switch len(n.children) {
|
|
|
|
case 0:
|
2022-11-30 18:50:46 +08:00
|
|
|
n.children = nil
|
2022-11-30 19:42:05 +08:00
|
|
|
return
|
|
|
|
case 1:
|
|
|
|
for key := range n.children {
|
|
|
|
n.childStr = key
|
|
|
|
n.childNode = n.children[key]
|
|
|
|
}
|
|
|
|
n.children = nil
|
|
|
|
n.optimize()
|
|
|
|
return
|
2022-11-30 18:50:46 +08:00
|
|
|
}
|
|
|
|
children := make(map[string]*Node[T], len(n.children)) // avoid map reallocate memory
|
|
|
|
for key := range n.children {
|
|
|
|
child := n.children[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)
|
|
|
|
}
|
|
|
|
children[key] = child
|
2022-11-30 19:42:05 +08:00
|
|
|
child.optimize()
|
2022-11-30 18:50:46 +08:00
|
|
|
}
|
|
|
|
n.children = children
|
|
|
|
}
|
|
|
|
|
2022-11-02 22:28:18 +08:00
|
|
|
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]) Data() T {
|
|
|
|
return n.data
|
|
|
|
}
|
|
|
|
|
|
|
|
func newNode[T any]() *Node[T] {
|
2022-11-30 19:42:05 +08:00
|
|
|
return &Node[T]{}
|
2019-07-14 19:29:58 +08:00
|
|
|
}
|