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.113 by jsr166, Wed Nov 30 03:31:47 2016 UTC vs.
Revision 1.121 by jsr166, Wed Mar 28 02:50:41 2018 UTC

# Line 1 | Line 1
1   /*
2 < * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
2 > * Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
3   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4   *
5   * This code is free software; you can redistribute it and/or modify it
# Line 26 | Line 26
26   package java.util;
27  
28   import java.util.function.Consumer;
29 + import jdk.internal.misc.SharedSecrets;
30  
31   /**
32   * An unbounded priority {@linkplain Queue queue} based on a priority heap.
# Line 73 | Line 74 | import java.util.function.Consumer;
74   * ({@code peek}, {@code element}, and {@code size}).
75   *
76   * <p>This class is a member of the
77 < * <a href="{@docRoot}/../technotes/guides/collections/index.html">
77 > * <a href="{@docRoot}/java/util/package-summary.html#CollectionsFramework">
78   * Java Collections Framework</a>.
79   *
80   * @since 1.5
# Line 522 | Line 523 | public class PriorityQueue<E> extends Ab
523           */
524          private int expectedModCount = modCount;
525  
526 +        Itr() {}                        // prevent access constructor creation
527 +
528          public boolean hasNext() {
529              return cursor < size ||
530                  (forgetMeNot != null && !forgetMeNot.isEmpty());
# Line 631 | Line 634 | public class PriorityQueue<E> extends Ab
634       * promoting x up the tree until it is greater than or equal to
635       * its parent, or is the root.
636       *
637 <     * To simplify and speed up coercions and comparisons. the
637 >     * To simplify and speed up coercions and comparisons, the
638       * Comparable and Comparator versions are separated into different
639       * methods that are otherwise identical. (Similarly for siftDown.)
640       *
# Line 731 | Line 734 | public class PriorityQueue<E> extends Ab
734       */
735      @SuppressWarnings("unchecked")
736      private void heapify() {
737 <        for (int i = (size >>> 1) - 1; i >= 0; i--)
738 <            siftDown(i, (E) queue[i]);
737 >        final Object[] es = queue;
738 >        int i = (size >>> 1) - 1;
739 >        if (comparator == null)
740 >            for (; i >= 0; i--)
741 >                siftDownComparable(i, (E) es[i]);
742 >        else
743 >            for (; i >= 0; i--)
744 >                siftDownUsingComparator(i, (E) es[i]);
745      }
746  
747      /**
# Line 787 | Line 796 | public class PriorityQueue<E> extends Ab
796          // Read in (and discard) array length
797          s.readInt();
798  
799 +        SharedSecrets.getJavaObjectInputStreamAccess().checkArray(s, Object[].class, size);
800          queue = new Object[size];
801  
802          // Read in all elements.
# Line 817 | Line 827 | public class PriorityQueue<E> extends Ab
827      }
828  
829      final class PriorityQueueSpliterator implements Spliterator<E> {
820        /*
821         * This is very similar to ArrayList Spliterator, except for
822         * extra null checks.
823         */
830          private int index;            // current index, modified on advance/split
831          private int fence;            // -1 until first use
832          private int expectedModCount; // initialized when fence set
# Line 849 | Line 855 | public class PriorityQueue<E> extends Ab
855  
856          @SuppressWarnings("unchecked")
857          public void forEachRemaining(Consumer<? super E> action) {
852            int i, hi, mc; // hoist accesses and checks from loop
853            final Object[] a;
858              if (action == null)
859                  throw new NullPointerException();
860 <            if ((a = queue) != null) {
861 <                if ((hi = fence) < 0) {
862 <                    mc = modCount;
863 <                    hi = size;
864 <                }
865 <                else
866 <                    mc = expectedModCount;
863 <                if ((i = index) >= 0 && (index = hi) <= a.length) {
864 <                    for (E e;; ++i) {
865 <                        if (i < hi) {
866 <                            if ((e = (E) a[i]) == null) // must be CME
867 <                                break;
868 <                            action.accept(e);
869 <                        }
870 <                        else if (modCount != mc)
871 <                            break;
872 <                        else
873 <                            return;
874 <                    }
875 <                }
860 >            if (fence < 0) { fence = size; expectedModCount = modCount; }
861 >            final Object[] a = queue;
862 >            int i, hi; E e;
863 >            for (i = index, index = hi = fence; i < hi; i++) {
864 >                if ((e = (E) a[i]) == null)
865 >                    break;      // must be CME
866 >                action.accept(e);
867              }
868 <            throw new ConcurrentModificationException();
868 >            if (modCount != expectedModCount)
869 >                throw new ConcurrentModificationException();
870          }
871  
872 +        @SuppressWarnings("unchecked")
873          public boolean tryAdvance(Consumer<? super E> action) {
874              if (action == null)
875                  throw new NullPointerException();
876 <            int hi = getFence(), lo = index;
877 <            if (lo >= 0 && lo < hi) {
878 <                index = lo + 1;
879 <                @SuppressWarnings("unchecked") E e = (E)queue[lo];
880 <                if (e == null)
876 >            if (fence < 0) { fence = size; expectedModCount = modCount; }
877 >            int i;
878 >            if ((i = index) < fence) {
879 >                index = i + 1;
880 >                E e;
881 >                if ((e = (E) queue[i]) == null
882 >                    || modCount != expectedModCount)
883                      throw new ConcurrentModificationException();
884                  action.accept(e);
890                if (modCount != expectedModCount)
891                    throw new ConcurrentModificationException();
885                  return true;
886              }
887              return false;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines