|
| 1 | +from cpython cimport Py_buffer |
| 2 | +from libcpp.vector cimport vector |
| 3 | + |
| 4 | +# define a Cython array wrapper class to hold a C++ vector of ints, adhering to numpy's buffer protocol: |
| 5 | +cdef class ArrayWrapper_int: |
| 6 | + # constructor and destructor are fairly unimportant now since |
| 7 | + # vec will be destroyed automatically. |
| 8 | + |
| 9 | + def __cinit__(self, vector[int]& data): |
| 10 | + self.vec.swap(data) |
| 11 | + self.view_count = 0 |
| 12 | + |
| 13 | + # now implement the buffer protocol for the class |
| 14 | + # which makes it generally useful to anything that expects an array |
| 15 | + def __getbuffer__(self, Py_buffer *buffer, int flags): |
| 16 | + # relevant documentation http://cython.readthedocs.io/en/latest/src/userguide/buffer.html#a-matrix-class |
| 17 | + cdef Py_ssize_t itemsize = sizeof(self.vec[0]) |
| 18 | + |
| 19 | + self.shape[1] = self.vec.size() |
| 20 | + self.shape[0] = 1 |
| 21 | + self.strides[1] = <Py_ssize_t>( <char *>&(self.vec[1]) - <char *>&(self.vec[0])) |
| 22 | + self.strides[0] = self.vec.size() * self.strides[1] |
| 23 | + buffer.buf = <char *>&(self.vec[0]) |
| 24 | + buffer.format = 'i' |
| 25 | + buffer.internal = NULL |
| 26 | + buffer.itemsize = itemsize |
| 27 | + buffer.len = self.vec.size() * itemsize # product(shape) * itemsize |
| 28 | + buffer.ndim = 2 |
| 29 | + buffer.obj = self |
| 30 | + buffer.readonly = 0 |
| 31 | + buffer.shape = self.shape |
| 32 | + buffer.strides = self.strides |
| 33 | + buffer.suboffsets = NULL |
| 34 | + self.view_count += 1 |
| 35 | + |
| 36 | + def __releasebuffer__(self, Py_buffer *buffer): |
| 37 | + self.view_count -= 1 |
| 38 | + |
| 39 | + |
| 40 | +# define a Cython array wrapper class to hold a C++ vector of doubles, adhering to numpy's buffer protocol: |
| 41 | +cdef class ArrayWrapper_double: |
| 42 | + # constructor and destructor are fairly unimportant now since |
| 43 | + # vec will be destroyed automatically. |
| 44 | + |
| 45 | + def __cinit__(self, vector[double]& data): |
| 46 | + self.vec.swap(data) |
| 47 | + self.view_count = 0 |
| 48 | + |
| 49 | + # now implement the buffer protocol for the class |
| 50 | + # which makes it generally useful to anything that expects an array |
| 51 | + def __getbuffer__(self, Py_buffer *buffer, int flags): |
| 52 | + # relevant documentation http://cython.readthedocs.io/en/latest/src/userguide/buffer.html#a-matrix-class |
| 53 | + cdef Py_ssize_t itemsize = sizeof(self.vec[0]) |
| 54 | + |
| 55 | + self.shape[1] = self.vec.size() |
| 56 | + self.shape[0] = 1 |
| 57 | + self.strides[1] = <Py_ssize_t>( <char *>&(self.vec[1]) - <char *>&(self.vec[0])) |
| 58 | + self.strides[0] = self.vec.size() * self.strides[1] |
| 59 | + buffer.buf = <char *>&(self.vec[0]) |
| 60 | + buffer.format = 'd' |
| 61 | + buffer.internal = NULL |
| 62 | + buffer.itemsize = itemsize |
| 63 | + buffer.len = self.vec.size() * itemsize # product(shape) * itemsize |
| 64 | + buffer.ndim = 2 |
| 65 | + buffer.obj = self |
| 66 | + buffer.readonly = 0 |
| 67 | + buffer.shape = self.shape |
| 68 | + buffer.strides = self.strides |
| 69 | + buffer.suboffsets = NULL |
| 70 | + self.view_count += 1 |
| 71 | + |
| 72 | + def __releasebuffer__(self, Py_buffer *buffer): |
| 73 | + self.view_count -= 1 |
0 commit comments