Blaze 3.9
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>
48#include <blaze/math/Forward.h>
54#include <blaze/util/Assert.h>
61#include <blaze/util/Types.h>
62
63
64namespace blaze {
65
66//=================================================================================================
67//
68// CLASS DEFINITION
69//
70//=================================================================================================
71
72//*************************************************************************************************
181template< typename Type // Data type of the matrix
182 , typename Tag > // Type tag
184 : public DenseMatrix< InitializerMatrix<Type,Tag>, false >
185{
186 public:
187 //**Type definitions****************************************************************************
190
193
196
198 using ElementType = Type;
199 using TagType = Tag;
200 using ReturnType = const Type&;
201 using CompositeType = const This&;
202
203 using Reference = const Type&;
204 using ConstReference = const Type&;
205 using Pointer = const Type*;
206 using ConstPointer = const Type*;
207
210 //**********************************************************************************************
211
212 //**Rebind struct definition********************************************************************
215 template< typename NewType > // Data type of the other matrix
216 struct Rebind {
218 };
219 //**********************************************************************************************
220
221 //**Resize struct definition********************************************************************
224 template< size_t NewM // Number of rows of the other matrix
225 , size_t NewN > // Number of columns of the other matrix
226 struct Resize {
228 };
229 //**********************************************************************************************
230
231 //**Compilation flags***************************************************************************
233
237 static constexpr bool simdEnabled = false;
238
240
243 static constexpr bool smpAssignable = false;
244 //**********************************************************************************************
245
246 //**Constructors********************************************************************************
249 inline InitializerMatrix( initializer_list< initializer_list<Type> > list ) noexcept;
250 inline InitializerMatrix( initializer_list< initializer_list<Type> > list, size_t n );
251
252 InitializerMatrix( const InitializerMatrix& ) = default;
254 //**********************************************************************************************
255
256 //**Destructor**********************************************************************************
259 ~InitializerMatrix() = default;
261 //**********************************************************************************************
262
263 //**Data access functions***********************************************************************
266 inline ConstReference operator()( size_t i, size_t j ) const noexcept;
267 inline ConstReference at( size_t i, size_t j ) const;
268 inline ConstPointer data () const noexcept;
269 inline ConstPointer data ( size_t i ) const noexcept;
270 inline ConstIterator begin ( size_t i ) const noexcept;
271 inline ConstIterator cbegin( size_t i ) const noexcept;
272 inline ConstIterator end ( size_t i ) const noexcept;
273 inline ConstIterator cend ( size_t i ) const noexcept;
275 //**********************************************************************************************
276
277 //**Assignment operators************************************************************************
280 InitializerMatrix& operator=( const InitializerMatrix& ) = delete;
282 //**********************************************************************************************
283
284 //**Utility functions***************************************************************************
287 inline size_t rows() const noexcept;
288 inline size_t columns() const noexcept;
289 inline size_t spacing() const noexcept;
290 inline size_t capacity() const noexcept;
291 inline size_t capacity( size_t i ) const noexcept;
292 inline size_t nonZeros() const;
293 inline size_t nonZeros( size_t i ) const;
294 inline void swap( InitializerMatrix& m ) noexcept;
296 //**********************************************************************************************
297
298 //**Expression template evaluation functions****************************************************
301 template< typename Other > inline bool canAlias ( const Other* alias ) const noexcept;
302 template< typename Other > inline bool isAliased( const Other* alias ) const noexcept;
304 //**********************************************************************************************
305
306 private:
307 //**Type definitions****************************************************************************
308 using ListType = initializer_list< initializer_list<Type> >;
309 //**********************************************************************************************
310
311 //**Member variables****************************************************************************
314 size_t m_;
315 size_t n_;
326 static const Type zero_;
328 //**********************************************************************************************
329
330 //**Compile time checks*************************************************************************
337 //**********************************************************************************************
338};
339//*************************************************************************************************
340
341
342
343
344//=================================================================================================
345//
346// DEFINITION AND INITIALIZATION OF THE STATIC MEMBER VARIABLES
347//
348//=================================================================================================
349
350template< typename Type // Data type of the matrix
351 , typename Tag > // Type tag
352const Type InitializerMatrix<Type,Tag>::zero_{};
353
354
355
356
357//=================================================================================================
358//
359// CONSTRUCTORS
360//
361//=================================================================================================
362
363//*************************************************************************************************
368template< typename Type // Data type of the matrix
369 , typename Tag > // Type tag
370inline InitializerMatrix<Type,Tag>::InitializerMatrix( initializer_list< initializer_list<Type> > list ) noexcept
371 : m_ ( list.size() ) // The current number of rows of the matrix
372 , n_ ( determineColumns( list ) ) // The current number of columns of the matrix
373 , list_( list ) // The initializer list represented by the matrix
374{}
375//*************************************************************************************************
376
377
378//*************************************************************************************************
385template< typename Type // Data type of the matrix
386 , typename Tag > // Type tag
387inline InitializerMatrix<Type,Tag>::InitializerMatrix( initializer_list< initializer_list<Type> > list, size_t n )
388 : m_ ( list.size() ) // The current number of rows of the matrix
389 , n_ ( n ) // The current number of columns of the matrix
390 , list_( list ) // The initializer list represented by the matrix
391{
392 if( n < determineColumns( list ) ) {
393 BLAZE_THROW_INVALID_ARGUMENT( "Invalid initializer list dimension" );
394 }
395}
396//*************************************************************************************************
397
398
399
400
401//=================================================================================================
402//
403// DATA ACCESS FUNCTIONS
404//
405//=================================================================================================
406
407//*************************************************************************************************
417template< typename Type // Data type of the matrix
418 , typename Tag > // Type tag
420 InitializerMatrix<Type,Tag>::operator()( size_t i, size_t j ) const noexcept
421{
422 BLAZE_USER_ASSERT( i<m_, "Invalid row access index" );
423 BLAZE_USER_ASSERT( j<n_, "Invalid column access index" );
424
425 const initializer_list<Type>& list( list_.begin()[i] );
426
427 if( j < list.size() )
428 return list.begin()[j];
429 else
430 return zero_;
431}
432//*************************************************************************************************
433
434
435//*************************************************************************************************
446template< typename Type // Data type of the matrix
447 , typename Tag > // Type tag
449 InitializerMatrix<Type,Tag>::at( size_t i, size_t j ) const
450{
451 if( i >= m_ ) {
452 BLAZE_THROW_OUT_OF_RANGE( "Invalid row access index" );
453 }
454 if( j >= n_ ) {
455 BLAZE_THROW_OUT_OF_RANGE( "Invalid column access index" );
456 }
457 return (*this)(i,j);
458}
459//*************************************************************************************************
460
461
462//*************************************************************************************************
474template< typename Type // Data type of the matrix
475 , typename Tag > // Type tag
478{
479 return list_.begin()->begin();
480}
481//*************************************************************************************************
482
483
484//*************************************************************************************************
492template< typename Type // Data type of the matrix
493 , typename Tag > // Type tag
495 InitializerMatrix<Type,Tag>::data( size_t i ) const noexcept
496{
497 BLAZE_USER_ASSERT( i < m_, "Invalid dense matrix row access index" );
498 return list_.begin()[i].begin();
499}
500//*************************************************************************************************
501
502
503//*************************************************************************************************
511template< typename Type // Data type of the matrix
512 , typename Tag > // Type tag
514 InitializerMatrix<Type,Tag>::begin( size_t i ) const noexcept
515{
516 BLAZE_USER_ASSERT( i < m_, "Invalid dense matrix row access index" );
517 return ConstIterator( 0UL, list_.begin()[i] );
518}
519//*************************************************************************************************
520
521
522//*************************************************************************************************
530template< typename Type // Data type of the matrix
531 , typename Tag > // Type tag
533 InitializerMatrix<Type,Tag>::cbegin( size_t i ) const noexcept
534{
535 BLAZE_USER_ASSERT( i < m_, "Invalid dense matrix row access index" );
536 return ConstIterator( 0UL, list_.begin()[i] );
537}
538//*************************************************************************************************
539
540
541//*************************************************************************************************
549template< typename Type // Data type of the matrix
550 , typename Tag > // Type tag
552 InitializerMatrix<Type,Tag>::end( size_t i ) const noexcept
553{
554 BLAZE_USER_ASSERT( i < m_, "Invalid dense matrix row access index" );
555 return ConstIterator( n_, list_.begin()[i] );
556}
557//*************************************************************************************************
558
559
560//*************************************************************************************************
568template< typename Type // Data type of the matrix
569 , typename Tag > // Type tag
571 InitializerMatrix<Type,Tag>::cend( size_t i ) const noexcept
572{
573 BLAZE_USER_ASSERT( i < m_, "Invalid dense matrix row access index" );
574 return ConstIterator( n_, list_.begin()[i] );
575}
576//*************************************************************************************************
577
578
579
580
581//=================================================================================================
582//
583// UTILITY FUNCTIONS
584//
585//=================================================================================================
586
587//*************************************************************************************************
592template< typename Type // Data type of the matrix
593 , typename Tag > // Type tag
594inline size_t InitializerMatrix<Type,Tag>::rows() const noexcept
595{
596 return m_;
597}
598//*************************************************************************************************
599
600
601//*************************************************************************************************
606template< typename Type // Data type of the matrix
607 , typename Tag > // Type tag
608inline size_t InitializerMatrix<Type,Tag>::columns() const noexcept
609{
610 return n_;
611}
612//*************************************************************************************************
613
614
615//*************************************************************************************************
623template< typename Type // Data type of the matrix
624 , typename Tag > // Type tag
625inline size_t InitializerMatrix<Type,Tag>::spacing() const noexcept
626{
627 return m_;
628}
629//*************************************************************************************************
630
631
632//*************************************************************************************************
637template< typename Type // Data type of the matrix
638 , typename Tag > // Type tag
639inline size_t InitializerMatrix<Type,Tag>::capacity() const noexcept
640{
641 return m_ * n_;
642}
643//*************************************************************************************************
644
645
646//*************************************************************************************************
654template< typename Type // Data type of the matrix
655 , typename Tag > // Type tag
656inline size_t InitializerMatrix<Type,Tag>::capacity( size_t i ) const noexcept
657{
658 MAYBE_UNUSED( i );
659 BLAZE_USER_ASSERT( i < rows(), "Invalid row access index" );
660 return n_;
661}
662//*************************************************************************************************
663
664
665//*************************************************************************************************
674template< typename Type // Data type of the matrix
675 , typename Tag > // Type tag
677{
678 size_t nonzeros( 0 );
679
680 for( const auto& rowList : list_ ) {
681 for( size_t i=0UL; i<rowList.size(); ++i ) {
682 if( !isDefault<strict>( rowList.begin()[i] ) )
683 ++nonzeros;
684 }
685 }
686
687 return nonzeros;
688}
689//*************************************************************************************************
690
691
692//*************************************************************************************************
701template< typename Type // Data type of the matrix
702 , typename Tag > // Type tag
703inline size_t InitializerMatrix<Type,Tag>::nonZeros( size_t i ) const
704{
705 using blaze::nonZeros;
706
707 BLAZE_USER_ASSERT( i < rows(), "Invalid row access index" );
708
709 return nonZeros( list_.begin()[i] );
710}
711//*************************************************************************************************
712
713
714//*************************************************************************************************
720template< typename Type // Data type of the matrix
721 , typename Tag > // Type tag
723{
724 using std::swap;
725
726 swap( m_ , m.m_ );
727 swap( n_ , m.n_ );
728 swap( list_, m.list_ );
729}
730//*************************************************************************************************
731
732
733
734
735//=================================================================================================
736//
737// EXPRESSION TEMPLATE EVALUATION FUNCTIONS
738//
739//=================================================================================================
740
741//*************************************************************************************************
751template< typename Type // Data type of the matrix
752 , typename Tag > // Type tag
753template< typename Other > // Data type of the foreign expression
754inline bool InitializerMatrix<Type,Tag>::canAlias( const Other* alias ) const noexcept
755{
756 return static_cast<const void*>( this ) == static_cast<const void*>( alias );
757}
758//*************************************************************************************************
759
760
761//*************************************************************************************************
771template< typename Type // Data type of the matrix
772 , typename Tag > // Type tag
773template< typename Other > // Data type of the foreign expression
774inline bool InitializerMatrix<Type,Tag>::isAliased( const Other* alias ) const noexcept
775{
776 return static_cast<const void*>( this ) == static_cast<const void*>( alias );
777}
778//*************************************************************************************************
779
780
781
782
783//=================================================================================================
784//
785// INITIALIZERMATRIX OPERATORS
786//
787//=================================================================================================
788
789//*************************************************************************************************
792template< typename Type, typename Tag >
793bool isIntact( const InitializerMatrix<Type,Tag>& m ) noexcept;
794
795template< typename Type, typename Tag >
798//*************************************************************************************************
799
800
801//*************************************************************************************************
818template< typename Type // Data type of the matrix
819 , typename Tag > // Type tag
820inline bool isIntact( const InitializerMatrix<Type,Tag>& m ) noexcept
821{
822 MAYBE_UNUSED( m );
823
824 return true;
825}
826//*************************************************************************************************
827
828
829//*************************************************************************************************
837template< typename Type // Data type of the matrix
838 , typename Tag > // Type tag
840{
841 a.swap( b );
842}
843//*************************************************************************************************
844
845
846
847
848//=================================================================================================
849//
850// HASCONSTDATAACCESS SPECIALIZATIONS
851//
852//=================================================================================================
853
854//*************************************************************************************************
856template< typename T, typename Tag >
857struct HasConstDataAccess< InitializerMatrix<T,Tag> >
858 : public TrueType
859{};
861//*************************************************************************************************
862
863
864
865
866//=================================================================================================
867//
868// ISINITIALIZER SPECIALIZATIONS
869//
870//=================================================================================================
871
872//*************************************************************************************************
874template< typename T, typename Tag >
875struct IsInitializer< InitializerMatrix<T,Tag> >
876 : public TrueType
877{};
879//*************************************************************************************************
880
881
882
883
884//=================================================================================================
885//
886// HIGHTYPE SPECIALIZATIONS
887//
888//=================================================================================================
889
890//*************************************************************************************************
892template< typename T1, typename Tag, typename T2 >
893struct HighType< InitializerMatrix<T1,Tag>, InitializerMatrix<T2,Tag> >
894{
895 using Type = InitializerMatrix< typename HighType<T1,T2>::Type, Tag >;
896};
898//*************************************************************************************************
899
900
901
902
903//=================================================================================================
904//
905// LOWTYPE SPECIALIZATIONS
906//
907//=================================================================================================
908
909//*************************************************************************************************
911template< typename T1, typename Tag, typename T2 >
912struct LowType< InitializerMatrix<T1,Tag>, InitializerMatrix<T2,Tag> >
913{
914 using Type = InitializerMatrix< typename LowType<T1,T2>::Type, Tag >;
915};
917//*************************************************************************************************
918
919} // namespace blaze
920
921#endif
Header file for run time assertion macros.
Constraint on the data type.
Header file for the HasConstDataAccess type trait.
Header file for the HighType type trait.
Header file for the InitializerIterator class template.
Header file for the IntegralConstant class template.
Header file for the IsInitializer type trait.
Header file for the LowType type trait.
Header file for the MAYBE_UNUSED function template.
Constraint on the data type.
Constraint on the data type.
Constraint on the data type.
Base class for dense matrices.
Definition: DenseMatrix.h:82
Efficient implementation of a dynamic matrix.
Definition: DynamicMatrix.h:242
Implementation of an iterator for (extended) initializer lists.
Definition: InitializerIterator.h:57
Dense matrix representation of an initializer list.
Definition: InitializerMatrix.h:185
size_t rows() const noexcept
Returns the current number of rows of the matrix.
Definition: InitializerMatrix.h:594
Type ElementType
Type of the matrix elements.
Definition: InitializerMatrix.h:198
ConstIterator cbegin(size_t i) const noexcept
Returns an iterator to the first element of row i.
Definition: InitializerMatrix.h:533
static constexpr bool smpAssignable
Compilation flag for SMP assignments.
Definition: InitializerMatrix.h:243
bool canAlias(const Other *alias) const noexcept
Returns whether the matrix can alias with the given address alias.
Definition: InitializerMatrix.h:754
size_t nonZeros() const
Returns the total number of non-zero elements in the matrix.
Definition: InitializerMatrix.h:676
ConstPointer data() const noexcept
Low-level data access to the matrix elements.
Definition: InitializerMatrix.h:477
ConstReference operator()(size_t i, size_t j) const noexcept
2D-access to the matrix elements.
Definition: InitializerMatrix.h:420
const Type * ConstPointer
Pointer to a constant matrix value.
Definition: InitializerMatrix.h:206
const Type & Reference
Reference to a non-constant matrix value.
Definition: InitializerMatrix.h:203
size_t spacing() const noexcept
Returns the spacing between the beginning of two rows.
Definition: InitializerMatrix.h:625
ConstIterator end(size_t i) const noexcept
Returns an iterator just past the last element of row i.
Definition: InitializerMatrix.h:552
Tag TagType
Tag type of this InitializerVector instance.
Definition: InitializerMatrix.h:199
size_t m_
The current number of rows of the matrix.
Definition: InitializerMatrix.h:314
bool isAliased(const Other *alias) const noexcept
Returns whether the matrix is aliased with the given address alias.
Definition: InitializerMatrix.h:774
size_t columns() const noexcept
Returns the current number of columns of the matrix.
Definition: InitializerMatrix.h:608
static constexpr bool simdEnabled
Compilation flag for SIMD optimization.
Definition: InitializerMatrix.h:237
void swap(InitializerMatrix &m) noexcept
Swapping the contents of two matrices.
Definition: InitializerMatrix.h:722
ConstIterator cend(size_t i) const noexcept
Returns an iterator just past the last element of row i.
Definition: InitializerMatrix.h:571
ConstReference at(size_t i, size_t j) const
Checked access to the matrix elements.
Definition: InitializerMatrix.h:449
InitializerMatrix(initializer_list< initializer_list< Type > > list) noexcept
Constructor for InitializerMatrix.
Definition: InitializerMatrix.h:370
const Type & ConstReference
Reference to a constant matrix value.
Definition: InitializerMatrix.h:204
static const Type zero_
Neutral element for accesses to zero elements.
Definition: InitializerMatrix.h:326
size_t capacity() const noexcept
Returns the maximum capacity of the matrix.
Definition: InitializerMatrix.h:639
size_t n_
The current number of columns of the matrix.
Definition: InitializerMatrix.h:315
ListType list_
The initializer list represented by the matrix.
Definition: InitializerMatrix.h:316
const Type * Pointer
Pointer to a non-constant matrix value.
Definition: InitializerMatrix.h:205
initializer_list< initializer_list< Type > > ListType
Type of the represented initializer list.
Definition: InitializerMatrix.h:308
const Type & ReturnType
Return type for expression template evaluations.
Definition: InitializerMatrix.h:200
ConstIterator begin(size_t i) const noexcept
Returns an iterator to the first element of row i.
Definition: InitializerMatrix.h:514
Header file for the DenseMatrix base class.
#define BLAZE_CONSTRAINT_MUST_NOT_BE_VOLATILE(T)
Constraint on the data type.
Definition: Volatile.h:79
#define BLAZE_CONSTRAINT_MUST_NOT_BE_POINTER_TYPE(T)
Constraint on the data type.
Definition: Pointer.h:79
#define BLAZE_CONSTRAINT_MUST_NOT_BE_CONST(T)
Constraint on the data type.
Definition: Const.h:79
#define BLAZE_CONSTRAINT_MUST_NOT_BE_REFERENCE_TYPE(T)
Constraint on the data type.
Definition: Reference.h:79
void swap(InitializerMatrix< Type, Tag > &a, InitializerMatrix< Type, Tag > &b) noexcept
Swapping the contents of two initializer matrices.
Definition: InitializerMatrix.h:839
bool isIntact(const InitializerMatrix< Type, Tag > &m) noexcept
Returns whether the invariants of the given initializer matrix are intact.
Definition: InitializerMatrix.h:820
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:107
constexpr size_t rows(const Matrix< MT, SO > &matrix) noexcept
Returns the current number of rows of the matrix.
Definition: Matrix.h:644
size_t nonZeros(const Matrix< MT, SO > &matrix)
Returns the total number of non-zero elements in the matrix.
Definition: Matrix.h:730
constexpr size_t size(const Matrix< MT, SO > &matrix) noexcept
Returns the total number of elements of the matrix.
Definition: Matrix.h:676
#define BLAZE_USER_ASSERT(expr, msg)
Run time assertion macro for user checks.
Definition: Assert.h:117
BoolConstant< true > TrueType
Type traits base class.
Definition: IntegralConstant.h:132
constexpr void MAYBE_UNUSED(const Args &...)
Suppression of unused parameter warnings.
Definition: MaybeUnused.h:81
#define BLAZE_THROW_OUT_OF_RANGE(MESSAGE)
Macro for the emission of a std::out_of_range exception.
Definition: Exception.h:331
#define BLAZE_THROW_INVALID_ARGUMENT(MESSAGE)
Macro for the emission of a std::invalid_argument exception.
Definition: Exception.h:235
Header file for the exception macros of the math module.
Header file for all forward declarations of the math module.
Header file for the extended initializer_list functionality.
Rebind mechanism to obtain a InitializerMatrix with different data/element type.
Definition: InitializerMatrix.h:216
Resize mechanism to obtain a InitializerMatrix with different fixed dimensions.
Definition: InitializerMatrix.h:226
Header file for basic type definitions.