2005-08-29 Paolo Carlini <pcarlini@suse.de>
authorpaolo <paolo@138bc75d-0d04-0410-961f-82ee72b054a4>
Mon, 29 Aug 2005 16:11:19 +0000 (16:11 +0000)
committerpaolo <paolo@138bc75d-0d04-0410-961f-82ee72b054a4>
Mon, 29 Aug 2005 16:11:19 +0000 (16:11 +0000)
PR libstdc++/23578 (DR 464 [Ready])
* include/bits/stl_map.h (class map): Add at(const key_type&)
member functions.
* include/bits/stl_vector.h (class vector): Add data() member
functions.
* include/debug/map.h (class map): Adjust consistently.
* include/debug/vector (class vector): Likewise.
* testsuite/23_containers/map/element_access/1.cc: New.
* testsuite/23_containers/vector/data_access/1.cc: Likewise.
* docs/html/ext/howto.html: Add an entry for DR 464.

git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@103609 138bc75d-0d04-0410-961f-82ee72b054a4

libstdc++-v3/ChangeLog
libstdc++-v3/docs/html/ext/howto.html
libstdc++-v3/include/bits/stl_map.h
libstdc++-v3/include/bits/stl_vector.h
libstdc++-v3/include/debug/map.h
libstdc++-v3/include/debug/vector
libstdc++-v3/testsuite/23_containers/map/element_access/1.cc [new file with mode: 0644]
libstdc++-v3/testsuite/23_containers/vector/data_access/1.cc [new file with mode: 0644]

index 76c62a1..a69c4ce 100644 (file)
@@ -1,3 +1,16 @@
+2005-08-29  Paolo Carlini  <pcarlini@suse.de>
+
+       PR libstdc++/23578 (DR 464 [Ready])
+       * include/bits/stl_map.h (class map): Add at(const key_type&)
+       member functions.
+       * include/bits/stl_vector.h (class vector): Add data() member
+       functions.
+       * include/debug/map.h (class map): Adjust consistently.
+       * include/debug/vector (class vector): Likewise.
+       * testsuite/23_containers/map/element_access/1.cc: New.
+       * testsuite/23_containers/vector/data_access/1.cc: Likewise.
+       * docs/html/ext/howto.html: Add an entry for DR 464.
+
 2005-08-26  Benjamin Kosnik  <bkoz@redhat.com>
 
        PR libstdc++/20534 (contd)
index 0b7b17b..8516196 100644 (file)
     </dt>
     <dd>Don't fail if the next pointer is null and newoff is zero.
     </dd>
+
+    <dt><a href="lwg-active.html#464">464</a>:
+        <em>Suggestion for new member functions in standard containers</em>
+    </dt>
+    <dd>Add <code>data()</code> to <code>std::vector</code> and
+        <code>at(const key_type&amp;)</code> to <code>std::map</code>.
+    </dd>
 <!--
     <dt><a href="lwg-defects.html#"></a>:
         <em></em>
index c065609..81bfc25 100644 (file)
@@ -61,6 +61,7 @@
 #ifndef _MAP_H
 #define _MAP_H 1
 
+#include <bits/functexcept.h>
 #include <bits/concept_check.h>
 
 namespace _GLIBCXX_STD
@@ -348,6 +349,33 @@ namespace _GLIBCXX_STD
        return (*__i).second;
       }
 
