132 lines
2.2 KiB
Go
132 lines
2.2 KiB
Go
package q5
|
|
|
|
type ListHead_Foreach_Func func(interface{}) bool
|
|
|
|
type ListHead struct {
|
|
next *ListHead
|
|
prev *ListHead
|
|
data interface{}
|
|
}
|
|
|
|
var dummyData = &struct {
|
|
}{}
|
|
|
|
func (this *ListHead) Init(data interface{}) {
|
|
this.next = this
|
|
this.prev = this
|
|
this.data = data
|
|
}
|
|
|
|
func (this *ListHead) GetData() interface{} {
|
|
return this.data
|
|
}
|
|
|
|
func (this *ListHead) Del() {
|
|
this.next.prev = this.prev
|
|
this.prev.next = this.next
|
|
}
|
|
|
|
func (this *ListHead) AddTail(pnew *ListHead) {
|
|
prev := this.prev
|
|
next := this
|
|
|
|
next.prev = pnew
|
|
pnew.next = next
|
|
pnew.prev = prev
|
|
prev.next = pnew
|
|
}
|
|
|
|
func (this *ListHead) AddHead(pnew *ListHead) {
|
|
prev := this
|
|
next := this.prev
|
|
|
|
next.prev = pnew
|
|
pnew.next = next
|
|
pnew.prev = prev
|
|
prev.next = pnew
|
|
}
|
|
|
|
func (this *ListHead) FirstEntry() interface{} {
|
|
if this.next.data != dummyData {
|
|
return this.next.data
|
|
}
|
|
for pos := this.next; pos != this; pos = pos.next {
|
|
if pos.data != dummyData {
|
|
return pos.data
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (this *ListHead) Replace(pnew *ListHead) {
|
|
pnew.next = this.next
|
|
pnew.next.prev = pnew
|
|
pnew.prev = this.prev
|
|
pnew.prev.next = pnew
|
|
}
|
|
|
|
func (this *ListHead) ReplaceInit(pnew *ListHead) {
|
|
this.Replace(pnew)
|
|
this.next = this
|
|
this.prev = this
|
|
}
|
|
|
|
func (this *ListHead) Empty() bool {
|
|
if this.next == this {
|
|
return true
|
|
}
|
|
if this.next.data != dummyData {
|
|
return false
|
|
}
|
|
for pos := this.next; pos != this; pos = pos.next {
|
|
if pos.data != dummyData {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
func (this *ListHead) DelInit() {
|
|
this.Del()
|
|
this.next = this
|
|
this.prev = this
|
|
}
|
|
|
|
func (this *ListHead) Size() int32 {
|
|
num := int32(0)
|
|
this.ForEach(
|
|
func (data interface{}) bool {
|
|
num++
|
|
return true
|
|
})
|
|
return num
|
|
}
|
|
|
|
func (this *ListHead) ForEach(cb ListHead_Foreach_Func) {
|
|
cursor := ListHead{}
|
|
cursor.data = dummyData
|
|
this.AddHead(&cursor)
|
|
defer cursor.Del()
|
|
for ; cursor.next != this; {
|
|
oldN := cursor.next
|
|
if cursor.next.data != dummyData && !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
|
|
}
|
|
}
|
|
}
|
|
|
|
func NewListHead () *ListHead {
|
|
l := new(ListHead)
|
|
l.Init(nil)
|
|
return l
|
|
}
|