--- jsr166/src/main/java/util/PriorityQueue.java 2013/01/16 01:59:47 1.80
+++ jsr166/src/main/java/util/PriorityQueue.java 2013/02/18 03:15:10 1.92
@@ -1,12 +1,12 @@
/*
- * Copyright (c) 2003, 2006, Oracle and/or its affiliates. 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
@@ -24,6 +24,9 @@
*/
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.
@@ -63,7 +66,7 @@ package java.util;
* java.util.concurrent.PriorityBlockingQueue} class.
*
*
Implementation note: this implementation provides
- * O(log(n)) time for the enqueuing and dequeuing methods
+ * O(log(n)) time for the enqueing and dequeing methods
* ({@code offer}, {@code poll}, {@code remove()} and {@code add});
* linear time for the {@code remove(Object)} and {@code contains(Object)}
* methods; and constant time for the retrieval methods
@@ -77,7 +80,6 @@ package java.util;
* @author Josh Bloch, Doug Lea
* @param the type of elements held in this collection
*/
-@SuppressWarnings("unchecked")
public class PriorityQueue extends AbstractQueue
implements java.io.Serializable {
@@ -93,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.
@@ -110,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
@@ -331,6 +333,7 @@ public class PriorityQueue extends Ab
return true;
}
+ @SuppressWarnings("unchecked")
public E peek() {
return (size == 0) ? null : (E) queue[0];
}
@@ -447,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());
@@ -512,6 +517,7 @@ public class PriorityQueue extends Ab
(forgetMeNot != null && !forgetMeNot.isEmpty());
}
+ @SuppressWarnings("unchecked")
public E next() {
if (expectedModCount != modCount)
throw new ConcurrentModificationException();
@@ -536,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) {
@@ -564,6 +570,7 @@ public class PriorityQueue extends Ab
size = 0;
}
+ @SuppressWarnings("unchecked")
public E poll() {
if (size == 0)
return null;
@@ -589,6 +596,7 @@ 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;
modCount++;
@@ -627,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) {
@@ -640,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;
@@ -667,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
@@ -685,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) {
@@ -706,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]);
@@ -730,6 +743,7 @@ public class PriorityQueue extends Ab
* @serialData The length of the array backing the instance is
* emitted (int), followed by all of its elements
* (each an {@code Object}) in the proper order.
+ * @param s the stream
*/
private void writeObject(java.io.ObjectOutputStream s)
throws java.io.IOException {
@@ -745,7 +759,10 @@ public class PriorityQueue extends Ab
}
/**
- * Reconstitutes this queue from a stream (that is, deserializes it).
+ * Reconstitutes the {@code PriorityQueue} instance from a stream
+ * (that is, deserializes it).
+ *
+ * @param s the stream
*/
private void readObject(java.io.ObjectInputStream s)
throws java.io.IOException, ClassNotFoundException {
@@ -765,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());
+ }
+
+ static final class PriorityQueueSpliterator implements Spliterator {
+ /*
+ * This is very similar to ArrayList Spliterator, except for
+ * extra null checks.
+ */
+ 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;
+ }
+ }
}