65#include <cereal/cereal.hpp>
66#include <cereal/types/array.hpp>
67#include <cereal/types/string.hpp>
68#include <cereal/types/utility.hpp>
93template<
class Ar ,
bool L >
98 static constexpr bool loading = L;
101 explicit CerealArchive( Ar & a ) : f_a( a ) { }
104 template<
class... X >
105 void operator()( X &... x ) {
111 void require(
bool c ,
const char * what )
const {
114 throw( PersistError( std::string(
"ADS: an archive of cereal: " ) +
115 "corrupted: " + what ) );
120 static constexpr bool kBinary =
121 L ? cereal::traits::is_input_serializable< cereal::BinaryData< char * > ,
123 : cereal::traits::is_output_serializable< cereal::BinaryData< char * > ,
129 using U = std::remove_cv_t< T >;
130 if constexpr( Persistent< U > )
132 else if constexpr( HasPersist< U > )
134 else if constexpr( IsVector< U >::value )
136 else if constexpr( IsStdArray< U >::value || std::is_array_v< U > ) {
140 else if constexpr( IsPair< U >::value ) {
149 template<
class E ,
class A >
150 void vector( std::vector< E , A > & v );
158 template<
class E ,
class A >
159 static void array( Ar & a , std::vector< E , A > & v ) {
160 cereal::size_type n = v.size();
161 a( cereal::make_size_tag( n ) );
163 if constexpr( std::is_same_v< E , bool > )
164 v.assign( std::size_t( n ) ,
false );
166 v.resize( std::size_t( n ) );
168 if constexpr( PersistBlock< E > && kBinary ) {
169 using N =
typename PersistUnitOf< E >::type;
170 a( cereal::binary_data(
reinterpret_cast< N *
>( v.data() ) ,
171 std::size_t( n ) *
sizeof( E ) ) );
173 else if constexpr( PersistBlock< E > && ! PersistNumber< E > ) {
174 using N =
typename PersistUnitOf< E >::type;
175 for(
auto & e : v ) {
176 N u[
sizeof( E ) /
sizeof( N ) ];
178 std::memcpy( u , &e ,
sizeof( E ) );
182 std::memcpy( &e , u ,
sizeof( E ) );
185 else if constexpr( std::is_same_v< E , bool > )
186 for( std::size_t i = 0 ; i < v.size() ; ++i ) {
192 CerealArchive in( a );
208 void serialize( Ar & a ) {
209 CerealArchive< Ar , Ar::is_loading::value >::array( a , v );
213template<
class Ar ,
bool L >
214template<
class E ,
class A >
215inline void CerealArchive< Ar , L >::vector( std::vector< E , A > & v )
217 CerealArray< std::vector< E , A > > w{ v };
228template<
class Ar , Persistent T >
229void save( Ar & a ,
const T & x )
231 detail::CerealArchive< Ar , false > w( a );
232 const_cast< T &
>( x ).persist( w );
237template<
class Ar , Persistent T >
240 detail::CerealArchive< Ar , true > r( a );
249 const std::uint64_t n = b.
size();
250 const std::size_t nw = ( b.
size() + 63 ) / 64;
251 if constexpr( cereal::traits::is_output_serializable<
252 cereal::BinaryData< char * > , Ar >::value ) {
253 a( cereal::make_size_tag( std::uint8_t( 1 ) ) );
255 a( cereal::make_size_tag( n ) );
256 a( cereal::binary_data( b.
words().data() , nw *
sizeof(
Word ) ) );
259 a( cereal::make_nvp(
"m_width" , std::uint8_t( 1 ) ) );
260 a( cereal::make_nvp(
"growth_factor" , 1.5f ) );
261 a( cereal::make_nvp(
"m_size" , n ) );
262 for(
const bool v : b )
263 a( std::uint64_t( v ) );
276 const auto fail = [](
const char * what ) {
277 throw(
PersistError( std::string(
"ADS: an archive of cereal: " ) +
278 "corrupted: " + what ) );
280 if constexpr( cereal::traits::is_input_serializable<
281 cereal::BinaryData< char * > , Ar >::value ) {
282 a( cereal::make_size_tag( w ) );
284 a( cereal::make_size_tag( n ) );
286 fail(
"a vector of integers of more than 1 bit, not of bits" );
287 std::vector< Word > W( std::size_t( ( n + 63 ) / 64 ) );
288 a( cereal::binary_data( W.data() , W.size() *
sizeof(
Word ) ) );
289 if( ( n % 64 ) && ( W.back() >> ( n % 64 ) ) )
290 fail(
"the bits past the size of a vector of bits" );
294 a( cereal::make_nvp(
"m_width" , w ) );
295 a( cereal::make_nvp(
"growth_factor" , g ) );
296 a( cereal::make_nvp(
"m_size" , n ) );
298 fail(
"a vector of integers of more than 1 bit, not of bits" );
301 for( std::uint64_t i = 0 ; i < n ; ++i ) {
305 fail(
"a bit neither 0 nor 1 in a vector of bits" );
The vector of bits on which the succinct structures and the integer codes of ADS are built: the bits ...
The persistence of the static structures of ADS, beyond the book: a structure built once (a succinct ...
a vector of bits packed in words of 64 bits
Definition BitVector.h:479
void push_back(bool b)
appends the bit
void reserve(std::size_t n)
room for bits
const Words & words(void) const
the words, the bits past the size being 0; they are at least , a multiple of 8, and start on a line o...
std::size_t size(void) const
the number of bits
the error of a file (or a stream, or a buffer) of a structure that cannot be read: not a file of ADS,...
Definition MappedFile.h:83
void load(Ar &a, T &x)
the structure read from the archive of cereal
Definition Cereal.h:238
void save(Ar &a, const T &x)
the structure written to the archive of cereal
Definition Cereal.h:229
std::uint64_t Word
a word of 64 bits
Definition Bitwise.h:73