修改iterator ToHead ToTail的实现, 和使用方法.
This commit is contained in:
@@ -31,13 +31,36 @@ func NewIteratorWithCap(n *Node, cap int) *Iterator {
|
||||
}
|
||||
|
||||
func (iter *Iterator) ToHead() {
|
||||
for iter.Prev() {
|
||||
if iter.cur == nil {
|
||||
iter.cur = iter.up
|
||||
}
|
||||
|
||||
for iter.cur.parent != nil {
|
||||
iter.cur = iter.cur.parent
|
||||
}
|
||||
|
||||
for iter.cur.children[1] != nil {
|
||||
iter.cur = iter.cur.children[1]
|
||||
}
|
||||
iter.SetNode(iter.cur)
|
||||
iter.cur = nil
|
||||
}
|
||||
|
||||
func (iter *Iterator) ToTail() {
|
||||
for iter.Next() {
|
||||
|
||||
if iter.cur == nil {
|
||||
iter.cur = iter.up
|
||||
}
|
||||
|
||||
for iter.cur.parent != nil {
|
||||
iter.cur = iter.cur.parent
|
||||
}
|
||||
|
||||
for iter.cur.children[0] != nil {
|
||||
iter.cur = iter.cur.children[0]
|
||||
}
|
||||
iter.SetNode(iter.cur)
|
||||
iter.cur = nil
|
||||
}
|
||||
|
||||
func (iter *Iterator) GetNode() *Node {
|
||||
|
||||
@@ -387,13 +387,15 @@ func TestPriorityQueue_Iterator(t *testing.T) {
|
||||
|
||||
values := pq.Values()
|
||||
for i := 0; ; i++ {
|
||||
if values[i] != iter.Value() {
|
||||
t.Error(values[i], " != ", iter.Value())
|
||||
}
|
||||
|
||||
if !iter.Next() {
|
||||
break
|
||||
}
|
||||
|
||||
if values[i] != iter.Value() {
|
||||
t.Error(values[i], " != ", iter.Value())
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -481,10 +483,10 @@ func TestMain(t *testing.T) {
|
||||
log.Println(pq.String())
|
||||
// log.Println(iter.Value()) 直接使用会报错,
|
||||
iter.ToHead()
|
||||
iter.Next()
|
||||
log.Println(iter.Value()) // 起始最大值. true 5
|
||||
log.Println(iter.Prev(), iter.Value()) // false 5
|
||||
|
||||
// Prev 大到小
|
||||
log.Println(iter.Next(), iter.Value()) // true 4
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user