Using Cython to wrap a c++ template to accept any numpy array

前端 未结 1 501
你的背包
你的背包 2020-12-19 08:18

I\'m trying to wrap a parallel sort written in c++ as a template, to use it with numpy arrays of any numeric type. I\'m trying to use Cython to do this.

My problem

相关标签:
1条回答
  • 2020-12-19 09:00

    Yes, you want to use a fused type to have Cython call the sorting template for the appropriate specialization of the template. Here's a working example for all non-complex data types that does this with std::sort.

    # cython: wraparound = False
    # cython: boundscheck = False
    
    cimport cython
    
    cdef extern from "<algorithm>" namespace "std":
        cdef void sort[T](T first, T last) nogil
    
    ctypedef fused real:
        cython.char
        cython.uchar
        cython.short
        cython.ushort
        cython.int
        cython.uint
        cython.long
        cython.ulong
        cython.longlong
        cython.ulonglong
        cython.float
        cython.double
    
    cpdef void npy_sort(real[:] a) nogil:
        sort(&a[0], &a[a.shape[0]-1])
    
    0 讨论(0)
提交回复
热议问题