diff --git a/hashmap/hashmap.go b/hashmap/hashmap.go new file mode 100644 index 0000000..26ee198 --- /dev/null +++ b/hashmap/hashmap.go @@ -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) +} diff --git a/hashmap/hashmap_test.go b/hashmap/hashmap_test.go new file mode 100644 index 0000000..76935df --- /dev/null +++ b/hashmap/hashmap_test.go @@ -0,0 +1,7 @@ +package hashmap + +import "testing" + +func TestCount(t *testing.T) { + HashInt(123123131221) +} diff --git a/sparse_array/array2/array2.go b/sparse_array/array2/array2.go index 9611e90..ca0bbef 100644 --- a/sparse_array/array2/array2.go +++ b/sparse_array/array2/array2.go @@ -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 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 diff --git a/sparse_array/array3/array3.go b/sparse_array/array3/array3.go index bde429a..ad78953 100644 --- a/sparse_array/array3/array3.go +++ b/sparse_array/array3/array3.go @@ -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 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)