如何实现vector <vector <obj =“”>&gt; for Cython(C ++ to Python)?</vector>

时间:2014-03-02 05:32:54

标签: python c++ numpy vector cython

请参阅此处的代码:https://github.com/rootpy/root_numpy/blob/master/root_numpy/src/tree.pyx#L228

cdef cppclass VectorConverter[T](VectorConverterBase):
    int elesize
    int nptypecode
    Vector2Array[T] v2a
    __init__():
        cdef TypeName[T] ast = TypeName[T]()
        info = TYPES[ast.name]
        this.elesize = info[1].itemsize
        this.nptypecode = info[2]
    int write(Column* col, void* buffer):
        cdef vector[T]* tmp = <vector[T]*> col.GetValuePointer()
        cdef unsigned long numele = tmp.size()
        # check cython auto generate code
        # if it really does &((*tmp)[0])
        cdef T* fa = this.v2a.convert(tmp)
        return create_numpyarray(buffer, fa, this.nptypecode, numele, this.elesize)

基本上 - 这段代码意味着将vector <object>(通常是float或int)转换为数组[特别是NumPy数组]。我很难理解它,但我离题了。

我需要对它进行扩展,以便能够实现vector<vector <object>>。我认为它应该是我在所述链接中突出显示的代码的副本。

我从哪里开始?

1 个答案:

答案 0 :(得分:4)

我已经解决了这个问题,因为我不是DenverCoder9,所以这就是我如何做到的,如果它在将来帮助某人,甚至是过时的

cdef cppclass VectorVectorConverter[T](VectorConverterBase):
    int elesize
    int nptypecode
    Vector2Array[T] v2a
    __init__():
        cdef TypeName[T] ast = TypeName[T]()
        info = TYPES[ast.name]
        this.elesize = info[1].itemsize
        this.nptypecode = info[2]
    int write(Column* col, void* buffer):
        cdef vector[vector[T]]* tmp = <vector[vector[T]]*> col.GetValuePointer()
        #this will hold number of subvectors
        cdef unsigned long numele

        cdef T* fa

        #these are defined solely for the outer array wrapper
        cdef int objsize = np.dtype('O').itemsize
        cdef int objtypecode = np.NPY_OBJECT
        # it seems *tmp is exposed via tmp[0]
        # we want to create an outer array container that dataptr points to, containing pointers
        #    from create_numpyarray()
        numele = tmp[0].size()

        #define an (numele)-dimensional outer array to hold our subvectors fa
        cdef np.npy_intp dims[1]
        dims[0] = numele
        cdef np.ndarray outer = np.PyArray_EMPTY(1, dims, objtypecode, 0)
        cdef PyObject* outerobj = <PyObject*> outer # borrow ref
        # increase one since we are putting in buffer directly
        Py_INCREF(outer)
        # now write PyObject* to buffer
        memcpy(buffer, &outerobj, sizeof(PyObject*))

        # build a dataptr pointing to outer, so we can shift and write each of the subvectors
        cdef char* dataptr = <char*> outer.data

        # loop through all subvectors
        for i in xrange(numele):
          fa = this.v2a.convert(&tmp[0][i])
          # for some reason, shift isn't working, so we're directly shifting it ourselves
          #dataptr = shift(&dataptr, objsize)
          create_numpyarray(&dataptr[i*objsize], fa, this.nptypecode, tmp[0][i].size(), this.elesize)
        return sizeof(outerobj)