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)) }