Skip to content

Commit 7285238

Browse files
committed
simplify int to string casts
1 parent 4f9fde6 commit 7285238

File tree

1 file changed

+34
-216
lines changed

1 file changed

+34
-216
lines changed

stringdtype/stringdtype/src/casts.c

Lines changed: 34 additions & 216 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,32 @@ uint_to_string(unsigned long long in, char *out)
605605
return pylong_to_string(pylong_val, out);
606606
}
607607

608+
static NPY_CASTING
609+
int_to_string_resolve_descriptors(PyObject *NPY_UNUSED(self),
610+
PyArray_DTypeMeta *dtypes[2],
611+
PyArray_Descr *given_descrs[2],
612+
PyArray_Descr *loop_descrs[2],
613+
npy_intp *NPY_UNUSED(view_offset))
614+
{
615+
if (given_descrs[1] == NULL) {
616+
PyArray_Descr *new = (PyArray_Descr *)new_stringdtype_instance(
617+
(PyTypeObject *)dtypes[1]);
618+
if (new == NULL) {
619+
return (NPY_CASTING)-1;
620+
}
621+
loop_descrs[1] = new;
622+
}
623+
else {
624+
Py_INCREF(given_descrs[1]);
625+
loop_descrs[1] = given_descrs[1];
626+
}
627+
628+
Py_INCREF(given_descrs[0]);
629+
loop_descrs[0] = given_descrs[0];
630+
631+
return NPY_UNSAFE_CASTING;
632+
}
633+
608634
// string to int8
609635

610636
static NPY_CASTING
@@ -668,32 +694,6 @@ static char *s2i8_name = "cast_StringDType_to_Int8";
668694

669695
// int8 to string
670696

