Skip to content

Commit 56c5d31

Browse files
committed
simplify declaring int cast specs with a macro
1 parent 632c6d1 commit 56c5d31

File tree

1 file changed

+40
-197
lines changed

1 file changed

+40
-197
lines changed

stringdtype/stringdtype/src/casts.c

Lines changed: 40 additions & 197 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,38 @@ uint_to_string(unsigned long long in, char *out)
677677
\
678678
static char *shortname##2s_name = "cast_" #typename "_to_StringDType";
679679

680+
#define INT_DTYPES_AND_CAST_SPEC(shortname, typename) \
681+
PyArray_DTypeMeta **s2##shortname##_dtypes = \
682+
get_dtypes(this, &PyArray_##typename##DType); \
683+
\
684+
PyArrayMethod_Spec *StringTo##typename##CastSpec = \
685+
get_cast_spec(s2##shortname##_name, NPY_UNSAFE_CASTING, \
686+
NPY_METH_REQUIRES_PYAPI, s2##shortname##_dtypes, \
687+
s2##shortname##_slots); \
688+
\
689+
PyArray_DTypeMeta **shortname##2s_dtypes = \
690+
get_dtypes(&PyArray_##typename##DType, this); \
691+
\
692+
PyArrayMethod_Spec *typename##ToStringCastSpec = get_cast_spec( \
693+
shortname##2s_name, NPY_UNSAFE_CASTING, NPY_METH_REQUIRES_PYAPI, \
694+
shortname##2s_dtypes, shortname##2s_slots); \
695+
\
696+
PyArray_DTypeMeta **s2u##shortname##_dtypes = \
697+
get_dtypes(this, &PyArray_U##typename##DType); \
698+
\
699+
PyArrayMethod_Spec *StringToU##typename##CastSpec = \
700+
get_cast_spec(s2u##shortname##_name, NPY_UNSAFE_CASTING, \
701+
NPY_METH_REQUIRES_PYAPI, s2u##shortname##_dtypes, \
702+
s2u##shortname##_slots); \
703+
\
704+
PyArray_DTypeMeta **u##shortname##2s_dtypes = \
705+
get_dtypes(&PyArray_U##typename##DType, this); \
706+
\
707+
PyArrayMethod_Spec *U##typename##ToStringCastSpec = \
708+
get_cast_spec(u##shortname##2s_name, NPY_UNSAFE_CASTING, \
709+
NPY_METH_REQUIRES_PYAPI, u##shortname##2s_dtypes, \
710+
u##shortname##2s_slots);
711+
680712
STRING_TO_INT(int8, int, i8, NPY_INT8, lli, npy_longlong)
681713
INT_TO_STRING(int8, int, i8, long long)
682714

@@ -844,212 +876,23 @@ get_casts(PyArray_DTypeMeta *this, PyArray_DTypeMeta *other)
844876
b2s_name, NPY_SAFE_CASTING, NPY_METH_NO_FLOATINGPOINT_ERRORS,
845877
b2s_dtypes, b2s_slots);
846878

847-
PyArray_DTypeMeta **s2i8_dtypes = get_dtypes(this, &PyArray_Int8DType);
848-
849-
PyArrayMethod_Spec *StringToInt8CastSpec =
850-
get_cast_spec(s2i8_name, NPY_UNSAFE_CASTING,
851-
NPY_METH_REQUIRES_PYAPI, s2i8_dtypes, s2i8_slots);
852-
853-
PyArray_DTypeMeta **i82s_dtypes = get_dtypes(&PyArray_Int8DType, this);
854-
855-
PyArrayMethod_Spec *Int8ToStringCastSpec =
856-
get_cast_spec(i82s_name, NPY_UNSAFE_CASTING,
857-
NPY_METH_REQUIRES_PYAPI, i82s_dtypes, i82s_slots);
858-
859-
PyArray_DTypeMeta **s2ui8_dtypes = get_dtypes(this, &PyArray_UInt8DType);
860-
861-
PyArrayMethod_Spec *StringToUInt8CastSpec =
862-
get_cast_spec(s2ui8_name, NPY_UNSAFE_CASTING,
863-
NPY_METH_REQUIRES_PYAPI, s2ui8_dtypes, s2ui8_slots);
864-
865-
PyArray_DTypeMeta **ui82s_dtypes = get_dtypes(&PyArray_UInt8DType, this);
866-
867-
PyArrayMethod_Spec *UInt8ToStringCastSpec =
868-
get_cast_spec(ui82s_name, NPY_UNSAFE_CASTING,
869-
NPY_METH_REQUIRES_PYAPI, ui82s_dtypes, ui82s_slots);
870-
871-
PyArray_DTypeMeta **s2i16_dtypes = get_dtypes(this, &PyArray_Int16DType);
872-
873-
PyArrayMethod_Spec *StringToInt16CastSpec =
874-
get_cast_spec(s2i16_name, NPY_UNSAFE_CASTING,
875-
NPY_METH_REQUIRES_PYAPI, s2i16_dtypes, s2i16_slots);
876-
877-
PyArray_DTypeMeta **i162s_dtypes = get_dtypes(&PyArray_Int16DType, this);
878-
879-
PyArrayMethod_Spec *Int16ToStringCastSpec =
880-
get_cast_spec(i162s_name, NPY_UNSAFE_CASTING,
881-
NPY_METH_REQUIRES_PYAPI, i162s_dtypes, i162s_slots);
882-
883-
PyArray_DTypeMeta **s2ui16_dtypes = get_dtypes(this, &PyArray_UInt16DType);
884-
885-
PyArrayMethod_Spec *StringToUInt16CastSpec = get_cast_spec(
886-
s2ui16_name, NPY_UNSAFE_CASTING, NPY_METH_REQUIRES_PYAPI,
887-
s2ui16_dtypes, s2ui16_slots);
888-
889-
PyArray_DTypeMeta **ui162s_dtypes = get_dtypes(&PyArray_UInt16DType, this);
890-
891-
PyArrayMethod_Spec *UInt16ToStringCastSpec = get_cast_spec(
892-
ui162s_name, NPY_UNSAFE_CASTING, NPY_METH_REQUIRES_PYAPI,
893-
ui162s_dtypes, ui162s_slots);
894-
895-
PyArray_DTypeMeta **s2i32_dtypes = get_dtypes(this, &PyArray_Int32DType);
896-
897-
PyArrayMethod_Spec *StringToInt32CastSpec =
898-
get_cast_spec(s2i32_name, NPY_UNSAFE_CASTING,
899-
NPY_METH_REQUIRES_PYAPI, s2i32_dtypes, s2i32_slots);
900-
901-
PyArray_DTypeMeta **i322s_dtypes = get_dtypes(&PyArray_Int32DType, this);
902-
903-
PyArrayMethod_Spec *Int32ToStringCastSpec =
904-
get_cast_spec(i322s_name, NPY_UNSAFE_CASTING,
905-
NPY_METH_REQUIRES_PYAPI, i322s_dtypes, i322s_slots);
906-
907-
PyArray_DTypeMeta **s2ui32_dtypes = get_dtypes(this, &PyArray_UInt32DType);
908-
909-
PyArrayMethod_Spec *StringToUInt32CastSpec = get_cast_spec(
910-
s2ui32_name, NPY_UNSAFE_CASTING, NPY_METH_REQUIRES_PYAPI,
911-
s2ui32_dtypes, s2ui32_slots);
912-
913-
PyArray_DTypeMeta **ui322s_dtypes = get_dtypes(&PyArray_UInt32DType, this);
914-
915-
PyArrayMethod_Spec *UInt32ToStringCastSpec = get_cast_spec(
916-
ui322s_name, NPY_UNSAFE_CASTING, NPY_METH_REQUIRES_PYAPI,
917-
ui322s_dtypes, ui322s_slots);
918-
919-
PyArray_DTypeMeta **s2i64_dtypes = get_dtypes(this, &PyArray_Int64DType);
920-
921-
PyArrayMethod_Spec *StringToInt64CastSpec =
922-
get_cast_spec(s2i64_name, NPY_UNSAFE_CASTING,
923-
NPY_METH_REQUIRES_PYAPI, s2i64_dtypes, s2i64_slots);
924-
925-
PyArray_DTypeMeta **i642s_dtypes = get_dtypes(&PyArray_Int64DType, this);
926-
927-
PyArrayMethod_Spec *Int64ToStringCastSpec =
928-
get_cast_spec(i642s_name, NPY_UNSAFE_CASTING,
929-
NPY_METH_REQUIRES_PYAPI, i642s_dtypes, i642s_slots);
930-
879+
INT_DTYPES_AND_CAST_SPEC(i8, Int8)
880+
INT_DTYPES_AND_CAST_SPEC(i16, Int16)
881+
INT_DTYPES_AND_CAST_SPEC(i32, Int32)
882+
INT_DTYPES_AND_CAST_SPEC(i64, Int64)
931883
#if NPY_SIZEOF_BYTE == NPY_SIZEOF_SHORT
932-
PyArray_DTypeMeta **s2byte_dtypes = get_dtypes(this, &PyArray_ByteDType);
933-
934-
PyArrayMethod_Spec *StringToByteCastSpec = get_cast_spec(
935-
s2byte_name, NPY_UNSAFE_CASTING, NPY_METH_REQUIRES_PYAPI,
936-
s2byte_dtypes, s2byte_slots);
937-
938-
PyArray_DTypeMeta **byte2s_dtypes = get_dtypes(&PyArray_ByteDType, this);
939-
940-
PyArrayMethod_Spec *ByteToStringCastSpec = get_cast_spec(
941-
byte2s_name, NPY_UNSAFE_CASTING, NPY_METH_REQUIRES_PYAPI,
942-
byte2s_dtypes, byte2s_slots);
943-
944-
PyArray_DTypeMeta **s2ubyte_dtypes = get_dtypes(this, &PyArray_UByteDType);
945-
946-
PyArrayMethod_Spec *StringToUByteCastSpec = get_cast_spec(
947-
s2ubyte_name, NPY_UNSAFE_CASTING, NPY_METH_REQUIRES_PYAPI,
948-
s2ubyte_dtypes, s2ubyte_slots);
949-
950-
PyArray_DTypeMeta **ubyte2s_dtypes = get_dtypes(&PyArray_UByteDType, this);
951-
952-
PyArrayMethod_Spec *UByteToStringCastSpec = get_cast_spec(
953-
ubyte2s_name, NPY_UNSAFE_CASTING, NPY_METH_REQUIRES_PYAPI,
954-
ubyte2s_dtypes, ubyte2s_slots);
884+
INT_DTYPES_AND_CAST_SPEC(byte, Byte)
955885
#endif
956-
957886
#if NPY_SIZEOF_SHORT == NPY_SIZEOF_INT
958-
PyArray_DTypeMeta **s2short_dtypes = get_dtypes(this, &PyArray_ShortDType);
959-
960-
PyArrayMethod_Spec *StringToShortCastSpec = get_cast_spec(
961-
s2short_name, NPY_UNSAFE_CASTING, NPY_METH_REQUIRES_PYAPI,
962-
s2short_dtypes, s2short_slots);
963-
964-
PyArray_DTypeMeta **short2s_dtypes = get_dtypes(&PyArray_ShortDType, this);
965-
966-
PyArrayMethod_Spec *ShortToStringCastSpec = get_cast_spec(
967-
short2s_name, NPY_UNSAFE_CASTING, NPY_METH_REQUIRES_PYAPI,
968-
short2s_dtypes, short2s_slots);
969-
970-
PyArray_DTypeMeta **s2ushort_dtypes =
971-
get_dtypes(this, &PyArray_UShortDType);
972-
973-
PyArrayMethod_Spec *StringToUShortCastSpec = get_cast_spec(
974-
s2ushort_name, NPY_UNSAFE_CASTING, NPY_METH_REQUIRES_PYAPI,
975-
s2ushort_dtypes, s2ushort_slots);
976-
977-
PyArray_DTypeMeta **ushort2s_dtypes =
978-
get_dtypes(&PyArray_UShortDType, this);
979-
980-
PyArrayMethod_Spec *UShortToStringCastSpec = get_cast_spec(
981-
ushort2s_name, NPY_UNSAFE_CASTING, NPY_METH_REQUIRES_PYAPI,
982-
ushort2s_dtypes, ushort2s_slots);
887+
INT_DTYPES_AND_CAST_SPEC(short, Short)
983888
#endif
984-
985889
#if NPY_SIZEOF_INT == NPY_SIZEOF_LONG
986-
PyArray_DTypeMeta **s2int_dtypes = get_dtypes(this, &PyArray_IntDType);
987-
988-
PyArrayMethod_Spec *StringToIntCastSpec =
989-
get_cast_spec(s2int_name, NPY_UNSAFE_CASTING,
990-
NPY_METH_REQUIRES_PYAPI, s2int_dtypes, s2int_slots);
991-
992-
PyArray_DTypeMeta **int2s_dtypes = get_dtypes(&PyArray_IntDType, this);
993-
994-
PyArrayMethod_Spec *IntToStringCastSpec =
995-
get_cast_spec(int2s_name, NPY_UNSAFE_CASTING,
996-
NPY_METH_REQUIRES_PYAPI, int2s_dtypes, int2s_slots);
997-
998-
PyArray_DTypeMeta **s2uint_dtypes = get_dtypes(this, &PyArray_UIntDType);
999-
1000-
PyArrayMethod_Spec *StringToUIntCastSpec = get_cast_spec(
1001-
s2uint_name, NPY_UNSAFE_CASTING, NPY_METH_REQUIRES_PYAPI,
1002-
s2uint_dtypes, s2uint_slots);
1003-
1004-
PyArray_DTypeMeta **uint2s_dtypes = get_dtypes(&PyArray_UIntDType, this);
1005-
1006-
PyArrayMethod_Spec *UIntToStringCastSpec = get_cast_spec(
1007-
uint2s_name, NPY_UNSAFE_CASTING, NPY_METH_REQUIRES_PYAPI,
1008-
uint2s_dtypes, uint2s_slots);
890+
INT_DTYPES_AND_CAST_SPEC(int, Int)
1009891
#endif
1010-
1011892
#if NPY_SIZEOF_LONGLONG == NPY_SIZEOF_LONG
1012-
PyArray_DTypeMeta **s2longlong_dtypes =
1013-
get_dtypes(this, &PyArray_LongLongDType);
1014-
1015-
PyArrayMethod_Spec *StringToLongLongCastSpec = get_cast_spec(
1016-
s2longlong_name, NPY_UNSAFE_CASTING, NPY_METH_REQUIRES_PYAPI,
1017-
s2longlong_dtypes, s2longlong_slots);
1018-
1019-
PyArray_DTypeMeta **longlong2s_dtypes =
1020-
get_dtypes(&PyArray_LongLongDType, this);
1021-
1022-
PyArrayMethod_Spec *LongLongToStringCastSpec = get_cast_spec(
1023-
longlong2s_name, NPY_UNSAFE_CASTING, NPY_METH_REQUIRES_PYAPI,
1024-
longlong2s_dtypes, longlong2s_slots);
1025-
1026-
PyArray_DTypeMeta **s2ulonglong_dtypes =
1027-
get_dtypes(this, &PyArray_ULongLongDType);
1028-
1029-
PyArrayMethod_Spec *StringToULongLongCastSpec = get_cast_spec(
1030-
s2ulonglong_name, NPY_UNSAFE_CASTING, NPY_METH_REQUIRES_PYAPI,
1031-
s2ulonglong_dtypes, s2ulonglong_slots);
1032-
1033-
PyArray_DTypeMeta **ulonglong2s_dtypes =
1034-
get_dtypes(&PyArray_ULongLongDType, this);
1035-
1036-
PyArrayMethod_Spec *ULongLongToStringCastSpec = get_cast_spec(
1037-
ulonglong2s_name, NPY_UNSAFE_CASTING, NPY_METH_REQUIRES_PYAPI,
1038-
ulonglong2s_dtypes, ulonglong2s_slots);
893+
INT_DTYPES_AND_CAST_SPEC(longlong, LongLong)
1039894
#endif
1040895

1041-
PyArray_DTypeMeta **s2ui64_dtypes = get_dtypes(this, &PyArray_UInt64DType);
1042-
1043-
PyArrayMethod_Spec *StringToUInt64CastSpec = get_cast_spec(
1044-
s2ui64_name, NPY_UNSAFE_CASTING, NPY_METH_REQUIRES_PYAPI,
1045-
s2ui64_dtypes, s2ui64_slots);
1046-
1047-
PyArray_DTypeMeta **ui642s_dtypes = get_dtypes(&PyArray_UInt64DType, this);
1048-
1049-
PyArrayMethod_Spec *UInt64ToStringCastSpec = get_cast_spec(
1050-
ui642s_name, NPY_UNSAFE_CASTING, NPY_METH_REQUIRES_PYAPI,
1051-
ui642s_dtypes, ui642s_slots);
1052-
1053896
PyArrayMethod_Spec **casts = NULL;
1054897

1055898
casts = malloc((num_casts + 1) * sizeof(PyArrayMethod_Spec *));

0 commit comments

Comments
 (0)