mirror of
https://github.com/TeaOSLab/EdgeNode.git
synced 2025-11-06 10:00:25 +08:00
优化链表相关代码
This commit is contained in:
@@ -36,6 +36,15 @@ func (this *List[T]) Push(item *Item[T]) {
|
|||||||
this.add(item)
|
this.add(item)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (this *List[T]) Shift() *Item[T] {
|
||||||
|
if this.head != nil {
|
||||||
|
var old = this.head
|
||||||
|
this.Remove(this.head)
|
||||||
|
return old
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (this *List[T]) Remove(item *Item[T]) {
|
func (this *List[T]) Remove(item *Item[T]) {
|
||||||
if item == nil {
|
if item == nil {
|
||||||
return
|
return
|
||||||
@@ -71,6 +80,15 @@ func (this *List[T]) Range(f func(item *Item[T]) (goNext bool)) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (this *List[T]) RangeReverse(f func(item *Item[T]) (goNext bool)) {
|
||||||
|
for e := this.end; e != nil; e = e.prev {
|
||||||
|
goNext := f(e)
|
||||||
|
if !goNext {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (this *List[T]) Reset() {
|
func (this *List[T]) Reset() {
|
||||||
this.head = nil
|
this.head = nil
|
||||||
this.end = nil
|
this.end = nil
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ package linkedlist_test
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/TeaOSLab/EdgeNode/internal/utils/linkedlist"
|
"github.com/TeaOSLab/EdgeNode/internal/utils/linkedlist"
|
||||||
|
"github.com/iwind/TeaGo/types"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strconv"
|
"strconv"
|
||||||
"testing"
|
"testing"
|
||||||
@@ -95,6 +96,48 @@ func TestList_Push(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestList_Shift(t *testing.T) {
|
||||||
|
var list = linkedlist.NewList[int]()
|
||||||
|
list.Push(linkedlist.NewItem(1))
|
||||||
|
list.Push(linkedlist.NewItem(2))
|
||||||
|
list.Push(linkedlist.NewItem(3))
|
||||||
|
list.Push(linkedlist.NewItem(4))
|
||||||
|
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
t.Log("=== before shift " + types.String(i) + " ===")
|
||||||
|
list.Range(func(item *linkedlist.Item[int]) (goNext bool) {
|
||||||
|
t.Log(item.Value)
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Logf("shift: %+v", list.Shift())
|
||||||
|
|
||||||
|
t.Log("=== after shift " + types.String(i) + " ===")
|
||||||
|
list.Range(func(item *linkedlist.Item[int]) (goNext bool) {
|
||||||
|
t.Log(item.Value)
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestList_RangeReverse(t *testing.T) {
|
||||||
|
var list = linkedlist.NewList[int]()
|
||||||
|
list.Push(linkedlist.NewItem(1))
|
||||||
|
list.Push(linkedlist.NewItem(2))
|
||||||
|
|
||||||
|
var item3 = linkedlist.NewItem(3)
|
||||||
|
list.Push(item3)
|
||||||
|
|
||||||
|
list.Push(linkedlist.NewItem(4))
|
||||||
|
|
||||||
|
//list.Push(item3)
|
||||||
|
//list.Remove(item3)
|
||||||
|
list.RangeReverse(func(item *linkedlist.Item[int]) (goNext bool) {
|
||||||
|
t.Log(item.Value)
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func BenchmarkList_Add(b *testing.B) {
|
func BenchmarkList_Add(b *testing.B) {
|
||||||
var list = linkedlist.NewList[int]()
|
var list = linkedlist.NewList[int]()
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
|
|||||||
Reference in New Issue
Block a user