user3404608
user3404608

Reputation: 157

Cython fused can not casting

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

Above code give me an error (Type is not specialized),any one know how to slove this problem , Thanks.

Upvotes: 0

Views: 243

Answers (2)

DavidW
DavidW

Reputation: 30894

You can select the int specialization of the your fused-type function easily using square brackets (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))

Note that you only need to specify the type once (i.e. l and t must both be the same type). This is (I think?) where you were going wrong.

Upvotes: 1

MS-DDOS
MS-DDOS

Reputation: 576

You've got several syntax issues, and I think you've just made this a tad too complicated. Also as far as I know you have to pick one type for parameters to a cdef function or use a python object instead, this is probably why you're getting an error related to type. I haven't tested this, but give this a try

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)

Upvotes: 0

Related Questions