add GetOrSet
This commit is contained in:
42
hashmap/hashmap.go
Normal file
42
hashmap/hashmap.go
Normal file
@@ -0,0 +1,42 @@
|
||||
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)
|
||||
}
|
||||
7
hashmap/hashmap_test.go
Normal file
7
hashmap/hashmap_test.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package hashmap
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestCount(t *testing.T) {
|
||||
HashInt(123123131221)
|
||||
}
|
||||
Reference in New Issue
Block a user