|
Boost-Commit : |
From: nesotto_at_[hidden]
Date: 2008-06-04 11:31:59
Author: nesotto
Date: 2008-06-04 11:31:58 EDT (Wed, 04 Jun 2008)
New Revision: 46119
URL: http://svn.boost.org/trac/boost/changeset/46119
Log:
Update for unordered containers
Text files modified:
trunk/libs/ptr_container/test/Jamfile.v2 | 4
trunk/libs/ptr_container/test/associative_test_data.hpp | 54 +++++++++++---
trunk/libs/ptr_container/test/ptr_set.cpp | 8 +-
trunk/libs/ptr_container/test/ptr_unordered_map.cpp | 149 ++++++++++++++++-----------------------
trunk/libs/ptr_container/test/ptr_unordered_set.cpp | 32 +++++++-
trunk/libs/ptr_container/test/serialization.cpp | 42 ++++++++++-
trunk/libs/ptr_container/test/test_data.hpp | 34 +++++++++
7 files changed, 209 insertions(+), 114 deletions(-)
Modified: trunk/libs/ptr_container/test/Jamfile.v2
==============================================================================
--- trunk/libs/ptr_container/test/Jamfile.v2 (original)
+++ trunk/libs/ptr_container/test/Jamfile.v2 2008-06-04 11:31:58 EDT (Wed, 04 Jun 2008)
@@ -35,8 +35,8 @@
[ sc-test serialization : /boost/serialization//boost_serialization ]
[ sc-test no_exceptions ]
- #[ sc-test ptr_unordered_set ]
- #[ sc-test ptr_unordered_map ]
+ [ sc-test ptr_unordered_set ]
+ [ sc-test ptr_unordered_map ]
#[ sc-test ptr_circular_buffer ]
;
Modified: trunk/libs/ptr_container/test/associative_test_data.hpp
==============================================================================
--- trunk/libs/ptr_container/test/associative_test_data.hpp (original)
+++ trunk/libs/ptr_container/test/associative_test_data.hpp 2008-06-04 11:31:58 EDT (Wed, 04 Jun 2008)
@@ -13,10 +13,44 @@
#include <boost/ptr_container/exception.hpp>
#include <boost/range/sub_range.hpp>
-template< typename C, typename B, typename T >
+template< typename C, typename B, typename T, bool Ordered >
void ptr_set_test();
-template< typename C, typename B, typename T >
+template< class T, bool Ordered >
+struct test_algorithms
+{
+ template< class Cont >
+ void operator()( Cont& c, const Cont& c2 ) const
+ {
+ typename Cont::iterator i;
+ typename Cont::const_iterator ci;
+
+ T* t = new T;
+ i = c.lower_bound( *t );
+ ci = c2.lower_bound( *t );
+ i = c.upper_bound( *t );
+ ci = c2.upper_bound( *t );
+ delete t;
+
+ BOOST_DEDUCED_TYPENAME Cont::reverse_iterator ri = c.rbegin();
+ BOOST_DEDUCED_TYPENAME Cont::const_reverse_iterator cri = c2.rbegin();
+ BOOST_DEDUCED_TYPENAME Cont::reverse_iterator rv2 = c.rend();
+ BOOST_DEDUCED_TYPENAME Cont::const_reverse_iterator cvr2 = c2.rend();
+ cri = c.crbegin();
+ cri = c.crend();
+ }
+};
+
+template< class T >
+struct test_algorithms<T,false>
+{
+ template< class Cont>
+ void operator()( Cont& c, const Cont& c2 ) const
+ {
+ }
+};
+
+template< typename C, typename B, typename T, bool Ordered >
void ptr_set_test()
{
using namespace boost;
@@ -27,7 +61,7 @@
BOOST_CHECK( c.size() == 0 );
c.insert( c.end(), new T );
c.insert( c.end(), new T );
-
+
const C c2( c.begin(), c.end() );
BOOST_CHECK( c.size() == c2.size() );
@@ -49,12 +83,10 @@
BOOST_DEDUCED_TYPENAME C::allocator_type alloc = c.get_allocator();
BOOST_DEDUCED_TYPENAME C::iterator i = c.begin();
BOOST_DEDUCED_TYPENAME C::const_iterator ci = c2.begin();
+ ci = c.cbegin();
+ ci = c.cend();
BOOST_DEDUCED_TYPENAME C::iterator i2 = c.end();
BOOST_DEDUCED_TYPENAME C::const_iterator ci2 = c2.begin();
- BOOST_DEDUCED_TYPENAME C::reverse_iterator ri = c.rbegin();
- BOOST_DEDUCED_TYPENAME C::const_reverse_iterator cri = c2.rbegin();
- BOOST_DEDUCED_TYPENAME C::reverse_iterator rv2 = c.rend();
- BOOST_DEDUCED_TYPENAME C::const_reverse_iterator cvr2 = c2.rend();
BOOST_MESSAGE( "finished iterator test" );
@@ -143,14 +175,12 @@
i = c.find( *t );
ci = c2.find( *t );
c2.count( *t );
- i = c.lower_bound( *t );
- ci = c2.lower_bound( *t );
- i = c.upper_bound( *t );
- ci = c2.upper_bound( *t );
+
+ test_algorithms<T,Ordered>()( c, c2 );
sub = c.equal_range( *t );
csub = c2.equal_range( *t );
delete t;
-
+
BOOST_MESSAGE( "finished algorithms interface test" );
}
Modified: trunk/libs/ptr_container/test/ptr_set.cpp
==============================================================================
--- trunk/libs/ptr_container/test/ptr_set.cpp (original)
+++ trunk/libs/ptr_container/test/ptr_set.cpp 2008-06-04 11:31:58 EDT (Wed, 04 Jun 2008)
@@ -41,11 +41,11 @@
void test_set()
{
srand( 0 );
- ptr_set_test< ptr_set<Base>, Base, Derived_class >();
- ptr_set_test< ptr_set<Value>, Value, Value >();
+ ptr_set_test< ptr_set<Base>, Base, Derived_class, true >();
+ ptr_set_test< ptr_set<Value>, Value, Value, true >();
- ptr_set_test< ptr_multiset<Base>, Base, Derived_class >();
- ptr_set_test< ptr_multiset<Value>, Value, Value >();
+ ptr_set_test< ptr_multiset<Base>, Base, Derived_class, true >();
+ ptr_set_test< ptr_multiset<Value>, Value, Value, true >();
test_copy< ptr_set<Base>, ptr_set<Derived_class>,
Derived_class>();
Modified: trunk/libs/ptr_container/test/ptr_unordered_map.cpp
==============================================================================
--- trunk/libs/ptr_container/test/ptr_unordered_map.cpp (original)
+++ trunk/libs/ptr_container/test/ptr_unordered_map.cpp 2008-06-04 11:31:58 EDT (Wed, 04 Jun 2008)
@@ -1,7 +1,7 @@
//
// Boost.Pointer Container
//
-// Copyright Thorsten Ottosen 2003-2005. Use, modification and
+// Copyright Thorsten Ottosen 2008. Use, modification and
// distribution is subject to the Boost Software License, Version
// 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
@@ -57,6 +57,11 @@
return r.clone();
}
+inline std::size_t hash_value( const abstract_base& b )
+{
+ return boost::hash_value( &b );
+}
+
//
// ptr_map test
//
@@ -80,6 +85,7 @@
}
+
template< typename C, typename B, typename T >
void ptr_map_test()
{
@@ -102,10 +108,8 @@
BOOST_DEDUCED_TYPENAME C::const_iterator ci = c2.begin();
BOOST_DEDUCED_TYPENAME C::iterator i2 = c.end();
BOOST_DEDUCED_TYPENAME C::const_iterator ci2 = c2.begin();
- BOOST_DEDUCED_TYPENAME C::reverse_iterator ri = c.rbegin();
- BOOST_DEDUCED_TYPENAME C::const_reverse_iterator cri = c2.rbegin();
- BOOST_DEDUCED_TYPENAME C::reverse_iterator rv2 = c.rend();
- BOOST_DEDUCED_TYPENAME C::const_reverse_iterator cvr2 = c2.rend();
+ ci = c.cbegin();
+ ci = c.cend();
BOOST_DEDUCED_TYPENAME C::key_type a_key;
@@ -187,10 +191,6 @@
i = c.find( get_next_key( a_key ) );
ci = c2.find( get_next_key( a_key ) );
c2.count( get_next_key( a_key ) );
- i = c.lower_bound( get_next_key( a_key ) );
- ci = c2.lower_bound( get_next_key( a_key ) );
- i = c.upper_bound( get_next_key( a_key ) );
- ci = c2.upper_bound( get_next_key( a_key ) );
sub = c.equal_range( get_next_key( a_key ) );
csub = c2.equal_range( get_next_key( a_key ) );
@@ -216,25 +216,7 @@
std::cout << "\n mapped value = " << *it->second << " key = " << it->first;
//std::cout << "\n mapped value = " << it.value() << " key = " << it.key();
}
-
- typename C::reverse_iterator rit = c.rbegin(), re = c.rend();
- for( ; rit != re; ++rit )
- {
- std::cout << "\n mapped value = " << *rit->second << " key = " << rit->first;
- //std::cout << "\n mapped value = " << rit.value() << " key = " << rit.key();
- //std::cout << "\n mapped value (base) = "
- // << rit.base().value() << " key = " << rit.base().key();
- }
-
- typename C::const_reverse_iterator crit = c2.rbegin(), cre = c2.rend();
- for( ; crit != cre; ++crit )
- {
- std::cout << "\n mapped value = " << *(*crit).second << " key = " << (*crit).first;
- //std::cout << "\n mapped value = " << crit.value() << " key = " << crit.key();
- //std::cout << "\n mapped value (base) = "
- // << crit.base().value() << " key = " << crit.base().key();
- }
-
+
BOOST_MESSAGE( "finished iterator test" );
a_key = get_next_key( a_key );
@@ -285,51 +267,73 @@
-#include <boost/ptr_container/ptr_map.hpp>
+template< class Cont, class Key, class T >
+void test_unordered_interface()
+{
+ Cont c;
+ T* t = new T;
+ Key key = get_next_key( key );
+ c.insert( key, t );
+ typename Cont::local_iterator i = c.begin( 0 );
+ typename Cont::const_local_iterator ci = i;
+ ci = c.cbegin( 0 );
+ i = c.end( 0 );
+ ci = c.cend( 0 );
+ typename Cont::size_type s = c.bucket_count();
+ s = c.max_bucket_count();
+ s = c.bucket_size( 0 );
+ s = c.bucket( key );
+ float f = c.load_factor();
+ f = c.max_load_factor();
+ c.max_load_factor(f);
+ c.rehash(1000);
+}
+
+#include <boost/ptr_container/ptr_unordered_map.hpp>
using namespace std;
void test_map()
{
- ptr_map_test< ptr_map<int, Base>, Base, Derived_class >();
- ptr_map_test< ptr_map<int, Value>, Value, Value >();
- ptr_map_test< ptr_map<int, nullable<Base> >, Base, Derived_class >();
- ptr_map_test< ptr_map<int, nullable<Value> >, Value, Value >();
- ptr_map_test< ptr_map<int, abstract_base>, abstract_base, implementation >();
-
- ptr_map_test< ptr_multimap<int,Base>, Base, Derived_class >();
- ptr_map_test< ptr_multimap<int,Value>, Value, Value >();
- ptr_map_test< ptr_multimap<int, nullable<Base> >, Base, Derived_class >();
- ptr_map_test< ptr_multimap<int, nullable<Value> >, Value, Value >();
+ ptr_map_test< ptr_unordered_map<int, Base>, Base, Derived_class >();
+ ptr_map_test< ptr_unordered_map<int, Value>, Value, Value >();
+ ptr_map_test< ptr_unordered_map<int, nullable<Base> >, Base, Derived_class >();
+ ptr_map_test< ptr_unordered_map<int, nullable<Value> >, Value, Value >();
+ ptr_map_test< ptr_unordered_map<int, abstract_base>, abstract_base, implementation >();
+
+ ptr_map_test< ptr_unordered_multimap<int,Base>, Base, Derived_class >();
+ ptr_map_test< ptr_unordered_multimap<int,Value>, Value, Value >();
+ ptr_map_test< ptr_unordered_multimap<int, nullable<Base> >, Base, Derived_class >();
+ ptr_map_test< ptr_unordered_multimap<int, nullable<Value> >, Value, Value >();
- map_container_assignment_test< ptr_map<std::string,Base>,
- ptr_map<std::string,Derived_class>,
+ map_container_assignment_test< ptr_unordered_map<std::string,Base>,
+ ptr_unordered_map<std::string,Derived_class>,
Derived_class>();
- map_container_assignment_test< ptr_map<std::string, nullable<Base> >,
- ptr_map<std::string,Derived_class>,
+ map_container_assignment_test< ptr_unordered_map<std::string, nullable<Base> >,
+ ptr_unordered_map<std::string,Derived_class>,
Derived_class>();
- map_container_assignment_test< ptr_map<std::string, nullable<Base> >,
- ptr_map<std::string, nullable<Derived_class> >,
+ map_container_assignment_test< ptr_unordered_map<std::string, nullable<Base> >,
+ ptr_unordered_map<std::string, nullable<Derived_class> >,
Derived_class>();
- map_container_assignment_test< ptr_multimap<std::string,Base>,
- ptr_multimap<std::string,Derived_class>,
+ map_container_assignment_test< ptr_unordered_multimap<std::string,Base>,
+ ptr_unordered_multimap<std::string,Derived_class>,
Derived_class>();
- map_container_assignment_test< ptr_multimap<std::string, nullable<Base> >,
- ptr_multimap<std::string,Derived_class>,
+ map_container_assignment_test< ptr_unordered_multimap<std::string, nullable<Base> >,
+ ptr_unordered_multimap<std::string,Derived_class>,
Derived_class>();
- map_container_assignment_test< ptr_multimap<std::string, nullable<Base> >,
- ptr_multimap<std::string, nullable<Derived_class> >,
+ map_container_assignment_test< ptr_unordered_multimap<std::string, nullable<Base> >,
+ ptr_unordered_multimap<std::string, nullable<Derived_class> >,
Derived_class>();
- test_transfer< ptr_map<int,Derived_class>, ptr_map<int,Base>, Derived_class >();
- test_transfer< ptr_multimap<int,Derived_class>, ptr_multimap<int,Base>, Derived_class >();
+ test_transfer< ptr_unordered_map<int,Derived_class>, ptr_unordered_map<int,Base>, Derived_class >();
+ test_transfer< ptr_unordered_multimap<int,Derived_class>, ptr_unordered_multimap<int,Base>, Derived_class >();
string joe = "joe";
string brian = "brian";
string kenny = "kenny";
- ptr_map<string,int> m;
+ ptr_unordered_map<string,int> m;
m.insert( joe, new int( 4 ) );
m.insert( brian, new int( 6 ) );
BOOST_CHECK( m[ "foo" ] == 0 );
@@ -342,7 +346,7 @@
BOOST_CHECK_THROW( (m.replace(m.begin(), 0 )), bad_ptr_container_operation );
BOOST_CHECK_THROW( (m.at("not there")), bad_ptr_container_operation );
- for( ptr_map<string,int>::iterator i = m.begin();
+ for( ptr_unordered_map<string,int>::iterator i = m.begin();
i != m.end(); ++i )
{
if( is_null(i) )
@@ -353,7 +357,7 @@
ref2++;
}
- typedef ptr_map<string,Derived_class> map_type;
+ typedef ptr_unordered_map<string,Derived_class> map_type;
map_type m2;
m2.insert( joe, new Derived_class );
//
@@ -379,38 +383,10 @@
//a_cpointer->second->foo();
//const_begin(m2)->second->foo();
+ test_unordered_interface< ptr_unordered_map<string,Base>, string, Derived_class >();
+ test_unordered_interface< ptr_unordered_map<string,Base>, string, Derived_class >();
}
-#include <boost/tuple/tuple.hpp>
-#include <boost/iterator/zip_iterator.hpp>
-#include <map>
-#include <boost/ptr_container/ptr_map.hpp>
-
-void test_map_iterators()
-{
- using boost::zip_iterator;
- using boost::tuple;
- using boost::make_tuple;
- using boost::ptr_map;
- using std::map;
-
- //typedef map<int, int> theMapType;
- /*
- @remark: the following will not compile
- because of proxy (non-reference) returned by operator*()
- of the ptr_map's iterator type.
-
- typedef boost::ptr_map<int, int> theMapType;
- typedef zip_iterator
- <tuple<theMapType::iterator, theMapType::iterator> > zipIter;
- theMapType map1;
- theMapType map2;
- zipIter zip(make_tuple(map1.begin(), map2.begin()));
- */
-}
-
-
-
using boost::unit_test::test_suite;
test_suite* init_unit_test_suite( int argc, char* argv[] )
@@ -418,7 +394,6 @@
test_suite* test = BOOST_TEST_SUITE( "Pointer Container Test Suite" );
test->add( BOOST_TEST_CASE( &test_map ) );
- test->add( BOOST_TEST_CASE( &test_map_iterators ) );
return test;
}
Modified: trunk/libs/ptr_container/test/ptr_unordered_set.cpp
==============================================================================
--- trunk/libs/ptr_container/test/ptr_unordered_set.cpp (original)
+++ trunk/libs/ptr_container/test/ptr_unordered_set.cpp 2008-06-04 11:31:58 EDT (Wed, 04 Jun 2008)
@@ -38,14 +38,35 @@
base = base;
}
+template< class Cont, class T >
+void test_unordered_interface()
+{
+ Cont c;
+ T* t = new T;
+ c.insert( t );
+ typename Cont::local_iterator i = c.begin( 0 );
+ typename Cont::const_local_iterator ci = i;
+ ci = c.cbegin( 0 );
+ i = c.end( 0 );
+ ci = c.cend( 0 );
+ typename Cont::size_type s = c.bucket_count();
+ s = c.max_bucket_count();
+ s = c.bucket_size( 0 );
+ s = c.bucket( *t );
+ float f = c.load_factor();
+ f = c.max_load_factor();
+ c.max_load_factor(f);
+ c.rehash(1000);
+}
+
void test_set()
{
srand( 0 );
- ptr_set_test< ptr_unordered_set<Base>, Base, Derived_class >();
-/* ptr_set_test< ptr_unordered_set<Value>, Value, Value >();
+ ptr_set_test< ptr_unordered_set<Base>, Base, Derived_class, false >();
+ ptr_set_test< ptr_unordered_set<Value>, Value, Value, false >();
- ptr_set_test< ptr_unordered_multiset<Base>, Base, Derived_class >();
- ptr_set_test< ptr_unordered_multiset<Value>, Value, Value >();
+ ptr_set_test< ptr_unordered_multiset<Base>, Base, Derived_class, false >();
+ ptr_set_test< ptr_unordered_multiset<Value>, Value, Value, false >();
test_copy< ptr_unordered_set<Base>, ptr_unordered_set<Derived_class>,
Derived_class>();
@@ -62,8 +83,9 @@
set.insert( std::auto_ptr<int>( new int(1) ) );
BOOST_CHECK_THROW( (set.replace(set.begin(), 0 )), bad_ptr_container_operation );
BOOST_CHECK_THROW( (set.replace(set.begin(), std::auto_ptr<int>(0) )), bad_ptr_container_operation );
-*/
+ test_unordered_interface< ptr_unordered_set<Base>, Derived_class >();
+ test_unordered_interface< ptr_unordered_multiset<Base>, Derived_class >();
}
using boost::unit_test::test_suite;
Modified: trunk/libs/ptr_container/test/serialization.cpp
==============================================================================
--- trunk/libs/ptr_container/test/serialization.cpp (original)
+++ trunk/libs/ptr_container/test/serialization.cpp 2008-06-04 11:31:58 EDT (Wed, 04 Jun 2008)
@@ -19,6 +19,7 @@
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/xml_iarchive.hpp>
#include <boost/archive/xml_oarchive.hpp>
+#include <boost/functional/hash.hpp>
#include <boost/ptr_container/ptr_container.hpp>
#include <boost/ptr_container/serialize_ptr_container.hpp>
#include <boost/serialization/export.hpp>
@@ -68,6 +69,16 @@
return l.i < r.i;
}
+inline bool operator==( const Base& l, const Base& r )
+{
+ return l.i == r.i;
+}
+
+inline std::size_t hash_value( const Base& b )
+{
+ return boost::hash_value( b.i );
+}
+
struct Derived : Base
{
int i2;
@@ -127,10 +138,10 @@
BOOST_CHECK_EQUAL( vec.size(), vec2.size() );
BOOST_CHECK_EQUAL( (*vec2.begin()).i, -1 );
- BOOST_CHECK_EQUAL( (*--vec2.end()).i, 0 );
+ BOOST_CHECK_EQUAL( (*++vec2.begin()).i, 0 );
- typename Cont::iterator i = vec2.end();
- --i;
+ typename Cont::iterator i = vec2.begin();
+ ++i;
Derived* d = dynamic_cast<Derived*>( &*i );
BOOST_CHECK_EQUAL( d->i2, 1 );
@@ -218,7 +229,16 @@
test_serialization_helper< boost::ptr_multiset<Base>,
boost::archive::text_oarchive,
boost::archive::text_iarchive>();
-
+ /*test_serialization_helper< boost::ptr_unordered_set<Base>,
+ boost::archive::text_oarchive,
+ boost::archive::text_iarchive>();
+ test_serialization_helper< boost::ptr_unordered_multiset<Base>,
+ boost::archive::text_oarchive,
+ boost::archive::text_iarchive>();
+
+ @todo: fix ordernig issue here...
+ */
+
test_serialization_map_helper< boost::ptr_map<std::string,Base>,
boost::archive::text_oarchive,
boost::archive::text_iarchive>();
@@ -233,6 +253,20 @@
boost::archive::xml_oarchive,
boost::archive::xml_iarchive>();
+ test_serialization_map_helper< boost::ptr_unordered_map<std::string,Base>,
+ boost::archive::text_oarchive,
+ boost::archive::text_iarchive>();
+ test_serialization_map_helper< boost::ptr_unordered_multimap<std::string,Base>,
+ boost::archive::text_oarchive,
+ boost::archive::text_iarchive>();
+
+ test_serialization_map_helper< boost::ptr_unordered_map<std::string,Base>,
+ boost::archive::xml_oarchive,
+ boost::archive::xml_iarchive>();
+ test_serialization_map_helper< boost::ptr_unordered_multimap<std::string,Base>,
+ boost::archive::xml_oarchive,
+ boost::archive::xml_iarchive>();
+
}
Modified: trunk/libs/ptr_container/test/test_data.hpp
==============================================================================
--- trunk/libs/ptr_container/test/test_data.hpp (original)
+++ trunk/libs/ptr_container/test/test_data.hpp 2008-06-04 11:31:58 EDT (Wed, 04 Jun 2008)
@@ -14,6 +14,7 @@
#include <boost/config.hpp>
#include <boost/test/test_tools.hpp>
#include <boost/lexical_cast.hpp>
+#include <boost/functional/hash.hpp>
#include <algorithm>
#include <iostream>
#include <string>
@@ -39,6 +40,8 @@
}
Base& operator=( const Base& );
+
+public: // for test reasons only
int data1, data2, data3;
string data;
@@ -149,9 +152,23 @@
+inline std::size_t hash_value( const Base& b )
+{
+ std::size_t seed = 0;
+ boost::hash_combine( seed, b.data );
+ boost::hash_combine( seed, b.data1 );
+ boost::hash_combine( seed, b.data2 );
+ boost::hash_combine( seed, b.data3 );
+ return seed;
+}
+
+
class Derived_class : public Base
{
+public: // for test reasons only
int i_;
+
+private:
virtual void do_print( ostream& out ) const
{
@@ -180,12 +197,22 @@
}
};
+
+
+inline std::size_t hash_value( const Derived_class& b )
+{
+ std::size_t seed = hash_value( static_cast<const Base&>( b ) );
+ boost::hash_combine( seed, b.i_ );
+ return seed;
+}
+
//////////////////////////////////////////////////////////////////////////////
// Test class 2: a value class
//////////////////////////////////////////////////////////////////////////////
class Value
{
+public: // for test reasons only
string s_;
public:
@@ -236,6 +263,13 @@
return out << v.name() << " ";
}
+
+
+inline std::size_t hash_value( const Value& v )
+{
+ return boost::hash_value( v.s_ );
+}
+
//
// used to hide "unused variable" warnings
//
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