focus/list/priority_list/iterator.go

88 lines
1.4 KiB
Go
Raw Normal View History

2019-05-10 10:01:15 +00:00
package plist
type Iterator struct {
pl *PriorityList
cur *Node
}
func (iter *Iterator) Value() interface{} {
return iter.cur.value
}
func (iter *Iterator) Prev() bool {
if iter.cur == iter.pl.head {
return false
}
iter.cur = iter.cur.prev
return iter.cur != iter.pl.head
}
func (iter *Iterator) Next() bool {
if iter.cur == iter.pl.tail {
return false
}
iter.cur = iter.cur.next
return iter.cur != iter.pl.tail
}
func (iter *Iterator) ToHead() {
2019-05-10 10:01:15 +00:00
iter.cur = iter.pl.head
}
func (iter *Iterator) ToTail() {
2019-05-10 10:01:15 +00:00
iter.cur = iter.pl.tail
}
type CircularIterator struct {
pl *PriorityList
cur *Node
}
func (iter *CircularIterator) Value() interface{} {
return iter.cur.value
}
func (iter *CircularIterator) Prev() bool {
if iter.pl.size == 0 {
return false
}
if iter.cur == iter.pl.head {
iter.cur = iter.pl.tail.prev
return true
}
iter.cur = iter.cur.prev
if iter.cur == iter.pl.head {
iter.cur = iter.pl.tail.prev
}
return true
}
func (iter *CircularIterator) Next() bool {
if iter.pl.size == 0 {
return false
}
if iter.cur == iter.pl.tail {
iter.cur = iter.pl.head.next
return true
}
iter.cur = iter.cur.next
if iter.cur == iter.pl.tail {
iter.cur = iter.pl.head.next
}
return true
}
func (iter *CircularIterator) ToHead() {
2019-05-10 10:01:15 +00:00
iter.cur = iter.pl.head
}
func (iter *CircularIterator) ToTail() {
2019-05-10 10:01:15 +00:00
iter.cur = iter.pl.tail
}