43 lines
809 B
Go
43 lines
809 B
Go
|
package hashmap
|
||
|
|
||
|
import (
|
||
|
"474420502.top/eson/structure/sparse_array/array3"
|
||
|
)
|
||
|
|
||
|
type HashCode func(key interface{}) int
|
||
|
type EqualsCode func(k1, k2 interface{}) int
|
||
|
|
||
|
type HashMap struct {
|
||
|
growfactor int
|
||
|
table *array3.Array3
|
||
|
|
||
|
GetHash HashCode
|
||
|
IsEquals EqualsCode
|
||
|
}
|
||
|
|
||
|
type Bucket struct {
|
||
|
}
|
||
|
|
||
|
func HashInt(key interface{}) uint {
|
||
|
thekey := uint(key.(int))
|
||
|
hbit := thekey & 0xffffffff
|
||
|
lbit := (thekey & 0xffffffff00000000) >> 32
|
||
|
// log.Println(hbit)
|
||
|
// log.Println(lbit)
|
||
|
// log.Println()
|
||
|
return lbit ^ hbit
|
||
|
}
|
||
|
|
||
|
func New() *HashMap {
|
||
|
hm := &HashMap{}
|
||
|
hm.growfactor = 2
|
||
|
hm.table = array3.NewWithCap(hm.growfactor*2, hm.growfactor, hm.growfactor)
|
||
|
return hm
|
||
|
}
|
||
|
|
||
|
func (hm *HashMap) Put(key, value interface{}) {
|
||
|
hash := hm.GetHash(key)
|
||
|
index := hash % hm.table.Cap()
|
||
|
hm.table.Set(index, value)
|
||
|
}
|