Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are C++11 containers supported by Cython?

Cython gives us an easy way to import C++ standard library data structures, e.g.:

  from libcpp.vector cimport vector
    from libcpp.utility cimport pair

But what about newer containers introduced with C++11: std::unordered_map, std::unordered_set etc. Are they supported in the same way? I could not find the appropriate import statement.

like image 959
clstaudt Avatar asked Oct 08 '13 11:10

clstaudt


People also ask

Does Cython use C or C++?

Cython can call into both C and C++ code, and even subclass C++ classes. The C++ support is somewhat limited, though, given how complex the C++ language is.

Does Cython generate C code?

You need to add this path only if you use cimport numpy . With older Cython releases, setting this macro will fail the C compilation, because Cython generates code that uses this deprecated C-API.

What compiler does Cython use?

Unlike most Python software, Cython requires a C compiler to be present on the system. The details of getting a C compiler varies according to the system used: Linux The GNU C Compiler (gcc) is usually present, or easily available through the package system.

Is Cython compiled or interpreted?

Cython is a compiled language that is typically used to generate CPython extension modules.


2 Answers

Current cython versions allow them.

Make sure your setup.py contains something like:

ext_module = Extension(
    "foo",
    ["foo.pyx"],
    language="c++",
    extra_compile_args=["-std=c++11"],
    extra_link_args=["-std=c++11"]
)

You can then use

from libcpp.unordered_map cimport unordered_map

like for any other STL class.

like image 127
Ami Tavory Avatar answered Sep 21 '22 18:09

Ami Tavory


Cython doesn't support them by default, but you could probably create your own interface, following the structure of https://github.com/cython/cython/blob/master/Cython/Includes/libcpp/map.pxd.

Cython now supported unordered_map and unordered_set since 0.20.2.

from libcpp.unordered_map cimport unordered_map
from libcpp.unordered_set cimport unordered_set
like image 45
kennytm Avatar answered Sep 19 '22 18:09

kennytm