[cvs] / jsr166 / src / main / java / util / Vector.java Repository:
ViewVC logotype

Diff of /jsr166/src/main/java/util/Vector.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.32, Sat Nov 12 20:51:59 2016 UTC revision 1.33, Sun Nov 13 02:10:09 2016 UTC
# Line 759  Line 759 
759          return (E) elementData[index];          return (E) elementData[index];
760      }      }
761    
762        @SuppressWarnings("unchecked")
763        static <E> E elementAt(Object[] es, int index) {
764            return (E) es[index];
765        }
766    
767      /**      /**
768       * Returns the element at the specified position in this Vector.       * Returns the element at the specified position in this Vector.
769       *       *
# Line 984  Line 989 
989          return bulkRemove(filter);          return bulkRemove(filter);
990      }      }
991    
992      @SuppressWarnings("unchecked")      // A tiny bit set implementation
993    
994        private static long[] nBits(int n) {
995            return new long[((n - 1) >> 6) + 1];
996        }
997        private static void setBit(long[] bits, int i) {
998            bits[i >> 6] |= 1L << i;
999        }
1000        private static boolean isClear(long[] bits, int i) {
1001            return (bits[i >> 6] & (1L << i)) == 0;
1002        }
1003    
1004      private synchronized boolean bulkRemove(Predicate<? super E> filter) {      private synchronized boolean bulkRemove(Predicate<? super E> filter) {
1005          int expectedModCount = modCount;          int expectedModCount = modCount;
1006          final Object[] es = elementData;          final Object[] es = elementData;
1007          final int size = elementCount;          final int end = elementCount;
1008          final boolean modified;          final boolean modified;
1009          int r;          int i;
1010          // Optimize for initial run of survivors          // Optimize for initial run of survivors
1011          for (r = 0; r < size && !filter.test((E) es[r]); r++)          for (i = 0; i < end && !filter.test(elementAt(es, i)); i++)
1012              ;              ;
1013          if (modified = (r < size)) {          // Tolerate predicates that reentrantly access the collection for
1014            // read (but writers still get CME), so traverse once to find
1015            // elements to delete, a second pass to physically expunge.
1016            if (modified = (i < end)) {
1017              expectedModCount++;              expectedModCount++;
1018              modCount++;              modCount++;
1019              int w = r++;              final int beg = i;
1020              try {              final long[] deathRow = nBits(end - beg);
1021                  for (E e; r < size; r++)              deathRow[0] = 1L;   // set bit 0
1022                      if (!filter.test(e = (E) es[r]))              for (i = beg + 1; i < end; i++)
1023                          es[w++] = e;                  if (filter.test(elementAt(es, i)))
1024              } catch (Throwable ex) {                      setBit(deathRow, i - beg);
1025                  // copy remaining elements              int w = beg;
1026                  System.arraycopy(es, r, es, w, size - r);              for (i = beg; i < end; i++)
1027                  w += size - r;                  if (isClear(deathRow, i - beg))
1028                  throw ex;                      es[w++] = es[i];
1029              } finally {              Arrays.fill(es, elementCount = w, end, null);
                 Arrays.fill(es, elementCount = w, size, null);  
             }  
1030          }          }
1031          if (modCount != expectedModCount)          if (modCount != expectedModCount)
1032              throw new ConcurrentModificationException();              throw new ConcurrentModificationException();

Legend:
Removed from v.1.32  
changed lines
  Added in v.1.33

Doug Lea
ViewVC Help
Powered by ViewVC 1.0.8