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.83 by jsr166, Sat Jan 19 17:33:55 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.Block;
30  
31   /**
32   * An unbounded priority {@linkplain Queue queue} based on a priority heap.
# Line 451 | Line 450 | public class PriorityQueue<E> extends Ab
450       *         this queue
451       * @throws NullPointerException if the specified array is null
452       */
453 +    @SuppressWarnings("unchecked")
454      public <T> T[] toArray(T[] a) {
455 +        final int size = this.size;
456          if (a.length < size)
457              // Make a new array of a's runtime type, but my contents:
458              return (T[]) Arrays.copyOf(queue, size, a.getClass());
# Line 541 | 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 782 | 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,
788 <                                                  int expectedModCount) {
789 <        return new PriorityQueueSpliterator<E>(this, origin, fence,
790 <                                               expectedModCount);
791 <    }
792 <
793 <    public Stream<E> stream() {
794 <        int flags = Streams.STREAM_IS_SIZED;
794 <        return Streams.stream
795 <            (() -> spliterator(0, size, modCount), flags);
796 <    }
797 <    public Stream<E> parallelStream() {
798 <        int flags = Streams.STREAM_IS_SIZED;
799 <        return Streams.parallelStream
800 <            (() -> spliterator(0, size, modCount), flags);
801 <    }
802 <
803 <    /** Index-based split-by-two Spliterator */
804 <    static final class PriorityQueueSpliterator<E>
805 <        implements Spliterator<E>, Iterator<E> {
786 >    public Spliterator<E> spliterator() {
787 >        return new PriorityQueueSpliterator<E>(this, 0, -1, 0);
788 >    }
789 >
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);
822 >                                                expectedModCount);
823          }
824  
825 <        public void forEach(Block<? 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(Block<? super E> block) {
856 <            if (index >= 0 && index < fence) {
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();
845                @SuppressWarnings("unchecked") E e =
846                    (E)pq.queue[index++];
847                block.accept(e);
865                  return true;
866              }
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; }
856 <
857 <        // Iterator support
858 <        public Iterator<E> iterator() { return this; }
859 <        public void remove() { throw new UnsupportedOperationException(); }
860 <        public boolean hasNext() { return index >= 0 && index < fence; }
870 >        public long estimateSize() {
871 >            return (long) (getFence() - index);
872 >        }
873  
874 <        public E next() {
875 <            if (index < 0 || index >= fence)
864 <                throw new NoSuchElementException();
865 <            if (pq.modCount != expectedModCount)
866 <                throw new ConcurrentModificationException();
867 <            @SuppressWarnings("unchecked") E e =
868 <                (E) pq.queue[index++];
869 <            return e;
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