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 <blaze/math/Aliases.h>
45 #include <blaze/math/Exception.h>
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, bool TF >
249 
250  template< typename VT, bool TF >
252 
253  template< typename VT >
255 
256  template< typename Archive, typename VT >
257  void deserializeVector( Archive& archive, VT& vec );
258 
259  template< typename Archive, typename VT, bool TF >
261  deserializeDenseVector( Archive& archive, DenseVector<VT,TF>& vec );
262 
263  template< typename Archive, typename VT, bool TF >
265  deserializeDenseVector( Archive& archive, DenseVector<VT,TF>& vec );
266 
267  template< typename Archive, typename VT, bool TF >
268  void deserializeDenseVector( Archive& archive, SparseVector<VT,TF>& vec );
269 
270  template< typename Archive, typename VT, bool TF >
271  void deserializeSparseVector( Archive& archive, DenseVector<VT,TF>& vec );
272 
273  template< typename Archive, typename VT, bool TF >
274  void deserializeSparseVector( Archive& archive, SparseVector<VT,TF>& vec );
276  //**********************************************************************************************
277 
278  //**Member variables****************************************************************************
287 
288  //**********************************************************************************************
289 };
290 //*************************************************************************************************
291 
292 
293 
294 
295 //=================================================================================================
296 //
297 // CONSTRUCTOR
298 //
299 //=================================================================================================
300 
301 //*************************************************************************************************
305  : version_ ( 0U ) // The version of the archive
306  , type_ ( 0U ) // The type of the vector
307  , elementType_( 0U ) // The type of an element
308  , elementSize_( 0U ) // The size in bytes of a single element of the vector
309  , size_ ( 0UL ) // The size of the vector
310  , number_ ( 0UL ) // The total number of elements contained in the vector
311 {}
312 //*************************************************************************************************
313 
314 
315 
316 
317 //=================================================================================================
318 //
319 // SERIALIZATION FUNCTIONS
320 //
321 //=================================================================================================
322 
323 //*************************************************************************************************
334 template< typename Archive // Type of the archive
335  , typename VT // Type of the vector
336  , bool TF > // Transpose flag
338 {
339  if( !archive ) {
340  BLAZE_THROW_RUNTIME_ERROR( "Faulty archive detected" );
341  }
342 
343  serializeHeader( archive, ~vec );
344  serializeVector( archive, ~vec );
345 }
346 //*************************************************************************************************
347 
348 
349 //*************************************************************************************************
357 template< typename Archive // Type of the archive
358  , typename VT > // Type of the vector
359 void VectorSerializer::serializeHeader( Archive& archive, const VT& vec )
360 {
361  typedef ElementType_<VT> ET;
362 
363  archive << uint8_t ( 1U );
364  archive << uint8_t ( VectorValueMapping<VT>::value );
365  archive << uint8_t ( TypeValueMapping<ET>::value );
366  archive << uint8_t ( sizeof( ET ) );
367  archive << uint64_t( vec.size() );
368  archive << uint64_t( IsDenseVector<VT>::value ? vec.size() : vec.nonZeros() );
369 
370  if( !archive ) {
371  BLAZE_THROW_RUNTIME_ERROR( "File header could not be serialized" );
372  }
373 }
374 //*************************************************************************************************
375 
376 
377 //*************************************************************************************************
385 template< typename Archive // Type of the archive
386  , typename VT // Type of the vector
387  , bool TF > // Transpose flag
389 {
390  size_t i( 0UL );
391  while( ( i < (~vec).size() ) && ( archive << (~vec)[i] ) ) {
392  ++i;
393  }
394 
395  if( !archive ) {
396  BLAZE_THROW_RUNTIME_ERROR( "Dense vector could not be serialized" );
397  }
398 }
399 //*************************************************************************************************
400 
401 
402 //*************************************************************************************************
410 template< typename Archive // Type of the archive
411  , typename VT // Type of the vector
412  , bool TF > // Transpose flag
414 {
416 
417  ConstIterator element( (~vec).begin() );
418  while( ( element != (~vec).end() ) &&
419  ( archive << element->index() << element->value() ) ) {
420  ++element;
421  }
422 
423  if( !archive ) {
424  BLAZE_THROW_RUNTIME_ERROR( "Sparse vector could not be serialized" );
425  }
426 }
427 //*************************************************************************************************
428 
429 
430 
431 
432 //=================================================================================================
433 //
434 // DESERIALIZATION FUNCTIONS
435 //
436 //=================================================================================================
437 
438 //*************************************************************************************************
446 template< typename Archive // Type of the archive
447  , typename VT // Type of the vector
448  , bool TF > // Transpose flag
450 {
451  if( !archive ) {
452  BLAZE_THROW_INVALID_ARGUMENT( "Faulty archive detected" );
453  }
454 
455  deserializeHeader( archive, ~vec );
456  prepareVector( ~vec );
457  deserializeVector( archive, ~vec );
458 }
459 //*************************************************************************************************
460 
461 
462 //*************************************************************************************************
475 template< typename Archive // Type of the archive
476  , typename VT > // Type of the vector
477 void VectorSerializer::deserializeHeader( Archive& archive, const VT& vec )
478 {
479  typedef ElementType_<VT> ET;
480 
481  if( !( archive >> version_ >> type_ >> elementType_ >> elementSize_ >> size_ >> number_ ) ) {
482  BLAZE_THROW_RUNTIME_ERROR( "Corrupt archive detected" );
483  }
484  else if( version_ != 1UL ) {
485  BLAZE_THROW_RUNTIME_ERROR( "Invalid version detected" );
486  }
487  else if( ( type_ & 1U ) != 0U || ( type_ & (~3U) ) != 0U ) {
488  BLAZE_THROW_RUNTIME_ERROR( "Invalid vector type detected" );
489  }
491  BLAZE_THROW_RUNTIME_ERROR( "Invalid element type detected" );
492  }
493  else if( elementSize_ != sizeof( ET ) ) {
494  BLAZE_THROW_RUNTIME_ERROR( "Invalid element size detected" );
495  }
496  else if( !IsResizable<VT>::value && size_ != vec.size() ) {
497  BLAZE_THROW_RUNTIME_ERROR( "Invalid vector size detected" );
498  }
499  else if( number_ > size_ ) {
500  BLAZE_THROW_RUNTIME_ERROR( "Invalid number of elements detected" );
501  }
502 }
503 //*************************************************************************************************
504 
505 
506 //*************************************************************************************************
512 template< typename VT // Type of the dense vector
513  , bool TF > // Transpose flag
515 {
516  reset( ~vec );
517 }
518 //*************************************************************************************************
519 
520 
521 //*************************************************************************************************
527 template< typename VT // Type of the sparse vector
528  , bool TF > // Transpose flag
530 {
531  (~vec).reserve( number_ );
532  reset( ~vec );
533 }
534 //*************************************************************************************************
535 
536 
537 //*************************************************************************************************
543 template< typename VT > // Type of the vector
545 {
546  vec.resize ( size_, false );
547  vec.reserve( number_ );
548  reset( vec );
549 }
550 //*************************************************************************************************
551 
552 
553 //*************************************************************************************************
564 template< typename Archive // Type of the archive
565  , typename VT > // Type of the vector
567 {
568  if( type_ == 0U ) {
569  deserializeDenseVector( archive, vec );
570  }
571  else if( type_ == 2U ) {
572  deserializeSparseVector( archive, vec );
573  }
574  else {
575  BLAZE_INTERNAL_ASSERT( false, "Undefined type flag" );
576  }
577 }
578 //*************************************************************************************************
579 
580 
581 //*************************************************************************************************
593 template< typename Archive // Type of the archive
594  , typename VT // Type of the vector
595  , bool TF > // Transpose flag
597  VectorSerializer::deserializeDenseVector( Archive& archive, DenseVector<VT,TF>& vec )
598 {
599  typedef ElementType_<VT> ET;
600 
601  size_t i( 0UL );
602  ET value = ET();
603 
604  while( ( i != size_ ) && ( archive >> value ) ) {
605  (~vec)[i] = value;
606  ++i;
607  }
608 
609  if( !archive ) {
610  BLAZE_THROW_RUNTIME_ERROR( "Dense vector could not be deserialized" );
611  }
612 }
613 //*************************************************************************************************
614 
615 
616 //*************************************************************************************************
628 template< typename Archive // Type of the archive
629  , typename VT // Type of the vector
630  , bool TF > // Transpose flag
632  VectorSerializer::deserializeDenseVector( Archive& archive, DenseVector<VT,TF>& vec )
633 {
634  if( size_ == 0UL ) return;
635  archive.read( &(~vec)[0], size_ );
636 
637  if( !archive ) {
638  BLAZE_THROW_RUNTIME_ERROR( "Dense vector could not be deserialized" );
639  }
640 }
641 //*************************************************************************************************
642 
643 
644 //*************************************************************************************************
656 template< typename Archive // Type of the archive
657  , typename VT // Type of the vector
658  , bool TF > // Transpose flag
659 void VectorSerializer::deserializeDenseVector( Archive& archive, SparseVector<VT,TF>& vec )
660 {
661  typedef ElementType_<VT> ET;
662 
663  size_t i( 0UL );
664  ET value = ET();
665 
666  while( ( i != size_ ) && ( archive >> value ) ) {
667  (~vec)[i] = value;
668  ++i;
669  }
670 
671  if( !archive ) {
672  BLAZE_THROW_RUNTIME_ERROR( "Sparse vector could not be deserialized" );
673  }
674 }
675 //*************************************************************************************************
676 
677 
678 //*************************************************************************************************
690 template< typename Archive // Type of the archive
691  , typename VT // Type of the vector
692  , bool TF > // Transpose flag
694 {
695  typedef ElementType_<VT> ET;
696 
697  size_t i( 0UL );
698  size_t index( 0UL );
699  ET value = ET();
700 
701  while( ( i != number_ ) && ( archive >> index >> value ) ) {
702  (~vec)[index] = value;
703  ++i;
704  }
705 
706  if( !archive ) {
707  BLAZE_THROW_RUNTIME_ERROR( "Dense vector could not be deserialized" );
708  }
709 }
710 //*************************************************************************************************
711 
712 
713 //*************************************************************************************************
725 template< typename Archive // Type of the archive
726  , typename VT // Type of the vector
727  , bool TF > // Transpose flag
729 {
730  typedef ElementType_<VT> ET;
731 
732  size_t i( 0UL );
733  size_t index( 0UL );
734  ET value = ET();
735 
736  while( ( i != number_ ) && ( archive >> index >> value ) ) {
737  (~vec).append( index, value, false );
738  ++i;
739  }
740 
741  if( !archive ) {
742  BLAZE_THROW_RUNTIME_ERROR( "Sparse vector could not be deserialized" );
743  }
744 }
745 //*************************************************************************************************
746 
747 
748 
749 
750 //=================================================================================================
751 //
752 // VECTORVALUEMAPPINGHELPER SPECIALIZATIONS
753 //
754 //=================================================================================================
755 
756 //*************************************************************************************************
760 template<>
761 struct VectorSerializer::VectorValueMappingHelper<true>
762 {
763  enum { value = 0 };
764 };
766 //*************************************************************************************************
767 
768 
769 //*************************************************************************************************
773 template<>
774 struct VectorSerializer::VectorValueMappingHelper<false>
775 {
776  enum { value = 2 };
777 };
779 //*************************************************************************************************
780 
781 
782 
783 
784 //=================================================================================================
785 //
786 // GLOBAL FUNCTIONS
787 //
788 //=================================================================================================
789 
790 //*************************************************************************************************
854 template< typename Archive // Type of the archive
855  , typename VT // Type of the vector
856  , bool TF > // Transpose flag
857 void serialize( Archive& archive, const Vector<VT,TF>& vec )
858 {
859  VectorSerializer().serialize( archive, ~vec );
860 }
861 //*************************************************************************************************
862 
863 
864 //*************************************************************************************************
876 template< typename Archive // Type of the archive
877  , typename VT // Type of the vector
878  , bool TF > // Transpose flag
879 void deserialize( Archive& archive, Vector<VT,TF>& vec )
880 {
881  VectorSerializer().deserialize( archive, ~vec );
882 }
883 //*************************************************************************************************
884 
885 } // namespace blaze
886 
887 #endif
#define BLAZE_THROW_INVALID_ARGUMENT(MESSAGE)
Macro for the emission of a std::invalid_argument exception.This macro encapsulates the default way o...
Definition: Exception.h:235
void serialize(Archive &archive, const Vector< VT, TF > &vec)
Serializes the given vector and writes it to the archive.
Definition: VectorSerializer.h:337
Header file for auxiliary alias declarations.
uint64_t size_
The size of the vector.
Definition: VectorSerializer.h:285
Binary archive for the portable serialization of data.The Archive class implements the functionality ...
Definition: Archive.h:141
Header file for basic type definitions.
Header file for the SparseVector base class.
BLAZE_ALWAYS_INLINE size_t size(const Vector< VT, TF > &vector) noexcept
Returns the current size/dimension of the vector.
Definition: Vector.h:258
Serializer for dense and sparse vectors.The VectorSerializer implements the necessary logic to serial...
Definition: VectorSerializer.h:154
BLAZE_ALWAYS_INLINE MT::Iterator begin(Matrix< MT, SO > &matrix, size_t i)
Returns an iterator to the first element of row/column i.
Definition: Matrix.h:188
void reset(const DiagonalProxy< MT > &proxy)
Resetting the represented element to the default initial values.
Definition: DiagonalProxy.h:533
typename DisableIf< Condition, T >::Type DisableIf_
Auxiliary type for the DisableIf class template.The DisableIf_ alias declaration provides a convenien...
Definition: DisableIf.h:223
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:566
void deserializeHeader(Archive &archive, const VT &vec)
Deserializes all meta information about the given vector.
Definition: VectorSerializer.h:477
void serializeVector(Archive &archive, const DenseVector< VT, TF > &vec)
Serializes the elements of a dense vector.
Definition: VectorSerializer.h:388
uint8_t version_
The version of the archive.
Definition: VectorSerializer.h:281
#define BLAZE_THROW_RUNTIME_ERROR(MESSAGE)
Macro for the emission of a std::runtime_error exception.This macro encapsulates the default way of B...
Definition: Exception.h:379
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:359
Namespace of the Blaze C++ math library.
Definition: Blaze.h:57
const Element * ConstIterator
Iterator over constant elements.
Definition: CompressedMatrix.h:2647
void serialize(Archive &archive, const Matrix< MT, SO > &mat)
Serializes the given matrix and writes it to the archive.
Definition: MatrixSerializer.h:1268
uint8_t elementSize_
The size in bytes of a single element of the vector.
Definition: VectorSerializer.h:284
typename T::ElementType ElementType_
Alias declaration for nested ElementType type definitions.The ElementType_ alias declaration provides...
Definition: Aliases.h:163
Base class for N-dimensional dense vectors.The DenseVector class is a base class for all arbitrarily ...
Definition: DenseVector.h:70
Header file for the exception macros of the math module.
BLAZE_ALWAYS_INLINE MT::Iterator end(Matrix< MT, SO > &matrix, size_t i)
Returns an iterator just past the last element of row/column i.
Definition: Matrix.h:254
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
typename EnableIfTrue< Condition, T >::Type EnableIfTrue_
Auxiliary type for the EnableIfTrue class template.The EnableIfTrue_ alias declaration provides a con...
Definition: EnableIf.h:138
void deserialize(Archive &archive, Vector< VT, TF > &vec)
Deserializes a vector from the given archive.
Definition: VectorSerializer.h:449
#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:61
Header file for run time assertion macros.
VectorSerializer()
The default constructor of the VectorSerializer class.
Definition: VectorSerializer.h:304
Conversion from a data type to a serial representation.This class template converts the given data ty...
Definition: TypeValueMapping.h:163
uint64_t number_
The total number of elements contained in the vector.
Definition: VectorSerializer.h:286
uint8_t type_
The type of the vector.
Definition: VectorSerializer.h:282
typename EnableIf< Condition, T >::Type EnableIf_
Auxiliary alias declaration for the EnableIf class template.The EnableIf_ alias declaration provides ...
Definition: EnableIf.h:223
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:1290
void deserializeSparseVector(Archive &archive, DenseVector< VT, TF > &vec)
Deserializes a sparse vector from the archive.
Definition: VectorSerializer.h:693
Header file for the TypeValueMapping class template.
typename T::ConstIterator ConstIterator_
Alias declaration for nested ConstIterator type definitions.The ConstIterator_ alias declaration prov...
Definition: Aliases.h:103
DisableIf_< IsResizable< VT > > prepareVector(DenseVector< VT, TF > &vec)
Prepares the given non-resizable dense vector for the deserialization process.
Definition: VectorSerializer.h:514
Base class for N-dimensional vectors.The Vector class is a base class for all arbitrarily sized (N-di...
Definition: Forward.h:154
Base class for sparse vectors.The SparseVector class is a base class for all arbitrarily sized (N-dim...
Definition: Forward.h:110
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:283