diff --git a/pkg/application/arch/tequila/incl_viz.go b/pkg/application/arch/tequila/incl_viz.go index 6f6d1c82ff547bea6287ebb5e11d690e0cb8c353..30e995a402be6dc4611296eceb5cf7ce7a664936 100644 --- a/pkg/application/arch/tequila/incl_viz.go +++ b/pkg/application/arch/tequila/incl_viz.go @@ -2,7 +2,6 @@ package tequila import ( "github.com/awalterschulze/gographviz" - "github.com/phodal/coca/pkg/application/arch/tequila/trie" "sort" "strconv" "strings" @@ -159,7 +158,7 @@ func (fullGraph *FullGraph) ToMapDot(include func(string) bool) *gographviz.Grap return dot } -func (fullGraph *FullGraph) MapToGraph(trie *trie.PathTrie) *gographviz.Graph { +func (fullGraph *FullGraph) MapToGraph(trie *PathTrie) *gographviz.Graph { graph := gographviz.NewGraph() _ = graph.SetName("G") @@ -187,7 +186,7 @@ func (fullGraph *FullGraph) MapToGraph(trie *trie.PathTrie) *gographviz.Graph { return graph } -func (fullGraph *FullGraph) buildGraphNode(subgraph string, current *trie.PathTrie, graph *gographviz.Graph, nodes map[string]string, s string) { +func (fullGraph *FullGraph) buildGraphNode(subgraph string, current *PathTrie, graph *gographviz.Graph, nodes map[string]string, s string) { if s != "" { s = s + "." + current.Value } else { @@ -209,8 +208,8 @@ func (fullGraph *FullGraph) buildGraphNode(subgraph string, current *trie.PathTr } } -func (fullGraph *FullGraph) BuildMapTree(include func(key string) bool) *trie.PathTrie { - pkgTrie := trie.NewPathTrie() +func (fullGraph *FullGraph) BuildMapTree(include func(key string) bool) *PathTrie { + pkgTrie := NewPathTrie() for nodeKey := range fullGraph.NodeList { if include(nodeKey) || include(fullGraph.NodeList[nodeKey]) { pkgTrie.Put(strings.ReplaceAll(nodeKey, ".", "/")) diff --git a/pkg/application/arch/tequila/incl_viz_test.go b/pkg/application/arch/tequila/incl_viz_test.go index 32404a46f3be6a53529ec6d723e83c405c912341..551f6b6cdde806429c31537558e8af810834fe54 100644 --- a/pkg/application/arch/tequila/incl_viz_test.go +++ b/pkg/application/arch/tequila/incl_viz_test.go @@ -3,11 +3,10 @@ package tequila import ( . "github.com/onsi/gomega" "github.com/phodal/coca/cmd/cmd_util" - "github.com/phodal/coca/pkg/application/arch/tequila/trie" "testing" ) -func createBasicMap() (*trie.PathTrie, *FullGraph) { +func createBasicMap() (*PathTrie, *FullGraph) { fullGraph, nodeFilter := createGraph() node := fullGraph.BuildMapTree(nodeFilter) diff --git a/pkg/application/arch/tequila/trie/path_trie.go b/pkg/application/arch/tequila/path_trie.go similarity index 72% rename from pkg/application/arch/tequila/trie/path_trie.go rename to pkg/application/arch/tequila/path_trie.go index b10d77386e84888136bf6afe1c883d346f25d618..40c64411449caaba698d41d7bb0057058c8e4fe0 100644 --- a/pkg/application/arch/tequila/trie/path_trie.go +++ b/pkg/application/arch/tequila/path_trie.go @@ -21,40 +21,35 @@ //OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN //THE SOFTWARE. -package trie +package tequila import "strings" +type StringSegmenter func(key string, start int) (segment string, nextIndex int) + +func PathSegmenter(path string, start int) (segment string, next int) { + if len(path) == 0 || start < 0 || start > len(path)-1 { + return "", -1 + } + end := strings.IndexRune(path[start+1:], '/') // next '/' after 0th rune + if end == -1 { + return path[start:], -1 + } + return path[start : start+end+1], start + end + 1 +} + type PathTrie struct { - segmenter StringSegmenter // key segmenter, must not cause heap allocs + segmenter StringSegmenter Value string Children map[string]*PathTrie } -// PathTrieConfig for building a path trie with different segmenter -type PathTrieConfig struct { - Segmenter StringSegmenter -} - -// NewPathTrie allocates and returns a new *PathTrie. func NewPathTrie() *PathTrie { return &PathTrie{ segmenter: PathSegmenter, } } -// NewPathTrieWithConfig allocates and returns a new *PathTrie with the given *PathTrieConfig -func NewPathTrieWithConfig(config *PathTrieConfig) *PathTrie { - segmenter := PathSegmenter - if config != nil && config.Segmenter != nil { - segmenter = config.Segmenter - } - - return &PathTrie{ - segmenter: segmenter, - } -} - func (trie *PathTrie) Put(key string) { node := trie for part, i := trie.segmenter(key, 0); part != ""; part, i = trie.segmenter(key, i) { @@ -70,7 +65,4 @@ func (trie *PathTrie) Put(key string) { child.Value = strings.ReplaceAll(part, "/", "") node = child } - // does node have an existing Value? - //isNewVal := node.Value == nil - //return isNewVal } diff --git a/pkg/application/arch/tequila/trie/common.go b/pkg/application/arch/tequila/trie/common.go deleted file mode 100644 index d0715043862484a5401b211fb754a995c2a17012..0000000000000000000000000000000000000000 --- a/pkg/application/arch/tequila/trie/common.go +++ /dev/null @@ -1,53 +0,0 @@ -// https://github.com/dghubble/trie -//The MIT License (MIT) -// -//Copyright (c) 2014 Dalton Hubble -// -//Permission is hereby granted, free of charge, to any person obtaining a copy -//of this software and associated documentation files (the "Software"), to deal -//in the Software without restriction, including without limitation the rights -//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -//copies of the Software, and to permit persons to whom the Software is -//furnished to do so, subject to the following conditions: -// -//The above copyright notice and this permission notice shall be included in -//all copies or substantial portions of the Software. -// -//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -//THE SOFTWARE. - -package trie - -import ( - "strings" -) - -// WalkFunc defines some action to take on the given key and Value during -// a Trie Walk. Returning a non-nil error will terminate the Walk. -type WalkFunc func(key string, value interface{}) error - -// StringSegmenter takes a string key with a starting index and returns -// the first segment after the start and the ending index. When the end is -// reached, the returned nextIndex should be -1. -// Implementations should NOT allocate heap memory as Trie Segmenters are -// called upon Gets. See PathSegmenter. -type StringSegmenter func(key string, start int) (segment string, nextIndex int) - -// PathSegmenter segments string key paths by slash separators. For example, -// "/a/b/c" -> ("/a", 2), ("/b", 4), ("/c", -1) in successive calls. It does -// not allocate any heap memory. -func PathSegmenter(path string, start int) (segment string, next int) { - if len(path) == 0 || start < 0 || start > len(path)-1 { - return "", -1 - } - end := strings.IndexRune(path[start+1:], '/') // next '/' after 0th rune - if end == -1 { - return path[start:], -1 - } - return path[start : start+end+1], start + end + 1 -}