19 |
|
* best-effort in the presence of concurrent modifications, and do |
20 |
|
* <em>NOT</em> throw {@link ConcurrentModificationException}. An |
21 |
|
* iterator's {@code next()} method returns consecutive elements as |
22 |
< |
* they appear in the underlying array upon each access. Alternatvely, |
22 |
> |
* they appear in the underlying array upon each access. Alternatively, |
23 |
|
* method {@link #snapshotIterator} may be used for deterministic |
24 |
|
* traversals, at the expense of making a copy, and unavailability of |
25 |
|
* method {@code Iterator.remove}. |
57 |
|
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8; |
58 |
|
|
59 |
|
// fields are non-private to simpify nested class access |
60 |
< |
Object[] array; |
60 |
> |
volatile Object[] array; |
61 |
|
final SequenceLock lock; |
62 |
< |
int count; |
62 |
> |
volatile int count; |
63 |
|
final int capacityIncrement; |
64 |
|
|
65 |
|
/** |
129 |
|
} |
130 |
|
|
131 |
|
// For explanation, see CopyOnWriteArrayList |
132 |
< |
final void grow(int minCapacity) { |
132 |
> |
final Object[] grow(int minCapacity) { |
133 |
|
int oldCapacity = array.length; |
134 |
|
int newCapacity = oldCapacity + ((capacityIncrement > 0) ? |
135 |
|
capacityIncrement : oldCapacity); |
137 |
|
newCapacity = minCapacity; |
138 |
|
if (newCapacity - MAX_ARRAY_SIZE > 0) |
139 |
|
newCapacity = hugeCapacity(minCapacity); |
140 |
< |
array = Arrays.copyOf(array, newCapacity); |
140 |
> |
return array = Arrays.copyOf(array, newCapacity); |
141 |
|
} |
142 |
|
|
143 |
|
static int hugeCapacity(int minCapacity) { |
201 |
|
|
202 |
|
final void rawAdd(Object e) { |
203 |
|
int n = count; |
204 |
< |
if (n >= array.length) |
205 |
< |
grow(n + 1); |
206 |
< |
array[n] = e; |
204 |
> |
Object[] items = array; |
205 |
> |
if (n >= items.length) |
206 |
> |
items = grow(n + 1); |
207 |
> |
items[n] = e; |
208 |
|
count = n + 1; |
209 |
|
} |
210 |
|
|
211 |
|
final void rawAddAt(int index, Object e) { |
212 |
|
int n = count; |
213 |
+ |
Object[] items = array; |
214 |
|
if (index > n) |
215 |
|
throw new ArrayIndexOutOfBoundsException(index); |
216 |
< |
if (n >= array.length) |
217 |
< |
grow(n + 1); |
216 |
> |
if (n >= items.length) |
217 |
> |
items = grow(n + 1); |
218 |
|
if (index < n) |
219 |
< |
System.arraycopy(array, index, array, index + 1, n - index); |
220 |
< |
array[index] = e; |
219 |
> |
System.arraycopy(items, index, items, index + 1, n - index); |
220 |
> |
items[index] = e; |
221 |
|
count = n + 1; |
222 |
|
} |
223 |
|
|
224 |
|
final boolean rawAddAllAt(int index, Object[] elements) { |
225 |
|
int n = count; |
226 |
+ |
Object[] items = array; |
227 |
|
if (index < 0 || index > n) |
228 |
|
throw new ArrayIndexOutOfBoundsException(index); |
229 |
|
int len = elements.length; |
230 |
|
if (len == 0) |
231 |
|
return false; |
232 |
|
int newCount = n + len; |
233 |
< |
if (newCount >= array.length) |
234 |
< |
grow(newCount); |
235 |
< |
int mv = count - index; |
233 |
> |
if (newCount >= items.length) |
234 |
> |
items = grow(newCount); |
235 |
> |
int mv = n - index; |
236 |
|
if (mv > 0) |
237 |
< |
System.arraycopy(array, index, array, index + len, mv); |
238 |
< |
System.arraycopy(elements, 0, array, index, len); |
237 |
> |
System.arraycopy(items, index, items, index + len, mv); |
238 |
> |
System.arraycopy(elements, 0, items, index, len); |
239 |
|
count = newCount; |
240 |
|
return true; |
241 |
|
} |
242 |
|
|
243 |
|
final boolean rawRemoveAt(int index) { |
244 |
|
int n = count - 1; |
245 |
+ |
Object[] items = array; |
246 |
|
if (index < 0 || index > n) |
247 |
|
return false; |
248 |
|
int mv = n - index; |
249 |
|
if (mv > 0) |
250 |
< |
System.arraycopy(array, index + 1, array, index, mv); |
251 |
< |
array[n] = null; |
250 |
> |
System.arraycopy(items, index + 1, items, index, mv); |
251 |
> |
items[n] = null; |
252 |
|
count = n; |
253 |
|
return true; |
254 |
|
} |
285 |
|
if (c != this) { |
286 |
|
lock.lock(); |
287 |
|
try { |
288 |
+ |
Object[] items = array; |
289 |
|
int i = origin; |
290 |
|
int n = count; |
291 |
|
int fence = bound < 0 || bound > n ? n : bound; |
292 |
|
while (i >= 0 && i < fence) { |
293 |
< |
if (c.contains(array[i])) |
293 |
> |
if (c.contains(items[i])) |
294 |
|
++i; |
295 |
|
else { |
296 |
|
--fence; |
297 |
< |
int mv = --count - i; |
297 |
> |
int mv = --n - i; |
298 |
|
if (mv > 0) |
299 |
< |
System.arraycopy(array, i + 1, array, i, mv); |
295 |
< |
removed = true; |
299 |
> |
System.arraycopy(items, i + 1, items, i, mv); |
300 |
|
} |
301 |
|
} |
302 |
+ |
if (count != n) { |
303 |
+ |
count = n; |
304 |
+ |
removed = true; |
305 |
+ |
} |
306 |
|
} finally { |
307 |
|
lock.unlock(); |
308 |
|
} |
314 |
|
int n = count; |
315 |
|
int fence = bound < 0 || bound > n ? n : bound; |
316 |
|
if (origin >= 0 && origin < fence) { |
317 |
+ |
Object[] items = array; |
318 |
|
int removed = fence - origin; |
319 |
|
int newCount = n - removed; |
320 |
|
int mv = n - (origin + removed); |
321 |
|
if (mv > 0) |
322 |
< |
System.arraycopy(array, origin + removed, array, origin, mv); |
322 |
> |
System.arraycopy(items, origin + removed, items, origin, mv); |
323 |
|
for (int i = n; i < newCount; ++i) |
324 |
< |
array[i] = null; |
324 |
> |
items[i] = null; |
325 |
|
count = newCount; |
326 |
|
} |
327 |
|
} |
333 |
|
try { |
334 |
|
for (;;) { |
335 |
|
long seq = lock.awaitAvailability(); |
336 |
+ |
int n = count; |
337 |
|
Object[] items = array; |
338 |
|
int len = items.length; |
329 |
– |
int n = count; |
339 |
|
if (n > len) |
340 |
|
continue; |
341 |
|
int fence = bound < 0 || bound > n ? n : bound; |
416 |
|
hash = 1; |
417 |
|
long seq = lock.awaitAvailability(); |
418 |
|
Object[] items = array; |
410 |
– |
int len = items.length; |
419 |
|
int n = count; |
420 |
+ |
int len = items.length; |
421 |
|
if (n > len) |
422 |
|
continue; |
423 |
|
int fence = bound < 0 || bound > n ? n : bound; |
447 |
|
outer:for (;;) { |
448 |
|
long seq = lock.awaitAvailability(); |
449 |
|
Object[] items = array; |
441 |
– |
int len = items.length; |
450 |
|
int n = count; |
451 |
+ |
int len = items.length; |
452 |
|
if (n > len) |
453 |
|
continue; |
454 |
|
int fence = bound < 0 || bound > n ? n : bound; |
494 |
|
result = null; |
495 |
|
long seq = lock.awaitAvailability(); |
496 |
|
Object[] items = array; |
488 |
– |
int len = items.length; |
497 |
|
int n = count; |
498 |
+ |
int len = items.length; |
499 |
|
if (n > len) |
500 |
|
continue; |
501 |
|
int fence = bound < 0 || bound > n ? n : bound; |
523 |
|
for (;;) { |
524 |
|
long seq = lock.awaitAvailability(); |
525 |
|
Object[] items = array; |
517 |
– |
int len = items.length; |
526 |
|
int n = count; |
527 |
+ |
int len = items.length; |
528 |
|
if (n > len) |
529 |
|
continue; |
530 |
|
int fence = bound < 0 || bound > n ? n : bound; |
533 |
|
rlen = 0; |
534 |
|
if (origin < 0 || alen >= rlen) { |
535 |
|
if (rlen > 0) |
536 |
< |
System.arraycopy(array, 0, a, origin, rlen); |
536 |
> |
System.arraycopy(items, 0, a, origin, rlen); |
537 |
|
if (alen > rlen) |
538 |
|
a[rlen] = null; |
539 |
|
result = a; |
540 |
|
} |
541 |
|
else |
542 |
< |
result = (T[]) Arrays.copyOfRange(array, origin, |
542 |
> |
result = (T[]) Arrays.copyOfRange(items, origin, |
543 |
|
fence, a.getClass()); |
544 |
|
if (lock.getSequence() == seq) |
545 |
|
break; |
584 |
|
SequenceLock lock = this.lock; |
585 |
|
lock.lock(); |
586 |
|
try { |
587 |
< |
int newCount = count + len; |
588 |
< |
if (newCount >= array.length) |
589 |
< |
grow(newCount); |
590 |
< |
System.arraycopy(elements, 0, array, count, len); |
587 |
> |
Object[] items = array; |
588 |
> |
int n = count; |
589 |
> |
int newCount = n + len; |
590 |
> |
if (newCount >= items.length) |
591 |
> |
items = grow(newCount); |
592 |
> |
System.arraycopy(elements, 0, items, n, len); |
593 |
|
count = newCount; |
594 |
|
} finally { |
595 |
|
lock.unlock(); |
614 |
|
SequenceLock lock = this.lock; |
615 |
|
lock.lock(); |
616 |
|
try { |
617 |
< |
for (int i = 0; i < count; i++) |
618 |
< |
array[i] = null; |
617 |
> |
int n = count; |
618 |
> |
Object[] items = array; |
619 |
> |
for (int i = 0; i < n; i++) |
620 |
> |
items[i] = null; |
621 |
|
count = 0; |
622 |
|
} finally { |
623 |
|
lock.unlock(); |
644 |
|
SequenceLock lock = this.lock; |
645 |
|
for (;;) { |
646 |
|
long seq = lock.awaitAvailability(); |
634 |
– |
Object[] items = array; |
647 |
|
int n = count; |
648 |
+ |
Object[] items = array; |
649 |
|
if (n > items.length) |
650 |
|
continue; |
651 |
|
Object e; boolean ex; |
696 |
|
} |
697 |
|
|
698 |
|
public boolean isEmpty() { |
686 |
– |
long ignore = lock.getSequence(); |
699 |
|
return count == 0; |
700 |
|
} |
701 |
|
|
702 |
|
public Iterator<E> iterator() { |
703 |
< |
return new Itr(this, 0); |
703 |
> |
return new Itr<E>(this, 0); |
704 |
|
} |
705 |
|
|
706 |
|
public int lastIndexOf(Object o) { |
729 |
|
} |
730 |
|
|
731 |
|
public ListIterator<E> listIterator() { |
732 |
< |
return new Itr(this, 0); |
732 |
> |
return new Itr<E>(this, 0); |
733 |
|
} |
734 |
|
|
735 |
|
public ListIterator<E> listIterator(int index) { |
736 |
< |
return new Itr(this, index); |
736 |
> |
return new Itr<E>(this, index); |
737 |
|
} |
738 |
|
|
739 |
|
public E remove(int index) { |
776 |
|
SequenceLock lock = this.lock; |
777 |
|
lock.lock(); |
778 |
|
try { |
779 |
+ |
Object[] items = array; |
780 |
|
if (index < 0 || index >= count) |
781 |
|
throw new ArrayIndexOutOfBoundsException(index); |
782 |
< |
oldValue = array[index]; |
783 |
< |
array[index] = element; |
782 |
> |
oldValue = items[index]; |
783 |
> |
items[index] = element; |
784 |
|
} finally { |
785 |
|
lock.unlock(); |
786 |
|
} |
788 |
|
} |
789 |
|
|
790 |
|
public int size() { |
778 |
– |
long ignore = lock.getSequence(); |
791 |
|
return count; |
792 |
|
} |
793 |
|
|
796 |
|
int ssize = toIndex - fromIndex; |
797 |
|
if (fromIndex < 0 || toIndex > c || ssize < 0) |
798 |
|
throw new IndexOutOfBoundsException(); |
799 |
< |
return new ReadMostlyVectorSublist(this, fromIndex, ssize); |
799 |
> |
return new ReadMostlyVectorSublist<E>(this, fromIndex, ssize); |
800 |
|
} |
801 |
|
|
802 |
|
public Object[] toArray() { |
1024 |
|
if (newSize > n) |
1025 |
|
grow(newSize); |
1026 |
|
else { |
1027 |
+ |
Object[] items = array; |
1028 |
|
for (int i = newSize ; i < n ; i++) |
1029 |
< |
array[i] = null; |
1029 |
> |
items[i] = null; |
1030 |
|
} |
1031 |
|
count = newSize; |
1032 |
|
} finally { |
1050 |
|
SequenceLock lock = this.lock; |
1051 |
|
lock.lock(); |
1052 |
|
try { |
1053 |
< |
if (count < array.length) |
1054 |
< |
array = Arrays.copyOf(array, count); |
1053 |
> |
Object[] items = array; |
1054 |
> |
int n = count; |
1055 |
> |
if (n < items.length) |
1056 |
> |
array = Arrays.copyOf(items, n); |
1057 |
|
} finally { |
1058 |
|
lock.unlock(); |
1059 |
|
} |
1075 |
|
|
1076 |
|
/** See {@link Vector#elements} */ |
1077 |
|
public Enumeration<E> elements() { |
1078 |
< |
return new Itr(this, 0); |
1078 |
> |
return new Itr<E>(this, 0); |
1079 |
|
} |
1080 |
|
|
1081 |
|
/** See {@link Vector#capacity} */ |
1082 |
|
public int capacity() { |
1068 |
– |
long ignore = lock.getSequence(); |
1083 |
|
return array.length; |
1084 |
|
} |
1085 |
|
|
1120 |
|
|
1121 |
|
// other methods |
1122 |
|
|
1123 |
< |
public Object clone() { |
1123 |
> |
public ReadMostlyVector<E> clone() { |
1124 |
|
SequenceLock lock = this.lock; |
1125 |
|
Object[] a = null; |
1126 |
|
boolean retry = false; |
1140 |
|
lock.unlock(); |
1141 |
|
} |
1142 |
|
} |
1143 |
< |
return new ReadMostlyVector(a, n, capacityIncrement); |
1143 |
> |
return new ReadMostlyVector<E>(a, n, capacityIncrement); |
1144 |
|
} |
1145 |
|
|
1146 |
|
private void writeObject(java.io.ObjectOutputStream s) |
1430 |
|
} |
1431 |
|
|
1432 |
|
public Iterator<E> iterator() { |
1433 |
< |
return new SubItr(this, offset); |
1433 |
> |
return new SubItr<E>(this, offset); |
1434 |
|
} |
1435 |
|
|
1436 |
|
public int lastIndexOf(Object o) { |
1454 |
|
} |
1455 |
|
|
1456 |
|
public ListIterator<E> listIterator() { |
1457 |
< |
return new SubItr(this, offset); |
1457 |
> |
return new SubItr<E>(this, offset); |
1458 |
|
} |
1459 |
|
|
1460 |
|
public ListIterator<E> listIterator(int index) { |
1461 |
< |
return new SubItr(this, index + offset); |
1461 |
> |
return new SubItr<E>(this, index + offset); |
1462 |
|
} |
1463 |
|
|
1464 |
|
public E remove(int index) { |
1518 |
|
int ssize = toIndex - fromIndex; |
1519 |
|
if (fromIndex < 0 || toIndex > c || ssize < 0) |
1520 |
|
throw new IndexOutOfBoundsException(); |
1521 |
< |
return new ReadMostlyVectorSublist(list, offset+fromIndex, ssize); |
1521 |
> |
return new ReadMostlyVectorSublist<E>(list, offset+fromIndex, ssize); |
1522 |
|
} |
1523 |
|
|
1524 |
|
public Object[] toArray() { |