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.124 by jsr166, Sun May 6 19:35:51 2018 UTC vs.
Revision 1.125 by jsr166, Sun May 6 21:07:41 2018 UTC

# Line 242 | Line 242 | public class PriorityQueue<E> extends Ab
242          initElementsFromCollection(c);
243      }
244  
245 +    /** Ensures that queue[0] exists, helping peek() and poll(). */
246 +    private static Object[] ensureNonEmpty(Object[] es) {
247 +        return (es.length > 0) ? es : new Object[1];
248 +    }
249 +
250      private void initFromPriorityQueue(PriorityQueue<? extends E> c) {
251          if (c.getClass() == PriorityQueue.class) {
252 <            this.queue = c.toArray();
252 >            this.queue = ensureNonEmpty(c.toArray());
253              this.size = c.size();
254          } else {
255              initFromCollection(c);
# Line 261 | Line 266 | public class PriorityQueue<E> extends Ab
266              for (Object e : es)
267                  if (e == null)
268                      throw new NullPointerException();
269 <        this.queue = es;
269 >        this.queue = ensureNonEmpty(es);
270          this.size = len;
271      }
272  
# Line 343 | Line 348 | public class PriorityQueue<E> extends Ab
348      }
349  
350      public E peek() {
351 <        return (size == 0) ? null : (E) queue[0];
351 >        return (E) queue[0];
352      }
353  
354      private int indexOf(Object o) {
# Line 579 | Line 584 | public class PriorityQueue<E> extends Ab
584      }
585  
586      public E poll() {
587 <        if (size == 0)
588 <            return null;
589 <        int s = --size;
590 <        modCount++;
591 <        E result = (E) queue[0];
592 <        E x = (E) queue[s];
593 <        queue[s] = null;
594 <        if (s != 0)
595 <            siftDown(0, x);
587 >        final Object[] es;
588 >        final E result;
589 >
590 >        if ((result = (E) ((es = queue)[0])) != null) {
591 >            modCount++;
592 >            final int n;
593 >            final E x = (E) es[(n = --size)];
594 >            es[n] = null;
595 >            if (n > 0) {
596 >                final Comparator<? super E> cmp;
597 >                if ((cmp = comparator) == null)
598 >                    siftDownComparable(0, x, es, n);
599 >                else
600 >                    siftDownUsingComparator(0, x, es, n, cmp);
601 >            }
602 >        }
603          return result;
604      }
605  
# Line 727 | Line 739 | public class PriorityQueue<E> extends Ab
739      private void heapify() {
740          final Object[] es = queue;
741          int n = size, i = (n >>> 1) - 1;
742 <        Comparator<? super E> cmp = comparator;
743 <        if (cmp == null)
742 >        final Comparator<? super E> cmp;
743 >        if ((cmp = comparator) == null)
744              for (; i >= 0; i--)
745                  siftDownComparable(i, (E) es[i], es, n);
746          else
# Line 790 | Line 802 | public class PriorityQueue<E> extends Ab
802          s.readInt();
803  
804          SharedSecrets.getJavaObjectInputStreamAccess().checkArray(s, Object[].class, size);
805 <        queue = new Object[size];
805 >        final Object[] es = queue = new Object[Math.max(size, 1)];
806  
807          // Read in all elements.
796        final Object[] es = queue;
808          for (int i = 0, n = size; i < n; i++)
809              es[i] = s.readObject();
810  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines