package sparse type Array2 struct { sizes []int ysize int xsize int data [][]interface{} } func New() *Array2 { arr := &Array2{ysize: 8, xsize: 8} arr.sizes = make([]int, arr.ysize, arr.ysize) arr.data = make([][]interface{}, arr.ysize, arr.ysize) return arr } func NewWithCap(ysize, xsize int) *Array2 { arr := &Array2{ysize: ysize, xsize: xsize} arr.sizes = make([]int, arr.ysize, arr.ysize) arr.data = make([][]interface{}, arr.ysize, arr.ysize) return arr } func (arr *Array2) Values() []interface{} { var result []interface{} for _, y := range arr.data { if y != nil { for _, v := range y { if v == nil { result = append(result, struct{}{}) } else { result = append(result, v) } } } else { for i := 0; i < arr.ysize; i++ { result = append(result, nil) } } } return result } func (arr *Array2) Set(idx int, value interface{}) { yindex := idx / arr.ysize xindex := idx % arr.xsize ydata := arr.data[yindex] if ydata == nil { ydata = make([]interface{}, arr.xsize, arr.xsize) arr.data[yindex] = ydata } if ydata[xindex] == nil { arr.sizes[yindex]++ } ydata[xindex] = value } func (arr *Array2) Get(idx int) (interface{}, bool) { yindex := idx / arr.ysize xindex := idx % arr.xsize ydata := arr.data[yindex] if ydata == nil { return nil, false } v := ydata[xindex] return v, v != nil } func (arr *Array2) Del(idx int) (interface{}, bool) { yindex := idx / arr.ysize xindex := idx % arr.xsize ydata := arr.data[yindex] if ydata == nil { return nil, false } v := ydata[xindex] ydata[xindex] = nil isnil := v != nil if isnil { arr.sizes[yindex]-- if arr.sizes[yindex] == 0 { arr.data[yindex] = nil } } return v, isnil }