add GetOrSet

This commit is contained in:
2019-04-17 03:22:07 +08:00
parent 3bc9a99ca9
commit 8d0df4bfed
4 changed files with 104 additions and 0 deletions

42
hashmap/hashmap.go Normal file
View 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
View File

@@ -0,0 +1,7 @@
package hashmap
import "testing"
func TestCount(t *testing.T) {
HashInt(123123131221)
}