Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use unordered_map in cython?

I want a step by step guide, how to use unordered_map in cython.

I've included file unordered_map.pxd into Cython/Includes/libcpp from https://gist.github.com/ikuyamada/3265267 and use 3 other files:

main.py:

import pyximport;
pyximport.install()
from foo import F

F()

foo.pyx:

from libcpp.unordered_map cimport unordered_map


def F():
    cdef unordered_map[int, int] my_map
    my_map[1]=11
    my_map[2]=12
    print my_map[1],my_map[2]

foo.pyxbld: (to compile foo.pyx into C++)

def make_ext(modname, pyxfilename):
    from distutils.extension import Extension
    return Extension(name=modname,
                     sources=[pyxfilename],
                     language='C++')

When I run test.py, I get error:

foo.cpp
C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\INCLUDE\xlocale(342) : warning C4530: C++ exception handler used, but unwind semantics are not enabled. Specify /EHsc
C:\Users\kitov\.pyxbld\temp.win-amd64-2.7\Release\pyrex\foo.cpp(316) : fatal error C1083: Cannot open include file: 'tr1/unordered_map': No such file or directory

I use Win7, Python 2.7 64bit, VS2008 Professional.

like image 683
Apogentus Avatar asked Nov 02 '22 05:11

Apogentus


1 Answers

if you've already compiled foo, I don't think you'll need to run pyximport.install(). That might have messed it up since pyximport re-compiles the source code, but you didn't provide any compiler-args to pyximport, so it compiled with default settings (i.e. not knowing foo is a cpp source file).

main.py:

from foo import F
F()

foo.pyx: <-- don't forget the x

#distutils: language = c++
from libcpp.unordered_map cimport unordered_map

def F():
    cdef unordered_map[int, int] my_map
    my_map[1]=11
    my_map[2]=12
    print my_map[1],my_map[2]
like image 130
richizy Avatar answered Nov 15 '22 04:11

richizy