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 |
|
/** |
823 |
|
} |
824 |
|
|
825 |
|
final class PriorityQueueSpliterator implements Spliterator<E> { |
820 |
– |
/* |
821 |
– |
* This is very similar to ArrayList Spliterator, except for |
822 |
– |
* extra null checks. |
823 |
– |
*/ |
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 |
851 |
|
|
852 |
|
@SuppressWarnings("unchecked") |
853 |
|
public void forEachRemaining(Consumer<? super E> action) { |
852 |
– |
int i, hi, mc; // hoist accesses and checks from loop |
853 |
– |
final Object[] a; |
854 |
|
if (action == null) |
855 |
|
throw new NullPointerException(); |
856 |
< |
if ((a = queue) != null) { |
857 |
< |
if ((hi = fence) < 0) { |
858 |
< |
mc = modCount; |
859 |
< |
hi = size; |
860 |
< |
} |
861 |
< |
else |
862 |
< |
mc = expectedModCount; |
863 |
< |
if ((i = index) >= 0 && (index = hi) <= a.length) { |
864 |
< |
for (E e;; ++i) { |
865 |
< |
if (i < hi) { |
866 |
< |
if ((e = (E) a[i]) == null) // must be CME |
867 |
< |
break; |
868 |
< |
action.accept(e); |
869 |
< |
} |
870 |
< |
else if (modCount != mc) |
871 |
< |
break; |
872 |
< |
else |
873 |
< |
return; |
874 |
< |
} |
875 |
< |
} |
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)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); |
890 |
– |
if (modCount != expectedModCount) |
891 |
– |
throw new ConcurrentModificationException(); |
881 |
|
return true; |
882 |
|
} |
883 |
|
return false; |