带有 C++ 堆栈对象的 C++ 向量的 Cython cdef class

Cython cdef class with C++ vector of C++ stack objects

在 Cython 中,创建 int

的 C++ 向量的 Python 扩展 class 真的很容易
# 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

但是我如何创建 Python 扩展 class 向量的堆栈对象

# 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 returns 指向新堆栈的指针,

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

malloc 类似,C++ 的 new 分配内存并 returns 指向它的指针。这里want其实是想直接引用对象。


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