/** * Retrieves and removes the head of this queue, or <tt>null</tt> if this * queue is empty. * * @return the head of this queue, or <tt>null</tt> if this queue is empty. */ @Override public E poll() { E minValue; if (isEmpty()) return null; else { this.modCount++; minValue = data[0]; if (classicalRemove) { data[0] = data[heapSize - 1]; costs[0] = costs[heapSize - 1]; indices[this.getIndex(data[0])] = 0; indices[this.getIndex(minValue)] = -1; heapSize--; if (heapSize > 0) siftDown(0); } else { siftDownUp(0); indices[this.getIndex(minValue)] = -1; } return minValue; } }
private void siftDown(int nodeIndex) { int leftChildIndex = getLeftChildIndex(nodeIndex); if (leftChildIndex >= heapSize) return; int minIndex = -1; double minCosts = Double.POSITIVE_INFINITY; int limitChildIndex = Math.min(leftChildIndex + fanout, heapSize + 1); for (int childIndex = leftChildIndex; childIndex < limitChildIndex; childIndex++) { /* * If the costs are equal, use the array indices to define the sort order. * Doing so should guarantee a deterministic order of the heap entries. */ double childCosts = costs[childIndex]; if (childCosts <= minCosts) { if (childCosts < minCosts || this.getIndex(data[childIndex]) < this.getIndex(data[minIndex])) { minIndex = childIndex; minCosts = childCosts; } } } if (minIndex >= 0) { swapData(nodeIndex, minIndex); siftDown(minIndex); } }