C ++:使用用户定义的哈希/平等帮助创建unordered_map

时间:2011-11-30 23:41:41

标签: c++ functional-programming c++11 bind unordered-map

我尝试使用用户定义的哈希函数和等式谓词为整数内置类型的矩阵行创建一个std :: unordered_map。我使用std :: bind,因为我需要哈希和相等函数来处理变量范围。如何编译以下代码并按预期工作?我猜测我的错误是在列表的底部,我使用std :: bind并实例化std :: unordered_map。

一些澄清:

我不能使用boost :: hash_combine因为我关心存储在矩阵行中的整数中的各个位,所以除非我创建自己的迭代器,否则boost :: hash_combine会合并整数,导致错误的结果。另外,我需要哈希和相等函数来处理从1到200,000的范围,因此在模板参数中指定范围将不是一个合理的选择。


template <class T,class F,class A>
struct row_hash_right : std::function<size_t(
        ublas::matrix_row<ublas::matrix<T,F,A>>,
        unsigned, unsigned)>
{
        typedef ublas::matrix_row<ublas::matrix<T,F,A>> row_type;

        // I want to use std::bind to bind the last two arguments.
        size_t operator()(row_type& a, unsigned s, unsigned e)
        {
                // Implementation of hash function.
        }
};

template <class T,class F,class A>
struct row_equal_right : std::function<bool(
        ublas::matrix_row<ublas::matrix<T,F,A>>,
        ublas::matrix_row<ublas::matrix<T,F,A>>,
        unsigned, unsigned)>
{
        typedef ublas::matrix_row<ublas::matrix<T,F,A>> row_type;

        bool operator()(row_type& a, row_type& b, unsigned s, unsigned e)
        {
                // Implementation of equality predicate.
        }
};

// Inside a function.
for (unsigned i = 0; i < len; ++i) {
        for (unsigned j = i + 1; j < len; ++j) {
                auto x = std::bind(r_hash, _1, i, j);
                auto y = std::bind(r_equal, _1, _2, i, j);
                // ERROR:
                std::unordered_map<row_type, unsigned,
                        decltype(x), decltype(y)> m(256, x, y);
        }
}

错误:

这是(我认为)编译尝试时产生的错误中最重要的部分:

  

/ usr / include / c ++ / 4.6 / bits / stl_pair.h:92:11:错误:'std :: pair&lt; _T1,   _T2&gt; :: first'具有不完整的类型       /usr/include/boost/numeric/ublas/fwd.hpp:73:11:错误:'const struct的声明   提高::数字:: uBLAS库:: matrix_row,   boost :: numeric :: ublas :: unbounded_array&gt; &GT; &GT;”

如果你想看到整件事,我已经把它全部放在这里:


In file included from /usr/include/c++/4.6/bits/stl_algobase.h:65:0,
                 from /usr/include/c++/4.6/bits/char_traits.h:41,
                 from /usr/include/c++/4.6/ios:41,
                 from /usr/include/c++/4.6/ostream:40,
                 from /usr/include/c++/4.6/iostream:40,
                 from src/test/read_test.cpp:1:
/usr/include/c++/4.6/bits/stl_pair.h: In instantiation of ‘std::pair, boost::numeric::ublas::unbounded_array > > >, unsigned int>’:
/usr/include/c++/4.6/bits/stl_function.h:486:12:   instantiated from ‘std::_Select1st, boost::numeric::ublas::unbounded_array > > >, unsigned int> >’
/usr/include/c++/4.6/bits/hashtable_policy.h:789:20:   instantiated from ‘std::__detail::_Hash_code_base, boost::numeric::ublas::unbounded_array > > >, std::pair, boost::numeric::ublas::unbounded_array > > >, unsigned int>, std::_Select1st, boost::numeric::ublas::unbounded_array > > >, unsigned int> >, std::_Bind, boost::numeric::ublas::unbounded_array > >(std::_Placeholder, std::_Placeholder, unsigned int, unsigned int)>, std::_Bind, boost::numeric::ublas::unbounded_array > >(std::_Placeholder, unsigned int, unsigned int)>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, false>’
/usr/include/c++/4.6/bits/hashtable.h:105:11:   instantiated from ‘std::_Hashtable, boost::numeric::ublas::unbounded_array > > >, std::pair, boost::numeric::ublas::unbounded_array > > >, unsigned int>, std::allocator, boost::numeric::ublas::unbounded_array > > >, unsigned int> >, std::_Select1st, boost::numeric::ublas::unbounded_array > > >, unsigned int> >, std::_Bind, boost::numeric::ublas::unbounded_array > >(std::_Placeholder, std::_Placeholder, unsigned int, unsigned int)>, std::_Bind, boost::numeric::ublas::unbounded_array > >(std::_Placeholder, unsigned int, unsigned int)>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, false, false, true>’
/usr/include/c++/4.6/bits/unordered_map.h:44:11:   instantiated from ‘std::__unordered_map, boost::numeric::ublas::unbounded_array > > >, unsigned int, std::_Bind, boost::numeric::ublas::unbounded_array > >(std::_Placeholder, unsigned int, unsigned int)>, std::_Bind, boost::numeric::ublas::unbounded_array > >(std::_Placeholder, std::_Placeholder, unsigned int, unsigned int)>, std::allocator, boost::numeric::ublas::unbounded_array > > >, unsigned int> >, false>’
/usr/include/c++/4.6/bits/unordered_map.h:256:11:   instantiated from ‘std::unordered_map, boost::numeric::ublas::unbounded_array > > >, unsigned int, std::_Bind, boost::numeric::ublas::unbounded_array > >(std::_Placeholder, unsigned int, unsigned int)>, std::_Bind, boost::numeric::ublas::unbounded_array > >(std::_Placeholder, std::_Placeholder, unsigned int, unsigned int)>, std::allocator, boost::numeric::ublas::unbounded_array > > >, unsigned int> > >’
./sal/alg/ehh.hpp:144:31:   instantiated from ‘sal::ehh_results sal::compute_ehh(boost::numeric::ublas::matrix&, unsigned int) [with FloatType = double, T = unsigned int, F = boost::numeric::ublas::basic_row_major, A = boost::numeric::ublas::unbounded_array >]’
src/test/read_test.cpp:11:51:   instantiated from here
/usr/include/c++/4.6/bits/stl_pair.h:92:11: error: ‘std::pair::first’ has incomplete type
/usr/include/boost/numeric/ublas/fwd.hpp:73:11: error: declaration of ‘const struct boost::numeric::ublas::matrix_row, boost::numeric::ublas::unbounded_array > > >’

1 个答案:

答案 0 :(得分:1)

如果要散列一系列内容,则需要hash_combine()之类的内容。我通常会将此功能从Boost中取出(令人惊讶的是,它不包含在标准中!)。这是我在std::array上使用它的方式,我相信你可以把它操作成矩阵行上的东西:

#include <array>

template <class T>
inline void hash_combine(std::size_t & seed, const T & v)
{
    std::hash<T> hasher;
    seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
}

namespace std
{
    template<typename T, size_t N> struct hash<array<T, N>>
    {
        inline size_t operator()(const array<T, N> & a) const
        {
            size_t seed = 0;
            for (size_t i = 0; i != N; ++i)
            {
                ::hash_combine(seed, a[i]);
            }
            return seed;
        }
    };
}

(以上专业化允许您使用std::unordered_set<std::array<int, 10>>等。)

如果矩阵行没有带有等式谓词,您还可以为矩阵行添加std::equal_to的特化。