完成 ArrayN del 操作

This commit is contained in:
2019-04-13 03:35:18 +08:00
parent 3b54163d06
commit 6389722a0c
6 changed files with 382 additions and 96 deletions

View File

@@ -30,7 +30,7 @@ func (arr *Array2) Values() []interface{} {
}
}
} else {
for i := 0; i < arr.ysize; i++ {
for i := 0; i < arr.xsize; i++ {
result = append(result, nil)
}
}
@@ -40,51 +40,51 @@ func (arr *Array2) Values() []interface{} {
}
func (arr *Array2) Set(idx int, value interface{}) {
yindex := idx / arr.ysize
xindex := idx % arr.ysize
yindex := idx / arr.xsize
xindex := idx % arr.xsize
ydata := arr.data[yindex]
if ydata == nil {
ydata = make([]interface{}, arr.xsize, arr.xsize)
arr.data[yindex] = ydata
xdata := arr.data[yindex]
if xdata == nil {
xdata = make([]interface{}, arr.xsize, arr.xsize)
arr.data[yindex] = xdata
}
if ydata[xindex] == nil {
if xdata[xindex] == nil {
arr.sizes[yindex]++
}
ydata[xindex] = value
xdata[xindex] = value
}
func (arr *Array2) Get(idx int) (interface{}, bool) {
yindex := idx / arr.ysize
xindex := idx % arr.ysize
yindex := idx / arr.xsize
xindex := idx % arr.xsize
ydata := arr.data[yindex]
if ydata == nil {
xdata := arr.data[yindex]
if xdata == nil {
return nil, false
}
xdata := ydata[xindex]
return xdata, xdata != nil
v := xdata[xindex]
return v, v != nil
}
func (arr *Array2) Del(idx int) (interface{}, bool) {
yindex := idx / arr.ysize
xindex := idx % arr.ysize
yindex := idx / arr.xsize
xindex := idx % arr.xsize
ydata := arr.data[yindex]
if ydata == nil {
xdata := arr.data[yindex]
if xdata == nil {
return nil, false
}
xdata := ydata[xindex]
ydata[xindex] = nil
v := xdata[xindex]
xdata[xindex] = nil
isnil := xdata != nil
isnil := v != nil
if isnil {
arr.sizes[yindex]--
if arr.sizes[yindex] == 0 {
arr.data[yindex] = nil
}
}
return xdata, isnil
return v, isnil
}

View File

@@ -8,7 +8,7 @@ import (
"github.com/Pallinder/go-randomdata"
)
func TestArray2Set(t *testing.T) {
func testSet1(t *testing.T) {
arr := NewWithCap(4, 4)
l := []int{0, 6, 5, 15}
for _, v := range l {
@@ -30,7 +30,34 @@ func TestArray2Set(t *testing.T) {
arr.Set(16, 16)
}
func TestArray2Get(t *testing.T) {
func testSet2(t *testing.T) {
arr := NewWithCap(3, 6)
l := []int{0, 6, 5, 15}
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]" {
t.Error(result)
}
defer func() {
if err := recover(); err == nil {
t.Error("err == nil, but array the set is out of range")
}
}()
arr.Set(16, 16)
}
func TestArray2Set(t *testing.T) {
testSet1(t)
testSet2(t)
}
func testArray2Get1(t *testing.T) {
arr := New()
for i := 0; i < 64; i++ {
arr.Set(i, i)
@@ -55,8 +82,38 @@ func TestArray2Get(t *testing.T) {
arr.Get(64)
}
func testArray2Get2(t *testing.T) {
arr := NewWithCap(9, 8)
for i := 0; i < 64; i++ {
arr.Set(i, i)
}
for i := 0; i < 64; i++ {
if v, ok := arr.Get(i); ok {
if v != i {
t.Error("v is equal i, but", v, i)
}
} else {
t.Error("not ok is error")
}
}
defer func() {
if err := recover(); err == nil {
t.Error("err == nil, but array the get is out of range")
}
}()
arr.Get(72)
}
func TestArray2Get(t *testing.T) {
testArray2Get1(t)
testArray2Get2(t)
}
func TestArray2Del(t *testing.T) {
arr := NewWithCap(4, 4)
arr := NewWithCap(3, 6)
l := []int{0, 6, 5, 15}
for _, v := range l {
arr.Set(v, v)
@@ -66,25 +123,27 @@ func TestArray2Del(t *testing.T) {
arr.Del(0)
result = spew.Sprint(arr.Values())
if result != "[<nil> <nil> <nil> <nil> {} 5 6 {} <nil> <nil> <nil> <nil> {} {} {} 15]" {
if result != "[{} {} {} {} {} 5 6 {} {} {} {} {} {} {} {} 15 {} {}]" {
t.Error(arr.data)
t.Error(result)
}
arr.Del(5)
result = spew.Sprint(arr.Values())
if result != "[<nil> <nil> <nil> <nil> {} {} 6 {} <nil> <nil> <nil> <nil> {} {} {} 15]" {
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())
if result != "[<nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> {} {} {} 15]" {
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())
if result != "[<nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil>]" {
if result != "[<nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil>]" {
t.Error(result)
}
@@ -94,7 +153,7 @@ func TestArray2Del(t *testing.T) {
}
}()
arr.Del(16)
arr.Del(18)
}
func BenchmarkArray2Set(b *testing.B) {