983 |
|
*/ |
984 |
|
public void clear() { |
985 |
|
final Object[] elements = this.elements; |
986 |
< |
final int capacity = elements.length; |
987 |
< |
final int h = this.head; |
988 |
< |
final int s = size; |
989 |
< |
if (capacity - h >= s) |
990 |
< |
Arrays.fill(elements, h, h + s, null); |
986 |
> |
final int capacity = elements.length, tail = head + size; |
987 |
> |
if (capacity - tail >= 0) |
988 |
> |
Arrays.fill(elements, head, tail, null); |
989 |
|
else { |
990 |
< |
Arrays.fill(elements, h, capacity, null); |
991 |
< |
Arrays.fill(elements, 0, s - capacity + h, null); |
990 |
> |
Arrays.fill(elements, head, capacity, null); |
991 |
> |
Arrays.fill(elements, 0, tail - capacity, null); |
992 |
|
} |
993 |
|
size = head = 0; |
994 |
|
// checkInvariants(); |
1008 |
|
* @return an array containing all of the elements in this deque |
1009 |
|
*/ |
1010 |
|
public Object[] toArray() { |
1011 |
< |
final int head = this.head; |
1012 |
< |
final int firstLeg; |
1013 |
< |
Object[] a = Arrays.copyOfRange(elements, head, head + size); |
1014 |
< |
if ((firstLeg = elements.length - head) < size) |
1015 |
< |
System.arraycopy(elements, 0, a, firstLeg, size - firstLeg); |
1011 |
> |
return toArray(Object[].class); |
1012 |
> |
} |
1013 |
> |
|
1014 |
> |
private <T> T[] toArray(Class<T[]> klazz) { |
1015 |
> |
final Object[] elements = this.elements; |
1016 |
> |
final int capacity = elements.length; |
1017 |
> |
final int head = this.head, tail = head + size; |
1018 |
> |
final T[] a; |
1019 |
> |
if (tail >= 0) { |
1020 |
> |
a = Arrays.copyOfRange(elements, head, tail, klazz); |
1021 |
> |
} else { |
1022 |
> |
// integer overflow! |
1023 |
> |
a = Arrays.copyOfRange(elements, 0, size, klazz); |
1024 |
> |
System.arraycopy(elements, head, a, 0, capacity - head); |
1025 |
> |
} |
1026 |
> |
if (tail - capacity > 0) |
1027 |
> |
System.arraycopy(elements, 0, a, capacity - head, tail - capacity); |
1028 |
|
return a; |
1029 |
|
} |
1030 |
|
|
1066 |
|
*/ |
1067 |
|
@SuppressWarnings("unchecked") |
1068 |
|
public <T> T[] toArray(T[] a) { |
1069 |
+ |
final int size = this.size; |
1070 |
+ |
if (size > a.length) |
1071 |
+ |
return toArray((Class<T[]>) a.getClass()); |
1072 |
|
final Object[] elements = this.elements; |
1073 |
< |
final int head = this.head; |
1074 |
< |
final int firstLeg; |
1075 |
< |
boolean wrap = (firstLeg = elements.length - head) < size; |
1076 |
< |
if (size > a.length) { |
1077 |
< |
a = (T[]) Arrays.copyOfRange(elements, head, head + size, |
1078 |
< |
a.getClass()); |
1079 |
< |
} else { |
1067 |
< |
System.arraycopy(elements, head, a, 0, wrap ? firstLeg : size); |
1068 |
< |
if (size < a.length) |
1069 |
< |
a[size] = null; |
1073 |
> |
final int capacity = elements.length; |
1074 |
> |
final int head = this.head, tail = head + size; |
1075 |
> |
if (capacity - tail >= 0) |
1076 |
> |
System.arraycopy(elements, head, a, 0, size); |
1077 |
> |
else { |
1078 |
> |
System.arraycopy(elements, head, a, 0, capacity - head); |
1079 |
> |
System.arraycopy(elements, 0, a, capacity - head, tail - capacity); |
1080 |
|
} |
1081 |
< |
if (wrap) |
1082 |
< |
System.arraycopy(elements, 0, a, firstLeg, size - firstLeg); |
1081 |
> |
if (size < a.length) |
1082 |
> |
a[size] = null; |
1083 |
|
return a; |
1084 |
|
} |
1085 |
|
|