939 |
|
} |
940 |
|
|
941 |
|
/** Implementation of bulk remove methods. */ |
942 |
+ |
@SuppressWarnings("unchecked") |
943 |
|
private boolean bulkRemove(Predicate<? super E> filter) { |
944 |
|
// checkInvariants(); |
945 |
|
final Object[] es = elements; |
946 |
+ |
int i, end, to, todo; |
947 |
+ |
todo = (end = (i = head) + size) |
948 |
+ |
- (to = (es.length - end >= 0) ? end : es.length); |
949 |
+ |
// Optimize for initial run of non-removed elements |
950 |
+ |
findFirstRemoved: |
951 |
+ |
for (;; to = todo, todo = 0, i = 0) { |
952 |
+ |
for (; i < to; i++) |
953 |
+ |
if (filter.test((E) es[i])) |
954 |
+ |
break findFirstRemoved; |
955 |
+ |
if (todo == 0) return false; |
956 |
+ |
} |
957 |
+ |
bulkRemoveModified(filter, i, to, todo); |
958 |
+ |
return true; |
959 |
+ |
} |
960 |
+ |
|
961 |
+ |
/** |
962 |
+ |
* Helper for bulkRemove, in case of at least one deletion. |
963 |
+ |
* @param i valid index of first element to be deleted |
964 |
+ |
*/ |
965 |
+ |
@SuppressWarnings("unchecked") |
966 |
+ |
private void bulkRemoveModified( |
967 |
+ |
Predicate<? super E> filter, int i, int to, int todo) { |
968 |
+ |
final Object[] es = elements; |
969 |
|
final int capacity = es.length; |
970 |
< |
int i = head, j = i, remaining = size, deleted = 0; |
970 |
> |
// a two-finger algorithm, with hare i and tortoise j |
971 |
> |
int j = i++; |
972 |
|
try { |
973 |
< |
for (; remaining > 0; remaining--) { |
974 |
< |
@SuppressWarnings("unchecked") E e = (E) es[i]; |
975 |
< |
if (filter.test(e)) |
976 |
< |
deleted++; |
977 |
< |
else { |
978 |
< |
if (j != i) |
979 |
< |
es[j] = e; |
980 |
< |
if (++j >= capacity) j = 0; |
981 |
< |
} |
982 |
< |
if (++i >= capacity) i = 0; |
973 |
> |
for (;;) { |
974 |
> |
E e; |
975 |
> |
// In this loop, i and j are on the same leg, with i > j |
976 |
> |
for (; i < to; i++) |
977 |
> |
if (!filter.test(e = (E) es[i])) |
978 |
> |
es[j++] = e; |
979 |
> |
if (todo == 0) break; |
980 |
> |
// In this loop, j is on the first leg, i on the second |
981 |
> |
for (to = todo, todo = 0, i = 0; i < to && j < capacity; i++) |
982 |
> |
if (!filter.test(e = (E) es[i])) |
983 |
> |
es[j++] = e; |
984 |
> |
if (i >= to) break; |
985 |
> |
j = 0; // j rejoins i on second leg |
986 |
|
} |
987 |
< |
return deleted > 0; |
987 |
> |
bulkRemoveClear(es, j, i); |
988 |
> |
// checkInvariants(); |
989 |
|
} catch (Throwable ex) { |
990 |
< |
if (deleted > 0) |
991 |
< |
for (; remaining > 0; remaining--) { |
992 |
< |
es[j] = es[i]; |
993 |
< |
if (++i >= capacity) i = 0; |
994 |
< |
if (++j >= capacity) j = 0; |
995 |
< |
} |
996 |
< |
throw ex; |
968 |
< |
} finally { |
969 |
< |
size -= deleted; |
970 |
< |
circularClear(es, j, deleted); |
990 |
> |
// copy remaining elements |
991 |
> |
for (int remaining = (to - i) + todo; --remaining >= 0;) { |
992 |
> |
es[j] = es[i]; |
993 |
> |
if (++i >= capacity) i = 0; |
994 |
> |
if (++j >= capacity) j = 0; |
995 |
> |
} |
996 |
> |
bulkRemoveClear(es, j, i); |
997 |
|
// checkInvariants(); |
998 |
+ |
throw ex; |
999 |
|
} |
1000 |
|
} |
1001 |
|
|
1002 |
|
/** |
1003 |
+ |
* Nulls out all elements from index j upto index i. |
1004 |
+ |
*/ |
1005 |
+ |
private void bulkRemoveClear(Object[] es, int j, int i) { |
1006 |
+ |
int deleted; |
1007 |
+ |
if ((deleted = i - j) <= 0) deleted += es.length; |
1008 |
+ |
size -= deleted; |
1009 |
+ |
circularClear(es, j, deleted); |
1010 |
+ |
} |
1011 |
+ |
|
1012 |
+ |
/** |
1013 |
|
* Returns {@code true} if this deque contains the specified element. |
1014 |
|
* More formally, returns {@code true} if and only if this deque contains |
1015 |
|
* at least one element {@code e} such that {@code o.equals(e)}. |
1062 |
|
|
1063 |
|
/** |
1064 |
|
* Nulls out count elements, starting at array index from. |
1065 |
+ |
* Special case (from == es.length) is treated the same as (from == 0). |
1066 |
|
*/ |
1067 |
|
private static void circularClear(Object[] es, int from, int count) { |
1068 |
|
int end, to, todo; |