Commit 4b9ae426 by Gael varoquaux

Cosmit

parent ac4f4190
...@@ -33,33 +33,35 @@ cdef class ArrayWrapper: ...@@ -33,33 +33,35 @@ cdef class ArrayWrapper:
cdef int size cdef int size
cdef set_data(self, int size, void* data_ptr): cdef set_data(self, int size, void* data_ptr):
""" Constructor for the class. """ Set the data of the array
Mallocs a memory buffer of size (n*sizeof(int)) and sets up This cannot be done in the constructor as it must recieve C-level
the numpy array. arguments.
Parameters: Parameters:
----------- -----------
n -- Length of the array. size: int
Length of the array.
Data attributes: data_ptr: void*
---------------- Pointer to the data
data -- Pointer to an integer array.
alloc -- Size of the data buffer allocated.
""" """
self.data_ptr = data_ptr self.data_ptr = data_ptr
self.size = size self.size = size
def __array__(self): def __array__(self):
""" Here we use the __array__ method, that is called when numpy
tries to get an array from the object."""
cdef np.npy_intp shape[1] cdef np.npy_intp shape[1]
shape[0] = <np.npy_intp> self.size shape[0] = <np.npy_intp> self.size
# Create a 1D array, of length 'size'
ndarray = np.PyArray_SimpleNewFromData(1, shape, ndarray = np.PyArray_SimpleNewFromData(1, shape,
np.NPY_INT, self.data_ptr) np.NPY_INT, self.data_ptr)
return ndarray return ndarray
def __dealloc__(self): def __dealloc__(self):
""" Frees the array. """ """ Frees the array. This is called by Python when all the
references to the object are gone. """
free(<void*>self.data_ptr) free(<void*>self.data_ptr)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment