散列任意精度值(boost :: multiprecision :: cpp_int)

Ale*_*mez 5 c++ hash boost arbitrary-precision c++11

我需要以任意精度获取值的哈希值(来自Boost.Multiprecision); 我用cpp_int后端.现在,我想出了以下代码:

boost::multiprecision::cpp_int x0 = 1;
const auto seed = std::hash<std::string>{}(x0.str());
Run Code Online (Sandbox Code Playgroud)

我不需要代码尽可能快,但我发现散列字符串表示非常笨拙.

所以我的问题是双重的:

  • 保持任意精度,我可以更有效地散列值吗?
  • 也许我不应该坚持保持任意精度,我应该转换为double我可以轻松散列(我仍然会使用任意精度值进行哈希表所需的比较)?

seh*_*ehe 6

您可以(ab)使用序列化支持:

序列化支持有两种形式:类numberdebug_adaptorlogged_adaptorrational_adaptor有“通过”序列化支持,需要底层后端可序列化。

后端cpp_intcpp_bin_floatcpp_dec_floatfloat128具有为Boost.Serialization&nbsp全力支持。

所以,让我拼凑一些适用于 boost 和 std 无序容器的东西:

template <typename Map>
void test(Map const& map) {
    std::cout << "\n" << __PRETTY_FUNCTION__ << "\n";
    for(auto& p : map)
        std::cout << p.second << "\t" << p.first << "\n";
}

int main() {
    using boost::multiprecision::cpp_int;

    test(std::unordered_map<cpp_int, std::string> {
        { cpp_int(1) << 111, "one"   },
        { cpp_int(2) << 222, "two"   },
        { cpp_int(3) << 333, "three" },
    });

    test(boost::unordered_map<cpp_int, std::string> {
        { cpp_int(1) << 111, "one"   },
        { cpp_int(2) << 222, "two"   },
        { cpp_int(3) << 333, "three" },
    });
}
Run Code Online (Sandbox Code Playgroud)

让我们将相关hash<>实现转发到我们自己的hash_impl使用 MultiprecisionSerialization的专业化:

namespace std {
    template <typename backend> 
    struct hash<boost::multiprecision::number<backend> > 
        : mp_hashing::hash_impl<boost::multiprecision::number<backend> > 
    {};
}

namespace boost {
    template <typename backend> 
    struct hash<multiprecision::number<backend> > 
        : mp_hashing::hash_impl<multiprecision::number<backend> > 
    {};
}
Run Code Online (Sandbox Code Playgroud)

现在,当然,这引出了一个问题,如何hash_impl实施?

template <typename T> struct hash_impl {
    size_t operator()(T const& v) const {
        using namespace boost;
        size_t seed = 0;
        {
            iostreams::stream<hash_sink> os(seed);
            archive::binary_oarchive oa(os, archive::no_header | archive::no_codecvt);
            oa << v;
        }
        return seed;
    }
};
Run Code Online (Sandbox Code Playgroud)

这看起来很简单。那是因为 Boost 很棒,编写一个hash_sink用于 Boost Iostreams的设备只是以下简单的练习:

namespace io = boost::iostreams;

struct hash_sink {
    hash_sink(size_t& seed_ref) : _ptr(&seed_ref) {}

    typedef char         char_type;
    typedef io::sink_tag category;

    std::streamsize write(const char* s, std::streamsize n) {
        boost::hash_combine(*_ptr, boost::hash_range(s, s+n));
        return n;
    }
  private:
    size_t* _ptr;
};
Run Code Online (Sandbox Code Playgroud)

完整演示:

Live On Coliru

#include <iostream>
#include <iomanip>

#include <boost/archive/binary_oarchive.hpp>
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/multiprecision/cpp_int/serialize.hpp>
#include <boost/iostreams/device/back_inserter.hpp>
#include <boost/iostreams/stream_buffer.hpp>
#include <boost/iostreams/stream.hpp>

#include <boost/functional/hash.hpp>

namespace mp_hashing {
    namespace io = boost::iostreams;

    struct hash_sink {
        hash_sink(size_t& seed_ref) : _ptr(&seed_ref) {}

        typedef char         char_type;
        typedef io::sink_tag category;

        std::streamsize write(const char* s, std::streamsize n) {
            boost::hash_combine(*_ptr, boost::hash_range(s, s+n));
            return n;
        }
      private:
        size_t* _ptr;
    };

    template <typename T> struct hash_impl {
        size_t operator()(T const& v) const {
            using namespace boost;
            size_t seed = 0;
            {
                iostreams::stream<hash_sink> os(seed);
                archive::binary_oarchive oa(os, archive::no_header | archive::no_codecvt);
                oa << v;
            }
            return seed;
        }
    };
}

#include <unordered_map>
#include <boost/unordered_map.hpp>

namespace std {
    template <typename backend> 
    struct hash<boost::multiprecision::number<backend> > 
        : mp_hashing::hash_impl<boost::multiprecision::number<backend> > 
    {};
}

namespace boost {
    template <typename backend> 
    struct hash<multiprecision::number<backend> > 
        : mp_hashing::hash_impl<multiprecision::number<backend> > 
    {};
}

template <typename Map>
void test(Map const& map) {
    std::cout << "\n" << __PRETTY_FUNCTION__ << "\n";
    for(auto& p : map)
        std::cout << p.second << "\t" << p.first << "\n";
}

int main() {
    using boost::multiprecision::cpp_int;

    test(std::unordered_map<cpp_int, std::string> {
        { cpp_int(1) << 111, "one"   },
        { cpp_int(2) << 222, "two"   },
        { cpp_int(3) << 333, "three" },
    });

    test(boost::unordered_map<cpp_int, std::string> {
        { cpp_int(1) << 111, "one"   },
        { cpp_int(2) << 222, "two"   },
        { cpp_int(3) << 333, "three" },
    });
}
Run Code Online (Sandbox Code Playgroud)

印刷

void test(const Map&) [with Map = std::unordered_map<boost::multiprecision::number<boost::multiprecision::backends::cpp_int_backend<> >, std::basic_string<char> >]
one 2596148429267413814265248164610048
three   52494017394792286184940053450822912768476066341437098474218494553838871980785022157364316248553291776
two 13479973333575319897333507543509815336818572211270286240551805124608

void test(const Map&) [with Map = boost::unordered::unordered_map<boost::multiprecision::number<boost::multiprecision::backends::cpp_int_backend<> >, std::basic_string<char> >]
three   52494017394792286184940053450822912768476066341437098474218494553838871980785022157364316248553291776
two 13479973333575319897333507543509815336818572211270286240551805124608
one 2596148429267413814265248164610048
Run Code Online (Sandbox Code Playgroud)

如您所见,Boost 和标准库之间的实现差异体现在unordered_map相同散列的不同排序中。


小智 5

只是说我刚刚向 gitdevelop 添加了本机哈希支持(对于 Boost.Hash 和 std::hash)。它适用于所有数字类型,包括 GMP 等。不幸的是,该代码直到 Boost-1.62 才会发布。

上面的答案(ab)使用序列化支持,实际上非常酷并且非常聪明;)但是,如果您想使用像 CityHash 这样的基于向量的哈希器,它就不起作用,我添加了一个通过访问来使用它的示例肢体直接到文档:https://htmlpreview.github.io/?https ://github.com/boostorg/multi precision/blob/develop/doc/html/boost_multi precision/tut/hash.html 直接肢体访问或者序列化技巧当然适用于所有以前的版本。