Boost logo

Boost-Commit :

From: daniel_james_at_[hidden]
Date: 2007-11-15 19:31:13


Author: danieljames
Date: 2007-11-15 19:31:12 EST (Thu, 15 Nov 2007)
New Revision: 41125
URL: http://svn.boost.org/trac/boost/changeset/41125

Log:
Test, implement and document 'at'.

Text files modified:
   sandbox/unordered/boost/unordered/detail/hash_table.hpp | 1 +
   sandbox/unordered/boost/unordered/detail/hash_table_impl.hpp | 11 +++++++++++
   sandbox/unordered/boost/unordered_map.hpp | 10 ++++++++++
   sandbox/unordered/libs/unordered/doc/ref.xml | 16 ++++++++++++++++
   sandbox/unordered/libs/unordered/test/objects/minimal.hpp | 12 ++++++++++++
   sandbox/unordered/libs/unordered/test/unordered/Jamfile.v2 | 1 +
   sandbox/unordered/libs/unordered/test/unordered/compile_tests.cpp | 26 ++++++++++++++++++++++++++
   7 files changed, 77 insertions(+), 0 deletions(-)

Modified: sandbox/unordered/boost/unordered/detail/hash_table.hpp
==============================================================================
--- sandbox/unordered/boost/unordered/detail/hash_table.hpp (original)
+++ sandbox/unordered/boost/unordered/detail/hash_table.hpp 2007-11-15 19:31:12 EST (Thu, 15 Nov 2007)
@@ -17,6 +17,7 @@
 #include <cmath>
 #include <algorithm>
 #include <utility>
+#include <stdexcept>
 
 #include <boost/iterator.hpp>
 #include <boost/iterator/iterator_categories.hpp>

Modified: sandbox/unordered/boost/unordered/detail/hash_table_impl.hpp
==============================================================================
--- sandbox/unordered/boost/unordered/detail/hash_table_impl.hpp (original)
+++ sandbox/unordered/boost/unordered/detail/hash_table_impl.hpp 2007-11-15 19:31:12 EST (Thu, 15 Nov 2007)
@@ -1839,6 +1839,17 @@
                     return this->end();
             }
 
+ value_type& at(key_type const& k) const
+ {
+ bucket_ptr bucket = get_bucket(k);
+ local_iterator_base it = find_iterator(bucket, k);
+
+ if (it.not_finished())
+ return *it;
+ else
+ throw std::out_of_range("Unable to find key in unordered_map.");
+ }
+
             // equal_range
             //
             // strong exception safety, no side effects

Modified: sandbox/unordered/boost/unordered_map.hpp
==============================================================================
--- sandbox/unordered/boost/unordered_map.hpp (original)
+++ sandbox/unordered/boost/unordered_map.hpp 2007-11-15 19:31:12 EST (Thu, 15 Nov 2007)
@@ -211,6 +211,16 @@
             return base[k].second;
         }
 
+ mapped_type& at(const key_type& k)
+ {
+ return base.at(k).second;
+ }
+
+ mapped_type const& at(const key_type& k) const
+ {
+ return base.at(k).second;
+ }
+
         // lookup
 
         iterator find(const key_type& k)

Modified: sandbox/unordered/libs/unordered/doc/ref.xml
==============================================================================
--- sandbox/unordered/libs/unordered/doc/ref.xml (original)
+++ sandbox/unordered/libs/unordered/doc/ref.xml 2007-11-15 19:31:12 EST (Thu, 15 Nov 2007)
@@ -1641,6 +1641,22 @@
                 <para>Can invalidate iterators, but only if the insert causes the load factor to be greater to or equal to the maximum load factor.</para>
               </notes>
             </method>
+ <overloaded-method name="at">
+ <signature><type>T&amp;</type>
+ <parameter name="k"><paramtype>key_type const&amp;</paramtype></parameter></signature>
+ <signature cv="const"><type>T const&amp;</type>
+ <parameter name="k"><paramtype>key_type const&amp;</paramtype></parameter></signature>
+ <returns>
+ <para>A reference to <code>x.second</code> where <code>x</code> is the (unique) element whose key is equivalent to <code>k</code>.</para>
+ </returns>
+ <throws>
+ <para>An exception object of type <code>out_of_range</code> if no such element is present.</para>
+ </throws>
+ <notes>
+ <para>This is not specified in the draft standard, but that is probably an oversight. The issue has been raised in
+ <ulink url="http://groups.google.com/group/comp.std.c++/browse_thread/thread/ab7c22a868fd370b">comp.std.c++</ulink>.</para>
+ </notes>
+ </overloaded-method>
           </method-group>
           <method-group name="bucket interface">
             <method name="bucket_count" cv="const">

