All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
VectorSerializer.h
Go to the documentation of this file.
1 //=================================================================================================
20 //=================================================================================================
21 
22 #ifndef _BLAZE_MATH_SERIALIZATION_VECTORSERIALIZER_H_
23 #define _BLAZE_MATH_SERIALIZATION_VECTORSERIALIZER_H_
24 
25 
26 //*************************************************************************************************
27 // Includes
28 //*************************************************************************************************
29 
30 #include <stdexcept>
39 #include <blaze/util/Assert.h>
40 #include <blaze/util/DisableIf.h>
41 #include <blaze/util/EnableIf.h>
42 #include <blaze/util/Types.h>
44 
45 
46 namespace blaze {
47 
48 //=================================================================================================
49 //
50 // CLASS DEFINITION
51 //
52 //=================================================================================================
53 
54 //*************************************************************************************************
142 {
143  private:
144  //**Private class VectorValueMappingHelper******************************************************
158  template< bool IsDenseVector >
159  struct VectorValueMappingHelper;
161  //**********************************************************************************************
162 
163  //**Private class VectorValueMapping************************************************************
171  template< typename T >
172  struct VectorValueMapping
173  {
174  enum { value = VectorValueMappingHelper< IsDenseVector<T>::value >::value };
176  };
178  //**********************************************************************************************
179 
180  public:
181  //**Constructor*********************************************************************************
184  explicit inline VectorSerializer();
185  // No explicitly declared copy constructor.
187  //**********************************************************************************************
188 
189  //**Destructor**********************************************************************************
190  // No explicitly declared destructor.
191  //**********************************************************************************************
192 
193  //**Assignment operators************************************************************************
194  // No explicitly declared copy assignment operator.
195  //**********************************************************************************************
196 
197  //**Serialization functions*********************************************************************
200  template< typename Archive, typename VT, bool TF >
201  void serialize( Archive& archive, const Vector<VT,TF>& vec );
203  //**********************************************************************************************
204 
205  //**Deserialization functions*********************************************************************
208  template< typename Archive, typename VT, bool TF >
209  void deserialize( Archive& archive, Vector<VT,TF>& vec );
211  //**********************************************************************************************
212 
213  private:
214  //**Serialization functions*********************************************************************
217  template< typename Archive, typename VT >
218  void serializeHeader( Archive& archive, const VT& vec );
219 
220  template< typename Archive, typename VT, bool TF >
221  void serializeVector( Archive& archive, const DenseVector<VT,TF>& vec );
222 
223  template< typename Archive, typename VT, bool TF >
224  void serializeVector( Archive& archive, const SparseVector<VT,TF>& vec );
226  //**********************************************************************************************
227 
228  //**Deserialization functions*******************************************************************
231  template< typename Archive, typename VT >
232  void deserializeHeader( Archive& archive, const VT& vec );
233 
234  template< typename VT >
235  typename DisableIf< IsResizable<VT> >::Type prepareVector( VT& vec );
236 
237  template< typename VT >
238  typename EnableIf< IsResizable<VT> >::Type prepareVector( VT& vec );
239 
240  template< typename Archive, typename VT >
241  void deserializeVector( Archive& archive, VT& vec );
242 
243  template< typename Archive, typename VT, bool TF >
246 
247  template< typename Archive, typename VT, bool TF >
250 
251  template< typename Archive, typename VT, bool TF >
252  void deserializeDenseVector( Archive& archive, SparseVector<VT,TF>& vec );
253 
254  template< typename Archive, typename VT, bool TF >
255  void deserializeSparseVector( Archive& archive, DenseVector<VT,TF>& vec );
256 
257  template< typename Archive, typename VT, bool TF >
258  void deserializeSparseVector( Archive& archive, SparseVector<VT,TF>& vec );
260  //**********************************************************************************************
261 
262  //**Member variables****************************************************************************
271 
272  //**********************************************************************************************
273 };
274 //*************************************************************************************************
275 
276 
277 
278 
279 //=================================================================================================
280 //
281 // CONSTRUCTOR
282 //
283 //=================================================================================================
284 
285 //*************************************************************************************************
289  : version_ ( 0U ) // The version of the archive
290  , type_ ( 0U ) // The type of the vector
291  , elementType_( 0U ) // The type of an element
292  , elementSize_( 0U ) // The size in bytes of a single element of the vector
293  , size_ ( 0UL ) // The size of the vector
294  , number_ ( 0UL ) // The total number of elements contained in the vector
295 {}
296 //*************************************************************************************************
297 
298 
299 
300 
301 //=================================================================================================
302 //
303 // SERIALIZATION FUNCTIONS
304 //
305 //=================================================================================================
306 
307 //*************************************************************************************************
318 template< typename Archive // Type of the archive
319  , typename VT // Type of the vector
320  , bool TF > // Transpose flag
322 {
323  if( !archive ) {
324  throw std::runtime_error( "Faulty archive detected" );
325  }
326 
327  serializeHeader( archive, ~vec );
328  serializeVector( archive, ~vec );
329 }
330 //*************************************************************************************************
331 
332 
333 //*************************************************************************************************
341 template< typename Archive // Type of the archive
342  , typename VT > // Type of the vector
343 void VectorSerializer::serializeHeader( Archive& archive, const VT& vec )
344 {
345  typedef typename VT::ElementType ET;
346 
347  archive << uint8_t ( 1U );
348  archive << uint8_t ( VectorValueMapping<VT>::value );
349  archive << uint8_t ( TypeValueMapping<ET>::value );
350  archive << uint8_t ( sizeof( ET ) );
351  archive << uint64_t( vec.size() );
352  archive << uint64_t( IsDenseVector<VT>::value ? vec.size() : vec.nonZeros() );
353 
354  if( !archive ) {
355  throw std::runtime_error( "File header could not be serialized" );
356  }
357 }
358 //*************************************************************************************************
359 
360 
361 //*************************************************************************************************
369 template< typename Archive // Type of the archive
370  , typename VT // Type of the vector
371  , bool TF > // Transpose flag
373 {
374  size_t i( 0UL );
375  while( ( i < (~vec).size() ) && ( archive << (~vec)[i] ) ) {
376  ++i;
377  }
378 
379  if( !archive ) {
380  throw std::runtime_error( "Dense vector could not be serialized" );
381  }
382 }
383 //*************************************************************************************************
384 
385 
386 //*************************************************************************************************
394 template< typename Archive // Type of the archive
395  , typename VT // Type of the vector
396  , bool TF > // Transpose flag
398 {
399  typedef typename VT::ConstIterator ConstIterator;
400 
401  ConstIterator element( (~vec).begin() );
402  while( ( element != (~vec).end() ) &&
403  ( archive << element->index() << element->value() ) ) {
404  ++element;
405  }
406 
407  if( !archive ) {
408  throw std::runtime_error( "Sparse vector could not be serialized" );
409  }
410 }
411 //*************************************************************************************************
412 
413 
414 
415 
416 //=================================================================================================
417 //
418 // DESERIALIZATION FUNCTIONS
419 //
420 //=================================================================================================
421 
422 //*************************************************************************************************
430 template< typename Archive // Type of the archive
431  , typename VT // Type of the vector
432  , bool TF > // Transpose flag
434 {
436 
437  if( !archive ) {
438  throw std::invalid_argument( "Faulty archive detected" );
439  }
440 
441  deserializeHeader( archive, ~vec );
442  prepareVector( ~vec );
443  deserializeVector( archive, ~vec );
444 }
445 //*************************************************************************************************
446 
447 
448 //*************************************************************************************************
461 template< typename Archive // Type of the archive
462  , typename VT > // Type of the vector
463 void VectorSerializer::deserializeHeader( Archive& archive, const VT& vec )
464 {
465  typedef typename VT::ElementType ET;
466 
467  if( !( archive >> version_ >> type_ >> elementType_ >> elementSize_ >> size_ >> number_ ) ) {
468  throw std::runtime_error( "Corrupt archive detected" );
469  }
470  else if( version_ != 1UL ) {
471  throw std::runtime_error( "Invalid version detected" );
472  }
473  else if( ( type_ & 1U ) != 0U || type_ & (~3U) != 0U ) {
474  throw std::runtime_error( "Invalid vector type detected" );
475  }
477  throw std::runtime_error( "Invalid element type detected" );
478  }
479  else if( elementSize_ != sizeof( ET ) ) {
480  throw std::runtime_error( "Invalid element size detected" );
481  }
482  else if( !IsResizable<VT>::value && size_ != vec.size() ) {
483  throw std::runtime_error( "Invalid vector size detected" );
484  }
485  else if( number_ > size_ ) {
486  throw std::runtime_error( "Invalid number of elements detected" );
487  }
488 }
489 //*************************************************************************************************
490 
491 
492 //*************************************************************************************************
498 template< typename VT > // Type of the vector
500 {
501  reset( vec );
502 }
503 //*************************************************************************************************
504 
505 
506 //*************************************************************************************************
512 template< typename VT > // Type of the vector
514 {
515  vec.resize ( size_, false );
516  vec.reserve( number_ );
517  reset( vec );
518 }
519 //*************************************************************************************************
520 
521 
522 //*************************************************************************************************
533 template< typename Archive // Type of the archive
534  , typename VT > // Type of the vector
536 {
537  if( type_ == 0U ) {
538  deserializeDenseVector( archive, vec );
539  }
540  else if( type_ == 2U ) {
541  deserializeSparseVector( archive, vec );
542  }
543  else {
544  BLAZE_INTERNAL_ASSERT( false, "Undefined type flag" );
545  }
546 }
547 //*************************************************************************************************
548 
549 
550 //*************************************************************************************************
562 template< typename Archive // Type of the archive
563  , typename VT // Type of the vector
564  , bool TF > // Transpose flag
567 {
568  typedef typename VT::ElementType ET;
569 
570  size_t i( 0UL );
571  ET value = ET();
572 
573  while( ( i != size_ ) && ( archive >> value ) ) {
574  (~vec)[i] = value;
575  ++i;
576  }
577 
578  if( !archive ) {
579  throw std::runtime_error( "Dense vector could not be deserialized" );
580  }
581 }
582 //*************************************************************************************************
583 
584 
585 //*************************************************************************************************
597 template< typename Archive // Type of the archive
598  , typename VT // Type of the vector
599  , bool TF > // Transpose flag
602 {
603  if( size_ == 0UL ) return;
604  archive.read( &(~vec)[0], size_ );
605 
606  if( !archive ) {
607  throw std::runtime_error( "Dense vector could not be deserialized" );
608  }
609 }
610 //*************************************************************************************************
611 
612 
613 //*************************************************************************************************
625 template< typename Archive // Type of the archive
626  , typename VT // Type of the vector
627  , bool TF > // Transpose flag
629 {
630  typedef typename VT::ElementType ET;
631 
632  size_t i( 0UL );
633  ET value = ET();
634 
635  while( ( i != size_ ) && ( archive >> value ) ) {
636  (~vec)[i] = value;
637  ++i;
638  }
639 
640  if( !archive ) {
641  throw std::runtime_error( "Sparse vector could not be deserialized" );
642  }
643 }
644 //*************************************************************************************************
645 
646 
647 //*************************************************************************************************
659 template< typename Archive // Type of the archive
660  , typename VT // Type of the vector
661  , bool TF > // Transpose flag
663 {
664  typedef typename VT::ElementType ET;
665 
666  size_t i( 0UL );
667  size_t index( 0UL );
668  ET value = ET();
669 
670  while( ( i != number_ ) && ( archive >> index >> value ) ) {
671  (~vec)[index] = value;
672  ++i;
673  }
674 
675  if( !archive ) {
676  throw std::runtime_error( "Dense vector could not be deserialized" );
677  }
678 }
679 //*************************************************************************************************
680 
681 
682 //*************************************************************************************************
694 template< typename Archive // Type of the archive
695  , typename VT // Type of the vector
696  , bool TF > // Transpose flag
698 {
699  typedef typename VT::ElementType ET;
700 
701  size_t i( 0UL );
702  size_t index( 0UL );
703  ET value = ET();
704 
705  while( ( i != number_ ) && ( archive >> index >> value ) ) {
706  (~vec).append( index, value, false );
707  ++i;
708  }
709 
710  if( !archive ) {
711  throw std::runtime_error( "Sparse vector could not be deserialized" );
712  }
713 }
714 //*************************************************************************************************
715 
716 
717 
718 
719 //=================================================================================================
720 //
721 // VECTORVALUEMAPPINGHELPER SPECIALIZATIONS
722 //
723 //=================================================================================================
724 
725 //*************************************************************************************************
729 template<>
730 struct VectorSerializer::VectorValueMappingHelper<true>
731 {
732  enum { value = 0 };
733 };
735 //*************************************************************************************************
736 
737 
738 //*************************************************************************************************
742 template<>
743 struct VectorSerializer::VectorValueMappingHelper<false>
744 {
745  enum { value = 2 };
746 };
748 //*************************************************************************************************
749 
750 
751 
752 
753 //=================================================================================================
754 //
755 // GLOBAL FUNCTIONS
756 //
757 //=================================================================================================
758 
759 //*************************************************************************************************
823 template< typename Archive // Type of the archive
824  , typename VT // Type of the vector
825  , bool TF > // Transpose flag
826 void serialize( Archive& archive, const Vector<VT,TF>& vec )
827 {
828  VectorSerializer().serialize( archive, ~vec );
829 }
830 //*************************************************************************************************
831 
832 
833 //*************************************************************************************************
845 template< typename Archive // Type of the archive
846  , typename VT // Type of the vector
847  , bool TF > // Transpose flag
848 void deserialize( Archive& archive, Vector<VT,TF>& vec )
849 {
850  VectorSerializer().deserialize( archive, ~vec );
851 }
852 //*************************************************************************************************
853 
854 } // namespace blaze
855 
856 #endif