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

@@ -18,7 +18,7 @@ func NewWithCap(ysize, xsize int) *Array2 {
return arr
}
func (arr *Array2) Values() []interface{} {
func (arr *Array2) debugValues() []interface{} {
var result []interface{}
for _, y := range arr.data {
if y != nil {
@@ -35,10 +35,40 @@ func (arr *Array2) Values() []interface{} {
}
}
}
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) 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
}
func (arr *Array2) Set(idx int, value interface{}) {
yindex := idx / arr.xsize
xindex := idx % arr.xsize

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 {} {} {} {} 5 6 {} <nil> <nil> <nil> <nil> {} {} {} 15]" {
t.Error(result)
}
@@ -31,15 +31,15 @@ func testSet1(t *testing.T) {
}
func testSet2(t *testing.T) {
arr := NewWithCap(3, 6)
l := []int{0, 6, 5, 15}
arr := NewWithCap(5, 3)
l := []int{0, 6, 5, 14}
for _, v := range l {
arr.Set(v, v)
}
var result string
result = spew.Sprint(arr.Values())
if result != "[0 {} {} {} {} 5 6 {} <nil> <nil> <nil> <nil> {} {} {} 15]" {
result = spew.Sprint(arr.debugValues())
if result != "[0 {} {} {} {} 5 6 {} {} <nil> <nil> <nil> {} {} 14]" {
t.Error(result)
}
@@ -122,27 +122,27 @@ func TestArray2Del(t *testing.T) {
var result string
arr.Del(0)
result = spew.Sprint(arr.Values())
result = spew.Sprint(arr.debugValues())
if result != "[{} {} {} {} {} 5 6 {} {} {} {} {} {} {} {} 15 {} {}]" {
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 {} {} {} {} {} {} {} {} 15 {} {}]" {
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> <nil> <nil> <nil> {} {} {} 15 {} {}]" {
t.Error(result)
}
arr.Del(15)
result = spew.Sprint(arr.Values())
result = spew.Sprint(arr.debugValues())
if result != "[<nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil>]" {
t.Error(result)
}
@@ -156,6 +156,41 @@ func TestArray2Del(t *testing.T) {
arr.Del(18)
}
func TestArray2Grow(t *testing.T) {
arr := NewWithCap(4, 4)
l := []int{0, 6, 5, 15}
for _, v := range l {
arr.Set(v, v)
}
arr.Grow(1)
if v, ok := arr.Get(15); ok {
if v != 15 {
t.Error(v)
}
} else {
t.Error(v)
}
arr.Set(19, 19)
if v, ok := arr.Get(19); ok {
if v != 19 {
t.Error(v)
}
} else {
t.Error(v)
}
arr.Grow(-1)
var result string
result = spew.Sprint(arr.debugValues())
if result != "[0 {} {} {} {} 5 6 {} <nil> <nil> <nil> <nil> {} {} {} 15]" {
t.Error(result)
}
}
func BenchmarkArray2Set(b *testing.B) {
arr := NewWithCap(1000, 100)