+      // _GLIBCXX_RESOLVE_LIB_DEFECTS
+      // DR 464. Suggestion for new member functions in standard containers.
+      /**
+       *  @brief  Access to %map data.
+       *  @param  k  The key for which data should be retrieved.
+       *  @return  A reference to the data whose key is equivalent to k, if
+       *           such a data is present in the map.
+       *  @throw  std::out_of_range  If no such data is present.
+       */
+      mapped_type&
+      at(const key_type& __k)
+      {
+       iterator __i = lower_bound(__k);
+       if (__i == end() || key_comp()(__k, (*__i).first))
+         __throw_out_of_range(__N("map::at"));
+       return (*__i).second;
+      }
+
+      const mapped_type&
+      at(const key_type& __k) const
+      {
+       const_iterator __i = lower_bound(__k);
+       if (__i == end() || key_comp()(__k, (*__i).first))
+         __throw_out_of_range(__N("map::at"));
+       return (*__i).second;
+      }
+
       // modifiers
       /**
        *  @brief Attempts to insert a std::pair into the %map.
index 1a6dddc..94acec2 100644 (file)
@@ -568,6 +568,21 @@ namespace _GLIBCXX_STD
       back() const
       { return *(end() - 1); }
 
+      // _GLIBCXX_RESOLVE_LIB_DEFECTS
+      // DR 464. Suggestion for new member functions in standard containers.
+      // data access
+      /**
+       *   Returns a pointer such that [data(), data() + size()) is a valid
+       *   range.  For a non-empty %vector, data() == &front().
+       */
+      pointer
+      data()
+      { return pointer(this->_M_impl._M_start); }
+
+      const_pointer
+      data() const
+      { return const_pointer(this->_M_impl._M_start); }
+
       // [23.2.4.3] modifiers
       /**
        *  @brief  Add data to the end of the %vector.
index caef350..e0722db 100644 (file)
@@ -142,6 +142,10 @@ namespace __gnu_debug_def
       // 23.3.1.2 element access:
       using _Base::operator[];
 
+      // _GLIBCXX_RESOLVE_LIB_DEFECTS
+      // DR 464. Suggestion for new member functions in standard containers.
+      using _Base::at;
+
       // modifiers:
       std::pair<iterator, bool>
       insert(const value_type& __x)
index 0d62bd7..f2b3618 100644 (file)
@@ -230,6 +230,10 @@ namespace __gnu_debug_def
        return _Base::back();
       }
 
+      // _GLIBCXX_RESOLVE_LIB_DEFECTS
+      // DR 464. Suggestion for new member functions in standard containers.
+      using _Base::data;
+
       // 23.2.4.3 modifiers:
       void
       push_back(const _Tp& __x)
diff --git a/libstdc++-v3/testsuite/23_containers/map/element_access/1.cc b/libstdc++-v3/testsuite/23_containers/map/element_access/1.cc
new file mode 100644 (file)
index 0000000..5bc464a
--- /dev/null
@@ -0,0 +1,79 @@
+// 2005-08-29  Paolo Carlini  <pcarlini@suse.de>
+//
+// Copyright (C) 2005 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 <map>
+#include <stdexcept>
+#include <testsuite_hooks.h>
+
+// libstdc++/23578
+void test01() 
+{ 
+  bool test __attribute__((unused)) = true;
+  typedef std::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;
+}
diff --git a/libstdc++-v3/testsuite/23_containers/vector/data_access/1.cc b/libstdc++-v3/testsuite/23_containers/vector/data_access/1.cc
new file mode 100644 (file)
index 0000000..41d5819
--- /dev/null
@@ -0,0 +1,51 @@
+// 2005-08-29  Paolo Carlini  <pcarlini@suse.de>
+//
+// Copyright (C) 2005 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 <vector>
+#include <testsuite_hooks.h>
+
+// libstdc++/23578
+void test01() 
+{ 
+  bool test __attribute__((unused)) = true;
+  typedef std::vector<int> vector_type;
+
+  {
+    const int A[] = { 0, 1, 2, 3, 4 };    
+    vector_type v(A, A + 5);
+    VERIFY( v.data() == &v.front() );
+    int* pi = v.data();
+    VERIFY( *pi == 0 );
+  }
+
+  {
+    const int A[] = { 4, 3, 2, 1, 0 };    
+    const vector_type cv(A, A + 5);
+    VERIFY( cv.data() == &cv.front() );
+    const int* pci = cv.data();
+    VERIFY( *pci == 4 );
+  }
+}
+
+int main()
+{
+  test01();
+  return 0;
+}