This commit is contained in:
aozhiwei 2023-09-15 08:25:59 +08:00
parent a784d12ab4
commit f0ba360bdc

View File

@ -8,9 +8,6 @@ type ListHead struct {
data interface{} data interface{}
} }
var dummyData = &struct {
}{}
func (this *ListHead) Init(data interface{}) { func (this *ListHead) Init(data interface{}) {
this.next = this this.next = this
this.prev = this this.prev = this
@ -47,11 +44,11 @@ func (this *ListHead) AddHead(pnew *ListHead) {
} }
func (this *ListHead) FirstEntry() interface{} { func (this *ListHead) FirstEntry() interface{} {
if this.next.data != dummyData { if !this.next.isCursor() {
return this.next.data return this.next.data
} }
for pos := this.next; pos != this; pos = pos.next { for pos := this.next; pos != this; pos = pos.next {
if pos.data != dummyData { if !pos.isCursor() {
return pos.data return pos.data
} }
} }
@ -75,11 +72,11 @@ func (this *ListHead) Empty() bool {
if this.next == this { if this.next == this {
return true return true
} }
if this.next.data != dummyData { if !this.next.isCursor() {
return false return false
} }
for pos := this.next; pos != this; pos = pos.next { for pos := this.next; pos != this; pos = pos.next {
if pos.data != dummyData { if !pos.isCursor() {
return false return false
} }
} }
@ -102,24 +99,23 @@ func (this *ListHead) Size() int32 {
return num return num
} }
func (this *ListHead) isCursor() bool {
return this.data == this
}
func (this *ListHead) ForEach(cb ListHead_Foreach_Func) { func (this *ListHead) ForEach(cb ListHead_Foreach_Func) {
cursor := ListHead{} cursor := ListHead{}
cursor.data = dummyData cursor.data = &cursor
this.AddHead(&cursor) this.AddHead(&cursor)
defer cursor.Del() defer cursor.Del()
for ; cursor.next != this; { for ; cursor.next != this; {
oldN := cursor.next oldN := cursor.next
if cursor.next.data != dummyData && !cb(cursor.next.data) { if !cursor.next.isCursor() && !cb(cursor.next.data) {
break break
} }
if cursor.next == oldN { if cursor.next == oldN {
oldN.Del() oldN.Del()
cursor.prev.AddHead(oldN)
oldN.next = &cursor
oldN.next.prev = oldN
oldN.prev = cursor.prev
oldN.prev.next = oldN
} }
} }
} }