final class HitQueue extends PriorityQueue<ScoreDoc>
Constructor and Description |
---|
HitQueue(int size,
boolean prePopulate)
Creates a new instance with
size elements. |
Modifier and Type | Method and Description |
---|---|
protected ScoreDoc |
getSentinelObject()
This method can be overridden by extending classes to return a sentinel
object which will be used by the
PriorityQueue.PriorityQueue(int,boolean)
constructor to fill the queue, so that the code which uses that queue can always
assume it's full and only change the top without attempting to insert any new
object.Those sentinel values should always compare worse than any non-sentinel value (i.e., PriorityQueue.lessThan(T, T) should always favor the
non-sentinel values).By default, this method returns null, which means the queue will not be filled with sentinel values. |
protected boolean |
lessThan(ScoreDoc hitA,
ScoreDoc hitB)
Determines the ordering of objects in this priority queue.
|
add, clear, getHeapArray, insertWithOverflow, iterator, pop, remove, size, top, updateTop, updateTop
HitQueue(int size, boolean prePopulate)
size
elements. If
prePopulate
is set to true, the queue will pre-populate itself
with sentinel objects and set its PriorityQueue.size()
to size
. In
that case, you should not rely on PriorityQueue.size()
to get the number of
actual elements that were added to the queue, but keep track yourself.prePopulate
is true, you should pop
elements from the queue using the following code example:
PriorityQueue<ScoreDoc> pq = new HitQueue(10, true); // pre-populate. ScoreDoc top = pq.top(); // Add/Update one element. top.score = 1.0f; top.doc = 0; top = (ScoreDoc) pq.updateTop(); int totalHits = 1; // Now pop only the elements that were *truly* inserted. // First, pop all the sentinel elements (there are pq.size() - totalHits). for (int i = pq.size() - totalHits; i > 0; i--) pq.pop(); // Now pop the truly added elements. ScoreDoc[] results = new ScoreDoc[totalHits]; for (int i = totalHits - 1; i >= 0; i--) { results[i] = (ScoreDoc) pq.pop(); }
NOTE: This class pre-allocate a full array of
length size
.
size
- the requested size of this queue.prePopulate
- specifies whether to pre-populate the queue with sentinel values.getSentinelObject()
protected ScoreDoc getSentinelObject()
PriorityQueue
PriorityQueue.PriorityQueue(int,boolean)
constructor to fill the queue, so that the code which uses that queue can always
assume it's full and only change the top without attempting to insert any new
object.PriorityQueue.lessThan(T, T)
should always favor the
non-sentinel values).// extends getSentinelObject() to return a non-null value. PriorityQueue<MyObject> pq = new MyQueue<MyObject>(numHits); // save the 'top' element, which is guaranteed to not be null. MyObject pqTop = pq.top(); <...> // now in order to add a new element, which is 'better' than top (after // you've verified it is better), it is as simple as: pqTop.change(). pqTop = pq.updateTop();NOTE: if this method returns a non-null value, it will be called by the
PriorityQueue.PriorityQueue(int,boolean)
constructor
PriorityQueue.size()
times, relying on a new object to be returned and will not
check if it's null again. Therefore you should ensure any call to this
method creates a new instance and behaves consistently, e.g., it cannot
return null if it previously returned non-null.getSentinelObject
in class PriorityQueue<ScoreDoc>
protected final boolean lessThan(ScoreDoc hitA, ScoreDoc hitB)
PriorityQueue
lessThan
in class PriorityQueue<ScoreDoc>
true
iff parameter a is less than parameter b.