TODO: Index 有错误

This commit is contained in:
huangsimin 2019-02-13 10:46:30 +08:00
parent 841c58b4e8
commit 0403bebea4
2 changed files with 13 additions and 6 deletions

View File

@ -30,7 +30,7 @@ type Node struct {
// NewWithInt compare use int // NewWithInt compare use int
func NewWithInt() *PriorityQueue { func NewWithInt() *PriorityQueue {
p := new(PriorityQueue) p := new(PriorityQueue)
p.indexlimit = 20 p.indexlimit = 10
p.comparator = func(a, b interface{}) int { p.comparator = func(a, b interface{}) int {
if a.(int) > b.(int) { if a.(int) > b.(int) {
return 1 return 1
@ -116,10 +116,15 @@ func (pq *PriorityQueue) Push(v interface{}) {
for i := 0; cur.next != nil; i++ { for i := 0; cur.next != nil; i++ {
if i >= pq.indexlimit-1 { if i >= pq.indexlimit {
if idx.nlen >= pq.indexlimit*2 {
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 := new(Index)
index.node = node index.node = cur
index.nlen = idx.nlen - pq.indexlimit index.nlen = idx.nlen - pq.indexlimit
index.next = idx.next index.next = idx.next
@ -128,6 +133,7 @@ func (pq *PriorityQueue) Push(v interface{}) {
idx = index idx = index
i = 0 i = 0
} }
} }
if pq.comparator(v, cur.next.value) > 0 { if pq.comparator(v, cur.next.value) > 0 {

View File

@ -37,7 +37,7 @@ func TestNPQ(t *testing.T) {
func TestPriorityQueue(t *testing.T) { func TestPriorityQueue(t *testing.T) {
p := NewWithInt() p := NewWithInt()
for i := 0; i < 20; i++ { for i := 0; i < 100; i++ {
p.Push(randomdata.Number(0, 10000)) p.Push(randomdata.Number(0, 10000))
t.Log(p.String()) t.Log(p.String())
} }
@ -48,11 +48,12 @@ func TestPriorityQueue(t *testing.T) {
func BenchmarkPriorityQueue(b *testing.B) { func BenchmarkPriorityQueue(b *testing.B) {
p := NewWithInt() p := NewWithInt()
// for i := 0; i < 100000; i++ { // for i := 0; i < 10000; i++ {
// p.Push(randomdata.Number(0, 100000)) // p.Push(randomdata.Number(0, 100000))
// // p.Values() // // p.Values()
// } // }
b.N = 100000
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
p.Push(randomdata.Number(0, 100000)) p.Push(randomdata.Number(0, 100000))
} }