修改为函数形式
This commit is contained in:
parent
86253b5bfb
commit
b4829ba058
|
@ -47,11 +47,11 @@ func (tried *Tried) wordIndex(w byte) uint {
|
|||
return uint(w) - 'a'
|
||||
}
|
||||
|
||||
func (tried *Tried) Put(words ObjectIndex, values ...interface{}) {
|
||||
func (tried *Tried) Put(words string, values ...interface{}) {
|
||||
cur := tried.root
|
||||
var n *Node
|
||||
for i := uint(0); i < words.Size(); i++ {
|
||||
w := words.WordIndex(i)
|
||||
for i := 0; i < len(words); i++ {
|
||||
w := tried.wordIndex(words[i])
|
||||
|
||||
if cur.data == nil {
|
||||
cur.data = make([]*Node, tried.datasize)
|
||||
|
@ -78,11 +78,11 @@ func (tried *Tried) Put(words ObjectIndex, values ...interface{}) {
|
|||
|
||||
}
|
||||
|
||||
func (tried *Tried) Get(words ObjectIndex) interface{} {
|
||||
func (tried *Tried) Get(words string) interface{} {
|
||||
cur := tried.root
|
||||
var n *Node
|
||||
for i := uint(0); i < words.Size(); i++ {
|
||||
w := words.WordIndex(i) //TODO: 升级Index 函数
|
||||
for i := 0; i < len(words); i++ {
|
||||
w := tried.wordIndex(words[i]) //TODO: 升级Index 函数
|
||||
if n = cur.data[w]; n == nil {
|
||||
return nil
|
||||
}
|
||||
|
@ -91,7 +91,7 @@ func (tried *Tried) Get(words ObjectIndex) interface{} {
|
|||
return n.value
|
||||
}
|
||||
|
||||
func (tried *Tried) Has(words ObjectIndex) bool {
|
||||
func (tried *Tried) Has(words string) bool {
|
||||
return tried.Get(words) != nil
|
||||
}
|
||||
|
||||
|
|
|
@ -6,75 +6,75 @@ import (
|
|||
"github.com/Pallinder/go-randomdata"
|
||||
)
|
||||
|
||||
// func TestTried_PutAndGet1(t *testing.T) {
|
||||
// tried := New()
|
||||
func TestTried_PutAndGet1(t *testing.T) {
|
||||
tried := New()
|
||||
|
||||
// tried.Put(("asdf"))
|
||||
// tried.Put(("hehe"), "hehe")
|
||||
// tried.Put(("xixi"), 3)
|
||||
tried.Put(("asdf"))
|
||||
tried.Put(("hehe"), "hehe")
|
||||
tried.Put(("xixi"), 3)
|
||||
|
||||
// var result interface{}
|
||||
var result interface{}
|
||||
|
||||
// result = tried.Get("asdf")
|
||||
// if result != tried {
|
||||
// t.Error("result should be 3")
|
||||
// }
|
||||
result = tried.Get("asdf")
|
||||
if result != tried {
|
||||
t.Error("result should be 3")
|
||||
}
|
||||
|
||||
// result = tried.Get("xixi")
|
||||
// if result != 3 {
|
||||
// t.Error("result should be 3")
|
||||
// }
|
||||
result = tried.Get("xixi")
|
||||
if result != 3 {
|
||||
t.Error("result should be 3")
|
||||
}
|
||||
|
||||
// result = tried.Get("hehe")
|
||||
// if result != "hehe" {
|
||||
// t.Error("result should be hehe")
|
||||
// }
|
||||
result = tried.Get("hehe")
|
||||
if result != "hehe" {
|
||||
t.Error("result should be hehe")
|
||||
}
|
||||
|
||||
// result = tried.Get("haha")
|
||||
// if result != nil {
|
||||
// t.Error("result should be nil")
|
||||
// }
|
||||
result = tried.Get("haha")
|
||||
if result != nil {
|
||||
t.Error("result should be nil")
|
||||
}
|
||||
|
||||
// result = tried.Get("b")
|
||||
// if result != nil {
|
||||
// t.Error("result should be nil")
|
||||
// }
|
||||
// }
|
||||
result = tried.Get("b")
|
||||
if result != nil {
|
||||
t.Error("result should be nil")
|
||||
}
|
||||
}
|
||||
|
||||
// func TestTried_Traversal(t *testing.T) {
|
||||
// tried := New()
|
||||
// tried.Put("asdf")
|
||||
// tried.Put(("abdf"), "ab")
|
||||
// tried.Put(("hehe"), "hehe")
|
||||
// tried.Put(("xixi"), 3)
|
||||
func TestTried_Traversal(t *testing.T) {
|
||||
tried := New()
|
||||
tried.Put("asdf")
|
||||
tried.Put(("abdf"), "ab")
|
||||
tried.Put(("hehe"), "hehe")
|
||||
tried.Put(("xixi"), 3)
|
||||
|
||||
// var result []interface{}
|
||||
// tried.Traversal(func(idx uint, v interface{}) bool {
|
||||
// // t.Error(idx, v)
|
||||
// result = append(result, v)
|
||||
// return true
|
||||
// })
|
||||
var result []interface{}
|
||||
tried.Traversal(func(idx uint, v interface{}) bool {
|
||||
// t.Error(idx, v)
|
||||
result = append(result, v)
|
||||
return true
|
||||
})
|
||||
|
||||
// if result[0] != "ab" {
|
||||
// t.Error(result[0])
|
||||
// }
|
||||
if result[0] != "ab" {
|
||||
t.Error(result[0])
|
||||
}
|
||||
|
||||
// if result[1] != tried {
|
||||
// t.Error(result[1])
|
||||
// }
|
||||
if result[1] != tried {
|
||||
t.Error(result[1])
|
||||
}
|
||||
|
||||
// if result[2] != "hehe" {
|
||||
// t.Error(result[2])
|
||||
// }
|
||||
if result[2] != "hehe" {
|
||||
t.Error(result[2])
|
||||
}
|
||||
|
||||
// if result[3] != 3 {
|
||||
// t.Error(result[3])
|
||||
// }
|
||||
// }
|
||||
if result[3] != 3 {
|
||||
t.Error(result[3])
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkTried_Put(b *testing.B) {
|
||||
|
||||
var data []TriedString
|
||||
var data []string
|
||||
b.N = 1000000
|
||||
count := 10
|
||||
|
||||
|
@ -84,7 +84,7 @@ func BenchmarkTried_Put(b *testing.B) {
|
|||
char := randomdata.Number(0, 26) + 'a'
|
||||
content = append(content, rune(byte(char)))
|
||||
}
|
||||
data = append(data, TriedString(string(content)))
|
||||
data = append(data, (string(content)))
|
||||
}
|
||||
|
||||
b.ResetTimer()
|
||||
|
@ -99,7 +99,7 @@ func BenchmarkTried_Put(b *testing.B) {
|
|||
|
||||
func BenchmarkTried_Get(b *testing.B) {
|
||||
|
||||
var data []TriedString
|
||||
var data []string
|
||||
b.N = 1000000
|
||||
count := 10
|
||||
|
||||
|
@ -109,7 +109,7 @@ func BenchmarkTried_Get(b *testing.B) {
|
|||
char := randomdata.Number(0, 26) + 'a'
|
||||
content = append(content, rune(byte(char)))
|
||||
}
|
||||
data = append(data, TriedString(content))
|
||||
data = append(data, string(content))
|
||||
}
|
||||
|
||||
b.N = b.N * count
|
||||
|
|
Loading…
Reference in New Issue
Block a user