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.80 by jsr166, Wed Jan 16 01:59:47 2013 UTC vs.
Revision 1.91 by jsr166, Mon Feb 18 03:10:15 2013 UTC

# Line 1 | Line 1
1   /*
2 < * Copyright (c) 2003, 2006, Oracle and/or its affiliates. All rights reserved.
2 > * Copyright (c) 2003, 2012, 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
6   * under the terms of the GNU General Public License version 2 only, as
7 < * published by the Free Software Foundation.  Sun designates this
7 > * published by the Free Software Foundation.  Oracle designates this
8   * particular file as subject to the "Classpath" exception as provided
9 < * by Sun in the LICENSE file that accompanied this code.
9 > * by Oracle in the LICENSE file that accompanied this code.
10   *
11   * This code is distributed in the hope that it will be useful, but WITHOUT
12   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# Line 24 | Line 24
24   */
25  
26   package java.util;
27 + import java.util.function.Consumer;
28 + import java.util.stream.Stream;
29 + import java.util.stream.Streams;
30  
31   /**
32   * An unbounded priority {@linkplain Queue queue} based on a priority heap.
# Line 63 | Line 66 | package java.util;
66   * java.util.concurrent.PriorityBlockingQueue} class.
67   *
68   * <p>Implementation note: this implementation provides
69 < * O(log(n)) time for the enqueuing and dequeuing methods
69 > * O(log(n)) time for the enqueing and dequeing methods
70   * ({@code offer}, {@code poll}, {@code remove()} and {@code add});
71   * linear time for the {@code remove(Object)} and {@code contains(Object)}
72   * methods; and constant time for the retrieval methods
# Line 77 | Line 80 | package java.util;
80   * @author Josh Bloch, Doug Lea
81   * @param <E> the type of elements held in this collection
82   */
80 @SuppressWarnings("unchecked")
83   public class PriorityQueue<E> extends AbstractQueue<E>
84      implements java.io.Serializable {
85  
# Line 93 | Line 95 | public class PriorityQueue<E> extends Ab
95       * heap and each descendant d of n, n <= d.  The element with the
96       * lowest value is in queue[0], assuming the queue is nonempty.
97       */
98 <    private transient Object[] queue;
98 >    transient Object[] queue; // non-private to simplify nested class access
99  
100      /**
101       * The number of elements in the priority queue.
# Line 110 | Line 112 | public class PriorityQueue<E> extends Ab
112       * The number of times this priority queue has been
113       * <i>structurally modified</i>.  See AbstractList for gory details.
114       */
115 <    private transient int modCount = 0;
115 >    transient int modCount = 0; // non-private to simplify nested class access
116  
117      /**
118       * Creates a {@code PriorityQueue} with the default initial
# Line 331 | Line 333 | public class PriorityQueue<E> extends Ab
333          return true;
334      }
335  
336 +    @SuppressWarnings("unchecked")
337      public E peek() {
338          return (size == 0) ? null : (E) queue[0];
339      }
# Line 447 | 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 512 | Line 517 | public class PriorityQueue<E> extends Ab
517                  (forgetMeNot != null && !forgetMeNot.isEmpty());
518          }
519  
520 +        @SuppressWarnings("unchecked")
521          public E next() {
522              if (expectedModCount != modCount)
523                  throw new ConcurrentModificationException();
# Line 536 | 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 564 | Line 570 | public class PriorityQueue<E> extends Ab
570          size = 0;
571      }
572  
573 +    @SuppressWarnings("unchecked")
574      public E poll() {
575          if (size == 0)
576              return null;
# Line 589 | Line 596 | public class PriorityQueue<E> extends Ab
596       * position before i. This fact is used by iterator.remove so as to
597       * avoid missing traversing elements.
598       */
599 +    @SuppressWarnings("unchecked")
600      private E removeAt(int i) {
601          // assert i >= 0 && i < size;
602          modCount++;
# Line 627 | Line 635 | public class PriorityQueue<E> extends Ab
635              siftUpComparable(k, x);
636      }
637  
638 +    @SuppressWarnings("unchecked")
639      private void siftUpComparable(int k, E x) {
640          Comparable<? super E> key = (Comparable<? super E>) x;
641          while (k > 0) {
# Line 640 | Line 649 | public class PriorityQueue<E> extends Ab
649          queue[k] = key;
650      }
651  
652 +    @SuppressWarnings("unchecked")
653      private void siftUpUsingComparator(int k, E x) {
654          while (k > 0) {
655              int parent = (k - 1) >>> 1;
# Line 667 | Line 677 | public class PriorityQueue<E> extends Ab
677              siftDownComparable(k, x);
678      }
679  
680 +    @SuppressWarnings("unchecked")
681      private void siftDownComparable(int k, E x) {
682          Comparable<? super E> key = (Comparable<? super E>)x;
683          int half = size >>> 1;        // loop while a non-leaf
# Line 685 | Line 696 | public class PriorityQueue<E> extends Ab
696          queue[k] = key;
697      }
698  
699 +    @SuppressWarnings("unchecked")
700      private void siftDownUsingComparator(int k, E x) {
701          int half = size >>> 1;
702          while (k < half) {
# Line 706 | Line 718 | public class PriorityQueue<E> extends Ab
718       * Establishes the heap invariant (described above) in the entire tree,
719       * assuming nothing about the order of the elements prior to the call.
720       */
721 +    @SuppressWarnings("unchecked")
722      private void heapify() {
723          for (int i = (size >>> 1) - 1; i >= 0; i--)
724              siftDown(i, (E) queue[i]);
# Line 730 | Line 743 | public class PriorityQueue<E> extends Ab
743       * @serialData The length of the array backing the instance is
744       *             emitted (int), followed by all of its elements
745       *             (each an {@code Object}) in the proper order.
746 +     * @param s the stream
747       */
748      private void writeObject(java.io.ObjectOutputStream s)
749          throws java.io.IOException {
# Line 745 | Line 759 | public class PriorityQueue<E> extends Ab
759      }
760  
761      /**
762 <     * Reconstitutes this queue from a stream (that is, deserializes it).
762 >     * Reconstitutes the {@code PriorityQueue} instance from a stream
763 >     * (that is, deserializes it).
764 >     *
765 >     * @param s the stream
766       */
767      private void readObject(java.io.ObjectInputStream s)
768          throws java.io.IOException, ClassNotFoundException {
# Line 765 | Line 782 | public class PriorityQueue<E> extends Ab
782          // spec has never explained what that might be.
783          heapify();
784      }
785 +
786 +    final Spliterator<E> spliterator() {
787 +        return new PriorityQueueSpliterator<E>(this, 0, -1, 0);
788 +    }
789 +
790 +    public Stream<E> stream() {
791 +        return Streams.stream(spliterator());
792 +    }
793 +
794 +    public Stream<E> parallelStream() {
795 +        return Streams.parallelStream(spliterator());
796 +    }
797 +
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 int fence;            // -1 until first use
806 +        private int expectedModCount; // initialized when fence set
807 +
808 +        /** Creates new spliterator covering the given  range */
809 +        PriorityQueueSpliterator(PriorityQueue<E> pq, int origin, int fence,
810 +                             int expectedModCount) {
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 hi = getFence(), lo = index, mid = (lo + hi) >>> 1;
828 +            return (lo >= mid) ? null :
829 +                new PriorityQueueSpliterator<E>(pq, lo, index = mid,
830 +                                                expectedModCount);
831 +        }
832 +
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 ((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(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();
873 +                return true;
874 +            }
875 +            return false;
876 +        }
877 +
878 +        public long estimateSize() {
879 +            return (long) (getFence() - index);
880 +        }
881 +
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