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.109 by jsr166, Wed Jun 1 16:08:04 2016 UTC vs.
Revision 1.115 by jsr166, Fri Dec 2 07:11:36 2016 UTC

# Line 54 | Line 54 | import java.util.function.Consumer;
54   * <p>This class and its iterator implement all of the
55   * <em>optional</em> methods of the {@link Collection} and {@link
56   * Iterator} interfaces.  The Iterator provided in method {@link
57 < * #iterator()} is <em>not</em> guaranteed to traverse the elements of
57 > * #iterator()} and the Spliterator provided in method {@link #spliterator()}
58 > * are <em>not</em> guaranteed to traverse the elements of
59   * the priority queue in any particular order. If you need ordered
60   * traversal, consider using {@code Arrays.sort(pq.toArray())}.
61   *
# Line 726 | Line 727 | public class PriorityQueue<E> extends Ab
727      /**
728       * Establishes the heap invariant (described above) in the entire tree,
729       * assuming nothing about the order of the elements prior to the call.
730 +     * This classic algorithm due to Floyd (1964) is known to be O(size).
731       */
732      @SuppressWarnings("unchecked")
733      private void heapify() {
734 <        for (int i = (size >>> 1) - 1; i >= 0; i--)
735 <            siftDown(i, (E) queue[i]);
734 >        final Object[] es = queue;
735 >        final int half = (size >>> 1) - 1;
736 >        if (comparator == null)
737 >            for (int i = half; i >= 0; i--)
738 >                siftDownComparable(i, (E) es[i]);
739 >        else
740 >            for (int i = half; i >= 0; i--)
741 >                siftDownUsingComparator(i, (E) es[i]);
742      }
743  
744      /**
# Line 749 | Line 757 | public class PriorityQueue<E> extends Ab
757      /**
758       * Saves this queue to a stream (that is, serializes it).
759       *
760 +     * @param s the stream
761 +     * @throws java.io.IOException if an I/O error occurs
762       * @serialData The length of the array backing the instance is
763       *             emitted (int), followed by all of its elements
764       *             (each an {@code Object}) in the proper order.
755     * @param s the stream
756     * @throws java.io.IOException if an I/O error occurs
765       */
766      private void writeObject(java.io.ObjectOutputStream s)
767          throws java.io.IOException {
# Line 799 | Line 807 | public class PriorityQueue<E> extends Ab
807      /**
808       * Creates a <em><a href="Spliterator.html#binding">late-binding</a></em>
809       * and <em>fail-fast</em> {@link Spliterator} over the elements in this
810 <     * queue.
810 >     * queue. The spliterator does not traverse elements in any particular order
811 >     * (the {@link Spliterator#ORDERED ORDERED} characteristic is not reported).
812       *
813       * <p>The {@code Spliterator} reports {@link Spliterator#SIZED},
814       * {@link Spliterator#SUBSIZED}, and {@link Spliterator#NONNULL}.
# Line 810 | Line 819 | public class PriorityQueue<E> extends Ab
819       * @since 1.8
820       */
821      public final Spliterator<E> spliterator() {
822 <        return new PriorityQueueSpliterator<>(this, 0, -1, 0);
822 >        return new PriorityQueueSpliterator(0, -1, 0);
823      }
824  
825 <    static final class PriorityQueueSpliterator<E> implements Spliterator<E> {
817 <        /*
818 <         * This is very similar to ArrayList Spliterator, except for
819 <         * extra null checks.
820 <         */
821 <        private final PriorityQueue<E> pq;
825 >    final class PriorityQueueSpliterator implements Spliterator<E> {
826          private int index;            // current index, modified on advance/split
827          private int fence;            // -1 until first use
828          private int expectedModCount; // initialized when fence set
829  
830          /** Creates new spliterator covering the given range. */
831 <        PriorityQueueSpliterator(PriorityQueue<E> pq, int origin, int fence,
828 <                                 int expectedModCount) {
829 <            this.pq = pq;
831 >        PriorityQueueSpliterator(int origin, int fence, int expectedModCount) {
832              this.index = origin;
833              this.fence = fence;
834              this.expectedModCount = expectedModCount;
# Line 835 | Line 837 | public class PriorityQueue<E> extends Ab
837          private int getFence() { // initialize fence to size on first use
838              int hi;
839              if ((hi = fence) < 0) {
840 <                expectedModCount = pq.modCount;
841 <                hi = fence = pq.size;
840 >                expectedModCount = modCount;
841 >                hi = fence = size;
842              }
843              return hi;
844          }
845  
846 <        public PriorityQueueSpliterator<E> trySplit() {
846 >        public PriorityQueueSpliterator trySplit() {
847              int hi = getFence(), lo = index, mid = (lo + hi) >>> 1;
848              return (lo >= mid) ? null :
849 <                new PriorityQueueSpliterator<>(pq, lo, index = mid,
848 <                                               expectedModCount);
849 >                new PriorityQueueSpliterator(lo, index = mid, expectedModCount);
850          }
851  
852          @SuppressWarnings("unchecked")
853          public void forEachRemaining(Consumer<? super E> action) {
853            int i, hi, mc; // hoist accesses and checks from loop
854            PriorityQueue<E> q; Object[] a;
854              if (action == null)
855                  throw new NullPointerException();
856 <            if ((q = pq) != null && (a = q.queue) != null) {
857 <                if ((hi = fence) < 0) {
858 <                    mc = q.modCount;
859 <                    hi = q.size;
860 <                }
861 <                else
862 <                    mc = expectedModCount;
864 <                if ((i = index) >= 0 && (index = hi) <= a.length) {
865 <                    for (E e;; ++i) {
866 <                        if (i < hi) {
867 <                            if ((e = (E) a[i]) == null) // must be CME
868 <                                break;
869 <                            action.accept(e);
870 <                        }
871 <                        else if (q.modCount != mc)
872 <                            break;
873 <                        else
874 <                            return;
875 <                    }
876 <                }
856 >            if (fence < 0) { fence = size; expectedModCount = modCount; }
857 >            final Object[] a = queue;
858 >            int i, hi; E e;
859 >            for (i = index, index = hi = fence; i < hi; i++) {
860 >                if ((e = (E) a[i]) == null)
861 >                    break;      // must be CME
862 >                action.accept(e);
863              }
864 <            throw new ConcurrentModificationException();
864 >            if (modCount != expectedModCount)
865 >                throw new ConcurrentModificationException();
866          }
867  
868 +        @SuppressWarnings("unchecked")
869          public boolean tryAdvance(Consumer<? super E> action) {
870              if (action == null)
871                  throw new NullPointerException();
872 <            int hi = getFence(), lo = index;
873 <            if (lo >= 0 && lo < hi) {
874 <                index = lo + 1;
875 <                @SuppressWarnings("unchecked") E e = (E)pq.queue[lo];
876 <                if (e == null)
872 >            if (fence < 0) { fence = size; expectedModCount = modCount; }
873 >            int i;
874 >            if ((i = index) < fence) {
875 >                index = i + 1;
876 >                E e;
877 >                if ((e = (E) queue[i]) == null
878 >                    || modCount != expectedModCount)
879                      throw new ConcurrentModificationException();
880                  action.accept(e);
891                if (pq.modCount != expectedModCount)
892                    throw new ConcurrentModificationException();
881                  return true;
882              }
883              return false;
884          }
885  
886          public long estimateSize() {
887 <            return (long) (getFence() - index);
887 >            return getFence() - index;
888          }
889  
890          public int characteristics() {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines