一般常见数据结构, 新添加的缺少规模的测试

This commit is contained in:
2019-05-08 10:26:48 +08:00
parent ccb923e295
commit 1b0bcb9edc
20 changed files with 0 additions and 0 deletions

63
map/hashmap/hashmap.go Normal file
View File

@@ -0,0 +1,63 @@
package hashmap
import "fmt"
type HashMap struct {
hm map[interface{}]interface{}
}
// New instantiates a hash map.
func New() *HashMap {
return &HashMap{hm: make(map[interface{}]interface{})}
}
// Put inserts element into the map.
func (hm *HashMap) Put(key interface{}, value interface{}) {
hm.hm[key] = value
}
func (hm *HashMap) Get(key interface{}) (value interface{}, isfound bool) {
value, isfound = hm.hm[key]
return
}
func (hm *HashMap) Remove(key interface{}) {
delete(hm.hm, key)
}
func (hm *HashMap) Empty() bool {
return len(hm.hm) == 0
}
func (hm *HashMap) Size() int {
return len(hm.hm)
}
func (hm *HashMap) Keys() []interface{} {
keys := make([]interface{}, len(hm.hm))
count := 0
for key := range hm.hm {
keys[count] = key
count++
}
return keys
}
func (hm *HashMap) Values() []interface{} {
values := make([]interface{}, len(hm.hm))
count := 0
for _, value := range hm.hm {
values[count] = value
count++
}
return values
}
func (hm *HashMap) Clear() {
hm.hm = make(map[interface{}]interface{})
}
func (hm *HashMap) String() string {
str := fmt.Sprintf("%v", hm.hm)
return str
}

118
map/hashmap/hashmap_test.go Normal file
View File

@@ -0,0 +1,118 @@
package hashmap
import (
"bytes"
"encoding/gob"
"io/ioutil"
"log"
"testing"
)
func loadTestData() []int {
log.SetFlags(log.Lshortfile)
data, err := ioutil.ReadFile("../l.log")
if err != nil {
log.Println(err)
}
var l []int
decoder := gob.NewDecoder(bytes.NewReader(data))
decoder.Decode(&l)
return l
}
func TestCount(t *testing.T) {
hm := New()
// for i := 0; i < 100000; i++ {
// hm.Put(i, i)
// }
for i := 0; i < 100000; i++ {
hm.Put(i, i)
}
// t.Error(hm.Get(4))
}
var executeCount = 5
var compareSize = 100000
func BenchmarkPut(b *testing.B) {
b.StopTimer()
l := loadTestData()
hm := New()
b.N = len(l) * executeCount
// for i := 0; i < len(l); i++ {
// v := l[i]
// hm.Put(v, v)
// }
b.StartTimer()
for c := 0; c < executeCount; c++ {
for i := 0; i < len(l); i++ {
v := l[i]
hm.Put(v, v)
}
}
//b.Log(len(hm.table), hm.size)
//PrintMemUsage()
}
func BenchmarkGoPut(b *testing.B) {
l := loadTestData()
hm := make(map[int]int)
b.N = len(l) * executeCount
for c := 0; c < executeCount; c++ {
for i := 0; i < len(l); i++ {
v := l[i]
hm[v] = v
}
}
//b.Log(len(m))
//PrintMemUsage()
}
// func BenchmarkGet(b *testing.B) {
// b.StopTimer()
// l := loadTestData()
// hm := New()
// b.N = len(l) * executeCount
// for i := 0; i < len(l); i++ {
// v := l[i]
// hm.Put(v, v)
// }
// b.StartTimer()
// for i := 0; i < b.N; i++ {
// hm.Get(i)
// }
// //b.Log(len(hm.table), hm.size)
// //PrintMemUsage()
// }
// func BenchmarkGoGet(b *testing.B) {
// b.StopTimer()
// l := loadTestData()
// hm := make(map[int]int)
// b.N = len(l) * executeCount
// for i := 0; i < len(l); i++ {
// v := l[i]
// hm[v] = v
// }
// b.StartTimer()
// for i := 0; i < b.N; i++ {
// if _, ok := hm[i]; !ok {
// }
// }
// //b.Log(len(m))
// //PrintMemUsage()
// }