stl_bvector.h (hash<vector<bool, _Alloc>>): Add.

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

	* include/bits/stl_bvector.h (hash<vector<bool, _Alloc>>): Add.
	* include/debug/vector (hash<__debug::vector<bool, _Alloc>>):
	Likewise.
	* include/profile/vector (hash<__profile::vector<bool, _Alloc>>):
	Likewise.
	* testsuite/23_containers/vector/bool/hash/1.cc: New.

	* include/std/bitset (hash<bitset<_Nb>>): Small tweaks.
	(hash<bitset<0>>): Add.
	* include/debug/bitset (hash<__debug::bitset<_Nb>>): Forward to
	hash<bitset<_Nb>>.
	* include/profile/bitset (hash<__profile::bitset<_Nb>>): Likewise.
	* testsuite/23_containers/bitset/hash/1.cc: Improve.

From-SVN: r157176
This commit is contained in:
Paolo Carlini 2010-03-02 21:11:19 +00:00 committed by Paolo Carlini
parent 54bceaf397
commit 4cd533a7c7
9 changed files with 197 additions and 27 deletions

View File

@ -1,3 +1,19 @@
2010-03-02 Paolo Carlini <paolo.carlini@oracle.com>
* include/bits/stl_bvector.h (hash<vector<bool, _Alloc>>): Add.
* include/debug/vector (hash<__debug::vector<bool, _Alloc>>):
Likewise.
* include/profile/vector (hash<__profile::vector<bool, _Alloc>>):
Likewise.
* testsuite/23_containers/vector/bool/hash/1.cc: New.
* include/std/bitset (hash<bitset<_Nb>>): Small tweaks.
(hash<bitset<0>>): Add.
* include/debug/bitset (hash<__debug::bitset<_Nb>>): Forward to
hash<bitset<_Nb>>.
* include/profile/bitset (hash<__profile::bitset<_Nb>>): Likewise.
* testsuite/23_containers/bitset/hash/1.cc: Improve.
2010-03-02 Jonathan Wakely <jwakely.gcc@gmail.com>
PR libstdc++/43230
@ -7,9 +23,9 @@
2010-03-02 Paolo Carlini <paolo.carlini@oracle.com>
* include/std/bitset (_Base_bitset<>::_M_getdata()): Add.
(hash<_GLIBCXX_STD_D::bitset<_Nb>>): Add, use the latter.
* include/debug/bitset (hash<std::__debug::bitset<_Nb>>): Add.
* include/profile/bitset (hash<std::__profile::bitset<_Nb>>): Likewise.
(hash<bitset<_Nb>>): Add, use the latter.
* include/debug/bitset (hash<__debug::bitset<_Nb>>): Add.
* include/profile/bitset (hash<__profile::bitset<_Nb>>): Likewise.
* testsuite/23_containers/bitset/hash/1.cc: New.
2010-03-02 Jonathan Wakely <jwakely.gcc@gmail.com>

View File

@ -475,6 +475,10 @@ template<typename _Alloc>
{
typedef _Bvector_base<_Alloc> _Base;
#ifdef __GXX_EXPERIMENTAL_CXX0X__
template<typename> friend class hash;
#endif
public:
typedef bool value_type;
typedef size_t size_type;
@ -1024,4 +1028,60 @@ template<typename _Alloc>
_GLIBCXX_END_NESTED_NAMESPACE
#ifdef __GXX_EXPERIMENTAL_CXX0X__
#include <bits/functional_hash.h>
_GLIBCXX_BEGIN_NAMESPACE(std)
// DR 1182.
/// std::hash specialization for vector<bool>.
template<typename _Alloc>
struct hash<_GLIBCXX_STD_D::vector<bool, _Alloc>>
: public std::unary_function<_GLIBCXX_STD_D::vector<bool, _Alloc>, size_t>
{
size_t
operator()(const _GLIBCXX_STD_D::vector<bool, _Alloc>& __b) const;
};
template<typename _Alloc>
size_t
hash<_GLIBCXX_STD_D::vector<bool, _Alloc>>::
operator()(const _GLIBCXX_STD_D::vector<bool, _Alloc>& __b) const
{
size_t __hash = 0;
using _GLIBCXX_STD_D::_S_word_bit;
using _GLIBCXX_STD_D::_Bit_type;
const size_t __words = __b.size() / _S_word_bit;
if (__words)
{
const char* __data
= reinterpret_cast<const char*>(__b._M_impl._M_start._M_p);
const size_t __size = __words * sizeof(_Bit_type);
__hash = std::_Fnv_hash::hash(__data, __size);
}
const size_t __extrabits = __b.size() % _S_word_bit;
if (__extrabits)
{
_Bit_type __hiword = *__b._M_impl._M_finish._M_p;
__hiword &= ~((~static_cast<_Bit_type>(0)) << __extrabits);
const char* __data = reinterpret_cast<const char*>(&__hiword);
const size_t __size
= (__extrabits + __CHAR_BIT__ - 1) / __CHAR_BIT__;
if (__words)
__hash = std::_Fnv_hash::hash(__data, __size, __hash);
else
__hash = std::_Fnv_hash::hash(__data, __size);
}
return __hash;
}
_GLIBCXX_END_NAMESPACE
#endif // __GXX_EXPERIMENTAL_CXX0X__
#endif

View File

@ -389,10 +389,7 @@ namespace __debug
{
size_t
operator()(const std::__debug::bitset<_Nb>& __b) const
{
const size_t __size = (_Nb + __CHAR_BIT__ - 1) / __CHAR_BIT__;
return std::_Fnv_hash::hash(__b._M_base()._M_getdata(), __size);
}
{ return std::hash<_GLIBCXX_STD_D::bitset<_Nb>>()(__b._M_base()); }
};
#endif

View File

@ -536,6 +536,21 @@ namespace __debug
{ __lhs.swap(__rhs); }
} // namespace __debug
#ifdef __GXX_EXPERIMENTAL_CXX0X__
// DR 1182.
/// std::hash specialization for vector<bool>.
template<typename _Alloc>
struct hash<__debug::vector<bool, _Alloc>>
: public std::unary_function<std::__debug::vector<bool, _Alloc>, size_t>
{
size_t
operator()(const std::__debug::vector<bool, _Alloc>& __b) const
{ return std::hash<_GLIBCXX_STD_D::vector<bool, _Alloc>>()
(__b._M_base()); }
};
#endif
} // namespace std
#endif

