Class FinalBitSet

java.lang.Object
com.oracle.truffle.api.utilities.FinalBitSet

public final class FinalBitSet extends Object
Read-only bitset designed for partial evaluation. The implementation is partially re-used from BitSet.
Since:
20.0
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final FinalBitSet
    An empty bit set of size 0.
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    Returns the number of bits set to true in this BitSet.
    boolean
    Compares this object against the specified object.
    boolean
    get(int bitIndex)
    Returns the value of the bit with the specified index.
    int
    Returns the hash code value for this bit set.
    boolean
    Returns true if this FinalBitSet contains no bits that are set to true.
    int
    Returns the "logical size" of this FinalBitSet: the index of the highest set bit in the FinalBitSet plus one.
    int
    nextClearBit(int fromIndex)
    Returns the index of the first bit that is set to false that occurs on or after the specified starting index.
    int
    nextSetBit(int fromIndex)
    Returns the index of the first bit that is set to true that occurs on or after the specified starting index.
    int
    Returns the number of bits of space actually in use by this FinalBitSet to represent bit values.
    long[]
    Returns a new long array containing all the bits in this bit set.
    Returns a string representation of this bit set.
    valueOf(long[] longs)
    Returns a new bit set containing all the bits in the given long array.
    valueOf(BitSet originalBitSet)
    Returns a new final bit set from a Java BitSet instance.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • EMPTY

      public static final FinalBitSet EMPTY
      An empty bit set of size 0.
      Since:
      20.0
  • Method Details

    • toLongArray

      public long[] toLongArray()
      Returns a new long array containing all the bits in this bit set. This method is designed for partial evaluation.

      More precisely, if
      long[] longs = s.toLongArray();
      then longs.length == (s.length()+63)/64 and
      s.get(n) == ((longs[n/64] & (1L<<(n%64))) != 0)
      for all n < 64 * longs.length.

      Returns:
      a long array containing a little-endian representation of all the bits in this bit set
      Since:
      20.0
    • get

      public boolean get(int bitIndex)
      Returns the value of the bit with the specified index. The value is true if the bit with the index bitIndex is currently set in this FinalBitSet; otherwise, the result is false. This method is designed for partial evaluation and is guaranteed to fold if the receiver and the bitIndex parameter are constant.
      Parameters:
      bitIndex - the bit index
      Returns:
      the value of the bit with the specified index
      Throws:
      IndexOutOfBoundsException - if the specified index is negative
      Since:
      20.0
    • length

      public int length()
      Returns the "logical size" of this FinalBitSet: the index of the highest set bit in the FinalBitSet plus one. Returns zero if the FinalBitSet contains no set bits. This method is designed for partial evaluation and is guaranteed to fold if the receiver is constant.
      Returns:
      the logical size of this FinalBitSet
      Since:
      20.0
    • isEmpty

      public boolean isEmpty()
      Returns true if this FinalBitSet contains no bits that are set to true. This method is designed for partial evaluation and is guaranteed to fold if the receiver is constant.
      Returns:
      boolean indicating whether this FinalBitSet is empty
      Since:
      20.0
    • equals

      public boolean equals(Object obj)
      Compares this object against the specified object. The result is true if and only if the argument is not null and is a FinalBitSet object that has exactly the same set of bits set to true as this bit set. That is, for every nonnegative int index k,
       ((BitSet) obj).get(k) == this.get(k)
       
      must be true. The current sizes of the two bit sets are not compared.
      Overrides:
      equals in class Object
      Parameters:
      obj - the object to compare with
      Returns:
      true if the objects are the same; false otherwise
      Since:
      20.0
      See Also:
    • hashCode

      public int hashCode()
      Returns the hash code value for this bit set. The hash code depends only on which bits are set within this FinalBitSet.

      The hash code is defined to be the result of the following calculation:

        
       public int hashCode() {
           long h = 1234;
           long[] words = toLongArray();
           for (int i = words.length; --i >= 0;)
               h ^= words[i] * (i + 1);
           return (int) ((h >> 32) ^ h);
       }
       
       
      Note that the hash code changes if the set of bits is altered.
      Overrides:
      hashCode in class Object
      Returns:
      the hash code value for this bit set
      Since:
      20.0
    • size

      public int size()
      Returns the number of bits of space actually in use by this FinalBitSet to represent bit values. The maximum element in the set is the size - 1st element. This method is designed for partial evaluation and is guaranteed to fold if the receiver is constant.
      Returns:
      the number of bits currently in this bit set
      Since:
      20.0
    • cardinality

      public int cardinality()
      Returns the number of bits set to true in this BitSet.
      Returns:
      the number of bits set to true in this BitSet
      Since:
      20.0
    • nextSetBit

      public int nextSetBit(int fromIndex)
      Returns the index of the first bit that is set to true that occurs on or after the specified starting index. If no such bit exists then -1 is returned.

      To iterate over the true bits in a FinalBitSet, use the following loop:

        
       for (int i = bs.nextSetBit(0); i >= 0; i = bs.nextSetBit(i + 1)) {
           // operate on index i here
           if (i == Integer.MAX_VALUE) {
               break; // or (i+1) would overflow
           }
       }
       
       
      Parameters:
      fromIndex - the index to start checking from (inclusive)
      Returns:
      the index of the next set bit, or -1 if there is no such bit
      Throws:
      IndexOutOfBoundsException - if the specified index is negative
      Since:
      20.0
    • nextClearBit

      public int nextClearBit(int fromIndex)
      Returns the index of the first bit that is set to false that occurs on or after the specified starting index.
      Parameters:
      fromIndex - the index to start checking from (inclusive)
      Returns:
      the index of the next clear bit
      Throws:
      IndexOutOfBoundsException - if the specified index is negative
      Since:
      20.0
    • toString

      public String toString()
      Returns a string representation of this bit set. For every index for which this BitSet contains a bit in the set state, the decimal representation of that index is included in the result. Such indices are listed in order from lowest to highest, separated by ", " (a comma and a space) and surrounded by braces, resulting in the usual mathematical notation for a set of integers.
      Overrides:
      toString in class Object
      Returns:
      a string representation of this bit set
      Since:
      20.0
      See Also:
    • valueOf

      public static FinalBitSet valueOf(long[] longs)
      Returns a new bit set containing all the bits in the given long array.

      More precisely,
      FinalBitSet.valueOf(longs).get(n) == ((longs[n/64] & (1L<<(n%64))) != 0)
      for all n < 64 * longs.length.

      Parameters:
      longs - a long array containing a little-endian representation of a sequence of bits to be used as the initial bits of the new bit set
      Returns:
      a FinalBitSet containing all the bits in the long array
      Since:
      20.0
    • valueOf

      public static FinalBitSet valueOf(BitSet originalBitSet)
      Returns a new final bit set from a Java BitSet instance. The original bit set will be copied.
      Since:
      20.0