finish sparse array

This commit is contained in:
2019-04-15 00:56:01 +08:00
parent 6389722a0c
commit 703dff0813
6 changed files with 424 additions and 68 deletions

View File

@@ -29,11 +29,10 @@ func NewWithCap(zsize, ysize, xsize int) *Array3 {
return arr
}
func (arr *Array3) Values() []interface{} {
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++ {
@@ -55,10 +54,54 @@ func (arr *Array3) Values() []interface{} {
}
}
}
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) Grow(size int) {
arr.ysize += size
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
}
func (arr *Array3) Set(idx int, value interface{}) {
zindex := idx / arr.xyproduct
nidx := (idx % arr.xyproduct)

View File

@@ -16,7 +16,7 @@ func testSet1(t *testing.T) {
}
var result string
result = spew.Sprint(arr.Values())
result = spew.Sprint(arr.debugValues())
if result != "[0 1 <nil> <nil> <nil> <nil> {} 7]" {
t.Error(result)
}
@@ -38,7 +38,7 @@ func testSet2(t *testing.T) {
}
var result string
result = spew.Sprint(arr.Values())
result = spew.Sprint(arr.debugValues())
if result != "[0 {} {} {} {} 5 6 {} {} {} {} 11]" {
t.Error(arr.data)
t.Error(result)
@@ -74,22 +74,26 @@ func testArray2Get1(t *testing.T) {
}
}
if v, ok := arr.Get(8*8*8 - 1); ok {
t.Error(v)
}
defer func() {
if err := recover(); err == nil {
t.Error("err == nil, but array the get is out of range")
}
}()
arr.Get(64)
arr.Get(8 * 8 * 8)
}
func testArray2Get2(t *testing.T) {
arr := NewWithCap(4, 3, 3)
for i := 0; i < 64; i++ {
for i := 0; i < 36; i++ {
arr.Set(i, i)
}
for i := 0; i < 64; i++ {
for i := 0; i < 36; i++ {
if v, ok := arr.Get(i); ok {
if v != i {
t.Error("v is equal i, but", v, i)
@@ -105,7 +109,7 @@ func testArray2Get2(t *testing.T) {
}
}()
arr.Get(72)
arr.Get(36)
}
func TestArray2Get(t *testing.T) {
@@ -123,27 +127,27 @@ func TestArray2Del(t *testing.T) {
var result string
arr.Del(0)
result = spew.Sprint(arr.Values())
result = spew.Sprint(arr.debugValues())
if result != "[<nil> <nil> <nil> {} {} 5 6 {} {} {} {} 11]" {
t.Error(arr.data)
t.Error(result)
}
arr.Del(5)
result = spew.Sprint(arr.Values())
result = spew.Sprint(arr.debugValues())
if result != "[<nil> <nil> <nil> <nil> <nil> <nil> 6 {} {} {} {} 11]" {
t.Error(arr.data)
t.Error(result)
}
arr.Del(6)
result = spew.Sprint(arr.Values())
result = spew.Sprint(arr.debugValues())
if result != "[<nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> {} {} 11]" {
t.Error(result)
}
arr.Del(11)
result = spew.Sprint(arr.Values())
result = spew.Sprint(arr.debugValues())
if result != "[<nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil>]" {
t.Error(result)
}
@@ -157,7 +161,7 @@ func TestArray2Del(t *testing.T) {
arr.Del(18)
}
func BenchmarkArray2Set(b *testing.B) {
func BenchmarkArray3Set(b *testing.B) {
arr := NewWithCap(100, 100, 10)
b.N = 500000000
@@ -173,10 +177,9 @@ func BenchmarkArray2Set(b *testing.B) {
arr.Set(l[i], i)
}
}
}
func BenchmarkArray2Get(b *testing.B) {
func BenchmarkArray3Get(b *testing.B) {
arr := NewWithCap(100, 100, 10)
b.N = 500000000
@@ -192,10 +195,9 @@ func BenchmarkArray2Get(b *testing.B) {
for i := 0; i < b.N; i++ {
arr.Get(i % 65535)
}
}
func BenchmarkArray2Del(b *testing.B) {
func BenchmarkArray3Del(b *testing.B) {
arr := NewWithCap(100, 100, 10)
b.N = 500000000
@@ -210,5 +212,4 @@ func BenchmarkArray2Del(b *testing.B) {
for i := 0; i < b.N; i++ {
arr.Del(i % 65535)
}
}