如何在Cython中使用unordered_map?

6
我希望您提供一份逐步指南,介绍如何在Cython中使用unordered_map。
我已经将unordered_map.pxd文件包含在Cython/Includes/libcpp中。这个文件可以从https://gist.github.com/ikuyamada/3265267下载并导入。同时,您还需要使用其他三个文件:
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:(将foo.pyx编译成C ++)

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

当我运行test.py时,出现错误:
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

我使用的是Win7操作系统,Python 2.7 64位版本以及VS2008专业版。

2个回答

2

如果您已经编译了foo,那么我认为您不需要运行pyximport.install()。这可能会使它混乱,因为pyximport重新编译了源代码,但是您没有为pyximport提供任何编译器参数,因此它使用默认设置进行编译(即不知道foo是一个cpp源文件)。

main.py:

from foo import F
F()

foo.pyx: <-- 不要忘记 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]

当然,我是指foo.pyx。我已经更正了问题。 - Apogentus
事实上,当在同一目录中创建文件foo.pyxbld时,pyximport会接收编译参数。在我的情况下,将编译foo.cpp文件。 - Apogentus

0
我已经解决了这个问题:
1)我需要从http://www.microsoft.com/en-us/download/details.aspx?id=6922安装Visual C++ 2008功能包。
2)我需要使用以下unordered_map.pxd:
from libcpp.utility cimport pair

cdef extern from "<unordered_map>" namespace "std::tr1":
    cdef cppclass unordered_map[T, U]:
        cppclass iterator:
            pair[T, U]& operator*() nogil
            iterator operator++() nogil
            iterator operator--() nogil
            bint operator==(iterator) nogil
            bint operator!=(iterator) nogil
        unordered_map()
        unordered_map(unordered_map&)
        U& operator[](T&) nogil
        # unordered_map& operator=(unordered_map&)
        U& at(T&) nogil
        iterator begin() nogil
        void clear() nogil
        size_t count(T&) nogil
        bint empty() nogil
        iterator end() nogil
        pair[iterator, iterator] equal_range(T&) nogil
        void erase(iterator) nogil
        void erase(iterator, iterator) nogil
        size_t erase(T&) nogil
        iterator find(T&) nogil
        pair[iterator, bint] insert(pair[T, U]) nogil
        iterator insert(iterator, pair[T, U]) nogil
        void insert(input_iterator, input_iterator)
        size_t max_size() nogil
        void rehash(size_t)
        size_t size() nogil
        void swap(unordered_map&) nogil

相较于https://gist.github.com/ikuyamada/3265267,只有一行代码被修改:

cdef extern from "<tr1/unordered_map>" namespace "std::tr1": 

->

cdef extern from "<unordered_map>" namespace "std::tr1":

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接