Fix and close #19

All containers that used insert during loads (map, unordered_map, set, unordered_set)
now use emplace or emplace_hint and move their loaded values into position.
This commit is contained in:
Shane Grant 2013-09-12 10:23:43 -07:00
parent 112cb47c62
commit 83e06bdc7f
5 changed files with 73 additions and 4 deletions

View File

@ -65,7 +65,7 @@ namespace cereal
typename MapT::mapped_type value;
ar( make_map_item(key, value) );
hint = map.insert(hint, {key, value} );
hint = map.emplace_hint( hint, std::move( key ), std::move( value ) );
}
}
}

View File

@ -62,7 +62,7 @@ namespace cereal
typename SetT::key_type key;
ar( key );
hint = set.insert(hint, key );
hint = set.emplace_hint( hint, std::move( key ) );
}
}
}

View File

@ -63,7 +63,7 @@ namespace cereal
typename MapT::mapped_type value;
ar( make_map_item(key, value) );
map.insert( {key, value} );
map.emplace( std::move( key ), std::move( value ) );
}
}
}

View File

@ -62,7 +62,7 @@ namespace cereal
typename SetT::key_type key;
ar( key );
set.insert( key );
set.emplace( std::move( key ) );
}
}
}

View File

@ -798,6 +798,55 @@ void test_map()
}
}
template <class IArchive, class OArchive>
void test_map_memory()
{
std::random_device rd;
std::mt19937 gen(rd());
for(int i=0; i<100; ++i)
{
std::map<int, std::unique_ptr<int>> o_uniqueptrMap;
std::map<int, std::shared_ptr<int>> o_sharedptrMap;
for(int j=0; j<100; ++j)
{
o_uniqueptrMap.emplace( random_value<int>(gen), std::unique_ptr<int>( new int( random_value<int>(gen) ) ) );
o_sharedptrMap.emplace( random_value<int>(gen), std::make_shared<int>( random_value<int>(gen) ) );
}
std::ostringstream os;
{
OArchive oar(os);
oar( o_uniqueptrMap );
oar( o_sharedptrMap );
}
decltype( o_uniqueptrMap ) i_uniqueptrMap;
decltype( o_sharedptrMap ) i_sharedptrMap;
std::istringstream is(os.str());
{
IArchive iar(is);
iar( i_uniqueptrMap );
iar( i_sharedptrMap );
}
BOOST_CHECK_EQUAL(o_sharedptrMap.size(), i_sharedptrMap.size());
BOOST_CHECK_EQUAL(o_uniqueptrMap.size(), i_uniqueptrMap.size());
auto o_v_it = o_uniqueptrMap.begin();
auto i_v_it = i_uniqueptrMap.begin();
for(;o_v_it != o_uniqueptrMap.end(); ++o_v_it, ++i_v_it)
{
BOOST_CHECK_EQUAL(i_v_it->first, o_v_it->first);
BOOST_CHECK_EQUAL(*i_v_it->second, *o_v_it->second);
}
}
}
BOOST_AUTO_TEST_CASE( binary_map )
{
test_map<cereal::BinaryInputArchive, cereal::BinaryOutputArchive>();
@ -818,6 +867,26 @@ BOOST_AUTO_TEST_CASE( json_map )
test_map<cereal::JSONInputArchive, cereal::JSONOutputArchive>();
}
BOOST_AUTO_TEST_CASE( binary_map_memory )
{
test_map_memory<cereal::BinaryInputArchive, cereal::BinaryOutputArchive>();
}
BOOST_AUTO_TEST_CASE( portable_binary_map_memory )
{
test_map_memory<cereal::PortableBinaryInputArchive, cereal::PortableBinaryOutputArchive>();
}
BOOST_AUTO_TEST_CASE( xml_map_memory )
{
test_map_memory<cereal::XMLInputArchive, cereal::XMLOutputArchive>();
}
BOOST_AUTO_TEST_CASE( json_map_memory )
{
test_map_memory<cereal::JSONInputArchive, cereal::JSONOutputArchive>();
}
// ######################################################################
template <class IArchive, class OArchive>
void test_multimap()