--- jsr166/src/jsr166e/extra/ReadMostlyVector.java 2011/07/15 23:56:18 1.1
+++ jsr166/src/jsr166e/extra/ReadMostlyVector.java 2011/08/05 17:08:04 1.13
@@ -9,18 +9,20 @@ import jsr166e.*;
import java.util.*;
/**
- * A class with the same API and array-based characteristics as {@link
- * java.util.Vector} but with reduced contention and improved
+ * A class with the same methods and array-based characteristics as
+ * {@link java.util.Vector} but with reduced contention and improved
* throughput when invocations of read-only methods by multiple
- * threads are most common. Instances of this class may have
- * relatively poorer performance in other contexts.
+ * threads are most common.
*
*
The iterators returned by this class's {@link #iterator()
* iterator} and {@link #listIterator(int) listIterator} methods are
* best-effort in the presence of concurrent modifications, and do
* NOT throw {@link ConcurrentModificationException}. An
* iterator's {@code next()} method returns consecutive elements as
- * they appear in the underlying array upon each access.
+ * they appear in the underlying array upon each access. Alternatively,
+ * method {@link #snapshotIterator} may be used for deterministic
+ * traversals, at the expense of making a copy, and unavailability of
+ * method {@code Iterator.remove}.
*
*
Otherwise, this class supports all methods, under the same
* documented specifications, as {@code Vector}. Consult {@link
@@ -35,8 +37,17 @@ public class ReadMostlyVector impleme
/*
* This class exists mainly as a vehicle to exercise various
- * constructions using SequenceLocks, which are not yet explained
- * well here.
+ * constructions using SequenceLocks. Read-only methods
+ * take one of a few forms:
+ *
+ * Short methods,including get(index), continually retry obtaining
+ * a snapshot of array, count, and element, using sequence number
+ * to validate.
+ *
+ * Methods that are potentially O(n) (or worse) try once in
+ * read-only mode, and then lock. When in read-only mode, they
+ * validate only at the end of an array scan unless the element is
+ * actually used (for example, as an argument of method equals).
*/
/**
@@ -46,9 +57,9 @@ public class ReadMostlyVector impleme
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
// fields are non-private to simpify nested class access
- Object[] array;
+ volatile Object[] array;
final SequenceLock lock;
- int count;
+ volatile int count;
final int capacityIncrement;
/**
@@ -118,7 +129,7 @@ public class ReadMostlyVector impleme
}
// For explanation, see CopyOnWriteArrayList
- final void grow(int minCapacity) {
+ final Object[] grow(int minCapacity) {
int oldCapacity = array.length;
int newCapacity = oldCapacity + ((capacityIncrement > 0) ?
capacityIncrement : oldCapacity);
@@ -126,7 +137,7 @@ public class ReadMostlyVector impleme
newCapacity = minCapacity;
if (newCapacity - MAX_ARRAY_SIZE > 0)
newCapacity = hugeCapacity(minCapacity);
- array = Arrays.copyOf(array, newCapacity);
+ return array = Arrays.copyOf(array, newCapacity);
}
static int hugeCapacity(int minCapacity) {
@@ -143,94 +154,122 @@ public class ReadMostlyVector impleme
* as well as sublist and iterator classes.
*/
- static int internalIndexOf(Object o, Object[] items,
- int index, int fence) {
+ // Version of indexOf that returns -1 if either not present or invalid
+ final int validatedIndexOf(Object x, Object[] items, int index, int fence,
+ long seq) {
+ for (int i = index; i < fence; ++i) {
+ Object e = items[i];
+ if (lock.getSequence() != seq)
+ break;
+ if ((x == null) ? e == null : x.equals(e))
+ return i;
+ }
+ return -1;
+ }
+
+ final int rawIndexOf(Object x, int index, int fence) {
+ Object[] items = array;
for (int i = index; i < fence; ++i) {
- Object x = items[i];
- if (o == null? x == null : (x != null && o.equals(x)))
+ Object e = items[i];
+ if ((x == null) ? e == null : x.equals(e))
return i;
}
return -1;
}
- static int internalLastIndexOf(Object o, Object[] items,
- int index, int origin) {
+ final int validatedLastIndexOf(Object x, Object[] items,
+ int index, int origin, long seq) {
for (int i = index; i >= origin; --i) {
- Object x = items[i];
- if (o == null? x == null : (x != null && o.equals(x)))
+ Object e = items[i];
+ if (lock.getSequence() != seq)
+ break;
+ if ((x == null) ? e == null : x.equals(e))
return i;
}
return -1;
}
- final void internalAdd(E e) {
- int c = count;
- if (c >= array.length)
- grow(c + 1);
- array[c] = e;
- count = c + 1;
+ final int rawLastIndexOf(Object x, int index, int origin) {
+ Object[] items = array;
+ for (int i = index; i >= origin; --i) {
+ Object e = items[i];
+ if ((x == null) ? e == null : x.equals(e))
+ return i;
+ }
+ return -1;
+ }
+
+ final void rawAdd(Object e) {
+ int n = count;
+ Object[] items = array;
+ if (n >= items.length)
+ items = grow(n + 1);
+ items[n] = e;
+ count = n + 1;
}
- final void internalAddAt(int index, E e) {
- int c = count;
- if (index > c)
+ final void rawAddAt(int index, Object e) {
+ int n = count;
+ Object[] items = array;
+ if (index > n)
throw new ArrayIndexOutOfBoundsException(index);
- if (c >= array.length)
- grow(c + 1);
- System.arraycopy(array, index, array, index + 1, c - index);
- array[index] = e;
- count = c + 1;
+ if (n >= items.length)
+ items = grow(n + 1);
+ if (index < n)
+ System.arraycopy(items, index, items, index + 1, n - index);
+ items[index] = e;
+ count = n + 1;
}
- final boolean internalAddAllAt(int index, Object[] elements) {
- int c = count;
- if (index < 0 || index > c)
+ final boolean rawAddAllAt(int index, Object[] elements) {
+ int n = count;
+ Object[] items = array;
+ if (index < 0 || index > n)
throw new ArrayIndexOutOfBoundsException(index);
int len = elements.length;
if (len == 0)
return false;
- int newCount = c + len;
- if (newCount >= array.length)
- grow(newCount);
- int mv = count - index;
+ int newCount = n + len;
+ if (newCount >= items.length)
+ items = grow(newCount);
+ int mv = n - index;
if (mv > 0)
- System.arraycopy(array, index, array, index + len, mv);
- System.arraycopy(elements, 0, array, index, len);
+ System.arraycopy(items, index, items, index + len, mv);
+ System.arraycopy(elements, 0, items, index, len);
count = newCount;
return true;
}
- final boolean internalRemoveAt(int index) {
- int c = count - 1;
- if (index < 0 || index > c)
+ final boolean rawRemoveAt(int index) {
+ int n = count - 1;
+ Object[] items = array;
+ if (index < 0 || index > n)
return false;
- int mv = c - index;
+ int mv = n - index;
if (mv > 0)
- System.arraycopy(array, index + 1, array, index, mv);
- array[c] = null;
- count = c;
+ System.arraycopy(items, index + 1, items, index, mv);
+ items[n] = null;
+ count = n;
return true;
}
/**
* Internal version of removeAll for lists and sublists. In this
- * and other similar methods below, the span argument is, if
- * non-negative, the purported size of a list/sublist, or is left
- * negative if the size should be determined via count field under
- * lock.
+ * and other similar methods below, the bound argument is, if
+ * non-negative, the purported upper bound of a list/sublist, or
+ * is left negative if the bound should be determined via count
+ * field under lock.
*/
- final boolean internalRemoveAll(Collection> c, int origin, int span) {
+ final boolean internalRemoveAll(Collection> c, int origin, int bound) {
SequenceLock lock = this.lock;
boolean removed = false;
lock.lock();
try {
- int fence = count;
- if (span >= 0 && origin + span < fence)
- fence = origin + span;
+ int n = count;
+ int fence = bound < 0 || bound > n ? n : bound;
if (origin >= 0 && origin < fence) {
for (Object x : c) {
- while (internalRemoveAt(internalIndexOf(x, array,
- origin, fence)))
+ while (rawRemoveAt(rawIndexOf(x, origin, fence)))
removed = true;
}
}
@@ -240,27 +279,30 @@ public class ReadMostlyVector impleme
return removed;
}
- final boolean internalRetainAll(Collection> c, int origin, int span) {
+ final boolean internalRetainAll(Collection> c, int origin, int bound) {
SequenceLock lock = this.lock;
boolean removed = false;
if (c != this) {
lock.lock();
try {
+ Object[] items = array;
int i = origin;
- int fence = count;
- if (span >= 0 && origin + span < fence)
- fence = origin + span;
- while (i < fence) {
- if (c.contains(array[i]))
+ int n = count;
+ int fence = bound < 0 || bound > n ? n : bound;
+ while (i >= 0 && i < fence) {
+ if (c.contains(items[i]))
++i;
else {
--fence;
- int mv = --count - i;
+ int mv = --n - i;
if (mv > 0)
- System.arraycopy(array, i + 1, array, i, mv);
- removed = true;
+ System.arraycopy(items, i + 1, items, i, mv);
}
}
+ if (count != n) {
+ count = n;
+ removed = true;
+ }
} finally {
lock.unlock();
}
@@ -268,44 +310,45 @@ public class ReadMostlyVector impleme
return removed;
}
- final void internalClear(int origin, int span) {
- int c = count;
- int fence = c;
- if (span >= 0 && origin + span < fence)
- fence = origin + span;
+ final void internalClear(int origin, int bound) {
+ int n = count;
+ int fence = bound < 0 || bound > n ? n : bound;
if (origin >= 0 && origin < fence) {
+ Object[] items = array;
int removed = fence - origin;
- int newCount = c - removed;
- int mv = c - (origin + removed);
+ int newCount = n - removed;
+ int mv = n - (origin + removed);
if (mv > 0)
- System.arraycopy(array, origin + removed, array, origin, mv);
- for (int i = c; i < newCount; ++i)
- array[i] = null;
+ System.arraycopy(items, origin + removed, items, origin, mv);
+ for (int i = n; i < newCount; ++i)
+ items[i] = null;
count = newCount;
}
}
- final boolean internalContainsAll(Collection> coll, int origin, int span) {
+ final boolean internalContainsAll(Collection> c, int origin, int bound) {
SequenceLock lock = this.lock;
boolean contained;
boolean locked = false;
try {
for (;;) {
long seq = lock.awaitAvailability();
+ int n = count;
Object[] items = array;
int len = items.length;
- int c = count;
- if (c > len)
+ if (n > len)
continue;
- int fence = c;
- if (span >= 0 && origin + span < fence)
- fence = origin + span;
- if (origin < 0 || fence > c)
+ int fence = bound < 0 || bound > n ? n : bound;
+ if (origin < 0)
contained = false;
else {
contained = true;
- for (Object e : coll) {
- if (internalIndexOf(e, items, origin, fence) < 0) {
+ for (Object e : c) {
+ int idx = (locked ?
+ rawIndexOf(e, origin, fence) :
+ validatedIndexOf(e, items, origin,
+ fence, seq));
+ if (idx < 0) {
contained = false;
break;
}
@@ -323,34 +366,28 @@ public class ReadMostlyVector impleme
return contained;
}
- final boolean internalEquals(List> list, int origin, int span) {
+ final boolean internalEquals(List> list, int origin, int bound) {
SequenceLock lock = this.lock;
- boolean equal;
boolean locked = false;
+ boolean equal;
try {
for (;;) {
- equal = true;
long seq = lock.awaitAvailability();
Object[] items = array;
- int len = items.length;
- int c = count;
- if (c > len)
- continue;
- int fence = c;
- if (span >= 0 && origin + span < fence)
- fence = origin + span;
- if (origin < 0 || fence > c)
+ int n = count;
+ if (n > items.length || origin < 0)
equal = false;
else {
+ equal = true;
+ int fence = bound < 0 || bound > n ? n : bound;
Iterator> it = list.iterator();
for (int i = origin; i < fence; ++i) {
- if (!it.hasNext()) {
- equal = false;
- break;
- }
- Object x = it.next();
- Object y = items[i];
- if (x == null? y != null : (y == null || !x.equals(y))) {
+ Object x = items[i];
+ Object y;
+ if ((!locked && lock.getSequence() != seq) ||
+ !it.hasNext() ||
+ (y = it.next()) == null ?
+ x != null : !y.equals(x)) {
equal = false;
break;
}
@@ -370,7 +407,7 @@ public class ReadMostlyVector impleme
return equal;
}
- final int internalHashCode(int origin, int span) {
+ final int internalHashCode(int origin, int bound) {
SequenceLock lock = this.lock;
int hash;
boolean locked = false;
@@ -379,14 +416,12 @@ public class ReadMostlyVector impleme
hash = 1;
long seq = lock.awaitAvailability();
Object[] items = array;
+ int n = count;
int len = items.length;
- int c = count;
- if (c > len)
+ if (n > len)
continue;
- int fence = c;
- if (span >= 0 && origin + span < fence)
- fence = origin + span;
- if (origin >= 0 && fence <= c) {
+ int fence = bound < 0 || bound > n ? n : bound;
+ if (origin >= 0) {
for (int i = origin; i < fence; ++i) {
Object e = items[i];
hash = 31*hash + (e == null ? 0 : e.hashCode());
@@ -404,41 +439,42 @@ public class ReadMostlyVector impleme
return hash;
}
- final String internalToString(int origin, int span) {
+ final String internalToString(int origin, int bound) {
SequenceLock lock = this.lock;
String ret;
boolean locked = false;
try {
- for (;;) {
+ outer:for (;;) {
long seq = lock.awaitAvailability();
Object[] items = array;
+ int n = count;
int len = items.length;
- int c = count;
- if (c > len)
+ if (n > len)
continue;
- int fence = c;
- if (span >= 0 && origin + span < fence)
- fence = origin + span;
- if (origin >= 0 && fence <= c) {
- if (origin == fence)
- ret = "[]";
- else {
- StringBuilder sb = new StringBuilder();
- sb.append('[');
- for (int i = origin;;) {
- Object e = items[i];
- sb.append(e == this ? "(this Collection)" : e);
- if (++i < fence)
- sb.append(',').append(' ');
- else {
- ret = sb.append(']').toString();
- break;
- }
+ int fence = bound < 0 || bound > n ? n : bound;
+ if (origin < 0 || origin == fence)
+ ret = "[]";
+ else {
+ StringBuilder sb = new StringBuilder();
+ sb.append('[');
+ for (int i = origin;;) {
+ Object e = items[i];
+ if (e == this)
+ sb.append("(this Collection)");
+ else if (!locked && lock.getSequence() != seq)
+ continue outer;
+ else
+ sb.append(e.toString());
+ if (++i < fence)
+ sb.append(',').append(' ');
+ else {
+ ret = sb.append(']').toString();
+ break;
}
}
- if (lock.getSequence() == seq)
- break;
}
+ if (lock.getSequence() == seq)
+ break;
lock.lock();
locked = true;
}
@@ -449,7 +485,7 @@ public class ReadMostlyVector impleme
return ret;
}
- final Object[] internalToArray(int origin, int span) {
+ final Object[] internalToArray(int origin, int bound) {
Object[] result;
SequenceLock lock = this.lock;
boolean locked = false;
@@ -458,17 +494,16 @@ public class ReadMostlyVector impleme
result = null;
long seq = lock.awaitAvailability();
Object[] items = array;
+ int n = count;
int len = items.length;
- int c = count;
- int fence = c;
- if (span >= 0 && origin + span < fence)
- fence = origin + span;
- if (c <= len && fence <= len) {
+ if (n > len)
+ continue;
+ int fence = bound < 0 || bound > n ? n : bound;
+ if (origin >= 0)
result = Arrays.copyOfRange(items, origin, fence,
Object[].class);
- if (lock.getSequence() == seq)
- break;
- }
+ if (lock.getSequence() == seq)
+ break;
lock.lock();
locked = true;
}
@@ -479,7 +514,8 @@ public class ReadMostlyVector impleme
return result;
}
- final T[] internalToArray(T[] a, int origin, int span) {
+ final T[] internalToArray(T[] a, int origin, int bound) {
+ int alen = a.length;
T[] result;
SequenceLock lock = this.lock;
boolean locked = false;
@@ -487,25 +523,26 @@ public class ReadMostlyVector impleme
for (;;) {
long seq = lock.awaitAvailability();
Object[] items = array;
+ int n = count;
int len = items.length;
- int c = count;
- int fence = c;
- if (span >= 0 && origin + span < fence)
- fence = origin + span;
- if (c <= len && fence <= len) {
- if (a.length < count)
- result = (T[]) Arrays.copyOfRange(array, origin,
- fence, a.getClass());
- else {
- int n = fence - origin;
- System.arraycopy(array, 0, a, origin, fence - origin);
- if (a.length > n)
- a[n] = null;
- result = a;
- }
- if (lock.getSequence() == seq)
- break;
+ if (n > len)
+ continue;
+ int fence = bound < 0 || bound > n ? n : bound;
+ int rlen = fence - origin;
+ if (rlen < 0)
+ rlen = 0;
+ if (origin < 0 || alen >= rlen) {
+ if (rlen > 0)
+ System.arraycopy(items, 0, a, origin, rlen);
+ if (alen > rlen)
+ a[rlen] = null;
+ result = a;
}
+ else
+ result = (T[]) Arrays.copyOfRange(items, origin,
+ fence, a.getClass());
+ if (lock.getSequence() == seq)
+ break;
lock.lock();
locked = true;
}
@@ -522,7 +559,7 @@ public class ReadMostlyVector impleme
SequenceLock lock = this.lock;
lock.lock();
try {
- internalAdd(e);
+ rawAdd(e);
} finally {
lock.unlock();
}
@@ -533,7 +570,7 @@ public class ReadMostlyVector impleme
SequenceLock lock = this.lock;
lock.lock();
try {
- internalAddAt(index, element);
+ rawAddAt(index, element);
} finally {
lock.unlock();
}
@@ -547,10 +584,12 @@ public class ReadMostlyVector impleme
SequenceLock lock = this.lock;
lock.lock();
try {
- int newCount = count + len;
- if (newCount >= array.length)
- grow(newCount);
- System.arraycopy(elements, 0, array, count, len);
+ Object[] items = array;
+ int n = count;
+ int newCount = n + len;
+ if (newCount >= items.length)
+ items = grow(newCount);
+ System.arraycopy(elements, 0, items, n, len);
count = newCount;
} finally {
lock.unlock();
@@ -564,7 +603,7 @@ public class ReadMostlyVector impleme
Object[] elements = c.toArray();
lock.lock();
try {
- ret = internalAddAllAt(index, elements);
+ ret = rawAddAllAt(index, elements);
} finally {
lock.unlock();
}
@@ -575,8 +614,10 @@ public class ReadMostlyVector impleme
SequenceLock lock = this.lock;
lock.lock();
try {
- for (int i = 0; i < count; i++)
- array[i] = null;
+ int n = count;
+ Object[] items = array;
+ for (int i = 0; i < n; i++)
+ items[i] = null;
count = 0;
} finally {
lock.unlock();
@@ -596,32 +637,31 @@ public class ReadMostlyVector impleme
return true;
if (!(o instanceof List))
return false;
- return internalEquals((List>)(o), 0, -1);
+ return internalEquals((List>)o, 0, -1);
}
public E get(int index) {
SequenceLock lock = this.lock;
for (;;) {
long seq = lock.awaitAvailability();
+ int n = count;
Object[] items = array;
- int len = items.length;
- int c = count;
- if (c > len)
+ if (n > items.length)
continue;
- E e; boolean ex;
- if (index < 0 || index >= c) {
+ Object e; boolean ex;
+ if (index < 0 || index >= n) {
e = null;
ex = true;
}
else {
- e = (E)items[index];
+ e = items[index];
ex = false;
}
if (lock.getSequence() == seq) {
if (ex)
throw new ArrayIndexOutOfBoundsException(index);
else
- return e;
+ return (E)e;
}
}
}
@@ -632,70 +672,83 @@ public class ReadMostlyVector impleme
public int indexOf(Object o) {
SequenceLock lock = this.lock;
- long seq = lock.awaitAvailability();
- Object[] items = array;
- int c = count;
- if (c <= items.length) {
- int idx = internalIndexOf(o, items, 0, c);
- if (lock.getSequence() == seq)
- return idx;
- }
- lock.lock();
- try {
- return internalIndexOf(o, array, 0, count);
- } finally {
- lock.unlock();
+ for (;;) {
+ long seq = lock.awaitAvailability();
+ Object[] items = array;
+ int n = count;
+ if (n <= items.length) {
+ for (int i = 0; i < n; ++i) {
+ Object e = items[i];
+ if (lock.getSequence() != seq) {
+ lock.lock();
+ try {
+ return rawIndexOf(o, 0, count);
+ } finally {
+ lock.unlock();
+ }
+ }
+ else if ((o == null) ? e == null : o.equals(e))
+ return i;
+ }
+ return -1;
+ }
}
}
public boolean isEmpty() {
- long ignore = lock.getSequence();
return count == 0;
}
public Iterator iterator() {
- return new Itr(this, 0);
+ return new Itr(this, 0);
}
public int lastIndexOf(Object o) {
SequenceLock lock = this.lock;
- long seq = lock.awaitAvailability();
- Object[] items = array;
- int c = count;
- if (c <= items.length) {
- int idx = internalLastIndexOf(o, items, c - 1, 0);
- if (lock.getSequence() == seq)
- return idx;
- }
- lock.lock();
- try {
- return internalLastIndexOf(o, array, count-1, 0);
- } finally {
- lock.unlock();
+ for (;;) {
+ long seq = lock.awaitAvailability();
+ Object[] items = array;
+ int n = count;
+ if (n <= items.length) {
+ for (int i = n - 1; i >= 0; --i) {
+ Object e = items[i];
+ if (lock.getSequence() != seq) {
+ lock.lock();
+ try {
+ return rawLastIndexOf(o, 0, count);
+ } finally {
+ lock.unlock();
+ }
+ }
+ else if ((o == null) ? e == null : o.equals(e))
+ return i;
+ }
+ return -1;
+ }
}
}
public ListIterator listIterator() {
- return new Itr(this, 0);
+ return new Itr(this, 0);
}
public ListIterator listIterator(int index) {
- return new Itr(this, index);
+ return new Itr(this, index);
}
public E remove(int index) {
SequenceLock lock = this.lock;
- E oldValue;
+ Object oldValue;
lock.lock();
try {
if (index < 0 || index >= count)
throw new ArrayIndexOutOfBoundsException(index);
- oldValue = (E)array[index];
- internalRemoveAt(index);
+ oldValue = array[index];
+ rawRemoveAt(index);
} finally {
lock.unlock();
}
- return oldValue;
+ return (E)oldValue;
}
public boolean remove(Object o) {
@@ -703,7 +756,7 @@ public class ReadMostlyVector impleme
boolean removed;
lock.lock();
try {
- removed = internalRemoveAt(internalIndexOf(o, array, 0, count));
+ removed = rawRemoveAt(rawIndexOf(o, 0, count));
} finally {
lock.unlock();
}
@@ -719,22 +772,22 @@ public class ReadMostlyVector impleme
}
public E set(int index, E element) {
- E oldValue;
+ Object oldValue;
SequenceLock lock = this.lock;
lock.lock();
try {
+ Object[] items = array;
if (index < 0 || index >= count)
throw new ArrayIndexOutOfBoundsException(index);
- oldValue = (E)array[index];
- array[index] = element;
+ oldValue = items[index];
+ items[index] = element;
} finally {
lock.unlock();
}
- return oldValue;
+ return (E)oldValue;
}
public int size() {
- long ignore = lock.getSequence();
return count;
}
@@ -743,7 +796,7 @@ public class ReadMostlyVector impleme
int ssize = toIndex - fromIndex;
if (fromIndex < 0 || toIndex > c || ssize < 0)
throw new IndexOutOfBoundsException();
- return new ReadMostlyVectorSublist(this, fromIndex, ssize);
+ return new ReadMostlyVectorSublist(this, fromIndex, ssize);
}
public Object[] toArray() {
@@ -764,15 +817,15 @@ public class ReadMostlyVector impleme
* Append the element if not present.
*
* @param e element to be added to this list, if absent
- * @return true if the element was added
+ * @return {@code true} if the element was added
*/
public boolean addIfAbsent(E e) {
boolean added;
SequenceLock lock = this.lock;
lock.lock();
try {
- if (internalIndexOf(e, array, 0, count) < 0) {
- internalAdd(e);
+ if (rawIndexOf(e, 0, count) < 0) {
+ rawAdd(e);
added = true;
}
else
@@ -803,8 +856,8 @@ public class ReadMostlyVector impleme
try {
for (int i = 0; i < clen; ++i) {
Object e = cs[i];
- if (internalIndexOf(e, array, 0, count) < 0) {
- internalAdd((E)e);
+ if (rawIndexOf(e, 0, count) < 0) {
+ rawAdd(e);
++added;
}
}
@@ -815,6 +868,31 @@ public class ReadMostlyVector impleme
return added;
}
+ /**
+ * Returns an iterator operating over a snapshot copy of the
+ * elements of this collection created upon construction of the
+ * iterator. The iterator does NOT support the
+ * {@code remove} method.
+ *
+ * @return an iterator over the elements in this list in proper sequence
+ */
+ public Iterator snapshotIterator() {
+ return new SnapshotIterator(this);
+ }
+
+ static final class SnapshotIterator implements Iterator {
+ final Object[] items;
+ int cursor;
+ SnapshotIterator(ReadMostlyVector v) { items = v.toArray(); }
+ public boolean hasNext() { return cursor < items.length; }
+ public E next() {
+ if (cursor < items.length)
+ return (E) items[cursor++];
+ throw new NoSuchElementException();
+ }
+ public void remove() { throw new UnsupportedOperationException() ; }
+ }
+
// Vector-only methods
/** See {@link Vector#firstElement} */
@@ -824,23 +902,23 @@ public class ReadMostlyVector impleme
long seq = lock.awaitAvailability();
Object[] items = array;
int len = items.length;
- int c = count;
- if (c > len || c < 0)
+ int n = count;
+ if (n > len)
continue;
- E e; boolean ex;
- if (c == 0) {
- e = null;
- ex = true;
+ Object e; boolean ex;
+ if (n > 0) {
+ e = items[0];
+ ex = false;
}
else {
- e = (E)items[0];
- ex = false;
+ e = null;
+ ex = true;
}
if (lock.getSequence() == seq) {
if (ex)
throw new NoSuchElementException();
else
- return e;
+ return (E)e;
}
}
}
@@ -852,23 +930,23 @@ public class ReadMostlyVector impleme
long seq = lock.awaitAvailability();
Object[] items = array;
int len = items.length;
- int c = count;
- if (c > len || c < 0)
+ int n = count;
+ if (n > len)
continue;
- E e; boolean ex;
- if (c == 0) {
- e = null;
- ex = true;
+ Object e; boolean ex;
+ if (n > 0) {
+ e = items[n - 1];
+ ex = false;
}
else {
- e = (E)items[c - 1];
- ex = false;
+ e = null;
+ ex = true;
}
if (lock.getSequence() == seq) {
if (ex)
throw new NoSuchElementException();
else
- return e;
+ return (E)e;
}
}
}
@@ -880,21 +958,21 @@ public class ReadMostlyVector impleme
boolean ex = false;
long seq = lock.awaitAvailability();
Object[] items = array;
- int c = count;
+ int n = count;
boolean retry = false;
- if (c > items.length)
+ if (n > items.length)
retry = true;
else if (index < 0)
ex = true;
else
- idx = internalIndexOf(o, items, index, c);
+ idx = validatedIndexOf(o, items, index, n, seq);
if (retry || lock.getSequence() != seq) {
lock.lock();
try {
if (index < 0)
ex = true;
else
- idx = internalIndexOf(o, array, 0, count);
+ idx = rawIndexOf(o, 0, count);
} finally {
lock.unlock();
}
@@ -911,21 +989,21 @@ public class ReadMostlyVector impleme
boolean ex = false;
long seq = lock.awaitAvailability();
Object[] items = array;
- int c = count;
+ int n = count;
boolean retry = false;
- if (c > items.length)
+ if (n > items.length)
retry = true;
- else if (index >= c)
+ else if (index >= n)
ex = true;
else
- idx = internalLastIndexOf(o, items, index, 0);
+ idx = validatedLastIndexOf(o, items, index, 0, seq);
if (retry || lock.getSequence() != seq) {
lock.lock();
try {
if (index >= count)
ex = true;
else
- idx = internalLastIndexOf(o, array, index, 0);
+ idx = rawLastIndexOf(o, index, 0);
} finally {
lock.unlock();
}
@@ -942,12 +1020,13 @@ public class ReadMostlyVector impleme
SequenceLock lock = this.lock;
lock.lock();
try {
- int c = count;
- if (newSize > c)
+ int n = count;
+ if (newSize > n)
grow(newSize);
else {
- for (int i = newSize ; i < c ; i++)
- array[i] = null;
+ Object[] items = array;
+ for (int i = newSize ; i < n ; i++)
+ items[i] = null;
}
count = newSize;
} finally {
@@ -971,8 +1050,10 @@ public class ReadMostlyVector impleme
SequenceLock lock = this.lock;
lock.lock();
try {
- if (count < array.length)
- array = Arrays.copyOf(array, count);
+ Object[] items = array;
+ int n = count;
+ if (n < items.length)
+ array = Arrays.copyOf(items, n);
} finally {
lock.unlock();
}
@@ -994,12 +1075,11 @@ public class ReadMostlyVector impleme
/** See {@link Vector#elements} */
public Enumeration elements() {
- return new Itr(this, 0);
+ return new Itr(this, 0);
}
/** See {@link Vector#capacity} */
public int capacity() {
- long ignore = lock.getSequence();
return array.length;
}
@@ -1040,28 +1120,27 @@ public class ReadMostlyVector impleme
// other methods
- public Object clone() {
+ public ReadMostlyVector clone() {
SequenceLock lock = this.lock;
Object[] a = null;
- int c;
boolean retry = false;
long seq = lock.awaitAvailability();
Object[] items = array;
- c = count;
- if (c <= items.length)
- a = Arrays.copyOf(items, c);
+ int n = count;
+ if (n <= items.length)
+ a = Arrays.copyOf(items, n);
else
retry = true;
if (retry || lock.getSequence() != seq) {
lock.lock();
try {
- c = count;
- a = Arrays.copyOf(array, c);
+ n = count;
+ a = Arrays.copyOf(array, n);
} finally {
lock.unlock();
}
}
- return new ReadMostlyVector(a, c, capacityIncrement);
+ return new ReadMostlyVector(a, n, capacityIncrement);
}
private void writeObject(java.io.ObjectOutputStream s)
@@ -1084,7 +1163,7 @@ public class ReadMostlyVector impleme
int cursor;
int fence;
int lastRet;
- boolean haveNext, havePrev;
+ boolean validNext, validPrev;
Itr(ReadMostlyVector list, int index) {
this.list = list;
@@ -1097,51 +1176,66 @@ public class ReadMostlyVector impleme
}
private void refresh() {
+ validNext = validPrev = false;
do {
seq = lock.awaitAvailability();
items = list.array;
- fence = list.count;
- } while (lock.getSequence() != seq);
+ } while ((fence = list.count) > items.length ||
+ lock.getSequence() != seq);
}
public boolean hasNext() {
+ boolean valid;
int i = cursor;
- while (i < fence && i >= 0) {
+ for (;;) {
+ if (i >= fence || i < 0 || i >= items.length) {
+ valid = false;
+ break;
+ }
+ next = items[i];
if (lock.getSequence() == seq) {
- next = items[i];
- return haveNext = true;
+ valid = true;
+ break;
}
refresh();
}
- return false;
+ return validNext = valid;
}
public boolean hasPrevious() {
- int i = cursor;
- while (i <= fence && i > 0) {
+ boolean valid;
+ int i = cursor - 1;
+ for (;;) {
+ if (i >= fence || i < 0 || i >= items.length) {
+ valid = false;
+ break;
+ }
+ prev = items[i];
if (lock.getSequence() == seq) {
- prev = items[i - 1];
- return havePrev = true;
+ valid = true;
+ break;
}
refresh();
}
- return false;
+ return validPrev = valid;
}
public E next() {
- if (!haveNext && !hasNext())
- throw new NoSuchElementException();
- haveNext = false;
- lastRet = cursor++;
- return (E) next;
+ if (validNext || hasNext()) {
+ validNext = false;
+ lastRet = cursor++;
+ return (E) next;
+ }
+ throw new NoSuchElementException();
}
public E previous() {
- if (!havePrev && !hasPrevious())
- throw new NoSuchElementException();
- havePrev = false;
- lastRet = cursor--;
- return (E) prev;
+ if (validPrev || hasPrevious()) {
+ validPrev = false;
+ lastRet = cursor--;
+ return (E) prev;
+ }
+ throw new NoSuchElementException();
}
public void remove() {
@@ -1217,7 +1311,7 @@ public class ReadMostlyVector impleme
lock.lock();
try {
int c = size;
- list.internalAddAt(c + offset, element);
+ list.rawAddAt(c + offset, element);
size = c + 1;
} finally {
lock.unlock();
@@ -1231,7 +1325,7 @@ public class ReadMostlyVector impleme
try {
if (index < 0 || index > size)
throw new ArrayIndexOutOfBoundsException(index);
- list.internalAddAt(index + offset, element);
+ list.rawAddAt(index + offset, element);
++size;
} finally {
lock.unlock();
@@ -1246,7 +1340,7 @@ public class ReadMostlyVector impleme
try {
int s = size;
int pc = list.count;
- list.internalAddAllAt(offset + s, elements);
+ list.rawAddAllAt(offset + s, elements);
added = list.count - pc;
size = s + added;
} finally {
@@ -1265,7 +1359,7 @@ public class ReadMostlyVector impleme
if (index < 0 || index > s)
throw new ArrayIndexOutOfBoundsException(index);
int pc = list.count;
- list.internalAddAllAt(index + offset, elements);
+ list.rawAddAllAt(index + offset, elements);
added = list.count - pc;
size = s + added;
} finally {
@@ -1278,7 +1372,7 @@ public class ReadMostlyVector impleme
SequenceLock lock = list.lock;
lock.lock();
try {
- list.internalClear(offset, size);
+ list.internalClear(offset, offset + size);
size = 0;
} finally {
lock.unlock();
@@ -1290,7 +1384,7 @@ public class ReadMostlyVector impleme
}
public boolean containsAll(Collection> c) {
- return list.internalContainsAll(c, offset, size);
+ return list.internalContainsAll(c, offset, offset + size);
}
public boolean equals(Object o) {
@@ -1298,7 +1392,7 @@ public class ReadMostlyVector impleme
return true;
if (!(o instanceof List))
return false;
- return list.internalEquals((List>)(o), offset, size);
+ return list.internalEquals((List>)(o), offset, offset + size);
}
public E get(int index) {
@@ -1308,7 +1402,7 @@ public class ReadMostlyVector impleme
}
public int hashCode() {
- return list.internalHashCode(offset, size);
+ return list.internalHashCode(offset, offset + size);
}
public int indexOf(Object o) {
@@ -1317,13 +1411,14 @@ public class ReadMostlyVector impleme
Object[] items = list.array;
int c = list.count;
if (c <= items.length) {
- int idx = internalIndexOf(o, items, offset, offset+size);
+ int idx = list.validatedIndexOf(o, items, offset,
+ offset + size, seq);
if (lock.getSequence() == seq)
return idx < 0 ? -1 : idx - offset;
}
lock.lock();
try {
- int idx = internalIndexOf(o, list.array, offset, offset+size);
+ int idx = list.rawIndexOf(o, offset, offset + size);
return idx < 0 ? -1 : idx - offset;
} finally {
lock.unlock();
@@ -1335,7 +1430,7 @@ public class ReadMostlyVector impleme
}
public Iterator iterator() {
- return new SubItr(this, offset);
+ return new SubItr(this, offset);
}
public int lastIndexOf(Object o) {
@@ -1344,14 +1439,14 @@ public class ReadMostlyVector impleme
Object[] items = list.array;
int c = list.count;
if (c <= items.length) {
- int idx = internalLastIndexOf(o, items, offset+size-1, offset);
+ int idx = list.validatedLastIndexOf(o, items, offset+size-1,
+ offset, seq);
if (lock.getSequence() == seq)
return idx < 0 ? -1 : idx - offset;
}
lock.lock();
try {
- int idx = internalLastIndexOf(o, list.array, offset+size-1,
- offset);
+ int idx = list.rawLastIndexOf(o, offset + size - 1, offset);
return idx < 0 ? -1 : idx - offset;
} finally {
lock.unlock();
@@ -1359,28 +1454,29 @@ public class ReadMostlyVector impleme
}
public ListIterator listIterator() {
- return new SubItr(this, offset);
+ return new SubItr(this, offset);
}
public ListIterator listIterator(int index) {
- return new SubItr(this, index + offset);
+ return new SubItr(this, index + offset);
}
public E remove(int index) {
- E result;
+ Object result;
SequenceLock lock = list.lock;
lock.lock();
try {
- if (index < 0 || index >= size)
- throw new ArrayIndexOutOfBoundsException(index);
+ Object[] items = list.array;
int i = index + offset;
- result = (E)list.array[i];
- list.internalRemoveAt(i);
+ if (index < 0 || index >= size || i >= items.length)
+ throw new ArrayIndexOutOfBoundsException(index);
+ result = items[i];
+ list.rawRemoveAt(i);
size--;
} finally {
lock.unlock();
}
- return result;
+ return (E)result;
}
public boolean remove(Object o) {
@@ -1388,8 +1484,8 @@ public class ReadMostlyVector impleme
SequenceLock lock = list.lock;
lock.lock();
try {
- if (list.internalRemoveAt(internalIndexOf(o, list.array, offset,
- offset+size))) {
+ if (list.rawRemoveAt(list.rawIndexOf(o, offset,
+ offset + size))) {
removed = true;
--size;
}
@@ -1400,11 +1496,11 @@ public class ReadMostlyVector impleme
}
public boolean removeAll(Collection> c) {
- return list.internalRemoveAll(c, offset, size);
+ return list.internalRemoveAll(c, offset, offset + size);
}
public boolean retainAll(Collection> c) {
- return list.internalRetainAll(c, offset, size);
+ return list.internalRetainAll(c, offset, offset + size);
}
public E set(int index, E element) {
@@ -1422,19 +1518,19 @@ public class ReadMostlyVector impleme
int ssize = toIndex - fromIndex;
if (fromIndex < 0 || toIndex > c || ssize < 0)
throw new IndexOutOfBoundsException();
- return new ReadMostlyVectorSublist(list, offset+fromIndex, ssize);
+ return new ReadMostlyVectorSublist(list, offset+fromIndex, ssize);
}
public Object[] toArray() {
- return list.internalToArray(offset, size);
+ return list.internalToArray(offset, offset + size);
}
public T[] toArray(T[] a) {
- return list.internalToArray(a, offset, size);
+ return list.internalToArray(a, offset, offset + size);
}
public String toString() {
- return list.internalToString(offset, size);
+ return list.internalToString(offset, offset + size);
}
}
@@ -1449,7 +1545,7 @@ public class ReadMostlyVector impleme
int cursor;
int fence;
int lastRet;
- boolean haveNext, havePrev;
+ boolean validNext, validPrev;
SubItr(ReadMostlyVectorSublist sublist, int index) {
this.sublist = sublist;
@@ -1463,53 +1559,70 @@ public class ReadMostlyVector impleme
}
private void refresh() {
+ validNext = validPrev = false;
do {
+ int n;
seq = lock.awaitAvailability();
items = list.array;
- int c = list.count;
+ if ((n = list.count) > items.length)
+ continue;
int b = sublist.offset + sublist.size;
- fence = b < c ? b : c;
+ fence = b < n ? b : n;
} while (lock.getSequence() != seq);
}
public boolean hasNext() {
+ boolean valid;
int i = cursor;
- while (i < fence && i >= 0) {
+ for (;;) {
+ if (i >= fence || i < 0 || i >= items.length) {
+ valid = false;
+ break;
+ }
+ next = items[i];
if (lock.getSequence() == seq) {
- next = items[i];
- return haveNext = true;
+ valid = true;
+ break;
}
refresh();
}
- return false;
+ return validNext = valid;
}
public boolean hasPrevious() {
- int i = cursor;
- while (i <= fence && i > 0) {
+ boolean valid;
+ int i = cursor - 1;
+ for (;;) {
+ if (i >= fence || i < 0 || i >= items.length) {
+ valid = false;
+ break;
+ }
+ prev = items[i];
if (lock.getSequence() == seq) {
- prev = items[i - 1];
- return havePrev = true;
+ valid = true;
+ break;
}
refresh();
}
- return false;
+ return validPrev = valid;
}
public E next() {
- if (!haveNext && !hasNext())
- throw new NoSuchElementException();
- haveNext = false;
- lastRet = cursor++;
- return (E) next;
+ if (validNext || hasNext()) {
+ validNext = false;
+ lastRet = cursor++;
+ return (E) next;
+ }
+ throw new NoSuchElementException();
}
public E previous() {
- if (!havePrev && !hasPrevious())
- throw new NoSuchElementException();
- havePrev = false;
- lastRet = cursor--;
- return (E) prev;
+ if (validPrev || hasPrevious()) {
+ validPrev = false;
+ lastRet = cursor--;
+ return (E) prev;
+ }
+ throw new NoSuchElementException();
}
public int nextIndex() {