函数形式
This commit is contained in:
parent
ebbce1e0a2
commit
874acd6243
|
@ -49,11 +49,11 @@ func (tried *Tried) wordIndex(w byte) uint {
|
|||
}
|
||||
}
|
||||
|
||||
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)
|
||||
|
@ -80,11 +80,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
|
||||
}
|
||||
|
@ -93,7 +93,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
|
||||
}
|
||||
|
||||
|
|
|
@ -9,33 +9,33 @@ import (
|
|||
func TestTried_PutAndGet1(t *testing.T) {
|
||||
tried := New()
|
||||
|
||||
tried.Put(TriedString("asdf"))
|
||||
tried.Put(TriedString("hehe"), "hehe")
|
||||
tried.Put(TriedString("xixi"), 3)
|
||||
tried.Put(("asdf"))
|
||||
tried.Put(("hehe"), "hehe")
|
||||
tried.Put(("xixi"), 3)
|
||||
|
||||
var result interface{}
|
||||
|
||||
result = tried.Get(TriedString("asdf"))
|
||||
result = tried.Get("asdf")
|
||||
if result != tried {
|
||||
t.Error("result should be 3")
|
||||
}
|
||||
|
||||
result = tried.Get(TriedString("xixi"))
|
||||
result = tried.Get("xixi")
|
||||
if result != 3 {
|
||||
t.Error("result should be 3")
|
||||
}
|
||||
|
||||
result = tried.Get(TriedString("hehe"))
|
||||
result = tried.Get("hehe")
|
||||
if result != "hehe" {
|
||||
t.Error("result should be hehe")
|
||||
}
|
||||
|
||||
result = tried.Get(TriedString("haha"))
|
||||
result = tried.Get("haha")
|
||||
if result != nil {
|
||||
t.Error("result should be nil")
|
||||
}
|
||||
|
||||
result = tried.Get(TriedString("b"))
|
||||
result = tried.Get("b")
|
||||
if result != nil {
|
||||
t.Error("result should be nil")
|
||||
}
|
||||
|
@ -43,10 +43,10 @@ func TestTried_PutAndGet1(t *testing.T) {
|
|||
|
||||
func TestTried_Traversal(t *testing.T) {
|
||||
tried := New()
|
||||
tried.Put(TriedString("asdf"))
|
||||
tried.Put(TriedString("abdf"), "ab")
|
||||
tried.Put(TriedString("hehe"), "hehe")
|
||||
tried.Put(TriedString("xixi"), 3)
|
||||
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 {
|
||||
|
@ -74,12 +74,12 @@ func TestTried_Traversal(t *testing.T) {
|
|||
|
||||
func BenchmarkTried_Put(b *testing.B) {
|
||||
|
||||
var data []TriedString
|
||||
b.N = 10000
|
||||
count := 1000
|
||||
var data []string
|
||||
b.N = 100000
|
||||
count := 50
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
data = append(data, TriedString(randomdata.RandStringRunes(10)+randomdata.RandStringRunes(4)))
|
||||
data = append(data, (randomdata.RandStringRunes(10) + randomdata.RandStringRunes(4)))
|
||||
}
|
||||
|
||||
b.ResetTimer()
|
||||
|
@ -94,12 +94,12 @@ func BenchmarkTried_Put(b *testing.B) {
|
|||
|
||||
func BenchmarkTried_Get(b *testing.B) {
|
||||
|
||||
var data []TriedString
|
||||
b.N = 10000
|
||||
count := 1000
|
||||
var data []string
|
||||
b.N = 100000
|
||||
count := 50
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
data = append(data, TriedString(randomdata.RandStringRunes(10)+randomdata.RandStringRunes(4)))
|
||||
data = append(data, (randomdata.RandStringRunes(10) + randomdata.RandStringRunes(4)))
|
||||
}
|
||||
|
||||
b.N = b.N * count
|
||||
|
|
Loading…
Reference in New Issue
Block a user