12 |
|
* A class with the same methods and array-based characteristics as |
13 |
|
* {@link java.util.Vector} but with reduced contention and improved |
14 |
|
* throughput when invocations of read-only methods by multiple |
15 |
< |
* threads are most common. |
15 |
> |
* threads are most common. |
16 |
|
* |
17 |
|
* <p> The iterators returned by this class's {@link #iterator() |
18 |
|
* iterator} and {@link #listIterator(int) listIterator} methods are |
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. |
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}. |
26 |
|
* |
27 |
|
* <p>Otherwise, this class supports all methods, under the same |
28 |
|
* documented specifications, as {@code Vector}. Consult {@link |
43 |
|
* Short methods,including get(index), continually retry obtaining |
44 |
|
* a snapshot of array, count, and element, using sequence number |
45 |
|
* to validate. |
46 |
< |
* |
46 |
> |
* |
47 |
|
* Methods that are potentially O(n) (or worse) try once in |
48 |
|
* read-only mode, and then lock. When in read-only mode, they |
49 |
|
* validate only at the end of an array scan unless the element is |
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) { |
154 |
|
* as well as sublist and iterator classes. |
155 |
|
*/ |
156 |
|
|
157 |
< |
final int validatedIndexOf(Object x, Object[] items, int index, int fence, |
157 |
> |
// Version of indexOf that returns -1 if either not present or invalid |
158 |
> |
final int validatedIndexOf(Object x, Object[] items, int index, int fence, |
159 |
|
long seq) { |
160 |
|
for (int i = index; i < fence; ++i) { |
161 |
|
Object e = items[i]; |
162 |
|
if (lock.getSequence() != seq) |
163 |
|
break; |
164 |
< |
if (x == null? e == null : (e != null && x.equals(e))) |
164 |
> |
if ((x == null) ? e == null : x.equals(e)) |
165 |
|
return i; |
166 |
|
} |
167 |
|
return -1; |
171 |
|
Object[] items = array; |
172 |
|
for (int i = index; i < fence; ++i) { |
173 |
|
Object e = items[i]; |
174 |
< |
if (x == null? e == null : (e != null && x.equals(e))) |
174 |
> |
if ((x == null) ? e == null : x.equals(e)) |
175 |
|
return i; |
176 |
|
} |
177 |
|
return -1; |
178 |
|
} |
179 |
|
|
180 |
< |
final int validatedLastIndexOf(Object x, Object[] items, |
180 |
> |
final int validatedLastIndexOf(Object x, Object[] items, |
181 |
|
int index, int origin, long seq) { |
182 |
|
for (int i = index; i >= origin; --i) { |
183 |
|
Object e = items[i]; |
184 |
|
if (lock.getSequence() != seq) |
185 |
|
break; |
186 |
< |
if (x == null? e == null : (e != null && x.equals(e))) |
186 |
> |
if ((x == null) ? e == null : x.equals(e)) |
187 |
|
return i; |
188 |
|
} |
189 |
|
return -1; |
193 |
|
Object[] items = array; |
194 |
|
for (int i = index; i >= origin; --i) { |
195 |
|
Object e = items[i]; |
196 |
< |
if (x == null? e == null : (e != null && x.equals(e))) |
196 |
> |
if ((x == null) ? e == null : x.equals(e)) |
197 |
|
return i; |
198 |
|
} |
199 |
|
return -1; |
200 |
|
} |
201 |
|
|
202 |
< |
final void internalAdd(Object e) { |
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 internalAddAt(int index, Object e) { |
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 internalAddAllAt(int index, Object[] elements) { |
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 internalRemoveAt(int index) { |
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 |
|
} |
269 |
|
int fence = bound < 0 || bound > n ? n : bound; |
270 |
|
if (origin >= 0 && origin < fence) { |
271 |
|
for (Object x : c) { |
272 |
< |
while (internalRemoveAt(rawIndexOf(x, origin, fence))) |
272 |
> |
while (rawRemoveAt(rawIndexOf(x, origin, fence))) |
273 |
|
removed = true; |
274 |
|
} |
275 |
|
} |
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 < fence) { |
293 |
< |
if (c.contains(array[i])) |
292 |
> |
while (i >= 0 && i < fence) { |
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); |
291 |
< |
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; |
325 |
– |
int n = count; |
339 |
|
if (n > len) |
340 |
|
continue; |
341 |
|
int fence = bound < 0 || bound > n ? n : bound; |
344 |
|
else { |
345 |
|
contained = true; |
346 |
|
for (Object e : c) { |
347 |
< |
if (validatedIndexOf(e, items, origin, fence, seq) < 0) { |
347 |
> |
int idx = (locked ? |
348 |
> |
rawIndexOf(e, origin, fence) : |
349 |
> |
validatedIndexOf(e, items, origin, |
350 |
> |
fence, seq)); |
351 |
> |
if (idx < 0) { |
352 |
|
contained = false; |
353 |
|
break; |
354 |
|
} |
368 |
|
|
369 |
|
final boolean internalEquals(List<?> list, int origin, int bound) { |
370 |
|
SequenceLock lock = this.lock; |
354 |
– |
boolean equal; |
371 |
|
boolean locked = false; |
372 |
+ |
boolean equal; |
373 |
|
try { |
374 |
< |
outer:for (;;) { |
358 |
< |
equal = true; |
374 |
> |
for (;;) { |
375 |
|
long seq = lock.awaitAvailability(); |
376 |
|
Object[] items = array; |
361 |
– |
int len = items.length; |
377 |
|
int n = count; |
378 |
< |
if (n > len) |
364 |
< |
continue; |
365 |
< |
int fence = bound < 0 || bound > n ? n : bound; |
366 |
< |
if (origin < 0) |
378 |
> |
if (n > items.length || origin < 0) |
379 |
|
equal = false; |
380 |
|
else { |
381 |
+ |
equal = true; |
382 |
+ |
int fence = bound < 0 || bound > n ? n : bound; |
383 |
|
Iterator<?> it = list.iterator(); |
384 |
|
for (int i = origin; i < fence; ++i) { |
385 |
< |
if (!it.hasNext()) { |
386 |
< |
equal = false; |
387 |
< |
break; |
388 |
< |
} |
389 |
< |
Object x = it.next(); |
390 |
< |
Object y = items[i]; |
377 |
< |
if (lock.getSequence() != seq) |
378 |
< |
continue outer; |
379 |
< |
if (x == null? y != null : (y == null || !x.equals(y))) { |
385 |
> |
Object x = items[i]; |
386 |
> |
Object y; |
387 |
> |
if ((!locked && lock.getSequence() != seq) || |
388 |
> |
!it.hasNext() || |
389 |
> |
(y = it.next()) == null ? |
390 |
> |
x != null : !y.equals(x)) { |
391 |
|
equal = false; |
392 |
|
break; |
393 |
|
} |
416 |
|
hash = 1; |
417 |
|
long seq = lock.awaitAvailability(); |
418 |
|
Object[] items = array; |
408 |
– |
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; |
439 |
– |
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; |
461 |
|
Object e = items[i]; |
462 |
|
if (e == this) |
463 |
|
sb.append("(this Collection)"); |
464 |
< |
else if (lock.getSequence() != seq) |
464 |
> |
else if (!locked && lock.getSequence() != seq) |
465 |
|
continue outer; |
466 |
|
else |
467 |
|
sb.append(e.toString()); |
494 |
|
result = null; |
495 |
|
long seq = lock.awaitAvailability(); |
496 |
|
Object[] items = array; |
486 |
– |
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; |
515 |
– |
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; |
531 |
|
int rlen = fence - origin; |
532 |
+ |
if (rlen < 0) |
533 |
+ |
rlen = 0; |
534 |
|
if (origin < 0 || alen >= rlen) { |
535 |
< |
if (rlen < 0) |
536 |
< |
rlen = 0; |
524 |
< |
else if (rlen > 0) |
525 |
< |
System.arraycopy(array, 0, a, origin, rlen); |
535 |
> |
if (rlen > 0) |
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, |
541 |
> |
else |
542 |
> |
result = (T[]) Arrays.copyOfRange(items, origin, |
543 |
|
fence, a.getClass()); |
544 |
|
if (lock.getSequence() == seq) |
545 |
|
break; |
559 |
|
SequenceLock lock = this.lock; |
560 |
|
lock.lock(); |
561 |
|
try { |
562 |
< |
internalAdd(e); |
562 |
> |
rawAdd(e); |
563 |
|
} finally { |
564 |
|
lock.unlock(); |
565 |
|
} |
570 |
|
SequenceLock lock = this.lock; |
571 |
|
lock.lock(); |
572 |
|
try { |
573 |
< |
internalAddAt(index, element); |
573 |
> |
rawAddAt(index, element); |
574 |
|
} finally { |
575 |
|
lock.unlock(); |
576 |
|
} |
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(); |
603 |
|
Object[] elements = c.toArray(); |
604 |
|
lock.lock(); |
605 |
|
try { |
606 |
< |
ret = internalAddAllAt(index, elements); |
606 |
> |
ret = rawAddAllAt(index, elements); |
607 |
|
} finally { |
608 |
|
lock.unlock(); |
609 |
|
} |
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(); |
637 |
|
return true; |
638 |
|
if (!(o instanceof List)) |
639 |
|
return false; |
640 |
< |
return internalEquals((List<?>)(o), 0, -1); |
640 |
> |
return internalEquals((List<?>)o, 0, -1); |
641 |
|
} |
642 |
|
|
643 |
|
public E get(int index) { |
644 |
|
SequenceLock lock = this.lock; |
645 |
|
for (;;) { |
646 |
|
long seq = lock.awaitAvailability(); |
632 |
– |
Object[] items = array; |
647 |
|
int n = count; |
648 |
+ |
Object[] items = array; |
649 |
|
if (n > items.length) |
650 |
|
continue; |
651 |
|
Object e; boolean ex; |
672 |
|
|
673 |
|
public int indexOf(Object o) { |
674 |
|
SequenceLock lock = this.lock; |
675 |
< |
long seq = lock.awaitAvailability(); |
676 |
< |
Object[] items = array; |
677 |
< |
int n = count; |
678 |
< |
if (n <= items.length) { |
679 |
< |
int idx = validatedIndexOf(o, items, 0, n, seq); |
680 |
< |
if (lock.getSequence() == seq) |
681 |
< |
return idx; |
682 |
< |
} |
683 |
< |
lock.lock(); |
684 |
< |
try { |
685 |
< |
return rawIndexOf(o, 0, count); |
686 |
< |
} finally { |
687 |
< |
lock.unlock(); |
675 |
> |
for (;;) { |
676 |
> |
long seq = lock.awaitAvailability(); |
677 |
> |
Object[] items = array; |
678 |
> |
int n = count; |
679 |
> |
if (n <= items.length) { |
680 |
> |
for (int i = 0; i < n; ++i) { |
681 |
> |
Object e = items[i]; |
682 |
> |
if (lock.getSequence() != seq) { |
683 |
> |
lock.lock(); |
684 |
> |
try { |
685 |
> |
return rawIndexOf(o, 0, count); |
686 |
> |
} finally { |
687 |
> |
lock.unlock(); |
688 |
> |
} |
689 |
> |
} |
690 |
> |
else if ((o == null) ? e == null : o.equals(e)) |
691 |
> |
return i; |
692 |
> |
} |
693 |
> |
return -1; |
694 |
> |
} |
695 |
|
} |
696 |
|
} |
697 |
|
|
698 |
|
public boolean isEmpty() { |
677 |
– |
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) { |
707 |
|
SequenceLock lock = this.lock; |
708 |
< |
long seq = lock.awaitAvailability(); |
709 |
< |
Object[] items = array; |
710 |
< |
int n = count; |
711 |
< |
if (n <= items.length) { |
712 |
< |
int idx = validatedLastIndexOf(o, items, n - 1, 0, seq); |
713 |
< |
if (lock.getSequence() == seq) |
714 |
< |
return idx; |
715 |
< |
} |
716 |
< |
lock.lock(); |
717 |
< |
try { |
718 |
< |
return rawLastIndexOf(o, count - 1, 0); |
719 |
< |
} finally { |
720 |
< |
lock.unlock(); |
708 |
> |
for (;;) { |
709 |
> |
long seq = lock.awaitAvailability(); |
710 |
> |
Object[] items = array; |
711 |
> |
int n = count; |
712 |
> |
if (n <= items.length) { |
713 |
> |
for (int i = n - 1; i >= 0; --i) { |
714 |
> |
Object e = items[i]; |
715 |
> |
if (lock.getSequence() != seq) { |
716 |
> |
lock.lock(); |
717 |
> |
try { |
718 |
> |
return rawLastIndexOf(o, 0, count); |
719 |
> |
} finally { |
720 |
> |
lock.unlock(); |
721 |
> |
} |
722 |
> |
} |
723 |
> |
else if ((o == null) ? e == null : o.equals(e)) |
724 |
> |
return i; |
725 |
> |
} |
726 |
> |
return -1; |
727 |
> |
} |
728 |
|
} |
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) { |
744 |
|
if (index < 0 || index >= count) |
745 |
|
throw new ArrayIndexOutOfBoundsException(index); |
746 |
|
oldValue = array[index]; |
747 |
< |
internalRemoveAt(index); |
747 |
> |
rawRemoveAt(index); |
748 |
|
} finally { |
749 |
|
lock.unlock(); |
750 |
|
} |
756 |
|
boolean removed; |
757 |
|
lock.lock(); |
758 |
|
try { |
759 |
< |
removed = internalRemoveAt(rawIndexOf(o, 0, count)); |
759 |
> |
removed = rawRemoveAt(rawIndexOf(o, 0, count)); |
760 |
|
} finally { |
761 |
|
lock.unlock(); |
762 |
|
} |
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() { |
762 |
– |
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() { |
817 |
|
* Append the element if not present. |
818 |
|
* |
819 |
|
* @param e element to be added to this list, if absent |
820 |
< |
* @return <tt>true</tt> if the element was added |
820 |
> |
* @return {@code true} if the element was added |
821 |
|
*/ |
822 |
|
public boolean addIfAbsent(E e) { |
823 |
|
boolean added; |
825 |
|
lock.lock(); |
826 |
|
try { |
827 |
|
if (rawIndexOf(e, 0, count) < 0) { |
828 |
< |
internalAdd(e); |
828 |
> |
rawAdd(e); |
829 |
|
added = true; |
830 |
|
} |
831 |
|
else |
857 |
|
for (int i = 0; i < clen; ++i) { |
858 |
|
Object e = cs[i]; |
859 |
|
if (rawIndexOf(e, 0, count) < 0) { |
860 |
< |
internalAdd(e); |
860 |
> |
rawAdd(e); |
861 |
|
++added; |
862 |
|
} |
863 |
|
} |
868 |
|
return added; |
869 |
|
} |
870 |
|
|
871 |
+ |
/** |
872 |
+ |
* Returns an iterator operating over a snapshot copy of the |
873 |
+ |
* elements of this collection created upon construction of the |
874 |
+ |
* iterator. The iterator does <em>NOT</em> support the |
875 |
+ |
* {@code remove} method. |
876 |
+ |
* |
877 |
+ |
* @return an iterator over the elements in this list in proper sequence |
878 |
+ |
*/ |
879 |
+ |
public Iterator<E> snapshotIterator() { |
880 |
+ |
return new SnapshotIterator<E>(this); |
881 |
+ |
} |
882 |
+ |
|
883 |
+ |
static final class SnapshotIterator<E> implements Iterator<E> { |
884 |
+ |
final Object[] items; |
885 |
+ |
int cursor; |
886 |
+ |
SnapshotIterator(ReadMostlyVector<E> v) { items = v.toArray(); } |
887 |
+ |
public boolean hasNext() { return cursor < items.length; } |
888 |
+ |
public E next() { |
889 |
+ |
if (cursor < items.length) |
890 |
+ |
return (E) items[cursor++]; |
891 |
+ |
throw new NoSuchElementException(); |
892 |
+ |
} |
893 |
+ |
public void remove() { throw new UnsupportedOperationException() ; } |
894 |
+ |
} |
895 |
+ |
|
896 |
|
// Vector-only methods |
897 |
|
|
898 |
|
/** See {@link Vector#firstElement} */ |
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() { |
1027 |
– |
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) |
1311 |
|
lock.lock(); |
1312 |
|
try { |
1313 |
|
int c = size; |
1314 |
< |
list.internalAddAt(c + offset, element); |
1314 |
> |
list.rawAddAt(c + offset, element); |
1315 |
|
size = c + 1; |
1316 |
|
} finally { |
1317 |
|
lock.unlock(); |
1325 |
|
try { |
1326 |
|
if (index < 0 || index > size) |
1327 |
|
throw new ArrayIndexOutOfBoundsException(index); |
1328 |
< |
list.internalAddAt(index + offset, element); |
1328 |
> |
list.rawAddAt(index + offset, element); |
1329 |
|
++size; |
1330 |
|
} finally { |
1331 |
|
lock.unlock(); |
1340 |
|
try { |
1341 |
|
int s = size; |
1342 |
|
int pc = list.count; |
1343 |
< |
list.internalAddAllAt(offset + s, elements); |
1343 |
> |
list.rawAddAllAt(offset + s, elements); |
1344 |
|
added = list.count - pc; |
1345 |
|
size = s + added; |
1346 |
|
} finally { |
1359 |
|
if (index < 0 || index > s) |
1360 |
|
throw new ArrayIndexOutOfBoundsException(index); |
1361 |
|
int pc = list.count; |
1362 |
< |
list.internalAddAllAt(index + offset, elements); |
1362 |
> |
list.rawAddAllAt(index + offset, elements); |
1363 |
|
added = list.count - pc; |
1364 |
|
size = s + added; |
1365 |
|
} finally { |
1411 |
|
Object[] items = list.array; |
1412 |
|
int c = list.count; |
1413 |
|
if (c <= items.length) { |
1414 |
< |
int idx = list.validatedIndexOf(o, items, offset, offset+size, seq); |
1414 |
> |
int idx = list.validatedIndexOf(o, items, offset, |
1415 |
> |
offset + size, seq); |
1416 |
|
if (lock.getSequence() == seq) |
1417 |
|
return idx < 0 ? -1 : idx - offset; |
1418 |
|
} |
1419 |
|
lock.lock(); |
1420 |
|
try { |
1421 |
< |
int idx = list.rawIndexOf(o, offset, offset+size); |
1421 |
> |
int idx = list.rawIndexOf(o, offset, offset + size); |
1422 |
|
return idx < 0 ? -1 : idx - offset; |
1423 |
|
} finally { |
1424 |
|
lock.unlock(); |
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) { |
1439 |
|
Object[] items = list.array; |
1440 |
|
int c = list.count; |
1441 |
|
if (c <= items.length) { |
1442 |
< |
int idx = list.validatedLastIndexOf(o, items, offset+size-1, |
1442 |
> |
int idx = list.validatedLastIndexOf(o, items, offset+size-1, |
1443 |
|
offset, seq); |
1444 |
|
if (lock.getSequence() == seq) |
1445 |
|
return idx < 0 ? -1 : idx - offset; |
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) { |
1466 |
|
SequenceLock lock = list.lock; |
1467 |
|
lock.lock(); |
1468 |
|
try { |
1469 |
< |
if (index < 0 || index >= size) |
1414 |
< |
throw new ArrayIndexOutOfBoundsException(index); |
1469 |
> |
Object[] items = list.array; |
1470 |
|
int i = index + offset; |
1471 |
< |
result = list.array[i]; |
1472 |
< |
list.internalRemoveAt(i); |
1471 |
> |
if (index < 0 || index >= size || i >= items.length) |
1472 |
> |
throw new ArrayIndexOutOfBoundsException(index); |
1473 |
> |
result = items[i]; |
1474 |
> |
list.rawRemoveAt(i); |
1475 |
|
size--; |
1476 |
|
} finally { |
1477 |
|
lock.unlock(); |
1484 |
|
SequenceLock lock = list.lock; |
1485 |
|
lock.lock(); |
1486 |
|
try { |
1487 |
< |
if (list.internalRemoveAt(list.rawIndexOf(o, offset, |
1488 |
< |
offset + size))) { |
1487 |
> |
if (list.rawRemoveAt(list.rawIndexOf(o, offset, |
1488 |
> |
offset + size))) { |
1489 |
|
removed = true; |
1490 |
|
--size; |
1491 |
|
} |
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() { |