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.5 by dl, Sat Jul 16 16:05:32 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 161 | Line 161 | public class ReadMostlyVector<E> impleme
161              Object e = items[i];
162              if (lock.getSequence() != seq)
163                  break;
164 <            if (x == null? e == null : x.equals(e))
164 >            if ((x == null) ? e == null : x.equals(e))
165                  return i;
166          }
167          return -1;
# Line 171 | Line 171 | public class ReadMostlyVector<E> impleme
171          Object[] items = array;
172          for (int i = index; i < fence; ++i) {
173              Object e = items[i];
174 <            if (x == null? e == null : x.equals(e))
174 >            if ((x == null) ? e == null : x.equals(e))
175                  return i;
176          }
177          return -1;
# Line 183 | Line 183 | public class ReadMostlyVector<E> impleme
183              Object e = items[i];
184              if (lock.getSequence() != seq)
185                  break;
186 <            if (x == null? e == null : x.equals(e))
186 >            if ((x == null) ? e == null : x.equals(e))
187                  return i;
188          }
189          return -1;
# Line 193 | Line 193 | public class ReadMostlyVector<E> impleme
193          Object[] items = array;
194          for (int i = index; i >= origin; --i) {
195              Object e = items[i];
196 <            if (x == null? e == null : x.equals(e))
196 >            if ((x == null) ? e == null : x.equals(e))
197                  return i;
198          }
199          return -1;
# 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 335 | Line 344 | public class ReadMostlyVector<E> impleme
344                  else {
345                      contained = true;
346                      for (Object e : c) {
347 <                        int idx = (locked?
347 >                        int idx = (locked ?
348                                     rawIndexOf(e, origin, fence) :
349                                     validatedIndexOf(e, items, origin,
350                                                      fence, seq));
# 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 659 | Line 669 | public class ReadMostlyVector<E> impleme
669  
670      public int indexOf(Object o) {
671          SequenceLock lock = this.lock;
672 <        long seq = lock.awaitAvailability();
673 <        Object[] items = array;
674 <        int n = count;
675 <        if (n <= items.length) {
676 <            boolean valid = true;
677 <            for (int i = 0; i < n; ++i) {
678 <                Object e = items[i];
679 <                if (lock.getSequence() == seq) {
680 <                    if (o == null? e == null : o.equals(e))
672 >        for (;;) {
673 >            long seq = lock.awaitAvailability();
674 >            Object[] items = array;
675 >            int n = count;
676 >            if (n <= items.length) {
677 >                for (int i = 0; i < n; ++i) {
678 >                    Object e = items[i];
679 >                    if (lock.getSequence() != seq) {
680 >                        lock.lock();
681 >                        try {
682 >                            return rawIndexOf(o, 0, count);
683 >                        } finally {
684 >                            lock.unlock();
685 >                        }
686 >                    }
687 >                    else if ((o == null) ? e == null : o.equals(e))
688                          return i;
689                  }
673                else {
674                    valid = false;
675                    break;
676                }
677            }
678            if (valid)
690                  return -1;
691 <        }
681 <        lock.lock();
682 <        try {
683 <            return rawIndexOf(o, 0, count);
684 <        } finally {
685 <            lock.unlock();
691 >            }
692          }
693      }
694  
695      public boolean isEmpty() {
690        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) {
704          SequenceLock lock = this.lock;
705 <        long seq = lock.awaitAvailability();
706 <        Object[] items = array;
707 <        int n = count;
708 <        if (n <= items.length) {
709 <            int idx = validatedLastIndexOf(o, items, n - 1, 0, seq);
710 <            if (lock.getSequence() == seq)
711 <                return idx;
712 <        }
713 <        lock.lock();
714 <        try {
715 <            return rawLastIndexOf(o, count - 1, 0);
716 <        } finally {
717 <            lock.unlock();
705 >        for (;;) {
706 >            long seq = lock.awaitAvailability();
707 >            Object[] items = array;
708 >            int n = count;
709 >            if (n <= items.length) {
710 >                for (int i = n - 1; i >= 0; --i) {
711 >                    Object e = items[i];
712 >                    if (lock.getSequence() != seq) {
713 >                        lock.lock();
714 >                        try {
715 >                            return rawLastIndexOf(o, 0, count);
716 >                        } finally {
717 >                            lock.unlock();
718 >                        }
719 >                    }
720 >                    else if ((o == null) ? e == null : o.equals(e))
721 >                        return i;
722 >                }
723 >                return -1;
724 >            }
725          }
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 761 | 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 772 | Line 785 | public class ReadMostlyVector<E> impleme
785      }
786  
787      public int size() {
775        long ignore = lock.getSequence();
788          return count;
789      }
790  
# Line 781 | 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 802 | 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 857 | 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 1009 | 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 1034 | 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 1057 | 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() {
1065        long ignore = lock.getSequence();
1079          return array.length;
1080      }
1081  
# Line 1103 | 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 1123 | 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 1413 | 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 1437 | 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 1501 | 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