Package gg.xp.util

Class BitField

java.lang.Object
gg.xp.util.BitField

public class BitField extends Object
Efficiently store a mapping of int/long keys to boolean values by using a bitfield. Key set should ideally be contiguous. Each boolean only takes up a single bit, though the space needed is padded by 20% like ArrayList to avoid repeated append operations being expensive.

NOT thread safe! Concurrent reads are fine. Concurrent writes or reads+writes are not. This is for two reasons. First, if array expansion is necessary, any in-flight writes may end up writing to a "dead" array. Second, writes must do a read-modify-write, so are inherently unsafe if they lang within the same array element.

  • Constructor Details

    • BitField

      public BitField()
      Creates a BitField where all values are initially false
  • Method Details

    • fromLong

      public static BitField fromLong(Collection<Long> longs)
    • fromInts

      public static BitField fromInts(Collection<Integer> ints)
    • get

      public boolean get(int index)
    • get

      public boolean get(long index)
    • set

      public boolean set(int index, boolean value)
    • set

      public boolean set(long index, boolean value)
    • isValidIndex

      public boolean isValidIndex(long index)
    • assertValidIndex

      public static void assertValidIndex(long index)
    • ensureSize

      public void ensureSize(long index)