--- jsr166/src/main/java/util/PriorityQueue.java 2010/05/10 20:11:01 1.70
+++ jsr166/src/main/java/util/PriorityQueue.java 2013/02/26 17:28:00 1.93
@@ -1,12 +1,12 @@
/*
- * Copyright 2003-2006 Sun Microsystems, Inc. All Rights Reserved.
+ * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
- * published by the Free Software Foundation. Sun designates this
+ * published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
- * by Sun in the LICENSE file that accompanied this code.
+ * by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
@@ -18,12 +18,15 @@
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
- * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
- * CA 95054 USA or visit www.sun.com if you need additional information or
- * have any questions.
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
*/
package java.util;
+import java.util.function.Consumer;
+import java.util.stream.Stream;
+import java.util.stream.Streams;
/**
* An unbounded priority {@linkplain Queue queue} based on a priority heap.
@@ -56,7 +59,7 @@ package java.util;
* 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
@@ -92,7 +95,7 @@ public class PriorityQueue extends Ab
* heap and each descendant d of n, n <= d. The element with the
* lowest value is in queue[0], assuming the queue is nonempty.
*/
- private transient Object[] queue;
+ transient Object[] queue; // non-private to simplify nested class access
/**
* The number of elements in the priority queue.
@@ -109,7 +112,7 @@ public class PriorityQueue extends Ab
* The number of times this priority queue has been
* structurally modified. See AbstractList for gory details.
*/
- private transient int modCount = 0;
+ transient int modCount = 0; // non-private to simplify nested class access
/**
* Creates a {@code PriorityQueue} with the default initial
@@ -330,10 +333,9 @@ public class PriorityQueue extends Ab
return true;
}
+ @SuppressWarnings("unchecked")
public E peek() {
- if (size == 0)
- return null;
- return (E) queue[0];
+ return (size == 0) ? null : (E) queue[0];
}
private int indexOf(Object o) {
@@ -430,15 +432,14 @@ public class PriorityQueue extends Ab
* precise control over the runtime type of the output array, and may,
* under certain circumstances, be used to save allocation costs.
*
- * Suppose x is a queue known to contain only strings.
+ *
Suppose {@code x} is a queue known to contain only strings.
* The following code can be used to dump the queue into a newly
- * allocated array of String:
+ * allocated array of {@code String}:
*
- *
- * String[] y = x.toArray(new String[0]);
+ * {@code String[] y = x.toArray(new String[0]);}
*
- * Note that toArray(new Object[0]) is identical in function to
- * toArray().
+ * Note that {@code toArray(new Object[0])} is identical in function to
+ * {@code toArray()}.
*
* @param a the array into which the elements of the queue are to
* be stored, if it is big enough; otherwise, a new array of the
@@ -449,7 +450,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());
@@ -514,6 +517,7 @@ public class PriorityQueue extends Ab
(forgetMeNot != null && !forgetMeNot.isEmpty());
}
+ @SuppressWarnings("unchecked")
public E next() {
if (expectedModCount != modCount)
throw new ConcurrentModificationException();
@@ -538,7 +542,7 @@ public class PriorityQueue extends Ab
cursor--;
else {
if (forgetMeNot == null)
- forgetMeNot = new ArrayDeque();
+ forgetMeNot = new ArrayDeque<>();
forgetMeNot.add(moved);
}
} else if (lastRetElt != null) {
@@ -566,6 +570,7 @@ public class PriorityQueue extends Ab
size = 0;
}
+ @SuppressWarnings("unchecked")
public E poll() {
if (size == 0)
return null;
@@ -591,8 +596,9 @@ public class PriorityQueue extends Ab
* position before i. This fact is used by iterator.remove so as to
* avoid missing traversing elements.
*/
+ @SuppressWarnings("unchecked")
private E removeAt(int i) {
- assert i >= 0 && i < size;
+ // assert i >= 0 && i < size;
modCount++;
int s = --size;
if (s == i) // removed last element
@@ -629,6 +635,7 @@ public class PriorityQueue extends Ab
siftUpComparable(k, x);
}
+ @SuppressWarnings("unchecked")
private void siftUpComparable(int k, E x) {
Comparable super E> key = (Comparable super E>) x;
while (k > 0) {
@@ -642,6 +649,7 @@ public class PriorityQueue extends Ab
queue[k] = key;
}
+ @SuppressWarnings("unchecked")
private void siftUpUsingComparator(int k, E x) {
while (k > 0) {
int parent = (k - 1) >>> 1;
@@ -669,6 +677,7 @@ public class PriorityQueue extends Ab
siftDownComparable(k, x);
}
+ @SuppressWarnings("unchecked")
private void siftDownComparable(int k, E x) {
Comparable super E> key = (Comparable super E>)x;
int half = size >>> 1; // loop while a non-leaf
@@ -687,6 +696,7 @@ public class PriorityQueue extends Ab
queue[k] = key;
}
+ @SuppressWarnings("unchecked")
private void siftDownUsingComparator(int k, E x) {
int half = size >>> 1;
while (k < half) {
@@ -708,6 +718,7 @@ public class PriorityQueue extends Ab
* Establishes the heap invariant (described above) in the entire tree,
* assuming nothing about the order of the elements prior to the call.
*/
+ @SuppressWarnings("unchecked")
private void heapify() {
for (int i = (size >>> 1) - 1; i >= 0; i--)
siftDown(i, (E) queue[i]);
@@ -727,8 +738,7 @@ public class PriorityQueue extends Ab
}
/**
- * Saves the state of the instance to a stream (that
- * is, serializes it).
+ * Saves this queue to a stream (that is, serializes it).
*
* @serialData The length of the array backing the instance is
* emitted (int), followed by all of its elements
@@ -736,7 +746,7 @@ public class PriorityQueue extends Ab
* @param s the stream
*/
private void writeObject(java.io.ObjectOutputStream s)
- throws java.io.IOException{
+ throws java.io.IOException {
// Write out element count, and any hidden stuff
s.defaultWriteObject();
@@ -772,4 +782,105 @@ public class PriorityQueue extends Ab
// spec has never explained what that might be.
heapify();
}
+
+ final Spliterator spliterator() {
+ return new PriorityQueueSpliterator(this, 0, -1, 0);
+ }
+
+ public Stream stream() {
+ return Streams.stream(spliterator());
+ }
+
+ public Stream parallelStream() {
+ return Streams.parallelStream(spliterator());
+ }
+
+ /**
+ * 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 int fence; // -1 until first use
+ private int expectedModCount; // initialized when fence set
+
+ /** 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.expectedModCount = expectedModCount;
+ }
+
+ 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 PriorityQueueSpliterator trySplit() {
+ int hi = getFence(), lo = index, mid = (lo + hi) >>> 1;
+ return (lo >= mid) ? null :
+ new PriorityQueueSpliterator(pq, lo, index = mid,
+ expectedModCount);
+ }
+
+ @SuppressWarnings("unchecked")
+ public void forEach(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 ((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(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();
+ return true;
+ }
+ return false;
+ }
+
+ public long estimateSize() {
+ return (long) (getFence() - index);
+ }
+
+ public int characteristics() {
+ return Spliterator.SIZED | Spliterator.SUBSIZED | Spliterator.NONNULL;
+ }
+ }
}