183 |
|
* @throws NullPointerException if the specified collection is null |
184 |
|
*/ |
185 |
|
public ArrayDeque(Collection<? extends E> c) { |
186 |
< |
Object[] elements = c.toArray(); |
186 |
> |
Object[] es = c.toArray(); |
187 |
|
// defend against c.toArray (incorrectly) not returning Object[] |
188 |
|
// (see e.g. https://bugs.openjdk.java.net/browse/JDK-6260652) |
189 |
< |
size = elements.length; |
190 |
< |
if (elements.getClass() != Object[].class) |
191 |
< |
elements = Arrays.copyOf(elements, size, Object[].class); |
192 |
< |
for (Object obj : elements) |
189 |
> |
if (es.getClass() != Object[].class) |
190 |
> |
es = Arrays.copyOf(es, es.length, Object[].class); |
191 |
> |
for (Object obj : es) |
192 |
|
Objects.requireNonNull(obj); |
193 |
< |
this.elements = elements; |
193 |
> |
this.elements = es; |
194 |
> |
this.size = es.length; |
195 |
|
} |
196 |
|
|
197 |
|
/** |
243 |
|
* but does catch ones that corrupt traversal. It's a little |
244 |
|
* surprising that javac allows this abuse of generics. |
245 |
|
*/ |
246 |
< |
static final <E> E nonNullElementAt(Object[] elements, int i) { |
247 |
< |
@SuppressWarnings("unchecked") E e = (E) elements[i]; |
246 |
> |
static final <E> E nonNullElementAt(Object[] es, int i) { |
247 |
> |
@SuppressWarnings("unchecked") E e = (E) es[i]; |
248 |
|
if (e == null) |
249 |
|
throw new ConcurrentModificationException(); |
250 |
|
return e; |
263 |
|
public void addFirst(E e) { |
264 |
|
// checkInvariants(); |
265 |
|
Objects.requireNonNull(e); |
266 |
< |
Object[] elements; |
266 |
> |
Object[] es; |
267 |
|
int capacity, h; |
268 |
|
final int s; |
269 |
< |
if ((s = size) == (capacity = (elements = this.elements).length)) { |
269 |
> |
if ((s = size) == (capacity = (es = elements).length)) { |
270 |
|
grow(1); |
271 |
< |
capacity = (elements = this.elements).length; |
271 |
> |
capacity = (es = elements).length; |
272 |
|
} |
273 |
|
if ((h = head - 1) < 0) h = capacity - 1; |
274 |
< |
elements[head = h] = e; |
274 |
> |
es[head = h] = e; |
275 |
|
size = s + 1; |
276 |
|
// checkInvariants(); |
277 |
|
} |
287 |
|
public void addLast(E e) { |
288 |
|
// checkInvariants(); |
289 |
|
Objects.requireNonNull(e); |
290 |
< |
Object[] elements; |
290 |
> |
Object[] es; |
291 |
|
int capacity; |
292 |
|
final int s; |
293 |
< |
if ((s = size) == (capacity = (elements = this.elements).length)) { |
293 |
> |
if ((s = size) == (capacity = (es = elements).length)) { |
294 |
|
grow(1); |
295 |
< |
capacity = (elements = this.elements).length; |
295 |
> |
capacity = (es = elements).length; |
296 |
|
} |
297 |
< |
elements[add(head, s, capacity)] = e; |
297 |
> |
es[add(head, s, capacity)] = e; |
298 |
|
size = s + 1; |
299 |
|
// checkInvariants(); |
300 |
|
} |
446 |
|
int i, end, to, todo; |
447 |
|
todo = (end = (i = head) + size) |
448 |
|
- (to = (capacity - end >= 0) ? end : capacity); |
449 |
< |
for (;; i = 0, to = todo, todo = 0) { |
449 |
> |
for (;; to = todo, i = 0, todo = 0) { |
450 |
|
for (; i < to; i++) |
451 |
|
if (o.equals(elements[i])) { |
452 |
|
delete(i); |
476 |
|
final int capacity = elements.length; |
477 |
|
int i, to, end, todo; |
478 |
|
todo = (to = ((end = (i = tail()) - size) >= -1) ? end : -1) - end; |
479 |
< |
for (;; i = capacity - 1, to = capacity - 1 - todo, todo = 0) { |
479 |
> |
for (;; to = (i = capacity - 1) - todo, todo = 0) { |
480 |
|
for (; i > to; i--) |
481 |
|
if (o.equals(elements[i])) { |
482 |
|
delete(i); |
860 |
|
int i, end, to, todo; |
861 |
|
todo = (end = (i = head) + size) |
862 |
|
- (to = (capacity - end >= 0) ? end : capacity); |
863 |
< |
for (;; i = 0, to = todo, todo = 0) { |
863 |
> |
for (;; to = todo, i = 0, todo = 0) { |
864 |
|
for (; i < to; i++) |
865 |
|
action.accept((E) elements[i]); |
866 |
|
if (todo == 0) break; |
874 |
|
* checks for concurrent modification, for use in iterators. |
875 |
|
*/ |
876 |
|
static <E> void forEachRemaining( |
877 |
< |
Consumer<? super E> action, Object[] elements, int i, int remaining) { |
877 |
> |
Consumer<? super E> action, Object[] es, int i, int remaining) { |
878 |
|
Objects.requireNonNull(action); |
879 |
< |
final int capacity = elements.length; |
879 |
> |
final int capacity = es.length; |
880 |
|
int end, to, todo; |
881 |
|
todo = (end = i + remaining) |
882 |
|
- (to = (capacity - end >= 0) ? end : capacity); |
883 |
< |
for (;; i = 0, to = todo, todo = 0) { |
883 |
> |
for (;; to = todo, i = 0, todo = 0) { |
884 |
|
for (; i < to; i++) |
885 |
< |
action.accept(nonNullElementAt(elements, i)); |
885 |
> |
action.accept(nonNullElementAt(es, i)); |
886 |
|
if (todo == 0) break; |
887 |
|
} |
888 |
|
} |
889 |
|
|
890 |
|
static <E> void forEachRemainingDescending( |
891 |
< |
Consumer<? super E> action, Object[] elements, int i, int remaining) { |
891 |
> |
Consumer<? super E> action, Object[] es, int i, int remaining) { |
892 |
|
Objects.requireNonNull(action); |
893 |
< |
final int capacity = elements.length; |
893 |
> |
final int capacity = es.length; |
894 |
|
int end, to, todo; |
895 |
|
todo = (to = ((end = i - remaining) >= -1) ? end : -1) - end; |
896 |
< |
for (;; i = capacity - 1, to = capacity - 1 - todo, todo = 0) { |
896 |
> |
for (;; to = (i = capacity - 1) - todo, todo = 0) { |
897 |
|
for (; i > to; i--) |
898 |
< |
action.accept(nonNullElementAt(elements, i)); |
898 |
> |
action.accept(nonNullElementAt(es, i)); |
899 |
|
if (todo == 0) break; |
900 |
|
} |
901 |
|
} |
914 |
|
int i, end, to, todo; |
915 |
|
todo = (end = (i = head) + size) |
916 |
|
- (to = (capacity - end >= 0) ? end : capacity); |
917 |
< |
for (;; i = 0, to = todo, todo = 0) { |
917 |
> |
for (;; to = todo, i = 0, todo = 0) { |
918 |
|
for (; i < to; i++) |
919 |
|
elements[i] = operator.apply(elementAt(i)); |
920 |
|
if (todo == 0) break; |
995 |
|
int i, end, to, todo; |
996 |
|
todo = (end = (i = head) + size) |
997 |
|
- (to = (capacity - end >= 0) ? end : capacity); |
998 |
< |
for (;; i = 0, to = todo, todo = 0) { |
998 |
> |
for (;; to = todo, i = 0, todo = 0) { |
999 |
|
for (; i < to; i++) |
1000 |
|
if (o.equals(elements[i])) |
1001 |
|
return true; |
1035 |
|
/** |
1036 |
|
* Nulls out count elements, starting at array index from. |
1037 |
|
*/ |
1038 |
< |
private static void clearSlice(Object[] elements, int from, int count) { |
1039 |
< |
final int capacity = elements.length, end = from + count; |
1038 |
> |
private static void clearSlice(Object[] es, int from, int count) { |
1039 |
> |
final int capacity = es.length, end = from + count; |
1040 |
|
final int leg = (capacity - end >= 0) ? end : capacity; |
1041 |
< |
Arrays.fill(elements, from, leg, null); |
1041 |
> |
Arrays.fill(es, from, leg, null); |
1042 |
|
if (leg != end) |
1043 |
< |
Arrays.fill(elements, 0, end - capacity, null); |
1043 |
> |
Arrays.fill(es, 0, end - capacity, null); |
1044 |
|
} |
1045 |
|
|
1046 |
|
/** |
1172 |
|
int i, end, to, todo; |
1173 |
|
todo = (end = (i = head) + size) |
1174 |
|
- (to = (capacity - end >= 0) ? end : capacity); |
1175 |
< |
for (;; i = 0, to = todo, todo = 0) { |
1175 |
> |
for (;; to = todo, i = 0, todo = 0) { |
1176 |
|
for (; i < to; i++) |
1177 |
|
s.writeObject(elements[i]); |
1178 |
|
if (todo == 0) break; |