修改iterator ToHead ToTail的实现, 和使用方法.

This commit is contained in:
huangsimin 2020-03-18 11:44:27 +08:00
parent 239cb3c189
commit b34134e22f
15 changed files with 248 additions and 37 deletions

View File

@ -15,7 +15,7 @@ import (
) )
func main() { func main() {
pq := pqueuekey.New(compare.Int) pq := New(compare.Int)
pq.Push(1, 1) pq.Push(1, 1)
pq.Push(4, 4) pq.Push(4, 4)
pq.Push(5, 5) pq.Push(5, 5)
@ -40,14 +40,15 @@ func main() {
log.Println(values3) log.Println(values3)
iter := pq.Iterator() // Next 大到小 从root节点起始 iter := pq.Iterator() // Next 大到小 从root节点起始
log.Println(pq.String())
// log.Println(iter.Value()) 直接使用会报错, // log.Println(iter.Value()) 直接使用会报错,
iter.ToHead() iter.ToHead()
log.Println(iter.Value()) // 起始最大值. true 5 iter.Next()
log.Println(iter.Value()) // 起始最大值. true 5
log.Println(iter.Prev(), iter.Value()) // false 5 log.Println(iter.Prev(), iter.Value()) // false 5
// Prev 大到小 // Prev 大到小
log.Println(iter.Next(), iter.Value()) // true 4 log.Println(iter.Next(), iter.Value()) // true 4
} }
``` ```

View File

@ -1,7 +1,7 @@
package pqueue package pqueue
import ( import (
"github.com/474420502/focus/stack/listarraystack" lastack "github.com/474420502/focus/stack/listarraystack"
) )
type Iterator struct { type Iterator struct {
@ -31,13 +31,36 @@ func NewIteratorWithCap(n *Node, cap int) *Iterator {
} }
func (iter *Iterator) ToHead() { 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() { 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 { func (iter *Iterator) GetNode() *Node {

View File

@ -283,13 +283,15 @@ func TestPriorityQueue_Iterator(t *testing.T) {
values := pq.Values() values := pq.Values()
for i := 0; ; i++ { for i := 0; ; i++ {
if !iter.Next() {
break
}
if values[i] != iter.Value() { if values[i] != iter.Value() {
t.Error(values[i], " != ", iter.Value()) t.Error(values[i], " != ", iter.Value())
} }
if !iter.Prev() {
break
}
} }
} }

View File

@ -31,13 +31,36 @@ func NewIteratorWithCap(n *Node, cap int) *Iterator {
} }
func (iter *Iterator) ToHead() { 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() { 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 { func (iter *Iterator) GetNode() *Node {

View File

@ -387,13 +387,15 @@ func TestPriorityQueue_Iterator(t *testing.T) {
values := pq.Values() values := pq.Values()
for i := 0; ; i++ { for i := 0; ; i++ {
if values[i] != iter.Value() {
t.Error(values[i], " != ", iter.Value())
}
if !iter.Next() { if !iter.Next() {
break 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(pq.String())
// log.Println(iter.Value()) 直接使用会报错, // log.Println(iter.Value()) 直接使用会报错,
iter.ToHead() iter.ToHead()
iter.Next()
log.Println(iter.Value()) // 起始最大值. true 5 log.Println(iter.Value()) // 起始最大值. true 5
log.Println(iter.Prev(), iter.Value()) // false 5 log.Println(iter.Prev(), iter.Value()) // false 5
// Prev 大到小 // Prev 大到小
log.Println(iter.Next(), iter.Value()) // true 4 log.Println(iter.Next(), iter.Value()) // true 4
} }

View File

@ -152,7 +152,14 @@ func TestTreeSet_Iterator(t *testing.T) {
iter := set.Iterator() iter := set.Iterator()
iter.ToHead() iter.ToHead()
// 3 4 5
// if not call Next Prev will error
// 5 4 3
// if iter.Value() != nil {
// t.Error(iter.Value())
// }
iter.Next()
if iter.Value() != 3 { if iter.Value() != 3 {
t.Error(iter.Value()) t.Error(iter.Value())
} }
@ -162,12 +169,8 @@ func TestTreeSet_Iterator(t *testing.T) {
t.Error(iter.Value()) t.Error(iter.Value())
} }
iter.Next()
if iter.Value() != 5 {
t.Error(iter.Value())
}
iter.ToTail() iter.ToTail()
iter.Prev()
if iter.Value() != 5 { if iter.Value() != 5 {
t.Error(iter.Value()) t.Error(iter.Value())
} }
@ -176,4 +179,16 @@ func TestTreeSet_Iterator(t *testing.T) {
if iter.Value() != 4 { if iter.Value() != 4 {
t.Error(iter.Value()) t.Error(iter.Value())
} }
iter.ToHead()
iter.Prev()
if iter.Value() != 3 {
t.Error(iter.Value())
}
iter.ToTail()
iter.Next()
if iter.Value() != 5 {
t.Error(iter.Value())
}
} }

View File

@ -49,11 +49,13 @@ func TestIteratorHeadTail(t *testing.T) {
} }
iter.ToHead() iter.ToHead()
iter.Prev() // or iter.Next
if iter.Value() != 1 { if iter.Value() != 1 {
t.Error("iter.Value() != ", 14, " value =", iter.Value()) t.Error("iter.Value() != ", 14, " value =", iter.Value())
} }
iter.ToTail() iter.ToTail()
iter.Next() // or iter.Prev()
if iter.Value() != 30 { if iter.Value() != 30 {
t.Error("iter.Value() != ", 30, " value =", iter.Value()) t.Error("iter.Value() != ", 30, " value =", iter.Value())
} }

View File

@ -1,7 +1,7 @@
package avl package avl
import ( import (
"github.com/474420502/focus/stack/listarraystack" lastack "github.com/474420502/focus/stack/listarraystack"
) )
type Iterator struct { type Iterator struct {
@ -31,13 +31,36 @@ func NewIteratorWithCap(n *Node, cap int) *Iterator {
} }
func (iter *Iterator) ToHead() { 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[0] != nil {
iter.cur = iter.cur.children[0]
}
iter.SetNode(iter.cur)
iter.cur = nil
} }
func (iter *Iterator) ToTail() { 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[1] != nil {
iter.cur = iter.cur.children[1]
}
iter.SetNode(iter.cur)
iter.cur = nil
} }
func (iter *Iterator) GetNode() *Node { func (iter *Iterator) GetNode() *Node {

View File

@ -1,7 +1,7 @@
package avldup package avldup
import ( import (
"github.com/474420502/focus/stack/listarraystack" lastack "github.com/474420502/focus/stack/listarraystack"
) )
type Iterator struct { type Iterator struct {
@ -31,13 +31,36 @@ func NewIteratorWithCap(n *Node, cap int) *Iterator {
} }
func (iter *Iterator) ToHead() { 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[0] != nil {
iter.cur = iter.cur.children[0]
}
iter.SetNode(iter.cur)
iter.cur = nil
} }
func (iter *Iterator) ToTail() { 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[1] != nil {
iter.cur = iter.cur.children[1]
}
iter.SetNode(iter.cur)
iter.cur = nil
} }
func (iter *Iterator) GetNode() *Node { func (iter *Iterator) GetNode() *Node {

View File

@ -31,13 +31,36 @@ func NewIteratorWithCap(n *Node, cap int) *Iterator {
} }
func (iter *Iterator) ToHead() { 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[0] != nil {
iter.cur = iter.cur.children[0]
}
iter.SetNode(iter.cur)
iter.cur = nil
} }
func (iter *Iterator) ToTail() { 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[1] != nil {
iter.cur = iter.cur.children[1]
}
iter.SetNode(iter.cur)
iter.cur = nil
} }
func (iter *Iterator) GetNode() *Node { func (iter *Iterator) GetNode() *Node {

View File

@ -31,13 +31,36 @@ func NewIteratorWithCap(n *Node, cap int) *Iterator {
} }
func (iter *Iterator) ToHead() { 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[0] != nil {
iter.cur = iter.cur.children[0]
}
iter.SetNode(iter.cur)
iter.cur = nil
} }
func (iter *Iterator) ToTail() { 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[1] != nil {
iter.cur = iter.cur.children[1]
}
iter.SetNode(iter.cur)
iter.cur = nil
} }
func (iter *Iterator) GetNode() *Node { func (iter *Iterator) GetNode() *Node {

View File

@ -31,13 +31,36 @@ func NewIteratorWithCap(n *Node, cap int) *Iterator {
} }
func (iter *Iterator) ToHead() { 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[0] != nil {
iter.cur = iter.cur.children[0]
}
iter.SetNode(iter.cur)
iter.cur = nil
} }
func (iter *Iterator) ToTail() { 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[1] != nil {
iter.cur = iter.cur.children[1]
}
iter.SetNode(iter.cur)
iter.cur = nil
} }
func (iter *Iterator) GetNode() *Node { func (iter *Iterator) GetNode() *Node {

View File

@ -47,6 +47,7 @@ func (iter *Iterator) ToHead() {
iter.cur = iter.cur.children[0] iter.cur = iter.cur.children[0]
} }
iter.SetNode(iter.cur) iter.SetNode(iter.cur)
iter.cur = nil
} }
func (iter *Iterator) ToTail() { func (iter *Iterator) ToTail() {
@ -63,6 +64,7 @@ func (iter *Iterator) ToTail() {
iter.cur = iter.cur.children[1] iter.cur = iter.cur.children[1]
} }
iter.SetNode(iter.cur) iter.SetNode(iter.cur)
iter.cur = nil
} }
func (iter *Iterator) SetNode(n *Node) { func (iter *Iterator) SetNode(n *Node) {

View File

@ -34,12 +34,13 @@ func TestIteratorHeadTail(t *testing.T) {
iter := tree.Iterator() iter := tree.Iterator()
iter.ToHead() // 从小到大 iter.ToHead() // 从小到大
iter.Next()
if iter.Value() != 1 { if iter.Value() != 1 {
t.Error("value error", iter.Value()) t.Error("value error", iter.Value())
} }
iter.ToTail() iter.ToTail()
iter.Prev()
if iter.Value() != 100 { if iter.Value() != 100 {
t.Error("value error", iter.Value()) t.Error("value error", iter.Value())
} }
@ -57,6 +58,7 @@ func TestIteratorHeadTail(t *testing.T) {
iter = tree.Iterator() iter = tree.Iterator()
iter.ToTail() iter.ToTail()
iter.Prev()
if iter.Value() != result[0] { if iter.Value() != result[0] {
t.Error("ToTail error", result, iter.Value()) t.Error("ToTail error", result, iter.Value())
} }
@ -64,6 +66,7 @@ func TestIteratorHeadTail(t *testing.T) {
result = tree.GetAround(-1) result = tree.GetAround(-1)
iter.ToHead() iter.ToHead()
iter.Next()
if iter.Value() != result[2] { if iter.Value() != result[2] {
t.Error("ToTail error", result, iter.Value()) t.Error("ToTail error", result, iter.Value())
} }

View File

@ -35,13 +35,36 @@ func (iter *Iterator) GetNode() *Node {
} }
func (iter *Iterator) ToHead() { 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[0] != nil {
iter.cur = iter.cur.children[0]
}
iter.SetNode(iter.cur)
iter.cur = nil
} }
func (iter *Iterator) ToTail() { 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[1] != nil {
iter.cur = iter.cur.children[1]
}
iter.SetNode(iter.cur)
iter.cur = nil
} }
func (iter *Iterator) SetNode(n *Node) { func (iter *Iterator) SetNode(n *Node) {