54 |
|
* <p>This class and its iterator implement all of the |
55 |
|
* <em>optional</em> methods of the {@link Collection} and {@link |
56 |
|
* Iterator} interfaces. The Iterator provided in method {@link |
57 |
< |
* #iterator()} is <em>not</em> guaranteed to traverse the elements of |
57 |
> |
* #iterator()} and the Spliterator provided in method {@link #spliterator()} |
58 |
> |
* are <em>not</em> guaranteed to traverse the elements of |
59 |
|
* the priority queue in any particular order. If you need ordered |
60 |
|
* traversal, consider using {@code Arrays.sort(pq.toArray())}. |
61 |
|
* |
727 |
|
/** |
728 |
|
* Establishes the heap invariant (described above) in the entire tree, |
729 |
|
* assuming nothing about the order of the elements prior to the call. |
730 |
+ |
* This classic algorithm due to Floyd (1964) is known to be O(size). |
731 |
|
*/ |
732 |
|
@SuppressWarnings("unchecked") |
733 |
|
private void heapify() { |
734 |
< |
for (int i = (size >>> 1) - 1; i >= 0; i--) |
735 |
< |
siftDown(i, (E) queue[i]); |
734 |
> |
final Object[] es = queue; |
735 |
> |
final int half = (size >>> 1) - 1; |
736 |
> |
if (comparator == null) |
737 |
> |
for (int i = half; i >= 0; i--) |
738 |
> |
siftDownComparable(i, (E) es[i]); |
739 |
> |
else |
740 |
> |
for (int i = half; i >= 0; i--) |
741 |
> |
siftDownUsingComparator(i, (E) es[i]); |
742 |
|
} |
743 |
|
|
744 |
|
/** |
807 |
|
/** |
808 |
|
* Creates a <em><a href="Spliterator.html#binding">late-binding</a></em> |
809 |
|
* and <em>fail-fast</em> {@link Spliterator} over the elements in this |
810 |
< |
* queue. |
810 |
> |
* queue. The spliterator does not traverse elements in any particular order |
811 |
> |
* (the {@link Spliterator#ORDERED ORDERED} characteristic is not reported). |
812 |
|
* |
813 |
|
* <p>The {@code Spliterator} reports {@link Spliterator#SIZED}, |
814 |
|
* {@link Spliterator#SUBSIZED}, and {@link Spliterator#NONNULL}. |
819 |
|
* @since 1.8 |
820 |
|
*/ |
821 |
|
public final Spliterator<E> spliterator() { |
822 |
< |
return new PriorityQueueSpliterator<>(this, 0, -1, 0); |
822 |
> |
return new PriorityQueueSpliterator(0, -1, 0); |
823 |
|
} |
824 |
|
|
825 |
< |
static final class PriorityQueueSpliterator<E> implements Spliterator<E> { |
825 |
> |
final class PriorityQueueSpliterator implements Spliterator<E> { |
826 |
|
/* |
827 |
|
* This is very similar to ArrayList Spliterator, except for |
828 |
|
* extra null checks. |
829 |
|
*/ |
821 |
– |
private final PriorityQueue<E> pq; |
830 |
|
private int index; // current index, modified on advance/split |
831 |
|
private int fence; // -1 until first use |
832 |
|
private int expectedModCount; // initialized when fence set |
833 |
|
|
834 |
|
/** Creates new spliterator covering the given range. */ |
835 |
< |
PriorityQueueSpliterator(PriorityQueue<E> pq, int origin, int fence, |
828 |
< |
int expectedModCount) { |
829 |
< |
this.pq = pq; |
835 |
> |
PriorityQueueSpliterator(int origin, int fence, int expectedModCount) { |
836 |
|
this.index = origin; |
837 |
|
this.fence = fence; |
838 |
|
this.expectedModCount = expectedModCount; |
841 |
|
private int getFence() { // initialize fence to size on first use |
842 |
|
int hi; |
843 |
|
if ((hi = fence) < 0) { |
844 |
< |
expectedModCount = pq.modCount; |
845 |
< |
hi = fence = pq.size; |
844 |
> |
expectedModCount = modCount; |
845 |
> |
hi = fence = size; |
846 |
|
} |
847 |
|
return hi; |
848 |
|
} |
849 |
|
|
850 |
< |
public PriorityQueueSpliterator<E> trySplit() { |
850 |
> |
public PriorityQueueSpliterator trySplit() { |
851 |
|
int hi = getFence(), lo = index, mid = (lo + hi) >>> 1; |
852 |
|
return (lo >= mid) ? null : |
853 |
< |
new PriorityQueueSpliterator<>(pq, lo, index = mid, |
848 |
< |
expectedModCount); |
853 |
> |
new PriorityQueueSpliterator(lo, index = mid, expectedModCount); |
854 |
|
} |
855 |
|
|
856 |
|
@SuppressWarnings("unchecked") |
857 |
|
public void forEachRemaining(Consumer<? super E> action) { |
858 |
|
int i, hi, mc; // hoist accesses and checks from loop |
859 |
< |
PriorityQueue<E> q; Object[] a; |
859 |
> |
final Object[] a; |
860 |
|
if (action == null) |
861 |
|
throw new NullPointerException(); |
862 |
< |
if ((q = pq) != null && (a = q.queue) != null) { |
862 |
> |
if ((a = queue) != null) { |
863 |
|
if ((hi = fence) < 0) { |
864 |
< |
mc = q.modCount; |
865 |
< |
hi = q.size; |
864 |
> |
mc = modCount; |
865 |
> |
hi = size; |
866 |
|
} |
867 |
|
else |
868 |
|
mc = expectedModCount; |
873 |
|
break; |
874 |
|
action.accept(e); |
875 |
|
} |
876 |
< |
else if (q.modCount != mc) |
876 |
> |
else if (modCount != mc) |
877 |
|
break; |
878 |
|
else |
879 |
|
return; |
889 |
|
int hi = getFence(), lo = index; |
890 |
|
if (lo >= 0 && lo < hi) { |
891 |
|
index = lo + 1; |
892 |
< |
@SuppressWarnings("unchecked") E e = (E)pq.queue[lo]; |
892 |
> |
@SuppressWarnings("unchecked") E e = (E)queue[lo]; |
893 |
|
if (e == null) |
894 |
|
throw new ConcurrentModificationException(); |
895 |
|
action.accept(e); |
896 |
< |
if (pq.modCount != expectedModCount) |
896 |
> |
if (modCount != expectedModCount) |
897 |
|
throw new ConcurrentModificationException(); |
898 |
|
return true; |
899 |
|
} |
901 |
|
} |
902 |
|
|
903 |
|
public long estimateSize() { |
904 |
< |
return (long) (getFence() - index); |
904 |
> |
return getFence() - index; |
905 |
|
} |
906 |
|
|
907 |
|
public int characteristics() { |