Commit 055f6a47 by Paolo Carlini Committed by Paolo Carlini

functional_hash.h (_Fnv_hash_base<>::hash): Change to template.

2010-03-02  Paolo Carlini  <paolo.carlini@oracle.com>  

	* include/bits/functional_hash.h (_Fnv_hash_base<>::hash): Change
	to template.
	* include/tr1/functional_hash.h (_Fnv_hash_base<>::hash): Likewise.
	* include/bits/vector.tcc (hash): Adjust.
	* include/bits/basic_string.h (hash): Likewise.
	* include/std/bitset (hash): Likewise.
	* src/hash-string-aux.cc (hash): Likewise.

From-SVN: r157185
parent 273e719b
2010-03-02 Paolo Carlini <paolo.carlini@oracle.com>
* include/bits/functional_hash.h (_Fnv_hash_base<>::hash): Change
to template.
* include/tr1/functional_hash.h (_Fnv_hash_base<>::hash): Likewise.
* include/bits/vector.tcc (hash): Adjust.
* include/bits/basic_string.h (hash): Likewise.
* include/std/bitset (hash): Likewise.
* src/hash-string-aux.cc (hash): Likewise.
2010-03-02 Jonathan Wakely <jwakely.gcc@gmail.com> 2010-03-02 Jonathan Wakely <jwakely.gcc@gmail.com>
* include/std/mutex (lock_guard::lock_guard): Do not lock mutex when * include/std/mutex (lock_guard::lock_guard): Do not lock mutex when
......
...@@ -2898,10 +2898,8 @@ _GLIBCXX_BEGIN_NAMESPACE(std) ...@@ -2898,10 +2898,8 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
{ {
size_t size_t
operator()(const wstring& __s) const operator()(const wstring& __s) const
{ { return std::_Fnv_hash::hash(__s.data(),
const char* __p = reinterpret_cast<const char*>(__s.data()); __s.length() * sizeof(wchar_t)); }
return std::_Fnv_hash::hash(__p, __s.length() * sizeof(wchar_t));
}
}; };
#endif #endif
#endif /* _GLIBCXX_COMPATIBILITY_CXX0X */ #endif /* _GLIBCXX_COMPATIBILITY_CXX0X */
...@@ -2914,10 +2912,8 @@ _GLIBCXX_BEGIN_NAMESPACE(std) ...@@ -2914,10 +2912,8 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
{ {
size_t size_t
operator()(const u16string& __s) const operator()(const u16string& __s) const
{ { return std::_Fnv_hash::hash(__s.data(),
const char* __p = reinterpret_cast<const char*>(__s.data()); __s.length() * sizeof(char16_t)); }
return std::_Fnv_hash::hash(__p, __s.length() * sizeof(char16_t));
}
}; };
/// std::hash specialization for u32string. /// std::hash specialization for u32string.
...@@ -2927,10 +2923,8 @@ _GLIBCXX_BEGIN_NAMESPACE(std) ...@@ -2927,10 +2923,8 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
{ {
size_t size_t
operator()(const u32string& __s) const operator()(const u32string& __s) const
{ { return std::_Fnv_hash::hash(__s.data(),
const char* __p = reinterpret_cast<const char*>(__s.data()); __s.length() * sizeof(char32_t)); }
return std::_Fnv_hash::hash(__p, __s.length() * sizeof(char32_t));
}
}; };
#endif #endif
......
...@@ -122,11 +122,13 @@ namespace std ...@@ -122,11 +122,13 @@ namespace std
template<size_t> template<size_t>
struct _Fnv_hash_base struct _Fnv_hash_base
{ {
template<typename _Tp>
static size_t static size_t
hash(const char* __first, size_t __length, size_t __hash = 0) hash(const _Tp* __ptr, size_t __clength, size_t __hash = 0)
{ {
for (; __length; --__length) const char* __cptr = reinterpret_cast<const char*>(__ptr);
__hash = (__hash * 131) + *__first++; for (; __clength; --__clength)
__hash = (__hash * 131) + *__cptr++;
return __hash; return __hash;
} }
}; };
...@@ -134,13 +136,15 @@ namespace std ...@@ -134,13 +136,15 @@ namespace std
template<> template<>
struct _Fnv_hash_base<4> struct _Fnv_hash_base<4>
{ {
template<typename _Tp>
static size_t static size_t
hash(const char* __first, size_t __length, hash(const _Tp* __ptr, size_t __clength,
size_t __hash = static_cast<size_t>(2166136261UL)) size_t __hash = static_cast<size_t>(2166136261UL))
{ {
for (; __length; --__length) const char* __cptr = reinterpret_cast<const char*>(__ptr);
for (; __clength; --__clength)
{ {
__hash ^= static_cast<size_t>(*__first++); __hash ^= static_cast<size_t>(*__cptr++);
__hash *= static_cast<size_t>(16777619UL); __hash *= static_cast<size_t>(16777619UL);
} }
return __hash; return __hash;
...@@ -150,13 +154,15 @@ namespace std ...@@ -150,13 +154,15 @@ namespace std
template<> template<>
struct _Fnv_hash_base<8> struct _Fnv_hash_base<8>
{ {
template<typename _Tp>
static size_t static size_t
hash(const char* __first, size_t __length, hash(const _Tp* __ptr, size_t __clength,
size_t __hash = static_cast<size_t>(14695981039346656037ULL)) size_t __hash = static_cast<size_t>(14695981039346656037ULL))
{ {
for (; __length; --__length) const char* __cptr = reinterpret_cast<const char*>(__ptr);
for (; __clength; --__clength)
{ {
__hash ^= static_cast<size_t>(*__first++); __hash ^= static_cast<size_t>(*__cptr++);
__hash *= static_cast<size_t>(1099511628211ULL); __hash *= static_cast<size_t>(1099511628211ULL);
} }
return __hash; return __hash;
...@@ -171,14 +177,12 @@ namespace std ...@@ -171,14 +177,12 @@ namespace std
template<typename _Tp> template<typename _Tp>
static size_t static size_t
hash(const _Tp& __val) hash(const _Tp& __val)
{ return hash(reinterpret_cast<const char*>(&__val), { return hash(&__val, sizeof(__val)); }
sizeof(__val)); }
template<typename _Tp> template<typename _Tp>
static size_t static size_t
__hash_combine(const _Tp& __val, size_t __hash) __hash_combine(const _Tp& __val, size_t __hash)
{ return hash(reinterpret_cast<const char*>(&__val), { return hash(&__val, sizeof(__val), __hash); }
sizeof(__val), __hash); }
}; };
/// Specialization for float. /// Specialization for float.
...@@ -201,7 +205,7 @@ namespace std ...@@ -201,7 +205,7 @@ namespace std
/// Specialization for long double. /// Specialization for long double.
template<> template<>
size_t _GLIBCXX_PURE size_t
hash<long double>::operator()(long double __val) const; hash<long double>::operator()(long double __val) const;
// @} group hashes // @} group hashes
......
...@@ -694,10 +694,8 @@ _GLIBCXX_BEGIN_NAMESPACE(std) ...@@ -694,10 +694,8 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
const size_t __words = __b.size() / _S_word_bit; const size_t __words = __b.size() / _S_word_bit;
if (__words) if (__words)
{ {
const char* __data const size_t __clength = __words * sizeof(_Bit_type);
= reinterpret_cast<const char*>(__b._M_impl._M_start._M_p); __hash = std::_Fnv_hash::hash(__b._M_impl._M_start._M_p, __clength);
const size_t __size = __words * sizeof(_Bit_type);
__hash = std::_Fnv_hash::hash(__data, __size);
} }
const size_t __extrabits = __b.size() % _S_word_bit; const size_t __extrabits = __b.size() % _S_word_bit;
...@@ -706,13 +704,12 @@ _GLIBCXX_BEGIN_NAMESPACE(std) ...@@ -706,13 +704,12 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
_Bit_type __hiword = *__b._M_impl._M_finish._M_p; _Bit_type __hiword = *__b._M_impl._M_finish._M_p;
__hiword &= ~((~static_cast<_Bit_type>(0)) << __extrabits); __hiword &= ~((~static_cast<_Bit_type>(0)) << __extrabits);
const char* __data = reinterpret_cast<const char*>(&__hiword); const size_t __clength
const size_t __size
= (__extrabits + __CHAR_BIT__ - 1) / __CHAR_BIT__; = (__extrabits + __CHAR_BIT__ - 1) / __CHAR_BIT__;
if (__words) if (__words)
__hash = std::_Fnv_hash::hash(__data, __size, __hash); __hash = std::_Fnv_hash::hash(&__hiword, __clength, __hash);
else else
__hash = std::_Fnv_hash::hash(__data, __size); __hash = std::_Fnv_hash::hash(&__hiword, __clength);
} }
return __hash; return __hash;
......
...@@ -115,9 +115,9 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D) ...@@ -115,9 +115,9 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D)
{ return _M_w[_S_whichword(__pos)]; } { return _M_w[_S_whichword(__pos)]; }
#ifdef __GXX_EXPERIMENTAL_CXX0X__ #ifdef __GXX_EXPERIMENTAL_CXX0X__
const char* const _WordT*
_M_getdata() const _M_getdata() const
{ return reinterpret_cast<const char*>(_M_w); } { return _M_w; }
#endif #endif
_WordT& _WordT&
...@@ -406,9 +406,9 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D) ...@@ -406,9 +406,9 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D)
{ return _M_w; } { return _M_w; }
#ifdef __GXX_EXPERIMENTAL_CXX0X__ #ifdef __GXX_EXPERIMENTAL_CXX0X__
const char* const _WordT*
_M_getdata() const _M_getdata() const
{ return reinterpret_cast<const char*>(&_M_w); } { return &_M_w; }
#endif #endif
_WordT& _WordT&
...@@ -1501,8 +1501,8 @@ _GLIBCXX_BEGIN_NAMESPACE(std) ...@@ -1501,8 +1501,8 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
size_t size_t
operator()(const _GLIBCXX_STD_D::bitset<_Nb>& __b) const operator()(const _GLIBCXX_STD_D::bitset<_Nb>& __b) const
{ {
const size_t __size = (_Nb + __CHAR_BIT__ - 1) / __CHAR_BIT__; const size_t __clength = (_Nb + __CHAR_BIT__ - 1) / __CHAR_BIT__;
return std::_Fnv_hash::hash(__b._M_getdata(), __size); return std::_Fnv_hash::hash(__b._M_getdata(), __clength);
} }
}; };
......
...@@ -85,12 +85,14 @@ namespace tr1 ...@@ -85,12 +85,14 @@ namespace tr1
template<size_t> template<size_t>
struct _Fnv_hash_base struct _Fnv_hash_base
{ {
template<typename _Tp>
static size_t static size_t
hash(const char* __first, size_t __length) hash(const _Tp* __ptr, size_t __clength)
{ {
size_t __result = 0; size_t __result = 0;
for (; __length > 0; --__length) const char* __cptr = reinterpret_cast<const char*>(__ptr);
__result = (__result * 131) + *__first++; for (; __clength; --__clength)
__result = (__result * 131) + *__cptr++;
return __result; return __result;
} }
}; };
...@@ -98,13 +100,15 @@ namespace tr1 ...@@ -98,13 +100,15 @@ namespace tr1
template<> template<>
struct _Fnv_hash_base<4> struct _Fnv_hash_base<4>
{ {
template<typename _Tp>
static size_t static size_t
hash(const char* __first, size_t __length) hash(const _Tp* __ptr, size_t __clength)
{ {
size_t __result = static_cast<size_t>(2166136261UL); size_t __result = static_cast<size_t>(2166136261UL);
for (; __length > 0; --__length) const char* __cptr = reinterpret_cast<const char*>(__ptr);
for (; __clength; --__clength)
{ {
__result ^= static_cast<size_t>(*__first++); __result ^= static_cast<size_t>(*__cptr++);
__result *= static_cast<size_t>(16777619UL); __result *= static_cast<size_t>(16777619UL);
} }
return __result; return __result;
...@@ -114,14 +118,16 @@ namespace tr1 ...@@ -114,14 +118,16 @@ namespace tr1
template<> template<>
struct _Fnv_hash_base<8> struct _Fnv_hash_base<8>
{ {
template<typename _Tp>
static size_t static size_t
hash(const char* __first, size_t __length) hash(const _Tp* __ptr, size_t __clength)
{ {
size_t __result = size_t __result
static_cast<size_t>(14695981039346656037ULL); = static_cast<size_t>(14695981039346656037ULL);
for (; __length > 0; --__length) const char* __cptr = reinterpret_cast<const char*>(__ptr);
for (; __clength; --__clength)
{ {
__result ^= static_cast<size_t>(*__first++); __result ^= static_cast<size_t>(*__cptr++);
__result *= static_cast<size_t>(1099511628211ULL); __result *= static_cast<size_t>(1099511628211ULL);
} }
return __result; return __result;
...@@ -136,8 +142,7 @@ namespace tr1 ...@@ -136,8 +142,7 @@ namespace tr1
template<typename _Tp> template<typename _Tp>
static size_t static size_t
hash(const _Tp& __val) hash(const _Tp& __val)
{ return hash(reinterpret_cast<const char*>(&__val), { return hash(&__val, sizeof(__val)); }
sizeof(__val)); }
}; };
/// Explicit specializations for float. /// Explicit specializations for float.
......
...@@ -37,18 +37,12 @@ ...@@ -37,18 +37,12 @@
template<> template<>
size_t size_t
hash<wstring>::operator()(wstring __s) const hash<wstring>::operator()(wstring __s) const
{ { return _Fnv_hash::hash(__s.data(), __s.length() * sizeof(wchar_t)); }
const char* __p = reinterpret_cast<const char*>(__s.data());
return _Fnv_hash::hash(__p, __s.length() * sizeof(wchar_t));
}
template<> template<>
size_t size_t
hash<const wstring&>::operator()(const wstring& __s) const hash<const wstring&>::operator()(const wstring& __s) const
{ { return _Fnv_hash::hash(__s.data(), __s.length() * sizeof(wchar_t)); }
const char* __p = reinterpret_cast<const char*>(__s.data());
return _Fnv_hash::hash(__p, __s.length() * sizeof(wchar_t));
}
#endif #endif
#endif #endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment