Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vector assign in Cython

Here is my program of cython

cdef struct Node:
    int v
    Node* next
    Node* pre

def f(int N):
    cdef:
        vector[Node*] narray
        int i
    narray.assign(N, 0)
    for i in xrange(N):
        narray[i] = 0

Cython compiling result:

Error compiling Cython file:
------------------------------------------------------------
...
    cdef:
        vector[Node*] narray
        int i
    narray.assign(N, 0)
    for i in xrange(N):
        narray[i] = 0
             ^
------------------------------------------------------------

testLinkList.pyx:107:14: Compiler crash in AnalyseExpressionsTransform

But I can use push_back() to append values at the end of the vector or use int instead of Node*. What is wrong?

like image 806
user2107996 Avatar asked Feb 25 '13 16:02

user2107996


People also ask

Does Cython use C or C++?

Cython can call into both C and C++ code, and even subclass C++ classes.

How does Cython work?

Cython works by producing a standard Python module. However, the behavior differs from standard Python in that the module code, originally written in Python, is translated into C. While the resulting code is fast, it makes many calls into the CPython interpreter and CPython standard libraries to perform actual work.


1 Answers

What version of Cython are you using? Version 0.20.1 works for me with this code:

# distutils: language=c++

from libcpp.vector cimport vector

cdef struct Node:
    int v
    Node* next
    Node* pre

def f(int N):
    cdef:
        vector[Node*] narray
        int i
    narray.assign(N, NULL)
    for i in range(N):
        narray[i] = NULL

And with this setup.py file:

from distutils.core import setup
from Cython.Build import cythonize

setup(ext_modules=cythonize("test_vector.pyx"))
like image 168
lothario Avatar answered Sep 21 '22 05:09

lothario