如何实现 <Obj>Cython 的向量<向量>(C++ 到 Python)?
How do I implement vector <vector <Obj>> for Cython (C++ to Python)?
请参阅此处的代码: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>
(通常是浮点数或整数)转换为数组[特别是 NumPy 数组]。我很难理解它,但我跑题了。
我需要潜在地扩展它,以便有一些东西也可以实现vector<vector <object>>
。我认为它应该是我在上述链接中突出显示的代码的副本。
我从哪里开始?
我已经
解决了这个问题,因为我不是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)
相关文章:
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- 呼叫运营商<<临时
- 如何防止clang格式在流运算符调用之间添加换行符<<
- <<操作员在下面的行中工作
- EASTL矢量<向量<int>>连续的
- C - 创建矢量&lt; vector&lt; double&gt;&gt;矩阵具有分配而不是inizializ
- 为什么将此对向量&lt; map&lt; int,int&gt;&gt;中的地图进行更新.失败
- C :对矢量进行排序&lt; struct&gt;(结构有2个整数)基于结构的整数之一
- 明确的专业化“ CheckIntmap&lt;&gt;”实例化
- 什么是模板&lt;&gt;inline bla bla
- 编辑C Qlist&lt; object*&gt; gt;QML代码和一些QML警告中的模型
- eigen :: llt&lt;eigen :: matrixxd&gt;具有不完整的类型
- 错误,包括&lt; ctype&gt;在原子上使用C 11
- std::vector<;uint8_t>;当C++11/14启用时,手动复制而不是调用memcpy
- 如何加入向量&lt; int&gt;到C 中的单个INT
- 是std :: set&lt; std :: future&gt;不可能存在
- 是numeric_limits&lt; int&gt; :: is_modulo从逻辑上矛盾
- opencv 2.4.7在iOS错误背景_segm.hpp #include&lt; list&gt;未找到
- 在修改列表后,std :: list&lt; t&gt; :: end()的值是否会更改
- ///<评论></评论>在Visual Studio中