ll完成
This commit is contained in:
parent
8d5b098286
commit
a02f238ff1
|
@ -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))
|
panic(fmt.Sprintf("out of list range, size is %d, idx is %d", l.size, idx))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
l.size--
|
||||||
if idx > l.size/2 {
|
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 {
|
for cur := l.tail.prev; cur != nil; cur = cur.prev {
|
||||||
if idx == 0 {
|
if idx == 0 {
|
||||||
|
|
|
@ -175,10 +175,36 @@ func TestIndex(t *testing.T) {
|
||||||
if _, ok := l.Index(5); ok {
|
if _, ok := l.Index(5); ok {
|
||||||
t.Error("[4 3 2 1 0] Index 5, out of range,ok = true is error")
|
t.Error("[4 3 2 1 0] Index 5, out of range,ok = true is error")
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRemove(t *testing.T) {
|
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) {
|
func BenchmarkPushBack(b *testing.B) {
|
||||||
|
|
9
utils/math.go
Normal file
9
utils/math.go
Normal 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)
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user