函数形式

This commit is contained in:
huangsimin 2019-08-19 15:08:49 +08:00
parent ebbce1e0a2
commit 874acd6243
2 changed files with 27 additions and 27 deletions

View File

@ -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 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)
@ -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 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
} }
@ -93,7 +93,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
} }

View File

@ -9,33 +9,33 @@ import (
func TestTried_PutAndGet1(t *testing.T) { func TestTried_PutAndGet1(t *testing.T) {
tried := New() tried := New()
tried.Put(TriedString("asdf")) tried.Put(("asdf"))
tried.Put(TriedString("hehe"), "hehe") tried.Put(("hehe"), "hehe")
tried.Put(TriedString("xixi"), 3) tried.Put(("xixi"), 3)
var result interface{} var result interface{}
result = tried.Get(TriedString("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(TriedString("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(TriedString("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(TriedString("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(TriedString("b")) result = tried.Get("b")
if result != nil { if result != nil {
t.Error("result should be nil") t.Error("result should be nil")
} }
@ -43,10 +43,10 @@ func TestTried_PutAndGet1(t *testing.T) {
func TestTried_Traversal(t *testing.T) { func TestTried_Traversal(t *testing.T) {
tried := New() tried := New()
tried.Put(TriedString("asdf")) tried.Put("asdf")
tried.Put(TriedString("abdf"), "ab") tried.Put(("abdf"), "ab")
tried.Put(TriedString("hehe"), "hehe") tried.Put(("hehe"), "hehe")
tried.Put(TriedString("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 {
@ -74,12 +74,12 @@ func TestTried_Traversal(t *testing.T) {
func BenchmarkTried_Put(b *testing.B) { func BenchmarkTried_Put(b *testing.B) {
var data []TriedString var data []string
b.N = 10000 b.N = 100000
count := 1000 count := 50
for i := 0; i < b.N; i++ { 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() b.ResetTimer()
@ -94,12 +94,12 @@ 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 = 10000 b.N = 100000
count := 1000 count := 50
for i := 0; i < b.N; i++ { 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 b.N = b.N * count