Skip to content

Commit

Permalink
修复后缀树查找问题
Browse files Browse the repository at this point in the history
  • Loading branch information
singer233 committed Jun 26, 2024
1 parent 40d29da commit 8e6b7d3
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 34 deletions.
4 changes: 2 additions & 2 deletions src/utils/arknights_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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])
}
Expand All @@ -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)
}
Expand Down
35 changes: 5 additions & 30 deletions src/utils/suffixtree/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 12 additions & 2 deletions src/utils/suffixtree/suffixtree.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package suffixtree

import (
"fmt"
"strings"
"unicode/utf8"
)
Expand All @@ -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.
Expand Down Expand Up @@ -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

0 comments on commit 8e6b7d3

Please sign in to comment.