【问题标题】:Cython cdef class with C++ vector of C++ stack objects具有 C++ 堆栈对象的 C++ 向量的 Cython cdef 类
【发布时间】:2021-02-15 07:52:13
【问题描述】:

在 Cython 中,创建 int 的 C++ 向量的 Python 扩展类非常容易

# vector_int.pyx

# distutils: language = c++
from libcpp.stack cimport stack
from libcpp.vector cimport vector
cdef class VectorInt:
    cdef vector[int] v
    
    def __cinit__(self, count):
        self.v = [0] * count  # <-- Works! Allocate variable length of zeros

    def set(self, idx, x):
        self.v[idx] = x

    def get(self, idx):
        return self.v[idx]
    

def test_vector_int_stack():
    v = VectorInt(11)
    v.set(0, 42)
    assert v.get(0) == 42

但是如何创建int的堆栈对象向量的Python扩展类?

# vector_stack_int.pyx

# distutils: language = c++
from libcpp.stack cimport stack
from libcpp.vector cimport vector

cdef class VectorStackInt:

    cdef vector[stack[long]] v
    
    def __cinit__(self, count):
        for i in range(count):
            self.v.push_back((new stack[long]()))  # <--- Error here?!

    def set(self, idx, x):
        self.v[idx].push(x)

    def get(self, idx):
        return self.v[idx].top()

这会失败,因为new 返回一个指向新堆栈的指针,

Error compiling Cython file:
------------------------------------------------------------
...

    cdef vector[stack[long]] v
    
    def __cinit__(self, count):
        for i in range(count):
            self.v.push_back((new stack[long]()))  # <--- Error here?!
                                            ^
------------------------------------------------------------

pvtracec/libs/simple.pyx:58:45: Cannot assign type 'stack[long] *' to 'stack[long]'

【问题讨论】:

  • 你试过删除new吗?即正是你在 C++ 中所做的事情
  • 是的!我刚刚意识到,正要回答我自己的答案。为什么不将您的评论升级为一些免费积分的答案。

标签: c++ python-3.x cython cythonize


【解决方案1】:

糟糕...只需删除 new

就像malloc,C++ 的new 分配内存并返回一个指向它的指针。这里想要其实想直接引用对象。


cdef class VectorStackInt:

    cdef vector[stack[long]] v
    
    def __cinit__(self, count):
        for i in range(count):
            self.v.push_back(stack[long]())  # <--- Remove `new`!

    def push(self, idx, x):
        self.v[idx].push(x)
    
    def pop(self, idx):
        self.v[idx].pop()

    def top(self, idx):
        return self.v[idx].top()
    

def test_vector_stack_int():
    v = VectorStackInt(11)
    v.push(0, 42)
    v.push(0, 43)
    v.top(0) == 43
    v.pop(0)
    v.top(0) == 42

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-30
    • 2012-02-05
    • 2018-01-06
    • 2013-09-21
    • 2016-05-28
    • 1970-01-01
    • 2020-05-05
    • 2015-08-06
    相关资源
    最近更新 更多