structure/sparse_array/array3/array3.go

196 lines
3.6 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) {
arr.zsize += size
2019-04-14 16:56:01 +00:00
temp := make([][][]interface{}, arr.zsize, arr.zsize)
copy(temp, arr.data)
arr.data = temp
tempysizes := make([]int, arr.zsize, arr.zsize)
copy(tempysizes, arr.ysizes)
arr.ysizes = tempysizes
tempxsizes := make([][]int, arr.ysize, arr.ysize)
copy(tempxsizes, arr.xsizes)
arr.xsizes = tempxsizes
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
}
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
}