langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/util/Bits.java
author vromero
Wed, 18 Feb 2015 17:07:06 -0800
changeset 29054 310b8028d7df
parent 26537 026471c1a12b
permissions -rw-r--r--
8068489: remove unnecessary complexity in Flow and Bits, after JDK-8064857 Reviewed-by: mcimadamore, jjg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
29054
310b8028d7df 8068489: remove unnecessary complexity in Flow and Bits, after JDK-8064857
vromero
parents: 26537
diff changeset
     2
 * Copyright (c) 1999, 2015, Oracle and/or its affiliates. All rights reserved.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    20
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
    23
 * questions.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
package com.sun.tools.javac.util;
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
14049
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents: 13844
diff changeset
    28
import java.util.Arrays;
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents: 13844
diff changeset
    29
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
/** A class for extensible, mutable bit sets.
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
    32
 *  <p><b>This is NOT part of any supported API.
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
    33
 *  If you write code that depends on this, you do so at your own risk.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
 *  This code and its internal interfaces are subject to change or
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
 *  deletion without notice.</b>
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
public class Bits {
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
17282
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
    39
    //       ____________      reset    _________
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
    40
    //      /  UNKNOWN   \   <-------- / UNINIT  \
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
    41
    //      \____________/       |     \_________/
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
    42
    //            |              |          |
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
    43
    //            |assign        |          | any
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
    44
    //            |        ___________      |
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
    45
    //            ------> /  NORMAL   \ <----
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
    46
    //                    \___________/     |
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
    47
    //                            |         |
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
    48
    //                            |         |
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
    49
    //                            -----------
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
    50
    //                               any
19941
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents: 17282
diff changeset
    51
    protected enum BitsState {
17282
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
    52
        /*  A Bits instance is in UNKNOWN state if it has been explicitly reset.
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
    53
         *  It is possible to get to this state from any other by calling the
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
    54
         *  reset method. An instance in the UNKNOWN state can pass to the
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
    55
         *  NORMAL state after being assigned another Bits instance.
19941
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents: 17282
diff changeset
    56
         *
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents: 17282
diff changeset
    57
         *  Bits instances are final fields in Flow so the UNKNOWN state models
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents: 17282
diff changeset
    58
         *  the null assignment.
17282
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
    59
         */
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
    60
        UNKNOWN,
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
    61
        /*  A Bits instance is in UNINIT when it is created with the default
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
    62
         *  constructor but it isn't explicitly reset. The main objective of this
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
    63
         *  internal state is to save some memory.
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
    64
         */
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
    65
        UNINIT,
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
    66
        /*  The normal state is reached after creating a Bits instance from an
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
    67
         *  existing one or after applying any operation to an instance on UNINIT
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
    68
         *  or NORMAL state. From this state a bits instance can pass to the
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
    69
         *  UNKNOWN state by calling the reset method.
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
    70
         */
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
    71
        NORMAL;
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
    72
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
    73
        static BitsState getState(int[] someBits, boolean reset) {
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
    74
            if (reset) {
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
    75
                return UNKNOWN;
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
    76
            } else {
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
    77
                if (someBits != unassignedBits) {
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
    78
                    return NORMAL;
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
    79
                } else {
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
    80
                    return UNINIT;
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
    81
                }
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
    82
            }
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
    83
        }
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
    84
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
    85
    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
    private final static int wordlen = 32;
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
    private final static int wordshift = 5;
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
    private final static int wordmask = wordlen - 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
17282
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
    91
    public int[] bits = null;
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
    92
    // This field will store last version of bits after every change.
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
    93
    private static final int[] unassignedBits = new int[0];
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
    94
19941
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents: 17282
diff changeset
    95
    protected BitsState currentState;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
    /** Construct an initially empty set.
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
    public Bits() {
17282
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
   100
        this(false);
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
   101
    }
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
   102
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
   103
    public Bits(Bits someBits) {
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
   104
        this(someBits.dup().bits, BitsState.getState(someBits.bits, false));
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
   105
    }
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
   106
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
   107
    public Bits(boolean reset) {
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
   108
        this(unassignedBits, BitsState.getState(unassignedBits, reset));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
    /** Construct a set consisting initially of given bit vector.
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
     */
19941
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents: 17282
diff changeset
   113
    protected Bits(int[] bits, BitsState initState) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
        this.bits = bits;
