src/share/classes/com/sun/tools/javac/util/Bits.java

changeset 2027
4932bb04c4b8
parent 1713
2ca9e7d50136
child 2525
2eb010b6cb22
child 2566
58e7e71b302e
     1.1 --- a/src/share/classes/com/sun/tools/javac/util/Bits.java	Sat Sep 14 15:23:21 2013 +0100
     1.2 +++ b/src/share/classes/com/sun/tools/javac/util/Bits.java	Sat Sep 14 19:04:47 2013 +0100
     1.3 @@ -27,8 +27,6 @@
     1.4  
     1.5  import java.util.Arrays;
     1.6  
     1.7 -import static com.sun.tools.javac.util.Bits.BitsOpKind.*;
     1.8 -
     1.9  /** A class for extensible, mutable bit sets.
    1.10   *
    1.11   *  <p><b>This is NOT part of any supported API.
    1.12 @@ -38,20 +36,6 @@
    1.13   */
    1.14  public class Bits {
    1.15  
    1.16 -    public enum BitsOpKind {
    1.17 -        INIT,
    1.18 -        CLEAR,
    1.19 -        INCL_BIT,
    1.20 -        EXCL_BIT,
    1.21 -        ASSIGN,
    1.22 -        AND_SET,
    1.23 -        OR_SET,
    1.24 -        DIFF_SET,
    1.25 -        XOR_SET,
    1.26 -        INCL_RANGE,
    1.27 -        EXCL_RANGE,
    1.28 -    }
    1.29 -
    1.30      //       ____________      reset    _________
    1.31      //      /  UNKNOWN   \   <-------- / UNINIT  \
    1.32      //      \____________/       |     \_________/
    1.33 @@ -64,11 +48,14 @@
    1.34      //                            |         |
    1.35      //                            -----------
    1.36      //                               any
    1.37 -    private enum BitsState {
    1.38 +    protected enum BitsState {
    1.39          /*  A Bits instance is in UNKNOWN state if it has been explicitly reset.
    1.40           *  It is possible to get to this state from any other by calling the
    1.41           *  reset method. An instance in the UNKNOWN state can pass to the
    1.42           *  NORMAL state after being assigned another Bits instance.
    1.43 +         *
    1.44 +         *  Bits instances are final fields in Flow so the UNKNOWN state models
    1.45 +         *  the null assignment.
    1.46           */
    1.47          UNKNOWN,
    1.48          /*  A Bits instance is in UNINIT when it is created with the default
    1.49 @@ -103,13 +90,9 @@
    1.50  
    1.51      public int[] bits = null;
    1.52      // This field will store last version of bits after every change.
    1.53 -    public int[] oldBits = null;
    1.54 -
    1.55 -    public BitsOpKind lastOperation = null;
    1.56 -
    1.57      private static final int[] unassignedBits = new int[0];
    1.58  
    1.59 -    private BitsState currentState;
    1.60 +    protected BitsState currentState;
    1.61  
    1.62      /** Construct an initially empty set.
    1.63       */
    1.64 @@ -127,27 +110,20 @@
    1.65  
    1.66      /** Construct a set consisting initially of given bit vector.
    1.67       */
    1.68 -    private Bits(int[] bits, BitsState initState) {
    1.69 +    protected Bits(int[] bits, BitsState initState) {
    1.70          this.bits = bits;
    1.71          this.currentState = initState;
    1.72          switch (initState) {
    1.73              case UNKNOWN:
    1.74 -                reset(); //this will also set current state;
    1.75 +                this.bits = null;
    1.76                  break;
    1.77              case NORMAL:
    1.78                  Assert.check(bits != unassignedBits);
    1.79 -                lastOperation = INIT;
    1.80                  break;
    1.81          }
    1.82      }
    1.83  
    1.84 -    /** This method will be called after any operation that causes a change to
    1.85 -     *  the bits. Subclasses can thus override it in order to extract information
    1.86 -     *  from the changes produced to the bits by the given operation.
    1.87 -     */
    1.88 -    public void changed() {}
    1.89 -
    1.90 -    private void sizeTo(int len) {
    1.91 +    protected void sizeTo(int len) {
    1.92          if (bits.length < len) {
    1.93              bits = Arrays.copyOf(bits, len);
    1.94          }
    1.95 @@ -157,16 +133,18 @@
    1.96       */
    1.97      public void clear() {
    1.98          Assert.check(currentState != BitsState.UNKNOWN);
    1.99 -        oldBits = bits;
   1.100 -        lastOperation = CLEAR;
   1.101 -        for (int i = 0; i < bits.length; i++) bits[i] = 0;
   1.102 -        changed();
   1.103 +        for (int i = 0; i < bits.length; i++) {
   1.104 +            bits[i] = 0;
   1.105 +        }
   1.106          currentState = BitsState.NORMAL;
   1.107      }
   1.108  
   1.109      public void reset() {
   1.110 +        internalReset();
   1.111 +    }
   1.112 +
   1.113 +    protected void internalReset() {
   1.114          bits = null;
   1.115 -        oldBits = null;
   1.116          currentState = BitsState.UNKNOWN;
   1.117      }
   1.118  
   1.119 @@ -175,40 +153,40 @@
   1.120      }
   1.121  
   1.122      public Bits assign(Bits someBits) {
   1.123 -        lastOperation = ASSIGN;
   1.124 -        oldBits = bits;
   1.125          bits = someBits.dup().bits;
   1.126 -        changed();
   1.127          currentState = BitsState.NORMAL;
   1.128          return this;
   1.129      }
   1.130  
   1.131      /** Return a copy of this set.
   1.132       */
   1.133 -    private Bits dup() {
   1.134 +    public Bits dup() {
   1.135          Assert.check(currentState != BitsState.UNKNOWN);
   1.136          Bits tmp = new Bits();
   1.137 -        if (currentState != BitsState.NORMAL) {
   1.138 -            tmp.bits = bits;
   1.139 -        } else {
   1.140 -            tmp.bits = new int[bits.length];
   1.141 -            System.arraycopy(bits, 0, tmp.bits, 0, bits.length);
   1.142 -        }
   1.143 +        tmp.bits = dupBits();
   1.144          currentState = BitsState.NORMAL;
   1.145          return tmp;
   1.146      }
   1.147  
   1.148 +    protected int[] dupBits() {
   1.149 +        int [] result;
   1.150 +        if (currentState != BitsState.NORMAL) {
   1.151 +            result = bits;
   1.152 +        } else {
   1.153 +            result = new int[bits.length];
   1.154 +            System.arraycopy(bits, 0, result, 0, bits.length);
   1.155 +        }
   1.156 +        return result;
   1.157 +    }
   1.158 +
   1.159      /** Include x in this set.
   1.160       */
   1.161      public void incl(int x) {
   1.162          Assert.check(currentState != BitsState.UNKNOWN);
   1.163 -        Assert.check(x >= 0);
   1.164 -        oldBits = bits;
   1.165 -        lastOperation = INCL_BIT;
   1.166 +        Assert.check(x >= 0, "Value of x " + x);
   1.167          sizeTo((x >>> wordshift) + 1);
   1.168          bits[x >>> wordshift] = bits[x >>> wordshift] |
   1.169              (1 << (x & wordmask));
   1.170 -        changed();
   1.171          currentState = BitsState.NORMAL;
   1.172      }
   1.173  
   1.174 @@ -217,14 +195,11 @@
   1.175       */
   1.176      public void inclRange(int start, int limit) {
   1.177          Assert.check(currentState != BitsState.UNKNOWN);
   1.178 -        oldBits = bits;
   1.179 -        lastOperation = INCL_RANGE;
   1.180          sizeTo((limit >>> wordshift) + 1);
   1.181          for (int x = start; x < limit; x++) {
   1.182              bits[x >>> wordshift] = bits[x >>> wordshift] |
   1.183                  (1 << (x & wordmask));
   1.184          }
   1.185 -        changed();
   1.186          currentState = BitsState.NORMAL;
   1.187      }
   1.188  
   1.189 @@ -232,13 +207,10 @@
   1.190       */
   1.191      public void excludeFrom(int start) {
   1.192          Assert.check(currentState != BitsState.UNKNOWN);
   1.193 -        oldBits = bits;
   1.194 -        lastOperation = EXCL_RANGE;
   1.195          Bits temp = new Bits();
   1.196          temp.sizeTo(bits.length);
   1.197          temp.inclRange(0, start);
   1.198          internalAndSet(temp);
   1.199 -        changed();
   1.200          currentState = BitsState.NORMAL;
   1.201      }
   1.202  
   1.203 @@ -247,12 +219,9 @@
   1.204      public void excl(int x) {
   1.205          Assert.check(currentState != BitsState.UNKNOWN);
   1.206          Assert.check(x >= 0);
   1.207 -        oldBits = bits;
   1.208 -        lastOperation = EXCL_BIT;
   1.209          sizeTo((x >>> wordshift) + 1);
   1.210          bits[x >>> wordshift] = bits[x >>> wordshift] &
   1.211              ~(1 << (x & wordmask));
   1.212 -        changed();
   1.213          currentState = BitsState.NORMAL;
   1.214      }
   1.215  
   1.216 @@ -269,15 +238,12 @@
   1.217       */
   1.218      public Bits andSet(Bits xs) {
   1.219          Assert.check(currentState != BitsState.UNKNOWN);
   1.220 -        oldBits = bits;
   1.221 -        lastOperation = AND_SET;
   1.222          internalAndSet(xs);
   1.223 -        changed();
   1.224          currentState = BitsState.NORMAL;
   1.225          return this;
   1.226      }
   1.227  
   1.228 -    private void internalAndSet(Bits xs) {
   1.229 +    protected void internalAndSet(Bits xs) {
   1.230          Assert.check(currentState != BitsState.UNKNOWN);
   1.231          sizeTo(xs.bits.length);
   1.232          for (int i = 0; i < xs.bits.length; i++) {
   1.233 @@ -289,13 +255,10 @@
   1.234       */
   1.235      public Bits orSet(Bits xs) {
   1.236          Assert.check(currentState != BitsState.UNKNOWN);
   1.237 -        oldBits = bits;
   1.238 -        lastOperation = OR_SET;
   1.239          sizeTo(xs.bits.length);
   1.240          for (int i = 0; i < xs.bits.length; i++) {
   1.241              bits[i] = bits[i] | xs.bits[i];
   1.242          }
   1.243 -        changed();
   1.244          currentState = BitsState.NORMAL;
   1.245          return this;
   1.246      }
   1.247 @@ -304,14 +267,11 @@
   1.248       */
   1.249      public Bits diffSet(Bits xs) {
   1.250          Assert.check(currentState != BitsState.UNKNOWN);
   1.251 -        oldBits = bits;
   1.252 -        lastOperation = DIFF_SET;
   1.253          for (int i = 0; i < bits.length; i++) {
   1.254              if (i < xs.bits.length) {
   1.255                  bits[i] = bits[i] & ~xs.bits[i];
   1.256              }
   1.257          }
   1.258 -        changed();
   1.259          currentState = BitsState.NORMAL;
   1.260          return this;
   1.261      }
   1.262 @@ -320,13 +280,10 @@
   1.263       */
   1.264      public Bits xorSet(Bits xs) {
   1.265          Assert.check(currentState != BitsState.UNKNOWN);
   1.266 -        oldBits = bits;
   1.267 -        lastOperation = XOR_SET;
   1.268          sizeTo(xs.bits.length);
   1.269          for (int i = 0; i < xs.bits.length; i++) {
   1.270              bits[i] = bits[i] ^ xs.bits[i];
   1.271          }
   1.272 -        changed();
   1.273          currentState = BitsState.NORMAL;
   1.274          return this;
   1.275      }
   1.276 @@ -336,7 +293,9 @@
   1.277       */
   1.278      private static int trailingZeroBits(int x) {
   1.279          Assert.check(wordlen == 32);
   1.280 -        if (x == 0) return 32;
   1.281 +        if (x == 0) {
   1.282 +            return 32;
   1.283 +        }
   1.284          int n = 1;
   1.285          if ((x & 0xffff) == 0) { n += 16; x >>>= 16; }
   1.286          if ((x & 0x00ff) == 0) { n +=  8; x >>>=  8; }
   1.287 @@ -355,24 +314,31 @@
   1.288      public int nextBit(int x) {
   1.289          Assert.check(currentState != BitsState.UNKNOWN);
   1.290          int windex = x >>> wordshift;
   1.291 -        if (windex >= bits.length) return -1;
   1.292 +        if (windex >= bits.length) {
   1.293 +            return -1;
   1.294 +        }
   1.295          int word = bits[windex] & ~((1 << (x & wordmask))-1);
   1.296          while (true) {
   1.297 -            if (word != 0)
   1.298 +            if (word != 0) {
   1.299                  return (windex << wordshift) + trailingZeroBits(word);
   1.300 +            }
   1.301              windex++;
   1.302 -            if (windex >= bits.length) return -1;
   1.303 +            if (windex >= bits.length) {
   1.304 +                return -1;
   1.305 +            }
   1.306              word = bits[windex];
   1.307          }
   1.308      }
   1.309  
   1.310      /** a string representation of this set.
   1.311       */
   1.312 +    @Override
   1.313      public String toString() {
   1.314 -        if (bits.length > 0) {
   1.315 +        if (bits != null && bits.length > 0) {
   1.316              char[] digits = new char[bits.length * wordlen];
   1.317 -            for (int i = 0; i < bits.length * wordlen; i++)
   1.318 +            for (int i = 0; i < bits.length * wordlen; i++) {
   1.319                  digits[i] = isMember(i) ? '1' : '0';
   1.320 +            }
   1.321              return new String(digits);
   1.322          } else {
   1.323              return "[]";
   1.324 @@ -396,6 +362,8 @@
   1.325              System.out.println("found " + i);
   1.326              count ++;
   1.327          }
   1.328 -        if (count != 125) throw new Error();
   1.329 +        if (count != 125) {
   1.330 +            throw new Error();
   1.331 +        }
   1.332      }
   1.333  }

mercurial