All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
VectorSerializer.h
Go to the documentation of this file.
1 //=================================================================================================
33 //=================================================================================================
34 
35 #ifndef _BLAZE_MATH_SERIALIZATION_VECTORSERIALIZER_H_
36 #define _BLAZE_MATH_SERIALIZATION_VECTORSERIALIZER_H_
37 
38 
39 //*************************************************************************************************
40 // Includes
41 //*************************************************************************************************
42 
43 #include <stdexcept>
52 #include <blaze/util/Assert.h>
53 #include <blaze/util/DisableIf.h>
54 #include <blaze/util/EnableIf.h>
55 #include <blaze/util/Types.h>
57 
58 
59 namespace blaze {
60 
61 //=================================================================================================
62 //
63 // CLASS DEFINITION
64 //
65 //=================================================================================================
66 
67 //*************************************************************************************************
155 {
156  private:
157  //**Private class VectorValueMappingHelper******************************************************
171  template< bool IsDenseVector >
172  struct VectorValueMappingHelper;
174  //**********************************************************************************************
175 
176  //**Private class VectorValueMapping************************************************************
184  template< typename T >
185  struct VectorValueMapping
186  {
187  enum { value = VectorValueMappingHelper< IsDenseVector<T>::value >::value };
189  };
191  //**********************************************************************************************
192 
193  public:
194  //**Constructor*********************************************************************************
197  explicit inline VectorSerializer();
198  // No explicitly declared copy constructor.
200  //**********************************************************************************************
201 
202  //**Destructor**********************************************************************************
203  // No explicitly declared destructor.
204  //**********************************************************************************************
205 
206  //**Assignment operators************************************************************************
207  // No explicitly declared copy assignment operator.
208  //**********************************************************************************************
209 
210  //**Serialization functions*********************************************************************
213  template< typename Archive, typename VT, bool TF >
214  void serialize( Archive& archive, const Vector<VT,TF>& vec );
216  //**********************************************************************************************
217 
218  //**Deserialization functions*********************************************************************
221  template< typename Archive, typename VT, bool TF >
222  void deserialize( Archive& archive, Vector<VT,TF>& vec );
224  //**********************************************************************************************
225 
226  private:
227  //**Serialization functions*********************************************************************
230  template< typename Archive, typename VT >
231  void serializeHeader( Archive& archive, const VT& vec );
232 
233  template< typename Archive, typename VT, bool TF >
234  void serializeVector( Archive& archive, const DenseVector<VT,TF>& vec );
235 
236  template< typename Archive, typename VT, bool TF >
237  void serializeVector( Archive& archive, const SparseVector<VT,TF>& vec );
239  //**********************************************************************************************
240 
241  //**Deserialization functions*******************************************************************
244  template< typename Archive, typename VT >
245  void deserializeHeader( Archive& archive, const VT& vec );
246 
247  template< typename VT >
248  typename DisableIf< IsResizable<VT> >::Type prepareVector( VT& vec );
249 
250  template< typename VT >
251  typename EnableIf< IsResizable<VT> >::Type prepareVector( VT& vec );
252 
253  template< typename Archive, typename VT >
254  void deserializeVector( Archive& archive, VT& vec );
255 
256  template< typename Archive, typename VT, bool TF >
259 
260  template< typename Archive, typename VT, bool TF >
263 
264  template< typename Archive, typename VT, bool TF >
265  void deserializeDenseVector( Archive& archive, SparseVector<VT,TF>& vec );
266 
267  template< typename Archive, typename VT, bool TF >
268  void deserializeSparseVector( Archive& archive, DenseVector<VT,TF>& vec );
269 
270  template< typename Archive, typename VT, bool TF >
271  void deserializeSparseVector( Archive& archive, SparseVector<VT,TF>& vec );
273  //**********************************************************************************************
274 
275  //**Member variables****************************************************************************
284 
285  //**********************************************************************************************
286 };
287 //*************************************************************************************************
288 
289 
290 
291 
292 //=================================================================================================
293 //
294 // CONSTRUCTOR
295 //
296 //=================================================================================================
297 
298 //*************************************************************************************************
302  : version_ ( 0U ) // The version of the archive
303  , type_ ( 0U ) // The type of the vector
304  , elementType_( 0U ) // The type of an element
305  , elementSize_( 0U ) // The size in bytes of a single element of the vector
306  , size_ ( 0UL ) // The size of the vector
307  , number_ ( 0UL ) // The total number of elements contained in the vector
308 {}
309 //*************************************************************************************************
310 
311 
312 
313 
314 //=================================================================================================
315 //
316 // SERIALIZATION FUNCTIONS
317 //
318 //=================================================================================================
319 
320 //*************************************************************************************************
331 template< typename Archive // Type of the archive
332  , typename VT // Type of the vector
333  , bool TF > // Transpose flag
335 {
336  if( !archive ) {
337  throw std::runtime_error( "Faulty archive detected" );
338  }
339 
340  serializeHeader( archive, ~vec );
341  serializeVector( archive, ~vec );
342 }
343 //*************************************************************************************************
344 
345 
346 //*************************************************************************************************
354 template< typename Archive // Type of the archive
355  , typename VT > // Type of the vector
356 void VectorSerializer::serializeHeader( Archive& archive, const VT& vec )
357 {
358  typedef typename VT::ElementType ET;
359 
360  archive << uint8_t ( 1U );
361  archive << uint8_t ( VectorValueMapping<VT>::value );
362  archive << uint8_t ( TypeValueMapping<ET>::value );
363  archive << uint8_t ( sizeof( ET ) );
364  archive << uint64_t( vec.size() );
365  archive << uint64_t( IsDenseVector<VT>::value ? vec.size() : vec.nonZeros() );
366 
367  if( !archive ) {
368  throw std::runtime_error( "File header could not be serialized" );
369  }
370 }
371 //*************************************************************************************************
372 
373 
374 //*************************************************************************************************
382 template< typename Archive // Type of the archive
383  , typename VT // Type of the vector
384  , bool TF > // Transpose flag
386 {
387  size_t i( 0UL );
388  while( ( i < (~vec).size() ) && ( archive << (~vec)[i] ) ) {
389  ++i;
390  }
391 
392  if( !archive ) {
393  throw std::runtime_error( "Dense vector could not be serialized" );
394  }
395 }
396 //*************************************************************************************************
397 
398 
399 //*************************************************************************************************
407 template< typename Archive // Type of the archive
408  , typename VT // Type of the vector
409  , bool TF > // Transpose flag
411 {
412  typedef typename VT::ConstIterator ConstIterator;
413 
414  ConstIterator element( (~vec).begin() );
415  while( ( element != (~vec).end() ) &&
416  ( archive << element->index() << element->value() ) ) {
417  ++element;
418  }
419 
420  if( !archive ) {
421  throw std::runtime_error( "Sparse vector could not be serialized" );
422  }
423 }
424 //*************************************************************************************************
425 
426 
427 
428 
429 //=================================================================================================
430 //
431 // DESERIALIZATION FUNCTIONS
432 //
433 //=================================================================================================
434 
435 //*************************************************************************************************
443 template< typename Archive // Type of the archive
444  , typename VT // Type of the vector
445  , bool TF > // Transpose flag
447 {
449 
450  if( !archive ) {
451  throw std::invalid_argument( "Faulty archive detected" );
452  }
453 
454  deserializeHeader( archive, ~vec );
455  prepareVector( ~vec );
456  deserializeVector( archive, ~vec );
457 }
458 //*************************************************************************************************
459 
460 
461 //*************************************************************************************************
474 template< typename Archive // Type of the archive
475  , typename VT > // Type of the vector
476 void VectorSerializer::deserializeHeader( Archive& archive, const VT& vec )
477 {
478  typedef typename VT::ElementType ET;
479 
480  if( !( archive >> version_ >> type_ >> elementType_ >> elementSize_ >> size_ >> number_ ) ) {
481  throw std::runtime_error( "Corrupt archive detected" );
482  }
483  else if( version_ != 1UL ) {
484  throw std::runtime_error( "Invalid version detected" );
485  }
486  else if( ( type_ & 1U ) != 0U || ( type_ & (~3U) ) != 0U ) {
487  throw std::runtime_error( "Invalid vector type detected" );
488  }
490  throw std::runtime_error( "Invalid element type detected" );
491  }
492  else if( elementSize_ != sizeof( ET ) ) {
493  throw std::runtime_error( "Invalid element size detected" );
494  }
495  else if( !IsResizable<VT>::value && size_ != vec.size() ) {
496  throw std::runtime_error( "Invalid vector size detected" );
497  }
498  else if( number_ > size_ ) {
499  throw std::runtime_error( "Invalid number of elements detected" );
500  }
501 }
502 //*************************************************************************************************
503 
504 
505 //*************************************************************************************************
511 template< typename VT > // Type of the vector
513 {
514  reset( vec );
515 }
516 //*************************************************************************************************
517 
518 
519 //*************************************************************************************************
525 template< typename VT > // Type of the vector
527 {
528  vec.resize ( size_, false );
529  vec.reserve( number_ );
530  reset( vec );
531 }
532 //*************************************************************************************************
533 
534 
535 //*************************************************************************************************
546 template< typename Archive // Type of the archive
547  , typename VT > // Type of the vector
549 {
550  if( type_ == 0U ) {
551  deserializeDenseVector( archive, vec );
552  }
553  else if( type_ == 2U ) {
554  deserializeSparseVector( archive, vec );
555  }
556  else {
557  BLAZE_INTERNAL_ASSERT( false, "Undefined type flag" );
558  }
559 }
560 //*************************************************************************************************
561 
562 
563 //*************************************************************************************************
575 template< typename Archive // Type of the archive
576  , typename VT // Type of the vector
577  , bool TF > // Transpose flag
580 {
581  typedef typename VT::ElementType ET;
582 
583  size_t i( 0UL );
584  ET value = ET();
585 
586  while( ( i != size_ ) && ( archive >> value ) ) {
587  (~vec)[i] = value;
588  ++i;
589  }
590 
591  if( !archive ) {
592  throw std::runtime_error( "Dense vector could not be deserialized" );
593  }
594 }
595 //*************************************************************************************************
596 
597 
598 //*************************************************************************************************
610 template< typename Archive // Type of the archive
611  , typename VT // Type of the vector
612  , bool TF > // Transpose flag
615 {
616  if( size_ == 0UL ) return;
617  archive.read( &(~vec)[0], size_ );
618 
619  if( !archive ) {
620  throw std::runtime_error( "Dense vector could not be deserialized" );
621  }
622 }
623 //*************************************************************************************************
624 
625 
626 //*************************************************************************************************
638 template< typename Archive // Type of the archive
639  , typename VT // Type of the vector
640  , bool TF > // Transpose flag
642 {
643  typedef typename VT::ElementType ET;
644 
645  size_t i( 0UL );
646  ET value = ET();
647 
648  while( ( i != size_ ) && ( archive >> value ) ) {
649  (~vec)[i] = value;
650  ++i;
651  }
652 
653  if( !archive ) {
654  throw std::runtime_error( "Sparse vector could not be deserialized" );
655  }
656 }
657 //*************************************************************************************************
658 
659 
660 //*************************************************************************************************
672 template< typename Archive // Type of the archive
673  , typename VT // Type of the vector
674  , bool TF > // Transpose flag
676 {
677  typedef typename VT::ElementType ET;
678 
679  size_t i( 0UL );
680  size_t index( 0UL );
681  ET value = ET();
682 
683  while( ( i != number_ ) && ( archive >> index >> value ) ) {
684  (~vec)[index] = value;
685  ++i;
686  }
687 
688  if( !archive ) {
689  throw std::runtime_error( "Dense vector could not be deserialized" );
690  }
691 }
692 //*************************************************************************************************
693 
694 
695 //*************************************************************************************************
707 template< typename Archive // Type of the archive
708  , typename VT // Type of the vector
709  , bool TF > // Transpose flag
711 {
712  typedef typename VT::ElementType ET;
713 
714  size_t i( 0UL );
715  size_t index( 0UL );
716  ET value = ET();
717 
718  while( ( i != number_ ) && ( archive >> index >> value ) ) {
719  (~vec).append( index, value, false );
720  ++i;
721  }
722 
723  if( !archive ) {
724  throw std::runtime_error( "Sparse vector could not be deserialized" );
725  }
726 }
727 //*************************************************************************************************
728 
729 
730 
731 
732 //=================================================================================================
733 //
734 // VECTORVALUEMAPPINGHELPER SPECIALIZATIONS
735 //
736 //=================================================================================================
737 
738 //*************************************************************************************************
742 template<>
743 struct VectorSerializer::VectorValueMappingHelper<true>
744 {
745  enum { value = 0 };
746 };
748 //*************************************************************************************************
749 
750 
751 //*************************************************************************************************
755 template<>
756 struct VectorSerializer::VectorValueMappingHelper<false>
757 {
758  enum { value = 2 };
759 };
761 //*************************************************************************************************
762 
763 
764 
765 
766 //=================================================================================================
767 //
768 // GLOBAL FUNCTIONS
769 //
770 //=================================================================================================
771 
772 //*************************************************************************************************
836 template< typename Archive // Type of the archive
837  , typename VT // Type of the vector
838  , bool TF > // Transpose flag
839 void serialize( Archive& archive, const Vector<VT,TF>& vec )
840 {
841  VectorSerializer().serialize( archive, ~vec );
842 }
843 //*************************************************************************************************
844 
845 
846 //*************************************************************************************************
858 template< typename Archive // Type of the archive
859  , typename VT // Type of the vector
860  , bool TF > // Transpose flag
861 void deserialize( Archive& archive, Vector<VT,TF>& vec )
862 {
863  VectorSerializer().deserialize( archive, ~vec );
864 }
865 //*************************************************************************************************
866 
867 } // namespace blaze
868 
869 #endif
void serialize(Archive &archive, const Vector< VT, TF > &vec)
Serializes the given vector and writes it to the archive.
Definition: VectorSerializer.h:334
void reset(DynamicMatrix< Type, SO > &m)
Resetting the given dense matrix.
Definition: DynamicMatrix.h:4512
uint64_t size_
The size of the vector.
Definition: VectorSerializer.h:282
Binary archive for the portable serialization of data.The Archive class implements the functionality ...
Definition: Archive.h:141
Header file for the SparseVector base class.
Serializer for dense and sparse vectors.The VectorSerializer implements the necessary logic to serial...
Definition: VectorSerializer.h:154
DisableIfTrue< VT::vectorizable >::Type deserializeDenseVector(Archive &archive, DenseVector< VT, TF > &vec)
Deserializes a dense vector from the archive.
Definition: VectorSerializer.h:579
Constraint on the data type.
T Type
The instantiated type.
Definition: EnableIf.h:99
8-bit unsigned integer type of the Blaze library.
Header file for the DenseVector base class.
void deserializeVector(Archive &archive, VT &vec)
Deserializes a vector from the archive.
Definition: VectorSerializer.h:548
void deserializeHeader(Archive &archive, const VT &vec)
Deserializes all meta information about the given vector.
Definition: VectorSerializer.h:476
void serializeVector(Archive &archive, const DenseVector< VT, TF > &vec)
Serializes the elements of a dense vector.
Definition: VectorSerializer.h:385
uint8_t version_
The version of the archive.
Definition: VectorSerializer.h:278
Header file for the DisableIf class template.
void serializeHeader(Archive &archive, const VT &vec)
Serializes all meta information about the given vector.
Definition: VectorSerializer.h:356
const Element * ConstIterator
Iterator over constant elements.
Definition: CompressedMatrix.h:2379
void serialize(Archive &archive, const Matrix< MT, SO > &mat)
Serializes the given matrix and writes it to the archive.
Definition: MatrixSerializer.h:1248
uint8_t elementSize_
The size in bytes of a single element of the vector.
Definition: VectorSerializer.h:281
Base class for N-dimensional dense vectors.The DenseVector class is a base class for all arbitrarily ...
Definition: DenseVector.h:70
DisableIf< IsResizable< VT > >::Type prepareVector(VT &vec)
Prepares the given non-resizable vector for the deserialization process.
Definition: VectorSerializer.h:512
Type ElementType
Type of the sparse matrix elements.
Definition: CompressedMatrix.h:2373
Header file for the EnableIf class template.
64-bit unsigned integer type of the Blaze library.
Header file for the IsNumeric type trait.
Compile time check for resizable data types.This type trait tests whether the given data type is a re...
Definition: IsResizable.h:75
void deserialize(Archive &archive, Vector< VT, TF > &vec)
Deserializes a vector from the given archive.
Definition: VectorSerializer.h:446
#define BLAZE_CONSTRAINT_MUST_BE_VECTOR_TYPE(T)
Constraint on the data type.In case the given data type T is not a N-dimensional vector type...
Definition: Vector.h:79
Header file for run time assertion macros.
VectorSerializer()
The default constructor of the VectorSerializer class.
Definition: VectorSerializer.h:301
Conversion from a data type to a serial representation.This class template converts the given data ty...
Definition: TypeValueMapping.h:163
Substitution Failure Is Not An Error (SFINAE) class.The EnableIf class template is an auxiliary tool ...
Definition: EnableIf.h:184
uint64_t number_
The total number of elements contained in the vector.
Definition: VectorSerializer.h:283
uint8_t type_
The type of the vector.
Definition: VectorSerializer.h:279
Substitution Failure Is Not An Error (SFINAE) class.The DisableIf class template is an auxiliary tool...
Definition: DisableIf.h:184
Header file for the IsDenseVector type trait.
void deserialize(Archive &archive, Matrix< MT, SO > &mat)
Deserializes a matrix from the given archive.
Definition: MatrixSerializer.h:1270
void deserializeSparseVector(Archive &archive, DenseVector< VT, TF > &vec)
Deserializes a sparse vector from the archive.
Definition: VectorSerializer.h:675
Header file for the TypeValueMapping class template.
#define BLAZE_CONSTRAINT_MUST_NOT_BE_EXPRESSION_TYPE(T)
Constraint on the data type.In case the given data type T is an expression (i.e. a type derived from ...
Definition: Expression.h:118
Base class for sparse vectors.The SparseVector class is a base class for all arbitrarily sized (N-dim...
Definition: Forward.h:105
Header file for basic type definitions.
Constraint on the data type.
T Type
The instantiated type.
Definition: DisableIf.h:99
Header file for the IsResizable type trait.
Header file for the Vector CRTP base class.
#define BLAZE_INTERNAL_ASSERT(expr, msg)
Run time assertion macro for internal checks.In case of an invalid run time expression, the program execution is terminated. The BLAZE_INTERNAL_ASSERT macro can be disabled by setting the BLAZE_USER_ASSERTION flag to zero or by defining NDEBUG during the compilation.
Definition: Assert.h:101
uint8_t elementType_
The type of an element.
Definition: VectorSerializer.h:280