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.3 by jsr166, Sat Jul 16 14:56:30 2011 UTC vs.
Revision 1.4 by dl, Sat Jul 16 15:05:04 2011 UTC

# Line 151 | Line 151 | public class ReadMostlyVector<E> impleme
151       * as well as sublist and iterator classes.
152       */
153  
154 +    // Version of indexOf that returns -1 if either not present or invalid
155      final int validatedIndexOf(Object x, Object[] items, int index, int fence,
156                                 long seq) {
157          for (int i = index; i < fence; ++i) {
158              Object e = items[i];
159              if (lock.getSequence() != seq)
160                  break;
161 <            if ((x == null) ? e == null : (e != null && x.equals(e)))
161 >            if (x == null? e == null : x.equals(e))
162                  return i;
163          }
164          return -1;
# Line 167 | Line 168 | public class ReadMostlyVector<E> impleme
168          Object[] items = array;
169          for (int i = index; i < fence; ++i) {
170              Object e = items[i];
171 <            if ((x == null) ? e == null : (e != null && x.equals(e)))
171 >            if (x == null? e == null : x.equals(e))
172                  return i;
173          }
174          return -1;
# Line 179 | Line 180 | public class ReadMostlyVector<E> impleme
180              Object e = items[i];
181              if (lock.getSequence() != seq)
182                  break;
183 <            if ((x == null) ? e == null : (e != null && x.equals(e)))
183 >            if (x == null? e == null : x.equals(e))
184                  return i;
185          }
186          return -1;
# Line 189 | Line 190 | public class ReadMostlyVector<E> impleme
190          Object[] items = array;
191          for (int i = index; i >= origin; --i) {
192              Object e = items[i];
193 <            if ((x == null) ? e == null : (e != null && x.equals(e)))
193 >            if (x == null? e == null : x.equals(e))
194                  return i;
195          }
196          return -1;
197      }
198  
199 <    final void internalAdd(Object e) {
199 >    final void rawAdd(Object e) {
200          int n = count;
201          if (n >= array.length)
202              grow(n + 1);
# Line 203 | Line 204 | public class ReadMostlyVector<E> impleme
204          count = n + 1;
205      }
206  
207 <    final void internalAddAt(int index, Object e) {
207 >    final void rawAddAt(int index, Object e) {
208          int n = count;
209          if (index > n)
210              throw new ArrayIndexOutOfBoundsException(index);
# Line 215 | Line 216 | public class ReadMostlyVector<E> impleme
216          count = n + 1;
217      }
218  
219 <    final boolean internalAddAllAt(int index, Object[] elements) {
219 >    final boolean rawAddAllAt(int index, Object[] elements) {
220          int n = count;
221          if (index < 0 || index > n)
222              throw new ArrayIndexOutOfBoundsException(index);
# Line 233 | Line 234 | public class ReadMostlyVector<E> impleme
234          return true;
235      }
236  
237 <    final boolean internalRemoveAt(int index) {
237 >    final boolean rawRemoveAt(int index) {
238          int n = count - 1;
239          if (index < 0 || index > n)
240              return false;
# Line 261 | Line 262 | public class ReadMostlyVector<E> impleme
262              int fence = bound < 0 || bound > n ? n : bound;
263              if (origin >= 0 && origin < fence) {
264                  for (Object x : c) {
265 <                    while (internalRemoveAt(rawIndexOf(x, origin, fence)))
265 >                    while (rawRemoveAt(rawIndexOf(x, origin, fence)))
266                          removed = true;
267                  }
268              }
# Line 280 | Line 281 | public class ReadMostlyVector<E> impleme
281                  int i = origin;
282                  int n = count;
283                  int fence = bound < 0 || bound > n ? n : bound;
284 <                while (i < fence) {
284 >                while (i >= 0 && i < fence) {
285                      if (c.contains(array[i]))
286                          ++i;
287                      else {
# Line 331 | Line 332 | public class ReadMostlyVector<E> impleme
332                  else {
333                      contained = true;
334                      for (Object e : c) {
335 <                        if (validatedIndexOf(e, items, origin, fence, seq) < 0) {
335 >                        int idx = (locked?
336 >                                   rawIndexOf(e, origin, fence) :
337 >                                   validatedIndexOf(e, items, origin,
338 >                                                    fence, seq));
339 >                        if (idx < 0) {
340                              contained = false;
341                              break;
342                          }
# Line 351 | Line 356 | public class ReadMostlyVector<E> impleme
356  
357      final boolean internalEquals(List<?> list, int origin, int bound) {
358          SequenceLock lock = this.lock;
354        boolean equal;
359          boolean locked = false;
360 +        boolean equal;
361          try {
362 <            outer:for (;;) {
358 <                equal = true;
362 >            for (;;) {
363                  long seq = lock.awaitAvailability();
364                  Object[] items = array;
361                int len = items.length;
365                  int n = count;
366 <                if (n > len)
364 <                    continue;
365 <                int fence = bound < 0 || bound > n ? n : bound;
366 <                if (origin < 0)
366 >                if (n > items.length || origin < 0)
367                      equal = false;
368                  else {
369 +                    equal = true;
370 +                    int fence = bound < 0 || bound > n ? n : bound;
371                      Iterator<?> it = list.iterator();
372                      for (int i = origin; i < fence; ++i) {
373 <                        if (!it.hasNext()) {
374 <                            equal = false;
375 <                            break;
376 <                        }
377 <                        Object x = it.next();
378 <                        Object y = items[i];
377 <                        if (lock.getSequence() != seq)
378 <                            continue outer;
379 <                        if ((x == null) ? y != null : (y == null || !x.equals(y))) {
373 >                        Object x = items[i];
374 >                        Object y;
375 >                        if ((!locked && lock.getSequence() != seq) ||
376 >                            !it.hasNext() ||
377 >                            (y = it.next()) == null ?
378 >                            x != null : !y.equals(x)) {
379                              equal = false;
380                              break;
381                          }
# Line 450 | Line 449 | public class ReadMostlyVector<E> impleme
449                          Object e = items[i];
450                          if (e == this)
451                              sb.append("(this Collection)");
452 <                        else if (lock.getSequence() != seq)
452 >                        else if (!locked && lock.getSequence() != seq)
453                              continue outer;
454                          else
455                              sb.append(e.toString());
# Line 518 | Line 517 | public class ReadMostlyVector<E> impleme
517                      continue;
518                  int fence = bound < 0 || bound > n ? n : bound;
519                  int rlen = fence - origin;
520 +                if (rlen < 0)
521 +                    rlen = 0;
522                  if (origin < 0 || alen >= rlen) {
523 <                    if (rlen < 0)
523 <                        rlen = 0;
524 <                    else if (rlen > 0)
523 >                    if (rlen > 0)
524                          System.arraycopy(array, 0, a, origin, rlen);
525                      if (alen > rlen)
526                          a[rlen] = null;
# Line 548 | Line 547 | public class ReadMostlyVector<E> impleme
547          SequenceLock lock = this.lock;
548          lock.lock();
549          try {
550 <            internalAdd(e);
550 >            rawAdd(e);
551          } finally {
552              lock.unlock();
553          }
# Line 559 | Line 558 | public class ReadMostlyVector<E> impleme
558          SequenceLock lock = this.lock;
559          lock.lock();
560          try {
561 <            internalAddAt(index, element);
561 >            rawAddAt(index, element);
562          } finally {
563              lock.unlock();
564          }
# Line 590 | Line 589 | public class ReadMostlyVector<E> impleme
589          Object[] elements = c.toArray();
590          lock.lock();
591          try {
592 <            ret = internalAddAllAt(index, elements);
592 >            ret = rawAddAllAt(index, elements);
593          } finally {
594              lock.unlock();
595          }
# Line 622 | Line 621 | public class ReadMostlyVector<E> impleme
621              return true;
622          if (!(o instanceof List))
623              return false;
624 <        return internalEquals((List<?>)(o), 0, -1);
624 >        return internalEquals((List<?>)o, 0, -1);
625      }
626  
627      public E get(int index) {
# Line 661 | Line 660 | public class ReadMostlyVector<E> impleme
660          Object[] items = array;
661          int n = count;
662          if (n <= items.length) {
663 <            int idx = validatedIndexOf(o, items, 0, n, seq);
664 <            if (lock.getSequence() == seq)
665 <                return idx;
663 >            boolean valid = true;
664 >            for (int i = 0; i < n; ++i) {
665 >                Object e = items[i];
666 >                if (lock.getSequence() == seq) {
667 >                    if (o == null? e == null : o.equals(e))
668 >                        return i;
669 >                }
670 >                else {
671 >                    valid = false;
672 >                    break;
673 >                }
674 >            }
675 >            if (valid)
676 >                return -1;
677          }
678          lock.lock();
679          try {
# Line 716 | Line 726 | public class ReadMostlyVector<E> impleme
726              if (index < 0 || index >= count)
727                  throw new ArrayIndexOutOfBoundsException(index);
728              oldValue = array[index];
729 <            internalRemoveAt(index);
729 >            rawRemoveAt(index);
730          } finally {
731              lock.unlock();
732          }
# Line 728 | Line 738 | public class ReadMostlyVector<E> impleme
738          boolean removed;
739          lock.lock();
740          try {
741 <            removed = internalRemoveAt(rawIndexOf(o, 0, count));
741 >            removed = rawRemoveAt(rawIndexOf(o, 0, count));
742          } finally {
743              lock.unlock();
744          }
# Line 797 | Line 807 | public class ReadMostlyVector<E> impleme
807          lock.lock();
808          try {
809              if (rawIndexOf(e, 0, count) < 0) {
810 <                internalAdd(e);
810 >                rawAdd(e);
811                  added = true;
812              }
813              else
# Line 829 | Line 839 | public class ReadMostlyVector<E> impleme
839                  for (int i = 0; i < clen; ++i) {
840                      Object e = cs[i];
841                      if (rawIndexOf(e, 0, count) < 0) {
842 <                        internalAdd(e);
842 >                        rawAdd(e);
843                          ++added;
844                      }
845                  }
# Line 1256 | Line 1266 | public class ReadMostlyVector<E> impleme
1266              lock.lock();
1267              try {
1268                  int c = size;
1269 <                list.internalAddAt(c + offset, element);
1269 >                list.rawAddAt(c + offset, element);
1270                  size = c + 1;
1271              } finally {
1272                  lock.unlock();
# Line 1270 | Line 1280 | public class ReadMostlyVector<E> impleme
1280              try {
1281                  if (index < 0 || index > size)
1282                      throw new ArrayIndexOutOfBoundsException(index);
1283 <                list.internalAddAt(index + offset, element);
1283 >                list.rawAddAt(index + offset, element);
1284                  ++size;
1285              } finally {
1286                  lock.unlock();
# Line 1285 | Line 1295 | public class ReadMostlyVector<E> impleme
1295              try {
1296                  int s = size;
1297                  int pc = list.count;
1298 <                list.internalAddAllAt(offset + s, elements);
1298 >                list.rawAddAllAt(offset + s, elements);
1299                  added = list.count - pc;
1300                  size = s + added;
1301              } finally {
# Line 1304 | Line 1314 | public class ReadMostlyVector<E> impleme
1314                  if (index < 0 || index > s)
1315                      throw new ArrayIndexOutOfBoundsException(index);
1316                  int pc = list.count;
1317 <                list.internalAddAllAt(index + offset, elements);
1317 >                list.rawAddAllAt(index + offset, elements);
1318                  added = list.count - pc;
1319                  size = s + added;
1320              } finally {
# Line 1356 | Line 1366 | public class ReadMostlyVector<E> impleme
1366              Object[] items = list.array;
1367              int c = list.count;
1368              if (c <= items.length) {
1369 <                int idx = list.validatedIndexOf(o, items, offset, offset+size, seq);
1369 >                int idx = list.validatedIndexOf(o, items, offset,
1370 >                                                offset + size, seq);
1371                  if (lock.getSequence() == seq)
1372                      return idx < 0 ? -1 : idx - offset;
1373              }
1374              lock.lock();
1375              try {
1376 <                int idx = list.rawIndexOf(o, offset, offset+size);
1376 >                int idx = list.rawIndexOf(o, offset, offset + size);
1377                  return idx < 0 ? -1 : idx - offset;
1378              } finally {
1379                  lock.unlock();
# Line 1410 | Line 1421 | public class ReadMostlyVector<E> impleme
1421              SequenceLock lock = list.lock;
1422              lock.lock();
1423              try {
1424 <                if (index < 0 || index >= size)
1414 <                    throw new ArrayIndexOutOfBoundsException(index);
1424 >                Object[] items = list.array;
1425                  int i = index + offset;
1426 <                result = list.array[i];
1427 <                list.internalRemoveAt(i);
1426 >                if (index < 0 || index >= size || i >= items.length)
1427 >                    throw new ArrayIndexOutOfBoundsException(index);
1428 >                result = items[i];
1429 >                list.rawRemoveAt(i);
1430                  size--;
1431              } finally {
1432                  lock.unlock();
# Line 1427 | Line 1439 | public class ReadMostlyVector<E> impleme
1439              SequenceLock lock = list.lock;
1440              lock.lock();
1441              try {
1442 <                if (list.internalRemoveAt(list.rawIndexOf(o, offset,
1443 <                                                          offset + size))) {
1442 >                if (list.rawRemoveAt(list.rawIndexOf(o, offset,
1443 >                                                     offset + size))) {
1444                      removed = true;
1445                      --size;
1446                  }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines