/** * Returns the k-<i>th</i> smallest value seen so far. */ public double peek() { if (n < k) sort(heap, n); return heap[0]; }
DoubleHeapSelect heap = new DoubleHeapSelect(k); heap.add(Double.MAX_VALUE); heap.add(d); emptyHeap = false; double upperBound = emptyHeap ? Double.POSITIVE_INFINITY : heap.peek(); if (d <= (upperBound + child.maxDist)) { if (c > 0 && d < upperBound) { if (!identicalExcluded || child.getObject() != q) { heap.add(d); double upperBound = heap.peek(); for (int i = 0; i < zeroSet.size(); i++) { DistanceNode ds = zeroSet.get(i);
/** * Sort the smallest values. */ public void sort() { if (!sorted) { sort(heap, Math.min(k,n)); sorted = true; } }
/** * Returns the i-<i>th</i> smallest value seen so far. i = 0 returns the smallest * value seen, i = 1 the second largest, ..., i = k-1 the last position * tracked. Also, i must be less than the number of previous assimilated. */ public double get(int i) { if (i > Math.min(k, n) - 1) { throw new IllegalArgumentException("HeapSelect i is greater than the number of data received so far."); } if (i == k-1) { return heap[0]; } if (!sorted) { sort(heap, Math.min(k,n)); sorted = true; } return heap[k-1-i]; }
/** * Assimilate a new value from the stream. */ public void add(double datum) { sorted = false; if (n < k) { heap[n++] = datum; if (n == k) { sort(heap, k); } } else { n++; if (datum < heap[0]) { heap[0] = datum; SortUtils.siftDown(heap, 0, k-1); } } }