--- jsr166/src/jsr166e/extra/ReadMostlyVector.java 2011/07/19 12:16:06 1.10 +++ jsr166/src/jsr166e/extra/ReadMostlyVector.java 2011/07/20 20:29:33 1.11 @@ -57,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; /** @@ -201,9 +201,13 @@ public class ReadMostlyVector impleme final void rawAdd(Object e) { int n = count; - if (n >= array.length) + Object[] items = array; + if (n < items.length) + items[n] = e; + else { grow(n + 1); - array[n] = e; + array[n] = e; + } count = n + 1; } @@ -213,9 +217,10 @@ public class ReadMostlyVector impleme throw new ArrayIndexOutOfBoundsException(index); if (n >= array.length) grow(n + 1); + Object[] items = array; if (index < n) - System.arraycopy(array, index, array, index + 1, n - index); - array[index] = e; + System.arraycopy(items, index, items, index + 1, n - index); + items[index] = e; count = n + 1; } @@ -229,22 +234,24 @@ public class ReadMostlyVector impleme int newCount = n + len; if (newCount >= array.length) grow(newCount); - int mv = count - index; + Object[] items = array; + 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 rawRemoveAt(int index) { + Object[] items = array; int n = count - 1; if (index < 0 || index > n) return false; int mv = n - index; if (mv > 0) - System.arraycopy(array, index + 1, array, index, mv); - array[n] = null; + System.arraycopy(items, index + 1, items, index, mv); + items[n] = null; count = n; return true; } @@ -281,17 +288,18 @@ public class ReadMostlyVector impleme if (c != this) { lock.lock(); try { + Object[] items = array; int i = origin; int n = count; int fence = bound < 0 || bound > n ? n : bound; while (i >= 0 && i < fence) { - if (c.contains(array[i])) + if (c.contains(items[i])) ++i; else { --fence; int mv = --count - i; if (mv > 0) - System.arraycopy(array, i + 1, array, i, mv); + System.arraycopy(items, i + 1, items, i, mv); removed = true; } } @@ -303,6 +311,7 @@ public class ReadMostlyVector impleme } final void internalClear(int origin, int bound) { + Object[] items = array; int n = count; int fence = bound < 0 || bound > n ? n : bound; if (origin >= 0 && origin < fence) { @@ -310,9 +319,9 @@ public class ReadMostlyVector impleme int newCount = n - removed; int mv = n - (origin + removed); if (mv > 0) - System.arraycopy(array, origin + removed, array, origin, mv); + System.arraycopy(items, origin + removed, items, origin, mv); for (int i = n; i < newCount; ++i) - array[i] = null; + items[i] = null; count = newCount; } } @@ -524,13 +533,13 @@ public class ReadMostlyVector impleme rlen = 0; if (origin < 0 || alen >= rlen) { if (rlen > 0) - System.arraycopy(array, 0, a, origin, rlen); + System.arraycopy(items, 0, a, origin, rlen); if (alen > rlen) a[rlen] = null; result = a; } else - result = (T[]) Arrays.copyOfRange(array, origin, + result = (T[]) Arrays.copyOfRange(items, origin, fence, a.getClass()); if (lock.getSequence() == seq) break; @@ -603,8 +612,9 @@ public class ReadMostlyVector impleme SequenceLock lock = this.lock; lock.lock(); try { + Object[] items = array; for (int i = 0; i < count; i++) - array[i] = null; + items[i] = null; count = 0; } finally { lock.unlock(); @@ -683,7 +693,6 @@ public class ReadMostlyVector impleme } public boolean isEmpty() { - long ignore = lock.getSequence(); return count == 0; } @@ -764,10 +773,11 @@ public class ReadMostlyVector impleme SequenceLock lock = this.lock; lock.lock(); try { + Object[] items = array; if (index < 0 || index >= count) throw new ArrayIndexOutOfBoundsException(index); - oldValue = array[index]; - array[index] = element; + oldValue = items[index]; + items[index] = element; } finally { lock.unlock(); } @@ -775,7 +785,6 @@ public class ReadMostlyVector impleme } public int size() { - long ignore = lock.getSequence(); return count; } @@ -1012,8 +1021,9 @@ public class ReadMostlyVector impleme if (newSize > n) grow(newSize); else { + Object[] items = array; for (int i = newSize ; i < n ; i++) - array[i] = null; + items[i] = null; } count = newSize; } finally { @@ -1037,8 +1047,9 @@ public class ReadMostlyVector impleme SequenceLock lock = this.lock; lock.lock(); try { - if (count < array.length) - array = Arrays.copyOf(array, count); + Object[] items = array; + if (count < items.length) + array = Arrays.copyOf(items, count); } finally { lock.unlock(); } @@ -1065,7 +1076,6 @@ public class ReadMostlyVector impleme /** See {@link Vector#capacity} */ public int capacity() { - long ignore = lock.getSequence(); return array.length; }