发现已经做了位置处理, 不需要再去改名
This commit is contained in:
@@ -30,6 +30,16 @@ func NewIteratorWithCap(n *Node, cap int) *Iterator {
|
||||
return iter
|
||||
}
|
||||
|
||||
func (iter *Iterator) ToHead() {
|
||||
for iter.Prev() {
|
||||
}
|
||||
}
|
||||
|
||||
func (iter *Iterator) ToTail() {
|
||||
for iter.Next() {
|
||||
}
|
||||
}
|
||||
|
||||
func (iter *Iterator) GetNode() *Node {
|
||||
return iter.cur
|
||||
}
|
||||
@@ -44,25 +54,7 @@ func (iter *Iterator) Value() interface{} {
|
||||
return iter.cur.value
|
||||
}
|
||||
|
||||
func (iter *Iterator) Left() bool {
|
||||
if iter.cur.children[0] != nil {
|
||||
iter.dir = 0
|
||||
iter.cur = iter.cur.children[0]
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (iter *Iterator) Right() bool {
|
||||
if iter.cur.children[1] != nil {
|
||||
iter.dir = 0
|
||||
iter.cur = iter.cur.children[1]
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (iter *Iterator) GetNext(cur *Node, idx int) *Node {
|
||||
func (iter *Iterator) GetPrev(cur *Node, idx int) *Node {
|
||||
|
||||
// iter := NewIterator(cur)
|
||||
iter.SetNode(cur)
|
||||
@@ -93,7 +85,7 @@ func (iter *Iterator) GetNext(cur *Node, idx int) *Node {
|
||||
return cur
|
||||
}
|
||||
|
||||
func (iter *Iterator) Next() (result bool) {
|
||||
func (iter *Iterator) Prev() (result bool) {
|
||||
|
||||
if iter.dir > -1 {
|
||||
if iter.dir == 1 && iter.cur != nil {
|
||||
@@ -121,7 +113,7 @@ func (iter *Iterator) Next() (result bool) {
|
||||
return false
|
||||
}
|
||||
|
||||
func (iter *Iterator) GetPrev(cur *Node, idx int) *Node {
|
||||
func (iter *Iterator) GetNext(cur *Node, idx int) *Node {
|
||||
|
||||
// iter := NewIterator(cur)
|
||||
iter.SetNode(cur)
|
||||
@@ -152,7 +144,7 @@ func (iter *Iterator) GetPrev(cur *Node, idx int) *Node {
|
||||
return cur
|
||||
}
|
||||
|
||||
func (iter *Iterator) Prev() (result bool) {
|
||||
func (iter *Iterator) Next() (result bool) {
|
||||
|
||||
if iter.dir < 1 { // 非 1(next 方向定义 -1 为 prev)
|
||||
if iter.dir == -1 && iter.cur != nil { // 如果上次为prev方向, 则清空辅助计算的栈
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
package pqueue
|
||||
|
||||
import "github.com/474420502/focus/compare"
|
||||
import (
|
||||
"github.com/474420502/focus/compare"
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
)
|
||||
|
||||
type PriorityQueue struct {
|
||||
queue *vbTree
|
||||
@@ -78,3 +81,7 @@ func (pq *PriorityQueue) Remove(key interface{}) (interface{}, bool) {
|
||||
func (pq *PriorityQueue) Values() []interface{} {
|
||||
return pq.queue.Values()
|
||||
}
|
||||
|
||||
func (pq *PriorityQueue) String() string {
|
||||
return spew.Sprint(pq.queue.Values())
|
||||
}
|
||||
|
||||
@@ -242,6 +242,35 @@ func TestQueueIndex(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestPriorityQueue_Iterator(t *testing.T) {
|
||||
pq := New(compare.Int)
|
||||
for i := 0; i < 5; i++ {
|
||||
pq.Push(i)
|
||||
}
|
||||
|
||||
pq.Push(-1)
|
||||
pq.Push(10)
|
||||
|
||||
result := pq.String()
|
||||
if result != "[10 4 3 2 1 0 -1]" {
|
||||
t.Error("should be [10 4 3 2 1 0 -1]")
|
||||
}
|
||||
|
||||
iter := pq.Iterator()
|
||||
iter.ToHead()
|
||||
|
||||
values := pq.Values()
|
||||
for i := 0; ; i++ {
|
||||
if values[i] != iter.Value() {
|
||||
t.Error(values[i], " != ", iter.Value())
|
||||
}
|
||||
|
||||
if !iter.Next() {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// func BenchmarkQueueGet(b *testing.B) {
|
||||
|
||||
// l := loadTestData()
|
||||
|
||||
Reference in New Issue
Block a user