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 20:38:03 +08:00
|
|
|
childMap map[string]*Node[T]
|
2022-11-30 19:42:05 +08:00
|
|
|
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 {
|
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
|
|
|
|
}
|
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 {
|
2022-11-30 19:42:05 +08:00
|
|
|
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]{}
|
2022-11-30 19:42:05 +08:00
|
|
|
if n.childNode != nil {
|
2022-11-30 20:38:03 +08:00
|
|
|
n.childMap[n.childStr] = n.childNode
|
2022-11-30 19:42:05 +08:00
|
|
|
}
|
|
|
|
n.childStr = ""
|
|
|
|
n.childNode = nil
|
2022-11-30 18:50:46 +08:00
|
|
|
}
|
2022-11-30 19:42:05 +08:00
|
|
|
|
2022-11-30 20:38:03 +08:00
|
|
|
n.childMap[s] = child
|
2019-07-14 19:29:58 +08:00
|
|
|
}
|
|
|
|
|
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 {
|
2022-11-30 20:38:03 +08:00
|
|
|
n.childStr = strClone(n.childStr)
|
2022-11-30 19:42:05 +08:00
|
|
|
}
|
|
|
|
if n.childNode != nil {
|
|
|
|
n.childNode.optimize()
|
|
|
|
}
|
2022-11-30 20:38:03 +08:00
|
|
|
if n.childMap == nil {
|
2022-11-30 18:50:46 +08:00
|
|
|
return
|
|
|
|
}
|
2022-11-30 20:38:03 +08:00
|
|
|
switch len(n.childMap) {
|
2022-11-30 19:42:05 +08:00
|
|
|
case 0:
|
2022-11-30 20:38:03 +08:00
|
|
|
n.childMap = nil
|
2022-11-30 19:42:05 +08:00
|
|
|
return
|
|
|
|
case 1:
|
2022-11-30 20:38:03 +08:00
|
|
|
for key := range n.childMap {
|
2022-11-30 19:42:05 +08:00
|
|
|
n.childStr = key
|
2022-11-30 20:38:03 +08:00
|
|
|
n.childNode = n.childMap[key]
|
2022-11-30 19:42:05 +08:00
|
|
|
}
|
2022-11-30 20:38:03 +08:00
|
|
|
n.childMap = nil
|
2022-11-30 19:42:05 +08:00
|
|
|
n.optimize()
|
|
|
|
return
|
2022-11-30 18:50:46 +08:00
|
|
|
}
|
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]
|
2022-11-30 18:50:46 +08:00
|
|
|
if child == nil {
|
|
|
|
continue
|
|
|
|
}
|
2022-11-30 20:38:03 +08:00
|
|
|
key = strClone(key)
|
2022-11-30 18:50:46 +08:00
|
|
|
children[key] = child
|
2022-11-30 19:42:05 +08:00
|
|
|
child.optimize()
|
2022-11-30 18:50:46 +08:00
|
|
|
}
|
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
|
2022-11-30 18:50:46 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-04-01 11:53:39 +08:00
|
|
|
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
|
|
|
|
}
|
2022-11-02 22:28:18 +08:00
|
|
|
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
|
|
|
}
|