77 lines
1.3 KiB
Go
77 lines
1.3 KiB
Go
package hashmap
|
|
|
|
import (
|
|
"474420502.top/eson/structure/compare"
|
|
)
|
|
|
|
type hmBucket struct {
|
|
size uint
|
|
data []*bucketNode
|
|
}
|
|
|
|
type bucketNode struct {
|
|
hash uint
|
|
key, value interface{}
|
|
}
|
|
|
|
func newBucket() *hmBucket {
|
|
return &hmBucket{}
|
|
}
|
|
|
|
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
|
|
}
|
|
}
|
|
|
|
if bkt.size < uint(len(bkt.data)) {
|
|
n := bkt.data[bkt.size]
|
|
n.hash = hash
|
|
n.key = k
|
|
n.value = v
|
|
bkt.size++
|
|
return
|
|
}
|
|
|
|
bkt.data = append(bkt.data, &bucketNode{key: k, value: v, hash: hash})
|
|
bkt.size++
|
|
}
|
|
|
|
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
|
|
}
|
|
}
|
|
|
|
if bkt.size < uint(len(bkt.data)) {
|
|
bkt.data[bkt.size] = node
|
|
bkt.size++
|
|
return
|
|
}
|
|
bkt.data = append(bkt.data, node)
|
|
bkt.size++
|
|
}
|