InitializerMatrix.h
Go to the documentation of this file.
1 //=================================================================================================
33 //=================================================================================================
34 
35 #ifndef _BLAZE_MATH_DENSE_INITIALIZERMATRIX_H_
36 #define _BLAZE_MATH_DENSE_INITIALIZERMATRIX_H_
37 
38 
39 //*************************************************************************************************
40 // Includes
41 //*************************************************************************************************
42 
43 #include <algorithm>
44 #include <iterator>
46 #include <blaze/math/Exception.h>
48 #include <blaze/math/Forward.h>
54 #include <blaze/util/Assert.h>
59 #include <blaze/util/TrueType.h>
60 #include <blaze/util/Types.h>
61 #include <blaze/util/Unused.h>
62 
63 
64 namespace blaze {
65 
66 //=================================================================================================
67 //
68 // CLASS DEFINITION
69 //
70 //=================================================================================================
71 
72 //*************************************************************************************************
175 template< typename Type > // Data type of the matrix
177  : public DenseMatrix< InitializerMatrix<Type>, false >
178 {
179  public:
180  //**Type definitions****************************************************************************
186  using ElementType = Type;
187  using ReturnType = const Type&;
188  using CompositeType = const This&;
189 
190  using Reference = const Type&;
191  using ConstReference = const Type&;
192  using Pointer = const Type*;
193  using ConstPointer = const Type*;
194 
197  //**********************************************************************************************
198 
199  //**Rebind struct definition********************************************************************
202  template< typename NewType > // Data type of the other matrix
203  struct Rebind {
205  };
206  //**********************************************************************************************
207 
208  //**Resize struct definition********************************************************************
211  template< size_t NewM // Number of rows of the other matrix
212  , size_t NewN > // Number of columns of the other matrix
213  struct Resize {
215  };
216  //**********************************************************************************************
217 
218  //**Compilation flags***************************************************************************
220 
224  static constexpr bool simdEnabled = false;
225 
227 
230  static constexpr bool smpAssignable = false;
231  //**********************************************************************************************
232 
233  //**Constructors********************************************************************************
236  explicit inline InitializerMatrix( initializer_list< initializer_list<Type> > list ) noexcept;
237  explicit inline InitializerMatrix( initializer_list< initializer_list<Type> > list, size_t n );
238 
239  InitializerMatrix( const InitializerMatrix& ) = default;
241  //**********************************************************************************************
242 
243  //**Destructor**********************************************************************************
246  ~InitializerMatrix() = default;
248  //**********************************************************************************************
249 
250  //**Data access functions***********************************************************************
253  inline ConstReference operator()( size_t i, size_t j ) const noexcept;
254  inline ConstReference at( size_t i, size_t j ) const;
255  inline ConstPointer data () const noexcept;
256  inline ConstPointer data ( size_t i ) const noexcept;
257  inline ConstIterator begin ( size_t i ) const noexcept;
258  inline ConstIterator cbegin( size_t i ) const noexcept;
259  inline ConstIterator end ( size_t i ) const noexcept;
260  inline ConstIterator cend ( size_t i ) const noexcept;
262  //**********************************************************************************************
263 
264  //**Assignment operators************************************************************************
267  InitializerMatrix& operator=( const InitializerMatrix& ) = delete;
269  //**********************************************************************************************
270 
271  //**Utility functions***************************************************************************
274  inline size_t rows() const noexcept;
275  inline size_t columns() const noexcept;
276  inline size_t spacing() const noexcept;
277  inline size_t capacity() const noexcept;
278  inline size_t capacity( size_t i ) const noexcept;
279  inline size_t nonZeros() const;
280  inline size_t nonZeros( size_t i ) const;
281  inline void swap( InitializerMatrix& m ) noexcept;
283  //**********************************************************************************************
284 
285  //**Expression template evaluation functions****************************************************
288  template< typename Other > inline bool canAlias ( const Other* alias ) const noexcept;
289  template< typename Other > inline bool isAliased( const Other* alias ) const noexcept;
291  //**********************************************************************************************
292 
293  private:
294  //**Type definitions****************************************************************************
296  //**********************************************************************************************
297 
298  //**Member variables****************************************************************************
301  size_t m_;
302  size_t n_;
304 
313  static const Type zero_;
314 
315  //**********************************************************************************************
316 
317  //**Compile time checks*************************************************************************
324  //**********************************************************************************************
325 };
326 //*************************************************************************************************
327 
328 
329 
330 
331 //=================================================================================================
332 //
333 // DEFINITION AND INITIALIZATION OF THE STATIC MEMBER VARIABLES
334 //
335 //=================================================================================================
336 
337 template< typename Type > // Data type of the matrix
338 const Type InitializerMatrix<Type>::zero_{};
339 
340 
341 
342 
343 //=================================================================================================
344 //
345 // CONSTRUCTORS
346 //
347 //=================================================================================================
348 
349 //*************************************************************************************************
354 template< typename Type > // Data type of the matrix
356  : m_ ( list.size() ) // The current number of rows of the matrix
357  , n_ ( determineColumns( list ) ) // The current number of columns of the matrix
358  , list_( list ) // The initializer list represented by the matrix
359 {}
360 //*************************************************************************************************
361 
362 
363 //*************************************************************************************************
370 template< typename Type > // Data type of the matrix
372  : m_ ( list.size() ) // The current number of rows of the matrix
373  , n_ ( n ) // The current number of columns of the matrix
374  , list_( list ) // The initializer list represented by the matrix
375 {
376  if( n < determineColumns( list ) ) {
377  BLAZE_THROW_INVALID_ARGUMENT( "Invalid initializer list dimension" );
378  }
379 }
380 //*************************************************************************************************
381 
382 
383 
384 
385 //=================================================================================================
386 //
387 // DATA ACCESS FUNCTIONS
388 //
389 //=================================================================================================
390 
391 //*************************************************************************************************
401 template< typename Type > // Data type of the matrix
403  InitializerMatrix<Type>::operator()( size_t i, size_t j ) const noexcept
404 {
405  BLAZE_USER_ASSERT( i<m_, "Invalid row access index" );
406  BLAZE_USER_ASSERT( j<n_, "Invalid column access index" );
407 
408  const initializer_list<Type>& list( list_.begin()[i] );
409 
410  if( j < list.size() )
411  return list.begin()[j];
412  else
413  return zero_;
414 }
415 //*************************************************************************************************
416 
417 
418 //*************************************************************************************************
429 template< typename Type > // Data type of the matrix
431  InitializerMatrix<Type>::at( size_t i, size_t j ) const
432 {
433  if( i >= m_ ) {
434  BLAZE_THROW_OUT_OF_RANGE( "Invalid row access index" );
435  }
436  if( j >= n_ ) {
437  BLAZE_THROW_OUT_OF_RANGE( "Invalid column access index" );
438  }
439  return (*this)(i,j);
440 }
441 //*************************************************************************************************
442 
443 
444 //*************************************************************************************************
456 template< typename Type > // Data type of the matrix
459 {
460  return list_.begin()->begin();
461 }
462 //*************************************************************************************************
463 
464 
465 //*************************************************************************************************
473 template< typename Type > // Data type of the matrix
475  InitializerMatrix<Type>::data( size_t i ) const noexcept
476 {
477  BLAZE_USER_ASSERT( i < m_, "Invalid dense matrix row access index" );
478  return list_.begin()[i].begin();
479 }
480 //*************************************************************************************************
481 
482 
483 //*************************************************************************************************
491 template< typename Type > // Data type of the matrix
493  InitializerMatrix<Type>::begin( size_t i ) const noexcept
494 {
495  BLAZE_USER_ASSERT( i < m_, "Invalid dense matrix row access index" );
496  return ConstIterator( 0UL, list_.begin()[i] );
497 }
498 //*************************************************************************************************
499 
500 
501 //*************************************************************************************************
509 template< typename Type > // Data type of the matrix
511  InitializerMatrix<Type>::cbegin( size_t i ) const noexcept
512 {
513  BLAZE_USER_ASSERT( i < m_, "Invalid dense matrix row access index" );
514  return ConstIterator( 0UL, list_.begin()[i] );
515 }
516 //*************************************************************************************************
517 
518 
519 //*************************************************************************************************
527 template< typename Type > // Data type of the matrix
529  InitializerMatrix<Type>::end( size_t i ) const noexcept
530 {
531  BLAZE_USER_ASSERT( i < m_, "Invalid dense matrix row access index" );
532  return ConstIterator( n_, list_.begin()[i] );
533 }
534 //*************************************************************************************************
535 
536 
537 //*************************************************************************************************
545 template< typename Type > // Data type of the matrix
547  InitializerMatrix<Type>::cend( size_t i ) const noexcept
548 {
549  BLAZE_USER_ASSERT( i < m_, "Invalid dense matrix row access index" );
550  return ConstIterator( n_, list_.begin()[i] );
551 }
552 //*************************************************************************************************
553 
554 
555 
556 
557 //=================================================================================================
558 //
559 // UTILITY FUNCTIONS
560 //
561 //=================================================================================================
562 
563 //*************************************************************************************************
568 template< typename Type > // Data type of the matrix
569 inline size_t InitializerMatrix<Type>::rows() const noexcept
570 {
571  return m_;
572 }
573 //*************************************************************************************************
574 
575 
576 //*************************************************************************************************
581 template< typename Type > // Data type of the matrix
582 inline size_t InitializerMatrix<Type>::columns() const noexcept
583 {
584  return n_;
585 }
586 //*************************************************************************************************
587 
588 
589 //*************************************************************************************************
597 template< typename Type > // Data type of the matrix
598 inline size_t InitializerMatrix<Type>::spacing() const noexcept
599 {
600  return m_;
601 }
602 //*************************************************************************************************
603 
604 
605 //*************************************************************************************************
610 template< typename Type > // Data type of the matrix
611 inline size_t InitializerMatrix<Type>::capacity() const noexcept
612 {
613  return m_ * n_;
614 }
615 //*************************************************************************************************
616 
617 
618 //*************************************************************************************************
626 template< typename Type > // Data type of the matrix
627 inline size_t InitializerMatrix<Type>::capacity( size_t i ) const noexcept
628 {
629  UNUSED_PARAMETER( i );
630  BLAZE_USER_ASSERT( i < rows(), "Invalid row access index" );
631  return n_;
632 }
633 //*************************************************************************************************
634 
635 
636 //*************************************************************************************************
641 template< typename Type > // Data type of the matrix
643 {
644  size_t nonzeros( 0 );
645 
646  for( const auto& rowList : list_ ) {
647  for( size_t i=0UL; i<rowList.size(); ++i ) {
648  if( !isDefault( rowList.begin()[i] ) )
649  ++nonzeros;
650  }
651  }
652 
653  return nonzeros;
654 }
655 //*************************************************************************************************
656 
657 
658 //*************************************************************************************************
666 template< typename Type > // Data type of the matrix
667 inline size_t InitializerMatrix<Type>::nonZeros( size_t i ) const
668 {
669  using blaze::nonZeros;
670 
671  BLAZE_USER_ASSERT( i < rows(), "Invalid row access index" );
672 
673  return nonZeros( list_.begin()[i] );
674 }
675 //*************************************************************************************************
676 
677 
678 //*************************************************************************************************
684 template< typename Type > // Data type of the matrix
686 {
687  using std::swap;
688 
689  swap( m_ , m.m_ );
690  swap( n_ , m.n_ );
691  swap( list_, m.list_ );
692 }
693 //*************************************************************************************************
694 
695 
696 
697 
698 //=================================================================================================
699 //
700 // EXPRESSION TEMPLATE EVALUATION FUNCTIONS
701 //
702 //=================================================================================================
703 
704 //*************************************************************************************************
714 template< typename Type > // Data type of the matrix
715 template< typename Other > // Data type of the foreign expression
716 inline bool InitializerMatrix<Type>::canAlias( const Other* alias ) const noexcept
717 {
718  return static_cast<const void*>( this ) == static_cast<const void*>( alias );
719 }
720 //*************************************************************************************************
721 
722 
723 //*************************************************************************************************
733 template< typename Type > // Data type of the matrix
734 template< typename Other > // Data type of the foreign expression
735 inline bool InitializerMatrix<Type>::isAliased( const Other* alias ) const noexcept
736 {
737  return static_cast<const void*>( this ) == static_cast<const void*>( alias );
738 }
739 //*************************************************************************************************
740 
741 
742 
743 
744 //=================================================================================================
745 //
746 // INITIALIZERMATRIX OPERATORS
747 //
748 //=================================================================================================
749 
750 //*************************************************************************************************
753 template< typename Type >
754 bool isIntact( const InitializerMatrix<Type>& m ) noexcept;
755 
756 template< typename Type >
759 //*************************************************************************************************
760 
761 
762 //*************************************************************************************************
779 template< typename Type >
780 inline bool isIntact( const InitializerMatrix<Type>& m ) noexcept
781 {
782  UNUSED_PARAMETER( m );
783 
784  return true;
785 }
786 //*************************************************************************************************
787 
788 
789 //*************************************************************************************************
797 template< typename Type >
799 {
800  a.swap( b );
801 }
802 //*************************************************************************************************
803 
804 
805 
806 
807 //=================================================================================================
808 //
809 // HASCONSTDATAACCESS SPECIALIZATIONS
810 //
811 //=================================================================================================
812 
813 //*************************************************************************************************
815 template< typename T >
816 struct HasConstDataAccess< InitializerMatrix<T> >
817  : public TrueType
818 {};
820 //*************************************************************************************************
821 
822 
823 
824 
825 //=================================================================================================
826 //
827 // ISINITIALIZER SPECIALIZATIONS
828 //
829 //=================================================================================================
830 
831 //*************************************************************************************************
833 template< typename T >
834 struct IsInitializer< InitializerMatrix<T> >
835  : public TrueType
836 {};
838 //*************************************************************************************************
839 
840 
841 
842 
843 //=================================================================================================
844 //
845 // HIGHTYPE SPECIALIZATIONS
846 //
847 //=================================================================================================
848 
849 //*************************************************************************************************
851 template< typename T1, typename T2 >
852 struct HighType< InitializerMatrix<T1>, InitializerMatrix<T2> >
853 {
854  using Type = InitializerMatrix< typename HighType<T1,T2>::Type >;
855 };
857 //*************************************************************************************************
858 
859 
860 
861 
862 //=================================================================================================
863 //
864 // LOWTYPE SPECIALIZATIONS
865 //
866 //=================================================================================================
867 
868 //*************************************************************************************************
870 template< typename T1, typename T2 >
871 struct LowType< InitializerMatrix<T1>, InitializerMatrix<T2> >
872 {
873  using Type = InitializerMatrix< typename LowType<T1,T2>::Type >;
874 };
876 //*************************************************************************************************
877 
878 } // namespace blaze
879 
880 #endif
ConstReference at(size_t i, size_t j) const
Checked access to the matrix elements.
Definition: InitializerMatrix.h:431
#define BLAZE_CONSTRAINT_MUST_NOT_BE_CONST(T)
Constraint on the data type.In case the given data type is a const-qualified type, a compilation error is created.
Definition: Const.h:79
#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
Header file for the IsInitializer type trait.
#define BLAZE_USER_ASSERT(expr, msg)
Run time assertion macro for user checks.In case of an invalid run time expression, the program execution is terminated. The BLAZE_USER_ASSERT macro can be disabled by setting the BLAZE_USER_ASSERT flag to zero or by defining NDEBUG during the compilation.
Definition: Assert.h:117
Header file for the UNUSED_PARAMETER function template.
Header file for basic type definitions.
Implementation of an iterator for (extended) initializer lists.The InitializerIterator represents a g...
Definition: InitializerIterator.h:56
Dense matrix representation of an initializer list.The InitializerMatrix class template is a dense ma...
Definition: InitializerMatrix.h:176
static const Type zero_
Neutral element for accesses to zero elements.
Definition: InitializerMatrix.h:313
const Type * ConstPointer
Pointer to a constant matrix value.
Definition: InitializerMatrix.h:193
const Type & Reference
Reference to a non-constant matrix value.
Definition: InitializerMatrix.h:190
size_t m_
The current number of rows of the compressed matrix.
Definition: CompressedMatrix.h:3289
#define BLAZE_CONSTRAINT_MUST_NOT_BE_VOLATILE(T)
Constraint on the data type.In case the given data type is a volatile-qualified type, a compilation error is created.
Definition: Volatile.h:79
size_t m_
The current number of rows of the matrix.
Definition: InitializerMatrix.h:301
size_t nonZeros(const Matrix< MT, SO > &matrix)
Returns the total number of non-zero elements in the matrix.
Definition: Matrix.h:584
const Type * Pointer
Pointer to a non-constant matrix value.
Definition: InitializerMatrix.h:192
BoolConstant< true > TrueType
Type traits base class.The TrueType class is used as base class for type traits and value traits that...
Definition: TrueType.h:61
Header file for the extended initializer_list functionality.
Efficient implementation of a dynamic matrix.The DynamicMatrix class template is the representation ...
Definition: DynamicMatrix.h:220
constexpr void UNUSED_PARAMETER(const Args &...)
Suppression of unused parameter warnings.
Definition: Unused.h:81
size_t nonZeros() const
Returns the total number of non-zero elements in the matrix.
Definition: InitializerMatrix.h:642
Base class for dense matrices.The DenseMatrix class is a base class for all dense matrix classes...
Definition: DenseMatrix.h:80
static constexpr bool smpAssignable
Compilation flag for SMP assignments.
Definition: InitializerMatrix.h:230
Constraint on the data type.
Header file for the LowType type trait.
ConstIterator begin(size_t i) const noexcept
Returns an iterator to the first element of row i.
Definition: InitializerMatrix.h:493
Namespace of the Blaze C++ math library.
Definition: Blaze.h:58
void swap(CompressedMatrix< Type, SO > &a, CompressedMatrix< Type, SO > &b) noexcept
Swapping the contents of two compressed matrices.
Definition: CompressedMatrix.h:5907
ListType list_
The initializer list represented by the matrix.
Definition: InitializerMatrix.h:303
size_t rows() const noexcept
Returns the current number of rows of the matrix.
Definition: InitializerMatrix.h:569
size_t columns() const noexcept
Returns the current number of columns of the matrix.
Definition: InitializerMatrix.h:582
Header file for all forward declarations of the math module.
#define BLAZE_CONSTRAINT_MUST_NOT_BE_POINTER_TYPE(T)
Constraint on the data type.In case the given data type T is not a pointer type, a compilation error ...
Definition: Pointer.h:79
#define BLAZE_THROW_OUT_OF_RANGE(MESSAGE)
Macro for the emission of a std::out_of_range exception.This macro encapsulates the default way of Bl...
Definition: Exception.h:331
Header file for the DenseMatrix base class.
const Element * ConstIterator
Iterator over constant elements.
Definition: CompressedMatrix.h:3086
static constexpr bool simdEnabled
Compilation flag for SIMD optimization.
Definition: InitializerMatrix.h:224
Constraint on the data type.
size_t spacing() const noexcept
Returns the spacing between the beginning of two rows.
Definition: InitializerMatrix.h:598
bool canAlias(const Other *alias) const noexcept
Returns whether the matrix can alias with the given address alias.
Definition: InitializerMatrix.h:716
Rebind mechanism to obtain a InitializerMatrix with different data/element type.
Definition: InitializerMatrix.h:203
ConstPointer data() const noexcept
Low-level data access to the matrix elements.
Definition: InitializerMatrix.h:458
Type ElementType
Type of the matrix elements.
Definition: InitializerMatrix.h:186
Header file for the exception macros of the math module.
ConstIterator cend(size_t i) const noexcept
Returns an iterator just past the last element of row i.
Definition: InitializerMatrix.h:547
ConstReference operator()(size_t i, size_t j) const noexcept
2D-access to the matrix elements.
Definition: InitializerMatrix.h:403
Header file for the HasConstDataAccess type trait.
Header file for run time assertion macros.
Constraint on the data type.
Resize mechanism to obtain a InitializerMatrix with different fixed dimensions.
Definition: InitializerMatrix.h:213
ConstIterator cbegin(size_t i) const noexcept
Returns an iterator to the first element of row i.
Definition: InitializerMatrix.h:511
#define BLAZE_CONSTRAINT_MUST_NOT_BE_REFERENCE_TYPE(T)
Constraint on the data type.In case the given data type T is not a reference type, a compilation error is created.
Definition: Reference.h:79
void swap(DiagonalMatrix< MT, SO, DF > &a, DiagonalMatrix< MT, SO, DF > &b) noexcept
Swapping the contents of two matrices.
Definition: DiagonalMatrix.h:281
constexpr size_t size(const Matrix< MT, SO > &matrix) noexcept
Returns the total number of elements of the matrix.
Definition: Matrix.h:530
Constraint on the data type.
void swap(InitializerMatrix &m) noexcept
Swapping the contents of two matrices.
Definition: InitializerMatrix.h:685
constexpr size_t rows(const Matrix< MT, SO > &matrix) noexcept
Returns the current number of rows of the matrix.
Definition: Matrix.h:498
InitializerMatrix(initializer_list< initializer_list< Type > > list) noexcept
Constructor for InitializerMatrix.
Definition: InitializerMatrix.h:355
size_t n_
The current number of columns of the compressed matrix.
Definition: CompressedMatrix.h:3290
ConstIterator end(size_t i) const noexcept
Returns an iterator just past the last element of row i.
Definition: InitializerMatrix.h:529
size_t n_
The current number of columns of the matrix.
Definition: InitializerMatrix.h:302
bool isAliased(const Other *alias) const noexcept
Returns whether the matrix is aliased with the given address alias.
Definition: InitializerMatrix.h:735
Initializer list type of the Blaze library.
static const Type zero_
Neutral element for accesses to zero elements.
Definition: CompressedMatrix.h:3295
Header file for the InitializerIterator class template.
bool isIntact(const DiagonalMatrix< MT, SO, DF > &m)
Returns whether the invariants of the given diagonal matrix are intact.
Definition: DiagonalMatrix.h:263
bool isDefault(const DiagonalProxy< MT > &proxy)
Returns whether the represented element is in default state.
Definition: DiagonalProxy.h:631
const Type & ConstReference
Reference to a constant matrix value.
Definition: InitializerMatrix.h:191
size_t capacity() const noexcept
Returns the maximum capacity of the matrix.
Definition: InitializerMatrix.h:611
constexpr size_t determineColumns(initializer_list< initializer_list< Type > > list) noexcept
Determines the maximum number of columns specified by the given initializer list. ...
Definition: InitializerList.h:108
const Type & ReturnType
Return type for expression template evaluations.
Definition: InitializerMatrix.h:187
Header file for the HighType type trait.
Header file for the TrueType type/value trait base class.