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 |
|
* |
338 |
|
int i = size; |
339 |
|
if (i >= queue.length) |
340 |
|
grow(i + 1); |
341 |
+ |
siftUp(i, e); |
342 |
|
size = i + 1; |
341 |
– |
if (i == 0) |
342 |
– |
queue[0] = e; |
343 |
– |
else |
344 |
– |
siftUp(i, e); |
343 |
|
return true; |
344 |
|
} |
345 |
|
|
631 |
|
* promoting x up the tree until it is greater than or equal to |
632 |
|
* its parent, or is the root. |
633 |
|
* |
634 |
< |
* To simplify and speed up coercions and comparisons. the |
634 |
> |
* To simplify and speed up coercions and comparisons, the |
635 |
|
* Comparable and Comparator versions are separated into different |
636 |
|
* methods that are otherwise identical. (Similarly for siftDown.) |
637 |
|
* |
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 |
|
/** |
757 |
|
/** |
758 |
|
* Saves this queue to a stream (that is, serializes it). |
759 |
|
* |
760 |
+ |
* @param s the stream |
761 |
+ |
* @throws java.io.IOException if an I/O error occurs |
762 |
|
* @serialData The length of the array backing the instance is |
763 |
|
* emitted (int), followed by all of its elements |
764 |
|
* (each an {@code Object}) in the proper order. |
758 |
– |
* @param s the stream |
759 |
– |
* @throws java.io.IOException if an I/O error occurs |
765 |
|
*/ |
766 |
|
private void writeObject(java.io.ObjectOutputStream s) |
767 |
|
throws java.io.IOException { |
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> { |
820 |
< |
/* |
821 |
< |
* This is very similar to ArrayList Spliterator, except for |
822 |
< |
* extra null checks. |
823 |
< |
*/ |
824 |
< |
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, |
831 |
< |
int expectedModCount) { |
832 |
< |
this.pq = pq; |
830 |
> |
/** Creates new spliterator covering the given range. */ |
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, |
851 |
< |
expectedModCount); |
849 |
> |
new PriorityQueueSpliterator(lo, index = mid, expectedModCount); |
850 |
|
} |
851 |
|
|
852 |
|
@SuppressWarnings("unchecked") |
853 |
|
public void forEachRemaining(Consumer<? super E> action) { |
856 |
– |
int i, hi, mc; // hoist accesses and checks from loop |
857 |
– |
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; |
867 |
< |
if ((i = index) >= 0 && (index = hi) <= a.length) { |
868 |
< |
for (E e;; ++i) { |
869 |
< |
if (i < hi) { |
870 |
< |
if ((e = (E) a[i]) == null) // must be CME |
871 |
< |
break; |
872 |
< |
action.accept(e); |
873 |
< |
} |
874 |
< |
else if (q.modCount != mc) |
875 |
< |
break; |
876 |
< |
else |
877 |
< |
return; |
878 |
< |
} |
879 |
< |
} |
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); |
894 |
– |
if (pq.modCount != expectedModCount) |
895 |
– |
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() { |