修改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

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

View File

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