summaryrefslogtreecommitdiff
path: root/docs/src/tutorial/array.rst
blob: fb255cf26f5405886c01e398784f1c26f9d10b00 (plain)
1
2
3
4
5
6
7
8
9
10
11
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
40
41
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
.. _array-array:

==========================
Working with Python arrays
==========================

.. include::
    ../two-syntax-variants-used

Python has a builtin array module supporting dynamic 1-dimensional arrays of
primitive types. It is possible to access the underlying C array of a Python
array from within Cython. At the same time they are ordinary Python objects
which can be stored in lists and serialized between processes when using
:obj:`multiprocessing`.

Compared to the manual approach with :c:func:`malloc` and :c:func:`free`, this
gives the safe and automatic memory management of Python, and compared to a
Numpy array there is no need to install a dependency, as the :obj:`array`
module is built into both Python and Cython.

Safe usage with memory views
----------------------------


.. tabs::
    .. group-tab:: Pure Python

        .. literalinclude:: ../../examples/tutorial/array/safe_usage.py

    .. group-tab:: Cython

        .. literalinclude:: ../../examples/tutorial/array/safe_usage.pyx


NB: the import brings the regular Python array object into the namespace
while the cimport adds functions accessible from Cython.

A Python array is constructed with a type signature and sequence of
initial values. For the possible type signatures, refer to the Python
documentation for the `array module <https://docs.python.org/library/array.html>`_.

Notice that when a Python array is assigned to a variable typed as
memory view, there will be a slight overhead to construct the memory
view. However, from that point on the variable can be passed to other
functions without overhead, so long as it is typed:


.. tabs::
    .. group-tab:: Pure Python

        .. literalinclude:: ../../examples/tutorial/array/overhead.py

    .. group-tab:: Cython

        .. literalinclude:: ../../examples/tutorial/array/overhead.pyx


Zero-overhead, unsafe access to raw C pointer
---------------------------------------------
To avoid any overhead and to be able to pass a C pointer to other
functions, it is possible to access the underlying contiguous array as a
pointer. There is no type or bounds checking, so be careful to use the
right type and signedness.


.. tabs::
    .. group-tab:: Pure Python

        .. literalinclude:: ../../examples/tutorial/array/unsafe_usage.py

    .. group-tab:: Cython

        .. literalinclude:: ../../examples/tutorial/array/unsafe_usage.pyx


Note that any length-changing operation on the array object may invalidate the
pointer.


Cloning, extending arrays
-------------------------
To avoid having to use the array constructor from the Python module,
it is possible to create a new array with the same type as a template,
and preallocate a given number of elements. The array is initialized to
zero when requested.


.. tabs::
    .. group-tab:: Pure Python

        .. literalinclude:: ../../examples/tutorial/array/clone.py

    .. group-tab:: Cython

        .. literalinclude:: ../../examples/tutorial/array/clone.pyx


An array can also be extended and resized; this avoids repeated memory
reallocation which would occur if elements would be appended or removed
one by one.


.. tabs::
    .. group-tab:: Pure Python

        .. literalinclude:: ../../examples/tutorial/array/resize.py

    .. group-tab:: Cython

        .. literalinclude:: ../../examples/tutorial/array/resize.pyx


API reference
-------------

Data fields
~~~~~~~~~~~

::

    data.as_voidptr
    data.as_chars
    data.as_schars
    data.as_uchars
    data.as_shorts
    data.as_ushorts
    data.as_ints
    data.as_uints
    data.as_longs
    data.as_ulongs
    data.as_longlongs  # requires Python >=3
    data.as_ulonglongs  # requires Python >=3
    data.as_floats
    data.as_doubles
    data.as_pyunicodes

Direct access to the underlying contiguous C array, with given type;
e.g., ``myarray.data.as_ints``.


Functions
~~~~~~~~~
The following functions are available to Cython from the array module

.. tabs::
    .. group-tab:: Pure Python

        .. code-block:: python

            @cython.cfunc
            @cython.exceptval(-1)
            def resize(self: array.array, n: cython.Py_ssize_t) -> cython.int

    .. group-tab:: Cython

        .. code-block:: cython

            cdef int resize(array.array self, Py_ssize_t n) except -1

Fast resize / realloc. Not suitable for repeated, small increments; resizes
underlying array to exactly the requested amount.

----

.. tabs::
    .. group-tab:: Pure Python

        .. code-block:: python

            @cython.cfunc
            @cython.exceptval(-1)
            def resize_smart(self: array.array, n: cython.Py_ssize_t) -> cython.int

    .. group-tab:: Cython

        .. code-block:: cython

            cdef int resize_smart(array.array self, Py_ssize_t n) except -1

Efficient for small increments; uses growth pattern that delivers
amortized linear-time appends.

----

.. tabs::
    .. group-tab:: Pure Python

        .. code-block:: python

            @cython.cfunc
            @cython.inline
            def clone(template: array.array, length: cython.Py_ssize_t, zero: cython.bint) -> array.array

    .. group-tab:: Cython

        .. code-block:: cython

            cdef inline array.array clone(array.array template, Py_ssize_t length, bint zero)


Fast creation of a new array, given a template array. Type will be same as
``template``. If zero is ``True``, new array will be initialized with zeroes.

----

.. tabs::
    .. group-tab:: Pure Python

        .. code-block:: python

            @cython.cfunc
            @cython.inline
            def copy(self: array.array) -> array.array

    .. group-tab:: Cython

        .. code-block:: cython

            cdef inline array.array copy(array.array self)

Make a copy of an array.

----

.. tabs::
    .. group-tab:: Pure Python

        .. code-block:: python

            @cython.cfunc
            @cython.inline
            @cython.exceptval(-1)
            def extend_buffer(self: array.array, stuff: cython.p_char, n: cython.Py_ssize_t) -> cython.int

    .. group-tab:: Cython

        .. code-block:: cython

            cdef inline int extend_buffer(array.array self, char* stuff, Py_ssize_t n) except -1

Efficient appending of new data of same type (e.g. of same array type)
``n``: number of elements (not number of bytes!)

----

.. tabs::
    .. group-tab:: Pure Python

        .. code-block:: python

            @cython.cfunc
            @cython.inline
            @cython.exceptval(-1)
            def extend(self: array.array, other: array.array) -> cython.int

    .. group-tab:: Cython

        .. code-block:: cython

            cdef inline int extend(array.array self, array.array other) except -1

Extend array with data from another array; types must match.

----

.. tabs::
    .. group-tab:: Pure Python

        .. code-block:: python

            @cython.cfunc
            @cython.inline
            def zero(self: array.array) -> cython.void

    .. group-tab:: Cython

        .. code-block:: cython

            cdef inline void zero(array.array self)

Set all elements of array to zero.