Modified: sandbox/unordered/libs/unordered/test/objects/minimal.hpp
==============================================================================
--- sandbox/unordered/libs/unordered/test/objects/minimal.hpp (original)
+++ sandbox/unordered/libs/unordered/test/objects/minimal.hpp 2007-11-15 19:31:12 EST (Thu, 15 Nov 2007)
@@ -18,6 +18,7 @@
 namespace minimal
 {
     class copy_constructible;
+ class default_copy_constructible;
     class assignable;
     template <class T> class hash;
     template <class T> class equal_to;
@@ -36,6 +37,17 @@
         copy_constructible() {}
     };
 
+ class default_copy_constructible
+ {
+ public:
+ static default_copy_constructible create() { return default_copy_constructible(); }
+ default_copy_constructible() {}
+ default_copy_constructible(default_copy_constructible const&) {}
+ ~default_copy_constructible() {}
+ private:
+ default_copy_constructible& operator=(default_copy_constructible const&);
+ };
+
     class assignable
     {
     public:

Modified: sandbox/unordered/libs/unordered/test/unordered/Jamfile.v2
==============================================================================
--- sandbox/unordered/libs/unordered/test/unordered/Jamfile.v2 (original)
+++ sandbox/unordered/libs/unordered/test/unordered/Jamfile.v2 2007-11-15 19:31:12 EST (Thu, 15 Nov 2007)
@@ -25,6 +25,7 @@
         [ run erase_tests.cpp ]
         [ run erase_equiv_tests.cpp ]
         [ run find_tests.cpp ]
+ [ run at_tests.cpp ]
         [ run bucket_tests.cpp ]
         [ run load_factor_tests.cpp ]
         [ run rehash_tests.cpp ]

Modified: sandbox/unordered/libs/unordered/test/unordered/compile_tests.cpp
==============================================================================
--- sandbox/unordered/libs/unordered/test/unordered/compile_tests.cpp (original)
+++ sandbox/unordered/libs/unordered/test/unordered/compile_tests.cpp 2007-11-15 19:31:12 EST (Thu, 15 Nov 2007)
@@ -58,6 +58,19 @@
     test::check_return_type<iterator>::equals(r.insert(t));
 }
 
+template <class X, class Key, class T>
+void unordered_map_functions(X&, Key const& k, T const& t)
+{
+ typedef typename X::mapped_type mapped_type;
+
+ X a;
+ test::check_return_type<mapped_type>::equals_ref(a[k]);
+ test::check_return_type<mapped_type>::equals_ref(a.at(k));
+
+ X const b = a;
+ test::check_return_type<mapped_type const>::equals_ref(b.at(k));
+}
+
 template <class X, class Key, class T, class Hash, class Pred>
 void unordered_test(X&, Key& k, T& t, Hash& hf, Pred& eq)
 {
@@ -217,6 +230,7 @@
     unordered_unique_test(map, map_value);
     unordered_map_test(map, value, value);
     unordered_test(map, value, map_value, hash, equal_to);
+ unordered_map_functions(map, value, value);
 
     std::cout<<"Test unordered_multimap.\n";
 
@@ -279,6 +293,18 @@
     unordered_map_test(map, assignable, copy_constructible);
     unordered_test(map, assignable, map_value, hash, equal_to);
 
+
+ boost::unordered_map<
+ test::minimal::assignable,
+ test::minimal::default_copy_constructible,
+ test::minimal::hash<test::minimal::assignable>,
+ test::minimal::equal_to<test::minimal::assignable>,
+ test::minimal::allocator<map_value_type> > map2;
+
+ test::minimal::default_copy_constructible default_copy_constructible;
+
+ unordered_map_functions(map2, assignable, default_copy_constructible);
+
     std::cout<<"Test unordered_multimap.\n";
 
     boost::unordered_multimap<


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk