Class ThreadSafeBitSet


  • public class ThreadSafeBitSet
    extends Object
    This is a lock-free, thread-safe version of a BitSet.

    Instead of a long array to hold the bits, this implementation uses an AtomicLongArray, then does the appropriate compare-and-swap operations when setting the bits.

    Author:
    dkoszewnik
    • Field Detail

      • DEFAULT_LOG2_SEGMENT_SIZE_IN_BITS

        public static final int DEFAULT_LOG2_SEGMENT_SIZE_IN_BITS
        See Also:
        Constant Field Values
    • Constructor Detail

      • ThreadSafeBitSet

        public ThreadSafeBitSet()
      • ThreadSafeBitSet

        public ThreadSafeBitSet​(int log2SegmentSizeInBits)
      • ThreadSafeBitSet

        public ThreadSafeBitSet​(int log2SegmentSizeInBits,
                                int numBitsToPreallocate)
    • Method Detail

      • set

        public void set​(int position)
      • setUntil

        public void setUntil​(int position)
      • clear

        public void clear​(int position)
      • get

        public boolean get​(int position)
      • maxSetBit

        public long maxSetBit()
      • nextSetBit

        public int nextSetBit​(int fromIndex)
      • cardinality

        public int cardinality()
        Returns:
        the number of bits which are set in this bit set.
      • getWords

        public long[] getWords()
      • currentCapacity

        public int currentCapacity()
        Returns:
        the number of bits which are current specified by this bit set. This is the maximum value to which you might need to iterate, if you were to iterate over all bits in this set.
      • clearAll

        public void clearAll()
        Clear all bits to 0.
      • andNot

        public ThreadSafeBitSet andNot​(ThreadSafeBitSet other)
        Return a new bit set which contains all bits which are contained in this bit set, and which are NOT contained in the other bit set.

        In other words, return a new bit set, which is a bitwise and with the bitwise not of the other bit set.

        Parameters:
        other - the other bit set
        Returns:
        the resulting bit set
      • orAll

        public static ThreadSafeBitSet orAll​(ThreadSafeBitSet... bitSets)
        Return a new bit set which contains all bits which are contained in *any* of the specified bit sets.
        Parameters:
        bitSets - the other bit sets
        Returns:
        the resulting bit set
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object
      • toBitSet

        public BitSet toBitSet()
        Returns:
        a new BitSet with same bits set