private static long cost(Collection<BulkScorer> scorers, int minShouldMatch) { final PriorityQueue<BulkScorer> pq = new PriorityQueue<BulkScorer>(scorers.size() - minShouldMatch + 1) { @Override protected boolean lessThan(BulkScorer a, BulkScorer b) { return a.cost() > b.cost(); } }; for (BulkScorer scorer : scorers) { pq.insertWithOverflow(scorer); } long cost = 0; for (BulkScorer scorer = pq.pop(); scorer != null; scorer = pq.pop()) { cost += scorer.cost(); } return cost; }
@Override public int nextPosition() throws IOException { if (started == false) { started = true; return posQueue.top().pos; } if (posQueue.top().upto == 1) { posQueue.pop(); return posQueue.top().pos; } posQueue.top().pos = posQueue.top().pe.nextPosition(); posQueue.top().upto--; posQueue.updateTop(); return posQueue.top().pos; }
/** * Removes an existing element currently stored in the PriorityQueue. Cost is * linear with the size of the queue. (A specialization of PriorityQueue which * tracks element positions would provide a constant remove time but the * trade-off would be extra cost to all additions/insertions) */ public final boolean remove(T element) { for (int i = 1; i <= size; i++) { if (heap[i] == element) { heap[i] = heap[size]; heap[size] = null; // permit GC of objects size--; if (i <= size) { if (!upHeap(i)) { downHeap(i); } } return true; } } return false; }
/** * Adds an Object to a PriorityQueue in log(size) time. * It returns the object (if any) that was * dropped off the heap because it was full. This can be * the given parameter (in case it is smaller than the * full heap's minimum, and couldn't be added), or another * object that was previously the smallest value in the * heap and now has been replaced by a larger one, or null * if the queue wasn't yet full with maxSize elements. */ public T insertWithOverflow(T element) { if (size < maxSize) { add(element); return null; } else if (size > 0 && !lessThan(element, heap[1])) { T ret = heap[1]; heap[1] = element; updateTop(); return ret; } else { return element; } }
if (t.getDepartureTime(sidx) != -1 && t.getDepartureTime(sidx) >= secondsSinceMidnight) { pq.insertWithOverflow(new TripTimeShort(t, sidx, stop, sd)); int i = 0; while (departureTime <= lastDeparture && i < numberOfDepartures) { pq.insertWithOverflow(new TripTimeShort(freq.materialize(sidx, departureTime, true), sidx, stop, sd)); departureTime += freq.headway; i++; if (pq.size() != 0) { StopTimesInPattern stopTimes = new StopTimesInPattern(pattern); while (pq.size() != 0) { stopTimes.times.add(0, pq.pop());
queue.insertWithOverflow(new FileAndTop(i, item)); while ((top = queue.top()) != null) { writer.write(top.current); try { queue.updateTop(); } else { queue.pop();
@Override protected TopDocs newTopDocs(ScoreDoc[] results, int start) { if (results == null) { return EMPTY_TOPDOCS; } // We need to compute maxScore in order to set it in TopDocs. If start == 0, // it means the largest element is already in results, use its score as // maxScore. Otherwise pop everything else, until the largest element is // extracted and use its score as maxScore. float maxScore = Float.NaN; if (start == 0) { maxScore = results[0].score; } else { for (int i = pq.size(); i > 1; i--) { pq.pop(); } maxScore = pq.pop().score; } return new TopDocs(totalHits, results, maxScore); }
private float findSmallestLargeValue(Vector userVector) { PriorityQueue<Float> topPrefValues = new PriorityQueue<Float>(maxPrefsPerUserConsidered) { @Override protected boolean lessThan(Float f1, Float f2) { return f1 < f2; } }; for (Element e : userVector.nonZeroes()) { float absValue = Math.abs((float) e.get()); topPrefValues.insertWithOverflow(absValue); } return topPrefValues.top(); }
@Override public Query getQuery() { return queue.top().getQuery(); } }
/** * Adds element to the PriorityQueue in log(size) time if either * the PriorityQueue is not full, or not lessThan(element, top()). * @param element * @return true if element is added, false otherwise. */ public boolean insert(Object element) { return insertWithOverflow(element) != element; }
/** * Populates the results array with the ScoreDoc instances. This can be * overridden in case a different ScoreDoc type should be returned. */ protected void populateResults(ScoreDoc[] results, int howMany) { for (int i = howMany - 1; i >= 0; i--) { results[i] = pq.pop(); } }
/** * Adds element to the PriorityQueue in log(size) time if either * the PriorityQueue is not full, or not lessThan(element, top()). * @param element * @return true if element is added, false otherwise. */ public boolean insert(Object element){ if(size < maxSize){ put(element); return true; } else if(size > 0 && !lessThan(element, top())){ heap[1] = element; adjustTop(); return true; } else return false; }
final void updateBottom(int doc) { // bottom.score is already set to Float.NaN in add(). bottom.doc = docBase + doc; bottom = pq.updateTop(); }
private final boolean upHeap(int origPos) { int i = origPos; T node = heap[i]; // save bottom node int j = i >>> 1; while (j > 0 && lessThan(node, heap[j])) { heap[i] = heap[j]; // shift parents down i = j; j = j >>> 1; } heap[i] = node; // install saved node return i != origPos; }
/** Removes and returns the least element of the PriorityQueue in log(size) time. */ public final T pop() { if (size > 0) { T result = heap[1]; // save first value heap[1] = heap[size]; // move last to first heap[size] = null; // permit GC of objects size--; downHeap(1); // adjust heap return result; } else { return null; } }
/** * Adds an Object to a PriorityQueue in log(size) time. If one tries to add * more objects than maxSize from initialize an * {@link ArrayIndexOutOfBoundsException} is thrown. * * @return the new 'top' element in the queue. */ public final T add(T element) { size++; heap[size] = element; upHeap(size); return heap[1]; }