add GetOrSet
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user