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 |
|
/** |
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> { |
819 |
< |
/* |
820 |
< |
* This is very similar to ArrayList Spliterator, except for |
821 |
< |
* extra null checks. |
822 |
< |
*/ |
823 |
< |
private final PriorityQueue<E> pq; |
825 |
> |
final class PriorityQueueSpliterator implements Spliterator<E> { |
826 |
|
private int index; // current index, modified on advance/split |
827 |
|
private int fence; // -1 until first use |
828 |
|
private int expectedModCount; // initialized when fence set |
829 |
|
|
830 |
|
/** Creates new spliterator covering the given range. */ |
831 |
< |
PriorityQueueSpliterator(PriorityQueue<E> pq, int origin, int fence, |
830 |
< |
int expectedModCount) { |
831 |
< |
this.pq = pq; |
831 |
> |
PriorityQueueSpliterator(int origin, int fence, int expectedModCount) { |
832 |
|
this.index = origin; |
833 |
|
this.fence = fence; |
834 |
|
this.expectedModCount = expectedModCount; |
837 |
|
private int getFence() { // initialize fence to size on first use |
838 |
|
int hi; |
839 |
|
if ((hi = fence) < 0) { |
840 |
< |
expectedModCount = pq.modCount; |
841 |
< |
hi = fence = pq.size; |
840 |
> |
expectedModCount = modCount; |
841 |
> |
hi = fence = size; |
842 |
|
} |
843 |
|
return hi; |
844 |
|
} |
845 |
|
|
846 |
< |
public PriorityQueueSpliterator<E> trySplit() { |
846 |
> |
public PriorityQueueSpliterator trySplit() { |
847 |
|
int hi = getFence(), lo = index, mid = (lo + hi) >>> 1; |
848 |
|
return (lo >= mid) ? null : |
849 |
< |
new PriorityQueueSpliterator<>(pq, lo, index = mid, |
850 |
< |
expectedModCount); |
849 |
> |
new PriorityQueueSpliterator(lo, index = mid, expectedModCount); |
850 |
|
} |
851 |
|
|
852 |
|
@SuppressWarnings("unchecked") |
853 |
|
public void forEachRemaining(Consumer<? super E> action) { |
855 |
– |
int i, hi, mc; // hoist accesses and checks from loop |
856 |
– |
PriorityQueue<E> q; Object[] a; |
854 |
|
if (action == null) |
855 |
|
throw new NullPointerException(); |
856 |
< |
if ((q = pq) != null && (a = q.queue) != null) { |
857 |
< |
if ((hi = fence) < 0) { |
858 |
< |
mc = q.modCount; |
859 |
< |
hi = q.size; |
860 |
< |
} |
861 |
< |
else |
862 |
< |
mc = expectedModCount; |
866 |
< |
if ((i = index) >= 0 && (index = hi) <= a.length) { |
867 |
< |
for (E e;; ++i) { |
868 |
< |
if (i < hi) { |
869 |
< |
if ((e = (E) a[i]) == null) // must be CME |
870 |
< |
break; |
871 |
< |
action.accept(e); |
872 |
< |
} |
873 |
< |
else if (q.modCount != mc) |
874 |
< |
break; |
875 |
< |
else |
876 |
< |
return; |
877 |
< |
} |
878 |
< |
} |
856 |
> |
if (fence < 0) { fence = size; expectedModCount = modCount; } |
857 |
> |
final Object[] a = queue; |
858 |
> |
int i, hi; E e; |
859 |
> |
for (i = index, index = hi = fence; i < hi; i++) { |
860 |
> |
if ((e = (E) a[i]) == null) |
861 |
> |
break; // must be CME |
862 |
> |
action.accept(e); |
863 |
|
} |
864 |
< |
throw new ConcurrentModificationException(); |
864 |
> |
if (modCount != expectedModCount) |
865 |
> |
throw new ConcurrentModificationException(); |
866 |
|
} |
867 |
|
|
868 |
+ |
@SuppressWarnings("unchecked") |
869 |
|
public boolean tryAdvance(Consumer<? super E> action) { |
870 |
|
if (action == null) |
871 |
|
throw new NullPointerException(); |
872 |
< |
int hi = getFence(), lo = index; |
873 |
< |
if (lo >= 0 && lo < hi) { |
874 |
< |
index = lo + 1; |
875 |
< |
@SuppressWarnings("unchecked") E e = (E)pq.queue[lo]; |
876 |
< |
if (e == null) |
872 |
> |
if (fence < 0) { fence = size; expectedModCount = modCount; } |
873 |
> |
int i; |
874 |
> |
if ((i = index) < fence) { |
875 |
> |
index = i + 1; |
876 |
> |
E e; |
877 |
> |
if ((e = (E) queue[i]) == null |
878 |
> |
|| modCount != expectedModCount) |
879 |
|
throw new ConcurrentModificationException(); |
880 |
|
action.accept(e); |
893 |
– |
if (pq.modCount != expectedModCount) |
894 |
– |
throw new ConcurrentModificationException(); |
881 |
|
return true; |
882 |
|
} |
883 |
|
return false; |
884 |
|
} |
885 |
|
|
886 |
|
public long estimateSize() { |
887 |
< |
return (long) (getFence() - index); |
887 |
> |
return getFence() - index; |
888 |
|
} |
889 |
|
|
890 |
|
public int characteristics() { |