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.127 by jsr166, Sun May 6 23:09:28 2018 UTC vs.
Revision 1.130 by jsr166, Sun Nov 11 16:27:28 2018 UTC

# Line 26 | Line 26
26   package java.util;
27  
28   import java.util.function.Consumer;
29 < import jdk.internal.misc.SharedSecrets;
29 > import java.util.function.Predicate;
30 > // OPENJDK import jdk.internal.access.SharedSecrets;
31  
32   /**
33   * An unbounded priority {@linkplain Queue queue} based on a priority heap.
# Line 74 | Line 75 | import jdk.internal.misc.SharedSecrets;
75   * ({@code peek}, {@code element}, and {@code size}).
76   *
77   * <p>This class is a member of the
78 < * <a href="{@docRoot}/java/util/package-summary.html#CollectionsFramework">
78 > * <a href="{@docRoot}/java.base/java/util/package-summary.html#CollectionsFramework">
79   * Java Collections Framework</a>.
80   *
81   * @since 1.5
# Line 802 | Line 803 | public class PriorityQueue<E> extends Ab
803          // Read in (and discard) array length
804          s.readInt();
805  
806 <        SharedSecrets.getJavaObjectInputStreamAccess().checkArray(s, Object[].class, size);
806 >        jsr166.Platform.checkArray(s, Object[].class, size);
807          final Object[] es = queue = new Object[Math.max(size, 1)];
808  
809          // Read in all elements.
# Line 901 | Line 902 | public class PriorityQueue<E> extends Ab
902      }
903  
904      /**
905 +     * @throws NullPointerException {@inheritDoc}
906 +     */
907 +    public boolean removeIf(Predicate<? super E> filter) {
908 +        Objects.requireNonNull(filter);
909 +        return bulkRemove(filter);
910 +    }
911 +
912 +    /**
913 +     * @throws NullPointerException {@inheritDoc}
914 +     */
915 +    public boolean removeAll(Collection<?> c) {
916 +        Objects.requireNonNull(c);
917 +        return bulkRemove(e -> c.contains(e));
918 +    }
919 +
920 +    /**
921 +     * @throws NullPointerException {@inheritDoc}
922 +     */
923 +    public boolean retainAll(Collection<?> c) {
924 +        Objects.requireNonNull(c);
925 +        return bulkRemove(e -> !c.contains(e));
926 +    }
927 +
928 +    // A tiny bit set implementation
929 +
930 +    private static long[] nBits(int n) {
931 +        return new long[((n - 1) >> 6) + 1];
932 +    }
933 +    private static void setBit(long[] bits, int i) {
934 +        bits[i >> 6] |= 1L << i;
935 +    }
936 +    private static boolean isClear(long[] bits, int i) {
937 +        return (bits[i >> 6] & (1L << i)) == 0;
938 +    }
939 +
940 +    /** Implementation of bulk remove methods. */
941 +    private boolean bulkRemove(Predicate<? super E> filter) {
942 +        final int expectedModCount = ++modCount;
943 +        final Object[] es = queue;
944 +        final int end = size;
945 +        int i;
946 +        // Optimize for initial run of survivors
947 +        for (i = 0; i < end && !filter.test((E) es[i]); i++)
948 +            ;
949 +        if (i >= end) {
950 +            if (modCount != expectedModCount)
951 +                throw new ConcurrentModificationException();
952 +            return false;
953 +        }
954 +        // Tolerate predicates that reentrantly access the collection for
955 +        // read (but writers still get CME), so traverse once to find
956 +        // elements to delete, a second pass to physically expunge.
957 +        final int beg = i;
958 +        final long[] deathRow = nBits(end - beg);
959 +        deathRow[0] = 1L;   // set bit 0
960 +        for (i = beg + 1; i < end; i++)
961 +            if (filter.test((E) es[i]))
962 +                setBit(deathRow, i - beg);
963 +        if (modCount != expectedModCount)
964 +            throw new ConcurrentModificationException();
965 +        int w = beg;
966 +        for (i = beg; i < end; i++)
967 +            if (isClear(deathRow, i - beg))
968 +                es[w++] = es[i];
969 +        for (i = size = w; i < end; i++)
970 +            es[i] = null;
971 +        heapify();
972 +        return true;
973 +    }
974 +
975 +    /**
976       * @throws NullPointerException {@inheritDoc}
977       */
978      public void forEach(Consumer<? super E> action) {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines