我想了解更多关于 Cython 令人敬畏的typed-memoryviews和 memory layout 的信息indirect_contiguous
。
根据文档 indirect_contiguous
,当“指针列表是连续的”时使用。
还有一个示例用法:
# contiguous list of pointers to contiguous lists of ints
cdef int[::view.indirect_contiguous, ::1] b
因此,如果我错了,请纠正我,但我假设“指向连续整数列表的指针的连续列表”意味着类似于由以下 c++ 虚拟代码创建的数组:
// we want to create a 'contiguous list of pointers to contiguous lists of ints'
int** array;
// allocate row-pointers
// This is the 'contiguous list of pointers' related to the first dimension:
array = new int*[ROW_COUNT]
// allocate some rows, each row is a 'contiguous list of ints'
array[0] = new int[COL_COUNT]{1,2,3}
因此,如果我理解正确,那么在我的 Cython 代码中应该可以从int**
这样的代码中获取内存视图:
cdef int** list_of_pointers = get_pointers()
cdef int[::view.indirect_contiguous, ::1] view = <int[:ROW_COUNT:view.indirect_contiguous,COL_COUNT:1]> list_of_pointers
但我得到编译错误:
cdef int[::view.indirect_contiguous, ::1] view = <int[:ROW_COUNT:view.indirect_contiguous,:COL_COUNT:1]> list_of_pointers
^
------------------------------------------------------------
memview_test.pyx:76:116: Pointer base type does not match cython.array base type
我做错什么了?我错过了任何演员还是我误解了indirect_contiguous的概念?