Commit 221a620a authored by mbohlool's avatar mbohlool

Move Trie to util package

parent 049a0236
...@@ -27,6 +27,7 @@ import ( ...@@ -27,6 +27,7 @@ import (
"k8s.io/kubernetes/pkg/genericapiserver/openapi/common" "k8s.io/kubernetes/pkg/genericapiserver/openapi/common"
"k8s.io/kubernetes/pkg/util/json" "k8s.io/kubernetes/pkg/util/json"
"k8s.io/kubernetes/pkg/util"
) )
const ( const (
...@@ -134,7 +135,7 @@ func (o *openAPI) buildDefinitionForType(sample interface{}) (string, error) { ...@@ -134,7 +135,7 @@ func (o *openAPI) buildDefinitionForType(sample interface{}) (string, error) {
// buildPaths builds OpenAPI paths using go-restful's web services. // buildPaths builds OpenAPI paths using go-restful's web services.
func (o *openAPI) buildPaths() error { func (o *openAPI) buildPaths() error {
pathsToIgnore := createTrie(o.config.IgnorePrefixes) pathsToIgnore := util.CreateTrie(o.config.IgnorePrefixes)
duplicateOpId := make(map[string]bool) duplicateOpId := make(map[string]bool)
// Find duplicate operation IDs. // Find duplicate operation IDs.
for _, service := range o.config.WebServices { for _, service := range o.config.WebServices {
......
...@@ -59,54 +59,3 @@ func mapKeyFromParam(param *restful.Parameter) interface{} { ...@@ -59,54 +59,3 @@ func mapKeyFromParam(param *restful.Parameter) interface{} {
Kind: param.Data().Kind, 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
}
/*
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
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment