mirror of
https://github.com/USCiLab/cereal.git
synced 2025-10-18 01:45:52 +02:00
Specialized save/load for vectors with default allocator
This commit is contained in:
@@ -6,9 +6,33 @@
|
|||||||
|
|
||||||
namespace cereal
|
namespace cereal
|
||||||
{
|
{
|
||||||
//! Serialization for std::vectors of arithmetic (but not bool) types to binary
|
//! Serialization for std::vectors of arithmetic (but not bool) types to binary, default allocator
|
||||||
template <class T, class A> inline
|
template <class T, class A> inline
|
||||||
typename std::enable_if<std::is_arithmetic<T>::value && !std::is_same<T, bool>::value, void>::type
|
typename std::enable_if<std::is_arithmetic<T>::value && !std::is_same<T, bool>::value
|
||||||
|
&& std::is_same<A, std::allocator<T>>::value, void>::type
|
||||||
|
save( BinaryOutputArchive & ar, std::vector<T, A> const & vector )
|
||||||
|
{
|
||||||
|
ar & vector.size(); // number of elements
|
||||||
|
ar.save_binary( vector.data(), vector.size() * sizeof(T) ); // actual data
|
||||||
|
}
|
||||||
|
|
||||||
|
//! Serialization for std::vectors of arithmetic (but not bool) types to binary, default allocator
|
||||||
|
template <class T, class A> inline
|
||||||
|
typename std::enable_if<std::is_arithmetic<T>::value && !std::is_same<T, bool>::value
|
||||||
|
&& std::is_same<A, std::allocator<T>>::value, void>::type
|
||||||
|
load( BinaryInputArchive & ar, std::vector<T, A> & vector )
|
||||||
|
{
|
||||||
|
size_t vectorSize;
|
||||||
|
ar & vectorSize;
|
||||||
|
|
||||||
|
vector.resize( vectorSize );
|
||||||
|
ar.load_binary( vector.data(), vectorSize * sizeof(T));
|
||||||
|
}
|
||||||
|
|
||||||
|
//! Serialization for std::vectors of arithmetic (but not bool) types to binary, special allocator
|
||||||
|
template <class T, class A> inline
|
||||||
|
typename std::enable_if<std::is_arithmetic<T>::value && !std::is_same<T, bool>::value
|
||||||
|
&& !std::is_same<A, std::allocator<T>>::value, void>::type
|
||||||
save( BinaryOutputArchive & ar, std::vector<T, A> const & vector )
|
save( BinaryOutputArchive & ar, std::vector<T, A> const & vector )
|
||||||
{
|
{
|
||||||
size_t const dataSize = std::addressof(vector.back()) - std::addressof(vector.front()) + 1;
|
size_t const dataSize = std::addressof(vector.back()) - std::addressof(vector.front()) + 1;
|
||||||
@@ -18,9 +42,10 @@ namespace cereal
|
|||||||
ar.save_binary( vector.data(), dataSize * sizeof(T) ); // actual data
|
ar.save_binary( vector.data(), dataSize * sizeof(T) ); // actual data
|
||||||
}
|
}
|
||||||
|
|
||||||
//! Serialization for std::vectors of arithmetic (but not bool) types to binary
|
//! Serialization for std::vectors of arithmetic (but not bool) types to binary, special allocator
|
||||||
template <class T, class A> inline
|
template <class T, class A> inline
|
||||||
typename std::enable_if<std::is_arithmetic<T>::value && !std::is_same<T, bool>::value, void>::type
|
typename std::enable_if<std::is_arithmetic<T>::value && !std::is_same<T, bool>::value
|
||||||
|
&& !std::is_same<A, std::allocator<T>>::value, void>::type
|
||||||
load( BinaryInputArchive & ar, std::vector<T, A> & vector )
|
load( BinaryInputArchive & ar, std::vector<T, A> & vector )
|
||||||
{
|
{
|
||||||
size_t vectorSize;
|
size_t vectorSize;
|
||||||
|
|||||||
@@ -307,7 +307,7 @@ int main()
|
|||||||
test<binary>( name.str(), data );
|
test<binary>( name.str(), data );
|
||||||
};
|
};
|
||||||
|
|
||||||
//vectorCharTest(1024*1024*1024, randomize); // 1 GB
|
vectorCharTest(1024*1024*1024, randomize); // 1 GB
|
||||||
|
|
||||||
//########################################
|
//########################################
|
||||||
auto vectorPoDStructTest = [&](size_t s)
|
auto vectorPoDStructTest = [&](size_t s)
|
||||||
@@ -323,7 +323,7 @@ int main()
|
|||||||
vectorPoDStructTest(64);
|
vectorPoDStructTest(64);
|
||||||
vectorPoDStructTest(1024);
|
vectorPoDStructTest(1024);
|
||||||
vectorPoDStructTest(1024*1024);
|
vectorPoDStructTest(1024*1024);
|
||||||
//vectorPoDStructTest(1024*1024*64);
|
vectorPoDStructTest(1024*1024*64);
|
||||||
|
|
||||||
//########################################
|
//########################################
|
||||||
auto vectorPoDChildTest = [&](size_t s)
|
auto vectorPoDChildTest = [&](size_t s)
|
||||||
|
|||||||
Reference in New Issue
Block a user