【发布时间】:2011-12-02 07:31:37
【问题描述】:
我正在尝试使用自定义比较函数在 Cython 中调用 qsort,但我不明白如何传递函数引用。首先,我有一个结构:
cdef struct Pair:
int i,j
float h
比较函数按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
这是我遇到问题的部分:
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)
最后一行不会编译并生成此错误,我认为这与我无法弄清楚如何将compare 作为对qsort 的引用的事实有关:
Cannot assign type 'int (const_void *, const_void *)' to 'int (*)(const_void *, const_void *) nogil'
【问题讨论】:
标签: cython