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