优化代码

This commit is contained in:
GoEdgeLab
2023-10-03 21:38:45 +08:00
parent b2c6c3cc07
commit a7856c1c88
6 changed files with 46 additions and 41 deletions

View File

@@ -2,13 +2,13 @@
package linkedlist
type Item struct {
prev *Item
next *Item
type Item[T any] struct {
prev *Item[T]
next *Item[T]
Value interface{}
Value T
}
func NewItem(value interface{}) *Item {
return &Item{Value: value}
func NewItem[T any](value T) *Item[T] {
return &Item[T]{Value: value}
}

View File

@@ -2,25 +2,25 @@
package linkedlist
type List struct {
head *Item
end *Item
type List[T any] struct {
head *Item[T]
end *Item[T]
count int
}
func NewList() *List {
return &List{}
func NewList[T any]() *List[T] {
return &List[T]{}
}
func (this *List) Head() *Item {
func (this *List[T]) Head() *Item[T] {
return this.head
}
func (this *List) End() *Item {
func (this *List[T]) End() *Item[T] {
return this.end
}
func (this *List) Push(item *Item) {
func (this *List[T]) Push(item *Item[T]) {
if item == nil {
return
}
@@ -36,7 +36,7 @@ func (this *List) Push(item *Item) {
this.add(item)
}
func (this *List) Remove(item *Item) {
func (this *List[T]) Remove(item *Item[T]) {
if item == nil {
return
}
@@ -58,11 +58,11 @@ func (this *List) Remove(item *Item) {
this.count--
}
func (this *List) Len() int {
func (this *List[T]) Len() int {
return this.count
}
func (this *List) Range(f func(item *Item) (goNext bool)) {
func (this *List[T]) Range(f func(item *Item[T]) (goNext bool)) {
for e := this.head; e != nil; e = e.next {
goNext := f(e)
if !goNext {
@@ -71,12 +71,12 @@ func (this *List) Range(f func(item *Item) (goNext bool)) {
}
}
func (this *List) Reset() {
func (this *List[T]) Reset() {
this.head = nil
this.end = nil
}
func (this *List) add(item *Item) {
func (this *List[T]) add(item *Item[T]) {
if item == nil {
return
}

View File

@@ -12,19 +12,21 @@ func TestNewList_Memory(t *testing.T) {
var stat1 = &runtime.MemStats{}
runtime.ReadMemStats(stat1)
var list = linkedlist.NewList()
var list = linkedlist.NewList[int]()
for i := 0; i < 1_000_000; i++ {
var item = &linkedlist.Item{}
var item = &linkedlist.Item[int]{}
list.Push(item)
}
runtime.GC()
var stat2 = &runtime.MemStats{}
runtime.ReadMemStats(stat2)
t.Log((stat2.HeapInuse-stat1.HeapInuse)/1024/1024, "MB")
t.Log((stat2.HeapInuse-stat1.HeapInuse)>>20, "MB")
t.Log(list.Len())
var count = 0
list.Range(func(item *linkedlist.Item) (goNext bool) {
list.Range(func(item *linkedlist.Item[int]) (goNext bool) {
count++
return true
})
@@ -32,7 +34,7 @@ func TestNewList_Memory(t *testing.T) {
}
func TestList_Push(t *testing.T) {
var list = linkedlist.NewList()
var list = linkedlist.NewList[int]()
list.Push(linkedlist.NewItem(1))
list.Push(linkedlist.NewItem(2))
@@ -41,42 +43,43 @@ func TestList_Push(t *testing.T) {
var item4 = linkedlist.NewItem(4)
list.Push(item4)
list.Range(func(item *linkedlist.Item) (goNext bool) {
list.Range(func(item *linkedlist.Item[int]) (goNext bool) {
t.Log(item.Value)
return true
})
t.Log("=== after push3 ===")
t.Log("=== after push 3 ===")
list.Push(item3)
list.Range(func(item *linkedlist.Item) (goNext bool) {
list.Range(func(item *linkedlist.Item[int]) (goNext bool) {
t.Log(item.Value)
return true
})
t.Log("=== after push4 ===")
t.Log("=== after push 4 ===")
list.Push(item4)
list.Push(item3)
list.Push(item3)
list.Push(item3)
list.Push(item4)
list.Push(item4)
list.Range(func(item *linkedlist.Item) (goNext bool) {
list.Range(func(item *linkedlist.Item[int]) (goNext bool) {
t.Log(item.Value)
return true
})
t.Log("=== after remove ===")
t.Log("=== after remove 3 ===")
list.Remove(item3)
list.Range(func(item *linkedlist.Item) (goNext bool) {
list.Range(func(item *linkedlist.Item[int]) (goNext bool) {
t.Log(item.Value)
return true
})
}
func BenchmarkList_Add(b *testing.B) {
var list = linkedlist.NewList()
var list = linkedlist.NewList[int]()
for i := 0; i < b.N; i++ {
var item = &linkedlist.Item{}
var item = &linkedlist.Item[int]{}
list.Push(item)
}
}