cana.function_outputs.bit_lut module

Bit-packed dense Boolean look-up tables.

class cana.function_outputs.bit_lut.BitLut(k=0, *, fill=0)[source]

Bases: object

Dense Boolean truth table packed 8 entries per byte.

Row i (input state number, matching statenum_to_binstate()) lives in bit (i & 7) of byte i >> 3 (LSB-first within each byte). Indexing and iteration yield int values in {0, 1}.

Parameters:
  • k (int)

  • fill (int)

classmethod from_sequence(seq)[source]

Build a BitLut from any 0/1 sequence of length 2**k.

Parameters:

seq (Sequence[object])

Return type:

BitLut

classmethod from_bytes(data, k)[source]

Build from packed bytes with the same layout as to_bytes().

Parameters:
  • data (bytes | bytearray)

  • k (int)

Return type:

BitLut

property k: int

Number of inputs (len(self) == 2**k, or 1 when k == 0).

eval(statenum)[source]

Return the output for input state number statenum.

Parameters:

statenum (int)

Return type:

int

to_list()[source]

Return an unpacked list of 0/1 ints.

Return type:

List[int]

to_bytes()[source]

Return a copy of the packed byte buffer.

Return type:

bytes

fill(value)[source]

Set every row to 0 or 1.

Parameters:

value (object)

Return type:

None

is_constant()[source]

Return 0 or 1 if every row equals that value, else None.

Return type:

int | None

popcount()[source]

Number of rows equal to 1.

Return type:

int

negate_input(idx)[source]

Return a copy with input bit idx negated (MSB-first bit order).

Row i of the result equals row i ^ (1 << (k - 1 - idx)) of self, matching the legacy negate_LUT_input remapping.

Parameters:

idx (int)

Return type:

BitLut

lut_as_str()[source]

Return a multi-line string of binstate : output rows.

Return type:

str

reorder(old_labels, new_labels)[source]

Return a copy with input-bit order remapped from old_labels to new_labels.

Labels identify the k input variables in MSB-first bit order. new_labels must be a permutation of old_labels. If the orders already match, returns self unchanged.

Parameters:
  • old_labels (Sequence[object])

  • new_labels (Sequence[object])

Return type:

BitLut

classmethod reorder_sequence(lut, old_labels, new_labels)[source]

Like reorder(), but accepts any 0/1 sequence or BitLut.

Parameters:
  • lut (Sequence[object])

  • old_labels (Sequence[object])

  • new_labels (Sequence[object])

Return type:

BitLut

cana.function_outputs.bit_lut.refuse_dense_lut_k(k, *, via)[source]

Raise ValueError when materializing a dense BitLut would be too large.

Parameters:
  • k (int)

  • via (str)

Return type:

None