2019-08-18 19:46:12 +00:00
|
|
|
package tried
|
|
|
|
|
2019-08-19 17:39:14 +00:00
|
|
|
import "github.com/davecgh/go-spew/spew"
|
|
|
|
|
2019-08-19 07:24:22 +00:00
|
|
|
// func (ts TriedString) WordIndex(idx uint) uint {
|
|
|
|
// w := ts[idx]
|
|
|
|
// if w >= 'a' && w <= 'z' {
|
|
|
|
// return uint(w) - 'a'
|
|
|
|
// } else if w >= 'A' && w <= 'Z' {
|
|
|
|
// return uint(w) - 'A' + 26
|
|
|
|
// } else {
|
|
|
|
// return uint(w) - '0' + 52
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
2019-08-18 19:46:12 +00:00
|
|
|
type Tried struct {
|
2019-08-19 11:03:58 +00:00
|
|
|
root *Node
|
|
|
|
wiStore *wordIndexStore
|
2019-08-18 19:46:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Node struct {
|
|
|
|
data []*Node
|
|
|
|
value interface{}
|
|
|
|
}
|
|
|
|
|
2019-08-19 11:03:58 +00:00
|
|
|
// New 默认 WordIndexLower 意味着只支持小写
|
2019-08-18 19:46:12 +00:00
|
|
|
func New() *Tried {
|
|
|
|
tried := &Tried{}
|
|
|
|
tried.root = new(Node)
|
2019-08-19 11:03:58 +00:00
|
|
|
|
|
|
|
tried.wiStore = WordIndexDict[WordIndexLower]
|
2019-08-18 19:46:12 +00:00
|
|
|
return tried
|
|
|
|
}
|
|
|
|
|
2019-08-19 11:03:58 +00:00
|
|
|
// NewWithWordType 选择单词的类型 WordIndexLower 意味着只支持小写
|
|
|
|
func NewWithWordType(t WordIndexType) *Tried {
|
|
|
|
tried := &Tried{}
|
|
|
|
tried.root = new(Node)
|
|
|
|
|
|
|
|
tried.wiStore = WordIndexDict[t]
|
|
|
|
|
|
|
|
return tried
|
2019-08-19 06:30:24 +00:00
|
|
|
}
|
|
|
|
|
2019-08-19 07:28:09 +00:00
|
|
|
func (tried *Tried) Put(words string, values ...interface{}) {
|
2019-08-18 19:46:12 +00:00
|
|
|
cur := tried.root
|
|
|
|
var n *Node
|
2019-08-19 11:03:58 +00:00
|
|
|
|
2019-08-20 03:38:24 +00:00
|
|
|
bytes := []byte(words)
|
|
|
|
|
|
|
|
for i := 0; i < len(bytes); i++ {
|
|
|
|
w := tried.wiStore.Byte2Index(bytes[i])
|
2019-08-18 19:46:12 +00:00
|
|
|
|
|
|
|
if cur.data == nil {
|
2019-08-19 11:03:58 +00:00
|
|
|
cur.data = make([]*Node, tried.wiStore.DataSize)
|
2019-08-18 19:46:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if n = cur.data[w]; n == nil {
|
|
|
|
n = new(Node)
|
|
|
|
cur.data[w] = n
|
|
|
|
}
|
|
|
|
cur = n
|
|
|
|
}
|
|
|
|
|
2019-08-20 07:03:05 +00:00
|
|
|
if values != nil {
|
2019-08-18 19:46:12 +00:00
|
|
|
cur.value = values[0]
|
2019-08-20 07:03:05 +00:00
|
|
|
} else {
|
|
|
|
cur.value = tried
|
2019-08-18 19:46:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-19 07:28:09 +00:00
|
|
|
func (tried *Tried) Get(words string) interface{} {
|
2019-08-18 19:46:12 +00:00
|
|
|
cur := tried.root
|
|
|
|
var n *Node
|
2019-08-20 07:03:05 +00:00
|
|
|
bytes := []byte(words)
|
2019-08-19 11:03:58 +00:00
|
|
|
|
2019-08-20 07:03:05 +00:00
|
|
|
for i := 0; i < len(bytes); i++ {
|
|
|
|
w := tried.wiStore.Byte2Index(bytes[i]) //TODO: 升级Index 函数
|
2019-08-18 19:46:12 +00:00
|
|
|
if n = cur.data[w]; n == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
cur = n
|
|
|
|
}
|
|
|
|
return n.value
|
|
|
|
}
|
|
|
|
|
2019-08-19 07:28:09 +00:00
|
|
|
func (tried *Tried) Has(words string) bool {
|
2019-08-18 19:46:12 +00:00
|
|
|
return tried.Get(words) != nil
|
|
|
|
}
|
|
|
|
|
2019-08-19 17:39:14 +00:00
|
|
|
func (tried *Tried) HasPrefix(words string) bool {
|
2019-08-20 07:03:05 +00:00
|
|
|
cur := tried.root
|
|
|
|
var n *Node
|
|
|
|
bytes := []byte(words)
|
|
|
|
|
|
|
|
for i := 0; i < len(bytes); i++ {
|
|
|
|
w := tried.wiStore.Byte2Index(bytes[i]) //TODO: 升级Index 函数
|
|
|
|
if n = cur.data[w]; n == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
cur = n
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tried *Tried) PrefixWords(words string) []string {
|
|
|
|
cur := tried.root
|
|
|
|
var n *Node
|
|
|
|
bytes := []byte(words)
|
|
|
|
|
|
|
|
var header []byte
|
|
|
|
for i := 0; i < len(bytes); i++ {
|
|
|
|
curbyte := bytes[i]
|
|
|
|
header = append(header, curbyte)
|
|
|
|
w := tried.wiStore.Byte2Index(curbyte)
|
|
|
|
if n = cur.data[w]; n == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
cur = n
|
|
|
|
}
|
|
|
|
|
|
|
|
var result []string
|
|
|
|
|
|
|
|
var traversal func([]byte, *Node)
|
|
|
|
traversal = func(prefix []byte, cur *Node) {
|
|
|
|
|
|
|
|
for i, n := range cur.data {
|
|
|
|
if n != nil {
|
|
|
|
nextPrefix := append(prefix, tried.wiStore.Index2Byte(uint(i)))
|
|
|
|
traversal(nextPrefix, n)
|
|
|
|
if n.value != nil {
|
|
|
|
result = append(result, string(append(header, nextPrefix...)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
// 拼接头
|
|
|
|
if n != nil {
|
|
|
|
if n.value != nil {
|
|
|
|
result = append(result, string(header))
|
|
|
|
}
|
|
|
|
traversal([]byte{}, n)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
2019-08-19 17:39:14 +00:00
|
|
|
}
|
|
|
|
|
2019-08-18 19:46:12 +00:00
|
|
|
func (tried *Tried) Traversal(every func(cidx uint, value interface{}) bool) {
|
|
|
|
|
|
|
|
var traversal func(*Node)
|
|
|
|
traversal = func(cur *Node) {
|
|
|
|
if cur != nil {
|
|
|
|
for i, n := range cur.data {
|
|
|
|
if n != nil {
|
|
|
|
if n.value != nil {
|
|
|
|
if !every(uint(i), n.value) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
traversal(n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
root := tried.root
|
|
|
|
traversal(root)
|
|
|
|
}
|
|
|
|
|
2019-08-19 17:39:14 +00:00
|
|
|
func (tried *Tried) WordsArray() []string {
|
|
|
|
var result []string
|
|
|
|
|
2019-08-20 03:38:24 +00:00
|
|
|
var traversal func([]byte, *Node)
|
|
|
|
traversal = func(prefix []byte, cur *Node) {
|
2019-08-19 17:39:14 +00:00
|
|
|
|
|
|
|
for i, n := range cur.data {
|
|
|
|
if n != nil {
|
2019-08-20 03:38:24 +00:00
|
|
|
nextPrefix := append(prefix, tried.wiStore.Index2Byte(uint(i)))
|
2019-08-20 03:11:10 +00:00
|
|
|
traversal(nextPrefix, n)
|
2019-08-19 17:39:14 +00:00
|
|
|
if n.value != nil {
|
2019-08-20 03:11:10 +00:00
|
|
|
result = append(result, string(nextPrefix))
|
2019-08-19 17:39:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if tried.root != nil {
|
2019-08-20 03:38:24 +00:00
|
|
|
traversal([]byte{}, tried.root)
|
2019-08-19 17:39:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tried *Tried) String() string {
|
|
|
|
return spew.Sprint(tried.WordsArray())
|
|
|
|
}
|