mirror of
				https://github.com/TeaOSLab/EdgeNode.git
				synced 2025-11-04 07:40:56 +08:00 
			
		
		
		
	优化链表相关代码
This commit is contained in:
		@@ -2,7 +2,7 @@
 | 
			
		||||
 | 
			
		||||
package linkedlist
 | 
			
		||||
 | 
			
		||||
type List[T any]  struct {
 | 
			
		||||
type List[T any] struct {
 | 
			
		||||
	head  *Item[T]
 | 
			
		||||
	end   *Item[T]
 | 
			
		||||
	count int
 | 
			
		||||
@@ -36,6 +36,15 @@ func (this *List[T]) Push(item *Item[T]) {
 | 
			
		||||
	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]) {
 | 
			
		||||
	if item == nil {
 | 
			
		||||
		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() {
 | 
			
		||||
	this.head = nil
 | 
			
		||||
	this.end = nil
 | 
			
		||||
 
 | 
			
		||||
@@ -4,6 +4,7 @@ package linkedlist_test
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"github.com/TeaOSLab/EdgeNode/internal/utils/linkedlist"
 | 
			
		||||
	"github.com/iwind/TeaGo/types"
 | 
			
		||||
	"runtime"
 | 
			
		||||
	"strconv"
 | 
			
		||||
	"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) {
 | 
			
		||||
	var list = linkedlist.NewList[int]()
 | 
			
		||||
	for i := 0; i < b.N; i++ {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user