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