boost::包含提升单元的元组的哈希值

boost::hash for tuple containing boost units

本文关键字:元组 哈希值 包含提 boost 单元      更新时间:2023-10-16

我想使用键为std::tuple<A...>键的tbb::concurrent_hash_map,其中A...将包含增强单元类型。我目前使用的 HashCompare 结构看起来像这样:

template<typename K>
struct HashCompare {
static size_t hash( const K& key )
{
boost::hash<K> hasher;
return hasher(key);
}
static bool equal( const K& key1, const K& key2 ) {return key1 == key2;}
};

对于所有非升压单元类型,我尝试过这很好用,但对于增强单元则不然。我知道可以使用自定义类型扩展boost::hash函数,但我似乎无法做到这一点。由于我有很多单位,我想使用以下形式的模板来做到这一点:

std::size_t hash_value(T const& t){
boost::hash<double> hasher;
return hasher(t.value());
}

将此函数放在boost命名空间或定义单位的命名空间中不起作用。

如何将提升哈希函数扩展到自定义类型或为仅采用提升单元的HashCompare::hash函数编写模板?

事实上,Boost Units没有哈希支持。您可以添加它:

template <typename... T>
size_t hash_value(boost::units::quantity<T...> const& q) {
using boost::hash_value; // enable ADL
return hash_value(q.value());
}

最小演示:

住在科里鲁

#include <boost/functional/hash.hpp>
#include <boost/units/unit.hpp>
#include <boost/units/systems/si.hpp>
#include <boost/units/io.hpp>
#include <iostream>
using boost::hash_value;
template <typename... T>
size_t hash_value(boost::units::quantity<T...> const& q) {
using boost::hash_value; // enable ADL
return hash_value(q.value());
}
int main() {
using namespace boost::units::si;
auto l  = 23.0*meter;
auto dt = 2.0*second;
std::cout << (l) << "n";
std::cout << (dt) << "n";
std::cout << (l/dt) << "n";
std::cout << std::hex << std::showbase;
std::cout << hash_value(l) << "n";
std::cout << hash_value(dt) << "n";
std::cout << hash_value(l/dt) << "n";
}

指纹

23 m
2 s
11.5 m s^-1
0x4037000000000000
0x4000000000000000
0x4027000000000000

在泛型代码中使用boost::hash

确保重载在实例化点可访问。启用参数相关查找 (ADL(:

住在科里鲁

#include <boost/units/unit.hpp>
#include <boost/units/systems/si.hpp>
#include <boost/units/io.hpp>
#include <iostream>
#include <boost/functional/hash.hpp>
namespace boost::units {
template <typename... T>
size_t hash_value(quantity<T...> const& q) {
using boost::hash_value; // enable ADL
return hash_value(q.value());
}
}
namespace detail {
template<typename K>
struct HashCompare {
static size_t hash( const K& key )
{
boost::hash<K> hasher;
return hasher(key);
}
static bool equal( const K& key1, const K& key2 ) {return key1 == key2;}
};
template <typename T>
size_t test_mycompare(T const& v) {
return HashCompare<T>::hash(v);
}
}
int main() {
using namespace boost::units::si;
using boost::hash_value;
auto l  = 23.0*meter;
auto dt = 2.0*second;
std::cout << (l) << "n";
std::cout << (dt) << "n";
std::cout << (l/dt) << "n";
std::cout << std::hex << std::showbase;
std::cout << hash_value(l) << "n";
std::cout << hash_value(dt) << "n";
std::cout << hash_value(l/dt) << "n";
std::cout << detail::test_mycompare(l) << "n";
std::cout << detail::test_mycompare(dt) << "n";
std::cout << detail::test_mycompare(l/dt) << "n";
}

指纹

23 m
2 s
11.5 m s^-1
0x4037000000000000
0x4000000000000000
0x4027000000000000
0x4037000000000000
0x4000000000000000
0x4027000000000000