759 |
|
return (E) elementData[index]; |
760 |
|
} |
761 |
|
|
762 |
+ |
@SuppressWarnings("unchecked") |
763 |
+ |
static <E> E elementAt(Object[] es, int index) { |
764 |
+ |
return (E) es[index]; |
765 |
+ |
} |
766 |
+ |
|
767 |
|
/** |
768 |
|
* Returns the element at the specified position in this Vector. |
769 |
|
* |
989 |
|
return bulkRemove(filter); |
990 |
|
} |
991 |
|
|
992 |
< |
@SuppressWarnings("unchecked") |
992 |
> |
// A tiny bit set implementation |
993 |
> |
|
994 |
> |
private static long[] nBits(int n) { |
995 |
> |
return new long[((n - 1) >> 6) + 1]; |
996 |
> |
} |
997 |
> |
private static void setBit(long[] bits, int i) { |
998 |
> |
bits[i >> 6] |= 1L << i; |
999 |
> |
} |
1000 |
> |
private static boolean isClear(long[] bits, int i) { |
1001 |
> |
return (bits[i >> 6] & (1L << i)) == 0; |
1002 |
> |
} |
1003 |
> |
|
1004 |
|
private synchronized boolean bulkRemove(Predicate<? super E> filter) { |
1005 |
|
int expectedModCount = modCount; |
1006 |
|
final Object[] es = elementData; |
1007 |
< |
final int size = elementCount; |
1007 |
> |
final int end = elementCount; |
1008 |
|
final boolean modified; |
1009 |
< |
int r; |
1009 |
> |
int i; |
1010 |
|
// Optimize for initial run of survivors |
1011 |
< |
for (r = 0; r < size; r++) |
1012 |
< |
if (filter.test((E) es[r])) |
1013 |
< |
break; |
1014 |
< |
if (modified = (r < size)) { |
1011 |
> |
for (i = 0; i < end && !filter.test(elementAt(es, i)); i++) |
1012 |
> |
; |
1013 |
> |
// Tolerate predicates that reentrantly access the collection for |
1014 |
> |
// read (but writers still get CME), so traverse once to find |
1015 |
> |
// elements to delete, a second pass to physically expunge. |
1016 |
> |
if (modified = (i < end)) { |
1017 |
|
expectedModCount++; |
1018 |
|
modCount++; |
1019 |
< |
int w = r++; |
1020 |
< |
try { |
1021 |
< |
for (E e; r < size; r++) |
1022 |
< |
if (!filter.test(e = (E) es[r])) |
1023 |
< |
es[w++] = e; |
1024 |
< |
} catch (Throwable ex) { |
1025 |
< |
// copy remaining elements |
1026 |
< |
System.arraycopy(es, r, es, w, size - r); |
1027 |
< |
w += size - r; |
1028 |
< |
throw ex; |
1029 |
< |
} finally { |
1012 |
< |
Arrays.fill(es, elementCount = w, size, null); |
1013 |
< |
} |
1019 |
> |
final int beg = i; |
1020 |
> |
final long[] deathRow = nBits(end - beg); |
1021 |
> |
deathRow[0] = 1L; // set bit 0 |
1022 |
> |
for (i = beg + 1; i < end; i++) |
1023 |
> |
if (filter.test(elementAt(es, i))) |
1024 |
> |
setBit(deathRow, i - beg); |
1025 |
> |
int w = beg; |
1026 |
> |
for (i = beg; i < end; i++) |
1027 |
> |
if (isClear(deathRow, i - beg)) |
1028 |
> |
es[w++] = es[i]; |
1029 |
> |
Arrays.fill(es, elementCount = w, end, null); |
1030 |
|
} |
1031 |
|
if (modCount != expectedModCount) |
1032 |
|
throw new ConcurrentModificationException(); |