ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/jsr166e/extra/ReadMostlyVector.java
(Generate patch)

Comparing jsr166/src/jsr166e/extra/ReadMostlyVector.java (file contents):
Revision 1.11 by dl, Wed Jul 20 20:29:33 2011 UTC vs.
Revision 1.12 by dl, Thu Jul 21 12:49:37 2011 UTC

# Line 129 | Line 129 | public class ReadMostlyVector<E> impleme
129      }
130  
131      // For explanation, see CopyOnWriteArrayList
132 <    final void grow(int minCapacity) {
132 >    final Object[] grow(int minCapacity) {
133          int oldCapacity = array.length;
134          int newCapacity = oldCapacity + ((capacityIncrement > 0) ?
135                                           capacityIncrement : oldCapacity);
# Line 137 | Line 137 | public class ReadMostlyVector<E> impleme
137              newCapacity = minCapacity;
138          if (newCapacity - MAX_ARRAY_SIZE > 0)
139              newCapacity = hugeCapacity(minCapacity);
140 <        array = Arrays.copyOf(array, newCapacity);
140 >        return array = Arrays.copyOf(array, newCapacity);
141      }
142  
143      static int hugeCapacity(int minCapacity) {
# Line 202 | Line 202 | public class ReadMostlyVector<E> impleme
202      final void rawAdd(Object e) {
203          int n = count;
204          Object[] items = array;
205 <        if (n < items.length)
206 <            items[n] = e;
207 <        else {
208 <            grow(n + 1);
209 <            array[n] = e;
210 <        }
205 >        if (n >= items.length)
206 >            items = grow(n + 1);
207 >        items[n] = e;
208          count = n + 1;
209      }
210  
211      final void rawAddAt(int index, Object e) {
212          int n = count;
213 +        Object[] items = array;
214          if (index > n)
215              throw new ArrayIndexOutOfBoundsException(index);
216 <        if (n >= array.length)
217 <            grow(n + 1);
220 <        Object[] items = array;
216 >        if (n >= items.length)
217 >            items = grow(n + 1);
218          if (index < n)
219              System.arraycopy(items, index, items, index + 1, n - index);
220          items[index] = e;
# Line 226 | Line 223 | public class ReadMostlyVector<E> impleme
223  
224      final boolean rawAddAllAt(int index, Object[] elements) {
225          int n = count;
226 +        Object[] items = array;
227          if (index < 0 || index > n)
228              throw new ArrayIndexOutOfBoundsException(index);
229          int len = elements.length;
230          if (len == 0)
231              return false;
232          int newCount = n + len;
233 <        if (newCount >= array.length)
234 <            grow(newCount);
237 <        Object[] items = array;
233 >        if (newCount >= items.length)
234 >            items = grow(newCount);
235          int mv = n - index;
236          if (mv > 0)
237              System.arraycopy(items, index, items, index + len, mv);
# Line 244 | Line 241 | public class ReadMostlyVector<E> impleme
241      }
242  
243      final boolean rawRemoveAt(int index) {
247        Object[] items = array;
244          int n = count - 1;
245 +        Object[] items = array;
246          if (index < 0 || index > n)
247              return false;
248          int mv = n - index;
# Line 297 | Line 294 | public class ReadMostlyVector<E> impleme
294                          ++i;
295                      else {
296                          --fence;
297 <                        int mv = --count - i;
297 >                        int mv = --n - i;
298                          if (mv > 0)
299                              System.arraycopy(items, i + 1, items, i, mv);
303                        removed = true;
300                      }
301                  }
302 +                if (count != n) {
303 +                    count = n;
304 +                    removed = true;
305 +                }
306              } finally {
307                  lock.unlock();
308              }
# Line 311 | Line 311 | public class ReadMostlyVector<E> impleme
311      }
312  
313      final void internalClear(int origin, int bound) {
314        Object[] items = array;
314          int n = count;
315          int fence = bound < 0 || bound > n ? n : bound;
316          if (origin >= 0 && origin < fence) {
317 +            Object[] items = array;
318              int removed = fence - origin;
319              int newCount = n - removed;
320              int mv = n - (origin + removed);
# Line 333 | Line 333 | public class ReadMostlyVector<E> impleme
333          try {
334              for (;;) {
335                  long seq = lock.awaitAvailability();
336 +                int n = count;
337                  Object[] items = array;
338                  int len = items.length;
338                int n = count;
339                  if (n > len)
340                      continue;
341                  int fence = bound < 0 || bound > n ? n : bound;
# Line 416 | Line 416 | public class ReadMostlyVector<E> impleme
416                  hash = 1;
417                  long seq = lock.awaitAvailability();
418                  Object[] items = array;
419                int len = items.length;
419                  int n = count;
420 +                int len = items.length;
421                  if (n > len)
422                      continue;
423                  int fence = bound < 0 || bound > n ? n : bound;
# Line 447 | Line 447 | public class ReadMostlyVector<E> impleme
447              outer:for (;;) {
448                  long seq = lock.awaitAvailability();
449                  Object[] items = array;
450                int len = items.length;
450                  int n = count;
451 +                int len = items.length;
452                  if (n > len)
453                      continue;
454                  int fence = bound < 0 || bound > n ? n : bound;
# Line 494 | Line 494 | public class ReadMostlyVector<E> impleme
494                  result = null;
495                  long seq = lock.awaitAvailability();
496                  Object[] items = array;
497                int len = items.length;
497                  int n = count;
498 +                int len = items.length;
499                  if (n > len)
500                      continue;
501                  int fence = bound < 0 || bound > n ? n : bound;
# Line 523 | Line 523 | public class ReadMostlyVector<E> impleme
523              for (;;) {
524                  long seq = lock.awaitAvailability();
525                  Object[] items = array;
526                int len = items.length;
526                  int n = count;
527 +                int len = items.length;
528                  if (n > len)
529                      continue;
530                  int fence = bound < 0 || bound > n ? n : bound;
# Line 584 | Line 584 | public class ReadMostlyVector<E> impleme
584          SequenceLock lock = this.lock;
585          lock.lock();
586          try {
587 <            int newCount = count + len;
588 <            if (newCount >= array.length)
589 <                grow(newCount);
590 <            System.arraycopy(elements, 0, array, count, len);
587 >            Object[] items = array;
588 >            int n = count;
589 >            int newCount = n + len;
590 >            if (newCount >= items.length)
591 >                items = grow(newCount);
592 >            System.arraycopy(elements, 0, items, n, len);
593              count = newCount;
594          } finally {
595              lock.unlock();
# Line 612 | Line 614 | public class ReadMostlyVector<E> impleme
614          SequenceLock lock = this.lock;
615          lock.lock();
616          try {
617 +            int n = count;
618              Object[] items = array;
619 <            for (int i = 0; i < count; i++)
619 >            for (int i = 0; i < n; i++)
620                  items[i] = null;
621              count = 0;
622          } finally {
# Line 641 | Line 644 | public class ReadMostlyVector<E> impleme
644          SequenceLock lock = this.lock;
645          for (;;) {
646              long seq = lock.awaitAvailability();
644            Object[] items = array;
647              int n = count;
648 +            Object[] items = array;
649              if (n > items.length)
650                  continue;
651              Object e; boolean ex;
# Line 1048 | Line 1051 | public class ReadMostlyVector<E> impleme
1051          lock.lock();
1052          try {
1053              Object[] items = array;
1054 <            if (count < items.length)
1055 <                array = Arrays.copyOf(items, count);
1054 >            int n = count;
1055 >            if (n < items.length)
1056 >                array = Arrays.copyOf(items, n);
1057          } finally {
1058              lock.unlock();
1059          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines