Refactor: DomainTrie use generics

This commit is contained in:
yaling888
2022-04-06 04:25:53 +08:00
committed by Meta
parent 0c65f6962a
commit a6eb11ce18
10 changed files with 100 additions and 68 deletions

View File

@ -1,34 +1,31 @@
package trie
// Node is the trie's node
type Node struct {
children map[string]*Node
Data any
type Node[T comparable] struct {
children map[string]*Node[T]
Data T
}
func (n *Node) getChild(s string) *Node {
if n.children == nil {
return nil
}
func (n *Node[T]) getChild(s string) *Node[T] {
return n.children[s]
}
func (n *Node) hasChild(s string) bool {
func (n *Node[T]) hasChild(s string) bool {
return n.getChild(s) != nil
}
func (n *Node) addChild(s string, child *Node) {
if n.children == nil {
n.children = map[string]*Node{}
}
func (n *Node[T]) addChild(s string, child *Node[T]) {
n.children[s] = child
}
func newNode(data any) *Node {
return &Node{
func newNode[T comparable](data T) *Node[T] {
return &Node[T]{
Data: data,
children: nil,
children: map[string]*Node[T]{},
}
}
func getZero[T comparable]() T {
var result T
return result
}