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.81 by dl, Wed Jan 16 15:06:10 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.Block;
30  
31   /**
32   * An unbounded priority {@linkplain Queue queue} based on a priority heap.
# Line 60 | Line 59 | import java.util.function.Block;
59   * the priority queue in any particular order. If you need ordered
60   * traversal, consider using {@code Arrays.sort(pq.toArray())}.
61   *
62 < * <p> <strong>Note that this implementation is not synchronized.</strong>
62 > * <p><strong>Note that this implementation is not synchronized.</strong>
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
# 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,
787 <                                                  int expectedModCount) {
788 <        return new PriorityQueueSpliterator(this, origin, fence,
789 <                                            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;
794 <        return Streams.stream
795 <            (() -> spliterator(0, size, modCount), flags);
791 >        return Streams.stream(spliterator());
792      }
793 +
794      public Stream<E> parallelStream() {
795 <        int flags = Streams.STREAM_IS_SIZED;
799 <        return Streams.parallelStream
800 <            (() -> spliterator(0, size, modCount), flags);
795 >        return Streams.parallelStream(spliterator());
796      }
797  
798 <    /** Index-based split-by-two Spliterator */
799 <    static final class PriorityQueueSpliterator<E>
800 <        implements Spliterator<E>, Iterator<E> {
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,
830 <                                            expectedModCount);
829 >                new PriorityQueueSpliterator<E>(pq, lo, index = mid,
830 >                                                expectedModCount);
831          }
832  
833 <        public void forEach(Block<? 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(Block<? super E> block) {
864 <            if (index >= 0 && index < fence) {
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();
845                @SuppressWarnings("unchecked") E e =
846                    (E)pq.queue[index++];
847                block.accept(e);
873                  return true;
874              }
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; }
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; }
878 >        public long estimateSize() {
879 >            return (long) (getFence() - index);
880 >        }
881  
882 <        public E next() {
883 <            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;
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