I'm trying call qsort
in Cython with a custom compare function but I don't understand how to pass the function reference. First, I have a struct:
cdef struct Pair:
int i,j
float h
The compare function sorts by h
:
cdef int compare(const_void *a, const_void *b):
cdef float v = ((<Pair*>a)).h-((<Pair*>b)).h
if v < 0: return -1
if v > 0: return 1
return 0
This is the part I'm having trouble with:
cdef Pair[5] pa
for i in range(5):
pa[i].i = i;
pa[i].j = i*2;
pa[i].h = i*.5;
qsort(pa,5,sizeof(Pair),compare)
The last line won't compile and generates this error which I believe is related to the fact that I can't figure out how to pass compare
as a reference to qsort
:
Cannot assign type 'int (const_void *, const_void *)' to 'int (*)(const_void *, const_void *) nogil'
I've not been able to reproduce your error. The code you're using is right, and working with Cython 0.15. The only thing i see that might be your error is the "gil" appended to the type. If you want to specifically declare a imported method as "gil safe", append "nogil" at the end of the declaration.
(note that you can check your python code with cython -a , then open web browser for )
This snippet is compiled as: