structure/sparse_array/array3/array3.go

233 lines
4.5 KiB
Go
Raw Normal View History

2019-04-11 20:48:54 +00:00
package array3
type Array3 struct {
ysizes []int
xsizes [][]int
xyproduct int
zsize int
ysize int
xsize int
data [][][]interface{}
cap int
2019-04-11 20:48:54 +00:00
}
func New() *Array3 {
return NewWithCap(8, 8, 8)
}
func NewWithCap(zsize, ysize, xsize int) *Array3 {
arr := &Array3{zsize: zsize, ysize: ysize, xsize: xsize}
arr.ysizes = make([]int, arr.zsize, arr.zsize)
arr.xsizes = make([][]int, arr.zsize, arr.zsize)
for i := 0; i < arr.zsize; i++ {
arr.xsizes[i] = make([]int, arr.ysize, arr.ysize)
}
arr.xyproduct = arr.ysize * arr.xsize
arr.data = make([][][]interface{}, arr.zsize, arr.zsize)
arr.cap = arr.zsize * arr.xyproduct
2019-04-11 20:48:54 +00:00
return arr
}
2019-04-14 16:56:01 +00:00
func (arr *Array3) debugValues() []interface{} {
var result []interface{}
for _, z := range arr.data {
if z != nil {
for _, y := range z {
if y == nil {
for i := 0; i < arr.xsize; i++ {
result = append(result, nil)
}
} else {
for _, x := range y {
if x == nil {
result = append(result, struct{}{})
} else {
result = append(result, x)
}
}
}
}
} else {
for i := 0; i < arr.ysize*arr.xsize; i++ {
result = append(result, nil)
}
}
}
return result
}
2019-04-11 20:48:54 +00:00
func (arr *Array3) Values() []interface{} {
var result []interface{}
for _, z := range arr.data {
if z != nil {
for _, y := range z {
if y == nil {
for i := 0; i < arr.xsize; i++ {
result = append(result, nil)
}
} else {
for _, x := range y {
if x == nil {
2019-04-14 16:56:01 +00:00
result = append(result, nil)
2019-04-11 20:48:54 +00:00
} else {
result = append(result, x)
}
}
}
}
} else {
for i := 0; i < arr.ysize*arr.xsize; i++ {
result = append(result, nil)
}
}
}
return result
}
func (arr *Array3) Cap() int {
return arr.cap
}
2019-04-14 16:56:01 +00:00
func (arr *Array3) Grow(size int) {
2019-04-15 14:57:44 +00:00
zsize := arr.zsize + size
temp := make([][][]interface{}, zsize, zsize)
2019-04-14 16:56:01 +00:00
copy(temp, arr.data)
arr.data = temp
2019-04-15 14:57:44 +00:00
tempysizes := make([]int, zsize, zsize)
2019-04-14 16:56:01 +00:00
copy(tempysizes, arr.ysizes)
arr.ysizes = tempysizes
2019-04-15 14:57:44 +00:00
tempxsizes := make([][]int, zsize, zsize)
2019-04-14 16:56:01 +00:00
copy(tempxsizes, arr.xsizes)
arr.xsizes = tempxsizes
2019-04-15 14:57:44 +00:00
for i := arr.zsize; i < zsize; i++ {
arr.xsizes[i] = make([]int, arr.ysize, arr.ysize)
}
arr.zsize += size
arr.cap = arr.zsize * arr.xyproduct
2019-04-14 16:56:01 +00:00
}
2019-04-11 20:48:54 +00:00
func (arr *Array3) Set(idx int, value interface{}) {
zindex := idx / arr.xyproduct
nidx := (idx % arr.xyproduct)
2019-04-12 19:35:18 +00:00
yindex := nidx / arr.xsize
xindex := nidx % arr.xsize
2019-04-11 20:48:54 +00:00
2019-04-12 19:35:18 +00:00
ydata := arr.data[zindex]
if ydata == nil {
ydata = make([][]interface{}, arr.ysize, arr.ysize)
arr.data[zindex] = ydata
2019-04-11 20:48:54 +00:00
}
2019-04-12 19:35:18 +00:00
xdata := ydata[yindex]
if xdata == nil {
xdata = make([]interface{}, arr.xsize, arr.xsize)
ydata[yindex] = xdata
2019-04-11 20:48:54 +00:00
arr.ysizes[zindex]++
}
2019-04-12 19:35:18 +00:00
v := xdata[xindex]
if v == nil {
2019-04-11 20:48:54 +00:00
arr.xsizes[zindex][yindex]++
}
2019-04-12 19:35:18 +00:00
xdata[xindex] = value
2019-04-11 20:48:54 +00:00
}
func (arr *Array3) Get(idx int) (interface{}, bool) {
zindex := idx / arr.xyproduct
nextsize := (idx % arr.xyproduct)
2019-04-12 19:35:18 +00:00
yindex := nextsize / arr.xsize
xindex := nextsize % arr.xsize
2019-04-11 20:48:54 +00:00
2019-04-12 19:35:18 +00:00
ydata := arr.data[zindex]
if ydata == nil {
2019-04-11 20:48:54 +00:00
return nil, false
}
2019-04-12 19:35:18 +00:00
xdata := ydata[yindex]
if xdata == nil {
2019-04-11 20:48:54 +00:00
return nil, false
}
2019-04-12 19:35:18 +00:00
v := xdata[xindex]
return v, v != nil
2019-04-11 20:48:54 +00:00
}
2019-04-16 19:22:07 +00:00
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
}
2019-04-11 20:48:54 +00:00
func (arr *Array3) Del(idx int) (interface{}, bool) {
zindex := idx / arr.xyproduct
nextsize := (idx % arr.xyproduct)
2019-04-12 19:35:18 +00:00
yindex := nextsize / arr.xsize
xindex := nextsize % arr.xsize
2019-04-11 20:48:54 +00:00
2019-04-12 19:35:18 +00:00
ydata := arr.data[zindex]
if ydata == nil {
2019-04-11 20:48:54 +00:00
return nil, false
}
2019-04-12 19:35:18 +00:00
xdata := ydata[yindex]
if xdata == nil {
2019-04-11 20:48:54 +00:00
return nil, false
}
2019-04-12 19:35:18 +00:00
v := xdata[xindex]
xdata[xindex] = nil
2019-04-11 20:48:54 +00:00
2019-04-12 19:35:18 +00:00
isnotnil := v != nil
2019-04-11 20:48:54 +00:00
if isnotnil {
arr.xsizes[zindex][yindex]--
if arr.xsizes[zindex][yindex] == 0 {
arr.data[zindex][yindex] = nil
arr.ysizes[zindex]--
if arr.ysizes[zindex] == 0 {
arr.data[zindex] = nil
}
}
}
2019-04-12 19:35:18 +00:00
return v, isnotnil
2019-04-11 20:48:54 +00:00
}