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.8 by jsr166, Mon Jul 18 15:24:08 2011 UTC vs.
Revision 1.11 by dl, Wed Jul 20 20:29:33 2011 UTC

# Line 57 | Line 57 | public class ReadMostlyVector<E> impleme
57      private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
58  
59      // fields are non-private to simpify nested class access
60 <    Object[] array;
60 >    volatile Object[] array;
61      final SequenceLock lock;
62 <    int count;
62 >    volatile int count;
63      final int capacityIncrement;
64  
65      /**
# Line 201 | Line 201 | public class ReadMostlyVector<E> impleme
201  
202      final void rawAdd(Object e) {
203          int n = count;
204 <        if (n >= array.length)
204 >        Object[] items = array;
205 >        if (n < items.length)
206 >            items[n] = e;
207 >        else {
208              grow(n + 1);
209 <        array[n] = e;
209 >            array[n] = e;
210 >        }
211          count = n + 1;
212      }
213  
# Line 213 | Line 217 | public class ReadMostlyVector<E> impleme
217              throw new ArrayIndexOutOfBoundsException(index);
218          if (n >= array.length)
219              grow(n + 1);
220 +        Object[] items = array;
221          if (index < n)
222 <            System.arraycopy(array, index, array, index + 1, n - index);
223 <        array[index] = e;
222 >            System.arraycopy(items, index, items, index + 1, n - index);
223 >        items[index] = e;
224          count = n + 1;
225      }
226  
# Line 229 | Line 234 | public class ReadMostlyVector<E> impleme
234          int newCount = n + len;
235          if (newCount >= array.length)
236              grow(newCount);
237 <        int mv = count - index;
237 >        Object[] items = array;
238 >        int mv = n - index;
239          if (mv > 0)
240 <            System.arraycopy(array, index, array, index + len, mv);
241 <        System.arraycopy(elements, 0, array, index, len);
240 >            System.arraycopy(items, index, items, index + len, mv);
241 >        System.arraycopy(elements, 0, items, index, len);
242          count = newCount;
243          return true;
244      }
245  
246      final boolean rawRemoveAt(int index) {
247 +        Object[] items = array;
248          int n = count - 1;
249          if (index < 0 || index > n)
250              return false;
251          int mv = n - index;
252          if (mv > 0)
253 <            System.arraycopy(array, index + 1, array, index, mv);
254 <        array[n] = null;
253 >            System.arraycopy(items, index + 1, items, index, mv);
254 >        items[n] = null;
255          count = n;
256          return true;
257      }
# Line 281 | Line 288 | public class ReadMostlyVector<E> impleme
288          if (c != this) {
289              lock.lock();
290              try {
291 +                Object[] items = array;
292                  int i = origin;
293                  int n = count;
294                  int fence = bound < 0 || bound > n ? n : bound;
295                  while (i >= 0 && i < fence) {
296 <                    if (c.contains(array[i]))
296 >                    if (c.contains(items[i]))
297                          ++i;
298                      else {
299                          --fence;
300                          int mv = --count - i;
301                          if (mv > 0)
302 <                            System.arraycopy(array, i + 1, array, i, mv);
302 >                            System.arraycopy(items, i + 1, items, i, mv);
303                          removed = true;
304                      }
305                  }
# Line 303 | Line 311 | public class ReadMostlyVector<E> impleme
311      }
312  
313      final void internalClear(int origin, int bound) {
314 +        Object[] items = array;
315          int n = count;
316          int fence = bound < 0 || bound > n ? n : bound;
317          if (origin >= 0 && origin < fence) {
# Line 310 | Line 319 | public class ReadMostlyVector<E> impleme
319              int newCount = n - removed;
320              int mv = n - (origin + removed);
321              if (mv > 0)
322 <                System.arraycopy(array, origin + removed, array, origin, mv);
322 >                System.arraycopy(items, origin + removed, items, origin, mv);
323              for (int i = n; i < newCount; ++i)
324 <                array[i] = null;
324 >                items[i] = null;
325              count = newCount;
326          }
327      }
# Line 524 | Line 533 | public class ReadMostlyVector<E> impleme
533                      rlen = 0;
534                  if (origin < 0 || alen >= rlen) {
535                      if (rlen > 0)
536 <                        System.arraycopy(array, 0, a, origin, rlen);
536 >                        System.arraycopy(items, 0, a, origin, rlen);
537                      if (alen > rlen)
538                          a[rlen] = null;
539                      result = a;
540                  }
541                  else
542 <                    result = (T[]) Arrays.copyOfRange(array, origin,
542 >                    result = (T[]) Arrays.copyOfRange(items, origin,
543                                                        fence, a.getClass());
544                  if (lock.getSequence() == seq)
545                      break;
# Line 603 | Line 612 | public class ReadMostlyVector<E> impleme
612          SequenceLock lock = this.lock;
613          lock.lock();
614          try {
615 +            Object[] items = array;
616              for (int i = 0; i < count; i++)
617 <                array[i] = null;
617 >                items[i] = null;
618              count = 0;
619          } finally {
620              lock.unlock();
# Line 683 | Line 693 | public class ReadMostlyVector<E> impleme
693      }
694  
695      public boolean isEmpty() {
686        long ignore = lock.getSequence();
696          return count == 0;
697      }
698  
699      public Iterator<E> iterator() {
700 <        return new Itr(this, 0);
700 >        return new Itr<E>(this, 0);
701      }
702  
703      public int lastIndexOf(Object o) {
# Line 717 | Line 726 | public class ReadMostlyVector<E> impleme
726      }
727  
728      public ListIterator<E> listIterator() {
729 <        return new Itr(this, 0);
729 >        return new Itr<E>(this, 0);
730      }
731  
732      public ListIterator<E> listIterator(int index) {
733 <        return new Itr(this, index);
733 >        return new Itr<E>(this, index);
734      }
735  
736      public E remove(int index) {
# Line 764 | Line 773 | public class ReadMostlyVector<E> impleme
773          SequenceLock lock = this.lock;
774          lock.lock();
775          try {
776 +            Object[] items = array;
777              if (index < 0 || index >= count)
778                  throw new ArrayIndexOutOfBoundsException(index);
779 <            oldValue = array[index];
780 <            array[index] = element;
779 >            oldValue = items[index];
780 >            items[index] = element;
781          } finally {
782              lock.unlock();
783          }
# Line 775 | Line 785 | public class ReadMostlyVector<E> impleme
785      }
786  
787      public int size() {
778        long ignore = lock.getSequence();
788          return count;
789      }
790  
# Line 784 | Line 793 | public class ReadMostlyVector<E> impleme
793          int ssize = toIndex - fromIndex;
794          if (fromIndex < 0 || toIndex > c || ssize < 0)
795              throw new IndexOutOfBoundsException();
796 <        return new ReadMostlyVectorSublist(this, fromIndex, ssize);
796 >        return new ReadMostlyVectorSublist<E>(this, fromIndex, ssize);
797      }
798  
799      public Object[] toArray() {
# Line 805 | Line 814 | public class ReadMostlyVector<E> impleme
814       * Append the element if not present.
815       *
816       * @param e element to be added to this list, if absent
817 <     * @return <tt>true</tt> if the element was added
817 >     * @return {@code true} if the element was added
818       */
819      public boolean addIfAbsent(E e) {
820          boolean added;
# Line 860 | Line 869 | public class ReadMostlyVector<E> impleme
869       * Returns an iterator operating over a snapshot copy of the
870       * elements of this collection created upon construction of the
871       * iterator. The iterator does <em>NOT</em> support the
872 <     * <tt>remove</tt> method.
872 >     * {@code remove} method.
873       *
874       * @return an iterator over the elements in this list in proper sequence
875       */
# Line 1012 | Line 1021 | public class ReadMostlyVector<E> impleme
1021              if (newSize > n)
1022                  grow(newSize);
1023              else {
1024 +                Object[] items = array;
1025                  for (int i = newSize ; i < n ; i++)
1026 <                    array[i] = null;
1026 >                    items[i] = null;
1027              }
1028              count = newSize;
1029          } finally {
# Line 1037 | Line 1047 | public class ReadMostlyVector<E> impleme
1047          SequenceLock lock = this.lock;
1048          lock.lock();
1049          try {
1050 <            if (count < array.length)
1051 <                array = Arrays.copyOf(array, count);
1050 >            Object[] items = array;
1051 >            if (count < items.length)
1052 >                array = Arrays.copyOf(items, count);
1053          } finally {
1054              lock.unlock();
1055          }
# Line 1060 | Line 1071 | public class ReadMostlyVector<E> impleme
1071  
1072      /** See {@link Vector#elements} */
1073      public Enumeration<E> elements() {
1074 <        return new Itr(this, 0);
1074 >        return new Itr<E>(this, 0);
1075      }
1076  
1077      /** See {@link Vector#capacity} */
1078      public int capacity() {
1068        long ignore = lock.getSequence();
1079          return array.length;
1080      }
1081  
# Line 1106 | Line 1116 | public class ReadMostlyVector<E> impleme
1116  
1117      // other methods
1118  
1119 <    public Object clone() {
1119 >    public ReadMostlyVector<E> clone() {
1120          SequenceLock lock = this.lock;
1121          Object[] a = null;
1122          boolean retry = false;
# Line 1126 | Line 1136 | public class ReadMostlyVector<E> impleme
1136                  lock.unlock();
1137              }
1138          }
1139 <        return new ReadMostlyVector(a, n, capacityIncrement);
1139 >        return new ReadMostlyVector<E>(a, n, capacityIncrement);
1140      }
1141  
1142      private void writeObject(java.io.ObjectOutputStream s)
# Line 1416 | Line 1426 | public class ReadMostlyVector<E> impleme
1426          }
1427  
1428          public Iterator<E> iterator() {
1429 <            return new SubItr(this, offset);
1429 >            return new SubItr<E>(this, offset);
1430          }
1431  
1432          public int lastIndexOf(Object o) {
# Line 1440 | Line 1450 | public class ReadMostlyVector<E> impleme
1450          }
1451  
1452          public ListIterator<E> listIterator() {
1453 <            return new SubItr(this, offset);
1453 >            return new SubItr<E>(this, offset);
1454          }
1455  
1456          public ListIterator<E> listIterator(int index) {
1457 <            return new SubItr(this, index + offset);
1457 >            return new SubItr<E>(this, index + offset);
1458          }
1459  
1460          public E remove(int index) {
# Line 1504 | Line 1514 | public class ReadMostlyVector<E> impleme
1514              int ssize = toIndex - fromIndex;
1515              if (fromIndex < 0 || toIndex > c || ssize < 0)
1516                  throw new IndexOutOfBoundsException();
1517 <            return new ReadMostlyVectorSublist(list, offset+fromIndex, ssize);
1517 >            return new ReadMostlyVectorSublist<E>(list, offset+fromIndex, ssize);
1518          }
1519  
1520          public Object[] toArray() {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines