1 |
|
/* |
2 |
< |
* Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved. |
2 |
> |
* Copyright (c) 2003, 2017, Oracle and/or its affiliates. All rights reserved. |
3 |
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
|
* |
5 |
|
* This code is free software; you can redistribute it and/or modify it |
26 |
|
package java.util; |
27 |
|
|
28 |
|
import java.util.function.Consumer; |
29 |
+ |
import jdk.internal.misc.SharedSecrets; |
30 |
|
|
31 |
|
/** |
32 |
|
* An unbounded priority {@linkplain Queue queue} based on a priority heap. |
55 |
|
* <p>This class and its iterator implement all of the |
56 |
|
* <em>optional</em> methods of the {@link Collection} and {@link |
57 |
|
* Iterator} interfaces. The Iterator provided in method {@link |
58 |
< |
* #iterator()} is <em>not</em> guaranteed to traverse the elements of |
58 |
> |
* #iterator()} and the Spliterator provided in method {@link #spliterator()} |
59 |
> |
* are <em>not</em> guaranteed to traverse the elements of |
60 |
|
* the priority queue in any particular order. If you need ordered |
61 |
|
* traversal, consider using {@code Arrays.sort(pq.toArray())}. |
62 |
|
* |
74 |
|
* ({@code peek}, {@code element}, and {@code size}). |
75 |
|
* |
76 |
|
* <p>This class is a member of the |
77 |
< |
* <a href="{@docRoot}/../technotes/guides/collections/index.html"> |
77 |
> |
* <a href="{@docRoot}/java/util/package-summary.html#CollectionsFramework"> |
78 |
|
* Java Collections Framework</a>. |
79 |
|
* |
80 |
|
* @since 1.5 |
339 |
|
int i = size; |
340 |
|
if (i >= queue.length) |
341 |
|
grow(i + 1); |
342 |
+ |
siftUp(i, e); |
343 |
|
size = i + 1; |
341 |
– |
if (i == 0) |
342 |
– |
queue[0] = e; |
343 |
– |
else |
344 |
– |
siftUp(i, e); |
344 |
|
return true; |
345 |
|
} |
346 |
|
|
523 |
|
*/ |
524 |
|
private int expectedModCount = modCount; |
525 |
|
|
526 |
+ |
Itr() {} // prevent access constructor creation |
527 |
+ |
|
528 |
|
public boolean hasNext() { |
529 |
|
return cursor < size || |
530 |
|
(forgetMeNot != null && !forgetMeNot.isEmpty()); |
634 |
|
* promoting x up the tree until it is greater than or equal to |
635 |
|
* its parent, or is the root. |
636 |
|
* |
637 |
< |
* To simplify and speed up coercions and comparisons. the |
637 |
> |
* To simplify and speed up coercions and comparisons, the |
638 |
|
* Comparable and Comparator versions are separated into different |
639 |
|
* methods that are otherwise identical. (Similarly for siftDown.) |
640 |
|
* |
730 |
|
/** |
731 |
|
* Establishes the heap invariant (described above) in the entire tree, |
732 |
|
* assuming nothing about the order of the elements prior to the call. |
733 |
+ |
* This classic algorithm due to Floyd (1964) is known to be O(size). |
734 |
|
*/ |
735 |
|
@SuppressWarnings("unchecked") |
736 |
|
private void heapify() { |
737 |
< |
for (int i = (size >>> 1) - 1; i >= 0; i--) |
738 |
< |
siftDown(i, (E) queue[i]); |
737 |
> |
final Object[] es = queue; |
738 |
> |
int i = (size >>> 1) - 1; |
739 |
> |
if (comparator == null) |
740 |
> |
for (; i >= 0; i--) |
741 |
> |
siftDownComparable(i, (E) es[i]); |
742 |
> |
else |
743 |
> |
for (; i >= 0; i--) |
744 |
> |
siftDownUsingComparator(i, (E) es[i]); |
745 |
|
} |
746 |
|
|
747 |
|
/** |
760 |
|
/** |
761 |
|
* Saves this queue to a stream (that is, serializes it). |
762 |
|
* |
763 |
+ |
* @param s the stream |
764 |
+ |
* @throws java.io.IOException if an I/O error occurs |
765 |
|
* @serialData The length of the array backing the instance is |
766 |
|
* emitted (int), followed by all of its elements |
767 |
|
* (each an {@code Object}) in the proper order. |
758 |
– |
* @param s the stream |
759 |
– |
* @throws java.io.IOException if an I/O error occurs |
768 |
|
*/ |
769 |
|
private void writeObject(java.io.ObjectOutputStream s) |
770 |
|
throws java.io.IOException { |
796 |
|
// Read in (and discard) array length |
797 |
|
s.readInt(); |
798 |
|
|
799 |
+ |
SharedSecrets.getJavaObjectInputStreamAccess().checkArray(s, Object[].class, size); |
800 |
|
queue = new Object[size]; |
801 |
|
|
802 |
|
// Read in all elements. |
811 |
|
/** |
812 |
|
* Creates a <em><a href="Spliterator.html#binding">late-binding</a></em> |
813 |
|
* and <em>fail-fast</em> {@link Spliterator} over the elements in this |
814 |
< |
* queue. |
814 |
> |
* queue. The spliterator does not traverse elements in any particular order |
815 |
> |
* (the {@link Spliterator#ORDERED ORDERED} characteristic is not reported). |
816 |
|
* |
817 |
|
* <p>The {@code Spliterator} reports {@link Spliterator#SIZED}, |
818 |
|
* {@link Spliterator#SUBSIZED}, and {@link Spliterator#NONNULL}. |
823 |
|
* @since 1.8 |
824 |
|
*/ |
825 |
|
public final Spliterator<E> spliterator() { |
826 |
< |
return new PriorityQueueSpliterator<>(this, 0, -1, 0); |
826 |
> |
return new PriorityQueueSpliterator(0, -1, 0); |
827 |
|
} |
828 |
|
|
829 |
< |
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; |
829 |
> |
final class PriorityQueueSpliterator implements Spliterator<E> { |
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) { |
856 |
– |
int i, hi, mc; // hoist accesses and checks from loop |
857 |
– |
PriorityQueue<E> q; Object[] a; |
858 |
|
if (action == null) |
859 |
|
throw new NullPointerException(); |
860 |
< |
if ((q = pq) != null && (a = q.queue) != null) { |
861 |
< |
if ((hi = fence) < 0) { |
862 |
< |
mc = q.modCount; |
863 |
< |
hi = q.size; |
864 |
< |
} |
865 |
< |
else |
866 |
< |
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 |
< |
} |
860 |
> |
if (fence < 0) { fence = size; expectedModCount = modCount; } |
861 |
> |
final Object[] a = queue; |
862 |
> |
int i, hi; E e; |
863 |
> |
for (i = index, index = hi = fence; i < hi; i++) { |
864 |
> |
if ((e = (E) a[i]) == null) |
865 |
> |
break; // must be CME |
866 |
> |
action.accept(e); |
867 |
|
} |
868 |
< |
throw new ConcurrentModificationException(); |
868 |
> |
if (modCount != expectedModCount) |
869 |
> |
throw new ConcurrentModificationException(); |
870 |
|
} |
871 |
|
|
872 |
+ |
@SuppressWarnings("unchecked") |
873 |
|
public boolean tryAdvance(Consumer<? super E> action) { |
874 |
|
if (action == null) |
875 |
|
throw new NullPointerException(); |
876 |
< |
int hi = getFence(), lo = index; |
877 |
< |
if (lo >= 0 && lo < hi) { |
878 |
< |
index = lo + 1; |
879 |
< |
@SuppressWarnings("unchecked") E e = (E)pq.queue[lo]; |
880 |
< |
if (e == null) |
876 |
> |
if (fence < 0) { fence = size; expectedModCount = modCount; } |
877 |
> |
int i; |
878 |
> |
if ((i = index) < fence) { |
879 |
> |
index = i + 1; |
880 |
> |
E e; |
881 |
> |
if ((e = (E) queue[i]) == null |
882 |
> |
|| modCount != expectedModCount) |
883 |
|
throw new ConcurrentModificationException(); |
884 |
|
action.accept(e); |
894 |
– |
if (pq.modCount != expectedModCount) |
895 |
– |
throw new ConcurrentModificationException(); |
885 |
|
return true; |
886 |
|
} |
887 |
|
return false; |
888 |
|
} |
889 |
|
|
890 |
|
public long estimateSize() { |
891 |
< |
return (long) (getFence() - index); |
891 |
> |
return getFence() - index; |
892 |
|
} |
893 |
|
|
894 |
|
public int characteristics() { |