From 60c1442dca2d00a5b75e1840fdf4521d2dfcf00c Mon Sep 17 00:00:00 2001 From: eson <474420502@qq.com> Date: Mon, 22 Jul 2019 03:28:27 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95=E9=80=9A?= =?UTF-8?q?=E8=BF=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- priority_queue/priority_queue_test.go | 57 ++++++++++++++++++++- priority_queue/vbt.go | 32 ++++++------ priority_queuekey/priority_queuekey_test.go | 55 ++++++++++++++++++++ priority_queuekey/vbt.go | 2 +- 4 files changed, 128 insertions(+), 18 deletions(-) diff --git a/priority_queue/priority_queue_test.go b/priority_queue/priority_queue_test.go index b168708..d096bdf 100644 --- a/priority_queue/priority_queue_test.go +++ b/priority_queue/priority_queue_test.go @@ -265,12 +265,67 @@ func TestPriorityQueue_Iterator(t *testing.T) { t.Error(values[i], " != ", iter.Value()) } - if !iter.Next() { + if !iter.Prev() { break } } } +func TestPriorityQueue_Iterator2(t *testing.T) { + pq := New(compare.Int) + for i := 0; i < 5; i++ { + pq.Push(i) + } + + iter := pq.Iterator() + iter.ToHead() + + n, _ := pq.IndexNode(0) + if n.value != 4 { + t.Error(n) + } + + if v, _ := pq.Top(); v != 4 { + t.Error("Top != 4, and is ", v) + } + + if v := iter.GetNext(n, 2).value; v != 2 { + t.Error("iter.GetNext(n, 2) != 2, and is ", v) + } + + pq = New(compare.Int) + for i := 100; i >= 0; i-- { + pq.Push(i) + } + if v, _ := pq.Top(); v != 100 { + t.Error("Top != 100, and is ", v) + } + + for pq.Size() >= 50 { + pq.Pop() + } + + if v, _ := pq.Top(); v != 48 { + t.Error("Top != 48, and is ", v) + } + + pq = New(compare.Int) + for i := 0; i < 100; i++ { + pq.Push(i) + } + if v, _ := pq.Top(); v != 99 { + t.Error("Top != 99, and is ", v) + } + + for pq.Size() >= 50 { + pq.Pop() + } + + if v, _ := pq.Top(); v != 48 { + t.Error("Top != 49, and is ", v) + } +} + // func BenchmarkQueueGet(b *testing.B) { // l := loadTestData() diff --git a/priority_queue/vbt.go b/priority_queue/vbt.go index 62d51bd..c9bcd78 100644 --- a/priority_queue/vbt.go +++ b/priority_queue/vbt.go @@ -118,7 +118,7 @@ func (tree *vbTree) IndexRange(idx1, idx2 int) (result []interface{}, ok bool) { iter := tree.iter result = make([]interface{}, 0, idx1-idx2) for i := idx2; i <= idx1; i++ { - if iter.Next() { + if iter.Prev() { result = append(result, iter.Value()) } else { ok = false @@ -140,7 +140,7 @@ func (tree *vbTree) IndexRange(idx1, idx2 int) (result []interface{}, ok bool) { iter := tree.iter result = make([]interface{}, 0, idx2-idx1) for i := idx1; i <= idx2; i++ { - if iter.Prev() { + if iter.Next() { result = append(result, iter.Value()) } else { ok = false @@ -278,7 +278,7 @@ func (tree *vbTree) GetRange(k1, k2 interface{}) (result []interface{}) { // iter := NewIterator(min) tree.iter.SetNode(min) iter := tree.iter - for iter.Next() { + for iter.Prev() { result = append(result, iter.Value()) if iter.cur == max { break @@ -305,7 +305,7 @@ func (tree *vbTree) GetRange(k1, k2 interface{}) (result []interface{}) { // iter := NewIterator(max) tree.iter.SetNode(max) iter := tree.iter - for iter.Prev() { + for iter.Next() { result = append(result, iter.Value()) if iter.cur == min { break @@ -358,8 +358,8 @@ func (tree *vbTree) getArounNode(key interface{}) (result [3]*Node) { tree.iter.SetNode(n) iter := tree.iter - iter.Prev() - for iter.Prev() { + iter.Next() + for iter.Next() { if tree.Compare(iter.cur.value, n.value) == 0 { n = iter.cur } else { @@ -378,21 +378,21 @@ func (tree *vbTree) getArounNode(key interface{}) (result [3]*Node) { if result[1] != nil { - result[0] = tree.iter.GetPrev(result[1], 1) - result[2] = tree.iter.GetNext(result[1], 1) + result[0] = tree.iter.GetNext(result[1], 1) + result[2] = tree.iter.GetPrev(result[1], 1) } else { result[0] = last - result[2] = tree.iter.GetNext(last, 1) + result[2] = tree.iter.GetPrev(last, 1) } case -1: if result[1] != nil { - result[0] = tree.iter.GetPrev(result[1], 1) - result[2] = tree.iter.GetNext(result[1], 1) + result[0] = tree.iter.GetNext(result[1], 1) + result[2] = tree.iter.GetPrev(result[1], 1) } else { result[2] = last - result[0] = tree.iter.GetPrev(last, 1) + result[0] = tree.iter.GetNext(last, 1) } case 0: @@ -400,8 +400,8 @@ func (tree *vbTree) getArounNode(key interface{}) (result [3]*Node) { if result[1] == nil { return } - result[0] = tree.iter.GetPrev(result[1], 1) - result[2] = tree.iter.GetNext(result[1], 1) + result[0] = tree.iter.GetNext(result[1], 1) + result[2] = tree.iter.GetPrev(result[1], 1) } return } @@ -418,8 +418,8 @@ func (tree *vbTree) GetNode(value interface{}) (*Node, bool) { tree.iter.SetNode(n) iter := tree.iter - iter.Prev() - for iter.Prev() { + iter.Next() + for iter.Next() { if tree.Compare(iter.cur.value, n.value) == 0 { n = iter.cur } else { diff --git a/priority_queuekey/priority_queuekey_test.go b/priority_queuekey/priority_queuekey_test.go index faa8d9f..5b9b538 100644 --- a/priority_queuekey/priority_queuekey_test.go +++ b/priority_queuekey/priority_queuekey_test.go @@ -375,6 +375,61 @@ func TestPriorityQueue_Iterator(t *testing.T) { } } +func TestPriorityQueue_Iterator2(t *testing.T) { + pq := New(compare.Int) + for i := 0; i < 5; i++ { + pq.Push(i, i) + } + + iter := pq.Iterator() + iter.ToHead() + + n, _ := pq.IndexNode(0) + if n.value != 4 { + t.Error(n) + } + + if v, _ := pq.Top(); v != 4 { + t.Error("Top != 4, and is ", v) + } + + if v := iter.GetNext(n, 2).value; v != 2 { + t.Error("iter.GetNext(n, 2) != 2, and is ", v) + } + + pq = New(compare.Int) + for i := 100; i >= 0; i-- { + pq.Push(i, i) + } + if v, _ := pq.Top(); v != 100 { + t.Error("Top != 100, and is ", v) + } + + for pq.Size() >= 50 { + pq.Pop() + } + + if v, _ := pq.Top(); v != 48 { + t.Error("Top != 48, and is ", v) + } + + pq = New(compare.Int) + for i := 0; i < 100; i++ { + pq.Push(i, i) + } + if v, _ := pq.Top(); v != 99 { + t.Error("Top != 99, and is ", v) + } + + for pq.Size() >= 50 { + pq.Pop() + } + + if v, _ := pq.Top(); v != 48 { + t.Error("Top != 49, and is ", v) + } +} + func TestMain(t *testing.T) { pq := New(compare.Int) pq.Push(1, 1) diff --git a/priority_queuekey/vbt.go b/priority_queuekey/vbt.go index b3caedd..27fa641 100644 --- a/priority_queuekey/vbt.go +++ b/priority_queuekey/vbt.go @@ -175,7 +175,7 @@ func (tree *vbTree) removeNode(n *Node) { } if tree.top == n { - tree.top = tree.iter.GetNext(n, 1) + tree.top = tree.iter.GetNext(n, 1) // 最大值 Next一位后为下个最大值 } ls, rs := getChildrenSize(n)