mihomo/component/trie/node.go

32 lines
536 B
Go
Raw Normal View History

2019-07-14 19:29:58 +08:00
package trie
// Node is the trie's node
2022-04-06 04:25:53 +08:00
type Node[T comparable] struct {
children map[string]*Node[T]
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] {
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]) {
2019-07-14 19:29:58 +08:00
n.children[s] = child
}
2022-04-06 04:25:53 +08:00
func newNode[T comparable](data T) *Node[T] {
return &Node[T]{
2019-07-14 19:29:58 +08:00
Data: data,
2022-04-06 04:25:53 +08:00
children: map[string]*Node[T]{},
2019-07-14 19:29:58 +08:00
}
}
2022-04-06 04:25:53 +08:00
func getZero[T comparable]() T {
var result T
return result
}