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.95 by dl, Wed Mar 27 23:09:35 2013 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines