1 |
|
/* |
2 |
< |
* Copyright 2003-2006 Sun Microsystems, Inc. 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 |
18 |
|
* 2 along with this work; if not, write to the Free Software Foundation, |
19 |
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
20 |
|
* |
21 |
< |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
22 |
< |
* CA 95054 USA or visit www.sun.com if you need additional information or |
23 |
< |
* have any questions. |
21 |
> |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
> |
* or visit www.oracle.com if you need additional information or have any |
23 |
> |
* questions. |
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. |
59 |
|
* the priority queue in any particular order. If you need ordered |
60 |
|
* traversal, consider using {@code Arrays.sort(pq.toArray())}. |
61 |
|
* |
62 |
< |
* <p> <strong>Note that this implementation is not synchronized.</strong> |
62 |
> |
* <p><strong>Note that this implementation is not synchronized.</strong> |
63 |
|
* Multiple threads should not access a {@code PriorityQueue} |
64 |
|
* instance concurrently if any of the threads modifies the queue. |
65 |
|
* Instead, use the thread-safe {@link |
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. |
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 |
333 |
|
return true; |
334 |
|
} |
335 |
|
|
336 |
+ |
@SuppressWarnings("unchecked") |
337 |
|
public E peek() { |
338 |
< |
if (size == 0) |
335 |
< |
return null; |
336 |
< |
return (E) queue[0]; |
338 |
> |
return (size == 0) ? null : (E) queue[0]; |
339 |
|
} |
340 |
|
|
341 |
|
private int indexOf(Object o) { |
432 |
|
* precise control over the runtime type of the output array, and may, |
433 |
|
* under certain circumstances, be used to save allocation costs. |
434 |
|
* |
435 |
< |
* <p>Suppose <tt>x</tt> is a queue known to contain only strings. |
435 |
> |
* <p>Suppose {@code x} is a queue known to contain only strings. |
436 |
|
* The following code can be used to dump the queue into a newly |
437 |
< |
* allocated array of <tt>String</tt>: |
437 |
> |
* allocated array of {@code String}: |
438 |
|
* |
439 |
< |
* <pre> |
438 |
< |
* String[] y = x.toArray(new String[0]);</pre> |
439 |
> |
* <pre> {@code String[] y = x.toArray(new String[0]);}</pre> |
440 |
|
* |
441 |
< |
* Note that <tt>toArray(new Object[0])</tt> is identical in function to |
442 |
< |
* <tt>toArray()</tt>. |
441 |
> |
* Note that {@code toArray(new Object[0])} is identical in function to |
442 |
> |
* {@code toArray()}. |
443 |
|
* |
444 |
|
* @param a the array into which the elements of the queue are to |
445 |
|
* be stored, if it is big enough; otherwise, a new array of the |
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()); |
517 |
|
(forgetMeNot != null && !forgetMeNot.isEmpty()); |
518 |
|
} |
519 |
|
|
520 |
+ |
@SuppressWarnings("unchecked") |
521 |
|
public E next() { |
522 |
|
if (expectedModCount != modCount) |
523 |
|
throw new ConcurrentModificationException(); |
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) { |
570 |
|
size = 0; |
571 |
|
} |
572 |
|
|
573 |
+ |
@SuppressWarnings("unchecked") |
574 |
|
public E poll() { |
575 |
|
if (size == 0) |
576 |
|
return null; |
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; |
601 |
> |
// assert i >= 0 && i < size; |
602 |
|
modCount++; |
603 |
|
int s = --size; |
604 |
|
if (s == i) // removed last element |
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) { |
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; |
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 |
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) { |
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]); |
738 |
|
} |
739 |
|
|
740 |
|
/** |
741 |
< |
* Saves the state of the instance to a stream (that |
731 |
< |
* is, serializes it). |
741 |
> |
* Saves this queue to a stream (that is, serializes it). |
742 |
|
* |
743 |
|
* @serialData The length of the array backing the instance is |
744 |
|
* emitted (int), followed by all of its elements |
746 |
|
* @param s the stream |
747 |
|
*/ |
748 |
|
private void writeObject(java.io.ObjectOutputStream s) |
749 |
< |
throws java.io.IOException{ |
749 |
> |
throws java.io.IOException { |
750 |
|
// Write out element count, and any hidden stuff |
751 |
|
s.defaultWriteObject(); |
752 |
|
|
782 |
|
// spec has never explained what that might be. |
783 |
|
heapify(); |
784 |
|
} |
785 |
+ |
|
786 |
+ |
public Spliterator<E> spliterator() { |
787 |
+ |
return new PriorityQueueSpliterator<E>(this, 0, -1, 0); |
788 |
+ |
} |
789 |
+ |
|
790 |
+ |
/** |
791 |
+ |
* This is very similar to ArrayList Spliterator, except for extra |
792 |
+ |
* null checks. |
793 |
+ |
*/ |
794 |
+ |
static final class PriorityQueueSpliterator<E> implements Spliterator<E> { |
795 |
+ |
private final PriorityQueue<E> pq; |
796 |
+ |
private int index; // current index, modified on advance/split |
797 |
+ |
private int fence; // -1 until first use |
798 |
+ |
private int expectedModCount; // initialized when fence set |
799 |
+ |
|
800 |
+ |
/** Creates new spliterator covering the given range */ |
801 |
+ |
PriorityQueueSpliterator(PriorityQueue<E> pq, int origin, int fence, |
802 |
+ |
int expectedModCount) { |
803 |
+ |
this.pq = pq; |
804 |
+ |
this.index = origin; |
805 |
+ |
this.fence = fence; |
806 |
+ |
this.expectedModCount = expectedModCount; |
807 |
+ |
} |
808 |
+ |
|
809 |
+ |
private int getFence() { // initialize fence to size on first use |
810 |
+ |
int hi; |
811 |
+ |
if ((hi = fence) < 0) { |
812 |
+ |
expectedModCount = pq.modCount; |
813 |
+ |
hi = fence = pq.size; |
814 |
+ |
} |
815 |
+ |
return hi; |
816 |
+ |
} |
817 |
+ |
|
818 |
+ |
public Spliterator<E> trySplit() { |
819 |
+ |
int hi = getFence(), lo = index, mid = (lo + hi) >>> 1; |
820 |
+ |
return (lo >= mid) ? null : |
821 |
+ |
new PriorityQueueSpliterator<E>(pq, lo, index = mid, |
822 |
+ |
expectedModCount); |
823 |
+ |
} |
824 |
+ |
|
825 |
+ |
@SuppressWarnings("unchecked") |
826 |
+ |
public void forEachRemaining(Consumer<? super E> action) { |
827 |
+ |
int i, hi, mc; // hoist accesses and checks from loop |
828 |
+ |
PriorityQueue<E> q; Object[] a; |
829 |
+ |
if (action == null) |
830 |
+ |
throw new NullPointerException(); |
831 |
+ |
if ((q = pq) != null && (a = q.queue) != null) { |
832 |
+ |
if ((hi = fence) < 0) { |
833 |
+ |
mc = q.modCount; |
834 |
+ |
hi = q.size; |
835 |
+ |
} |
836 |
+ |
else |
837 |
+ |
mc = expectedModCount; |
838 |
+ |
if ((i = index) >= 0 && (index = hi) <= a.length) { |
839 |
+ |
for (E e;; ++i) { |
840 |
+ |
if (i < hi) { |
841 |
+ |
if ((e = (E) a[i]) == null) // must be CME |
842 |
+ |
break; |
843 |
+ |
action.accept(e); |
844 |
+ |
} |
845 |
+ |
else if (q.modCount != mc) |
846 |
+ |
break; |
847 |
+ |
else |
848 |
+ |
return; |
849 |
+ |
} |
850 |
+ |
} |
851 |
+ |
} |
852 |
+ |
throw new ConcurrentModificationException(); |
853 |
+ |
} |
854 |
+ |
|
855 |
+ |
public boolean tryAdvance(Consumer<? super E> action) { |
856 |
+ |
int hi = getFence(), lo = index; |
857 |
+ |
if (lo >= 0 && lo < hi) { |
858 |
+ |
index = lo + 1; |
859 |
+ |
@SuppressWarnings("unchecked") E e = (E)pq.queue[lo]; |
860 |
+ |
if (e == null) |
861 |
+ |
throw new ConcurrentModificationException(); |
862 |
+ |
action.accept(e); |
863 |
+ |
if (pq.modCount != expectedModCount) |
864 |
+ |
throw new ConcurrentModificationException(); |
865 |
+ |
return true; |
866 |
+ |
} |
867 |
+ |
return false; |
868 |
+ |
} |
869 |
+ |
|
870 |
+ |
public long estimateSize() { |
871 |
+ |
return (long) (getFence() - index); |
872 |
+ |
} |
873 |
+ |
|
874 |
+ |
public int characteristics() { |
875 |
+ |
return Spliterator.SIZED | Spliterator.SUBSIZED | Spliterator.NONNULL; |
876 |
+ |
} |
877 |
+ |
} |
878 |
|
} |