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

问题描述

在 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,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

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