2016-04-10 85 views
0
cdef int bs_contains_nogil(float_or_int[:] l,float_or_int t,int size) nogil: 
    cdef int low=0 
    cdef int high=size-1 
    cdef int mid=0 
    while(low<=high): 
     mid=(low+high)//2 
     if t==l[mid]: 
      return mid 
     elif t < l[mid]: 
      high=mid-1 
     else: 
      low=mid+1 
    return -(low+1) 

@boundscheck(False) 
@wraparound(False) 
def insertplace_nogil(l,t): 
    idx=(<object (*)(int[:],int,int)>bs_contains_nogil)(l,t,len(l)) 
    return idx //return the target position 

上面的代碼給我一個錯誤(類型不專業),任何人都知道如何解決這個問題,謝謝。Cython融合不能鑄造

+0

它給你什麼樣的錯誤?此外,你設置'idx'值的整個行都很瘋狂。你想做什麼?我們可以簡化這一點的可能性非常高。 –

+0

對不起,沒有清除代碼,我想返回數組中的目標位置,謝謝。 – user3035661

回答

0

您可以選擇int專業化容易用方括號(as discussed in the documentation)了您的融合型功能。

# for completeness 
ctypedef fused float_or_int: 
    float 
    int 

cdef int bs_contains_nogil(float_or_int[:] l,float_or_int t,int size) nogil: 
    return 5 # your function goes here... I've cut this down for simplicitity since I don't think it's the main point of your question. 

def insertplace_nogil(l,t): 
    return bs_contains_nogil[int](l,t,len(l)) 

請注意,您只需要指定一次類型(即lt都必須是同一類型)。這是(我認爲?)你出錯的地方。

+0

謝謝,正是我想要做的!非常感謝! – user3035661

0

你有幾個語法問題,我認爲你讓這太複雜了。還據我所知,你必須選擇一種類型的參數到一個cdef函數或使用python對象,這可能是爲什麼你得到一個與類型相關的錯誤。我沒有測試過這一點,但讓這個嘗試

cdef int bs_contains_nogil(int[:] l, int t,int size) nogil: 
    cdef int low=0 
    cdef int high=size-1 
    cdef int mid=0 
    while(low<=high): 
     mid=(low+high)/2 
     if t==l[mid]: 
      return mid 
     elif t < l[mid]: 
      high=mid-1 
     else: 
      low=mid+1 
    return low+1 

@boundscheck(False) 
@wraparound(False) 
def insertplace_nogil(l,t): 
    return bs_contains_nogil(l,t,len(l)