From b34134e22fcb4c77a08f91208d3a0f6b74acd940 Mon Sep 17 00:00:00 2001 From: huangsimin Date: Wed, 18 Mar 2020 11:44:27 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9iterator=20ToHead=20ToTail?= =?UTF-8?q?=E7=9A=84=E5=AE=9E=E7=8E=B0,=20=E5=92=8C=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E6=96=B9=E6=B3=95.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 9 ++++--- priority_queue/iterator.go | 29 ++++++++++++++++++--- priority_queue/priority_queue_test.go | 8 +++--- priority_queuekey/iterator.go | 27 +++++++++++++++++-- priority_queuekey/priority_queuekey_test.go | 10 ++++--- set/treeset/treeset_test.go | 27 ++++++++++++++----- tree/avl/avl_test.go | 2 ++ tree/avl/iterator.go | 29 ++++++++++++++++++--- tree/avldup/iterator.go | 29 ++++++++++++++++++--- tree/avlkey/iterator.go | 27 +++++++++++++++++-- tree/avlkeydup/iterator.go | 27 +++++++++++++++++-- tree/vbt/iterator.go | 27 +++++++++++++++++-- tree/vbtdupkey/iterator.go | 2 ++ tree/vbtdupkey/vbtdupkey_test.go | 5 +++- tree/vbtkey/iterator.go | 27 +++++++++++++++++-- 15 files changed, 248 insertions(+), 37 deletions(-) diff --git a/README.md b/README.md index 812a477..90e688b 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ import ( ) func main() { - pq := pqueuekey.New(compare.Int) + pq := New(compare.Int) pq.Push(1, 1) pq.Push(4, 4) pq.Push(5, 5) @@ -40,14 +40,15 @@ func main() { log.Println(values3) iter := pq.Iterator() // Next 大到小 从root节点起始 + log.Println(pq.String()) // log.Println(iter.Value()) 直接使用会报错, iter.ToHead() - log.Println(iter.Value()) // 起始最大值. true 5 + 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 - - + } ``` diff --git a/priority_queue/iterator.go b/priority_queue/iterator.go index 1f6efc4..ecc74e7 100644 --- a/priority_queue/iterator.go +++ b/priority_queue/iterator.go @@ -1,7 +1,7 @@ package pqueue import ( - "github.com/474420502/focus/stack/listarraystack" + lastack "github.com/474420502/focus/stack/listarraystack" ) type Iterator struct { @@ -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 { diff --git a/priority_queue/priority_queue_test.go b/priority_queue/priority_queue_test.go index 2375e59..56c02ad 100644 --- a/priority_queue/priority_queue_test.go +++ b/priority_queue/priority_queue_test.go @@ -283,13 +283,15 @@ func TestPriorityQueue_Iterator(t *testing.T) { values := pq.Values() for i := 0; ; i++ { + + if !iter.Next() { + break + } + if values[i] != iter.Value() { t.Error(values[i], " != ", iter.Value()) } - if !iter.Prev() { - break - } } } diff --git a/priority_queuekey/iterator.go b/priority_queuekey/iterator.go index 2174357..94a3215 100644 --- a/priority_queuekey/iterator.go +++ b/priority_queuekey/iterator.go @@ -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 { diff --git a/priority_queuekey/priority_queuekey_test.go b/priority_queuekey/priority_queuekey_test.go index ce96b71..f26c10d 100644 --- a/priority_queuekey/priority_queuekey_test.go +++ b/priority_queuekey/priority_queuekey_test.go @@ -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 - } diff --git a/set/treeset/treeset_test.go b/set/treeset/treeset_test.go index 5c2254a..0bf5312 100644 --- a/set/treeset/treeset_test.go +++ b/set/treeset/treeset_test.go @@ -152,7 +152,14 @@ func TestTreeSet_Iterator(t *testing.T) { iter := set.Iterator() 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 { t.Error(iter.Value()) } @@ -162,12 +169,8 @@ func TestTreeSet_Iterator(t *testing.T) { t.Error(iter.Value()) } - iter.Next() - if iter.Value() != 5 { - t.Error(iter.Value()) - } - iter.ToTail() + iter.Prev() if iter.Value() != 5 { t.Error(iter.Value()) } @@ -176,4 +179,16 @@ func TestTreeSet_Iterator(t *testing.T) { if iter.Value() != 4 { 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()) + } } diff --git a/tree/avl/avl_test.go b/tree/avl/avl_test.go index 0192966..e86abed 100644 --- a/tree/avl/avl_test.go +++ b/tree/avl/avl_test.go @@ -49,11 +49,13 @@ func TestIteratorHeadTail(t *testing.T) { } iter.ToHead() + iter.Prev() // or iter.Next if iter.Value() != 1 { t.Error("iter.Value() != ", 14, " value =", iter.Value()) } iter.ToTail() + iter.Next() // or iter.Prev() if iter.Value() != 30 { t.Error("iter.Value() != ", 30, " value =", iter.Value()) } diff --git a/tree/avl/iterator.go b/tree/avl/iterator.go index 926c60a..41f832c 100644 --- a/tree/avl/iterator.go +++ b/tree/avl/iterator.go @@ -1,7 +1,7 @@ package avl import ( - "github.com/474420502/focus/stack/listarraystack" + lastack "github.com/474420502/focus/stack/listarraystack" ) type Iterator struct { @@ -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[0] != nil { + iter.cur = iter.cur.children[0] + } + 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[1] != nil { + iter.cur = iter.cur.children[1] + } + iter.SetNode(iter.cur) + iter.cur = nil } func (iter *Iterator) GetNode() *Node { diff --git a/tree/avldup/iterator.go b/tree/avldup/iterator.go index d802dce..864fae2 100644 --- a/tree/avldup/iterator.go +++ b/tree/avldup/iterator.go @@ -1,7 +1,7 @@ package avldup import ( - "github.com/474420502/focus/stack/listarraystack" + lastack "github.com/474420502/focus/stack/listarraystack" ) type Iterator struct { @@ -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[0] != nil { + iter.cur = iter.cur.children[0] + } + 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[1] != nil { + iter.cur = iter.cur.children[1] + } + iter.SetNode(iter.cur) + iter.cur = nil } func (iter *Iterator) GetNode() *Node { diff --git a/tree/avlkey/iterator.go b/tree/avlkey/iterator.go index 943fa05..b74ba8d 100644 --- a/tree/avlkey/iterator.go +++ b/tree/avlkey/iterator.go @@ -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[0] != nil { + iter.cur = iter.cur.children[0] + } + 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[1] != nil { + iter.cur = iter.cur.children[1] + } + iter.SetNode(iter.cur) + iter.cur = nil } func (iter *Iterator) GetNode() *Node { diff --git a/tree/avlkeydup/iterator.go b/tree/avlkeydup/iterator.go index 3c897b0..c7b054c 100644 --- a/tree/avlkeydup/iterator.go +++ b/tree/avlkeydup/iterator.go @@ -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[0] != nil { + iter.cur = iter.cur.children[0] + } + 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[1] != nil { + iter.cur = iter.cur.children[1] + } + iter.SetNode(iter.cur) + iter.cur = nil } func (iter *Iterator) GetNode() *Node { diff --git a/tree/vbt/iterator.go b/tree/vbt/iterator.go index 1bedc99..8a38ac6 100644 --- a/tree/vbt/iterator.go +++ b/tree/vbt/iterator.go @@ -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[0] != nil { + iter.cur = iter.cur.children[0] + } + 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[1] != nil { + iter.cur = iter.cur.children[1] + } + iter.SetNode(iter.cur) + iter.cur = nil } func (iter *Iterator) GetNode() *Node { diff --git a/tree/vbtdupkey/iterator.go b/tree/vbtdupkey/iterator.go index 5d545a6..afafeb8 100644 --- a/tree/vbtdupkey/iterator.go +++ b/tree/vbtdupkey/iterator.go @@ -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) { diff --git a/tree/vbtdupkey/vbtdupkey_test.go b/tree/vbtdupkey/vbtdupkey_test.go index a584349..22f7166 100644 --- a/tree/vbtdupkey/vbtdupkey_test.go +++ b/tree/vbtdupkey/vbtdupkey_test.go @@ -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()) } diff --git a/tree/vbtkey/iterator.go b/tree/vbtkey/iterator.go index 556eb4a..28f5327 100644 --- a/tree/vbtkey/iterator.go +++ b/tree/vbtkey/iterator.go @@ -35,13 +35,36 @@ func (iter *Iterator) GetNode() *Node { } 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() { - 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) {