From 2aa5c17ce58d1e54763875dfb10580fbdd871731 Mon Sep 17 00:00:00 2001 From: Paolo Carlini Date: Fri, 23 May 2008 01:39:17 +0000 Subject: [PATCH] hashtable_policy.h (_Map_base<,, [...]): Add per DR 761. 2008-05-22 Paolo Carlini * include/tr1_impl/hashtable_policy.h (_Map_base<,, std::_Select1st<_Pair>, true,>::at): Add per DR 761. * testsuite/23_containers/unordered_map/dr761.cc: New. * doc/xml/manual/intro.xml: Add an entry for DR 761. From-SVN: r135787 --- libstdc++-v3/ChangeLog | 7 ++ libstdc++-v3/doc/xml/manual/intro.xml | 6 ++ .../include/tr1_impl/hashtable_policy.h | 52 +++++++++++- .../23_containers/unordered_map/dr761.cc | 80 +++++++++++++++++++ 4 files changed, 143 insertions(+), 2 deletions(-) create mode 100644 libstdc++-v3/testsuite/23_containers/unordered_map/dr761.cc diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index ee010df7714..dac9a8ac794 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,10 @@ +2008-05-22 Paolo Carlini + + * include/tr1_impl/hashtable_policy.h (_Map_base<,, + std::_Select1st<_Pair>, true,>::at): Add per DR 761. + * testsuite/23_containers/unordered_map/dr761.cc: New. + * doc/xml/manual/intro.xml: Add an entry for DR 761. + 2008-05-22 Paolo Carlini * testsuite/26_numerics/complex/dr781.cc: Add test variable. diff --git a/libstdc++-v3/doc/xml/manual/intro.xml b/libstdc++-v3/doc/xml/manual/intro.xml index 578d303a396..3e84b554795 100644 --- a/libstdc++-v3/doc/xml/manual/intro.xml +++ b/libstdc++-v3/doc/xml/manual/intro.xml @@ -629,6 +629,12 @@ Make the member functions table and classic_table public. + 761: + unordered_map needs an at() member function + + In C++0x mode, add at() and at() const. + + 778: std::bitset does not have any constructor taking a string literal diff --git a/libstdc++-v3/include/tr1_impl/hashtable_policy.h b/libstdc++-v3/include/tr1_impl/hashtable_policy.h index f23f8c6f51d..6677a72d8d2 100644 --- a/libstdc++-v3/include/tr1_impl/hashtable_policy.h +++ b/libstdc++-v3/include/tr1_impl/hashtable_policy.h @@ -1,6 +1,6 @@ // Internal policy header for TR1 unordered_set and unordered_map -*- C++ -*- -// Copyright (C) 2007 Free Software Foundation, Inc. +// Copyright (C) 2007, 2008 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 @@ -530,12 +530,22 @@ namespace __detail }; template - struct _Map_base<_Key, _Pair, std::_Select1st<_Pair>, true, _Hashtable> + struct _Map_base<_Key, _Pair, std::_Select1st<_Pair>, true, _Hashtable> { typedef typename _Pair::second_type mapped_type; mapped_type& operator[](const _Key& __k); + +#ifdef _GLIBCXX_INCLUDE_AS_CXX0X + // _GLIBCXX_RESOLVE_LIB_DEFECTS + // DR 761. unordered_map needs an at() member function. + mapped_type& + at(const _Key& __k); + + const mapped_type& + at(const _Key& __k) const; +#endif }; template @@ -557,6 +567,44 @@ namespace __detail return (__p->_M_v).second; } +#ifdef _GLIBCXX_INCLUDE_AS_CXX0X + template + typename _Map_base<_Key, _Pair, std::_Select1st<_Pair>, + true, _Hashtable>::mapped_type& + _Map_base<_Key, _Pair, std::_Select1st<_Pair>, true, _Hashtable>:: + at(const _Key& __k) + { + _Hashtable* __h = static_cast<_Hashtable*>(this); + typename _Hashtable::_Hash_code_type __code = __h->_M_hash_code(__k); + std::size_t __n = __h->_M_bucket_index(__k, __code, + __h->_M_bucket_count); + + typename _Hashtable::_Node* __p = + __h->_M_find_node(__h->_M_buckets[__n], __k, __code); + if (!__p) + __throw_out_of_range(__N("_Map_base::at")); + return (__p->_M_v).second; + } + + template + const typename _Map_base<_Key, _Pair, std::_Select1st<_Pair>, + true, _Hashtable>::mapped_type& + _Map_base<_Key, _Pair, std::_Select1st<_Pair>, true, _Hashtable>:: + at(const _Key& __k) const + { + const _Hashtable* __h = static_cast(this); + typename _Hashtable::_Hash_code_type __code = __h->_M_hash_code(__k); + std::size_t __n = __h->_M_bucket_index(__k, __code, + __h->_M_bucket_count); + + typename _Hashtable::_Node* __p = + __h->_M_find_node(__h->_M_buckets[__n], __k, __code); + if (!__p) + __throw_out_of_range(__N("_Map_base::at")); + return (__p->_M_v).second; + } +#endif + // class template _Rehash_base. Give hashtable the max_load_factor // functions iff the rehash policy is _Prime_rehash_policy. template diff --git a/libstdc++-v3/testsuite/23_containers/unordered_map/dr761.cc b/libstdc++-v3/testsuite/23_containers/unordered_map/dr761.cc new file mode 100644 index 00000000000..a582bc4f6ea --- /dev/null +++ b/libstdc++-v3/testsuite/23_containers/unordered_map/dr761.cc @@ -0,0 +1,80 @@ +// { dg-options "-std=gnu++0x" } +// 2008-05-22 Paolo Carlini +// +// Copyright (C) 2008 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 2, 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 COPYING. If not, write to the Free +// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, +// USA. + +#include +#include +#include + +// DR 761. unordered_map needs an at() member function. +void test01() +{ + bool test __attribute__((unused)) = true; + typedef std::unordered_map map_type; + + { + map_type m; + m[0] = 1.5; + + double& rd = m.at(0); + VERIFY( rd == 1.5 ); + try + { + m.at(1); + } + catch(std::out_of_range& obj) + { + // Expected. + } + catch(...) + { + // Failed. + throw; + } + } + + { + map_type m; + m[1] = 2.5; + const map_type cm(m); + + const double& crd = cm.at(1); + VERIFY( crd == 2.5 ); + try + { + cm.at(0); + } + catch(std::out_of_range& obj) + { + // Expected. + } + catch(...) + { + // Failed. + throw; + } + } +} + +int main() +{ + test01(); + return 0; +} -- 2.30.2