Refactor: DomainTrie use generics
This commit is contained in:
@ -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
|
||||
}
|
||||
|
Reference in New Issue
Block a user