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.88 by dl, Fri Feb 1 16:23:04 2013 UTC vs.
Revision 1.89 by dl, Sun Feb 17 23:36:16 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 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);
786 >    final Spliterator<E> spliterator() {
787 >        return new PriorityQueueSpliterator<E>(this, 0, -1, 0);
788      }
789  
790      public Stream<E> stream() {
791 <        int flags = Streams.STREAM_IS_SIZED;
796 <        return Streams.stream
797 <            (() -> spliterator(0, size, modCount), flags);
791 >        return Streams.stream(spliterator());
792      }
793 +
794      public Stream<E> parallelStream() {
795 <        int flags = Streams.STREAM_IS_SIZED;
801 <        return Streams.parallelStream
802 <            (() -> spliterator(0, size, modCount), flags);
795 >        return Streams.parallelStream(spliterator());
796      }
797  
805    /** Index-based split-by-two Spliterator */
798      static final class PriorityQueueSpliterator<E> implements Spliterator<E> {
799 +        /*
800 +         * This is very similar to ArrayList Spliterator, except for
801 +         * extra null checks.
802 +         */
803          private final PriorityQueue<E> pq;
804 <        private int index;           // current index, modified on advance/split
805 <        private final int fence;     // one past last index
806 <        private final int expectedModCount; // for comodification checks
804 >        private int index;            // current index, modified on advance/split
805 >        private int fence;            // -1 until first use
806 >        private int expectedModCount; // initialized when fence set
807  
808          /** Create new spliterator covering the given  range */
809          PriorityQueueSpliterator(PriorityQueue<E> pq, int origin, int fence,
810                               int expectedModCount) {
811 <            this.pq = pq; this.index = origin; this.fence = fence;
811 >            this.pq = pq;
812 >            this.index = origin;
813 >            this.fence = fence;
814              this.expectedModCount = expectedModCount;
815          }
816  
817 +        private int getFence() { // initialize fence to size on first use
818 +            int hi;
819 +            if ((hi = fence) < 0) {
820 +                expectedModCount = pq.modCount;
821 +                hi = fence = pq.size;
822 +            }
823 +            return hi;
824 +        }
825 +            
826          public PriorityQueueSpliterator<E> trySplit() {
827 <            int lo = index, mid = (lo + fence) >>> 1;
827 >            int hi = getFence(), lo = index, mid = (lo + hi) >>> 1;
828              return (lo >= mid) ? null :
829 <                new PriorityQueueSpliterator<E>(pq, lo, index = mid,
829 >                new PriorityQueueSpliterator<E>(pq, lo, index = mid,
830                                                  expectedModCount);
831          }
832  
833 <        public void forEach(Consumer<? super E> block) {
834 <            Object[] a; int i, hi; // hoist accesses and checks from loop
835 <            if (block == null)
833 >        @SuppressWarnings("unchecked")
834 >        public void forEach(Consumer<? super E> action) {
835 >            int i, hi, mc; // hoist accesses and checks from loop
836 >            PriorityQueue<E> q; Object[] a;
837 >            if (action == null)
838                  throw new NullPointerException();
839 <            if ((a = pq.queue).length >= (hi = fence) &&
840 <                (i = index) >= 0 && i < hi) {
841 <                index = hi;
842 <                do {
843 <                    @SuppressWarnings("unchecked") E e = (E) a[i];
844 <                    block.accept(e);
845 <                } while (++i < hi);
846 <                if (pq.modCount != expectedModCount)
847 <                    throw new ConcurrentModificationException();
839 >            if ((q = pq) != null && (a = q.queue) != null) {
840 >                if ((hi = fence) < 0) {
841 >                    mc = q.modCount;
842 >                    hi = q.size;
843 >                }
844 >                else
845 >                    mc = expectedModCount;
846 >                if ((i = index) >= 0 && (index = hi) <= a.length) {
847 >                    for (E e;; ++i) {
848 >                        if (i < hi) {
849 >                            if ((e = (E) a[i]) == null) // must be CME
850 >                                break;
851 >                            action.accept(e);
852 >                        }
853 >                        else if (q.modCount != mc)
854 >                            break;
855 >                        else
856 >                            return;
857 >                    }
858 >                }
859              }
860 +            throw new ConcurrentModificationException();
861          }
862  
863 <        public boolean tryAdvance(Consumer<? super E> block) {
864 <            if (index >= 0 && index < fence) {
865 <                @SuppressWarnings("unchecked") E e =
866 <                    (E)pq.queue[index++];
867 <                block.accept(e);
863 >        public boolean tryAdvance(Consumer<? super E> action) {
864 >            int hi = getFence(), lo = index;
865 >            if (lo >= 0 && lo < hi) {
866 >                index = lo + 1;
867 >                @SuppressWarnings("unchecked") E e = (E)pq.queue[lo];
868 >                if (e == null)
869 >                    throw new ConcurrentModificationException();
870 >                action.accept(e);
871                  if (pq.modCount != expectedModCount)
872                      throw new ConcurrentModificationException();
873                  return true;
# Line 851 | Line 875 | public class PriorityQueue<E> extends Ab
875              return false;
876          }
877  
878 <        public long estimateSize() { return (long)(fence - index); }
879 <        public boolean hasExactSize() { return true; }
880 <        public boolean hasExactSplits() { return true; }
878 >        public long estimateSize() {
879 >            return (long) (getFence() - index);
880 >        }
881 >
882 >        public int characteristics() {
883 >            return Spliterator.SIZED | Spliterator.SUBSIZED | Spliterator.NONNULL;
884 >        }
885      }
886   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines