This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[v3] Implement DR 761 (Ready)


Hi,

tested x86_64-linux, committed to mainline.

Paolo.

////////////////////////
2008-05-22  Paolo Carlini  <paolo.carlini@oracle.com>

	* 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.
Index: doc/xml/manual/intro.xml
===================================================================
--- doc/xml/manual/intro.xml	(revision 135776)
+++ doc/xml/manual/intro.xml	(working copy)
@@ -629,6 +629,12 @@
     <listitem><para>Make the member functions table and classic_table public.
     </para></listitem></varlistentry>
 
+    <varlistentry><term><ulink url="lwg-active.html#761">761</ulink>:
+        <emphasis>unordered_map needs an at() member function</emphasis>
+    </term>
+    <listitem><para>In C++0x mode, add at() and at() const.
+    </para></listitem></varlistentry>
+
     <varlistentry><term><ulink url="lwg-active.html#778">778</ulink>:
         <emphasis>std::bitset does not have any constructor taking a string literal</emphasis>
     </term>
Index: include/tr1_impl/hashtable_policy.h
===================================================================
--- include/tr1_impl/hashtable_policy.h	(revision 135776)
+++ include/tr1_impl/hashtable_policy.h	(working copy)
@@ -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 @@
     };
 
   template<typename _Key, typename _Pair, typename _Hashtable>
-  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<typename _Key, typename _Pair, typename _Hashtable>
@@ -557,6 +567,44 @@
       return (__p->_M_v).second;
     }
 
+#ifdef _GLIBCXX_INCLUDE_AS_CXX0X
+  template<typename _Key, typename _Pair, typename _Hashtable>
+    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<typename _Key, typename _Pair, typename _Hashtable>
+    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<const _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;
+    }
+#endif
+
   // class template _Rehash_base.  Give hashtable the max_load_factor
   // functions iff the rehash policy is _Prime_rehash_policy.
   template<typename _RehashPolicy, typename _Hashtable>
Index: testsuite/23_containers/unordered_map/dr761.cc
===================================================================
--- testsuite/23_containers/unordered_map/dr761.cc	(revision 0)
+++ testsuite/23_containers/unordered_map/dr761.cc	(revision 0)
@@ -0,0 +1,80 @@
+// { dg-options "-std=gnu++0x" }
+// 2008-05-22  Paolo Carlini  <paolo.carlini@oracle.com>
+//
+// 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 <unordered_map>
+#include <stdexcept>
+#include <testsuite_hooks.h>
+
+// DR 761. unordered_map needs an at() member function.
+void test01() 
+{ 
+  bool test __attribute__((unused)) = true;
+  typedef std::unordered_map<int, double> 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;
+}

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]