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 |
25 |
|
|
26 |
|
package java.util; |
27 |
|
|
28 |
+ |
import java.util.function.Consumer; |
29 |
+ |
import java.util.stream.Stream; |
30 |
+ |
|
31 |
|
/** |
32 |
|
* An unbounded priority {@linkplain Queue queue} based on a priority heap. |
33 |
|
* The elements of the priority queue are ordered according to their |
78 |
|
* |
79 |
|
* @since 1.5 |
80 |
|
* @author Josh Bloch, Doug Lea |
81 |
< |
* @param <E> the type of elements held in this collection |
81 |
> |
* @param <E> the type of elements held in this queue |
82 |
|
*/ |
80 |
– |
@SuppressWarnings("unchecked") |
83 |
|
public class PriorityQueue<E> extends AbstractQueue<E> |
84 |
|
implements java.io.Serializable { |
85 |
|
|
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. |
102 |
|
*/ |
103 |
< |
private int size = 0; |
103 |
> |
private int size; |
104 |
|
|
105 |
|
/** |
106 |
|
* The comparator, or null if priority queue uses elements' |
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 |
|
return (size == 0) ? null : (E) queue[0]; |
339 |
|
} |
394 |
|
* @return {@code true} if this queue contains the specified element |
395 |
|
*/ |
396 |
|
public boolean contains(Object o) { |
397 |
< |
return indexOf(o) != -1; |
397 |
> |
return indexOf(o) >= 0; |
398 |
|
} |
399 |
|
|
400 |
|
/** |
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()); |
477 |
|
* Index (into queue array) of element to be returned by |
478 |
|
* subsequent call to next. |
479 |
|
*/ |
480 |
< |
private int cursor = 0; |
480 |
> |
private int cursor; |
481 |
|
|
482 |
|
/** |
483 |
|
* Index of element returned by most recent call to next, |
497 |
|
* We expect that most iterations, even those involving removals, |
498 |
|
* will not need to store elements in this field. |
499 |
|
*/ |
500 |
< |
private ArrayDeque<E> forgetMeNot = null; |
500 |
> |
private ArrayDeque<E> forgetMeNot; |
501 |
|
|
502 |
|
/** |
503 |
|
* Element returned by the most recent call to next iff that |
504 |
|
* element was drawn from the forgetMeNot list. |
505 |
|
*/ |
506 |
< |
private E lastRetElt = null; |
506 |
> |
private E lastRetElt; |
507 |
|
|
508 |
|
/** |
509 |
|
* The modCount value that the iterator believes that the backing |
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; |
602 |
|
modCount++; |
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]); |
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 |
+ |
* @throws java.io.IOException if an I/O error occurs |
748 |
|
*/ |
749 |
|
private void writeObject(java.io.ObjectOutputStream s) |
750 |
|
throws java.io.IOException { |
760 |
|
} |
761 |
|
|
762 |
|
/** |
763 |
< |
* Reconstitutes this queue from a stream (that is, deserializes it). |
763 |
> |
* Reconstitutes the {@code PriorityQueue} instance from a stream |
764 |
> |
* (that is, deserializes it). |
765 |
> |
* |
766 |
> |
* @param s the stream |
767 |
> |
* @throws ClassNotFoundException if the class of a serialized object |
768 |
> |
* could not be found |
769 |
> |
* @throws java.io.IOException if an I/O error occurs |
770 |
|
*/ |
771 |
|
private void readObject(java.io.ObjectInputStream s) |
772 |
|
throws java.io.IOException, ClassNotFoundException { |
786 |
|
// spec has never explained what that might be. |
787 |
|
heapify(); |
788 |
|
} |
789 |
+ |
|
790 |
+ |
public Spliterator<E> spliterator() { |
791 |
+ |
return new PriorityQueueSpliterator<E>(this, 0, -1, 0); |
792 |
+ |
} |
793 |
+ |
|
794 |
+ |
/** |
795 |
+ |
* This is very similar to ArrayList Spliterator, except for extra |
796 |
+ |
* null checks. |
797 |
+ |
*/ |
798 |
+ |
static final class PriorityQueueSpliterator<E> implements Spliterator<E> { |
799 |
+ |
private final PriorityQueue<E> pq; |
800 |
+ |
private int index; // current index, modified on advance/split |
801 |
+ |
private int fence; // -1 until first use |
802 |
+ |
private int expectedModCount; // initialized when fence set |
803 |
+ |
|
804 |
+ |
/** Creates new spliterator covering the given range */ |
805 |
+ |
PriorityQueueSpliterator(PriorityQueue<E> pq, int origin, int fence, |
806 |
+ |
int expectedModCount) { |
807 |
+ |
this.pq = pq; |
808 |
+ |
this.index = origin; |
809 |
+ |
this.fence = fence; |
810 |
+ |
this.expectedModCount = expectedModCount; |
811 |
+ |
} |
812 |
+ |
|
813 |
+ |
private int getFence() { // initialize fence to size on first use |
814 |
+ |
int hi; |
815 |
+ |
if ((hi = fence) < 0) { |
816 |
+ |
expectedModCount = pq.modCount; |
817 |
+ |
hi = fence = pq.size; |
818 |
+ |
} |
819 |
+ |
return hi; |
820 |
+ |
} |
821 |
+ |
|
822 |
+ |
public Spliterator<E> trySplit() { |
823 |
+ |
int hi = getFence(), lo = index, mid = (lo + hi) >>> 1; |
824 |
+ |
return (lo >= mid) ? null : |
825 |
+ |
new PriorityQueueSpliterator<E>(pq, lo, index = mid, |
826 |
+ |
expectedModCount); |
827 |
+ |
} |
828 |
+ |
|
829 |
+ |
@SuppressWarnings("unchecked") |
830 |
+ |
public void forEachRemaining(Consumer<? super E> action) { |
831 |
+ |
int i, hi, mc; // hoist accesses and checks from loop |
832 |
+ |
PriorityQueue<E> q; Object[] a; |
833 |
+ |
if (action == null) |
834 |
+ |
throw new NullPointerException(); |
835 |
+ |
if ((q = pq) != null && (a = q.queue) != null) { |
836 |
+ |
if ((hi = fence) < 0) { |
837 |
+ |
mc = q.modCount; |
838 |
+ |
hi = q.size; |
839 |
+ |
} |
840 |
+ |
else |
841 |
+ |
mc = expectedModCount; |
842 |
+ |
if ((i = index) >= 0 && (index = hi) <= a.length) { |
843 |
+ |
for (E e;; ++i) { |
844 |
+ |
if (i < hi) { |
845 |
+ |
if ((e = (E) a[i]) == null) // must be CME |
846 |
+ |
break; |
847 |
+ |
action.accept(e); |
848 |
+ |
} |
849 |
+ |
else if (q.modCount != mc) |
850 |
+ |
break; |
851 |
+ |
else |
852 |
+ |
return; |
853 |
+ |
} |
854 |
+ |
} |
855 |
+ |
} |
856 |
+ |
throw new ConcurrentModificationException(); |
857 |
+ |
} |
858 |
+ |
|
859 |
+ |
public boolean tryAdvance(Consumer<? super E> action) { |
860 |
+ |
int hi = getFence(), lo = index; |
861 |
+ |
if (lo >= 0 && lo < hi) { |
862 |
+ |
index = lo + 1; |
863 |
+ |
@SuppressWarnings("unchecked") E e = (E)pq.queue[lo]; |
864 |
+ |
if (e == null) |
865 |
+ |
throw new ConcurrentModificationException(); |
866 |
+ |
action.accept(e); |
867 |
+ |
if (pq.modCount != expectedModCount) |
868 |
+ |
throw new ConcurrentModificationException(); |
869 |
+ |
return true; |
870 |
+ |
} |
871 |
+ |
return false; |
872 |
+ |
} |
873 |
+ |
|
874 |
+ |
public long estimateSize() { |
875 |
+ |
return (long) (getFence() - index); |
876 |
+ |
} |
877 |
+ |
|
878 |
+ |
public int characteristics() { |
879 |
+ |
return Spliterator.SIZED | Spliterator.SUBSIZED | Spliterator.NONNULL; |
880 |
+ |
} |
881 |
+ |
} |
882 |
|
} |