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 |
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. |
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 |
351 |
|
|
352 |
|
private int indexOf(Object o) { |
353 |
|
if (o != null) { |
354 |
< |
for (int i = 0; i < size; i++) |
355 |
< |
if (o.equals(queue[i])) |
354 |
> |
final Object[] es = queue; |
355 |
> |
for (int i = 0, n = size; i < n; i++) |
356 |
> |
if (o.equals(es[i])) |
357 |
|
return i; |
358 |
|
} |
359 |
|
return -1; |
381 |
|
} |
382 |
|
|
383 |
|
/** |
384 |
< |
* Version of remove using reference equality, not equals. |
383 |
< |
* Needed by iterator.remove. |
384 |
> |
* Identity-based version for use in Itr.remove. |
385 |
|
* |
386 |
|
* @param o element to be removed from this queue, if present |
386 |
– |
* @return {@code true} if removed |
387 |
|
*/ |
388 |
< |
boolean removeEq(Object o) { |
389 |
< |
for (int i = 0; i < size; i++) { |
390 |
< |
if (o == queue[i]) { |
388 |
> |
void removeEq(Object o) { |
389 |
> |
final Object[] es = queue; |
390 |
> |
for (int i = 0, n = size; i < n; i++) { |
391 |
> |
if (o == es[i]) { |
392 |
|
removeAt(i); |
393 |
< |
return true; |
393 |
> |
break; |
394 |
|
} |
395 |
|
} |
395 |
– |
return false; |
396 |
|
} |
397 |
|
|
398 |
|
/** |
522 |
|
*/ |
523 |
|
private int expectedModCount = modCount; |
524 |
|
|
525 |
+ |
Itr() {} // prevent access constructor creation |
526 |
+ |
|
527 |
|
public boolean hasNext() { |
528 |
|
return cursor < size || |
529 |
|
(forgetMeNot != null && !forgetMeNot.isEmpty()); |
577 |
|
*/ |
578 |
|
public void clear() { |
579 |
|
modCount++; |
580 |
< |
for (int i = 0; i < size; i++) |
581 |
< |
queue[i] = null; |
580 |
> |
final Object[] es = queue; |
581 |
> |
for (int i = 0, n = size; i < n; i++) |
582 |
> |
es[i] = null; |
583 |
|
size = 0; |
584 |
|
} |
585 |
|
|
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 |
|
* |
735 |
|
@SuppressWarnings("unchecked") |
736 |
|
private void heapify() { |
737 |
|
final Object[] es = queue; |
738 |
< |
final int half = (size >>> 1) - 1; |
738 |
> |
int i = (size >>> 1) - 1; |
739 |
|
if (comparator == null) |
740 |
< |
for (int i = half; i >= 0; i--) |
740 |
> |
for (; i >= 0; i--) |
741 |
|
siftDownComparable(i, (E) es[i]); |
742 |
|
else |
743 |
< |
for (int i = half; i >= 0; i--) |
743 |
> |
for (; i >= 0; i--) |
744 |
|
siftDownUsingComparator(i, (E) es[i]); |
745 |
|
} |
746 |
|
|
775 |
|
s.writeInt(Math.max(2, size + 1)); |
776 |
|
|
777 |
|
// Write out all elements in the "proper order". |
778 |
< |
for (int i = 0; i < size; i++) |
779 |
< |
s.writeObject(queue[i]); |
778 |
> |
final Object[] es = queue; |
779 |
> |
for (int i = 0, n = size; i < n; i++) |
780 |
> |
s.writeObject(es[i]); |
781 |
|
} |
782 |
|
|
783 |
|
/** |
797 |
|
// Read in (and discard) array length |
798 |
|
s.readInt(); |
799 |
|
|
800 |
+ |
SharedSecrets.getJavaObjectInputStreamAccess().checkArray(s, Object[].class, size); |
801 |
|
queue = new Object[size]; |
802 |
|
|
803 |
|
// Read in all elements. |
804 |
< |
for (int i = 0; i < size; i++) |
805 |
< |
queue[i] = s.readObject(); |
804 |
> |
final Object[] es = queue; |
805 |
> |
for (int i = 0, n = size; i < n; i++) |
806 |
> |
es[i] = s.readObject(); |
807 |
|
|
808 |
|
// Elements are guaranteed to be in "proper order", but the |
809 |
|
// spec has never explained what that might be. |
829 |
|
} |
830 |
|
|
831 |
|
final class PriorityQueueSpliterator implements Spliterator<E> { |
826 |
– |
/* |
827 |
– |
* This is very similar to ArrayList Spliterator, except for |
828 |
– |
* extra null checks. |
829 |
– |
*/ |
832 |
|
private int index; // current index, modified on advance/split |
833 |
|
private int fence; // -1 until first use |
834 |
|
private int expectedModCount; // initialized when fence set |
857 |
|
|
858 |
|
@SuppressWarnings("unchecked") |
859 |
|
public void forEachRemaining(Consumer<? super E> action) { |
858 |
– |
int i, hi, mc; // hoist accesses and checks from loop |
859 |
– |
final Object[] a; |
860 |
|
if (action == null) |
861 |
|
throw new NullPointerException(); |
862 |
< |
if ((a = queue) != null) { |
863 |
< |
if ((hi = fence) < 0) { |
864 |
< |
mc = modCount; |
865 |
< |
hi = size; |
866 |
< |
} |
867 |
< |
else |
868 |
< |
mc = expectedModCount; |
869 |
< |
if ((i = index) >= 0 && (index = hi) <= a.length) { |
870 |
< |
for (E e;; ++i) { |
871 |
< |
if (i < hi) { |
872 |
< |
if ((e = (E) a[i]) == null) // must be CME |
873 |
< |
break; |
874 |
< |
action.accept(e); |
875 |
< |
} |
876 |
< |
else if (modCount != mc) |
877 |
< |
break; |
878 |
< |
else |
879 |
< |
return; |
880 |
< |
} |
881 |
< |
} |
862 |
> |
if (fence < 0) { fence = size; expectedModCount = modCount; } |
863 |
> |
final Object[] a = queue; |
864 |
> |
int i, hi; E e; |
865 |
> |
for (i = index, index = hi = fence; i < hi; i++) { |
866 |
> |
if ((e = (E) a[i]) == null) |
867 |
> |
break; // must be CME |
868 |
> |
action.accept(e); |
869 |
|
} |
870 |
< |
throw new ConcurrentModificationException(); |
870 |
> |
if (modCount != expectedModCount) |
871 |
> |
throw new ConcurrentModificationException(); |
872 |
|
} |
873 |
|
|
874 |
+ |
@SuppressWarnings("unchecked") |
875 |
|
public boolean tryAdvance(Consumer<? super E> action) { |
876 |
|
if (action == null) |
877 |
|
throw new NullPointerException(); |
878 |
< |
int hi = getFence(), lo = index; |
879 |
< |
if (lo >= 0 && lo < hi) { |
880 |
< |
index = lo + 1; |
881 |
< |
@SuppressWarnings("unchecked") E e = (E)queue[lo]; |
882 |
< |
if (e == null) |
878 |
> |
if (fence < 0) { fence = size; expectedModCount = modCount; } |
879 |
> |
int i; |
880 |
> |
if ((i = index) < fence) { |
881 |
> |
index = i + 1; |
882 |
> |
E e; |
883 |
> |
if ((e = (E) queue[i]) == null |
884 |
> |
|| modCount != expectedModCount) |
885 |
|
throw new ConcurrentModificationException(); |
886 |
|
action.accept(e); |
896 |
– |
if (modCount != expectedModCount) |
897 |
– |
throw new ConcurrentModificationException(); |
887 |
|
return true; |
888 |
|
} |
889 |
|
return false; |
897 |
|
return Spliterator.SIZED | Spliterator.SUBSIZED | Spliterator.NONNULL; |
898 |
|
} |
899 |
|
} |
900 |
+ |
|
901 |
+ |
/** |
902 |
+ |
* @throws NullPointerException {@inheritDoc} |
903 |
+ |
*/ |
904 |
+ |
@SuppressWarnings("unchecked") |
905 |
+ |
public void forEach(Consumer<? super E> action) { |
906 |
+ |
Objects.requireNonNull(action); |
907 |
+ |
final int expectedModCount = modCount; |
908 |
+ |
final Object[] es = queue; |
909 |
+ |
for (int i = 0, n = size; i < n; i++) |
910 |
+ |
action.accept((E) es[i]); |
911 |
+ |
if (expectedModCount != modCount) |
912 |
+ |
throw new ConcurrentModificationException(); |
913 |
+ |
} |
914 |
|
} |