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 vs. the global index (for iterating over the bytes contained in the list) run in opposing directions!

The toBitStringView 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 memDumpView.

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.

Link copied to clipboard

Functions

Link copied to clipboard
fun BitSet.clear(index: Int)
fun BitSet.clear(index: Long)

clears the bit at index (shorthand for this[index] = false)

fun BitSet.clear(indexes: LongRange)

clears the bits in indexes (shorthand for this[indexes] = false)

fun BitSet.clear(fromIndex: Long, toIndex: Long)

clears all bits from fromIndex (inclusive) to toIndex (exclusive)

Link copied to clipboard
fun copyOf(): BitSet

Creates a deep copy of the current BitSet instance.

Link copied to clipboard
@JvmName(name = "encodeAllPemBlocksToPem")
fun Iterable<PemBlock>.encodeAllToPem(): String
@JvmName(name = "encodeAllPemEncodablesToPem")
fun Iterable<PemEncodable>.encodeAllToPem(): String
Link copied to clipboard
open operator override fun equals(other: Any?): Boolean
Link copied to clipboard
fun flip(index: Long)
fun flip(indexes: LongRange)
Link copied to clipboard
fun BitSet.flip(index: Int)
fun BitSet.flip(fromIndex: Long, toIndex: Long)
Link copied to clipboard
inline fun forEachIndexed(action: (i: Long, it: Boolean) -> Unit)

Iterates over each bit in the BitSet and invokes the provided action for every index and corresponding bit value.

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

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 override fun hashCode(): Int
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
open operator override fun iterator(): Iterator<Boolean>

returns an iterator over bits. use bytes.iterator() to iterate over bytes. modifying the BitSet invalidates the iterator, causing undefined behavior.

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

Returns the first bit set to true from fromIndex (= inclusive).

Link copied to clipboard

Returns the first bit set to true after index (= exclusive).

Link copied to clipboard
operator fun set(index: Int, value: Boolean)
operator fun set(index: Long, value: Boolean)

Sets the bit at index to value

operator fun set(indexes: IntRange, value: Boolean)

operator fun set(indexes: LongRange, value: Boolean)

Sets the bits in indexes to value

Link copied to clipboard
fun BitSet.set(index: Int)

sets the bit at index (short-hand for this[index] = true)

fun BitSet.set(index: Long)

sets the bit at index (shorthand for this[index] = true)

fun BitSet.set(indexes: LongRange)

sets the bits in indexes (shorthand for this[indexes] = true)

fun BitSet.set(fromIndex: Long, toIndex: Long)

sets all bits from fromIndex (inclusive) to toIndex (exclusive)

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. Within each byte, index 0 is the least significant bit, while index 7 is the most significant bit. (This matches Java's java.util.BitSet::toByteArray.)

Link copied to clipboard
Link copied to clipboard

Re-allocates the internal buffer to not consume any memory beyond what is necessary to store the bits. This trims any spare capacity, and should only be used if the BitSet is not expected to grow further, and memory is a concern. If the set might grow again, this is almost certainly not worth it.