9 |
|
import java.util.*; |
10 |
|
|
11 |
|
/** |
12 |
< |
* A class with the same API and array-based characteristics as {@link |
13 |
< |
* java.util.Vector} but with reduced contention and improved |
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. Instances of this class may have |
16 |
< |
* relatively poorer performance in other contexts. |
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 |
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 |
|
/* |
40 |
|
* This class exists mainly as a vehicle to exercise various |
41 |
< |
* constructions using SequenceLocks, which are not yet explained |
42 |
< |
* well here. |
41 |
> |
* constructions using SequenceLocks. Read-only methods |
42 |
> |
* take one of a few forms: |
43 |
> |
* |
44 |
> |
* Short methods,including get(index), continually retry obtaining |
45 |
> |
* a snapshot of array, count, and element, using sequence number |
46 |
> |
* to validate. |
47 |
> |
* |
48 |
> |
* Methods that are potentially O(n) (or worse) try once in |
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 |
|
|
54 |
|
/** |
58 |
|
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8; |
59 |
|
|
60 |
|
// fields are non-private to simpify nested class access |
61 |
< |
Object[] array; |
61 |
> |
volatile Object[] array; |
62 |
|
final SequenceLock lock; |
63 |
< |
int count; |
63 |
> |
volatile int count; |
64 |
|
final int capacityIncrement; |
65 |
|
|
66 |
|
/** |
130 |
|
} |
131 |
|
|
132 |
|
// For explanation, see CopyOnWriteArrayList |
133 |
< |
final void grow(int minCapacity) { |
133 |
> |
final Object[] grow(int minCapacity) { |
134 |
|
int oldCapacity = array.length; |
135 |
|
int newCapacity = oldCapacity + ((capacityIncrement > 0) ? |
136 |
|
capacityIncrement : oldCapacity); |
138 |
|
newCapacity = minCapacity; |
139 |
|
if (newCapacity - MAX_ARRAY_SIZE > 0) |
140 |
|
newCapacity = hugeCapacity(minCapacity); |
141 |
< |
array = Arrays.copyOf(array, newCapacity); |
141 |
> |
return array = Arrays.copyOf(array, newCapacity); |
142 |
|
} |
143 |
|
|
144 |
|
static int hugeCapacity(int minCapacity) { |
155 |
|
* as well as sublist and iterator classes. |
156 |
|
*/ |
157 |
|
|
158 |
< |
static int internalIndexOf(Object o, Object[] items, |
159 |
< |
int index, int fence) { |
158 |
> |
// Version of indexOf that returns -1 if either not present or invalid |
159 |
> |
final int validatedIndexOf(Object x, Object[] items, int index, int fence, |
160 |
> |
long seq) { |
161 |
> |
for (int i = index; i < fence; ++i) { |
162 |
> |
Object e = items[i]; |
163 |
> |
if (lock.getSequence() != seq) |
164 |
> |
break; |
165 |
> |
if ((x == null) ? e == null : x.equals(e)) |
166 |
> |
return i; |
167 |
> |
} |
168 |
> |
return -1; |
169 |
> |
} |
170 |
> |
|
171 |
> |
final int rawIndexOf(Object x, int index, int fence) { |
172 |
> |
Object[] items = array; |
173 |
|
for (int i = index; i < fence; ++i) { |
174 |
< |
Object x = items[i]; |
175 |
< |
if (o == null? x == null : (x != null && o.equals(x))) |
174 |
> |
Object e = items[i]; |
175 |
> |
if ((x == null) ? e == null : x.equals(e)) |
176 |
|
return i; |
177 |
|
} |
178 |
|
return -1; |
179 |
|
} |
180 |
|
|
181 |
< |
static int internalLastIndexOf(Object o, Object[] items, |
182 |
< |
int index, int origin) { |
181 |
> |
final int validatedLastIndexOf(Object x, Object[] items, |
182 |
> |
int index, int origin, long seq) { |
183 |
|
for (int i = index; i >= origin; --i) { |
184 |
< |
Object x = items[i]; |
185 |
< |
if (o == null? x == null : (x != null && o.equals(x))) |
184 |
> |
Object e = items[i]; |
185 |
> |
if (lock.getSequence() != seq) |
186 |
> |
break; |
187 |
> |
if ((x == null) ? e == null : x.equals(e)) |
188 |
|
return i; |
189 |
|
} |
190 |
|
return -1; |
191 |
|
} |
192 |
|
|
193 |
< |
final void internalAdd(E e) { |
194 |
< |
int c = count; |
195 |
< |
if (c >= array.length) |
196 |
< |
grow(c + 1); |
197 |
< |
array[c] = e; |
198 |
< |
count = c + 1; |
193 |
> |
final int rawLastIndexOf(Object x, int index, int origin) { |
194 |
> |
Object[] items = array; |
195 |
> |
for (int i = index; i >= origin; --i) { |
196 |
> |
Object e = items[i]; |
197 |
> |
if ((x == null) ? e == null : x.equals(e)) |
198 |
> |
return i; |
199 |
> |
} |
200 |
> |
return -1; |
201 |
> |
} |
202 |
> |
|
203 |
> |
final void rawAdd(E e) { |
204 |
> |
int n = count; |
205 |
> |
Object[] items = array; |
206 |
> |
if (n >= items.length) |
207 |
> |
items = grow(n + 1); |
208 |
> |
items[n] = e; |
209 |
> |
count = n + 1; |
210 |
|
} |
211 |
|
|
212 |
< |
final void internalAddAt(int index, E e) { |
213 |
< |
int c = count; |
214 |
< |
if (index > c) |
212 |
> |
final void rawAddAt(int index, E e) { |
213 |
> |
int n = count; |
214 |
> |
Object[] items = array; |
215 |
> |
if (index > n) |
216 |
|
throw new ArrayIndexOutOfBoundsException(index); |
217 |
< |
if (c >= array.length) |
218 |
< |
grow(c + 1); |
219 |
< |
System.arraycopy(array, index, array, index + 1, c - index); |
220 |
< |
array[index] = e; |
221 |
< |
count = c + 1; |
217 |
> |
if (n >= items.length) |
218 |
> |
items = grow(n + 1); |
219 |
> |
if (index < n) |
220 |
> |
System.arraycopy(items, index, items, index + 1, n - index); |
221 |
> |
items[index] = e; |
222 |
> |
count = n + 1; |
223 |
|
} |
224 |
|
|
225 |
< |
final boolean internalAddAllAt(int index, Object[] elements) { |
226 |
< |
int c = count; |
227 |
< |
if (index < 0 || index > c) |
225 |
> |
final boolean rawAddAllAt(int index, Object[] elements) { |
226 |
> |
int n = count; |
227 |
> |
Object[] items = array; |
228 |
> |
if (index < 0 || index > n) |
229 |
|
throw new ArrayIndexOutOfBoundsException(index); |
230 |
|
int len = elements.length; |
231 |
|
if (len == 0) |
232 |
|
return false; |
233 |
< |
int newCount = c + len; |
234 |
< |
if (newCount >= array.length) |
235 |
< |
grow(newCount); |
236 |
< |
int mv = count - index; |
233 |
> |
int newCount = n + len; |
234 |
> |
if (newCount >= items.length) |
235 |
> |
items = grow(newCount); |
236 |
> |
int mv = n - index; |
237 |
|
if (mv > 0) |
238 |
< |
System.arraycopy(array, index, array, index + len, mv); |
239 |
< |
System.arraycopy(elements, 0, array, index, len); |
238 |
> |
System.arraycopy(items, index, items, index + len, mv); |
239 |
> |
System.arraycopy(elements, 0, items, index, len); |
240 |
|
count = newCount; |
241 |
|
return true; |
242 |
|
} |
243 |
|
|
244 |
< |
final boolean internalRemoveAt(int index) { |
245 |
< |
int c = count - 1; |
246 |
< |
if (index < 0 || index > c) |
244 |
> |
final boolean rawRemoveAt(int index) { |
245 |
> |
int n = count - 1; |
246 |
> |
Object[] items = array; |
247 |
> |
if (index < 0 || index > n) |
248 |
|
return false; |
249 |
< |
int mv = c - index; |
249 |
> |
int mv = n - index; |
250 |
|
if (mv > 0) |
251 |
< |
System.arraycopy(array, index + 1, array, index, mv); |
252 |
< |
array[c] = null; |
253 |
< |
count = c; |
251 |
> |
System.arraycopy(items, index + 1, items, index, mv); |
252 |
> |
items[n] = null; |
253 |
> |
count = n; |
254 |
|
return true; |
255 |
|
} |
256 |
|
|
257 |
|
/** |
258 |
|
* Internal version of removeAll for lists and sublists. In this |
259 |
< |
* and other similar methods below, the span argument is, if |
260 |
< |
* non-negative, the purported size of a list/sublist, or is left |
261 |
< |
* negative if the size should be determined via count field under |
262 |
< |
* lock. |
259 |
> |
* and other similar methods below, the bound argument is, if |
260 |
> |
* non-negative, the purported upper bound of a list/sublist, or |
261 |
> |
* is left negative if the bound should be determined via count |
262 |
> |
* field under lock. |
263 |
|
*/ |
264 |
< |
final boolean internalRemoveAll(Collection<?> c, int origin, int span) { |
265 |
< |
SequenceLock lock = this.lock; |
264 |
> |
final boolean internalRemoveAll(Collection<?> c, int origin, int bound) { |
265 |
> |
final SequenceLock lock = this.lock; |
266 |
|
boolean removed = false; |
267 |
|
lock.lock(); |
268 |
|
try { |
269 |
< |
int fence = count; |
270 |
< |
if (span >= 0 && origin + span < fence) |
229 |
< |
fence = origin + span; |
269 |
> |
int n = count; |
270 |
> |
int fence = bound < 0 || bound > n ? n : bound; |
271 |
|
if (origin >= 0 && origin < fence) { |
272 |
|
for (Object x : c) { |
273 |
< |
while (internalRemoveAt(internalIndexOf(x, array, |
233 |
< |
origin, fence))) |
273 |
> |
while (rawRemoveAt(rawIndexOf(x, origin, fence))) |
274 |
|
removed = true; |
275 |
|
} |
276 |
|
} |
280 |
|
return removed; |
281 |
|
} |
282 |
|
|
283 |
< |
final boolean internalRetainAll(Collection<?> c, int origin, int span) { |
284 |
< |
SequenceLock lock = this.lock; |
283 |
> |
final boolean internalRetainAll(Collection<?> c, int origin, int bound) { |
284 |
> |
final SequenceLock lock = this.lock; |
285 |
|
boolean removed = false; |
286 |
|
if (c != this) { |
287 |
|
lock.lock(); |
288 |
|
try { |
289 |
+ |
Object[] items = array; |
290 |
|
int i = origin; |
291 |
< |
int fence = count; |
292 |
< |
if (span >= 0 && origin + span < fence) |
293 |
< |
fence = origin + span; |
294 |
< |
while (i < fence) { |
254 |
< |
if (c.contains(array[i])) |
291 |
> |
int n = count; |
292 |
> |
int fence = bound < 0 || bound > n ? n : bound; |
293 |
> |
while (i >= 0 && i < fence) { |
294 |
> |
if (c.contains(items[i])) |
295 |
|
++i; |
296 |
|
else { |
297 |
|
--fence; |
298 |
< |
int mv = --count - i; |
298 |
> |
int mv = --n - i; |
299 |
|
if (mv > 0) |
300 |
< |
System.arraycopy(array, i + 1, array, i, mv); |
261 |
< |
removed = true; |
300 |
> |
System.arraycopy(items, i + 1, items, i, mv); |
301 |
|
} |
302 |
|
} |
303 |
+ |
if (count != n) { |
304 |
+ |
count = n; |
305 |
+ |
removed = true; |
306 |
+ |
} |
307 |
|
} finally { |
308 |
|
lock.unlock(); |
309 |
|
} |
311 |
|
return removed; |
312 |
|
} |
313 |
|
|
314 |
< |
final void internalClear(int origin, int span) { |
315 |
< |
int c = count; |
316 |
< |
int fence = c; |
274 |
< |
if (span >= 0 && origin + span < fence) |
275 |
< |
fence = origin + span; |
314 |
> |
final void internalClear(int origin, int bound) { |
315 |
> |
int n = count; |
316 |
> |
int fence = bound < 0 || bound > n ? n : bound; |
317 |
|
if (origin >= 0 && origin < fence) { |
318 |
+ |
Object[] items = array; |
319 |
|
int removed = fence - origin; |
320 |
< |
int newCount = c - removed; |
321 |
< |
int mv = c - (origin + removed); |
320 |
> |
int newCount = n - removed; |
321 |
> |
int mv = n - (origin + removed); |
322 |
|
if (mv > 0) |
323 |
< |
System.arraycopy(array, origin + removed, array, origin, mv); |
324 |
< |
for (int i = c; i < newCount; ++i) |
325 |
< |
array[i] = null; |
323 |
> |
System.arraycopy(items, origin + removed, items, origin, mv); |
324 |
> |
for (int i = n; i < newCount; ++i) |
325 |
> |
items[i] = null; |
326 |
|
count = newCount; |
327 |
|
} |
328 |
|
} |
329 |
|
|
330 |
< |
final boolean internalContainsAll(Collection<?> coll, int origin, int span) { |
331 |
< |
SequenceLock lock = this.lock; |
330 |
> |
final boolean internalContainsAll(Collection<?> c, int origin, int bound) { |
331 |
> |
final SequenceLock lock = this.lock; |
332 |
|
boolean contained; |
333 |
|
boolean locked = false; |
334 |
|
try { |
335 |
|
for (;;) { |
336 |
|
long seq = lock.awaitAvailability(); |
337 |
+ |
int n = count; |
338 |
|
Object[] items = array; |
339 |
|
int len = items.length; |
340 |
< |
int c = count; |
298 |
< |
if (c > len) |
340 |
> |
if (n > len) |
341 |
|
continue; |
342 |
< |
int fence = c; |
343 |
< |
if (span >= 0 && origin + span < fence) |
302 |
< |
fence = origin + span; |
303 |
< |
if (origin < 0 || fence > c) |
342 |
> |
int fence = bound < 0 || bound > n ? n : bound; |
343 |
> |
if (origin < 0) |
344 |
|
contained = false; |
345 |
|
else { |
346 |
|
contained = true; |
347 |
< |
for (Object e : coll) { |
348 |
< |
if (internalIndexOf(e, items, origin, fence) < 0) { |
347 |
> |
for (Object e : c) { |
348 |
> |
int idx = (locked ? |
349 |
> |
rawIndexOf(e, origin, fence) : |
350 |
> |
validatedIndexOf(e, items, origin, |
351 |
> |
fence, seq)); |
352 |
> |
if (idx < 0) { |
353 |
|
contained = false; |
354 |
|
break; |
355 |
|
} |
367 |
|
return contained; |
368 |
|
} |
369 |
|
|
370 |
< |
final boolean internalEquals(List<?> list, int origin, int span) { |
371 |
< |
SequenceLock lock = this.lock; |
328 |
< |
boolean equal; |
370 |
> |
final boolean internalEquals(List<?> list, int origin, int bound) { |
371 |
> |
final SequenceLock lock = this.lock; |
372 |
|
boolean locked = false; |
373 |
+ |
boolean equal; |
374 |
|
try { |
375 |
|
for (;;) { |
332 |
– |
equal = true; |
376 |
|
long seq = lock.awaitAvailability(); |
377 |
|
Object[] items = array; |
378 |
< |
int len = items.length; |
379 |
< |
int c = count; |
337 |
< |
if (c > len) |
338 |
< |
continue; |
339 |
< |
int fence = c; |
340 |
< |
if (span >= 0 && origin + span < fence) |
341 |
< |
fence = origin + span; |
342 |
< |
if (origin < 0 || fence > c) |
378 |
> |
int n = count; |
379 |
> |
if (n > items.length || origin < 0) |
380 |
|
equal = false; |
381 |
|
else { |
382 |
+ |
equal = true; |
383 |
+ |
int fence = bound < 0 || bound > n ? n : bound; |
384 |
|
Iterator<?> it = list.iterator(); |
385 |
|
for (int i = origin; i < fence; ++i) { |
386 |
< |
if (!it.hasNext()) { |
387 |
< |
equal = false; |
388 |
< |
break; |
389 |
< |
} |
390 |
< |
Object x = it.next(); |
391 |
< |
Object y = items[i]; |
353 |
< |
if (x == null? y != null : (y == null || !x.equals(y))) { |
386 |
> |
Object x = items[i]; |
387 |
> |
Object y; |
388 |
> |
if ((!locked && lock.getSequence() != seq) || |
389 |
> |
!it.hasNext() || |
390 |
> |
(y = it.next()) == null ? |
391 |
> |
x != null : !y.equals(x)) { |
392 |
|
equal = false; |
393 |
|
break; |
394 |
|
} |
408 |
|
return equal; |
409 |
|
} |
410 |
|
|
411 |
< |
final int internalHashCode(int origin, int span) { |
412 |
< |
SequenceLock lock = this.lock; |
411 |
> |
final int internalHashCode(int origin, int bound) { |
412 |
> |
final SequenceLock lock = this.lock; |
413 |
|
int hash; |
414 |
|
boolean locked = false; |
415 |
|
try { |
417 |
|
hash = 1; |
418 |
|
long seq = lock.awaitAvailability(); |
419 |
|
Object[] items = array; |
420 |
+ |
int n = count; |
421 |
|
int len = items.length; |
422 |
< |
int c = count; |
384 |
< |
if (c > len) |
422 |
> |
if (n > len) |
423 |
|
continue; |
424 |
< |
int fence = c; |
425 |
< |
if (span >= 0 && origin + span < fence) |
388 |
< |
fence = origin + span; |
389 |
< |
if (origin >= 0 && fence <= c) { |
424 |
> |
int fence = bound < 0 || bound > n ? n : bound; |
425 |
> |
if (origin >= 0) { |
426 |
|
for (int i = origin; i < fence; ++i) { |
427 |
|
Object e = items[i]; |
428 |
|
hash = 31*hash + (e == null ? 0 : e.hashCode()); |
440 |
|
return hash; |
441 |
|
} |
442 |
|
|
443 |
< |
final String internalToString(int origin, int span) { |
444 |
< |
SequenceLock lock = this.lock; |
443 |
> |
final String internalToString(int origin, int bound) { |
444 |
> |
final SequenceLock lock = this.lock; |
445 |
|
String ret; |
446 |
|
boolean locked = false; |
447 |
|
try { |
448 |
< |
for (;;) { |
448 |
> |
outer:for (;;) { |
449 |
|
long seq = lock.awaitAvailability(); |
450 |
|
Object[] items = array; |
451 |
+ |
int n = count; |
452 |
|
int len = items.length; |
453 |
< |
int c = count; |
417 |
< |
if (c > len) |
453 |
> |
if (n > len) |
454 |
|
continue; |
455 |
< |
int fence = c; |
456 |
< |
if (span >= 0 && origin + span < fence) |
457 |
< |
fence = origin + span; |
458 |
< |
if (origin >= 0 && fence <= c) { |
459 |
< |
if (origin == fence) |
460 |
< |
ret = "[]"; |
461 |
< |
else { |
462 |
< |
StringBuilder sb = new StringBuilder(); |
463 |
< |
sb.append('['); |
464 |
< |
for (int i = origin;;) { |
465 |
< |
Object e = items[i]; |
466 |
< |
sb.append(e == this ? "(this Collection)" : e); |
467 |
< |
if (++i < fence) |
468 |
< |
sb.append(',').append(' '); |
469 |
< |
else { |
470 |
< |
ret = sb.append(']').toString(); |
471 |
< |
break; |
472 |
< |
} |
455 |
> |
int fence = bound < 0 || bound > n ? n : bound; |
456 |
> |
if (origin < 0 || origin == fence) |
457 |
> |
ret = "[]"; |
458 |
> |
else { |
459 |
> |
StringBuilder sb = new StringBuilder(); |
460 |
> |
sb.append('['); |
461 |
> |
for (int i = origin;;) { |
462 |
> |
Object e = items[i]; |
463 |
> |
if (e == this) |
464 |
> |
sb.append("(this Collection)"); |
465 |
> |
else if (!locked && lock.getSequence() != seq) |
466 |
> |
continue outer; |
467 |
> |
else |
468 |
> |
sb.append(e.toString()); |
469 |
> |
if (++i < fence) |
470 |
> |
sb.append(',').append(' '); |
471 |
> |
else { |
472 |
> |
ret = sb.append(']').toString(); |
473 |
> |
break; |
474 |
|
} |
475 |
|
} |
439 |
– |
if (lock.getSequence() == seq) |
440 |
– |
break; |
476 |
|
} |
477 |
+ |
if (lock.getSequence() == seq) |
478 |
+ |
break; |
479 |
|
lock.lock(); |
480 |
|
locked = true; |
481 |
|
} |
486 |
|
return ret; |
487 |
|
} |
488 |
|
|
489 |
< |
final Object[] internalToArray(int origin, int span) { |
489 |
> |
final Object[] internalToArray(int origin, int bound) { |
490 |
|
Object[] result; |
491 |
< |
SequenceLock lock = this.lock; |
491 |
> |
final SequenceLock lock = this.lock; |
492 |
|
boolean locked = false; |
493 |
|
try { |
494 |
|
for (;;) { |
495 |
|
result = null; |
496 |
|
long seq = lock.awaitAvailability(); |
497 |
|
Object[] items = array; |
498 |
+ |
int n = count; |
499 |
|
int len = items.length; |
500 |
< |
int c = count; |
501 |
< |
int fence = c; |
502 |
< |
if (span >= 0 && origin + span < fence) |
503 |
< |
fence = origin + span; |
466 |
< |
if (c <= len && fence <= len) { |
500 |
> |
if (n > len) |
501 |
> |
continue; |
502 |
> |
int fence = bound < 0 || bound > n ? n : bound; |
503 |
> |
if (origin >= 0) |
504 |
|
result = Arrays.copyOfRange(items, origin, fence, |
505 |
|
Object[].class); |
506 |
< |
if (lock.getSequence() == seq) |
507 |
< |
break; |
471 |
< |
} |
506 |
> |
if (lock.getSequence() == seq) |
507 |
> |
break; |
508 |
|
lock.lock(); |
509 |
|
locked = true; |
510 |
|
} |
515 |
|
return result; |
516 |
|
} |
517 |
|
|
518 |
< |
final <T> T[] internalToArray(T[] a, int origin, int span) { |
518 |
> |
@SuppressWarnings("unchecked") |
519 |
> |
final <T> T[] internalToArray(T[] a, int origin, int bound) { |
520 |
> |
int alen = a.length; |
521 |
|
T[] result; |
522 |
< |
SequenceLock lock = this.lock; |
522 |
> |
final SequenceLock lock = this.lock; |
523 |
|
boolean locked = false; |
524 |
|
try { |
525 |
|
for (;;) { |
526 |
|
long seq = lock.awaitAvailability(); |
527 |
|
Object[] items = array; |
528 |
+ |
int n = count; |
529 |
|
int len = items.length; |
530 |
< |
int c = count; |
531 |
< |
int fence = c; |
532 |
< |
if (span >= 0 && origin + span < fence) |
533 |
< |
fence = origin + span; |
534 |
< |
if (c <= len && fence <= len) { |
535 |
< |
if (a.length < count) |
536 |
< |
result = (T[]) Arrays.copyOfRange(array, origin, |
537 |
< |
fence, a.getClass()); |
538 |
< |
else { |
539 |
< |
int n = fence - origin; |
540 |
< |
System.arraycopy(array, 0, a, origin, fence - origin); |
541 |
< |
if (a.length > n) |
503 |
< |
a[n] = null; |
504 |
< |
result = a; |
505 |
< |
} |
506 |
< |
if (lock.getSequence() == seq) |
507 |
< |
break; |
530 |
> |
if (n > len) |
531 |
> |
continue; |
532 |
> |
int fence = bound < 0 || bound > n ? n : bound; |
533 |
> |
int rlen = fence - origin; |
534 |
> |
if (rlen < 0) |
535 |
> |
rlen = 0; |
536 |
> |
if (origin < 0 || alen >= rlen) { |
537 |
> |
if (rlen > 0) |
538 |
> |
System.arraycopy(items, 0, a, origin, rlen); |
539 |
> |
if (alen > rlen) |
540 |
> |
a[rlen] = null; |
541 |
> |
result = a; |
542 |
|
} |
543 |
+ |
else |
544 |
+ |
result = (T[]) Arrays.copyOfRange(items, origin, |
545 |
+ |
fence, a.getClass()); |
546 |
+ |
if (lock.getSequence() == seq) |
547 |
+ |
break; |
548 |
|
lock.lock(); |
549 |
|
locked = true; |
550 |
|
} |
558 |
|
// public List methods |
559 |
|
|
560 |
|
public boolean add(E e) { |
561 |
< |
SequenceLock lock = this.lock; |
561 |
> |
final SequenceLock lock = this.lock; |
562 |
|
lock.lock(); |
563 |
|
try { |
564 |
< |
internalAdd(e); |
564 |
> |
rawAdd(e); |
565 |
|
} finally { |
566 |
|
lock.unlock(); |
567 |
|
} |
569 |
|
} |
570 |
|
|
571 |
|
public void add(int index, E element) { |
572 |
< |
SequenceLock lock = this.lock; |
572 |
> |
final SequenceLock lock = this.lock; |
573 |
|
lock.lock(); |
574 |
|
try { |
575 |
< |
internalAddAt(index, element); |
575 |
> |
rawAddAt(index, element); |
576 |
|
} finally { |
577 |
|
lock.unlock(); |
578 |
|
} |
583 |
|
int len = elements.length; |
584 |
|
if (len == 0) |
585 |
|
return false; |
586 |
< |
SequenceLock lock = this.lock; |
586 |
> |
final SequenceLock lock = this.lock; |
587 |
|
lock.lock(); |
588 |
|
try { |
589 |
< |
int newCount = count + len; |
590 |
< |
if (newCount >= array.length) |
591 |
< |
grow(newCount); |
592 |
< |
System.arraycopy(elements, 0, array, count, len); |
589 |
> |
Object[] items = array; |
590 |
> |
int n = count; |
591 |
> |
int newCount = n + len; |
592 |
> |
if (newCount >= items.length) |
593 |
> |
items = grow(newCount); |
594 |
> |
System.arraycopy(elements, 0, items, n, len); |
595 |
|
count = newCount; |
596 |
|
} finally { |
597 |
|
lock.unlock(); |
600 |
|
} |
601 |
|
|
602 |
|
public boolean addAll(int index, Collection<? extends E> c) { |
603 |
< |
SequenceLock lock = this.lock; |
603 |
> |
final SequenceLock lock = this.lock; |
604 |
|
boolean ret; |
605 |
|
Object[] elements = c.toArray(); |
606 |
|
lock.lock(); |
607 |
|
try { |
608 |
< |
ret = internalAddAllAt(index, elements); |
608 |
> |
ret = rawAddAllAt(index, elements); |
609 |
|
} finally { |
610 |
|
lock.unlock(); |
611 |
|
} |
613 |
|
} |
614 |
|
|
615 |
|
public void clear() { |
616 |
< |
SequenceLock lock = this.lock; |
616 |
> |
final SequenceLock lock = this.lock; |
617 |
|
lock.lock(); |
618 |
|
try { |
619 |
< |
for (int i = 0; i < count; i++) |
620 |
< |
array[i] = null; |
619 |
> |
int n = count; |
620 |
> |
Object[] items = array; |
621 |
> |
for (int i = 0; i < n; i++) |
622 |
> |
items[i] = null; |
623 |
|
count = 0; |
624 |
|
} finally { |
625 |
|
lock.unlock(); |
639 |
|
return true; |
640 |
|
if (!(o instanceof List)) |
641 |
|
return false; |
642 |
< |
return internalEquals((List<?>)(o), 0, -1); |
642 |
> |
return internalEquals((List<?>)o, 0, -1); |
643 |
|
} |
644 |
|
|
645 |
|
public E get(int index) { |
646 |
< |
SequenceLock lock = this.lock; |
646 |
> |
final SequenceLock lock = this.lock; |
647 |
|
for (;;) { |
648 |
|
long seq = lock.awaitAvailability(); |
649 |
+ |
int n = count; |
650 |
|
Object[] items = array; |
651 |
< |
int len = items.length; |
652 |
< |
int c = count; |
609 |
< |
if (c > len) |
610 |
< |
continue; |
611 |
< |
E e; boolean ex; |
612 |
< |
if (index < 0 || index >= c) { |
613 |
< |
e = null; |
614 |
< |
ex = true; |
615 |
< |
} |
616 |
< |
else { |
617 |
< |
e = (E)items[index]; |
618 |
< |
ex = false; |
619 |
< |
} |
651 |
> |
@SuppressWarnings("unchecked") |
652 |
> |
E e = (index < items.length) ? (E) items[index] : null; |
653 |
|
if (lock.getSequence() == seq) { |
654 |
< |
if (ex) |
654 |
> |
if (index >= n) |
655 |
|
throw new ArrayIndexOutOfBoundsException(index); |
656 |
< |
else |
624 |
< |
return e; |
656 |
> |
return e; |
657 |
|
} |
658 |
|
} |
659 |
|
} |
663 |
|
} |
664 |
|
|
665 |
|
public int indexOf(Object o) { |
666 |
< |
SequenceLock lock = this.lock; |
635 |
< |
long seq = lock.awaitAvailability(); |
636 |
< |
Object[] items = array; |
637 |
< |
int c = count; |
638 |
< |
if (c <= items.length) { |
639 |
< |
int idx = internalIndexOf(o, items, 0, c); |
640 |
< |
if (lock.getSequence() == seq) |
641 |
< |
return idx; |
642 |
< |
} |
643 |
< |
lock.lock(); |
644 |
< |
try { |
645 |
< |
return internalIndexOf(o, array, 0, count); |
646 |
< |
} finally { |
647 |
< |
lock.unlock(); |
648 |
< |
} |
666 |
> |
return indexOf(o, 0); |
667 |
|
} |
668 |
|
|
669 |
|
public boolean isEmpty() { |
652 |
– |
long ignore = lock.getSequence(); |
670 |
|
return count == 0; |
671 |
|
} |
672 |
|
|
673 |
|
public Iterator<E> iterator() { |
674 |
< |
return new Itr(this, 0); |
674 |
> |
return new Itr<E>(this, 0); |
675 |
|
} |
676 |
|
|
677 |
|
public int lastIndexOf(Object o) { |
678 |
< |
SequenceLock lock = this.lock; |
679 |
< |
long seq = lock.awaitAvailability(); |
680 |
< |
Object[] items = array; |
681 |
< |
int c = count; |
682 |
< |
if (c <= items.length) { |
683 |
< |
int idx = internalLastIndexOf(o, items, c - 1, 0); |
684 |
< |
if (lock.getSequence() == seq) |
685 |
< |
return idx; |
686 |
< |
} |
687 |
< |
lock.lock(); |
688 |
< |
try { |
689 |
< |
return internalLastIndexOf(o, array, count-1, 0); |
690 |
< |
} finally { |
691 |
< |
lock.unlock(); |
678 |
> |
final SequenceLock lock = this.lock; |
679 |
> |
for (;;) { |
680 |
> |
long seq = lock.awaitAvailability(); |
681 |
> |
Object[] items = array; |
682 |
> |
int n = count; |
683 |
> |
if (n <= items.length) { |
684 |
> |
for (int i = n - 1; i >= 0; --i) { |
685 |
> |
Object e = items[i]; |
686 |
> |
if (lock.getSequence() != seq) { |
687 |
> |
lock.lock(); |
688 |
> |
try { |
689 |
> |
return rawLastIndexOf(o, 0, count); |
690 |
> |
} finally { |
691 |
> |
lock.unlock(); |
692 |
> |
} |
693 |
> |
} |
694 |
> |
else if ((o == null) ? e == null : o.equals(e)) |
695 |
> |
return i; |
696 |
> |
} |
697 |
> |
return -1; |
698 |
> |
} |
699 |
|
} |
700 |
|
} |
701 |
|
|
702 |
|
public ListIterator<E> listIterator() { |
703 |
< |
return new Itr(this, 0); |
703 |
> |
return new Itr<E>(this, 0); |
704 |
|
} |
705 |
|
|
706 |
|
public ListIterator<E> listIterator(int index) { |
707 |
< |
return new Itr(this, index); |
707 |
> |
return new Itr<E>(this, index); |
708 |
|
} |
709 |
|
|
710 |
|
public E remove(int index) { |
711 |
< |
SequenceLock lock = this.lock; |
688 |
< |
E oldValue; |
711 |
> |
final SequenceLock lock = this.lock; |
712 |
|
lock.lock(); |
713 |
|
try { |
714 |
|
if (index < 0 || index >= count) |
715 |
|
throw new ArrayIndexOutOfBoundsException(index); |
716 |
< |
oldValue = (E)array[index]; |
717 |
< |
internalRemoveAt(index); |
716 |
> |
@SuppressWarnings("unchecked") |
717 |
> |
E oldValue = (E) array[index]; |
718 |
> |
rawRemoveAt(index); |
719 |
> |
return oldValue; |
720 |
|
} finally { |
721 |
|
lock.unlock(); |
722 |
|
} |
698 |
– |
return oldValue; |
723 |
|
} |
724 |
|
|
725 |
|
public boolean remove(Object o) { |
726 |
< |
SequenceLock lock = this.lock; |
703 |
< |
boolean removed; |
726 |
> |
final SequenceLock lock = this.lock; |
727 |
|
lock.lock(); |
728 |
|
try { |
729 |
< |
removed = internalRemoveAt(internalIndexOf(o, array, 0, count)); |
729 |
> |
return rawRemoveAt(rawIndexOf(o, 0, count)); |
730 |
|
} finally { |
731 |
|
lock.unlock(); |
732 |
|
} |
710 |
– |
return removed; |
733 |
|
} |
734 |
|
|
735 |
|
public boolean removeAll(Collection<?> c) { |
741 |
|
} |
742 |
|
|
743 |
|
public E set(int index, E element) { |
744 |
< |
E oldValue; |
723 |
< |
SequenceLock lock = this.lock; |
744 |
> |
final SequenceLock lock = this.lock; |
745 |
|
lock.lock(); |
746 |
|
try { |
747 |
+ |
Object[] items = array; |
748 |
|
if (index < 0 || index >= count) |
749 |
|
throw new ArrayIndexOutOfBoundsException(index); |
750 |
< |
oldValue = (E)array[index]; |
751 |
< |
array[index] = element; |
750 |
> |
@SuppressWarnings("unchecked") |
751 |
> |
E oldValue = (E) items[index]; |
752 |
> |
items[index] = element; |
753 |
> |
return oldValue; |
754 |
|
} finally { |
755 |
|
lock.unlock(); |
756 |
|
} |
733 |
– |
return oldValue; |
757 |
|
} |
758 |
|
|
759 |
|
public int size() { |
737 |
– |
long ignore = lock.getSequence(); |
760 |
|
return count; |
761 |
|
} |
762 |
|
|
765 |
|
int ssize = toIndex - fromIndex; |
766 |
|
if (fromIndex < 0 || toIndex > c || ssize < 0) |
767 |
|
throw new IndexOutOfBoundsException(); |
768 |
< |
return new ReadMostlyVectorSublist(this, fromIndex, ssize); |
768 |
> |
return new ReadMostlyVectorSublist<E>(this, fromIndex, ssize); |
769 |
|
} |
770 |
|
|
771 |
|
public Object[] toArray() { |
786 |
|
* Append the element if not present. |
787 |
|
* |
788 |
|
* @param e element to be added to this list, if absent |
789 |
< |
* @return <tt>true</tt> if the element was added |
789 |
> |
* @return {@code true} if the element was added |
790 |
|
*/ |
791 |
|
public boolean addIfAbsent(E e) { |
792 |
< |
boolean added; |
771 |
< |
SequenceLock lock = this.lock; |
792 |
> |
final SequenceLock lock = this.lock; |
793 |
|
lock.lock(); |
794 |
|
try { |
795 |
< |
if (internalIndexOf(e, array, 0, count) < 0) { |
796 |
< |
internalAdd(e); |
797 |
< |
added = true; |
795 |
> |
if (rawIndexOf(e, 0, count) < 0) { |
796 |
> |
rawAdd(e); |
797 |
> |
return true; |
798 |
|
} |
799 |
|
else |
800 |
< |
added = false; |
800 |
> |
return false; |
801 |
|
} finally { |
802 |
|
lock.unlock(); |
803 |
|
} |
783 |
– |
return added; |
804 |
|
} |
805 |
|
|
806 |
|
/** |
822 |
|
lock.lock(); |
823 |
|
try { |
824 |
|
for (int i = 0; i < clen; ++i) { |
825 |
< |
Object e = cs[i]; |
826 |
< |
if (internalIndexOf(e, array, 0, count) < 0) { |
827 |
< |
internalAdd((E)e); |
825 |
> |
@SuppressWarnings("unchecked") |
826 |
> |
E e = (E) cs[i]; |
827 |
> |
if (rawIndexOf(e, 0, count) < 0) { |
828 |
> |
rawAdd(e); |
829 |
|
++added; |
830 |
|
} |
831 |
|
} |
836 |
|
return added; |
837 |
|
} |
838 |
|
|
839 |
+ |
/** |
840 |
+ |
* Returns an iterator operating over a snapshot copy of the |
841 |
+ |
* elements of this collection created upon construction of the |
842 |
+ |
* iterator. The iterator does <em>NOT</em> support the |
843 |
+ |
* {@code remove} method. |
844 |
+ |
* |
845 |
+ |
* @return an iterator over the elements in this list in proper sequence |
846 |
+ |
*/ |
847 |
+ |
public Iterator<E> snapshotIterator() { |
848 |
+ |
return new SnapshotIterator<E>(this); |
849 |
+ |
} |
850 |
+ |
|
851 |
+ |
static final class SnapshotIterator<E> implements Iterator<E> { |
852 |
+ |
private final Object[] items; |
853 |
+ |
private int cursor; |
854 |
+ |
SnapshotIterator(ReadMostlyVector<E> v) { items = v.toArray(); } |
855 |
+ |
public boolean hasNext() { return cursor < items.length; } |
856 |
+ |
@SuppressWarnings("unchecked") |
857 |
+ |
public E next() { |
858 |
+ |
if (cursor < items.length) |
859 |
+ |
return (E) items[cursor++]; |
860 |
+ |
throw new NoSuchElementException(); |
861 |
+ |
} |
862 |
+ |
public void remove() { throw new UnsupportedOperationException() ; } |
863 |
+ |
} |
864 |
+ |
|
865 |
|
// Vector-only methods |
866 |
|
|
867 |
|
/** See {@link Vector#firstElement} */ |
868 |
|
public E firstElement() { |
869 |
< |
SequenceLock lock = this.lock; |
869 |
> |
final SequenceLock lock = this.lock; |
870 |
|
for (;;) { |
871 |
|
long seq = lock.awaitAvailability(); |
872 |
|
Object[] items = array; |
873 |
< |
int len = items.length; |
874 |
< |
int c = count; |
875 |
< |
if (c > len || c < 0) |
829 |
< |
continue; |
830 |
< |
E e; boolean ex; |
831 |
< |
if (c == 0) { |
832 |
< |
e = null; |
833 |
< |
ex = true; |
834 |
< |
} |
835 |
< |
else { |
836 |
< |
e = (E)items[0]; |
837 |
< |
ex = false; |
838 |
< |
} |
873 |
> |
int n = count; |
874 |
> |
@SuppressWarnings("unchecked") |
875 |
> |
E e = (items.length > 0) ? (E) items[0] : null; |
876 |
|
if (lock.getSequence() == seq) { |
877 |
< |
if (ex) |
877 |
> |
if (n <= 0) |
878 |
|
throw new NoSuchElementException(); |
879 |
< |
else |
843 |
< |
return e; |
879 |
> |
return e; |
880 |
|
} |
881 |
|
} |
882 |
|
} |
883 |
|
|
884 |
|
/** See {@link Vector#lastElement} */ |
885 |
|
public E lastElement() { |
886 |
< |
SequenceLock lock = this.lock; |
886 |
> |
final SequenceLock lock = this.lock; |
887 |
|
for (;;) { |
888 |
|
long seq = lock.awaitAvailability(); |
889 |
|
Object[] items = array; |
890 |
< |
int len = items.length; |
891 |
< |
int c = count; |
892 |
< |
if (c > len || c < 0) |
857 |
< |
continue; |
858 |
< |
E e; boolean ex; |
859 |
< |
if (c == 0) { |
860 |
< |
e = null; |
861 |
< |
ex = true; |
862 |
< |
} |
863 |
< |
else { |
864 |
< |
e = (E)items[c - 1]; |
865 |
< |
ex = false; |
866 |
< |
} |
890 |
> |
int n = count; |
891 |
> |
@SuppressWarnings("unchecked") |
892 |
> |
E e = (n > 0 && items.length >= n) ? (E) items[n - 1] : null; |
893 |
|
if (lock.getSequence() == seq) { |
894 |
< |
if (ex) |
894 |
> |
if (n <= 0) |
895 |
|
throw new NoSuchElementException(); |
896 |
< |
else |
871 |
< |
return e; |
896 |
> |
return e; |
897 |
|
} |
898 |
|
} |
899 |
|
} |
900 |
|
|
901 |
|
/** See {@link Vector#indexOf(Object, int)} */ |
902 |
|
public int indexOf(Object o, int index) { |
903 |
< |
SequenceLock lock = this.lock; |
903 |
> |
final SequenceLock lock = this.lock; |
904 |
|
int idx = 0; |
905 |
|
boolean ex = false; |
906 |
|
long seq = lock.awaitAvailability(); |
907 |
|
Object[] items = array; |
908 |
< |
int c = count; |
908 |
> |
int n = count; |
909 |
|
boolean retry = false; |
910 |
< |
if (c > items.length) |
910 |
> |
if (n > items.length) |
911 |
|
retry = true; |
912 |
|
else if (index < 0) |
913 |
|
ex = true; |
914 |
|
else |
915 |
< |
idx = internalIndexOf(o, items, index, c); |
915 |
> |
idx = validatedIndexOf(o, items, index, n, seq); |
916 |
|
if (retry || lock.getSequence() != seq) { |
917 |
|
lock.lock(); |
918 |
|
try { |
919 |
|
if (index < 0) |
920 |
|
ex = true; |
921 |
|
else |
922 |
< |
idx = internalIndexOf(o, array, 0, count); |
922 |
> |
idx = rawIndexOf(o, index, count); |
923 |
|
} finally { |
924 |
|
lock.unlock(); |
925 |
|
} |
931 |
|
|
932 |
|
/** See {@link Vector#lastIndexOf(Object, int)} */ |
933 |
|
public int lastIndexOf(Object o, int index) { |
934 |
< |
SequenceLock lock = this.lock; |
934 |
> |
final SequenceLock lock = this.lock; |
935 |
|
int idx = 0; |
936 |
|
boolean ex = false; |
937 |
|
long seq = lock.awaitAvailability(); |
938 |
|
Object[] items = array; |
939 |
< |
int c = count; |
939 |
> |
int n = count; |
940 |
|
boolean retry = false; |
941 |
< |
if (c > items.length) |
941 |
> |
if (n > items.length) |
942 |
|
retry = true; |
943 |
< |
else if (index >= c) |
943 |
> |
else if (index >= n) |
944 |
|
ex = true; |
945 |
|
else |
946 |
< |
idx = internalLastIndexOf(o, items, index, 0); |
946 |
> |
idx = validatedLastIndexOf(o, items, index, 0, seq); |
947 |
|
if (retry || lock.getSequence() != seq) { |
948 |
|
lock.lock(); |
949 |
|
try { |
950 |
|
if (index >= count) |
951 |
|
ex = true; |
952 |
|
else |
953 |
< |
idx = internalLastIndexOf(o, array, index, 0); |
953 |
> |
idx = rawLastIndexOf(o, index, 0); |
954 |
|
} finally { |
955 |
|
lock.unlock(); |
956 |
|
} |
964 |
|
public void setSize(int newSize) { |
965 |
|
if (newSize < 0) |
966 |
|
throw new ArrayIndexOutOfBoundsException(newSize); |
967 |
< |
SequenceLock lock = this.lock; |
967 |
> |
final SequenceLock lock = this.lock; |
968 |
|
lock.lock(); |
969 |
|
try { |
970 |
< |
int c = count; |
971 |
< |
if (newSize > c) |
970 |
> |
int n = count; |
971 |
> |
if (newSize > n) |
972 |
|
grow(newSize); |
973 |
|
else { |
974 |
< |
for (int i = newSize ; i < c ; i++) |
975 |
< |
array[i] = null; |
974 |
> |
Object[] items = array; |
975 |
> |
for (int i = newSize ; i < n ; i++) |
976 |
> |
items[i] = null; |
977 |
|
} |
978 |
|
count = newSize; |
979 |
|
} finally { |
983 |
|
|
984 |
|
/** See {@link Vector#copyInto} */ |
985 |
|
public void copyInto(Object[] anArray) { |
986 |
< |
SequenceLock lock = this.lock; |
986 |
> |
final SequenceLock lock = this.lock; |
987 |
|
lock.lock(); |
988 |
|
try { |
989 |
|
System.arraycopy(array, 0, anArray, 0, count); |
994 |
|
|
995 |
|
/** See {@link Vector#trimToSize} */ |
996 |
|
public void trimToSize() { |
997 |
< |
SequenceLock lock = this.lock; |
997 |
> |
final SequenceLock lock = this.lock; |
998 |
|
lock.lock(); |
999 |
|
try { |
1000 |
< |
if (count < array.length) |
1001 |
< |
array = Arrays.copyOf(array, count); |
1000 |
> |
Object[] items = array; |
1001 |
> |
int n = count; |
1002 |
> |
if (n < items.length) |
1003 |
> |
array = Arrays.copyOf(items, n); |
1004 |
|
} finally { |
1005 |
|
lock.unlock(); |
1006 |
|
} |
1009 |
|
/** See {@link Vector#ensureCapacity} */ |
1010 |
|
public void ensureCapacity(int minCapacity) { |
1011 |
|
if (minCapacity > 0) { |
1012 |
< |
SequenceLock lock = this.lock; |
1012 |
> |
final SequenceLock lock = this.lock; |
1013 |
|
lock.lock(); |
1014 |
|
try { |
1015 |
|
if (minCapacity - array.length > 0) |
1022 |
|
|
1023 |
|
/** See {@link Vector#elements} */ |
1024 |
|
public Enumeration<E> elements() { |
1025 |
< |
return new Itr(this, 0); |
1025 |
> |
return new Itr<E>(this, 0); |
1026 |
|
} |
1027 |
|
|
1028 |
|
/** See {@link Vector#capacity} */ |
1029 |
|
public int capacity() { |
1002 |
– |
long ignore = lock.getSequence(); |
1030 |
|
return array.length; |
1031 |
|
} |
1032 |
|
|
1067 |
|
|
1068 |
|
// other methods |
1069 |
|
|
1070 |
< |
public Object clone() { |
1071 |
< |
SequenceLock lock = this.lock; |
1070 |
> |
public ReadMostlyVector<E> clone() { |
1071 |
> |
final SequenceLock lock = this.lock; |
1072 |
|
Object[] a = null; |
1046 |
– |
int c; |
1073 |
|
boolean retry = false; |
1074 |
|
long seq = lock.awaitAvailability(); |
1075 |
|
Object[] items = array; |
1076 |
< |
c = count; |
1077 |
< |
if (c <= items.length) |
1078 |
< |
a = Arrays.copyOf(items, c); |
1076 |
> |
int n = count; |
1077 |
> |
if (n <= items.length) |
1078 |
> |
a = Arrays.copyOf(items, n); |
1079 |
|
else |
1080 |
|
retry = true; |
1081 |
|
if (retry || lock.getSequence() != seq) { |
1082 |
|
lock.lock(); |
1083 |
|
try { |
1084 |
< |
c = count; |
1085 |
< |
a = Arrays.copyOf(array, c); |
1084 |
> |
n = count; |
1085 |
> |
a = Arrays.copyOf(array, n); |
1086 |
|
} finally { |
1087 |
|
lock.unlock(); |
1088 |
|
} |
1089 |
|
} |
1090 |
< |
return new ReadMostlyVector(a, c, capacityIncrement); |
1090 |
> |
return new ReadMostlyVector<E>(a, n, capacityIncrement); |
1091 |
|
} |
1092 |
|
|
1093 |
|
private void writeObject(java.io.ObjectOutputStream s) |
1094 |
|
throws java.io.IOException { |
1095 |
< |
SequenceLock lock = this.lock; |
1095 |
> |
final SequenceLock lock = this.lock; |
1096 |
|
lock.lock(); |
1097 |
|
try { |
1098 |
|
s.defaultWriteObject(); |
1101 |
|
} |
1102 |
|
} |
1103 |
|
|
1104 |
< |
static final class Itr<E> implements ListIterator<E>, Enumeration<E> { |
1104 |
> |
static final class Itr<E> implements ListIterator<E>, Enumeration<E> { |
1105 |
|
final ReadMostlyVector<E> list; |
1106 |
|
final SequenceLock lock; |
1107 |
|
Object[] items; |
1108 |
< |
Object next, prev; |
1108 |
> |
E next, prev; |
1109 |
|
long seq; |
1110 |
|
int cursor; |
1111 |
|
int fence; |
1112 |
|
int lastRet; |
1113 |
< |
boolean haveNext, havePrev; |
1113 |
> |
boolean validNext, validPrev; |
1114 |
|
|
1115 |
|
Itr(ReadMostlyVector<E> list, int index) { |
1116 |
|
this.list = list; |
1123 |
|
} |
1124 |
|
|
1125 |
|
private void refresh() { |
1126 |
+ |
validNext = validPrev = false; |
1127 |
|
do { |
1128 |
|
seq = lock.awaitAvailability(); |
1129 |
|
items = list.array; |
1130 |
< |
fence = list.count; |
1131 |
< |
} while (lock.getSequence() != seq); |
1130 |
> |
} while ((fence = list.count) > items.length || |
1131 |
> |
lock.getSequence() != seq); |
1132 |
|
} |
1133 |
|
|
1134 |
+ |
@SuppressWarnings("unchecked") |
1135 |
|
public boolean hasNext() { |
1136 |
+ |
boolean valid; |
1137 |
|
int i = cursor; |
1138 |
< |
while (i < fence && i >= 0) { |
1138 |
> |
for (;;) { |
1139 |
> |
if (i >= fence || i < 0 || i >= items.length) { |
1140 |
> |
valid = false; |
1141 |
> |
break; |
1142 |
> |
} |
1143 |
> |
next = (E) items[i]; |
1144 |
|
if (lock.getSequence() == seq) { |
1145 |
< |
next = items[i]; |
1146 |
< |
return haveNext = true; |
1145 |
> |
valid = true; |
1146 |
> |
break; |
1147 |
|
} |
1148 |
|
refresh(); |
1149 |
|
} |
1150 |
< |
return false; |
1150 |
> |
return validNext = valid; |
1151 |
|
} |
1152 |
|
|
1153 |
+ |
@SuppressWarnings("unchecked") |
1154 |
|
public boolean hasPrevious() { |
1155 |
< |
int i = cursor; |
1156 |
< |
while (i <= fence && i > 0) { |
1155 |
> |
boolean valid; |
1156 |
> |
int i = cursor - 1; |
1157 |
> |
for (;;) { |
1158 |
> |
if (i >= fence || i < 0 || i >= items.length) { |
1159 |
> |
valid = false; |
1160 |
> |
break; |
1161 |
> |
} |
1162 |
> |
prev = (E) items[i]; |
1163 |
|
if (lock.getSequence() == seq) { |
1164 |
< |
prev = items[i - 1]; |
1165 |
< |
return havePrev = true; |
1164 |
> |
valid = true; |
1165 |
> |
break; |
1166 |
|
} |
1167 |
|
refresh(); |
1168 |
|
} |
1169 |
< |
return false; |
1169 |
> |
return validPrev = valid; |
1170 |
|
} |
1171 |
|
|
1172 |
|
public E next() { |
1173 |
< |
if (!haveNext && !hasNext()) |
1174 |
< |
throw new NoSuchElementException(); |
1175 |
< |
haveNext = false; |
1176 |
< |
lastRet = cursor++; |
1177 |
< |
return (E) next; |
1173 |
> |
if (validNext || hasNext()) { |
1174 |
> |
validNext = false; |
1175 |
> |
lastRet = cursor++; |
1176 |
> |
return next; |
1177 |
> |
} |
1178 |
> |
throw new NoSuchElementException(); |
1179 |
|
} |
1180 |
|
|
1181 |
|
public E previous() { |
1182 |
< |
if (!havePrev && !hasPrevious()) |
1183 |
< |
throw new NoSuchElementException(); |
1184 |
< |
havePrev = false; |
1185 |
< |
lastRet = cursor--; |
1186 |
< |
return (E) prev; |
1182 |
> |
if (validPrev || hasPrevious()) { |
1183 |
> |
validPrev = false; |
1184 |
> |
lastRet = cursor--; |
1185 |
> |
return prev; |
1186 |
> |
} |
1187 |
> |
throw new NoSuchElementException(); |
1188 |
|
} |
1189 |
|
|
1190 |
|
public void remove() { |
1239 |
|
public int previousIndex() { return cursor - 1; } |
1240 |
|
} |
1241 |
|
|
1242 |
< |
static final class ReadMostlyVectorSublist<E> implements List<E>, RandomAccess, java.io.Serializable { |
1242 |
> |
static final class ReadMostlyVectorSublist<E> |
1243 |
> |
implements List<E>, RandomAccess, java.io.Serializable { |
1244 |
> |
static final long serialVersionUID = 3041673470172026059L; |
1245 |
> |
|
1246 |
|
final ReadMostlyVector<E> list; |
1247 |
|
final int offset; |
1248 |
|
volatile int size; |
1249 |
|
|
1250 |
< |
ReadMostlyVectorSublist(ReadMostlyVector<E> list, int offset, int size) { |
1250 |
> |
ReadMostlyVectorSublist(ReadMostlyVector<E> list, |
1251 |
> |
int offset, int size) { |
1252 |
|
this.list = list; |
1253 |
|
this.offset = offset; |
1254 |
|
this.size = size; |
1260 |
|
} |
1261 |
|
|
1262 |
|
public boolean add(E element) { |
1263 |
< |
SequenceLock lock = list.lock; |
1263 |
> |
final SequenceLock lock = list.lock; |
1264 |
|
lock.lock(); |
1265 |
|
try { |
1266 |
|
int c = size; |
1267 |
< |
list.internalAddAt(c + offset, element); |
1267 |
> |
list.rawAddAt(c + offset, element); |
1268 |
|
size = c + 1; |
1269 |
|
} finally { |
1270 |
|
lock.unlock(); |
1273 |
|
} |
1274 |
|
|
1275 |
|
public void add(int index, E element) { |
1276 |
< |
SequenceLock lock = list.lock; |
1276 |
> |
final SequenceLock lock = list.lock; |
1277 |
|
lock.lock(); |
1278 |
|
try { |
1279 |
|
if (index < 0 || index > size) |
1280 |
|
throw new ArrayIndexOutOfBoundsException(index); |
1281 |
< |
list.internalAddAt(index + offset, element); |
1281 |
> |
list.rawAddAt(index + offset, element); |
1282 |
|
++size; |
1283 |
|
} finally { |
1284 |
|
lock.unlock(); |
1287 |
|
|
1288 |
|
public boolean addAll(Collection<? extends E> c) { |
1289 |
|
Object[] elements = c.toArray(); |
1290 |
< |
int added; |
1244 |
< |
SequenceLock lock = list.lock; |
1290 |
> |
final SequenceLock lock = list.lock; |
1291 |
|
lock.lock(); |
1292 |
|
try { |
1293 |
|
int s = size; |
1294 |
|
int pc = list.count; |
1295 |
< |
list.internalAddAllAt(offset + s, elements); |
1296 |
< |
added = list.count - pc; |
1295 |
> |
list.rawAddAllAt(offset + s, elements); |
1296 |
> |
int added = list.count - pc; |
1297 |
|
size = s + added; |
1298 |
+ |
return added != 0; |
1299 |
|
} finally { |
1300 |
|
lock.unlock(); |
1301 |
|
} |
1255 |
– |
return added != 0; |
1302 |
|
} |
1303 |
|
|
1304 |
|
public boolean addAll(int index, Collection<? extends E> c) { |
1305 |
|
Object[] elements = c.toArray(); |
1306 |
< |
int added; |
1261 |
< |
SequenceLock lock = list.lock; |
1306 |
> |
final SequenceLock lock = list.lock; |
1307 |
|
lock.lock(); |
1308 |
|
try { |
1309 |
|
int s = size; |
1310 |
|
if (index < 0 || index > s) |
1311 |
|
throw new ArrayIndexOutOfBoundsException(index); |
1312 |
|
int pc = list.count; |
1313 |
< |
list.internalAddAllAt(index + offset, elements); |
1314 |
< |
added = list.count - pc; |
1313 |
> |
list.rawAddAllAt(index + offset, elements); |
1314 |
> |
int added = list.count - pc; |
1315 |
|
size = s + added; |
1316 |
+ |
return added != 0; |
1317 |
|
} finally { |
1318 |
|
lock.unlock(); |
1319 |
|
} |
1274 |
– |
return added != 0; |
1320 |
|
} |
1321 |
|
|
1322 |
|
public void clear() { |
1323 |
< |
SequenceLock lock = list.lock; |
1323 |
> |
final SequenceLock lock = list.lock; |
1324 |
|
lock.lock(); |
1325 |
|
try { |
1326 |
< |
list.internalClear(offset, size); |
1326 |
> |
list.internalClear(offset, offset + size); |
1327 |
|
size = 0; |
1328 |
|
} finally { |
1329 |
|
lock.unlock(); |
1335 |
|
} |
1336 |
|
|
1337 |
|
public boolean containsAll(Collection<?> c) { |
1338 |
< |
return list.internalContainsAll(c, offset, size); |
1338 |
> |
return list.internalContainsAll(c, offset, offset + size); |
1339 |
|
} |
1340 |
|
|
1341 |
|
public boolean equals(Object o) { |
1343 |
|
return true; |
1344 |
|
if (!(o instanceof List)) |
1345 |
|
return false; |
1346 |
< |
return list.internalEquals((List<?>)(o), offset, size); |
1346 |
> |
return list.internalEquals((List<?>)(o), offset, offset + size); |
1347 |
|
} |
1348 |
|
|
1349 |
|
public E get(int index) { |
1353 |
|
} |
1354 |
|
|
1355 |
|
public int hashCode() { |
1356 |
< |
return list.internalHashCode(offset, size); |
1356 |
> |
return list.internalHashCode(offset, offset + size); |
1357 |
|
} |
1358 |
|
|
1359 |
|
public int indexOf(Object o) { |
1360 |
< |
SequenceLock lock = list.lock; |
1360 |
> |
final SequenceLock lock = list.lock; |
1361 |
|
long seq = lock.awaitAvailability(); |
1362 |
|
Object[] items = list.array; |
1363 |
|
int c = list.count; |
1364 |
|
if (c <= items.length) { |
1365 |
< |
int idx = internalIndexOf(o, items, offset, offset+size); |
1365 |
> |
int idx = list.validatedIndexOf(o, items, offset, |
1366 |
> |
offset + size, seq); |
1367 |
|
if (lock.getSequence() == seq) |
1368 |
|
return idx < 0 ? -1 : idx - offset; |
1369 |
|
} |
1370 |
|
lock.lock(); |
1371 |
|
try { |
1372 |
< |
int idx = internalIndexOf(o, list.array, offset, offset+size); |
1372 |
> |
int idx = list.rawIndexOf(o, offset, offset + size); |
1373 |
|
return idx < 0 ? -1 : idx - offset; |
1374 |
|
} finally { |
1375 |
|
lock.unlock(); |
1381 |
|
} |
1382 |
|
|
1383 |
|
public Iterator<E> iterator() { |
1384 |
< |
return new SubItr(this, offset); |
1384 |
> |
return new SubItr<E>(this, offset); |
1385 |
|
} |
1386 |
|
|
1387 |
|
public int lastIndexOf(Object o) { |
1388 |
< |
SequenceLock lock = list.lock; |
1388 |
> |
final SequenceLock lock = list.lock; |
1389 |
|
long seq = lock.awaitAvailability(); |
1390 |
|
Object[] items = list.array; |
1391 |
|
int c = list.count; |
1392 |
|
if (c <= items.length) { |
1393 |
< |
int idx = internalLastIndexOf(o, items, offset+size-1, offset); |
1393 |
> |
int idx = list.validatedLastIndexOf(o, items, offset+size-1, |
1394 |
> |
offset, seq); |
1395 |
|
if (lock.getSequence() == seq) |
1396 |
|
return idx < 0 ? -1 : idx - offset; |
1397 |
|
} |
1398 |
|
lock.lock(); |
1399 |
|
try { |
1400 |
< |
int idx = internalLastIndexOf(o, list.array, offset+size-1, |
1354 |
< |
offset); |
1400 |
> |
int idx = list.rawLastIndexOf(o, offset + size - 1, offset); |
1401 |
|
return idx < 0 ? -1 : idx - offset; |
1402 |
|
} finally { |
1403 |
|
lock.unlock(); |
1405 |
|
} |
1406 |
|
|
1407 |
|
public ListIterator<E> listIterator() { |
1408 |
< |
return new SubItr(this, offset); |
1408 |
> |
return new SubItr<E>(this, offset); |
1409 |
|
} |
1410 |
|
|
1411 |
|
public ListIterator<E> listIterator(int index) { |
1412 |
< |
return new SubItr(this, index + offset); |
1412 |
> |
return new SubItr<E>(this, index + offset); |
1413 |
|
} |
1414 |
|
|
1415 |
|
public E remove(int index) { |
1416 |
< |
E result; |
1371 |
< |
SequenceLock lock = list.lock; |
1416 |
> |
final SequenceLock lock = list.lock; |
1417 |
|
lock.lock(); |
1418 |
|
try { |
1419 |
< |
if (index < 0 || index >= size) |
1375 |
< |
throw new ArrayIndexOutOfBoundsException(index); |
1419 |
> |
Object[] items = list.array; |
1420 |
|
int i = index + offset; |
1421 |
< |
result = (E)list.array[i]; |
1422 |
< |
list.internalRemoveAt(i); |
1421 |
> |
if (index < 0 || index >= size || i >= items.length) |
1422 |
> |
throw new ArrayIndexOutOfBoundsException(index); |
1423 |
> |
@SuppressWarnings("unchecked") |
1424 |
> |
E result = (E) items[i]; |
1425 |
> |
list.rawRemoveAt(i); |
1426 |
|
size--; |
1427 |
+ |
return result; |
1428 |
|
} finally { |
1429 |
|
lock.unlock(); |
1430 |
|
} |
1383 |
– |
return result; |
1431 |
|
} |
1432 |
|
|
1433 |
|
public boolean remove(Object o) { |
1434 |
< |
boolean removed = false; |
1388 |
< |
SequenceLock lock = list.lock; |
1434 |
> |
final SequenceLock lock = list.lock; |
1435 |
|
lock.lock(); |
1436 |
|
try { |
1437 |
< |
if (list.internalRemoveAt(internalIndexOf(o, list.array, offset, |
1438 |
< |
offset+size))) { |
1393 |
< |
removed = true; |
1437 |
> |
if (list.rawRemoveAt(list.rawIndexOf(o, offset, |
1438 |
> |
offset + size))) { |
1439 |
|
--size; |
1440 |
+ |
return true; |
1441 |
|
} |
1442 |
+ |
else |
1443 |
+ |
return false; |
1444 |
|
} finally { |
1445 |
|
lock.unlock(); |
1446 |
|
} |
1399 |
– |
return removed; |
1447 |
|
} |
1448 |
|
|
1449 |
|
public boolean removeAll(Collection<?> c) { |
1450 |
< |
return list.internalRemoveAll(c, offset, size); |
1450 |
> |
return list.internalRemoveAll(c, offset, offset + size); |
1451 |
|
} |
1452 |
|
|
1453 |
|
public boolean retainAll(Collection<?> c) { |
1454 |
< |
return list.internalRetainAll(c, offset, size); |
1454 |
> |
return list.internalRetainAll(c, offset, offset + size); |
1455 |
|
} |
1456 |
|
|
1457 |
|
public E set(int index, E element) { |
1469 |
|
int ssize = toIndex - fromIndex; |
1470 |
|
if (fromIndex < 0 || toIndex > c || ssize < 0) |
1471 |
|
throw new IndexOutOfBoundsException(); |
1472 |
< |
return new ReadMostlyVectorSublist(list, offset+fromIndex, ssize); |
1472 |
> |
return new ReadMostlyVectorSublist<E>(list, offset+fromIndex, ssize); |
1473 |
|
} |
1474 |
|
|
1475 |
|
public Object[] toArray() { |
1476 |
< |
return list.internalToArray(offset, size); |
1476 |
> |
return list.internalToArray(offset, offset + size); |
1477 |
|
} |
1478 |
|
|
1479 |
|
public <T> T[] toArray(T[] a) { |
1480 |
< |
return list.internalToArray(a, offset, size); |
1480 |
> |
return list.internalToArray(a, offset, offset + size); |
1481 |
|
} |
1482 |
|
|
1483 |
|
public String toString() { |
1484 |
< |
return list.internalToString(offset, size); |
1484 |
> |
return list.internalToString(offset, offset + size); |
1485 |
|
} |
1486 |
|
|
1487 |
|
} |
1491 |
|
final ReadMostlyVector<E> list; |
1492 |
|
final SequenceLock lock; |
1493 |
|
Object[] items; |
1494 |
< |
Object next, prev; |
1494 |
> |
E next, prev; |
1495 |
|
long seq; |
1496 |
|
int cursor; |
1497 |
|
int fence; |
1498 |
|
int lastRet; |
1499 |
< |
boolean haveNext, havePrev; |
1499 |
> |
boolean validNext, validPrev; |
1500 |
|
|
1501 |
|
SubItr(ReadMostlyVectorSublist<E> sublist, int index) { |
1502 |
|
this.sublist = sublist; |
1510 |
|
} |
1511 |
|
|
1512 |
|
private void refresh() { |
1513 |
+ |
validNext = validPrev = false; |
1514 |
|
do { |
1515 |
+ |
int n; |
1516 |
|
seq = lock.awaitAvailability(); |
1517 |
|
items = list.array; |
1518 |
< |
int c = list.count; |
1518 |
> |
if ((n = list.count) > items.length) |
1519 |
> |
continue; |
1520 |
|
int b = sublist.offset + sublist.size; |
1521 |
< |
fence = b < c ? b : c; |
1521 |
> |
fence = b < n ? b : n; |
1522 |
|
} while (lock.getSequence() != seq); |
1523 |
|
} |
1524 |
|
|
1525 |
+ |
@SuppressWarnings("unchecked") |
1526 |
|
public boolean hasNext() { |
1527 |
+ |
boolean valid; |
1528 |
|
int i = cursor; |
1529 |
< |
while (i < fence && i >= 0) { |
1529 |
> |
for (;;) { |
1530 |
> |
if (i >= fence || i < 0 || i >= items.length) { |
1531 |
> |
valid = false; |
1532 |
> |
break; |
1533 |
> |
} |
1534 |
> |
next = (E) items[i]; |
1535 |
|
if (lock.getSequence() == seq) { |
1536 |
< |
next = items[i]; |
1537 |
< |
return haveNext = true; |
1536 |
> |
valid = true; |
1537 |
> |
break; |
1538 |
|
} |
1539 |
|
refresh(); |
1540 |
|
} |
1541 |
< |
return false; |
1541 |
> |
return validNext = valid; |
1542 |
|
} |
1543 |
|
|
1544 |
+ |
@SuppressWarnings("unchecked") |
1545 |
|
public boolean hasPrevious() { |
1546 |
< |
int i = cursor; |
1547 |
< |
while (i <= fence && i > 0) { |
1546 |
> |
boolean valid; |
1547 |
> |
int i = cursor - 1; |
1548 |
> |
for (;;) { |
1549 |
> |
if (i >= fence || i < 0 || i >= items.length) { |
1550 |
> |
valid = false; |
1551 |
> |
break; |
1552 |
> |
} |
1553 |
> |
prev = (E) items[i]; |
1554 |
|
if (lock.getSequence() == seq) { |
1555 |
< |
prev = items[i - 1]; |
1556 |
< |
return havePrev = true; |
1555 |
> |
valid = true; |
1556 |
> |
break; |
1557 |
|
} |
1558 |
|
refresh(); |
1559 |
|
} |
1560 |
< |
return false; |
1560 |
> |
return validPrev = valid; |
1561 |
|
} |
1562 |
|
|
1563 |
|
public E next() { |
1564 |
< |
if (!haveNext && !hasNext()) |
1565 |
< |
throw new NoSuchElementException(); |
1566 |
< |
haveNext = false; |
1567 |
< |
lastRet = cursor++; |
1568 |
< |
return (E) next; |
1564 |
> |
if (validNext || hasNext()) { |
1565 |
> |
validNext = false; |
1566 |
> |
lastRet = cursor++; |
1567 |
> |
return next; |
1568 |
> |
} |
1569 |
> |
throw new NoSuchElementException(); |
1570 |
|
} |
1571 |
|
|
1572 |
|
public E previous() { |
1573 |
< |
if (!havePrev && !hasPrevious()) |
1574 |
< |
throw new NoSuchElementException(); |
1575 |
< |
havePrev = false; |
1576 |
< |
lastRet = cursor--; |
1577 |
< |
return (E) prev; |
1573 |
> |
if (validPrev || hasPrevious()) { |
1574 |
> |
validPrev = false; |
1575 |
> |
lastRet = cursor--; |
1576 |
> |
return prev; |
1577 |
> |
} |
1578 |
> |
throw new NoSuchElementException(); |
1579 |
|
} |
1580 |
|
|
1581 |
|
public int nextIndex() { |