8000 ENH: Add usage of new sorting dtype slots by MaanasArora · Pull Request #109 · numpy/numpy-user-dtypes · GitHub
[go: up one dir, main page]

Skip to content

ENH: Add usage of new sorting dtype slots #109

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions mpfdtype/mpfdtype/src/dtype.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,13 +159,35 @@ mpf_getitem(MPFDTypeObject *descr, char *dataptr)
return (PyObject *)new;
}


int mpf_compare(char *in1_ptr, char *in2_ptr, PyArray_Descr *descr)
{
/* Note that it is probably better to only get the descr from `ap` */
mpfr_prec_t precision = ((MPFDTypeObject *)descr)->precision;

mpfr_ptr in1, in2;

mpf_load(in1, in1_ptr, precision);
mpf_load(in2, in2_ptr, precision);

if (!mpfr_total_order_p(in1, in2)) {
return 1;
}
if (!mpfr_total_order_p(in2, in1)) {
return -1;
}
return 0;
}


static PyType_Slot MPFDType_Slots[] = {
{NPY_DT_ensure_canonical, &ensure_canonical},
{NPY_DT_common_instance, &common_instance},
{NPY_DT_common_dtype, &common_dtype},
{NPY_DT_discover_descr_from_pyobject, &mpf_discover_descriptor_from_pyobject},
{NPY_DT_setitem, &mpf_setitem},
{NPY_DT_getitem, &mpf_getitem},
{NPY_DT_sort_compare, &mpf_compare},
{0, NULL}};

/*
Expand Down
22 changes: 0 additions & 22 deletions mpfdtype/mpfdtype/src/terrible_hacks.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,27 +35,6 @@ copyswap_mpf(char *dst, char *src, int swap, PyArrayObject *ap)
}


/* Should only be used for sorting, so more complex than necessary, probably */
int compare_mpf(char *in1_ptr, char *in2_ptr, int swap, PyArrayObject *ap)
{
/* Note that it is probably better to only get the descr from `ap` */
mpfr_prec_t precision = ((MPFDTypeObject *)PyArray_DESCR(ap))->precision;

mpfr_ptr in1, in2;

mpf_load(in1, in1_ptr, precision);
mpf_load(in2, in2_ptr, precision);

if (!mpfr_total_order_p(in1, in2)) {
return 1;
}
if (!mpfr_total_order_p(in2, in1)) {
return -1;
}
return 0;
}


int
init_terrible_hacks(void) {
/* Defaults to -1 byt ISNUMBER misfires for it, so use MAX */
Expand All @@ -70,7 +49,6 @@ init_terrible_hacks(void) {
}
/* ->f slots are the same for all instances (currently). */
PyDataType_GetArrFuncs(&descr->base)->copyswap = (PyArray_CopySwapFunc *)&copyswap_mpf;
PyDataType_GetArrFuncs(&descr->base)->compare = (PyArray_CompareFunc *)&compare_mpf;
Py_DECREF(descr);

return 0;
Expand Down
0