diff --git a/src/utils/arknights_utils.go b/src/utils/arknights_utils.go index e8f9d69..061f174 100644 --- a/src/utils/arknights_utils.go +++ b/src/utils/arknights_utils.go @@ -140,7 +140,7 @@ func GetOperatorByName(name string) Operator { func GetOperatorsByName(name string) []Operator { updateData() var operatorList []Operator - for _, op := range operatorTree.Search(strings.ToLower(name), 0) { + for _, op := range operatorTree.Search(strings.ToLower(name)) { print(operators[op].Name) operatorList = append(operatorList, operators[op]) } @@ -156,7 +156,7 @@ func GetRecruitOperatorList() []Operator { func GetEnemiesByName(name string) map[string]string { updateData() var enemyMap = make(map[string]string) - for _, index := range enemyTree.Search(strings.ToLower(name), 0) { + for _, index := range enemyTree.Search(strings.ToLower(name)) { a := enemyArray[index] enemyMap[a.a.(string)] = a.b.(string) } diff --git a/src/utils/suffixtree/node.go b/src/utils/suffixtree/node.go index a7cdf5e..047d19c 100644 --- a/src/utils/suffixtree/node.go +++ b/src/utils/suffixtree/node.go @@ -31,40 +31,15 @@ type node struct { * @param numElements the number of results to return. Use <=0 to get all * @return the first numElements associated to this node and children */ -func (n *node) getData(numElements int) (ret []int) { - - if numElements > 0 { - if numElements > len(n.data) { - numElements -= len(n.data) - ret = n.data - } else { - ret = n.data[:numElements] - return - } - } else { - ret = n.data - } +func (n *node) getData() (ret []int) { + + ret = n.data // need to get more matches from child nodes. This is what may waste time for _, edge := range n.edges { - data := edge.node.getData(numElements) - NEXTIDX: - for _, idx := range data { - for _, v := range ret { - if v == idx { - continue NEXTIDX - } - } - - if numElements > 0 { - numElements-- - } - ret = append(ret, idx) - } + data := edge.node.getData() + ret = append(ret, data...) - if numElements == 0 { - break - } } return diff --git a/src/utils/suffixtree/suffixtree.go b/src/utils/suffixtree/suffixtree.go index 0e8be8c..80771c9 100644 --- a/src/utils/suffixtree/suffixtree.go +++ b/src/utils/suffixtree/suffixtree.go @@ -2,6 +2,7 @@ package suffixtree import ( + "fmt" "strings" "unicode/utf8" ) @@ -13,12 +14,12 @@ type generalizedSuffixTree struct { // Search search for the given word within the GST and returns at most the given number of matches. // numElments <= 0 get all matches -func (t *generalizedSuffixTree) Search(word string, numElements int) []int { +func (t *generalizedSuffixTree) Search(word string) []int { node := t.searchNode(word) if node == nil { return nil } - return node.getData(numElements) + return node.getData() } // searchNode returns the tree node (if present) that corresponds to the given string. @@ -282,5 +283,14 @@ func NewGeneralizedSuffixTree() GST { t.activeLeaf = t.root return t } +func PrintTree(n GST) { + printnode("\t", n.root) +} +func printnode(flag string, n *node) { + for _, e := range n.edges { + fmt.Printf("%s %s %v \n", flag, string(e.label), e.node.data) + printnode(flag+"\t-", e.node) + } +} type GST = *generalizedSuffixTree