ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
Cereal.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*----------------------------- File Cereal.h ------------------------------*/
3/*--------------------------------------------------------------------------*/
50/*--------------------------------------------------------------------------*/
51/*----------------------------- DEFINITIONS --------------------------------*/
52/*--------------------------------------------------------------------------*/
53
54#ifndef __ADS_Cereal
55 #define __ADS_Cereal
56/* self-identification: #endif constant */
57
58/*--------------------------------------------------------------------------*/
59/*------------------------------ INCLUDES ----------------------------------*/
60/*--------------------------------------------------------------------------*/
61
64
65#include <cereal/cereal.hpp>
66#include <cereal/types/array.hpp>
67#include <cereal/types/string.hpp>
68#include <cereal/types/utility.hpp>
69
70#include <cstddef>
71#include <cstdint>
72#include <cstring>
73#include <string>
74#include <type_traits>
75#include <vector>
76
77/*--------------------------------------------------------------------------*/
78/*--------------------------- NAMESPACE ------------------------------------*/
79/*--------------------------------------------------------------------------*/
80
81namespace ADS
82{
83
84namespace detail
85{
86
87/*--------------------------------------------------------------------------*/
88/*-------------------------- CLASS CerealArchive ---------------------------*/
89/*--------------------------------------------------------------------------*/
90
93template< class Ar , bool L >
94class CerealArchive
95{
96 public:
98 static constexpr bool loading = L;
99
101 explicit CerealArchive( Ar & a ) : f_a( a ) { }
102
104 template< class... X >
105 void operator()( X &... x ) {
106 ( item( x ) , ... );
107 }
108
111 void require( bool c , const char * what ) const {
112 if constexpr( L )
113 if( ! c )
114 throw( PersistError( std::string( "ADS: an archive of cereal: " ) +
115 "corrupted: " + what ) );
116 }
117
118 private:
120 static constexpr bool kBinary =
121 L ? cereal::traits::is_input_serializable< cereal::BinaryData< char * > ,
122 Ar >::value
123 : cereal::traits::is_output_serializable< cereal::BinaryData< char * > ,
124 Ar >::value;
125
127 template< class T >
128 void item( T & x ) {
129 using U = std::remove_cv_t< T >;
130 if constexpr( Persistent< U > )
131 f_a( x ); // a structure of its own, by save() and load() below
132 else if constexpr( HasPersist< U > )
133 x.persist( *this );
134 else if constexpr( IsVector< U >::value )
135 vector( x );
136 else if constexpr( IsStdArray< U >::value || std::is_array_v< U > ) {
137 for( auto & e : x )
138 item( e );
139 }
140 else if constexpr( IsPair< U >::value ) {
141 item( x.first );
142 item( x.second );
143 }
144 else
145 f_a( x ); // a number or a string
146 }
147
149 template< class E , class A >
150 void vector( std::vector< E , A > & v );
151
152 Ar & f_a;
153
154 public:
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 ) );
162 if constexpr( L ) {
163 if constexpr( std::is_same_v< E , bool > )
164 v.assign( std::size_t( n ) , false );
165 else
166 v.resize( std::size_t( n ) );
167 }
168 if constexpr( PersistBlock< E > && kBinary ) {
169 using N = typename PersistUnitOf< E >::type; // the bytes of a number
170 a( cereal::binary_data( reinterpret_cast< N * >( v.data() ) ,
171 std::size_t( n ) * sizeof( E ) ) ); // together
172 }
173 else if constexpr( PersistBlock< E > && ! PersistNumber< E > ) {
174 using N = typename PersistUnitOf< E >::type; // a block, by its numbers
175 for( auto & e : v ) {
176 N u[ sizeof( E ) / sizeof( N ) ];
177 if constexpr( ! L )
178 std::memcpy( u , &e , sizeof( E ) );
179 for( auto & k : u )
180 a( k );
181 if constexpr( L )
182 std::memcpy( &e , u , sizeof( E ) );
183 }
184 }
185 else if constexpr( std::is_same_v< E , bool > )
186 for( std::size_t i = 0 ; i < v.size() ; ++i ) {
187 bool b = v[ i ];
188 a( b );
189 v[ i ] = b;
190 }
191 else {
192 CerealArchive in( a );
193 for( auto & e : v )
194 in.item( e );
195 }
196 }
197 };
198
201template< class V >
202struct CerealArray
203{
204 V & v;
205
207 template< class Ar >
208 void serialize( Ar & a ) {
209 CerealArchive< Ar , Ar::is_loading::value >::array( a , v );
210 }
211 };
212
213template< class Ar , bool L >
214template< class E , class A >
215inline void CerealArchive< Ar , L >::vector( std::vector< E , A > & v )
216{
217 CerealArray< std::vector< E , A > > w{ v };
218 f_a( w );
219 }
220
221} // end( namespace detail )
222
223/*--------------------------------------------------------------------------*/
224/*------------------------------- FUNCTIONS --------------------------------*/
225/*--------------------------------------------------------------------------*/
226
228template< class Ar , Persistent T >
229void save( Ar & a , const T & x )
230{
231 detail::CerealArchive< Ar , false > w( a );
232 const_cast< T & >( x ).persist( w ); // which only reads it
233 }
234
237template< class Ar , Persistent T >
238void load( Ar & a , T & x )
239{
240 detail::CerealArchive< Ar , true > r( a );
241 x.persist( r );
242 }
243
246template< class Ar >
247void save( Ar & a , const BitVector & b )
248{
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 ) ) );
254 a( 1.5f );
255 a( cereal::make_size_tag( n ) );
256 a( cereal::binary_data( b.words().data() , nw * sizeof( Word ) ) );
257 }
258 else {
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 ) ); // the value_type of a bit_vector of sdsl
264 }
265 }
266
270template< class Ar >
271void load( Ar & a , BitVector & b )
272{
273 std::uint8_t w = 0;
274 float g = 0;
275 std::uint64_t n = 0;
276 const auto fail = []( const char * what ) {
277 throw( PersistError( std::string( "ADS: an archive of cereal: " ) +
278 "corrupted: " + what ) );
279 };
280 if constexpr( cereal::traits::is_input_serializable<
281 cereal::BinaryData< char * > , Ar >::value ) {
282 a( cereal::make_size_tag( w ) );
283 a( g );
284 a( cereal::make_size_tag( n ) );
285 if( w != 1 )
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" );
291 b = BitVector( W , std::size_t( n ) );
292 }
293 else {
294 a( cereal::make_nvp( "m_width" , w ) );
295 a( cereal::make_nvp( "growth_factor" , g ) );
296 a( cereal::make_nvp( "m_size" , n ) );
297 if( w != 1 )
298 fail( "a vector of integers of more than 1 bit, not of bits" );
299 b.clear();
300 b.reserve( std::size_t( n ) );
301 for( std::uint64_t i = 0 ; i < n ; ++i ) {
302 std::uint64_t v;
303 a( v );
304 if( v > 1 )
305 fail( "a bit neither 0 nor 1 in a vector of bits" );
306 b.push_back( v == 1 );
307 }
308 }
309 }
310
311/*--------------------------------------------------------------------------*/
312
313} // end( namespace ADS )
314
315/*--------------------------------------------------------------------------*/
316
317#endif /* Cereal.h included */
318
319/*--------------------------------------------------------------------------*/
320/*--------------------------- End File Cereal.h ----------------------------*/
321/*--------------------------------------------------------------------------*/
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 clear(void)
no bit
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
Definition ADT.h:96
std::uint64_t Word
a word of 64 bits
Definition Bitwise.h:73