Skip to content

buffer

Buffer

Bases: FTyped[BufferFType[FT]], ABC

Abstract base class for buffer-like data structures. Buffers support random access, and can be resized. They are used to store data in a way that allows for efficient reading and writing of elements.

Source code in src/finch/finch_assembly/buffer.py
class Buffer(FTyped[BufferFType[FT]], ABC):
    """
    Abstract base class for buffer-like data structures. Buffers support random access,
    and can be resized. They are used to store data in a way that allows for efficient
    reading and writing of elements.
    """

    @abstractmethod
    def __init__(self, length: int, dtype: type): ...

    @abstractmethod
    def length(self):
        """
        Return the length of the buffer.
        """
        ...

    @property
    def element_type(self) -> FT:
        """
        Return the type of elements stored in the buffer.
        This is typically the same as the dtype used to create the buffer.
        """
        return self.ftype.element_type

    @property
    def length_type(self) -> FDTypeInteger:
        """
        Return the type of indices used to access elements in the buffer.
        This is typically an integer type.
        """
        return self.ftype.length_type

    @abstractmethod
    def load(self, idx: int): ...

    @abstractmethod
    def store(self, idx: int, val): ...

    @abstractmethod
    def to_numpy(self, *, copy: bool | None = None):
        """Return logical values as a NumPy array.

        ``copy=True`` returns independent storage, ``copy=False`` requires shared
        storage, and ``copy=None`` shares storage when supported and copies otherwise.
        """
        ...

    @property
    def owner(self):
        """Object retaining the storage, which may change on resize.

        Raw pointers must be reacquired after resize. Borrowing implementations
        retain the previous allocation for existing views when they detach.
        """
        return self

    @abstractmethod
    def resize(self, new_len: int):
        """
        Resize the buffer to the new length.
        """
        ...

element_type property

Return the type of elements stored in the buffer. This is typically the same as the dtype used to create the buffer.

length_type property

Return the type of indices used to access elements in the buffer. This is typically an integer type.

owner property

Object retaining the storage, which may change on resize.

Raw pointers must be reacquired after resize. Borrowing implementations retain the previous allocation for existing views when they detach.

length() abstractmethod

Return the length of the buffer.

Source code in src/finch/finch_assembly/buffer.py
@abstractmethod
def length(self):
    """
    Return the length of the buffer.
    """
    ...

resize(new_len) abstractmethod

Resize the buffer to the new length.

Source code in src/finch/finch_assembly/buffer.py
@abstractmethod
def resize(self, new_len: int):
    """
    Resize the buffer to the new length.
    """
    ...

to_numpy(*, copy=None) abstractmethod

Return logical values as a NumPy array.

copy=True returns independent storage, copy=False requires shared storage, and copy=None shares storage when supported and copies otherwise.

Source code in src/finch/finch_assembly/buffer.py
@abstractmethod
def to_numpy(self, *, copy: bool | None = None):
    """Return logical values as a NumPy array.

    ``copy=True`` returns independent storage, ``copy=False`` requires shared
    storage, and ``copy=None`` shares storage when supported and copies otherwise.
    """
    ...

BufferFType

Bases: FType, Generic[FT]

Abstract base class for the ftype of arguments. The ftype defines how the data structures store data, and can construct a data structure with the call method.

Source code in src/finch/finch_assembly/buffer.py
class BufferFType(FType, Generic[FT]):
    """
    Abstract base class for the ftype of arguments. The ftype defines how the
    data structures store data, and can construct a data structure with the call method.
    """

    @abstractmethod
    def __call__(self, *args, **kwargs):
        """
        Create an instance of an object in this ftype with the given arguments.
        """
        ...

    @property
    @abstractmethod
    def element_type(self) -> FT:
        """
        Return the type of elements stored in the buffer.
        This is typically the same as the dtype used to create the buffer.
        """
        ...

    @property
    def length_type(self) -> FDTypeInteger:
        """
        Returns the type used for the length of the buffer.
        """
        return intp

element_type abstractmethod property

Return the type of elements stored in the buffer. This is typically the same as the dtype used to create the buffer.

length_type property

Returns the type used for the length of the buffer.

__call__(*args, **kwargs) abstractmethod

Create an instance of an object in this ftype with the given arguments.

Source code in src/finch/finch_assembly/buffer.py
@abstractmethod
def __call__(self, *args, **kwargs):
    """
    Create an instance of an object in this ftype with the given arguments.
    """
    ...

length_type(arg)

The length type of the given argument. The length type is the type of the value returned by len(arg).

Parameters:

Name Type Description Default
arg FTyped | FType

The object to determine the length type for.

required

Returns:

Type Description
FDTypeInteger

The length type of the given object.

Raises:

Type Description
AttributeError

If the length type is not implemented for the given type.

Source code in src/finch/finch_assembly/buffer.py
def length_type(arg: FTyped | FType) -> FDTypeInteger:
    """The length type of the given argument. The length type is the type of
    the value returned by len(arg).

    Args:
        arg: The object to determine the length type for.

    Returns:
        The length type of the given object.

    Raises:
        AttributeError: If the length type is not implemented for the given type.
    """
    length_type: FDTypeInteger | None = getattr(arg, "length_type", None)
    if length_type is None:
        raise AttributeError(f"{type(arg).__name__} has no length_type")
    return length_type