73 |
|
* ({@code peek}, {@code element}, and {@code size}). |
74 |
|
* |
75 |
|
* <p>This class is a member of the |
76 |
< |
* <a href="{@docRoot}/../technotes/guides/collections/index.html"> |
76 |
> |
* <a href="{@docRoot}/java/util/package-summary.html#CollectionsFramework"> |
77 |
|
* Java Collections Framework</a>. |
78 |
|
* |
79 |
|
* @since 1.5 |
522 |
|
*/ |
523 |
|
private int expectedModCount = modCount; |
524 |
|
|
525 |
+ |
Itr() {} // prevent access constructor creation |
526 |
+ |
|
527 |
|
public boolean hasNext() { |
528 |
|
return cursor < size || |
529 |
|
(forgetMeNot != null && !forgetMeNot.isEmpty()); |
633 |
|
* promoting x up the tree until it is greater than or equal to |
634 |
|
* its parent, or is the root. |
635 |
|
* |
636 |
< |
* To simplify and speed up coercions and comparisons. the |
636 |
> |
* To simplify and speed up coercions and comparisons, the |
637 |
|
* Comparable and Comparator versions are separated into different |
638 |
|
* methods that are otherwise identical. (Similarly for siftDown.) |
639 |
|
* |
733 |
|
*/ |
734 |
|
@SuppressWarnings("unchecked") |
735 |
|
private void heapify() { |
736 |
< |
for (int i = (size >>> 1) - 1; i >= 0; i--) |
737 |
< |
siftDown(i, (E) queue[i]); |
736 |
> |
final Object[] es = queue; |
737 |
> |
int i = (size >>> 1) - 1; |
738 |
> |
if (comparator == null) |
739 |
> |
for (; i >= 0; i--) |
740 |
> |
siftDownComparable(i, (E) es[i]); |
741 |
> |
else |
742 |
> |
for (; i >= 0; i--) |
743 |
> |
siftDownUsingComparator(i, (E) es[i]); |
744 |
|
} |
745 |
|
|
746 |
|
/** |
825 |
|
} |
826 |
|
|
827 |
|
final class PriorityQueueSpliterator implements Spliterator<E> { |
820 |
– |
/* |
821 |
– |
* This is very similar to ArrayList Spliterator, except for |
822 |
– |
* extra null checks. |
823 |
– |
*/ |
828 |
|
private int index; // current index, modified on advance/split |
829 |
|
private int fence; // -1 until first use |
830 |
|
private int expectedModCount; // initialized when fence set |
853 |
|
|
854 |
|
@SuppressWarnings("unchecked") |
855 |
|
public void forEachRemaining(Consumer<? super E> action) { |
852 |
– |
int i, hi, mc; // hoist accesses and checks from loop |
853 |
– |
final Object[] a; |
856 |
|
if (action == null) |
857 |
|
throw new NullPointerException(); |
858 |
< |
if ((a = queue) != null) { |
859 |
< |
if ((hi = fence) < 0) { |
860 |
< |
mc = modCount; |
861 |
< |
hi = size; |
862 |
< |
} |
863 |
< |
else |
864 |
< |
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 |
< |
} |
858 |
> |
if (fence < 0) { fence = size; expectedModCount = modCount; } |
859 |
> |
final Object[] a = queue; |
860 |
> |
int i, hi; E e; |
861 |
> |
for (i = index, index = hi = fence; i < hi; i++) { |
862 |
> |
if ((e = (E) a[i]) == null) |
863 |
> |
break; // must be CME |
864 |
> |
action.accept(e); |
865 |
|
} |
866 |
< |
throw new ConcurrentModificationException(); |
866 |
> |
if (modCount != expectedModCount) |
867 |
> |
throw new ConcurrentModificationException(); |
868 |
|
} |
869 |
|
|
870 |
+ |
@SuppressWarnings("unchecked") |
871 |
|
public boolean tryAdvance(Consumer<? super E> action) { |
872 |
|
if (action == null) |
873 |
|
throw new NullPointerException(); |
874 |
< |
int hi = getFence(), lo = index; |
875 |
< |
if (lo >= 0 && lo < hi) { |
876 |
< |
index = lo + 1; |
877 |
< |
@SuppressWarnings("unchecked") E e = (E)queue[lo]; |
878 |
< |
if (e == null) |
874 |
> |
if (fence < 0) { fence = size; expectedModCount = modCount; } |
875 |
> |
int i; |
876 |
> |
if ((i = index) < fence) { |
877 |
> |
index = i + 1; |
878 |
> |
E e; |
879 |
> |
if ((e = (E) queue[i]) == null |
880 |
> |
|| modCount != expectedModCount) |
881 |
|
throw new ConcurrentModificationException(); |
882 |
|
action.accept(e); |
890 |
– |
if (modCount != expectedModCount) |
891 |
– |
throw new ConcurrentModificationException(); |
883 |
|
return true; |
884 |
|
} |
885 |
|
return false; |