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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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 resize(self, 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.

length() abstractmethod

Return the length of the buffer.

Source code in src/finch/finch_assembly/buffer.py
52
53
54
55
56
57
@abstractmethod
def length(self):
    """
    Return the length of the buffer.
    """
    ...

resize(len) abstractmethod

Resize the buffer to the new length.

Source code in src/finch/finch_assembly/buffer.py
81
82
83
84
85
86
@abstractmethod
def resize(self, len: int):
    """
    Resize the buffer to the new length.
    """
    ...

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
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
18
19
20
21
22
23
@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
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
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