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-19 07:28:09 +00:00
|
|
|
for i := 0; i < len(words); i++ {
|
2019-08-19 11:03:58 +00:00
|
|
|
w := tried.wiStore.Byte2Index(words[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
|
|
|
|
}
|
|
|
|
|
|
|
|
vlen := len(values)
|
|
|
|
switch vlen {
|
|
|
|
case 0:
|
|
|
|
cur.value = tried
|
|
|
|
case 1:
|
|
|
|
cur.value = values[0]
|
|
|
|
case 2:
|
|
|
|
// TODO: 执行函数 values[1] 为函数类型 func (cur *Node, value interface{}) ...可以插入, 也可以不插入
|
|
|
|
default:
|
|
|
|
panic("unknow select to do")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
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-19 11:03:58 +00:00
|
|
|
|
2019-08-19 07:28:09 +00:00
|
|
|
for i := 0; i < len(words); i++ {
|
2019-08-19 11:03:58 +00:00
|
|
|
w := tried.wiStore.Byte2Index(words[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 {
|
|
|
|
return tried.Get(words) != nil
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
|
var traversal func([]rune, *Node)
|
|
|
|
traversal = func(prefix []rune, cur *Node) {
|
|
|
|
|
|
|
|
for i, n := range cur.data {
|
|
|
|
if n != nil {
|
|
|
|
prefix = append(prefix, rune(tried.wiStore.Index2Byte(uint(i))))
|
|
|
|
traversal(prefix, n)
|
|
|
|
if n.value != nil {
|
|
|
|
result = append(result, string(prefix))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if tried.root != nil {
|
|
|
|
traversal([]rune{}, tried.root)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tried *Tried) String() string {
|
|
|
|
return spew.Sprint(tried.WordsArray())
|
|
|
|
}
|