Skip to content

ENH, API: New sorting mechanism for DType API #28516

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

Open
wants to merge 30 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
4700c13
ENH: Allocate lock only once in StringDType quicksort
MaanasArora Mar 14, 2025
5fb0b5f
ENH: Add dtype slots for sorting and begin integration
MaanasArora Mar 26, 2025
76be21b
MAINT: Rename sort compare slot access function
MaanasArora Mar 26, 2025
59590d2
ENH: Add dtype slot sorting functionality support to all sort kinds
MaanasArora Mar 26, 2025
b89accd
ENH: Add descending flag to internal sorting functions
MaanasArora Mar 26, 2025
a437eb9
MAINT: Improve get dtype sort compare function name
MaanasArora Mar 27, 2025
aa63d11
MAINT: Fix doc typo
MaanasArora Mar 27, 2025
16e95a2
MAINT: Error out when non-legacy dtype has no sort_compare function
MaanasArora Mar 27, 2025
42e76d6
DOC: Add release notes for new dtype sorting API
MaanasArora Mar 28, 2025
88636cc
DOC: Add doc for sort compare slot in release notes
MaanasArora Mar 28, 2025
9d14ec1
DOC: Add note for potential deprecation of sort arrfuncs in release note
MaanasArora Mar 30, 2025
a556455
MAINT: Reorder dtype slots to prevent changing existing slot numbers
MaanasArora Mar 30, 2025
3c0957e
BUG: Error on missing `sort_compare` slot only when dtype is privatel…
MaanasArora Mar 30, 2025
9506798
DOC: Add C-API documentation for new sorting slots
MaanasArora Apr 1, 2025
6ce5351
ENH: Replace array object with context and auxdata in sortfunc signat…
MaanasArora Apr 5, 2025
96a53b2
BUG: Fix unnecessarily private function call due to underscore typo
MaanasArora Apr 5, 2025
9a2b100
MAINT: Fix whitespace typos
MaanasArora Apr 5, 2025
8d4c75d
ENH: Allow flexible sorting compare for arr or descr in npy_sort func…
MaanasArora Apr 11, 2025
50988ba
ENH: Add new sort func implementations and use in stringdtype
MaanasArora Apr 11, 2025
ca5797e
DOC: Fix missing newline in ctype doc
MaanasArora Apr 11, 2025
95cfd8f
DOC: Add sortfunc typedef docs
MaanasArora Apr 12, 2025
6dd4f4c
DOC: Fix missing newline in ctype doc
MaanasArora Apr 12, 2025
4fa813c
ENH: Define SortCompareFunc type
MaanasArora Apr 12, 2025
894911e
Update dtype sorting signatures: move context, move out auxdata to ge…
MaanasArora May 13, 2025
57687ac
MAINT: Check error in Get(Arg)SortFunc using return value
MaanasArora May 13, 2025
0edb4ea
DOC: Add missing newlines to c-types in array.rst
MaanasArora May 14, 2025
167301e
MAINT: Rename new sort funcs and restore older names for existing pub…
MaanasArora May 24, 2025
e6b8c1e
MAINT: Rename start pointer in new sort func documentation to data
MaanasArora May 24, 2025
579c351
ENH: Add flags to new get_(arg)sort_function
MaanasArora May 28, 2025
d854b00
DOC: Mention new sort func buffers to be contiguous
MaanasArora May 28, 2025
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
Prev Previous commit
Next Next commit
ENH: Define SortCompareFunc type
  • Loading branch information
MaanasArora committed May 20, 2025
commit 4fa813c35d08b526e1f1bc37f8f29e0de93181ed
2 changes: 2 additions & 0 deletions numpy/_core/include/numpy/dtype_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,8 @@ typedef PyObject *(PyArrayDTypeMeta_GetItem)(PyArray_Descr *, char *);

typedef int (PyArray_CompareFuncWithDescr)(const void *, const void *,
PyArray_Descr *);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The naming is a bit weird here, but I didn't want to disturb the original type as it's used a lot. I think the SortCompareFunc should still be a unique type so will do that (even if only a clone of this type).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have slightly mixed feelings. On the one hand, I think this is the pragmatic thing to have.
On the other hand, we could also look this function from the np.less_than or np.great_than ufunc to implement sorting, I think.
(The problem there is still how to deal with unordered elements, a compare ufunc would work better...)

But, on the other hand, it seems pragmatic even if it won't work well e.g. for structured dtypes (performance issues), it will always work and provides an easy entry-point (we can also use this to define default comparison ufuncs).

So overall, I think I end up at just doing this, although I could imaging punting if we don't need it for StringDType (I suspect we do, though).

Would like to hear if @ngoldbaum has an opinion.

(A neater future path would also be if this was more of a header-only code binding generator job with us making the sorting patterns available maybe. I.e. if this was defined in a C++ class and our sort code available, the DType could compile the full loop and avoid calling such a helper everywhere.)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO this is fine, if only because it exists right now 😄

typedef int (PyArray_SortCompareFunc)(const void *, const void *,
PyArray_Descr *);
typedef int (PyArray_SortFunc)(void *, npy_intp,
PyArrayMethod_Context *, NpyAuxData *,
NpyAuxData **);
Expand Down
4 changes: 2 additions & 2 deletions numpy/_core/src/multiarray/dtypemeta.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ typedef struct {
PyArrayDTypeMeta_GetSortFunction *get_sort_function;
PyArrayDTypeMeta_GetArgSortFunction *get_argsort_function;
PyArray_CompareFuncWithDescr *compare;
PyArray_CompareFuncWithDescr *sort_compare;
PyArray_SortCompareFunc *sort_compare;

/*
* The casting implementation (ArrayMethod) to convert between two
Expand Down Expand Up @@ -330,7 +330,7 @@ PyArray_GetCompareFunction(PyArray_Descr *descr)
return NPY_DT_SLOTS(NPY_DTYPE(descr))->compare;
}

static inline PyArray_CompareFuncWithDescr *
static inline PyArray_SortCompareFunc *
PyArray_GetSortCompareFunction(PyArray_Descr *descr)
{
return NPY_DT_SLOTS(NPY_DTYPE(descr))->sort_compare;
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy