[英]std::bitset hash function algorithm
有人知道bitset使用的哈希函數是什么算法,
這是來自網站: http : //en.cppreference.com/w/cpp/utility/bitset/hash
#include <iostream>
#include <bitset>
#include <functional>
int main()
{
std::bitset<4> b1(1);
std::bitset<4> b2(2);
std::bitset<4> b3(b2);
std::bitset<4> b4(8);
std::cout<<b4<<'\n';
std::hash<std::bitset<4>> hash_fn;
size_t h1 = hash_fn(b1);
size_t h2 = hash_fn(b2);
size_t h3 = hash_fn(b4);
std::cout << h1 << '\n';
std::cout << h2 << '\n';
std::cout << h3 << '\n';
}
而輸出是
1000
4334672815104069193
16667047557902998627
2258353126044249582
http://en.cppreference.com/w/cpp/utility/bitset/hash
另外為什么不將這些位轉換為unsigend long並生成一個哈希值?
正如Igor所指出的 ,C ++標准沒有指定算法, 它只 要求哈希值僅依賴於對象,並且在程序的持續時間內是相同的: http : //eel.is/c++draft/hash.requirements
20.5.3.4哈希要求[hash.requirements] 1如果出現以下情況,則H類型滿足哈希要求:
- (1.1)它是一個函數對象類型,
- (1.2)它滿足CopyConstructible和Destructible的要求,並且
- (1.3)表29中所示的表達式是有效的並且具有指示的語義。
2給定Key是類型H的函數對象的參數類型,在表29中,h是類型的值(可能是const)H,u是Key類型的左值,k是可轉換為的類型的值(可能是const) )關鍵。
表29 - 哈希要求
- 表達式返回類型要求
- h(k)size_t返回的值僅取決於程序持續時間的參數k。 [注意:因此,對於給定的程序執行,對具有相同k值的表達式h(k)的所有求值產生相同的結果。 - 結束注釋] [注意:對於兩個不同的值t1和t2,h(t1)和h(t2)比較的概率應該非常小,接近1.0 / numeric_limits :: max()。 - 結束說明]
- h(u)size_t不得修改你。
Gcc的bitset的libstdc ++實現使用std :: hash: https : //github.com/gcc-mirror/gcc/blob/master/libstdc%2B%2B-v3/include/debug/bitset
#if __cplusplus >= 201103L
// DR 1182.
/// std::hash specialization for bitset.
template<size_t _Nb>
struct hash<__debug::bitset<_Nb>>
: public __hash_base<size_t, __debug::bitset<_Nb>>
{
size_t
operator()(const __debug::bitset<_Nb>& __b) const noexcept
{ return std::hash<_GLIBCXX_STD_C::bitset<_Nb>>()(__b._M_base()); }
};
#endif
// DR 1182.
/// std::hash specialization for bitset.
template<size_t _Nb>
struct hash<_GLIBCXX_STD_C::bitset<_Nb>>
: public __hash_base<size_t, _GLIBCXX_STD_C::bitset<_Nb>>
{
size_t
operator()(const _GLIBCXX_STD_C::bitset<_Nb>& __b) const noexcept
{
const size_t __clength = (_Nb + __CHAR_BIT__ - 1) / __CHAR_BIT__;
return std::_Hash_impl::hash(__b._M_getdata(), __clength);
}
};
LLVM的libcxx使用自己的bitset實現,xoring所有單詞: https : //github.com/llvm-mirror/libcxx/blob/2c4b8af9aada61d83610330416eb8a39a8aa5494/include/bitset#L417
template <size_t _Size>
struct _LIBCPP_TEMPLATE_VIS hash<bitset<_Size> >
: public unary_function<bitset<_Size>, size_t>
{
_LIBCPP_INLINE_VISIBILITY
size_t operator()(const bitset<_Size>& __bs) const _NOEXCEPT
{return __bs.__hash_code();}
};
template <size_t _N_words, size_t _Size>
inline
size_t
__bitset<_N_words, _Size>::__hash_code() const _NOEXCEPT
{
size_t __h = 0;
for (size_type __i = 0; __i < _N_words; ++__i)
__h ^= __first_[__i];
return __h;
}
1字bitset的更簡單的變體:
inline
size_t
__bitset<1, _Size>::__hash_code() const _NOEXCEPT
{
return __first_;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.