|
| 1 | +#include <Python.h> |
| 2 | + |
| 3 | +#define PY_ARRAY_UNIQUE_SYMBOL asciidtype_ARRAY_API |
| 4 | +#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION |
| 5 | +#define NO_IMPORT_ARRAY |
| 6 | +#include "numpy/arrayobject.h" |
| 7 | +#include "numpy/experimental_dtype_api.h" |
| 8 | +#include "numpy/ndarraytypes.h" |
| 9 | + |
| 10 | +#include "casts.h" |
| 11 | +#include "dtype.h" |
| 12 | + |
| 13 | +static NPY_CASTING |
| 14 | +ascii_to_ascii_resolve_descriptors(PyObject *NPY_UNUSED(self), |
| 15 | + PyArray_DTypeMeta *NPY_UNUSED(dtypes[2]), |
| 16 | + PyArray_Descr *given_descrs[2], |
| 17 | + PyArray_Descr *loop_descrs[2], |
| 18 | + npy_intp *view_offset) |
| 19 | +{ |
| 20 | + Py_INCREF(given_descrs[0]); |
| 21 | + loop_descrs[0] = given_descrs[0]; |
| 22 | + if (given_descrs[1] == NULL) { |
| 23 | + Py_INCREF(given_descrs[0]); |
| 24 | + loop_descrs[1] = given_descrs[0]; |
| 25 | + } |
| 26 | + else { |
| 27 | + Py_INCREF(given_descrs[1]); |
| 28 | + loop_descrs[1] = given_descrs[0]; |
| 29 | + } |
| 30 | + |
| 31 | + if (((ASCIIDTypeObject *)loop_descrs[0])->size == |
| 32 | + ((ASCIIDTypeObject *)loop_descrs[1])->size) { |
| 33 | + *view_offset = 0; |
| 34 | + return NPY_NO_CASTING; |
| 35 | + } |
| 36 | + |
| 37 | + return NPY_SAME_KIND_CASTING; |
| 38 | +} |
| 39 | + |
| 40 | +static int |
| 41 | +ascii_to_ascii_contiguous(PyArrayMethod_Context *context, char *const data[], |
| 42 | + npy_intp const dimensions[], |
| 43 | + npy_intp const NPY_UNUSED(strides[]), |
| 44 | + NpyAuxData *NPY_UNUSED(auxdata)) |
| 45 | +{ |
| 46 | + PyArray_Descr **descrs = context->descriptors; |
| 47 | + // for contiguous assignment the sizes of the two dtypes should be |
| 48 | + // the same, consider adding an assert to check? |
| 49 | + long size = ((ASCIIDTypeObject *)descrs[0])->size; |
| 50 | + |
| 51 | + npy_intp N = dimensions[0] * size; |
| 52 | + char *in = data[0]; |
| 53 | + char *out = data[1]; |
| 54 | + |
| 55 | + while (N--) { |
| 56 | + *out = *in; |
| 57 | + out++; |
| 58 | + in++; |
| 59 | + } |
| 60 | + |
| 61 | + return 0; |
| 62 | +} |
| 63 | + |
| 64 | +static int |
| 65 | +ascii_to_ascii_strided_or_unaligned(PyArrayMethod_Context *context, |
| 66 | + char *const data[], |
| 67 | + npy_intp const dimensions[], |
| 68 | + npy_intp const strides[], |
| 69 | + NpyAuxData *NPY_UNUSED(auxdata)) |
| 70 | +{ |
| 71 | + PyArray_Descr **descrs = context->descriptors; |
| 72 | + long in_size = ((ASCIIDTypeObject *)descrs[0])->size; |
| 73 | + long out_size = ((ASCIIDTypeObject *)descrs[1])->size; |
| 74 | + long copy_size; |
| 75 | + |
| 76 | + if (out_size > in_size) { |
| 77 | + copy_size = in_size; |
| 78 | + } |
| 79 | + else { |
| 80 | + copy_size = out_size; |
| 81 | + } |
| 82 | + |
| 83 | + npy_intp N = dimensions[0]; |
| 84 | + char *in = data[0]; |
| 85 | + char *out = data[1]; |
| 86 | + npy_intp in_stride = strides[0]; |
| 87 | + npy_intp out_stride = strides[1]; |
| 88 | + |
| 89 | + while (N--) { |
| 90 | + memcpy(out, in, out_size * sizeof(char)); // NOLINT |
| 91 | + for (int i = copy_size; i < out_size; i++) { |
| 92 | + *(out + i) = '\0'; |
| 93 | + } |
| 94 | + in += in_stride; |
| 95 | + out += out_stride; |
| 96 | + } |
| 97 | + |
| 98 | + return 0; |
| 99 | +} |
| 100 | + |
| 101 | +static int |
| 102 | +ascii_to_ascii_get_loop(PyArrayMethod_Context *context, int aligned, |
| 103 | + int NPY_UNUSED(move_references), |
| 104 | + const npy_intp *strides, |
| 105 | + PyArrayMethod_StridedLoop **out_loop, |
| 106 | + NpyAuxData **NPY_UNUSED(out_transferdata), |
| 107 | + NPY_ARRAYMETHOD_FLAGS *flags) |
| 108 | +{ |
| 109 | + PyArray_Descr **descrs = context->descriptors; |
| 110 | + |
| 111 | + int contig = (strides[0] == ((ASCIIDTypeObject *)descrs[0])->size * |
| 112 | + sizeof(char) && |
| 113 | + strides[1] == ((ASCIIDTypeObject *)descrs[1])->size * |
| 114 | + sizeof(char)); |
| 115 | + |
| 116 | + if (aligned && contig) { |
| 117 | + *out_loop = (PyArrayMethod_StridedLoop *)&ascii_to_ascii_contiguous; |
| 118 | + } |
| 119 | + else { |
| 120 | + *out_loop = (PyArrayMethod_StridedLoop |
| 121 | + *)&ascii_to_ascii_strided_or_unaligned; |
| 122 | + } |
| 123 | + |
| 124 | + *flags = 0; |
| 125 | + return 0; |
| 126 | +} |
| 127 | + |
| 128 | +static PyArray_DTypeMeta *a2a_dtypes[2] = {NULL, NULL}; |
| 129 | + |
| 130 | +static PyType_Slot a2a_slots[] = { |
| 131 | + {NPY_METH_resolve_descriptors, &ascii_to_ascii_resolve_descriptors}, |
| 132 | + {_NPY_METH_get_loop, &ascii_to_ascii_get_loop}, |
| 133 | + {0, NULL}}; |
| 134 | + |
| 135 | +PyArrayMethod_Spec ASCIIToASCIICastSpec = { |
| 136 | + .name = "cast_ASCIIDType_to_ASCIIDType", |
| 137 | + .nin = 1, |
| 138 | + .nout = 1, |
| 139 | + .flags = NPY_METH_SUPPORTS_UNALIGNED, |
| 140 | + .casting = NPY_SAME_KIND_CASTING, |
| 141 | + .dtypes = a2a_dtypes, |
| 142 | + .slots = a2a_slots, |
| 143 | +}; |
0 commit comments