17282
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
   115
        this.currentState = initState;
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
   116
        switch (initState) {
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
   117
            case UNKNOWN:
19941
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents: 17282
diff changeset
   118
                this.bits = null;
17282
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
   119
                break;
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
   120
            case NORMAL:
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
   121
                Assert.check(bits != unassignedBits);
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
   122
                break;
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
   123
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
19941
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents: 17282
diff changeset
   126
    protected void sizeTo(int len) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
        if (bits.length < len) {
14049
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents: 13844
diff changeset
   128
            bits = Arrays.copyOf(bits, len);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
    /** This set = {}.
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
    public void clear() {
17282
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
   135
        Assert.check(currentState != BitsState.UNKNOWN);
19941
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents: 17282
diff changeset
   136
        for (int i = 0; i < bits.length; i++) {
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents: 17282
diff changeset
   137
            bits[i] = 0;
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents: 17282
diff changeset
   138
        }
17282
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
   139
        currentState = BitsState.NORMAL;
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
   140
    }
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
   141
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
   142
    public void reset() {
19941
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents: 17282
diff changeset
   143
        internalReset();
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents: 17282
diff changeset
   144
    }
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents: 17282
diff changeset
   145
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents: 17282
diff changeset
   146
    protected void internalReset() {
17282
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
   147
        bits = null;
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
   148
        currentState = BitsState.UNKNOWN;
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
   149
    }
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
   150
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
   151
    public boolean isReset() {
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
   152
        return currentState == BitsState.UNKNOWN;
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
   153
    }
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
   154
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
   155
    public Bits assign(Bits someBits) {
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
   156
        bits = someBits.dup().bits;
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
   157
        currentState = BitsState.NORMAL;
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
   158
        return this;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
    /** Return a copy of this set.
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
     */
19941
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents: 17282
diff changeset
   163
    public Bits dup() {
17282
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
   164
        Assert.check(currentState != BitsState.UNKNOWN);
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
   165
        Bits tmp = new Bits();
19941
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents: 17282
diff changeset
   166
        tmp.bits = dupBits();
17282
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
   167
        currentState = BitsState.NORMAL;
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
   168
        return tmp;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
19941
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents: 17282
diff changeset
   171
    protected int[] dupBits() {
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents: 17282
diff changeset
   172
        int [] result;
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents: 17282
diff changeset
   173
        if (currentState != BitsState.NORMAL) {
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents: 17282
diff changeset
   174
            result = bits;
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents: 17282
diff changeset
   175
        } else {
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents: 17282
diff changeset
   176
            result = new int[bits.length];
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents: 17282
diff changeset
   177
            System.arraycopy(bits, 0, result, 0, bits.length);
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents: 17282
diff changeset
   178
        }
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents: 17282
diff changeset
   179
        return result;
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents: 17282
diff changeset
   180
    }
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents: 17282
diff changeset
   181
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
    /** Include x in this set.
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
    public void incl(int x) {
17282
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
   185
        Assert.check(currentState != BitsState.UNKNOWN);
25848
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 22689
diff changeset
   186
        Assert.check(x >= 0);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
        sizeTo((x >>> wordshift) + 1);
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
        bits[x >>> wordshift] = bits[x >>> wordshift] |
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
            (1 << (x & wordmask));
17282
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
   190
        currentState = BitsState.NORMAL;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
    /** Include [start..limit) in this set.
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
    public void inclRange(int start, int limit) {
17282
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
   197
        Assert.check(currentState != BitsState.UNKNOWN);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
        sizeTo((limit >>> wordshift) + 1);
17282
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
   199
        for (int x = start; x < limit; x++) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
            bits[x >>> wordshift] = bits[x >>> wordshift] |
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
                (1 << (x & wordmask));
17282
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
   202
        }
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
   203
        currentState = BitsState.NORMAL;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
7621
9f2901d1f92f 7003744: Compiler error concerning final variables
mcimadamore
parents: 5847
diff changeset
   206
    /** Exclude [start...end] from this set.
9f2901d1f92f 7003744: Compiler error concerning final variables
mcimadamore
parents: 5847
diff changeset
   207
     */
9f2901d1f92f 7003744: Compiler error concerning final variables
mcimadamore
parents: 5847
diff changeset
   208
    public void excludeFrom(int start) {
17282
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
   209
        Assert.check(currentState != BitsState.UNKNOWN);
7621
9f2901d1f92f 7003744: Compiler error concerning final variables
mcimadamore
parents: 5847
diff changeset
   210
        Bits temp = new Bits();
9f2901d1f92f 7003744: Compiler error concerning final variables
mcimadamore
parents: 5847
diff changeset
   211
        temp.sizeTo(bits.length);
9f2901d1f92f 7003744: Compiler error concerning final variables
mcimadamore
parents: 5847
diff changeset
   212
        temp.inclRange(0, start);
17282
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
   213
        internalAndSet(temp);
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
   214
        currentState = BitsState.NORMAL;
7621
9f2901d1f92f 7003744: Compiler error concerning final variables
mcimadamore
parents: 5847
diff changeset
   215
    }
9f2901d1f92f 7003744: Compiler error concerning final variables
mcimadamore
parents: 5847
diff changeset
   216
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   217
    /** Exclude x from this set.
06bc494ca11e Initial load
duke
parents:
diff changeset
   218
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   219
    public void excl(int x) {
17282
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
   220
        Assert.check(currentState != BitsState.UNKNOWN);
8032
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents: 7681
diff changeset
   221
        Assert.check(x >= 0);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   222
        sizeTo((x >>> wordshift) + 1);
06bc494ca11e Initial load
duke
parents:
diff changeset
   223
        bits[x >>> wordshift] = bits[x >>> wordshift] &
06bc494ca11e Initial load
duke
parents:
diff changeset
   224
            ~(1 << (x & wordmask));
17282
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
   225
        currentState = BitsState.NORMAL;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   226
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   227
06bc494ca11e Initial load
duke
parents:
diff changeset
   228
    /** Is x an element of this set?
06bc494ca11e Initial load
duke
parents:
diff changeset
   229
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   230
    public boolean isMember(int x) {
17282
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
   231
        Assert.check(currentState != BitsState.UNKNOWN);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   232
        return
06bc494ca11e Initial load
duke
parents:
diff changeset
   233
            0 <= x && x < (bits.length << wordshift) &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   234
            (bits[x >>> wordshift] & (1 << (x & wordmask))) != 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   235
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   236
13844
56339cf983a3 7177970: fix issues in langtools doc comments
jjg
parents: 8032
diff changeset
   237
    /** {@literal this set = this set & xs}.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   238
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   239
    public Bits andSet(Bits xs) {
17282
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
   240
        Assert.check(currentState != BitsState.UNKNOWN);
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
   241
        internalAndSet(xs);
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
   242
        currentState = BitsState.NORMAL;
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
   243
        return this;
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
   244
    }
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
   245
19941
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents: 17282
diff changeset
   246
    protected void internalAndSet(Bits xs) {
17282
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
   247
        Assert.check(currentState != BitsState.UNKNOWN);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   248
        sizeTo(xs.bits.length);
17282
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
   249
        for (int i = 0; i < xs.bits.length; i++) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   250
            bits[i] = bits[i] & xs.bits[i];
17282
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
   251
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   252
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   253
06bc494ca11e Initial load
duke
parents:
diff changeset
   254
    /** this set = this set | xs.
06bc494ca11e Initial load
duke
parents:
diff changeset
   255
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   256
    public Bits orSet(Bits xs) {
17282
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
   257
        Assert.check(currentState != BitsState.UNKNOWN);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   258
        sizeTo(xs.bits.length);
17282
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
   259
        for (int i = 0; i < xs.bits.length; i++) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   260
            bits[i] = bits[i] | xs.bits[i];
17282
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
   261
        }
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
   262
        currentState = BitsState.NORMAL;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   263
        return this;
06bc494ca11e Initial load
duke
parents:
diff changeset
   264
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   265
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
    /** this set = this set \ xs.
06bc494ca11e Initial load
duke
parents:
diff changeset
   267
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   268
    public Bits diffSet(Bits xs) {
17282
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
   269
        Assert.check(currentState != BitsState.UNKNOWN);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   270
        for (int i = 0; i < bits.length; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   271
            if (i < xs.bits.length) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   272
                bits[i] = bits[i] & ~xs.bits[i];
06bc494ca11e Initial load
duke
parents:
diff changeset
   273
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   274
        }
17282
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
   275
        currentState = BitsState.NORMAL;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   276
        return this;
06bc494ca11e Initial load
duke
parents:
diff changeset
   277
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   278
06bc494ca11e Initial load
duke
parents:
diff changeset
   279
    /** this set = this set ^ xs.
06bc494ca11e Initial load
duke
parents:
diff changeset
   280
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   281
    public Bits xorSet(Bits xs) {
17282
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
   282
        Assert.check(currentState != BitsState.UNKNOWN);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   283
        sizeTo(xs.bits.length);
17282
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
   284
        for (int i = 0; i < xs.bits.length; i++) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   285
            bits[i] = bits[i] ^ xs.bits[i];
17282
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
   286
        }
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
   287
        currentState = BitsState.NORMAL;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   288
        return this;
06bc494ca11e Initial load
duke
parents:
diff changeset
   289
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   290
06bc494ca11e Initial load
duke
parents:
diff changeset
   291
    /** Count trailing zero bits in an int. Algorithm from "Hacker's
06bc494ca11e Initial load
duke
parents:
diff changeset
   292
     *  Delight" by Henry S. Warren Jr. (figure 5-13)
06bc494ca11e Initial load
duke
parents:
diff changeset
   293
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   294
    private static int trailingZeroBits(int x) {
8032
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents: 7681
diff changeset
   295
        Assert.check(wordlen == 32);
19941
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents: 17282
diff changeset
   296
        if (x == 0) {
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents: 17282
diff changeset
   297
            return 32;
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents: 17282
diff changeset
   298
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   299
        int n = 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   300
        if ((x & 0xffff) == 0) { n += 16; x >>>= 16; }
06bc494ca11e Initial load
duke
parents:
diff changeset
   301
        if ((x & 0x00ff) == 0) { n +=  8; x >>>=  8; }
06bc494ca11e Initial load
duke
parents:
diff changeset
   302
        if ((x & 0x000f) == 0) { n +=  4; x >>>=  4; }
06bc494ca11e Initial load
duke
parents:
diff changeset
   303
        if ((x & 0x0003) == 0) { n +=  2; x >>>=  2; }
06bc494ca11e Initial load
duke
parents:
diff changeset
   304
        return n - (x&1);
06bc494ca11e Initial load
duke
parents:
diff changeset
   305
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   306
13844
56339cf983a3 7177970: fix issues in langtools doc comments
jjg
parents: 8032
diff changeset
   307
    /** Return the index of the least bit position &ge; x that is set.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   308
     *  If none are set, returns -1.  This provides a nice way to iterate
06bc494ca11e Initial load
duke
parents:
diff changeset
   309
     *  over the members of a bit set:
13844
56339cf983a3 7177970: fix issues in langtools doc comments
jjg
parents: 8032
diff changeset
   310
     *  <pre>{@code
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   311
     *  for (int i = bits.nextBit(0); i>=0; i = bits.nextBit(i+1)) ...
13844
56339cf983a3 7177970: fix issues in langtools doc comments
jjg
parents: 8032
diff changeset
   312
     *  }</pre>
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   313
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   314
    public int nextBit(int x) {
17282
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
   315
        Assert.check(currentState != BitsState.UNKNOWN);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   316
        int windex = x >>> wordshift;
19941
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents: 17282
diff changeset
   317
        if (windex >= bits.length) {
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents: 17282
diff changeset
   318
            return -1;
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents: 17282
diff changeset
   319
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   320
        int word = bits[windex] & ~((1 << (x & wordmask))-1);
06bc494ca11e Initial load
duke
parents:
diff changeset
   321
        while (true) {
19941
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents: 17282
diff changeset
   322
            if (word != 0) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   323
                return (windex << wordshift) + trailingZeroBits(word);
19941
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents: 17282
diff changeset
   324
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   325
            windex++;
19941
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents: 17282
diff changeset
   326
            if (windex >= bits.length) {
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents: 17282
diff changeset
   327
                return -1;
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents: 17282
diff changeset
   328
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   329
            word = bits[windex];
06bc494ca11e Initial load
duke
parents:
diff changeset
   330
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   331
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   332
06bc494ca11e Initial load
duke
parents:
diff changeset
   333
    /** a string representation of this set.
06bc494ca11e Initial load
duke
parents:
diff changeset
   334
     */
19941
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents: 17282
diff changeset
   335
    @Override
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   336
    public String toString() {
19941
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents: 17282
diff changeset
   337
        if (bits != null && bits.length > 0) {
17282
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
   338
            char[] digits = new char[bits.length * wordlen];
19941
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents: 17282
diff changeset
   339
            for (int i = 0; i < bits.length * wordlen; i++) {
17282
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
   340
                digits[i] = isMember(i) ? '1' : '0';
19941
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents: 17282
diff changeset
   341
            }
17282
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
   342
            return new String(digits);
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
   343
        } else {
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
   344
            return "[]";
c6964ad7df63 8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents: 14049
diff changeset
   345
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   346
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   347
06bc494ca11e Initial load
duke
parents:
diff changeset
   348
}