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.82 by jsr166, Wed Jan 16 21:18:50 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.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;
29  
30   /**
31   * An unbounded priority {@linkplain Queue queue} based on a priority heap.
# Line 451 | Line 449 | public class PriorityQueue<E> extends Ab
449       *         this queue
450       * @throws NullPointerException if the specified array is null
451       */
452 +    @SuppressWarnings("unchecked")
453      public <T> T[] toArray(T[] a) {
454 +        final int size = this.size;
455          if (a.length < size)
456              // Make a new array of a's runtime type, but my contents:
457              return (T[]) Arrays.copyOf(queue, size, a.getClass());
# Line 541 | 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 782 | 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,
787 <                                                  int expectedModCount) {
788 <        return new PriorityQueueSpliterator(this, origin, fence,
789 <                                            expectedModCount);
790 <    }
791 <
792 <    public Stream<E> stream() {
793 <        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> {
785 >    public Spliterator<E> spliterator() {
786 >        return new PriorityQueueSpliterator<E>(this, 0, -1, 0);
787 >    }
788 >
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);
821 >                                                expectedModCount);
822          }
823  
824 <        public void forEach(Block<? 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(Block<? super E> block) {
855 <            if (index >= 0 && index < fence) {
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();
845                @SuppressWarnings("unchecked") E e =
846                    (E)pq.queue[index++];
847                block.accept(e);
864                  return true;
865              }
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; }
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; }
869 >        public long estimateSize() {
870 >            return (long) (getFence() - index);
871 >        }
872  
873 <        public E next() {
874 <            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;
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