arraylist remove 测试完成
This commit is contained in:
parent
fcf81f45a0
commit
089acbcc34
|
@ -47,7 +47,7 @@ func (l *ArrayList) Size() uint {
|
||||||
func (l *ArrayList) shrink() {
|
func (l *ArrayList) shrink() {
|
||||||
|
|
||||||
if l.size <= listMinLimit {
|
if l.size <= listMinLimit {
|
||||||
log.Panic("list size is over listMaxLimit", listMinLimit)
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if l.size <= l.shrinkSize {
|
if l.size <= l.shrinkSize {
|
||||||
|
@ -137,7 +137,7 @@ func (l *ArrayList) Index(idx uint) (interface{}, bool) {
|
||||||
|
|
||||||
func (l *ArrayList) Remove(idx uint) (result interface{}, isfound bool) {
|
func (l *ArrayList) Remove(idx uint) (result interface{}, isfound bool) {
|
||||||
|
|
||||||
if idx < l.size {
|
if idx >= l.size {
|
||||||
return nil, false
|
return nil, false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -145,9 +145,16 @@ func (l *ArrayList) Remove(idx uint) (result interface{}, isfound bool) {
|
||||||
|
|
||||||
isfound = true
|
isfound = true
|
||||||
result = l.data[offset]
|
result = l.data[offset]
|
||||||
l.data[offset] = nil // cleanup reference
|
// l.data[offset] = nil // cleanup reference
|
||||||
|
|
||||||
|
if l.size-l.tidx > l.hidx {
|
||||||
|
copy(l.data[offset:], l.data[offset+1:l.tidx]) // shift to the left by one (slow operation, need ways to optimize this)
|
||||||
|
l.tidx--
|
||||||
|
} else {
|
||||||
|
copy(l.data[l.hidx+2:], l.data[l.hidx+1:offset])
|
||||||
|
l.hidx++
|
||||||
|
}
|
||||||
|
|
||||||
copy(l.data[offset:], l.data[idx+1:l.size]) // shift to the left by one (slow operation, need ways to optimize this)
|
|
||||||
l.size--
|
l.size--
|
||||||
l.shrink()
|
l.shrink()
|
||||||
|
|
||||||
|
|
|
@ -95,3 +95,49 @@ func TestPop(t *testing.T) {
|
||||||
t.Error("should not be ok, v = ", v)
|
t.Error("should not be ok, v = ", v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestRemove(t *testing.T) {
|
||||||
|
l := New()
|
||||||
|
for i := 0; i < 5; i++ {
|
||||||
|
l.PushFront(uint(i))
|
||||||
|
}
|
||||||
|
|
||||||
|
var result string
|
||||||
|
|
||||||
|
for _, selval := range []uint{4, 3} {
|
||||||
|
last, _ := l.Index(selval)
|
||||||
|
if v, isfound := l.Remove(selval); isfound {
|
||||||
|
if v != last {
|
||||||
|
t.Error(v, " != ", last)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
t.Error("should be found")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
result = spew.Sprint(l.Values())
|
||||||
|
if result != "[4 3 2]" {
|
||||||
|
t.Error("should be [4 3 2], value =", result)
|
||||||
|
}
|
||||||
|
|
||||||
|
v, _ := l.Remove(1)
|
||||||
|
if v != uint(3) {
|
||||||
|
t.Error(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
v, _ = l.Remove(1)
|
||||||
|
if v != uint(2) {
|
||||||
|
t.Error(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
v, _ = l.Remove(1)
|
||||||
|
if v != nil && l.Size() != 1 {
|
||||||
|
t.Error(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
v, _ = l.Remove(0)
|
||||||
|
if v != uint(4) && l.Size() != 0 {
|
||||||
|
t.Error(v, "size = ", l.Size())
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user