BitSet

@Serializable(with = BitSetSerializer::class)
class BitSet : Iterable<Boolean> (source)

Pure Kotlin Bit Set created by throwing a bunch of extension functions at a MutableList<Byte>. As a mental model: this BitSet grows from left to right, just like writing a text.

Note: The in-byte bit index and the global index (for iterating over the bytes contained in the list) run in opposing directions!

The toBitString function print our the bits as they are accessible, disregarding byte-alignment and memory layout:

val bitSet = BitSet()
bitSet[0] = true //1 (ByteArray representation: [1])
bitSet[2] = true //101 (ByteArray representation: [5])
bitSet[8] = true //10100000 1 (ByteArray representation: [5,1])

To inspect the actual memory layout of the underlying bytes (i.e. the result of calling toByteArray), use memDump.

Implements Iterable over bits. Use bytes to iterate over bytes

Constructors

Link copied to clipboard
constructor(nbits: Long = 0)

Preallocates a buffer capable of holding nbits many bits

Types

Link copied to clipboard
object Companion

Properties

Link copied to clipboard

List view on the bytes backing this bit set. Changes to the bytes directly affect this bitset.

Functions

Link copied to clipboard
open operator override fun equals(other: Any?): Boolean
Link copied to clipboard
inline fun forEachIndexed(block: (i: Long, it: Boolean) -> Unit)

This is the real deal, as it has Long indices

Link copied to clipboard
operator fun get(index: Long): Boolean

Returns the bit at index. Never throws an exception when index>=0, as getting a bit outside the underlying bytes' bounds returns false.

Link copied to clipboard
open operator override fun iterator(): Iterator<Boolean>

returns an iterator over bits. use bytes.iterator() to iterate over bytes

Link copied to clipboard
fun length(): Long

Current length of the bitset.

Link copied to clipboard

Returns a binary representation of this bit set's memory layout, when packed into a byte array Bytes are separated by a single space. An empty byte array results in an empty string.

Link copied to clipboard
fun nextSetBit(fromIndex: Long): Long

return the next bit set to true following fromIndex

Link copied to clipboard
fun set(index: Long)

shorthand for set(index,true)

operator fun set(index: Long, value: Boolean)

Sets the bit at index to value

Link copied to clipboard

Returns all bits as they are accessible by the global bit index

Link copied to clipboard

Allocates a fresh byte array and writes the values of this bitset's underlying bytes to it

Link copied to clipboard
open override fun toString(): String