2019-04-11 20:48:54 +00:00
|
|
|
package array2
|
2019-04-11 13:34:50 +00:00
|
|
|
|
|
|
|
type Array2 struct {
|
|
|
|
sizes []int
|
|
|
|
ysize int
|
|
|
|
xsize int
|
|
|
|
data [][]interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func New() *Array2 {
|
2019-04-11 20:48:54 +00:00
|
|
|
return NewWithCap(8, 8)
|
2019-04-11 13:34:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2019-04-12 19:35:18 +00:00
|
|
|
for i := 0; i < arr.xsize; i++ {
|
2019-04-11 13:34:50 +00:00
|
|
|
result = append(result, nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func (arr *Array2) Set(idx int, value interface{}) {
|
2019-04-12 19:35:18 +00:00
|
|
|
yindex := idx / arr.xsize
|
|
|
|
xindex := idx % arr.xsize
|
2019-04-11 13:34:50 +00:00
|
|
|
|
2019-04-12 19:35:18 +00:00
|
|
|
xdata := arr.data[yindex]
|
|
|
|
if xdata == nil {
|
|
|
|
xdata = make([]interface{}, arr.xsize, arr.xsize)
|
|
|
|
arr.data[yindex] = xdata
|
2019-04-11 13:34:50 +00:00
|
|
|
}
|
|
|
|
|
2019-04-12 19:35:18 +00:00
|
|
|
if xdata[xindex] == nil {
|
2019-04-11 13:34:50 +00:00
|
|
|
arr.sizes[yindex]++
|
|
|
|
}
|
|
|
|
|
2019-04-12 19:35:18 +00:00
|
|
|
xdata[xindex] = value
|
2019-04-11 13:34:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (arr *Array2) Get(idx int) (interface{}, bool) {
|
2019-04-12 19:35:18 +00:00
|
|
|
yindex := idx / arr.xsize
|
|
|
|
xindex := idx % arr.xsize
|
2019-04-11 13:34:50 +00:00
|
|
|
|
2019-04-12 19:35:18 +00:00
|
|
|
xdata := arr.data[yindex]
|
|
|
|
if xdata == nil {
|
2019-04-11 13:34:50 +00:00
|
|
|
return nil, false
|
|
|
|
}
|
2019-04-12 19:35:18 +00:00
|
|
|
v := xdata[xindex]
|
|
|
|
return v, v != nil
|
2019-04-11 13:34:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (arr *Array2) Del(idx int) (interface{}, bool) {
|
2019-04-12 19:35:18 +00:00
|
|
|
yindex := idx / arr.xsize
|
|
|
|
xindex := idx % arr.xsize
|
2019-04-11 13:34:50 +00:00
|
|
|
|
2019-04-12 19:35:18 +00:00
|
|
|
xdata := arr.data[yindex]
|
|
|
|
if xdata == nil {
|
2019-04-11 13:34:50 +00:00
|
|
|
return nil, false
|
|
|
|
}
|
2019-04-12 19:35:18 +00:00
|
|
|
v := xdata[xindex]
|
|
|
|
xdata[xindex] = nil
|
2019-04-11 13:34:50 +00:00
|
|
|
|
2019-04-12 19:35:18 +00:00
|
|
|
isnil := v != nil
|
2019-04-11 13:34:50 +00:00
|
|
|
if isnil {
|
|
|
|
arr.sizes[yindex]--
|
|
|
|
if arr.sizes[yindex] == 0 {
|
|
|
|
arr.data[yindex] = nil
|
|
|
|
}
|
|
|
|
}
|
2019-04-12 19:35:18 +00:00
|
|
|
return v, isnil
|
2019-04-11 13:34:50 +00:00
|
|
|
}
|