focus/sparse_array/array3/array3.go

233 lines
4.5 KiB
Go
Raw Permalink Normal View History

new file: .gitignore new file: LICENSE new file: README.md new file: compare/compare.go new file: for_test.go new file: heap/heap.go new file: heap/heap_test.go new file: interface.go new file: lastack/lastack.go new file: lastack/lastack_test.go new file: list/list.go new file: list/list_test.go new file: map/hashmap/hashmap.go new file: map/hashmap/hashmap_test.go new file: priority_list/iterator.go new file: priority_list/priority_list.go new file: priority_list/priority_list_test.go new file: priority_queue/iterator.go new file: priority_queue/priority_queue.go new file: priority_queue/priority_queue_test.go new file: priority_queue/vbt.go new file: priority_queue/vbt_test.go new file: priority_queuekey/iterator.go new file: priority_queuekey/priority_queuekey.go new file: priority_queuekey/priority_queuekey_test.go new file: priority_queuekey/vbt.go new file: priority_queuekey/vbt_test.go new file: set/hashset/hashset.go new file: set/hashset/hashset_test.go new file: set/treeset/treeset.go new file: set/treeset/treeset_test.go new file: sparse_array/array2/array2.go new file: sparse_array/array2/array2_test.go new file: sparse_array/array3/array3.go new file: sparse_array/array3/array3_test.go new file: sparse_array/arrayn/arrayn.go new file: sparse_array/arrayn/arrayn_test.go new file: stack/stack.go new file: stack/stack_test.go new file: tree/avl/avl.go new file: tree/avl/avl_test.go new file: tree/avl/iterator.go new file: tree/avldup/avldup.go new file: tree/avldup/avldup_test.go new file: tree/avldup/iterator.go new file: tree/avlkey/avlkey.go new file: tree/avlkey/avlkey_test.go new file: tree/avlkey/iterator.go new file: tree/avlkeydup/avlkeydup.go new file: tree/avlkeydup/avlkeydup_test.go new file: tree/avlkeydup/iterator.go new file: tree/vbt/iterator.go new file: tree/vbt/vbt.go new file: tree/vbt/vbt_test.go new file: tree/vbtkey/iterator.go new file: tree/vbtkey/vbtkey.go new file: tree/vbtkey/vbtkey_test.go
2019-05-08 02:42:51 +00:00
package array3
type Array3 struct {
ysizes []int
xsizes [][]int
xyproduct int
zsize int
ysize int
xsize int
data [][][]interface{}
cap int
}
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
return arr
}
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
}
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 {
result = append(result, nil)
} 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
}
func (arr *Array3) Grow(size int) {
zsize := arr.zsize + size
temp := make([][][]interface{}, zsize, zsize)
copy(temp, arr.data)
arr.data = temp
tempysizes := make([]int, zsize, zsize)
copy(tempysizes, arr.ysizes)
arr.ysizes = tempysizes
tempxsizes := make([][]int, zsize, zsize)
copy(tempxsizes, arr.xsizes)
arr.xsizes = tempxsizes
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
}
func (arr *Array3) Set(idx int, value interface{}) {
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]++
}
v := xdata[xindex]
if v == nil {
arr.xsizes[zindex][yindex]++
}
xdata[xindex] = value
}
func (arr *Array3) Get(idx int) (interface{}, bool) {
zindex := idx / arr.xyproduct
nextsize := (idx % arr.xyproduct)
yindex := nextsize / arr.xsize
xindex := nextsize % arr.xsize
ydata := arr.data[zindex]
if ydata == nil {
return nil, false
}
xdata := ydata[yindex]
if xdata == nil {
return nil, false
}
v := xdata[xindex]
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)
yindex := nextsize / arr.xsize
xindex := nextsize % arr.xsize
ydata := arr.data[zindex]
if ydata == nil {
return nil, false
}
xdata := ydata[yindex]
if xdata == nil {
return nil, false
}
v := xdata[xindex]
xdata[xindex] = nil
isnotnil := v != nil
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
}
}
}
return v, isnotnil
}