c++ - 如何在 cython 中将 unordered_map 从 c struct 转换为 int?

标签 c++ struct cython unordered-map

如何在 cython 中将 unordered_map 从 c struct 转换为 int?

我尝试运行以下代码: 主.py

# !/usr/bin/env python
# encoding: utf-8
import pyximport


pyximport.install()
from foo import Fun

Fun()

Foo.pyx

# cython: experimental_cpp_class_def=True
#!/usr/bin/env python
# encoding: utf-8
from libcpp.unordered_map cimport unordered_map
from libcpp.map cimport map
from libcpp.string cimport string

cdef cppclass mystruct:
    int i,j

    bint equal_to "operator=="(const mystruct t) const:
         return this.i == t.i and this.j == t.j

    bint myhash "hash"(const mystruct t) const:
         return hash(i)+hash(i) # how to calculate it properly?


def Fun():
    cdef:
        unordered_map[mystruct,int] dDict
        mystruct A

    A.i=111

    dDict[A]=1234
    print A.i
    print dDict[A]

foo.pyxbld

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

我收到以下错误:

        C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\INCLUDE\functional(109) : while compiling class template member function 'bool std::equal_to<_Ty>::operator ()(const _Ty &,const _Ty &) const'
        with
        [
            _Ty=__pyx_t_3foo_mystruct
        ]
        C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\INCLUDE\xhash(184) : see reference to class template instantiation 'std::equal_to<_Ty>' being compiled
        with
        [
            _Ty=__pyx_t_3foo_mystruct
        ]
        C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\INCLUDE\unordered_map(74) : see reference to class template instantiation 'stdext::_Hash_compare<_Kty,_Hasher,_Keyeq>' being compiled
        with
        [
            _Kty=__pyx_t_3foo_mystruct,
            _Hasher=std::tr1::hash<__pyx_t_3foo_mystruct>,
            _Keyeq=std::equal_to<__pyx_t_3foo_mystruct>
        ]
        C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\INCLUDE\xhash(191) : see reference to class template instantiation 'std::tr1::_Umap_traits<_Kty,_Ty,_Tr,_Alloc,_Mfl>' being compiled
        with
        [
            _Kty=__pyx_t_3foo_mystruct,
            _Ty=int,
            _Tr=stdext::_Hash_compare<__pyx_t_3foo_mystruct,std::tr1::hash<__pyx_t_3foo_mystruct>,std::equal_to<__pyx_t_3foo_mystruct>>,
            _Alloc=std::allocator<std::pair<const __pyx_t_3foo_mystruct,int>>,
            _Mfl=false
        ]
        C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\INCLUDE\unordered_map(86) : see reference to class template instantiation 'stdext::_Hash<_Traits>' being compiled
        with
        [
            _Traits=std::tr1::_Umap_traits<__pyx_t_3foo_mystruct,int,stdext::_Hash_compare<__pyx_t_3foo_mystruct,std::tr1::hash<__pyx_t_3foo_mystruct>,std::equal_to<__pyx_t_3foo_mystruct>>,std::allocator<std::pair<const __pyx_t_3foo_mystruct,int>>,false>
        ]
        C:\Users\LENOVO\.pyxbld\temp.win-amd64-2.7\Release\pyrex\foo.cpp(796) : see reference to class template instantiation 'std::tr1::unordered_map<_Kty,_Ty>' being compiled
        with
        [
            _Kty=__pyx_t_3foo_mystruct,
            _Ty=int
        ]
C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\INCLUDE\functional(1265) : error C2440: 'type cast' : cannot convert from 'const __pyx_t_3foo_mystruct' to 'size_t'
        No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
        C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\INCLUDE\functional(1264) : while compiling class template member function 'size_t std::tr1::hash<_Kty>::operator ()(const _Kty &) const'
        with
        [
            _Kty=__pyx_t_3foo_mystruct
        ]
        C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\INCLUDE\xhash(183) : see reference to class template instantiation 'std::tr1::hash<_Kty>' being compiled
        with
        [
            _Kty=__pyx_t_3foo_mystruct
        ]

我应该如何正确定义散列函数并更正程序以使其工作? 预先感谢您的帮助。

最佳答案

问题是没有正确计算哈希值;问题是告诉标准 C++ 库使用您定义的库。为此,您要么

  • 必须重载std::hash<mystruct>但据我所知,这不能通过 Cython 完成

  • 或传递额外的模板参数,强制您重新包装所有 unordered_map东西因为 Cython 只包装了两个参数默认版本。这还不包括您必须破解才能从 Cython 传递非类型模板参数的事实,因为这还不受支持。

所以我的简短回答是:编写一个真正的 C++ 包装器文件。

关于c++ - 如何在 cython 中将 unordered_map 从 c struct 转换为 int?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22561332/

相关文章:

c++ - 将#include 指令包装到宏中

c++ - C++ 中的反向迭代器和负跨步迭代器,在开始之前使用一个作为哨兵

c++ - 断言在具有单元测试的 C++ 程序中的作用是什么?

c++ - 如何向 MIPS 中的函数添加 4 个以上的参数?

javax.faces.FacesException 。它是属于 Spring Security 3.x 的一个异常(exception)

c - fscanf 命令中标记前的预期主表达式

python - 使用 PyInstaller 构建 Cython 编译的 python 代码

python - Cython:如何制作具有不同签名的函数数组

c# - 如何使用反射获取 ValueType 类型的默认值

fortran - 将 Fortran 逻辑与 Cython Bint 接口(interface)的问题