diff options
author | Paolo Carlini <paolo.carlini@oracle.com> | 2010-03-02 21:11:19 +0000 |
---|---|---|
committer | Paolo Carlini <paolo@gcc.gnu.org> | 2010-03-02 21:11:19 +0000 |
commit | 4cd533a7c73a0813a39b41147c9fe771fd65fd28 (patch) | |
tree | 5e48d10195e6a0bccc697e1485ec1558a6910b5f | |
parent | 54bceaf397784a87995fa91a039dcb328b120bc9 (diff) | |
download | gcc-4cd533a7c73a0813a39b41147c9fe771fd65fd28.zip gcc-4cd533a7c73a0813a39b41147c9fe771fd65fd28.tar.gz gcc-4cd533a7c73a0813a39b41147c9fe771fd65fd28.tar.bz2 |
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
-rw-r--r-- | libstdc++-v3/ChangeLog | 22 | ||||
-rw-r--r-- | libstdc++-v3/include/bits/stl_bvector.h | 60 | ||||
-rw-r--r-- | libstdc++-v3/include/debug/bitset | 5 | ||||
-rw-r--r-- | libstdc++-v3/include/debug/vector | 15 | ||||
-rw-r--r-- | libstdc++-v3/include/profile/bitset | 5 | ||||
-rw-r--r-- | libstdc++-v3/include/profile/vector | 16 | ||||
-rw-r--r-- | libstdc++-v3/include/std/bitset | 26 | ||||
-rw-r--r-- | libstdc++-v3/testsuite/23_containers/bitset/hash/1.cc | 30 | ||||
-rw-r--r-- | libstdc++-v3/testsuite/23_containers/vector/bool/hash/1.cc | 45 |
9 files changed, 197 insertions, 27 deletions
diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index a87995c..b1f673c 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -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> diff --git a/libstdc++-v3/include/bits/stl_bvector.h b/libstdc++-v3/include/bits/stl_bvector.h index cce0484..c7a4acd 100644 --- a/libstdc++-v3/include/bits/stl_bvector.h +++ b/libstdc++-v3/include/bits/stl_bvector.h @@ -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 diff --git a/libstdc++-v3/include/debug/bitset b/libstdc++-v3/include/debug/bitset index d611892..034505d 100644 --- a/libstdc++-v3/include/debug/bitset +++ b/libstdc++-v3/include/debug/bitset @@ -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 diff --git a/libstdc++-v3/include/debug/vector b/libstdc++-v3/include/debug/vector index ca326e4..e3caef5 100644 --- a/libstdc++-v3/include/debug/vector +++ b/libstdc++-v3/include/debug/vector @@ -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 diff --git a/libstdc++-v3/include/profile/bitset b/libstdc++-v3/include/profile/bitset index 3a988b5..fb47566 100644 --- a/libstdc++-v3/include/profile/bitset +++ b/libstdc++-v3/include/profile/bitset @@ -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 diff --git a/libstdc++-v3/include/profile/vector b/libstdc++-v3/include/profile/vector index ba4d5e7..f7c1871 100644 --- a/libstdc++-v3/include/profile/vector +++ b/libstdc++-v3/include/profile/vector @@ -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 diff --git a/libstdc++-v3/include/std/bitset b/libstdc++-v3/include/std/bitset index 23a2e15..7372639 100644 --- a/libstdc++-v3/include/std/bitset +++ b/libstdc++-v3/include/std/bitset @@ -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 diff --git a/libstdc++-v3/testsuite/23_containers/bitset/hash/1.cc b/libstdc++-v3/testsuite/23_containers/bitset/hash/1.cc index 84dc31a..655192e 100644 --- a/libstdc++-v3/testsuite/23_containers/bitset/hash/1.cc +++ b/libstdc++-v3/testsuite/23_containers/bitset/hash/1.cc @@ -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; +} diff --git a/libstdc++-v3/testsuite/23_containers/vector/bool/hash/1.cc b/libstdc++-v3/testsuite/23_containers/vector/bool/hash/1.cc new file mode 100644 index 0000000..85e91e9 --- /dev/null +++ b/libstdc++-v3/testsuite/23_containers/vector/bool/hash/1.cc @@ -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; +} |