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}. |
32 |
|
* |
33 |
|
* @author Doug Lea |
34 |
|
*/ |
35 |
< |
public class ReadMostlyVector<E> implements List<E>, RandomAccess, Cloneable, java.io.Serializable { |
35 |
> |
public class ReadMostlyVector<E> |
36 |
> |
implements List<E>, RandomAccess, Cloneable, java.io.Serializable { |
37 |
|
private static final long serialVersionUID = 8673264195747942595L; |
38 |
|
|
39 |
|
/* |
49 |
|
* read-only mode, and then lock. When in read-only mode, they |
50 |
|
* validate only at the end of an array scan unless the element is |
51 |
|
* actually used (for example, as an argument of method equals). |
52 |
+ |
* |
53 |
+ |
* We rely on some invariants that are always true, even for field |
54 |
+ |
* reads in read-only mode that have not yet been validated: |
55 |
+ |
* - array != null |
56 |
+ |
* - count >= 0 |
57 |
|
*/ |
58 |
|
|
59 |
|
/** |
62 |
|
*/ |
63 |
|
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8; |
64 |
|
|
65 |
< |
// fields are non-private to simpify nested class access |
65 |
> |
// fields are non-private to simplify nested class access |
66 |
|
volatile Object[] array; |
67 |
|
final SequenceLock lock; |
68 |
|
volatile int count; |
135 |
|
} |
136 |
|
|
137 |
|
// For explanation, see CopyOnWriteArrayList |
138 |
< |
final void grow(int minCapacity) { |
138 |
> |
final Object[] grow(int minCapacity) { |
139 |
|
int oldCapacity = array.length; |
140 |
|
int newCapacity = oldCapacity + ((capacityIncrement > 0) ? |
141 |
|
capacityIncrement : oldCapacity); |
143 |
|
newCapacity = minCapacity; |
144 |
|
if (newCapacity - MAX_ARRAY_SIZE > 0) |
145 |
|
newCapacity = hugeCapacity(minCapacity); |
146 |
< |
array = Arrays.copyOf(array, newCapacity); |
146 |
> |
return array = Arrays.copyOf(array, newCapacity); |
147 |
|
} |
148 |
|
|
149 |
|
static int hugeCapacity(int minCapacity) { |
160 |
|
* as well as sublist and iterator classes. |
161 |
|
*/ |
162 |
|
|
163 |
< |
// Version of indexOf that returns -1 if either not present or invalid |
163 |
> |
/** |
164 |
> |
* Version of indexOf that returns -1 if either not present or invalid. |
165 |
> |
* |
166 |
> |
* @throws ArrayIndexOutOfBoundsException if index is negative |
167 |
> |
*/ |
168 |
|
final int validatedIndexOf(Object x, Object[] items, int index, int fence, |
169 |
|
long seq) { |
170 |
|
for (int i = index; i < fence; ++i) { |
177 |
|
return -1; |
178 |
|
} |
179 |
|
|
180 |
+ |
/** |
181 |
+ |
* @throws ArrayIndexOutOfBoundsException if index is negative |
182 |
+ |
*/ |
183 |
|
final int rawIndexOf(Object x, int index, int fence) { |
184 |
|
Object[] items = array; |
185 |
|
for (int i = index; i < fence; ++i) { |
212 |
|
return -1; |
213 |
|
} |
214 |
|
|
215 |
< |
final void rawAdd(Object e) { |
215 |
> |
final void rawAdd(E e) { |
216 |
|
int n = count; |
217 |
|
Object[] items = array; |
218 |
< |
if (n < items.length) |
219 |
< |
items[n] = e; |
220 |
< |
else { |
208 |
< |
grow(n + 1); |
209 |
< |
array[n] = e; |
210 |
< |
} |
218 |
> |
if (n >= items.length) |
219 |
> |
items = grow(n + 1); |
220 |
> |
items[n] = e; |
221 |
|
count = n + 1; |
222 |
|
} |
223 |
|
|
224 |
< |
final void rawAddAt(int index, Object e) { |
224 |
> |
final void rawAddAt(int index, E e) { |
225 |
|
int n = count; |
226 |
+ |
Object[] items = array; |
227 |
|
if (index > n) |
228 |
|
throw new ArrayIndexOutOfBoundsException(index); |
229 |
< |
if (n >= array.length) |
230 |
< |
grow(n + 1); |
220 |
< |
Object[] items = array; |
229 |
> |
if (n >= items.length) |
230 |
> |
items = grow(n + 1); |
231 |
|
if (index < n) |
232 |
|
System.arraycopy(items, index, items, index + 1, n - index); |
233 |
|
items[index] = e; |
236 |
|
|
237 |
|
final boolean rawAddAllAt(int index, Object[] elements) { |
238 |
|
int n = count; |
239 |
+ |
Object[] items = array; |
240 |
|
if (index < 0 || index > n) |
241 |
|
throw new ArrayIndexOutOfBoundsException(index); |
242 |
|
int len = elements.length; |
243 |
|
if (len == 0) |
244 |
|
return false; |
245 |
|
int newCount = n + len; |
246 |
< |
if (newCount >= array.length) |
247 |
< |
grow(newCount); |
237 |
< |
Object[] items = array; |
246 |
> |
if (newCount >= items.length) |
247 |
> |
items = grow(newCount); |
248 |
|
int mv = n - index; |
249 |
|
if (mv > 0) |
250 |
|
System.arraycopy(items, index, items, index + len, mv); |
254 |
|
} |
255 |
|
|
256 |
|
final boolean rawRemoveAt(int index) { |
247 |
– |
Object[] items = array; |
257 |
|
int n = count - 1; |
258 |
+ |
Object[] items = array; |
259 |
|
if (index < 0 || index > n) |
260 |
|
return false; |
261 |
|
int mv = n - index; |
274 |
|
* field under lock. |
275 |
|
*/ |
276 |
|
final boolean internalRemoveAll(Collection<?> c, int origin, int bound) { |
277 |
< |
SequenceLock lock = this.lock; |
277 |
> |
final SequenceLock lock = this.lock; |
278 |
|
boolean removed = false; |
279 |
|
lock.lock(); |
280 |
|
try { |
293 |
|
} |
294 |
|
|
295 |
|
final boolean internalRetainAll(Collection<?> c, int origin, int bound) { |
296 |
< |
SequenceLock lock = this.lock; |
296 |
> |
final SequenceLock lock = this.lock; |
297 |
|
boolean removed = false; |
298 |
|
if (c != this) { |
299 |
|
lock.lock(); |
307 |
|
++i; |
308 |
|
else { |
309 |
|
--fence; |
310 |
< |
int mv = --count - i; |
310 |
> |
int mv = --n - i; |
311 |
|
if (mv > 0) |
312 |
|
System.arraycopy(items, i + 1, items, i, mv); |
303 |
– |
removed = true; |
313 |
|
} |
314 |
|
} |
315 |
+ |
if (count != n) { |
316 |
+ |
count = n; |
317 |
+ |
removed = true; |
318 |
+ |
} |
319 |
|
} finally { |
320 |
|
lock.unlock(); |
321 |
|
} |
324 |
|
} |
325 |
|
|
326 |
|
final void internalClear(int origin, int bound) { |
314 |
– |
Object[] items = array; |
327 |
|
int n = count; |
328 |
|
int fence = bound < 0 || bound > n ? n : bound; |
329 |
|
if (origin >= 0 && origin < fence) { |
330 |
+ |
Object[] items = array; |
331 |
|
int removed = fence - origin; |
332 |
|
int newCount = n - removed; |
333 |
|
int mv = n - (origin + removed); |
340 |
|
} |
341 |
|
|
342 |
|
final boolean internalContainsAll(Collection<?> c, int origin, int bound) { |
343 |
< |
SequenceLock lock = this.lock; |
343 |
> |
final SequenceLock lock = this.lock; |
344 |
|
boolean contained; |
345 |
|
boolean locked = false; |
346 |
|
try { |
347 |
|
for (;;) { |
348 |
|
long seq = lock.awaitAvailability(); |
349 |
+ |
int n = count; |
350 |
|
Object[] items = array; |
351 |
|
int len = items.length; |
338 |
– |
int n = count; |
352 |
|
if (n > len) |
353 |
|
continue; |
354 |
|
int fence = bound < 0 || bound > n ? n : bound; |
380 |
|
} |
381 |
|
|
382 |
|
final boolean internalEquals(List<?> list, int origin, int bound) { |
383 |
< |
SequenceLock lock = this.lock; |
383 |
> |
final SequenceLock lock = this.lock; |
384 |
|
boolean locked = false; |
385 |
|
boolean equal; |
386 |
|
try { |
421 |
|
} |
422 |
|
|
423 |
|
final int internalHashCode(int origin, int bound) { |
424 |
< |
SequenceLock lock = this.lock; |
424 |
> |
final SequenceLock lock = this.lock; |
425 |
|
int hash; |
426 |
|
boolean locked = false; |
427 |
|
try { |
429 |
|
hash = 1; |
430 |
|
long seq = lock.awaitAvailability(); |
431 |
|
Object[] items = array; |
419 |
– |
int len = items.length; |
432 |
|
int n = count; |
433 |
+ |
int len = items.length; |
434 |
|
if (n > len) |
435 |
|
continue; |
436 |
|
int fence = bound < 0 || bound > n ? n : bound; |
453 |
|
} |
454 |
|
|
455 |
|
final String internalToString(int origin, int bound) { |
456 |
< |
SequenceLock lock = this.lock; |
456 |
> |
final SequenceLock lock = this.lock; |
457 |
|
String ret; |
458 |
|
boolean locked = false; |
459 |
|
try { |
460 |
|
outer:for (;;) { |
461 |
|
long seq = lock.awaitAvailability(); |
462 |
|
Object[] items = array; |
450 |
– |
int len = items.length; |
463 |
|
int n = count; |
464 |
+ |
int len = items.length; |
465 |
|
if (n > len) |
466 |
|
continue; |
467 |
|
int fence = bound < 0 || bound > n ? n : bound; |
500 |
|
|
501 |
|
final Object[] internalToArray(int origin, int bound) { |
502 |
|
Object[] result; |
503 |
< |
SequenceLock lock = this.lock; |
503 |
> |
final SequenceLock lock = this.lock; |
504 |
|
boolean locked = false; |
505 |
|
try { |
506 |
|
for (;;) { |
507 |
|
result = null; |
508 |
|
long seq = lock.awaitAvailability(); |
509 |
|
Object[] items = array; |
497 |
– |
int len = items.length; |
510 |
|
int n = count; |
511 |
+ |
int len = items.length; |
512 |
|
if (n > len) |
513 |
|
continue; |
514 |
|
int fence = bound < 0 || bound > n ? n : bound; |
527 |
|
return result; |
528 |
|
} |
529 |
|
|
530 |
+ |
@SuppressWarnings("unchecked") |
531 |
|
final <T> T[] internalToArray(T[] a, int origin, int bound) { |
532 |
|
int alen = a.length; |
533 |
|
T[] result; |
534 |
< |
SequenceLock lock = this.lock; |
534 |
> |
final SequenceLock lock = this.lock; |
535 |
|
boolean locked = false; |
536 |
|
try { |
537 |
|
for (;;) { |
538 |
|
long seq = lock.awaitAvailability(); |
539 |
|
Object[] items = array; |
526 |
– |
int len = items.length; |
540 |
|
int n = count; |
541 |
+ |
int len = items.length; |
542 |
|
if (n > len) |
543 |
|
continue; |
544 |
|
int fence = bound < 0 || bound > n ? n : bound; |
570 |
|
// public List methods |
571 |
|
|
572 |
|
public boolean add(E e) { |
573 |
< |
SequenceLock lock = this.lock; |
573 |
> |
final SequenceLock lock = this.lock; |
574 |
|
lock.lock(); |
575 |
|
try { |
576 |
|
rawAdd(e); |
581 |
|
} |
582 |
|
|
583 |
|
public void add(int index, E element) { |
584 |
< |
SequenceLock lock = this.lock; |
584 |
> |
final SequenceLock lock = this.lock; |
585 |
|
lock.lock(); |
586 |
|
try { |
587 |
|
rawAddAt(index, element); |
595 |
|
int len = elements.length; |
596 |
|
if (len == 0) |
597 |
|
return false; |
598 |
< |
SequenceLock lock = this.lock; |
598 |
> |
final SequenceLock lock = this.lock; |
599 |
|
lock.lock(); |
600 |
|
try { |
601 |
< |
int newCount = count + len; |
602 |
< |
if (newCount >= array.length) |
603 |
< |
grow(newCount); |
604 |
< |
System.arraycopy(elements, 0, array, count, len); |
601 |
> |
Object[] items = array; |
602 |
> |
int n = count; |
603 |
> |
int newCount = n + len; |
604 |
> |
if (newCount >= items.length) |
605 |
> |
items = grow(newCount); |
606 |
> |
System.arraycopy(elements, 0, items, n, len); |
607 |
|
count = newCount; |
608 |
|
} finally { |
609 |
|
lock.unlock(); |
612 |
|
} |
613 |
|
|
614 |
|
public boolean addAll(int index, Collection<? extends E> c) { |
615 |
< |
SequenceLock lock = this.lock; |
615 |
> |
final SequenceLock lock = this.lock; |
616 |
|
boolean ret; |
617 |
|
Object[] elements = c.toArray(); |
618 |
|
lock.lock(); |
625 |
|
} |
626 |
|
|
627 |
|
public void clear() { |
628 |
< |
SequenceLock lock = this.lock; |
628 |
> |
final SequenceLock lock = this.lock; |
629 |
|
lock.lock(); |
630 |
|
try { |
631 |
+ |
int n = count; |
632 |
|
Object[] items = array; |
633 |
< |
for (int i = 0; i < count; i++) |
633 |
> |
for (int i = 0; i < n; i++) |
634 |
|
items[i] = null; |
635 |
|
count = 0; |
636 |
|
} finally { |
655 |
|
} |
656 |
|
|
657 |
|
public E get(int index) { |
658 |
< |
SequenceLock lock = this.lock; |
658 |
> |
final SequenceLock lock = this.lock; |
659 |
|
for (;;) { |
660 |
|
long seq = lock.awaitAvailability(); |
644 |
– |
Object[] items = array; |
661 |
|
int n = count; |
662 |
< |
if (n > items.length) |
663 |
< |
continue; |
664 |
< |
Object e; boolean ex; |
649 |
< |
if (index < 0 || index >= n) { |
650 |
< |
e = null; |
651 |
< |
ex = true; |
652 |
< |
} |
653 |
< |
else { |
654 |
< |
e = items[index]; |
655 |
< |
ex = false; |
656 |
< |
} |
662 |
> |
Object[] items = array; |
663 |
> |
@SuppressWarnings("unchecked") |
664 |
> |
E e = (index < items.length) ? (E) items[index] : null; |
665 |
|
if (lock.getSequence() == seq) { |
666 |
< |
if (ex) |
666 |
> |
if (index >= n) |
667 |
|
throw new ArrayIndexOutOfBoundsException(index); |
668 |
< |
else |
661 |
< |
return (E)e; |
668 |
> |
return e; |
669 |
|
} |
670 |
|
} |
671 |
|
} |
675 |
|
} |
676 |
|
|
677 |
|
public int indexOf(Object o) { |
678 |
< |
SequenceLock lock = this.lock; |
672 |
< |
for (;;) { |
673 |
< |
long seq = lock.awaitAvailability(); |
674 |
< |
Object[] items = array; |
675 |
< |
int n = count; |
676 |
< |
if (n <= items.length) { |
677 |
< |
for (int i = 0; i < n; ++i) { |
678 |
< |
Object e = items[i]; |
679 |
< |
if (lock.getSequence() != seq) { |
680 |
< |
lock.lock(); |
681 |
< |
try { |
682 |
< |
return rawIndexOf(o, 0, count); |
683 |
< |
} finally { |
684 |
< |
lock.unlock(); |
685 |
< |
} |
686 |
< |
} |
687 |
< |
else if ((o == null) ? e == null : o.equals(e)) |
688 |
< |
return i; |
689 |
< |
} |
690 |
< |
return -1; |
691 |
< |
} |
692 |
< |
} |
678 |
> |
return indexOf(o, 0); |
679 |
|
} |
680 |
|
|
681 |
|
public boolean isEmpty() { |
687 |
|
} |
688 |
|
|
689 |
|
public int lastIndexOf(Object o) { |
690 |
< |
SequenceLock lock = this.lock; |
690 |
> |
final SequenceLock lock = this.lock; |
691 |
|
for (;;) { |
692 |
|
long seq = lock.awaitAvailability(); |
693 |
|
Object[] items = array; |
698 |
|
if (lock.getSequence() != seq) { |
699 |
|
lock.lock(); |
700 |
|
try { |
701 |
< |
return rawLastIndexOf(o, 0, count); |
701 |
> |
return rawLastIndexOf(o, count - 1, 0); |
702 |
|
} finally { |
703 |
|
lock.unlock(); |
704 |
|
} |
720 |
|
} |
721 |
|
|
722 |
|
public E remove(int index) { |
723 |
< |
SequenceLock lock = this.lock; |
738 |
< |
Object oldValue; |
723 |
> |
final SequenceLock lock = this.lock; |
724 |
|
lock.lock(); |
725 |
|
try { |
726 |
|
if (index < 0 || index >= count) |
727 |
|
throw new ArrayIndexOutOfBoundsException(index); |
728 |
< |
oldValue = array[index]; |
728 |
> |
@SuppressWarnings("unchecked") |
729 |
> |
E oldValue = (E) array[index]; |
730 |
|
rawRemoveAt(index); |
731 |
+ |
return oldValue; |
732 |
|
} finally { |
733 |
|
lock.unlock(); |
734 |
|
} |
748 |
– |
return (E)oldValue; |
735 |
|
} |
736 |
|
|
737 |
|
public boolean remove(Object o) { |
738 |
< |
SequenceLock lock = this.lock; |
753 |
< |
boolean removed; |
738 |
> |
final SequenceLock lock = this.lock; |
739 |
|
lock.lock(); |
740 |
|
try { |
741 |
< |
removed = rawRemoveAt(rawIndexOf(o, 0, count)); |
741 |
> |
return rawRemoveAt(rawIndexOf(o, 0, count)); |
742 |
|
} finally { |
743 |
|
lock.unlock(); |
744 |
|
} |
760 |
– |
return removed; |
745 |
|
} |
746 |
|
|
747 |
|
public boolean removeAll(Collection<?> c) { |
753 |
|
} |
754 |
|
|
755 |
|
public E set(int index, E element) { |
756 |
< |
Object oldValue; |
773 |
< |
SequenceLock lock = this.lock; |
756 |
> |
final SequenceLock lock = this.lock; |
757 |
|
lock.lock(); |
758 |
|
try { |
759 |
|
Object[] items = array; |
760 |
|
if (index < 0 || index >= count) |
761 |
|
throw new ArrayIndexOutOfBoundsException(index); |
762 |
< |
oldValue = items[index]; |
762 |
> |
@SuppressWarnings("unchecked") |
763 |
> |
E oldValue = (E) items[index]; |
764 |
|
items[index] = element; |
765 |
+ |
return oldValue; |
766 |
|
} finally { |
767 |
|
lock.unlock(); |
768 |
|
} |
784 |
– |
return (E)oldValue; |
769 |
|
} |
770 |
|
|
771 |
|
public int size() { |
795 |
|
// ReadMostlyVector-only methods |
796 |
|
|
797 |
|
/** |
798 |
< |
* Append the element if not present. |
798 |
> |
* Appends the element, if not present. |
799 |
|
* |
800 |
|
* @param e element to be added to this list, if absent |
801 |
|
* @return {@code true} if the element was added |
802 |
|
*/ |
803 |
|
public boolean addIfAbsent(E e) { |
804 |
< |
boolean added; |
821 |
< |
SequenceLock lock = this.lock; |
804 |
> |
final SequenceLock lock = this.lock; |
805 |
|
lock.lock(); |
806 |
|
try { |
807 |
|
if (rawIndexOf(e, 0, count) < 0) { |
808 |
|
rawAdd(e); |
809 |
< |
added = true; |
809 |
> |
return true; |
810 |
|
} |
811 |
|
else |
812 |
< |
added = false; |
812 |
> |
return false; |
813 |
|
} finally { |
814 |
|
lock.unlock(); |
815 |
|
} |
833 |
– |
return added; |
816 |
|
} |
817 |
|
|
818 |
|
/** |
834 |
|
lock.lock(); |
835 |
|
try { |
836 |
|
for (int i = 0; i < clen; ++i) { |
837 |
< |
Object e = cs[i]; |
837 |
> |
@SuppressWarnings("unchecked") |
838 |
> |
E e = (E) cs[i]; |
839 |
|
if (rawIndexOf(e, 0, count) < 0) { |
840 |
|
rawAdd(e); |
841 |
|
++added; |
861 |
|
} |
862 |
|
|
863 |
|
static final class SnapshotIterator<E> implements Iterator<E> { |
864 |
< |
final Object[] items; |
865 |
< |
int cursor; |
864 |
> |
private final Object[] items; |
865 |
> |
private int cursor; |
866 |
|
SnapshotIterator(ReadMostlyVector<E> v) { items = v.toArray(); } |
867 |
|
public boolean hasNext() { return cursor < items.length; } |
868 |
+ |
@SuppressWarnings("unchecked") |
869 |
|
public E next() { |
870 |
|
if (cursor < items.length) |
871 |
|
return (E) items[cursor++]; |
878 |
|
|
879 |
|
/** See {@link Vector#firstElement} */ |
880 |
|
public E firstElement() { |
881 |
< |
SequenceLock lock = this.lock; |
881 |
> |
final SequenceLock lock = this.lock; |
882 |
|
for (;;) { |
883 |
|
long seq = lock.awaitAvailability(); |
884 |
|
Object[] items = array; |
901 |
– |
int len = items.length; |
885 |
|
int n = count; |
886 |
< |
if (n > len) |
887 |
< |
continue; |
905 |
< |
Object e; boolean ex; |
906 |
< |
if (n > 0) { |
907 |
< |
e = items[0]; |
908 |
< |
ex = false; |
909 |
< |
} |
910 |
< |
else { |
911 |
< |
e = null; |
912 |
< |
ex = true; |
913 |
< |
} |
886 |
> |
@SuppressWarnings("unchecked") |
887 |
> |
E e = (items.length > 0) ? (E) items[0] : null; |
888 |
|
if (lock.getSequence() == seq) { |
889 |
< |
if (ex) |
889 |
> |
if (n <= 0) |
890 |
|
throw new NoSuchElementException(); |
891 |
< |
else |
918 |
< |
return (E)e; |
891 |
> |
return e; |
892 |
|
} |
893 |
|
} |
894 |
|
} |
895 |
|
|
896 |
|
/** See {@link Vector#lastElement} */ |
897 |
|
public E lastElement() { |
898 |
< |
SequenceLock lock = this.lock; |
898 |
> |
final SequenceLock lock = this.lock; |
899 |
|
for (;;) { |
900 |
|
long seq = lock.awaitAvailability(); |
901 |
|
Object[] items = array; |
929 |
– |
int len = items.length; |
902 |
|
int n = count; |
903 |
< |
if (n > len) |
904 |
< |
continue; |
933 |
< |
Object e; boolean ex; |
934 |
< |
if (n > 0) { |
935 |
< |
e = items[n - 1]; |
936 |
< |
ex = false; |
937 |
< |
} |
938 |
< |
else { |
939 |
< |
e = null; |
940 |
< |
ex = true; |
941 |
< |
} |
903 |
> |
@SuppressWarnings("unchecked") |
904 |
> |
E e = (n > 0 && items.length >= n) ? (E) items[n - 1] : null; |
905 |
|
if (lock.getSequence() == seq) { |
906 |
< |
if (ex) |
906 |
> |
if (n <= 0) |
907 |
|
throw new NoSuchElementException(); |
908 |
< |
else |
946 |
< |
return (E)e; |
908 |
> |
return e; |
909 |
|
} |
910 |
|
} |
911 |
|
} |
912 |
|
|
913 |
|
/** See {@link Vector#indexOf(Object, int)} */ |
914 |
|
public int indexOf(Object o, int index) { |
915 |
< |
SequenceLock lock = this.lock; |
954 |
< |
int idx = 0; |
955 |
< |
boolean ex = false; |
915 |
> |
final SequenceLock lock = this.lock; |
916 |
|
long seq = lock.awaitAvailability(); |
917 |
|
Object[] items = array; |
918 |
|
int n = count; |
919 |
< |
boolean retry = false; |
920 |
< |
if (n > items.length) |
961 |
< |
retry = true; |
962 |
< |
else if (index < 0) |
963 |
< |
ex = true; |
964 |
< |
else |
919 |
> |
int idx = -1; |
920 |
> |
if (n <= items.length) |
921 |
|
idx = validatedIndexOf(o, items, index, n, seq); |
922 |
< |
if (retry || lock.getSequence() != seq) { |
922 |
> |
if (lock.getSequence() != seq) { |
923 |
|
lock.lock(); |
924 |
|
try { |
925 |
< |
if (index < 0) |
970 |
< |
ex = true; |
971 |
< |
else |
972 |
< |
idx = rawIndexOf(o, 0, count); |
925 |
> |
idx = rawIndexOf(o, index, count); |
926 |
|
} finally { |
927 |
|
lock.unlock(); |
928 |
|
} |
929 |
|
} |
930 |
< |
if (ex) |
978 |
< |
throw new ArrayIndexOutOfBoundsException(index); |
930 |
> |
// Above code will throw AIOOBE when index < 0 |
931 |
|
return idx; |
932 |
|
} |
933 |
|
|
934 |
|
/** See {@link Vector#lastIndexOf(Object, int)} */ |
935 |
|
public int lastIndexOf(Object o, int index) { |
936 |
< |
SequenceLock lock = this.lock; |
985 |
< |
int idx = 0; |
986 |
< |
boolean ex = false; |
936 |
> |
final SequenceLock lock = this.lock; |
937 |
|
long seq = lock.awaitAvailability(); |
938 |
|
Object[] items = array; |
939 |
|
int n = count; |
940 |
< |
boolean retry = false; |
941 |
< |
if (n > items.length) |
992 |
< |
retry = true; |
993 |
< |
else if (index >= n) |
994 |
< |
ex = true; |
995 |
< |
else |
940 |
> |
int idx = -1; |
941 |
> |
if (index < Math.min(n, items.length)) |
942 |
|
idx = validatedLastIndexOf(o, items, index, 0, seq); |
943 |
< |
if (retry || lock.getSequence() != seq) { |
943 |
> |
if (lock.getSequence() != seq) { |
944 |
|
lock.lock(); |
945 |
|
try { |
946 |
< |
if (index >= count) |
947 |
< |
ex = true; |
1002 |
< |
else |
946 |
> |
n = count; |
947 |
> |
if (index < n) |
948 |
|
idx = rawLastIndexOf(o, index, 0); |
949 |
|
} finally { |
950 |
|
lock.unlock(); |
951 |
|
} |
952 |
|
} |
953 |
< |
if (ex) |
954 |
< |
throw new ArrayIndexOutOfBoundsException(index); |
953 |
> |
if (index >= n) |
954 |
> |
throw new IndexOutOfBoundsException(index + " >= " + n); |
955 |
|
return idx; |
956 |
|
} |
957 |
|
|
959 |
|
public void setSize(int newSize) { |
960 |
|
if (newSize < 0) |
961 |
|
throw new ArrayIndexOutOfBoundsException(newSize); |
962 |
< |
SequenceLock lock = this.lock; |
962 |
> |
final SequenceLock lock = this.lock; |
963 |
|
lock.lock(); |
964 |
|
try { |
965 |
|
int n = count; |
978 |
|
|
979 |
|
/** See {@link Vector#copyInto} */ |
980 |
|
public void copyInto(Object[] anArray) { |
981 |
< |
SequenceLock lock = this.lock; |
981 |
> |
final SequenceLock lock = this.lock; |
982 |
|
lock.lock(); |
983 |
|
try { |
984 |
|
System.arraycopy(array, 0, anArray, 0, count); |
989 |
|
|
990 |
|
/** See {@link Vector#trimToSize} */ |
991 |
|
public void trimToSize() { |
992 |
< |
SequenceLock lock = this.lock; |
992 |
> |
final SequenceLock lock = this.lock; |
993 |
|
lock.lock(); |
994 |
|
try { |
995 |
|
Object[] items = array; |
996 |
< |
if (count < items.length) |
997 |
< |
array = Arrays.copyOf(items, count); |
996 |
> |
int n = count; |
997 |
> |
if (n < items.length) |
998 |
> |
array = Arrays.copyOf(items, n); |
999 |
|
} finally { |
1000 |
|
lock.unlock(); |
1001 |
|
} |
1004 |
|
/** See {@link Vector#ensureCapacity} */ |
1005 |
|
public void ensureCapacity(int minCapacity) { |
1006 |
|
if (minCapacity > 0) { |
1007 |
< |
SequenceLock lock = this.lock; |
1007 |
> |
final SequenceLock lock = this.lock; |
1008 |
|
lock.lock(); |
1009 |
|
try { |
1010 |
|
if (minCapacity - array.length > 0) |
1063 |
|
// other methods |
1064 |
|
|
1065 |
|
public ReadMostlyVector<E> clone() { |
1066 |
< |
SequenceLock lock = this.lock; |
1066 |
> |
final SequenceLock lock = this.lock; |
1067 |
|
Object[] a = null; |
1068 |
|
boolean retry = false; |
1069 |
|
long seq = lock.awaitAvailability(); |
1087 |
|
|
1088 |
|
private void writeObject(java.io.ObjectOutputStream s) |
1089 |
|
throws java.io.IOException { |
1090 |
< |
SequenceLock lock = this.lock; |
1090 |
> |
final SequenceLock lock = this.lock; |
1091 |
|
lock.lock(); |
1092 |
|
try { |
1093 |
|
s.defaultWriteObject(); |
1096 |
|
} |
1097 |
|
} |
1098 |
|
|
1099 |
< |
static final class Itr<E> implements ListIterator<E>, Enumeration<E> { |
1099 |
> |
static final class Itr<E> implements ListIterator<E>, Enumeration<E> { |
1100 |
|
final ReadMostlyVector<E> list; |
1101 |
|
final SequenceLock lock; |
1102 |
|
Object[] items; |
1103 |
< |
Object next, prev; |
1103 |
> |
E next, prev; |
1104 |
|
long seq; |
1105 |
|
int cursor; |
1106 |
|
int fence; |
1126 |
|
lock.getSequence() != seq); |
1127 |
|
} |
1128 |
|
|
1129 |
+ |
@SuppressWarnings("unchecked") |
1130 |
|
public boolean hasNext() { |
1131 |
|
boolean valid; |
1132 |
|
int i = cursor; |
1135 |
|
valid = false; |
1136 |
|
break; |
1137 |
|
} |
1138 |
< |
next = items[i]; |
1138 |
> |
next = (E) items[i]; |
1139 |
|
if (lock.getSequence() == seq) { |
1140 |
|
valid = true; |
1141 |
|
break; |
1145 |
|
return validNext = valid; |
1146 |
|
} |
1147 |
|
|
1148 |
+ |
@SuppressWarnings("unchecked") |
1149 |
|
public boolean hasPrevious() { |
1150 |
|
boolean valid; |
1151 |
|
int i = cursor - 1; |
1154 |
|
valid = false; |
1155 |
|
break; |
1156 |
|
} |
1157 |
< |
prev = items[i]; |
1157 |
> |
prev = (E) items[i]; |
1158 |
|
if (lock.getSequence() == seq) { |
1159 |
|
valid = true; |
1160 |
|
break; |
1168 |
|
if (validNext || hasNext()) { |
1169 |
|
validNext = false; |
1170 |
|
lastRet = cursor++; |
1171 |
< |
return (E) next; |
1171 |
> |
return next; |
1172 |
|
} |
1173 |
|
throw new NoSuchElementException(); |
1174 |
|
} |
1177 |
|
if (validPrev || hasPrevious()) { |
1178 |
|
validPrev = false; |
1179 |
|
lastRet = cursor--; |
1180 |
< |
return (E) prev; |
1180 |
> |
return prev; |
1181 |
|
} |
1182 |
|
throw new NoSuchElementException(); |
1183 |
|
} |
1234 |
|
public int previousIndex() { return cursor - 1; } |
1235 |
|
} |
1236 |
|
|
1237 |
< |
static final class ReadMostlyVectorSublist<E> implements List<E>, RandomAccess, java.io.Serializable { |
1237 |
> |
static final class ReadMostlyVectorSublist<E> |
1238 |
> |
implements List<E>, RandomAccess, java.io.Serializable { |
1239 |
> |
static final long serialVersionUID = 3041673470172026059L; |
1240 |
> |
|
1241 |
|
final ReadMostlyVector<E> list; |
1242 |
|
final int offset; |
1243 |
|
volatile int size; |
1244 |
|
|
1245 |
< |
ReadMostlyVectorSublist(ReadMostlyVector<E> list, int offset, int size) { |
1245 |
> |
ReadMostlyVectorSublist(ReadMostlyVector<E> list, |
1246 |
> |
int offset, int size) { |
1247 |
|
this.list = list; |
1248 |
|
this.offset = offset; |
1249 |
|
this.size = size; |
1255 |
|
} |
1256 |
|
|
1257 |
|
public boolean add(E element) { |
1258 |
< |
SequenceLock lock = list.lock; |
1258 |
> |
final SequenceLock lock = list.lock; |
1259 |
|
lock.lock(); |
1260 |
|
try { |
1261 |
|
int c = size; |
1268 |
|
} |
1269 |
|
|
1270 |
|
public void add(int index, E element) { |
1271 |
< |
SequenceLock lock = list.lock; |
1271 |
> |
final SequenceLock lock = list.lock; |
1272 |
|
lock.lock(); |
1273 |
|
try { |
1274 |
|
if (index < 0 || index > size) |
1282 |
|
|
1283 |
|
public boolean addAll(Collection<? extends E> c) { |
1284 |
|
Object[] elements = c.toArray(); |
1285 |
< |
int added; |
1334 |
< |
SequenceLock lock = list.lock; |
1285 |
> |
final SequenceLock lock = list.lock; |
1286 |
|
lock.lock(); |
1287 |
|
try { |
1288 |
|
int s = size; |
1289 |
|
int pc = list.count; |
1290 |
|
list.rawAddAllAt(offset + s, elements); |
1291 |
< |
added = list.count - pc; |
1291 |
> |
int added = list.count - pc; |
1292 |
|
size = s + added; |
1293 |
+ |
return added != 0; |
1294 |
|
} finally { |
1295 |
|
lock.unlock(); |
1296 |
|
} |
1345 |
– |
return added != 0; |
1297 |
|
} |
1298 |
|
|
1299 |
|
public boolean addAll(int index, Collection<? extends E> c) { |
1300 |
|
Object[] elements = c.toArray(); |
1301 |
< |
int added; |
1351 |
< |
SequenceLock lock = list.lock; |
1301 |
> |
final SequenceLock lock = list.lock; |
1302 |
|
lock.lock(); |
1303 |
|
try { |
1304 |
|
int s = size; |
1306 |
|
throw new ArrayIndexOutOfBoundsException(index); |
1307 |
|
int pc = list.count; |
1308 |
|
list.rawAddAllAt(index + offset, elements); |
1309 |
< |
added = list.count - pc; |
1309 |
> |
int added = list.count - pc; |
1310 |
|
size = s + added; |
1311 |
+ |
return added != 0; |
1312 |
|
} finally { |
1313 |
|
lock.unlock(); |
1314 |
|
} |
1364 |
– |
return added != 0; |
1315 |
|
} |
1316 |
|
|
1317 |
|
public void clear() { |
1318 |
< |
SequenceLock lock = list.lock; |
1318 |
> |
final SequenceLock lock = list.lock; |
1319 |
|
lock.lock(); |
1320 |
|
try { |
1321 |
|
list.internalClear(offset, offset + size); |
1352 |
|
} |
1353 |
|
|
1354 |
|
public int indexOf(Object o) { |
1355 |
< |
SequenceLock lock = list.lock; |
1355 |
> |
final SequenceLock lock = list.lock; |
1356 |
|
long seq = lock.awaitAvailability(); |
1357 |
|
Object[] items = list.array; |
1358 |
|
int c = list.count; |
1380 |
|
} |
1381 |
|
|
1382 |
|
public int lastIndexOf(Object o) { |
1383 |
< |
SequenceLock lock = list.lock; |
1383 |
> |
final SequenceLock lock = list.lock; |
1384 |
|
long seq = lock.awaitAvailability(); |
1385 |
|
Object[] items = list.array; |
1386 |
|
int c = list.count; |
1408 |
|
} |
1409 |
|
|
1410 |
|
public E remove(int index) { |
1411 |
< |
Object result; |
1462 |
< |
SequenceLock lock = list.lock; |
1411 |
> |
final SequenceLock lock = list.lock; |
1412 |
|
lock.lock(); |
1413 |
|
try { |
1414 |
|
Object[] items = list.array; |
1415 |
|
int i = index + offset; |
1416 |
|
if (index < 0 || index >= size || i >= items.length) |
1417 |
|
throw new ArrayIndexOutOfBoundsException(index); |
1418 |
< |
result = items[i]; |
1418 |
> |
@SuppressWarnings("unchecked") |
1419 |
> |
E result = (E) items[i]; |
1420 |
|
list.rawRemoveAt(i); |
1421 |
|
size--; |
1422 |
+ |
return result; |
1423 |
|
} finally { |
1424 |
|
lock.unlock(); |
1425 |
|
} |
1475 |
– |
return (E)result; |
1426 |
|
} |
1427 |
|
|
1428 |
|
public boolean remove(Object o) { |
1429 |
< |
boolean removed = false; |
1480 |
< |
SequenceLock lock = list.lock; |
1429 |
> |
final SequenceLock lock = list.lock; |
1430 |
|
lock.lock(); |
1431 |
|
try { |
1432 |
|
if (list.rawRemoveAt(list.rawIndexOf(o, offset, |
1433 |
|
offset + size))) { |
1485 |
– |
removed = true; |
1434 |
|
--size; |
1435 |
+ |
return true; |
1436 |
|
} |
1437 |
+ |
else |
1438 |
+ |
return false; |
1439 |
|
} finally { |
1440 |
|
lock.unlock(); |
1441 |
|
} |
1491 |
– |
return removed; |
1442 |
|
} |
1443 |
|
|
1444 |
|
public boolean removeAll(Collection<?> c) { |
1486 |
|
final ReadMostlyVector<E> list; |
1487 |
|
final SequenceLock lock; |
1488 |
|
Object[] items; |
1489 |
< |
Object next, prev; |
1489 |
> |
E next, prev; |
1490 |
|
long seq; |
1491 |
|
int cursor; |
1492 |
|
int fence; |
1517 |
|
} while (lock.getSequence() != seq); |
1518 |
|
} |
1519 |
|
|
1520 |
+ |
@SuppressWarnings("unchecked") |
1521 |
|
public boolean hasNext() { |
1522 |
|
boolean valid; |
1523 |
|
int i = cursor; |
1526 |
|
valid = false; |
1527 |
|
break; |
1528 |
|
} |
1529 |
< |
next = items[i]; |
1529 |
> |
next = (E) items[i]; |
1530 |
|
if (lock.getSequence() == seq) { |
1531 |
|
valid = true; |
1532 |
|
break; |
1536 |
|
return validNext = valid; |
1537 |
|
} |
1538 |
|
|
1539 |
+ |
@SuppressWarnings("unchecked") |
1540 |
|
public boolean hasPrevious() { |
1541 |
|
boolean valid; |
1542 |
|
int i = cursor - 1; |
1545 |
|
valid = false; |
1546 |
|
break; |
1547 |
|
} |
1548 |
< |
prev = items[i]; |
1548 |
> |
prev = (E) items[i]; |
1549 |
|
if (lock.getSequence() == seq) { |
1550 |
|
valid = true; |
1551 |
|
break; |
1559 |
|
if (validNext || hasNext()) { |
1560 |
|
validNext = false; |
1561 |
|
lastRet = cursor++; |
1562 |
< |
return (E) next; |
1562 |
> |
return next; |
1563 |
|
} |
1564 |
|
throw new NoSuchElementException(); |
1565 |
|
} |
1568 |
|
if (validPrev || hasPrevious()) { |
1569 |
|
validPrev = false; |
1570 |
|
lastRet = cursor--; |
1571 |
< |
return (E) prev; |
1571 |
> |
return prev; |
1572 |
|
} |
1573 |
|
throw new NoSuchElementException(); |
1574 |
|
} |
1633 |
|
|
1634 |
|
} |
1635 |
|
} |
1684 |
– |
|