我正在尝试qsort使用自定义比较函数调用Cython,但我不明白如何传递函数引用.首先,我有一个结构:
cdef struct Pair:
int i,j
float h
Run Code Online (Sandbox Code Playgroud)
比较功能按以下方式排序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
Run Code Online (Sandbox Code Playgroud)
这是我遇到麻烦的部分:
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)
Run Code Online (Sandbox Code Playgroud)
最后一行不会编译并生成此错误,我认为这与我无法弄清楚如何compare作为参考传递的事实有关qsort:
Cannot assign type 'int (const_void *, const_void *)' to 'int (*)(const_void *, const_void *) nogil'
Run Code Online (Sandbox Code Playgroud)
我无法重现您的错误.您正在使用的代码是正确的,并使用Cython 0.15.我唯一看到的可能是你的错误是附加在类型上的"gil".如果要将导入的方法专门声明为"gil safe",请在声明的末尾附加"nogil".
(请注意,您可以使用cython -a检查您的python代码,然后打开Web浏览器)
cdef extern from "stdlib.h":
ctypedef void const_void "const void"
void qsort(void *base, int nmemb, int size,
int(*compar)(const_void *, const_void *)) nogil
cdef struct Pair:
int i,j
float h
cdef int compare(const_void *a, const_void *b):
cdef float v = ((a)).h-((b)).h
print 'do something with', v
if v 0: return 1
return 0
def r():
cdef Pair[5] pa
for i in range(5):
pa[i].i = i;
pa[i].j = i*2;
pa[i].h = i*.5;
print 'call qsort'
qsort(pa,5,sizeof(Pair),compare)
print 'call qsort done'
r()
此代码段编译为:
$ cython --version Cython version 0.15 $ cython --embed test.pyx $ gcc -I/usr/include/python2.7 -Wall -std=c99 test.c -lpython2.7 $ ./a.out call qsort do something with -0.5 do something with -0.5 do something with -0.5 do something with -1.0 do something with -0.5 call qsort done
| 归档时间: |
|
| 查看次数: |
5366 次 |
| 最近记录: |