add GetOrSet
This commit is contained in:
parent
3bc9a99ca9
commit
8d0df4bfed
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)
|
||||
}
|
|
@ -108,6 +108,29 @@ func (arr *Array2) Get(idx int) (interface{}, bool) {
|
|||
return v, v != nil
|
||||
}
|
||||
|
||||
func (arr *Array2) GetOrSet(idx int, DoSetValue func([]interface{}, int)) (result interface{}, isSet bool) {
|
||||
yindex := idx / arr.xsize
|
||||
xindex := idx % arr.xsize
|
||||
|
||||
xdata := arr.data[yindex]
|
||||
if xdata == nil {
|
||||
xdata = make([]interface{}, arr.xsize, arr.xsize)
|
||||
arr.data[yindex] = xdata
|
||||
}
|
||||
|
||||
result = xdata[xindex]
|
||||
if result == nil {
|
||||
DoSetValue(xdata, xindex)
|
||||
result = xdata[xindex]
|
||||
if result == nil {
|
||||
panic("DoSetValue Not Set <nil> Value")
|
||||
}
|
||||
arr.sizes[yindex]++
|
||||
return result, true
|
||||
}
|
||||
return result, false
|
||||
}
|
||||
|
||||
func (arr *Array2) Del(idx int) (interface{}, bool) {
|
||||
yindex := idx / arr.xsize
|
||||
xindex := idx % arr.xsize
|
||||
|
|
|
@ -163,6 +163,38 @@ func (arr *Array3) Get(idx int) (interface{}, bool) {
|
|||
return v, v != nil
|
||||
}
|
||||
|
||||
func (arr *Array3) GetOrSet(idx int, DoSetValue func([]interface{}, int)) (result interface{}, isSet bool) {
|
||||
zindex := idx / arr.xyproduct
|
||||
nidx := (idx % arr.xyproduct)
|
||||
yindex := nidx / arr.xsize
|
||||
xindex := nidx % arr.xsize
|
||||
|
||||
ydata := arr.data[zindex]
|
||||
if ydata == nil {
|
||||
ydata = make([][]interface{}, arr.ysize, arr.ysize)
|
||||
arr.data[zindex] = ydata
|
||||
}
|
||||
|
||||
xdata := ydata[yindex]
|
||||
if xdata == nil {
|
||||
xdata = make([]interface{}, arr.xsize, arr.xsize)
|
||||
ydata[yindex] = xdata
|
||||
arr.ysizes[zindex]++
|
||||
}
|
||||
|
||||
result = xdata[xindex]
|
||||
if result == nil {
|
||||
DoSetValue(xdata, xindex)
|
||||
result = xdata[xindex]
|
||||
if result == nil {
|
||||
panic("DoSetValue Not Set <nil> Value")
|
||||
}
|
||||
arr.xsizes[zindex][yindex]++
|
||||
return result, false
|
||||
}
|
||||
return result, true
|
||||
}
|
||||
|
||||
func (arr *Array3) Del(idx int) (interface{}, bool) {
|
||||
zindex := idx / arr.xyproduct
|
||||
nextsize := (idx % arr.xyproduct)
|
||||
|
|
Loading…
Reference in New Issue
Block a user