View File

@ -363,10 +363,7 @@ namespace __profile
{
size_t
operator()(const std::__profile::bitset<_Nb>& __b) const
{
const size_t __size = (_Nb + __CHAR_BIT__ - 1) / __CHAR_BIT__;
return std::_Fnv_hash::hash(__b._M_base()._M_getdata(), __size);
}
{ return std::hash<_GLIBCXX_STD_D::bitset<_Nb>>()(__b._M_base()); }
};
#endif

View File

@ -461,7 +461,21 @@ namespace __profile
#endif
} // namespace __profile
using _GLIBCXX_STD_D::_S_word_bit;
#ifdef __GXX_EXPERIMENTAL_CXX0X__
// DR 1182.
/// std::hash specialization for vector<bool>.
template<typename _Alloc>
struct hash<__profile::vector<bool, _Alloc>>
: public std::unary_function<std::__profile::vector<bool, _Alloc>, size_t>
{
size_t
operator()(const std::__profile::vector<bool, _Alloc>& __b) const
{ return std::hash<_GLIBCXX_STD_D::vector<bool, _Alloc>>()
(__b._M_base()); }
};
#endif
} // namespace std
#endif

View File

@ -552,12 +552,6 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D)
return *new _WordT;
}
#ifdef __GXX_EXPERIMENTAL_CXX0X__
const char*
_M_getdata() const
{ return reinterpret_cast<const char*>(&_M_getword(0)); }
#endif
_WordT
_M_hiword() const
{ return 0; }
@ -1493,8 +1487,11 @@ _GLIBCXX_END_NESTED_NAMESPACE
#undef _GLIBCXX_BITSET_BITS_PER_WORD
#ifdef __GXX_EXPERIMENTAL_CXX0X__
namespace std
{
#include <bits/functional_hash.h>
_GLIBCXX_BEGIN_NAMESPACE(std)
// DR 1182.
/// std::hash specialization for bitset.
template<size_t _Nb>
@ -1508,7 +1505,18 @@ namespace std
return std::_Fnv_hash::hash(__b._M_getdata(), __size);
}
};
}
template<>
struct hash<_GLIBCXX_STD_D::bitset<0>>
: public std::unary_function<_GLIBCXX_STD_D::bitset<0>, size_t>
{
size_t
operator()(const _GLIBCXX_STD_D::bitset<0>&) const
{ return 0; }
};
_GLIBCXX_END_NAMESPACE
#endif // __GXX_EXPERIMENTAL_CXX0X__
#ifdef _GLIBCXX_DEBUG

View File

@ -1,4 +1,3 @@
// { dg-do compile }
// { dg-options "-std=gnu++0x" }
// Copyright (C) 2010 Free Software Foundation, Inc.
@ -20,8 +19,27 @@
#include <bitset>
// bitset hash
std::hash<std::bitset<0>> h1;
std::hash<std::bitset<10>> h2;
std::hash<std::bitset<100>> h3;
std::hash<std::bitset<1000>> h4;
void test01()
{
std::bitset<0> b0;
std::hash<std::bitset<0>> h0;
h0(b0);
std::bitset<10> b1;
std::hash<std::bitset<10>> h1;
h1(b1);
std::bitset<100> b2;
std::hash<std::bitset<100>> h2;
h2(b2);
std::bitset<1000> b3;
std::hash<std::bitset<1000>> h3;
h3(b3);
}
int main()
{
test01();
return 0;
}

View File

@ -0,0 +1,45 @@
// { dg-options "-std=gnu++0x" }
// Copyright (C) 2010 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
#include <vector>
void test01()
{
std::vector<bool> b0;
std::hash<std::vector<bool>> h0;
h0(b0);
std::vector<bool> b1(10);
std::hash<std::vector<bool>> h1;
h1(b1);
std::vector<bool> b2(100);
std::hash<std::vector<bool>> h2;
h2(b2);
std::vector<bool> b3(1000);
std::hash<std::vector<bool>> h3;
h3(b3);
}
int main()
{
test01();
return 0;
}