mirror of
https://github.com/k3s-io/kubernetes.git
synced 2026-01-05 15:37:24 +00:00
Move Trie to util package
This commit is contained in:
@@ -59,54 +59,3 @@ func mapKeyFromParam(param *restful.Parameter) interface{} {
|
||||
Kind: param.Data().Kind,
|
||||
}
|
||||
}
|
||||
|
||||
// A simple trie implementation with Add an HasPrefix methods only.
|
||||
type trie struct {
|
||||
children map[byte]*trie
|
||||
wordTail bool
|
||||
}
|
||||
|
||||
func createTrie(list []string) trie {
|
||||
ret := trie{
|
||||
children: make(map[byte]*trie),
|
||||
wordTail: false,
|
||||
}
|
||||
for _, v := range list {
|
||||
ret.Add(v)
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
func (t *trie) Add(v string) {
|
||||
root := t
|
||||
for _, b := range []byte(v) {
|
||||
child, exists := root.children[b]
|
||||
if !exists {
|
||||
child = &trie{
|
||||
children: make(map[byte]*trie),
|
||||
wordTail: false,
|
||||
}
|
||||
root.children[b] = child
|
||||
}
|
||||
root = child
|
||||
}
|
||||
root.wordTail = true
|
||||
}
|
||||
|
||||
func (t *trie) HasPrefix(v string) bool {
|
||||
root := t
|
||||
if root.wordTail {
|
||||
return true
|
||||
}
|
||||
for _, b := range []byte(v) {
|
||||
child, exists := root.children[b]
|
||||
if !exists {
|
||||
return false
|
||||
}
|
||||
if child.wordTail {
|
||||
return true
|
||||
}
|
||||
root = child
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user