--- jsr166/src/main/java/util/PriorityQueue.java 2013/01/16 15:06:10 1.81
+++ jsr166/src/main/java/util/PriorityQueue.java 2013/05/02 06:02:17 1.96
@@ -24,10 +24,8 @@
*/
package java.util;
+import java.util.function.Consumer;
import java.util.stream.Stream;
-import java.util.Spliterator;
-import java.util.stream.Streams;
-import java.util.function.Block;
/**
* An unbounded priority {@linkplain Queue queue} based on a priority heap.
@@ -60,7 +58,7 @@ import java.util.function.Block;
* the priority queue in any particular order. If you need ordered
* traversal, consider using {@code Arrays.sort(pq.toArray())}.
*
- *
Note that this implementation is not synchronized.
+ *
Note that this implementation is not synchronized.
* Multiple threads should not access a {@code PriorityQueue}
* instance concurrently if any of the threads modifies the queue.
* Instead, use the thread-safe {@link
@@ -451,7 +449,9 @@ public class PriorityQueue extends Ab
* this queue
* @throws NullPointerException if the specified array is null
*/
+ @SuppressWarnings("unchecked")
public T[] toArray(T[] a) {
+ final int size = this.size;
if (a.length < size)
// Make a new array of a's runtime type, but my contents:
return (T[]) Arrays.copyOf(queue, size, a.getClass());
@@ -541,7 +541,7 @@ public class PriorityQueue extends Ab
cursor--;
else {
if (forgetMeNot == null)
- forgetMeNot = new ArrayDeque();
+ forgetMeNot = new ArrayDeque<>();
forgetMeNot.add(moved);
}
} else if (lastRetElt != null) {
@@ -782,91 +782,96 @@ public class PriorityQueue extends Ab
heapify();
}
- // wrapping constructor in method avoids transient javac problems
- final PriorityQueueSpliterator spliterator(int origin, int fence,
- int expectedModCount) {
- return new PriorityQueueSpliterator(this, origin, fence,
- expectedModCount);
- }
-
- public Stream stream() {
- int flags = Streams.STREAM_IS_SIZED;
- return Streams.stream
- (() -> spliterator(0, size, modCount), flags);
- }
- public Stream parallelStream() {
- int flags = Streams.STREAM_IS_SIZED;
- return Streams.parallelStream
- (() -> spliterator(0, size, modCount), flags);
- }
-
- /** Index-based split-by-two Spliterator */
- static final class PriorityQueueSpliterator
- implements Spliterator, Iterator {
+ public Spliterator spliterator() {
+ return new PriorityQueueSpliterator(this, 0, -1, 0);
+ }
+
+ /**
+ * This is very similar to ArrayList Spliterator, except for extra
+ * null checks.
+ */
+ static final class PriorityQueueSpliterator implements Spliterator {
private final PriorityQueue pq;
- private int index; // current index, modified on advance/split
- private final int fence; // one past last index
- private final int expectedModCount; // for comodification checks
+ private int index; // current index, modified on advance/split
+ private int fence; // -1 until first use
+ private int expectedModCount; // initialized when fence set
- /** Create new spliterator covering the given range */
+ /** Creates new spliterator covering the given range */
PriorityQueueSpliterator(PriorityQueue pq, int origin, int fence,
int expectedModCount) {
- this.pq = pq; this.index = origin; this.fence = fence;
+ this.pq = pq;
+ this.index = origin;
+ this.fence = fence;
this.expectedModCount = expectedModCount;
}
- public PriorityQueueSpliterator trySplit() {
- int lo = index, mid = (lo + fence) >>> 1;
+ private int getFence() { // initialize fence to size on first use
+ int hi;
+ if ((hi = fence) < 0) {
+ expectedModCount = pq.modCount;
+ hi = fence = pq.size;
+ }
+ return hi;
+ }
+
+ public Spliterator trySplit() {
+ int hi = getFence(), lo = index, mid = (lo + hi) >>> 1;
return (lo >= mid) ? null :
new PriorityQueueSpliterator(pq, lo, index = mid,
- expectedModCount);
+ expectedModCount);
}
- public void forEach(Block super E> block) {
- Object[] a; int i, hi; // hoist accesses and checks from loop
- if (block == null)
+ @SuppressWarnings("unchecked")
+ public void forEachRemaining(Consumer super E> action) {
+ int i, hi, mc; // hoist accesses and checks from loop
+ PriorityQueue q; Object[] a;
+ if (action == null)
throw new NullPointerException();
- if ((a = pq.queue).length >= (hi = fence) &&
- (i = index) >= 0 && i < hi) {
- index = hi;
- do {
- @SuppressWarnings("unchecked") E e = (E) a[i];
- block.accept(e);
- } while (++i < hi);
- if (pq.modCount != expectedModCount)
- throw new ConcurrentModificationException();
+ if ((q = pq) != null && (a = q.queue) != null) {
+ if ((hi = fence) < 0) {
+ mc = q.modCount;
+ hi = q.size;
+ }
+ else
+ mc = expectedModCount;
+ if ((i = index) >= 0 && (index = hi) <= a.length) {
+ for (E e;; ++i) {
+ if (i < hi) {
+ if ((e = (E) a[i]) == null) // must be CME
+ break;
+ action.accept(e);
+ }
+ else if (q.modCount != mc)
+ break;
+ else
+ return;
+ }
+ }
}
+ throw new ConcurrentModificationException();
}
- public boolean tryAdvance(Block super E> block) {
- if (index >= 0 && index < fence) {
+ public boolean tryAdvance(Consumer super E> action) {
+ int hi = getFence(), lo = index;
+ if (lo >= 0 && lo < hi) {
+ index = lo + 1;
+ @SuppressWarnings("unchecked") E e = (E)pq.queue[lo];
+ if (e == null)
+ throw new ConcurrentModificationException();
+ action.accept(e);
if (pq.modCount != expectedModCount)
throw new ConcurrentModificationException();
- @SuppressWarnings("unchecked") E e =
- (E)pq.queue[index++];
- block.accept(e);
return true;
}
return false;
}
- public long estimateSize() { return (long)(fence - index); }
- public boolean hasExactSize() { return true; }
- public boolean hasExactSplits() { return true; }
-
- // Iterator support
- public Iterator iterator() { return this; }
- public void remove() { throw new UnsupportedOperationException(); }
- public boolean hasNext() { return index >= 0 && index < fence; }
+ public long estimateSize() {
+ return (long) (getFence() - index);
+ }
- public E next() {
- if (index < 0 || index >= fence)
- throw new NoSuchElementException();
- if (pq.modCount != expectedModCount)
- throw new ConcurrentModificationException();
- @SuppressWarnings("unchecked") E e =
- (E) pq.queue[index++];
- return e;
+ public int characteristics() {
+ return Spliterator.SIZED | Spliterator.SUBSIZED | Spliterator.NONNULL;
}
}
}