This commit is contained in:
huangsimin 2019-05-20 19:12:38 +08:00
parent 8d5b098286
commit a02f238ff1
3 changed files with 37 additions and 1 deletions

View File

@ -265,8 +265,9 @@ func (l *LinkedList) Remove(idx uint) (interface{}, bool) {
panic(fmt.Sprintf("out of list range, size is %d, idx is %d", l.size, idx))
}
l.size--
if idx > l.size/2 {
idx = l.size - 1 - idx
idx = l.size - idx // l.size - 1 - idx
// 尾部
for cur := l.tail.prev; cur != nil; cur = cur.prev {
if idx == 0 {

View File

@ -175,10 +175,36 @@ func TestIndex(t *testing.T) {
if _, ok := l.Index(5); ok {
t.Error("[4 3 2 1 0] Index 5, out of range,ok = true is error")
}
}
func TestRemove(t *testing.T) {
l := New()
// "[4 3 2 1 0]"
for i := 0; i < 5; i++ {
l.PushFront(i)
}
l.Remove(0)
var result string
result = spew.Sprint(l.Values())
if result != "[3 2 1 0]" {
t.Error("should be [3 2 1 0] but result is", result)
}
l.Remove(3)
result = spew.Sprint(l.Values())
if result != "[3 2 1]" {
t.Error("should be [3 2 1] but result is", result)
}
defer func() {
if err := recover(); err == nil {
t.Error("should be out of range but is not")
}
}()
l.Remove(3)
}
func BenchmarkPushBack(b *testing.B) {

9
utils/math.go Normal file
View File

@ -0,0 +1,9 @@
package utils
var bit = uint(32 << (^uint(0) >> 63))
var bitsub1 = bit - 1
func AbsInt(n int) uint {
y := n >> bitsub1
return uint((n ^ y) - y)
}