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> { |
825 |
> |
final class PriorityQueueSpliterator implements Spliterator<E> { |
826 |
|
/* |
827 |
|
* This is very similar to ArrayList Spliterator, except for |
828 |
|
* extra null checks. |
829 |
|
*/ |
824 |
– |
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, |
831 |
< |
int expectedModCount) { |
832 |
< |
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, |
851 |
< |
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() { |