-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from IJNKAWAKAZE/dev
使用后缀树来提高inline模式干员和敌人的查询效率 O(n^2) -> O(n)
- Loading branch information
Showing
7 changed files
with
538 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from https://github.com/ljfuyuan/suffixtree modified to make the type is usable |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package suffixtree | ||
|
||
type edge struct { | ||
label []rune | ||
*node | ||
} | ||
|
||
func newEdge(label []rune, node *node) *edge { | ||
return &edge{label: label, node: node} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package suffixtree | ||
|
||
import ( | ||
"sort" | ||
) | ||
|
||
type node struct { | ||
/* | ||
* The payload array used to store the data (indexes) associated with this node. | ||
* In this case, it is used to store all property indexes. | ||
*/ | ||
data []int | ||
/** | ||
* The set of edges starting from this node | ||
*/ | ||
edges []*edge | ||
/** | ||
* The suffix link as described in Ukkonen's paper. | ||
* if str is the string denoted by the path from the root to this, this.suffix | ||
* is the node denoted by the path that corresponds to str without the first rune. | ||
*/ | ||
suffix *node | ||
} | ||
|
||
/* | ||
* getData returns the first numElements elements from the ones associated to this node. | ||
* | ||
* Gets data from the payload of both this node and its children, the string representation | ||
* of the path to this node is a substring of the one of the children nodes. | ||
* | ||
* @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() (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() | ||
ret = append(ret, data...) | ||
|
||
} | ||
return | ||
} | ||
|
||
// addRef adds the given index to the set of indexes associated with this | ||
func (n *node) addRef(index int) { | ||
if n.contains(index) { | ||
return | ||
} | ||
n.addIndex(index) | ||
// add this reference to all the suffixes as well | ||
iter := n.suffix | ||
for iter != nil { | ||
if iter.contains(index) { | ||
break | ||
} | ||
iter.addRef(index) | ||
iter = iter.suffix | ||
} | ||
} | ||
|
||
func (n *node) contains(index int) bool { | ||
i := sort.SearchInts(n.data, index) | ||
return i < len(n.data) && n.data[i] == index | ||
} | ||
|
||
func (n *node) addEdge(r rune, e *edge) { | ||
if idx := n.search(r); idx == -1 { | ||
n.edges = append(n.edges, e) | ||
sort.Slice(n.edges, func(i, j int) bool { return n.edges[i].label[0] < n.edges[j].label[0] }) | ||
} else { | ||
n.edges[idx] = e | ||
} | ||
|
||
} | ||
|
||
func (n *node) getEdge(r rune) *edge { | ||
idx := n.search(r) | ||
if idx < 0 { | ||
return nil | ||
} | ||
return n.edges[idx] | ||
} | ||
|
||
func (n *node) search(r rune) int { | ||
idx := sort.Search(len(n.edges), func(i int) bool { return n.edges[i].label[0] >= r }) | ||
if idx < len(n.edges) && n.edges[idx].label[0] == r { | ||
return idx | ||
} | ||
|
||
return -1 | ||
} | ||
|
||
func (n *node) addIndex(idx int) { | ||
n.data = append(n.data, idx) | ||
} | ||
|
||
func newNode() *node { | ||
return &node{} | ||
} |
Oops, something went wrong.