/** * Removes and returns the value at {@code index}. */ private E removeAndGet(int index) { E value = elementData(index); removeAt(index); return value; }
@Override public E peek() { return isEmpty() ? null : elementData(0); }
/** * Retrieves, but does not remove, the greatest element of this queue, or * returns {@code null} if the queue is empty. */ public E peekLast() { return isEmpty() ? null : elementData(getMaxElementIndex()); }
private MoveDesc<E> fillHole(int index, E toTrickle) { Heap heap = heapForIndex(index); // We consider elementData(index) a "hole", and we want to fill it // with the last element of the heap, toTrickle. // Since the last element of the heap is from the bottom level, we // optimistically fill index position with elements from lower levels, // moving the hole down. In most cases this reduces the number of // comparisons with toTrickle, but in some cases we will need to bubble it // all the way up again. int vacated = heap.fillHoleAt(index); // Try to see if toTrickle can be bubbled up min levels. int bubbledTo = heap.bubbleUpAlternatingLevels(vacated, toTrickle); if (bubbledTo == vacated) { // Could not bubble toTrickle up min levels, try moving // it from min level to max level (or max to min level) and bubble up // there. return heap.tryCrossOverAndBubbleUp(index, vacated, toTrickle); } else { return (bubbledTo < index) ? new MoveDesc<E>(toTrickle, elementData(index)) : null; } }
return null; E actualLastElement = elementData(size); int lastElementAt = heapForIndex(size).swapWithConceptuallyLastElement(actualLastElement); if (lastElementAt == index) { return null; E toTrickle = elementData(size); queue[size] = null; MoveDesc<E> changes = fillHole(index, toTrickle);