boost::包含提升单元的元组的哈希值
boost::hash for tuple containing boost units
我想使用键为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
相关文章:
- boost::包含提升单元的元组的哈希值
- 如何使用 STL 哈希容器中的哈希值检索元素?
- 哈希函数中同一键的不同值和良好的哈希值排序
- 字符串单词的唯一哈希值
- 是否可以将哈希值作为编译时常数
- CMake检查下载文件的哈希值(MD5/SHA256)
- 如何泛化接口以要求某个元组的返回值
- 将函数元组应用于值并返回元组
- 正在将哈希值存储到int
- 如何计算没有标签信息的 mp3 文件的哈希值
- 如何定义函数序列的哈希值(C++)
- C++-对象图的哈希值类似于boost::序列化
- 如何获取哈希值,C++ hash_map
- 在Qt中创建目录的哈希值
- 杂音哈希 - 哈希值不一致
- 逐行散列文件时错误的 md5 哈希值
- 为什么不同程序执行之间的哈希值不一致
- 如何有效地存储和排序生日攻击的哈希值
- unordered_map存储桶中的所有元素是否具有相同的哈希值
- 我的std::元组的哈希值.任何改进