Skip to content

sparse_bytemap_level

SparseByteMapLevel dataclass

Bases: SingleDimensionLevel

Byte map with sorted, zero-based packed child positions in srt.

Each child position is parent * dimension + index; ptr groups these positions by parent, and tbl marks the stored children. An occupied parent p owns srt[ptr[p]:ptr[p + 1]], while an unoccupied parent's ptr[p] is -1. While assembling, srt[:qos_fill] lists the stored children in insertion order, and qos_stop is its capacity.

Source code in src/finch/tensor/level/sparse_bytemap_level.py
@dataclass
class SparseByteMapLevel(SingleDimensionLevel):
    """Byte map with sorted, zero-based packed child positions in ``srt``.

    Each child position is ``parent * dimension + index``; ``ptr`` groups
    these positions by parent, and ``tbl`` marks the stored children. An
    occupied parent ``p`` owns ``srt[ptr[p]:ptr[p + 1]]``, while an unoccupied
    parent's ``ptr[p]`` is -1. While assembling, ``srt[:qos_fill]`` lists the
    stored children in insertion order, and ``qos_stop`` is its capacity.
    """

    lvl: Level
    dimension: np.integer
    ptr: Any | None = None
    tbl: Any | None = None
    srt: Any | None = None
    qos_fill: Any = None
    qos_stop: Any = None

    def __post_init__(self) -> None:
        if self.ptr is None:
            self.ptr = self.lvl.buffer_factory(self.lvl.position_type)(1)
        if self.tbl is None:
            self.tbl = self.lvl.buffer_factory(ftypes.bool_)(0)
        if self.srt is None:
            self.srt = self.lvl.buffer_factory(self.lvl.position_type)(0)
        p_t = self.lvl.position_type
        self.qos_fill = p_t(
            self.srt.length() if self.qos_fill is None else self.qos_fill
        )
        self.qos_stop = p_t(
            self.srt.length() if self.qos_stop is None else self.qos_stop
        )

    @property
    def shape(self) -> tuple:
        return (self.dimension, *self.lvl.shape)

    @property
    def ftype(self) -> SparseByteMapLevelFType:
        return SparseByteMapLevelFType(  # type: ignore[abstract]
            self.lvl.ftype,
            ftype(self.dimension),
            ftype(self.ptr),
            ftype(self.tbl),
            ftype(self.srt),
        )

    @property
    def val(self) -> Any:
        return self.lvl.val

    def __str__(self) -> str:
        return f"SparseByteMapLevel(lvl={self.lvl}, dim={self.dimension})"