From 0403bebea483f05435a8559811796fb6dc9f21e9 Mon Sep 17 00:00:00 2001 From: huangsimin Date: Wed, 13 Feb 2019 10:46:30 +0800 Subject: [PATCH] =?UTF-8?q?TODO:=20Index=20=E6=9C=89=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- priority_queue/priority_queue.go | 14 ++++++++++---- priority_queue/priority_queue_test.go | 5 +++-- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/priority_queue/priority_queue.go b/priority_queue/priority_queue.go index d822c42..fea7759 100644 --- a/priority_queue/priority_queue.go +++ b/priority_queue/priority_queue.go @@ -30,7 +30,7 @@ type Node struct { // NewWithInt compare use int func NewWithInt() *PriorityQueue { p := new(PriorityQueue) - p.indexlimit = 20 + p.indexlimit = 10 p.comparator = func(a, b interface{}) int { if a.(int) > b.(int) { return 1 @@ -116,10 +116,15 @@ func (pq *PriorityQueue) Push(v interface{}) { for i := 0; cur.next != nil; i++ { - if i >= pq.indexlimit-1 { - if idx.nlen >= pq.indexlimit*2 { + if i >= pq.indexlimit { + + if idx.next != nil && idx.next.nlen < pq.indexlimit { + idx.next.nlen += idx.nlen - pq.indexlimit + idx.nlen = pq.indexlimit + idx.next.node = cur + } else { index := new(Index) - index.node = node + index.node = cur index.nlen = idx.nlen - pq.indexlimit index.next = idx.next @@ -128,6 +133,7 @@ func (pq *PriorityQueue) Push(v interface{}) { idx = index i = 0 } + } if pq.comparator(v, cur.next.value) > 0 { diff --git a/priority_queue/priority_queue_test.go b/priority_queue/priority_queue_test.go index 20617c6..2a88264 100644 --- a/priority_queue/priority_queue_test.go +++ b/priority_queue/priority_queue_test.go @@ -37,7 +37,7 @@ func TestNPQ(t *testing.T) { func TestPriorityQueue(t *testing.T) { p := NewWithInt() - for i := 0; i < 20; i++ { + for i := 0; i < 100; i++ { p.Push(randomdata.Number(0, 10000)) t.Log(p.String()) } @@ -48,11 +48,12 @@ func TestPriorityQueue(t *testing.T) { func BenchmarkPriorityQueue(b *testing.B) { p := NewWithInt() - // for i := 0; i < 100000; i++ { + // for i := 0; i < 10000; i++ { // p.Push(randomdata.Number(0, 100000)) // // p.Values() // } + b.N = 100000 for i := 0; i < b.N; i++ { p.Push(randomdata.Number(0, 100000)) }