Package gg.xp.util
Class BitField
java.lang.Object
gg.xp.util.BitField
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic void
assertValidIndex
(long index) void
ensureSize
(long index) static BitField
fromInts
(Collection<Integer> ints) static BitField
fromLong
(Collection<Long> longs) boolean
get
(int index) boolean
get
(long index) boolean
isValidIndex
(long index) boolean
set
(int index, boolean value) boolean
set
(long index, boolean value)
-
Constructor Details
-
BitField
public BitField()Creates a BitField where all values are initially false
-
-
Method Details
-
fromLong
-
fromInts
-
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)
-