还需要修复

This commit is contained in:
eson 2019-05-06 18:40:09 +08:00
parent d6d4b89dc6
commit 48f48fe782
4 changed files with 80 additions and 38 deletions

View File

@ -1,7 +1,12 @@
package hashmap
import (
"474420502.top/eson/structure/compare"
)
type hmBucket struct {
data []bucketNode
size uint
data []*bucketNode
}
type bucketNode struct {
@ -13,6 +18,57 @@ func newBucket() *hmBucket {
return &hmBucket{}
}
func (bkt *hmBucket) Add(hash uint, k, v interface{}) {
bkt.data = append(bkt.data, bucketNode{key: k, value: v, hash: hash})
func (bkt *hmBucket) Get(key interface{}, Compare compare.Compare) interface{} {
// bkt.data
for _, n := range bkt.data {
if Compare(n.key, key) == 0 {
return n.value
}
}
return nil
}
func (bkt *hmBucket) Add(hash uint, k, v interface{}, Compare compare.Compare) {
for i := uint(0); i < bkt.size; i++ {
n := bkt.data[i]
if Compare(n.key, k) == 0 {
n.hash = hash
n.key = k
n.value = v
return
}
}
bkt.size++
if bkt.size < uint(len(bkt.data)) {
n := bkt.data[bkt.size]
n.hash = hash
n.key = k
n.value = v
return
}
bkt.data = append(bkt.data, &bucketNode{key: k, value: v, hash: hash})
}
func (bkt *hmBucket) AddNode(node *bucketNode, Compare compare.Compare) {
for i := uint(0); i < bkt.size; i++ {
n := bkt.data[i]
if Compare(n.key, node.key) == 0 {
n.hash = node.hash
n.key = node.key
n.value = node.value
panic(123)
return
}
}
bkt.size++
if bkt.size < uint(len(bkt.data)) {
bkt.data[bkt.size] = node
return
}
bkt.data = append(bkt.data, node)
}

View File

@ -48,24 +48,25 @@ func (hm *HashMap) grow() {
newsize := hm.size << 1
// newtable := make([]*avlTree, newsize, newsize)
hm.table.Grow(int(newsize - hm.size))
nodelist := make([]bucketNode, hm.size, hm.size)
i := 0
hm.table.Traversal(func(cur *bucketNode) {
if cur != nil {
nodelist[i] = cur
i++
}
})
nodelist := make([]*bucketNode, hm.size, hm.size)
var count = 0
for i := 0; i < len(hm.table.data); i++ {
curbkt := hm.table.data[i]
tnodelist := curbkt.data
for _, n := range tnodelist {
nodelist[count] = n
count++
}
curbkt.size = 0
}
var hash, index uint
for _, node := range nodelist {
hash = hm.GetHash(node.key)
index = hash % hm.table.cap
index := node.hash % hm.table.cap
//bkt := newtable[index]
bkt := hm.table.GetWithNilSet(index, func(data []*avlTree, idx uint) {
data[idx] = avlNew(hm.Compare)
})
bkt.PutNode(node)
hm.table.data[index].AddNode(node, hm.Compare)
// bkt.data
// bkt.PutNode(node)
}
hm.countNextGrow()
@ -78,15 +79,8 @@ func (hm *HashMap) Put(key, value interface{}) {
tlen := hm.table.Cap()
index := hash % tlen
bkt := hm.table.GetWithNilSet(index, func(data []*avlTree, idx uint) {
data[idx] = avlNew(hm.Compare)
})
n := &avlNode{key: key, value: value}
if bkt.PutNode(n) {
hm.size++
hm.grow()
}
bkt := hm.table.GetWithNilSet(index)
bkt.Add(hash, key, value, hm.Compare)
}
func (hm *HashMap) Get(key interface{}) (interface{}, bool) {
@ -97,7 +91,7 @@ func (hm *HashMap) Get(key interface{}) (interface{}, bool) {
bkt := hm.table.Get(index)
if bkt != nil {
return bkt.Get(key)
return bkt.Get(key, hm.Compare), true
}
return nil, false
}

View File

@ -51,7 +51,7 @@ func bToMb(b uint64) uint64 {
return b / 1024 / 1024
}
var executeCount = 5
var executeCount = 1
var compareSize = 100000
func BenchmarkPut(b *testing.B) {

View File

@ -27,14 +27,6 @@ func (t *Table) Cap() uint {
return t.cap
}
func (t *Table) Traversal(every func(bnode *bucketNode)) {
for _, btk := range t.data {
for _, v := range btk.data {
every(&v)
}
}
}
func (t *Table) Grow(size int) {
zsize := len(t.data) + size
temp := make([]hmBucket, zsize, zsize)
@ -47,7 +39,7 @@ func (t *Table) Get(idx uint) *hmBucket {
return &(t.data[idx])
}
func (t *Table) GetWithNilSet(idx uint, DoSetValue func([]*avlTree, uint)) *hmBucket {
func (t *Table) GetWithNilSet(idx uint) *hmBucket {
return &(t.data[idx])
}