ExVrp.DynamicBitset (ExVrp v0.4.2)

Copy Markdown View Source

A dynamic bitset for fast membership checks on integers.

This is useful for tracking which clients are in a solution, which routes have been visited, etc. The bitset is immutable - all operations return a new bitset.

The size is rounded up to the nearest multiple of 64 bits.

Example

bitset = ExVrp.DynamicBitset.new(128)
bitset = ExVrp.DynamicBitset.set(bitset, 0, true)
bitset = ExVrp.DynamicBitset.set(bitset, 64, true)
ExVrp.DynamicBitset.count(bitset)  # => 2

Summary

Functions

Returns true if all bits are set.

Returns true if any bit is set.

Bitwise AND of two bitsets. Returns a new bitset.

Bitwise NOT of a bitset. Returns a new bitset.

Bitwise OR of two bitsets. Returns a new bitset.

Bitwise XOR of two bitsets. Returns a new bitset.

Returns the number of set bits.

Checks if two bitsets are equal.

Gets the bit at the given index.

Creates a new bitset with the given number of bits.

Returns true if no bits are set.

Resets all bits to 0. Returns a new bitset.

Sets the bit at the given index. Returns a new bitset.

Sets all bits to 1. Returns a new bitset.

Returns the size (length) of the bitset.

Types

t()

@type t() :: reference()

Functions

all?(bitset)

@spec all?(t()) :: boolean()

Returns true if all bits are set.

any?(bitset)

@spec any?(t()) :: boolean()

Returns true if any bit is set.

bit_and(a, b)

@spec bit_and(t(), t()) :: t()

Bitwise AND of two bitsets. Returns a new bitset.

bit_not(bitset)

@spec bit_not(t()) :: t()

Bitwise NOT of a bitset. Returns a new bitset.

bit_or(a, b)

@spec bit_or(t(), t()) :: t()

Bitwise OR of two bitsets. Returns a new bitset.

Example

result = ExVrp.DynamicBitset.bit_or(bitset1, bitset2)

bit_xor(a, b)

@spec bit_xor(t(), t()) :: t()

Bitwise XOR of two bitsets. Returns a new bitset.

count(bitset)

@spec count(t()) :: non_neg_integer()

Returns the number of set bits.

equal?(a, b)

@spec equal?(t(), t()) :: boolean()

Checks if two bitsets are equal.

get(bitset, idx)

@spec get(t(), non_neg_integer()) :: boolean()

Gets the bit at the given index.

Example

ExVrp.DynamicBitset.get(bitset, 0)  # => false

new(num_bits)

@spec new(non_neg_integer()) :: t()

Creates a new bitset with the given number of bits.

The actual size is rounded up to the nearest multiple of 64.

Example

bitset = ExVrp.DynamicBitset.new(128)

none?(bitset)

@spec none?(t()) :: boolean()

Returns true if no bits are set.

reset_all(bitset)

@spec reset_all(t()) :: t()

Resets all bits to 0. Returns a new bitset.

set(bitset, idx, value)

@spec set(t(), non_neg_integer(), boolean()) :: t()

Sets the bit at the given index. Returns a new bitset.

Example

bitset = ExVrp.DynamicBitset.set(bitset, 0, true)

set_all(bitset)

@spec set_all(t()) :: t()

Sets all bits to 1. Returns a new bitset.

size(bitset)

@spec size(t()) :: non_neg_integer()

Returns the size (length) of the bitset.

This is the actual allocated size (rounded up to 64-bit blocks).