ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/PriorityQueue.java
(Generate patch)

Comparing jsr166/src/main/java/util/PriorityQueue.java (file contents):
Revision 1.87 by dl, Fri Feb 1 01:02:25 2013 UTC vs.
Revision 1.96 by jsr166, Thu May 2 06:02:17 2013 UTC

# Line 24 | Line 24
24   */
25  
26   package java.util;
27 import java.util.stream.Stream;
28 import java.util.Spliterator;
29 import java.util.stream.Streams;
27   import java.util.function.Consumer;
28 + import java.util.stream.Stream;
29  
30   /**
31   * An unbounded priority {@linkplain Queue queue} based on a priority heap.
# Line 64 | Line 62 | import java.util.function.Consumer;
62   * Multiple threads should not access a {@code PriorityQueue}
63   * instance concurrently if any of the threads modifies the queue.
64   * Instead, use the thread-safe {@link
65 < * java.util.concurrent.PriorityConsumeringQueue} class.
65 > * java.util.concurrent.PriorityBlockingQueue} class.
66   *
67   * <p>Implementation note: this implementation provides
68   * O(log(n)) time for the enqueing and dequeing methods
# Line 543 | Line 541 | public class PriorityQueue<E> extends Ab
541                      cursor--;
542                  else {
543                      if (forgetMeNot == null)
544 <                        forgetMeNot = new ArrayDeque<E>();
544 >                        forgetMeNot = new ArrayDeque<>();
545                      forgetMeNot.add(moved);
546                  }
547              } else if (lastRetElt != null) {
# Line 784 | Line 782 | public class PriorityQueue<E> extends Ab
782          heapify();
783      }
784  
785 <    // wrapping constructor in method avoids transient javac problems
786 <    final PriorityQueueSpliterator<E> spliterator(int origin, int fence,
789 <                                                  int expectedModCount) {
790 <        return new PriorityQueueSpliterator<E>(this, origin, fence,
791 <                                               expectedModCount);
792 <    }
793 <
794 <    public Stream<E> stream() {
795 <        int flags = Streams.STREAM_IS_SIZED;
796 <        return Streams.stream
797 <            (() -> spliterator(0, size, modCount), flags);
798 <    }
799 <    public Stream<E> parallelStream() {
800 <        int flags = Streams.STREAM_IS_SIZED;
801 <        return Streams.parallelStream
802 <            (() -> spliterator(0, size, modCount), flags);
785 >    public Spliterator<E> spliterator() {
786 >        return new PriorityQueueSpliterator<E>(this, 0, -1, 0);
787      }
788  
789 <    /** Index-based split-by-two Spliterator */
789 >    /**
790 >     * This is very similar to ArrayList Spliterator, except for extra
791 >     * null checks.
792 >     */
793      static final class PriorityQueueSpliterator<E> implements Spliterator<E> {
794          private final PriorityQueue<E> pq;
795 <        private int index;           // current index, modified on advance/split
796 <        private final int fence;     // one past last index
797 <        private final int expectedModCount; // for comodification checks
795 >        private int index;            // current index, modified on advance/split
796 >        private int fence;            // -1 until first use
797 >        private int expectedModCount; // initialized when fence set
798  
799 <        /** Create new spliterator covering the given  range */
799 >        /** Creates new spliterator covering the given range */
800          PriorityQueueSpliterator(PriorityQueue<E> pq, int origin, int fence,
801                               int expectedModCount) {
802 <            this.pq = pq; this.index = origin; this.fence = fence;
802 >            this.pq = pq;
803 >            this.index = origin;
804 >            this.fence = fence;
805              this.expectedModCount = expectedModCount;
806          }
807  
808 <        public PriorityQueueSpliterator<E> trySplit() {
809 <            int lo = index, mid = (lo + fence) >>> 1;
808 >        private int getFence() { // initialize fence to size on first use
809 >            int hi;
810 >            if ((hi = fence) < 0) {
811 >                expectedModCount = pq.modCount;
812 >                hi = fence = pq.size;
813 >            }
814 >            return hi;
815 >        }
816 >
817 >        public Spliterator<E> trySplit() {
818 >            int hi = getFence(), lo = index, mid = (lo + hi) >>> 1;
819              return (lo >= mid) ? null :
820                  new PriorityQueueSpliterator<E>(pq, lo, index = mid,
821                                                  expectedModCount);
822          }
823  
824 <        public void forEach(Consumer<? super E> block) {
825 <            Object[] a; int i, hi; // hoist accesses and checks from loop
826 <            if (block == null)
824 >        @SuppressWarnings("unchecked")
825 >        public void forEachRemaining(Consumer<? super E> action) {
826 >            int i, hi, mc; // hoist accesses and checks from loop
827 >            PriorityQueue<E> q; Object[] a;
828 >            if (action == null)
829                  throw new NullPointerException();
830 <            if ((a = pq.queue).length >= (hi = fence) &&
831 <                (i = index) >= 0 && i < hi) {
832 <                index = hi;
833 <                do {
834 <                    @SuppressWarnings("unchecked") E e = (E) a[i];
835 <                    block.accept(e);
836 <                } while (++i < hi);
837 <                if (pq.modCount != expectedModCount)
838 <                    throw new ConcurrentModificationException();
830 >            if ((q = pq) != null && (a = q.queue) != null) {
831 >                if ((hi = fence) < 0) {
832 >                    mc = q.modCount;
833 >                    hi = q.size;
834 >                }
835 >                else
836 >                    mc = expectedModCount;
837 >                if ((i = index) >= 0 && (index = hi) <= a.length) {
838 >                    for (E e;; ++i) {
839 >                        if (i < hi) {
840 >                            if ((e = (E) a[i]) == null) // must be CME
841 >                                break;
842 >                            action.accept(e);
843 >                        }
844 >                        else if (q.modCount != mc)
845 >                            break;
846 >                        else
847 >                            return;
848 >                    }
849 >                }
850              }
851 +            throw new ConcurrentModificationException();
852          }
853  
854 <        public boolean tryAdvance(Consumer<? super E> block) {
855 <            if (index >= 0 && index < fence) {
856 <                @SuppressWarnings("unchecked") E e =
857 <                    (E)pq.queue[index++];
858 <                block.accept(e);
854 >        public boolean tryAdvance(Consumer<? super E> action) {
855 >            int hi = getFence(), lo = index;
856 >            if (lo >= 0 && lo < hi) {
857 >                index = lo + 1;
858 >                @SuppressWarnings("unchecked") E e = (E)pq.queue[lo];
859 >                if (e == null)
860 >                    throw new ConcurrentModificationException();
861 >                action.accept(e);
862                  if (pq.modCount != expectedModCount)
863                      throw new ConcurrentModificationException();
864                  return true;
# Line 851 | Line 866 | public class PriorityQueue<E> extends Ab
866              return false;
867          }
868  
869 <        public long estimateSize() { return (long)(fence - index); }
870 <        public boolean hasExactSize() { return true; }
871 <        public boolean hasExactSplits() { return true; }
869 >        public long estimateSize() {
870 >            return (long) (getFence() - index);
871 >        }
872 >
873 >        public int characteristics() {
874 >            return Spliterator.SIZED | Spliterator.SUBSIZED | Spliterator.NONNULL;
875 >        }
876      }
877   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines