focus/sparse_array/array2/array2.go

154 lines
2.8 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 array2
type Array2 struct {
sizes []int
ysize int
xsize int
data [][]interface{}
cap int
}
func New() *Array2 {
return NewWithCap(8, 8)
}
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)
arr.cap = arr.ysize * arr.xsize
return arr
}
func (arr *Array2) debugValues() []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.xsize; i++ {
result = append(result, nil)
}
}
}
return result
}
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, nil)
} else {
result = append(result, v)
}
}
} else {
for i := 0; i < arr.xsize; i++ {
result = append(result, nil)
}
}
}
return result
}
func (arr *Array2) Cap() int {
return arr.cap
}
func (arr *Array2) Grow(size int) {
arr.ysize += size
temp := make([][]interface{}, arr.ysize, arr.ysize)
copy(temp, arr.data)
arr.data = temp
tempsizes := make([]int, arr.ysize, arr.ysize)
copy(tempsizes, arr.sizes)
arr.sizes = tempsizes
arr.cap = arr.ysize * arr.xsize
}
func (arr *Array2) Set(idx int, value interface{}) {
yindex := idx / arr.xsize
xindex := idx % arr.xsize
xdata := arr.data[yindex]
if xdata == nil {
xdata = make([]interface{}, arr.xsize, arr.xsize)
arr.data[yindex] = xdata
}
if xdata[xindex] == nil {
arr.sizes[yindex]++
}
xdata[xindex] = value
}
func (arr *Array2) Get(idx int) (interface{}, bool) {
yindex := idx / arr.xsize
xindex := idx % arr.xsize
xdata := arr.data[yindex]
if xdata == nil {
return nil, false
}
v := xdata[xindex]
return v, v != nil
}
func (arr *Array2) GetOrSet(idx int, DoSetValue func([]interface{}, int)) (result interface{}, isSet bool) {
yindex := idx / arr.xsize
xindex := idx % arr.xsize
xdata := arr.data[yindex]
if xdata == nil {
xdata = make([]interface{}, arr.xsize, arr.xsize)
arr.data[yindex] = xdata
}
result = xdata[xindex]
if result == nil {
DoSetValue(xdata, xindex)
result = xdata[xindex]
if result == nil {
panic("DoSetValue Not Set <nil> Value")
}
arr.sizes[yindex]++
return result, true
}
return result, false
}
func (arr *Array2) Del(idx int) (interface{}, bool) {
yindex := idx / arr.xsize
xindex := idx % arr.xsize
xdata := arr.data[yindex]
if xdata == nil {
return nil, false
}
v := xdata[xindex]
xdata[xindex] = nil
isnil := v != nil
if isnil {
arr.sizes[yindex]--
if arr.sizes[yindex] == 0 {
arr.data[yindex] = nil
}
}
return v, isnil
}