PDA

View Full Version : BinaryHeap not working as expected


someone12345
May 23rd, 2007, 06:05 PM
Pretty simple code that works as excepted with org.apache.lucene.search.ScoreDoc.PriorityQueue. LimeWire's BinaryQueue is faster (in most situations) but sorts in a non-comprehensible manner:

final int qMax = 10;
final int max = 10000000;

org.limewire.collection.BinaryHeap heap = new org.limewire.collection.BinaryHeap( qMax );
for( int i = 0; i < max; i++ )
{
heap.insert( new Integer( i ) );
}
while (!heap.isEmpty()) {
System.out.println(heap.extractMax());
}

Result:
LimeWire:
9999999
9999998
9999997
9999996
6
5
4
3
1
0
(BTW where's the "2" gone?)

This looks correct:

Lucene:
9999990 9999990.0
9999991 9999991.0
9999992 9999992.0
9999993 9999993.0
9999994 9999994.0
9999995 9999995.0
9999996 9999996.0
9999997 9999997.0
9999998 9999998.0
9999999 9999999.0

Sam
May 23rd, 2007, 07:01 PM
That doesn't look good at all.

Do you have any idea where something's going wrong in it?

zab
May 23rd, 2007, 07:21 PM
You just won our eternal gratitude right there. If you can pull off another catch like that you get the case of beer too!

Sam
May 23rd, 2007, 08:30 PM
We've just removed it from the code. It was hopelessly broken (which probably explained why it was so fast), and was only used in two places in the code that were easily converted to working structures.

Thanks very much for pointing it out to us!