Class BitSets


  • public final class BitSets
    extends java.lang.Object
    Utility functions for BitSet.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      private static class  BitSets.Closure
      Setup equivalence Sets for each position.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private BitSets()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.util.SortedMap<java.lang.Integer,​java.util.BitSet> closure​(java.util.SortedMap<java.lang.Integer,​java.util.BitSet> equivalence)
      Computes the closure of a map from integers to bits.
      static boolean contains​(java.util.BitSet set0, java.util.BitSet set1)
      Returns true if all bits set in the second parameter are also set in the first.
      static boolean contains​(java.util.BitSet set0, ImmutableBitSet set1)
      Returns true if all bits set in the second parameter are also set in the first.
      static java.util.BitSet of​(int... bits)
      Creates a bitset with given bits set.
      static java.util.BitSet of​(java.lang.Integer[] bits)
      Creates a BitSet with given bits set.
      static java.util.BitSet of​(java.lang.Iterable<? extends java.lang.Number> bits)
      Creates a BitSet with given bits set.
      static java.util.BitSet of​(ImmutableIntList bits)
      Creates a BitSet with given bits set.
      static void populate​(java.util.BitSet bitSet, java.lang.Iterable<? extends java.lang.Number> list)
      Populates a BitSet from an iterable, such as a list of integer.
      static void populate​(java.util.BitSet bitSet, ImmutableIntList list)
      Populates a BitSet from an ImmutableIntList.
      static int previousClearBit​(java.util.BitSet bitSet, int fromIndex)
      Returns the previous clear bit.
      static java.util.BitSet range​(int toIndex)
      Creates a BitSet with bits between 0 and toIndex set.
      static java.util.BitSet range​(int fromIndex, int toIndex)
      Creates a bitset with bits from fromIndex (inclusive) to specified toIndex (exclusive) set to true.
      static void setAll​(java.util.BitSet bitSet, java.lang.Iterable<? extends java.lang.Number> list)
      Sets all bits in a given BitSet corresponding to integers from a list.
      static int[] toArray​(java.util.BitSet bitSet)
      Converts a BitSet to an array.
      static java.lang.Iterable<java.lang.Integer> toIter​(java.util.BitSet bitSet)
      Returns an iterable over the bits in a bitmap that are set to '1'.
      static java.lang.Iterable<java.lang.Integer> toIter​(ImmutableBitSet bitSet)  
      static java.util.List<java.lang.Integer> toList​(java.util.BitSet bitSet)
      Converts a bitset to a list.
      static java.util.BitSet union​(java.util.BitSet set0, java.util.BitSet... sets)
      Returns a BitSet that is the union of the given BitSets.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • BitSets

        private BitSets()
    • Method Detail

      • contains

        public static boolean contains​(java.util.BitSet set0,
                                       java.util.BitSet set1)
        Returns true if all bits set in the second parameter are also set in the first. In other words, whether x is a super-set of y.
        Parameters:
        set0 - Containing bitmap
        set1 - Bitmap to be checked
        Returns:
        Whether all bits in set1 are set in set0
      • contains

        public static boolean contains​(java.util.BitSet set0,
                                       ImmutableBitSet set1)
        Returns true if all bits set in the second parameter are also set in the first. In other words, whether x is a super-set of y.
        Parameters:
        set0 - Containing bitmap
        set1 - Bitmap to be checked
        Returns:
        Whether all bits in set1 are set in set0
      • toIter

        public static java.lang.Iterable<java.lang.Integer> toIter​(java.util.BitSet bitSet)
        Returns an iterable over the bits in a bitmap that are set to '1'.

        This allows you to iterate over a bit set using a 'foreach' construct. For instance:

        BitSet bitSet;
        for (int i : Util.toIter(bitSet)) {
          print(i);
        }
        Parameters:
        bitSet - Bit set
        Returns:
        Iterable
      • toIter

        public static java.lang.Iterable<java.lang.Integer> toIter​(ImmutableBitSet bitSet)
      • toList

        public static java.util.List<java.lang.Integer> toList​(java.util.BitSet bitSet)
        Converts a bitset to a list.

        The list is mutable, and future changes to the list do not affect the contents of the bit set.

        Parameters:
        bitSet - Bit set
        Returns:
        List of set bits
      • toArray

        public static int[] toArray​(java.util.BitSet bitSet)
        Converts a BitSet to an array.
        Parameters:
        bitSet - Bit set
        Returns:
        Array of set bits
      • of

        public static java.util.BitSet of​(int... bits)
        Creates a bitset with given bits set.

        For example, of(0, 3) returns a bit set with bits {0, 3} set.

        Parameters:
        bits - Array of bits to set
        Returns:
        Bit set
      • of

        public static java.util.BitSet of​(java.lang.Integer[] bits)
        Creates a BitSet with given bits set.

        For example, of(new Integer[] {0, 3}) returns a bit set with bits {0, 3} set.

        Parameters:
        bits - Array of bits to set
        Returns:
        Bit set
      • of

        public static java.util.BitSet of​(java.lang.Iterable<? extends java.lang.Number> bits)
        Creates a BitSet with given bits set.

        For example, of(Arrays.asList(0, 3)) returns a bit set with bits {0, 3} set.

        Parameters:
        bits - Collection of bits to set
        Returns:
        Bit set
      • of

        public static java.util.BitSet of​(ImmutableIntList bits)
        Creates a BitSet with given bits set.

        For example, of(ImmutableIntList.of(0, 3)) returns a bit set with bits {0, 3} set.

        Parameters:
        bits - Collection of bits to set
        Returns:
        Bit set
      • range

        public static java.util.BitSet range​(int fromIndex,
                                             int toIndex)
        Creates a bitset with bits from fromIndex (inclusive) to specified toIndex (exclusive) set to true.

        For example, range(0, 3) returns a bit set with bits {0, 1, 2} set.

        Parameters:
        fromIndex - Index of the first bit to be set.
        toIndex - Index after the last bit to be set.
        Returns:
        Bit set
      • range

        public static java.util.BitSet range​(int toIndex)
        Creates a BitSet with bits between 0 and toIndex set.
      • setAll

        public static void setAll​(java.util.BitSet bitSet,
                                  java.lang.Iterable<? extends java.lang.Number> list)
        Sets all bits in a given BitSet corresponding to integers from a list.
      • union

        public static java.util.BitSet union​(java.util.BitSet set0,
                                             java.util.BitSet... sets)
        Returns a BitSet that is the union of the given BitSets. Does not modify any of the inputs.
      • previousClearBit

        public static int previousClearBit​(java.util.BitSet bitSet,
                                           int fromIndex)
        Returns the previous clear bit.

        Has same behavior as BitSet.previousClearBit(int), but that method does not exist before 1.7.

      • closure

        public static java.util.SortedMap<java.lang.Integer,​java.util.BitSet> closure​(java.util.SortedMap<java.lang.Integer,​java.util.BitSet> equivalence)
        Computes the closure of a map from integers to bits.

        The input must have an entry for each position.

        Does not modify the input map or its bit sets.

      • populate

        public static void populate​(java.util.BitSet bitSet,
                                    java.lang.Iterable<? extends java.lang.Number> list)
        Populates a BitSet from an iterable, such as a list of integer.