671-
static NPY_CASTING
672-
int8_to_string_resolve_descriptors(PyObject *NPY_UNUSED(self),
673-
PyArray_DTypeMeta *dtypes[2],
674-
PyArray_Descr *given_descrs[2],
675-
PyArray_Descr *loop_descrs[2],
676-
npy_intp *NPY_UNUSED(view_offset))
677-
{
678-
if (given_descrs[1] == NULL) {
679-
PyArray_Descr *new = (PyArray_Descr *)new_stringdtype_instance(
680-
(PyTypeObject *)dtypes[1]);
681-
if (new == NULL) {
682-
return (NPY_CASTING)-1;
683-
}
684-
loop_descrs[1] = new;
685-
}
686-
else {
687-
Py_INCREF(given_descrs[1]);
688-
loop_descrs[1] = given_descrs[1];
689-
}
690-
691-
Py_INCREF(given_descrs[0]);
692-
loop_descrs[0] = given_descrs[0];
693-
694-
return NPY_UNSAFE_CASTING;
695-
}
696-
697697
static int
698698
int8_to_string(PyArrayMethod_Context *NPY_UNUSED(context), char *const data[],
699699
npy_intp const dimensions[], npy_intp const strides[],
@@ -718,7 +718,7 @@ int8_to_string(PyArrayMethod_Context *NPY_UNUSED(context), char *const data[],
718718
}
719719

720720
static PyType_Slot i82s_slots[] = {
721-
{NPY_METH_resolve_descriptors, &int8_to_string_resolve_descriptors},
721+
{NPY_METH_resolve_descriptors, &int_to_string_resolve_descriptors},
722722
{NPY_METH_strided_loop, &int8_to_string},
723723
{0, NULL}};
724724

@@ -787,32 +787,6 @@ static char *s2i16_name = "cast_StringDType_to_Int16";
787787

788788
// int16 to string
789789

790-
static NPY_CASTING
791-
int16_to_string_resolve_descriptors(PyObject *NPY_UNUSED(self),
792-
PyArray_DTypeMeta *dtypes[2],
793-
PyArray_Descr *given_descrs[2],
794-
PyArray_Descr *loop_descrs[2],
795-
npy_intp *NPY_UNUSED(view_offset))
796-
{
797-
if (given_descrs[1] == NULL) {
798-
PyArray_Descr *new = (PyArray_Descr *)new_stringdtype_instance(
799-
(PyTypeObject *)dtypes[1]);
800-
if (new == NULL) {
801-
return (NPY_CASTING)-1;
802-
}
803-
loop_descrs[1] = new;
804-
}
805-
else {
806-
Py_INCREF(given_descrs[1]);
807-
loop_descrs[1] = given_descrs[1];
808-
}
809-
810-
Py_INCREF(given_descrs[0]);
811-
loop_descrs[0] = given_descrs[0];
812-
813-
return NPY_UNSAFE_CASTING;
814-
}
815-
816790
static int
817791
int16_to_string(PyArrayMethod_Context *NPY_UNUSED(context), char *const data[],
818792
npy_intp const dimensions[], npy_intp const strides[],
@@ -837,7 +811,7 @@ int16_to_string(PyArrayMethod_Context *NPY_UNUSED(context), char *const data[],
837811
}
838812

839813
static PyType_Slot i162s_slots[] = {
840-
{NPY_METH_resolve_descriptors, &int16_to_string_resolve_descriptors},
814+
{NPY_METH_resolve_descriptors, &int_to_string_resolve_descriptors},
841815
{NPY_METH_strided_loop, &int16_to_string},
842816
{0, NULL}};
843817

@@ -906,32 +880,6 @@ static char *s2i32_name = "cast_StringDType_to_Int32";
906880

907881
// int32 to string
908882

909-
static NPY_CASTING
910-
int32_to_string_resolve_descriptors(PyObject *NPY_UNUSED(self),
911-
PyArray_DTypeMeta *dtypes[2],
912-
PyArray_Descr *given_descrs[2],
913-
PyArray_Descr *loop_descrs[2],
914-
npy_intp *NPY_UNUSED(view_offset))
915-
{
916-
if (given_descrs[1] == NULL) {
917-
PyArray_Descr *new = (PyArray_Descr *)new_stringdtype_instance(
918-
(PyTypeObject *)dtypes[1]);
919-
if (new == NULL) {
920-
return (NPY_CASTING)-1;
921-
}
922-
loop_descrs[1] = new;
923-
}
924-
else {
925-
Py_INCREF(given_descrs[1]);
926-
loop_descrs[1] = given_descrs[1];
927-
}
928-
929-
Py_INCREF(given_descrs[0]);
930-
loop_descrs[0] = given_descrs[0];
931-
932-
return NPY_UNSAFE_CASTING;
933-
}
934-
935883
static int
936884
int32_to_string(PyArrayMethod_Context *NPY_UNUSED(context), char *const data[],
937885
npy_intp const dimensions[], npy_intp const strides[],
@@ -956,7 +904,7 @@ int32_to_string(PyArrayMethod_Context *NPY_UNUSED(context), char *const data[],
956904
}
957905

958906
static PyType_Slot i322s_slots[] = {
959-
{NPY_METH_resolve_descriptors, &int32_to_string_resolve_descriptors},
907+
{NPY_METH_resolve_descriptors, &int_to_string_resolve_descriptors},
960908
{NPY_METH_strided_loop, &int32_to_string},
961909
{0, NULL}};
962910

@@ -1025,32 +973,6 @@ static char *s2i64_name = "cast_StringDType_to_Int64";
1025973

1026974
// int64 to string
1027975

1028-
static NPY_CASTING
1029-
int64_to_string_resolve_descriptors(PyObject *NPY_UNUSED(self),
1030-
PyArray_DTypeMeta *dtypes[2],
1031-
PyArray_Descr *given_descrs[2],
1032-
PyArray_Descr *loop_descrs[2],
1033-
npy_intp *NPY_UNUSED(view_offset))
1034-
{
1035-
if (given_descrs[1] == NULL) {
1036-
PyArray_Descr *new = (PyArray_Descr *)new_stringdtype_instance(
1037-
(PyTypeObject *)dtypes[1]);
1038-
if (new == NULL) {
1039-
return (NPY_CASTING)-1;
1040-
}
1041-
loop_descrs[1] = new;
1042-
}
1043-
else {
1044-
Py_INCREF(given_descrs[1]);
1045-
loop_descrs[1] = given_descrs[1];
1046-
}
1047-
1048-
Py_INCREF(given_descrs[0]);
1049-
loop_descrs[0] = given_descrs[0];
1050-
1051-
return NPY_UNSAFE_CASTING;
1052-
}
1053-
1054976
static int
1055977
int64_to_string(PyArrayMethod_Context *NPY_UNUSED(context), char *const data[],
1056978
npy_intp const dimensions[], npy_intp const strides[],
@@ -1075,7 +997,7 @@ int64_to_string(PyArrayMethod_Context *NPY_UNUSED(context), char *const data[],
1075997
}
1076998

1077999
static PyType_Slot i642s_slots[] = {
1078-
{NPY_METH_resolve_descriptors, &int64_to_string_resolve_descriptors},
1000+
{NPY_METH_resolve_descriptors, &int_to_string_resolve_descriptors},
10791001
{NPY_METH_strided_loop, &int64_to_string},
10801002
{0, NULL}};
10811003

@@ -1144,32 +1066,6 @@ static char *s2ui8_name = "cast_StringDType_to_UInt8";
11441066

11451067
// uint8 to string
11461068

1147-
static NPY_CASTING
1148-
uint8_to_string_resolve_descriptors(PyObject *NPY_UNUSED(self),
1149-
PyArray_DTypeMeta *dtypes[2],
1150-
PyArray_Descr *given_descrs[2],
1151-
PyArray_Descr *loop_descrs[2],
1152-
npy_intp *NPY_UNUSED(view_offset))
1153-
{
1154-
if (given_descrs[1] == NULL) {
1155-
PyArray_Descr *new = (PyArray_Descr *)new_stringdtype_instance(
1156-
(PyTypeObject *)dtypes[1]);
1157-
if (new == NULL) {
1158-
return (NPY_CASTING)-1;
1159-
}
1160-
loop_descrs[1] = new;
1161-
}
1162-
else {
1163-
Py_INCREF(given_descrs[1]);
1164-
loop_descrs[1] = given_descrs[1];
1165-
}
1166-
1167-
Py_INCREF(given_descrs[0]);
1168-
loop_descrs[0] = given_descrs[0];
1169-
1170-
return NPY_UNSAFE_CASTING;
1171-
}
1172-
11731069
static int
11741070
uint8_to_string(PyArrayMethod_Context *NPY_UNUSED(context), char *const data[],
11751071
npy_intp const dimensions[], npy_intp const strides[],
@@ -1194,7 +1090,7 @@ uint8_to_string(PyArrayMethod_Context *NPY_UNUSED(context), char *const data[],
11941090
}
11951091

11961092
static PyType_Slot ui82s_slots[] = {
1197-
{NPY_METH_resolve_descriptors, &uint8_to_string_resolve_descriptors},
1093+
{NPY_METH_resolve_descriptors, &int_to_string_resolve_descriptors},
11981094
{NPY_METH_strided_loop, &uint8_to_string},
11991095
{0, NULL}};
12001096

@@ -1263,32 +1159,6 @@ static char *s2ui16_name = "cast_StringDType_to_UInt16";
12631159

12641160
// uint16 to string
12651161

1266-
static NPY_CASTING
1267-
uint16_to_string_resolve_descriptors(PyObject *NPY_UNUSED(self),
1268-
PyArray_DTypeMeta *dtypes[2],
1269-
PyArray_Descr *given_descrs[2],
1270-
PyArray_Descr *loop_descrs[2],
1271-
npy_intp *NPY_UNUSED(view_offset))
1272-
{
1273-
if (given_descrs[1] == NULL) {
1274-
PyArray_Descr *new = (PyArray_Descr *)new_stringdtype_instance(
1275-
(PyTypeObject *)dtypes[1]);
1276-
if (new == NULL) {
1277-
return (NPY_CASTING)-1;
1278-
}
1279-
loop_descrs[1] = new;
1280-
}
1281-
else {
1282-
Py_INCREF(given_descrs[1]);
1283-
loop_descrs[1] = given_descrs[1];
1284-
}
1285-
1286-
Py_INCREF(given_descrs[0]);
1287-
loop_descrs[0] = given_descrs[0];
1288-
1289-
return NPY_UNSAFE_CASTING;
1290-
}
1291-
12921162
static int
12931163
uint16_to_string(PyArrayMethod_Context *NPY_UNUSED(context),
12941164
char *const data[], npy_intp const dimensions[],
@@ -1313,7 +1183,7 @@ uint16_to_string(PyArrayMethod_Context *NPY_UNUSED(context),
13131183
}
13141184

13151185
static PyType_Slot ui162s_slots[] = {
1316-
{NPY_METH_resolve_descriptors, &uint16_to_string_resolve_descriptors},
1186+
{NPY_METH_resolve_descriptors, &int_to_string_resolve_descriptors},
13171187
{NPY_METH_strided_loop, &uint16_to_string},
13181188
{0, NULL}};
13191189

@@ -1382,32 +1252,6 @@ static char *s2ui32_name = "cast_StringDType_to_UInt32";
13821252

13831253
// uint32 to string
13841254

1385-
static NPY_CASTING
1386-
uint32_to_string_resolve_descriptors(PyObject *NPY_UNUSED(self),
1387-
PyArray_DTypeMeta *dtypes[2],
1388-
PyArray_Descr *given_descrs[2],
1389-
PyArray_Descr *loop_descrs[2],
1390-
npy_intp *NPY_UNUSED(view_offset))
1391-
{
1392-
if (given_descrs[1] == NULL) {
1393-
PyArray_Descr *new = (PyArray_Descr *)new_stringdtype_instance(
1394-
(PyTypeObject *)dtypes[1]);
1395-
if (new == NULL) {
1396-
return (NPY_CASTING)-1;
1397-
}
1398-
loop_descrs[1] = new;
1399-
}
1400-
else {
1401-
Py_INCREF(given_descrs[1]);
1402-
loop_descrs[1] = given_descrs[1];
1403-
}
1404-
1405-
Py_INCREF(given_descrs[0]);
1406-
loop_descrs[0] = given_descrs[0];
1407-
1408-
return NPY_UNSAFE_CASTING;
1409-
}
1410-
14111255
static int
14121256
uint32_to_string(PyArrayMethod_Context *NPY_UNUSED(context),
14131257
char *const data[], npy_intp const dimensions[],
@@ -1432,7 +1276,7 @@ uint32_to_string(PyArrayMethod_Context *NPY_UNUSED(context),
14321276
}
14331277

14341278
static PyType_Slot ui322s_slots[] = {
1435-
{NPY_METH_resolve_descriptors, &uint32_to_string_resolve_descriptors},
1279+
{NPY_METH_resolve_descriptors, &int_to_string_resolve_descriptors},
14361280
{NPY_METH_strided_loop, &uint32_to_string},
14371281
{0, NULL}};
14381282

@@ -1501,32 +1345,6 @@ static char *s2ui64_name = "cast_StringDType_to_UInt64";
15011345

15021346
// uint64 to string
15031347

1504-
static NPY_CASTING
1505-
uint64_to_string_resolve_descriptors(PyObject *NPY_UNUSED(self),
1506-
PyArray_DTypeMeta *dtypes[2],
1507-
PyArray_Descr *given_descrs[2],
1508-
PyArray_Descr *loop_descrs[2],
1509-
npy_intp *NPY_UNUSED(view_offset))
1510-
{
1511-
if (given_descrs[1] == NULL) {
1512-
PyArray_Descr *new = (PyArray_Descr *)new_stringdtype_instance(
1513-
(PyTypeObject *)dtypes[1]);
1514-
if (new == NULL) {
1515-
return (NPY_CASTING)-1;
1516-
}
1517-
loop_descrs[1] = new;
1518-
}
1519-
else {
1520-
Py_INCREF(given_descrs[1]);
1521-
loop_descrs[1] = given_descrs[1];
1522-
}
1523-
1524-
Py_INCREF(given_descrs[0]);
1525-
loop_descrs[0] = given_descrs[0];
1526-
1527-
return NPY_UNSAFE_CASTING;
1528-
}
1529-
15301348
static int
15311349
uint64_to_string(PyArrayMethod_Context *NPY_UNUSED(context),
15321350
char *const data[], npy_intp const dimensions[],
@@ -1551,7 +1369,7 @@ uint64_to_string(PyArrayMethod_Context *NPY_UNUSED(context),
15511369
}
15521370

15531371
static PyType_Slot ui642s_slots[] = {
1554-
{NPY_METH_resolve_descriptors, &uint64_to_string_resolve_descriptors},
1372+
{NPY_METH_resolve_descriptors, &int_to_string_resolve_descriptors},
15551373
{NPY_METH_strided_loop, &uint64_to_string},
15561374
{0, NULL}};
15571375

0 commit comments

Comments
 (0)