Move Trie to util package

pull/6/head
mbohlool 2016-10-03 23:19:16 -07:00
parent 049a023625
commit 221a620a14
3 changed files with 72 additions and 52 deletions

View File

@ -27,6 +27,7 @@ import (
"k8s.io/kubernetes/pkg/genericapiserver/openapi/common"
"k8s.io/kubernetes/pkg/util/json"
"k8s.io/kubernetes/pkg/util"
)
const (
@ -134,7 +135,7 @@ func (o *openAPI) buildDefinitionForType(sample interface{}) (string, error) {
// buildPaths builds OpenAPI paths using go-restful's web services.
func (o *openAPI) buildPaths() error {
pathsToIgnore := createTrie(o.config.IgnorePrefixes)
pathsToIgnore := util.CreateTrie(o.config.IgnorePrefixes)
duplicateOpId := make(map[string]bool)
// Find duplicate operation IDs.
for _, service := range o.config.WebServices {

View File

@ -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
}

70
pkg/util/trie.go Normal file
View File

@ -0,0 +1,70 @@
/*
Copyright 2016 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package util
// 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
}