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.112 by jsr166, Tue Nov 29 18:11:28 2016 UTC vs.
Revision 1.115 by jsr166, Fri Dec 2 07:11:36 2016 UTC

# Line 731 | Line 731 | public class PriorityQueue<E> extends Ab
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 813 | 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> {
820 <        /*
821 <         * This is very similar to ArrayList Spliterator, except for
822 <         * extra null checks.
823 <         */
824 <        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,
831 <                                 int expectedModCount) {
832 <            this.pq = pq;
831 >        PriorityQueueSpliterator(int origin, int fence, int expectedModCount) {
832              this.index = origin;
833              this.fence = fence;
834              this.expectedModCount = expectedModCount;
# Line 838 | 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,
851 <                                               expectedModCount);
849 >                new PriorityQueueSpliterator(lo, index = mid, expectedModCount);
850          }
851  
852          @SuppressWarnings("unchecked")
853          public void forEachRemaining(Consumer<? super E> action) {
856            int i, hi, mc; // hoist accesses and checks from loop
857            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;
867 <                if ((i = index) >= 0 && (index = hi) <= a.length) {
868 <                    for (E e;; ++i) {
869 <                        if (i < hi) {
870 <                            if ((e = (E) a[i]) == null) // must be CME
871 <                                break;
872 <                            action.accept(e);
873 <                        }
874 <                        else if (q.modCount != mc)
875 <                            break;
876 <                        else
877 <                            return;
878 <                    }
879 <                }
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);
894                if (pq.modCount != expectedModCount)
895                    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