InitializerVector.h
Go to the documentation of this file.
1 //=================================================================================================
33 //=================================================================================================
34 
35 #ifndef _BLAZE_MATH_DENSE_INITIALIZERVECTOR_H_
36 #define _BLAZE_MATH_DENSE_INITIALIZERVECTOR_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>
58 #include <blaze/util/Assert.h>
63 #include <blaze/util/TrueType.h>
64 #include <blaze/util/Types.h>
65 #include <blaze/util/Unused.h>
66 
67 
68 namespace blaze {
69 
70 //=================================================================================================
71 //
72 // CLASS DEFINITION
73 //
74 //=================================================================================================
75 
76 //*************************************************************************************************
174 template< typename Type // Data type of the vector
175  , bool TF = defaultTransposeFlag > // Transpose flag
177  : public DenseVector< InitializerVector<Type,TF>, TF >
178 {
179  public:
180  //**Type definitions****************************************************************************
185  using ElementType = Type;
186  using ReturnType = const Type&;
188 
189  using Reference = const Type&;
190  using ConstReference = const Type&;
191  using Pointer = const Type*;
192  using ConstPointer = const Type*;
193 
196  //**********************************************************************************************
197 
198  //**Rebind struct definition********************************************************************
201  template< typename NewType > // Data type of the other vector
202  struct Rebind {
204  };
205  //**********************************************************************************************
206 
207  //**Resize struct definition********************************************************************
210  template< size_t NewN > // Number of elements of the other vector
211  struct Resize {
213  };
214  //**********************************************************************************************
215 
216  //**Compilation flags***************************************************************************
218 
222  enum : bool { simdEnabled = false };
223 
225 
228  enum : bool { smpAssignable = false };
229  //**********************************************************************************************
230 
231  //**Constructors********************************************************************************
234  explicit inline InitializerVector( initializer_list<Type> list ) noexcept;
235  explicit inline InitializerVector( initializer_list<Type> list, size_t n );
236  // No explicitly declared copy constructor.
238  //**********************************************************************************************
239 
240  //**Destructor**********************************************************************************
241  // No explicitly declared destructor.
242  //**********************************************************************************************
243 
244  //**Data access functions***********************************************************************
247  inline ConstReference operator[]( size_t index ) const noexcept;
248  inline ConstReference at( size_t index ) const;
249  inline ConstPointer data () const noexcept;
250  inline ConstIterator begin () const noexcept;
251  inline ConstIterator cbegin() const noexcept;
252  inline ConstIterator end () const noexcept;
253  inline ConstIterator cend () const noexcept;
255  //**********************************************************************************************
256 
257  //**Assignment operators************************************************************************
260  InitializerVector( const InitializerVector& ) = delete;
262  //**********************************************************************************************
263 
264  //**Utility functions***************************************************************************
267  inline size_t size() const noexcept;
268  inline size_t spacing() const noexcept;
269  inline size_t capacity() const noexcept;
270  inline size_t nonZeros() const;
271  inline void swap( InitializerVector& v ) noexcept;
273  //**********************************************************************************************
274 
275  //**Expression template evaluation functions****************************************************
278  template< typename Other > inline bool canAlias ( const Other* alias ) const noexcept;
279  template< typename Other > inline bool isAliased( const Other* alias ) const noexcept;
281  //**********************************************************************************************
282 
283  private:
284  //**Type definitions****************************************************************************
286  //**********************************************************************************************
287 
288  //**Member variables****************************************************************************
291  size_t size_;
293 
299  static const Type zero_;
300 
301  //**********************************************************************************************
302 
303  //**Compile time checks*************************************************************************
310  //**********************************************************************************************
311 };
312 //*************************************************************************************************
313 
314 
315 
316 
317 //=================================================================================================
318 //
319 // DEFINITION AND INITIALIZATION OF THE STATIC MEMBER VARIABLES
320 //
321 //=================================================================================================
322 
323 template< typename Type // Data type of the vector
324  , bool TF > // Transpose flag
326 
327 
328 
329 
330 //=================================================================================================
331 //
332 // CONSTRUCTORS
333 //
334 //=================================================================================================
335 
336 //*************************************************************************************************
341 template< typename Type // Data type of the vector
342  , bool TF > // Transpose flag
344  : size_( list.size() ) // The current size/dimension of the vector
345  , list_( list ) // The initializer list represented by the vector
346 {}
347 //*************************************************************************************************
348 
349 
350 //*************************************************************************************************
357 template< typename Type // Data type of the vector
358  , bool TF > // Transpose flag
360  : size_( n ) // The current size/dimension of the vector
361  , list_( list ) // The initializer list represented by the vector
362 {
363  if( n < list.size() ) {
364  BLAZE_THROW_INVALID_ARGUMENT( "Invalid initializer list dimension" );
365  }
366 }
367 //*************************************************************************************************
368 
369 
370 
371 
372 //=================================================================================================
373 //
374 // DATA ACCESS FUNCTIONS
375 //
376 //=================================================================================================
377 
378 //*************************************************************************************************
387 template< typename Type // Data type of the vector
388  , bool TF > // Transpose flag
390  InitializerVector<Type,TF>::operator[]( size_t index ) const noexcept
391 {
392  BLAZE_USER_ASSERT( index < size_, "Invalid vector access index" );
393  if( index < list_.size() )
394  return list_.begin()[index];
395  else
396  return zero_;
397 }
398 //*************************************************************************************************
399 
400 
401 //*************************************************************************************************
411 template< typename Type // Data type of the vector
412  , bool TF > // Transpose flag
414  InitializerVector<Type,TF>::at( size_t index ) const
415 {
416  if( index >= size_ ) {
417  BLAZE_THROW_OUT_OF_RANGE( "Invalid vector access index" );
418  }
419  return (*this)[index];
420 }
421 //*************************************************************************************************
422 
423 
424 //*************************************************************************************************
431 template< typename Type // Data type of the vector
432  , bool TF > // Transpose flag
435 {
436  return list_.begin();
437 }
438 //*************************************************************************************************
439 
440 
441 //*************************************************************************************************
446 template< typename Type // Data type of the vector
447  , bool TF > // Transpose flag
450 {
451  return ConstIterator( 0UL, list_ );;
452 }
453 //*************************************************************************************************
454 
455 
456 //*************************************************************************************************
461 template< typename Type // Data type of the vector
462  , bool TF > // Transpose flag
465 {
466  return ConstIterator( 0UL, list_ );
467 }
468 //*************************************************************************************************
469 
470 
471 //*************************************************************************************************
476 template< typename Type // Data type of the vector
477  , bool TF > // Transpose flag
480 {
481  return ConstIterator( size_, list_ );
482 }
483 //*************************************************************************************************
484 
485 
486 //*************************************************************************************************
491 template< typename Type // Data type of the vector
492  , bool TF > // Transpose flag
495 {
496  return ConstIterator( size_, list_ );
497 }
498 //*************************************************************************************************
499 
500 
501 
502 
503 //=================================================================================================
504 //
505 // UTILITY FUNCTIONS
506 //
507 //=================================================================================================
508 
509 //*************************************************************************************************
514 template< typename Type // Data type of the vector
515  , bool TF > // Transpose flag
516 inline size_t InitializerVector<Type,TF>::size() const noexcept
517 {
518  return size_;
519 }
520 //*************************************************************************************************
521 
522 
523 //*************************************************************************************************
531 template< typename Type // Data type of the vector
532  , bool TF > // Transpose flag
533 inline size_t InitializerVector<Type,TF>::spacing() const noexcept
534 {
535  return size_;
536 }
537 //*************************************************************************************************
538 
539 
540 //*************************************************************************************************
545 template< typename Type // Data type of the vector
546  , bool TF > // Transpose flag
547 inline size_t InitializerVector<Type,TF>::capacity() const noexcept
548 {
549  return size_;
550 }
551 //*************************************************************************************************
552 
553 
554 //*************************************************************************************************
562 template< typename Type // Data type of the vector
563  , bool TF > // Transpose flag
565 {
566  size_t nonzeros( 0 );
567 
568  for( size_t i=0UL; i<list_.size(); ++i ) {
569  if( !isDefault( list_.begin()[i] ) )
570  ++nonzeros;
571  }
572 
573  return nonzeros;
574 }
575 //*************************************************************************************************
576 
577 
578 //*************************************************************************************************
584 template< typename Type // Data type of the vector
585  , bool TF > // Transpose flag
587 {
588  using std::swap;
589 
590  swap( size_, v.size_ );
591  swap( list_, v.list_ );
592 }
593 //*************************************************************************************************
594 
595 
596 
597 
598 //=================================================================================================
599 //
600 // EXPRESSION TEMPLATE EVALUATION FUNCTIONS
601 //
602 //=================================================================================================
603 
604 //*************************************************************************************************
614 template< typename Type // Data type of the vector
615  , bool TF > // Transpose flag
616 template< typename Other > // Data type of the foreign expression
617 inline bool InitializerVector<Type,TF>::canAlias( const Other* alias ) const noexcept
618 {
619  return static_cast<const void*>( this ) == static_cast<const void*>( alias );
620 }
621 //*************************************************************************************************
622 
623 
624 //*************************************************************************************************
634 template< typename Type // Data type of the vector
635  , bool TF > // Transpose flag
636 template< typename Other > // Data type of the foreign expression
637 inline bool InitializerVector<Type,TF>::isAliased( const Other* alias ) const noexcept
638 {
639  return static_cast<const void*>( this ) == static_cast<const void*>( alias );
640 }
641 //*************************************************************************************************
642 
643 
644 
645 
646 //=================================================================================================
647 //
648 // INITIALIZERVECTOR OPERATORS
649 //
650 //=================================================================================================
651 
652 //*************************************************************************************************
655 template< typename Type, bool TF >
656 inline bool isIntact( const InitializerVector<Type,TF>& v ) noexcept;
657 
658 template< typename Type, bool TF >
659 inline void swap( InitializerVector<Type,TF>& a, InitializerVector<Type,TF>& b ) noexcept;
661 //*************************************************************************************************
662 
663 
664 //*************************************************************************************************
682 template< typename Type // Data type of the vector
683  , bool TF > // Transpose flag
684 inline bool isIntact( const InitializerVector<Type,TF>& v ) noexcept
685 {
686  UNUSED_PARAMETER( v );
687 
688  return true;
689 }
690 //*************************************************************************************************
691 
692 
693 //*************************************************************************************************
701 template< typename Type // Data type of the vector
702  , bool TF > // Transpose flag
704 {
705  a.swap( b );
706 }
707 //*************************************************************************************************
708 
709 
710 
711 
712 //=================================================================================================
713 //
714 // HASCONSTDATAACCESS SPECIALIZATIONS
715 //
716 //=================================================================================================
717 
718 //*************************************************************************************************
720 template< typename T, bool TF >
721 struct HasConstDataAccess< InitializerVector<T,TF> >
722  : public TrueType
723 {};
725 //*************************************************************************************************
726 
727 
728 
729 
730 //=================================================================================================
731 //
732 // ISINITIALIZER SPECIALIZATIONS
733 //
734 //=================================================================================================
735 
736 //*************************************************************************************************
738 template< typename T, bool TF >
739 struct IsInitializer< InitializerVector<T,TF> >
740  : public TrueType
741 {};
743 //*************************************************************************************************
744 
745 
746 
747 
748 //=================================================================================================
749 //
750 // HIGHTYPE SPECIALIZATIONS
751 //
752 //=================================================================================================
753 
754 //*************************************************************************************************
756 template< typename T1, bool TF, typename T2 >
757 struct HighType< InitializerVector<T1,TF>, InitializerVector<T2,TF> >
758 {
760 };
762 //*************************************************************************************************
763 
764 
765 
766 
767 //=================================================================================================
768 //
769 // LOWTYPE SPECIALIZATIONS
770 //
771 //=================================================================================================
772 
773 //*************************************************************************************************
775 template< typename T1, bool TF, typename T2 >
776 struct LowType< InitializerVector<T1,TF>, InitializerVector<T2,TF> >
777 {
779 };
781 //*************************************************************************************************
782 
783 
784 
785 
786 //=================================================================================================
787 //
788 // SUBVECTORTRAIT SPECIALIZATIONS
789 //
790 //=================================================================================================
791 
792 //*************************************************************************************************
794 template< typename T, bool TF, size_t I, size_t N >
795 struct SubvectorTrait< InitializerVector<T,TF>, I, N >
796 {
797  using Type = StaticVector<T,N,TF>;
798 };
799 
800 template< typename T, bool TF >
801 struct SubvectorTrait< InitializerVector<T,TF> >
802 {
803  using Type = DynamicVector<T,TF>;
804 };
806 //*************************************************************************************************
807 
808 
809 
810 
811 //=================================================================================================
812 //
813 // ELEMENTSTRAIT SPECIALIZATIONS
814 //
815 //=================================================================================================
816 
817 //*************************************************************************************************
819 template< typename T, bool TF, size_t... CEAs >
820 struct ElementsTrait< InitializerVector<T,TF>, CEAs... >
821 {
822  using Type = StaticVector<T,sizeof...(CEAs),TF>;
823 };
824 
825 template< typename T, bool TF >
826 struct ElementsTrait< InitializerVector<T,TF> >
827 {
828  using Type = DynamicVector<T,TF>;
829 };
831 //*************************************************************************************************
832 
833 } // namespace blaze
834 
835 #endif
#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
ConstReference at(size_t index) const
Checked access to the vector elements.
Definition: InitializerVector.h:414
Compile time check for low-level access to constant data.This type trait tests whether the given data...
Definition: HasConstDataAccess.h:75
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
InitializerIterator< Type > ConstIterator
Iterator over constant elements.
Definition: InitializerVector.h:195
ListType list_
The initializer list represented by the vector.
Definition: InitializerVector.h:292
Generic wrapper for a compile time constant integral value.The IntegralConstant class template repres...
Definition: IntegralConstant.h:71
const Type * ConstPointer
Pointer to a constant vector value.
Definition: InitializerVector.h:192
size_t size_
The current size/dimension of the vector.
Definition: InitializerVector.h:291
Dense vector representation of an initializer list.The InitializerVector class template is a dense ve...
Definition: InitializerVector.h:176
Header file for the DenseVector base class.
#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
ConstIterator end() const noexcept
Returns an iterator just past the last element of the initializer vector.
Definition: InitializerVector.h:479
Efficient implementation of an arbitrary sized vector.The DynamicVector class template is the represe...
Definition: DynamicVector.h:185
InitializerVector(initializer_list< Type > list) noexcept
Constructor for InitializerVector.
Definition: InitializerVector.h:343
Header file for the extended initializer_list functionality.
Header file for the elements trait.
Constraint on the data type.
Efficient implementation of a fixed-sized vector.The StaticVector class template is the representatio...
Definition: Forward.h:61
Base template for the SubvectorTrait class.
Definition: SubvectorTrait.h:109
Header file for the LowType type trait.
Base template for the HighType type trait.
Definition: HighType.h:133
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:5908
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
size_t capacity() const noexcept
Returns the maximum capacity of the vector.
Definition: InitializerVector.h:547
ConstIterator cbegin() const noexcept
Returns an iterator to the first element of the initializer vector.
Definition: InitializerVector.h:464
Header file for the subvector trait.
Constraint on the data type.
Base class for N-dimensional dense vectors.The DenseVector class is a base class for all arbitrarily ...
Definition: DenseVector.h:76
const Type & Reference
Reference to a non-constant vector value.
Definition: InitializerVector.h:189
ConstIterator cend() const noexcept
Returns an iterator just past the last element of the initializer vector.
Definition: InitializerVector.h:494
size_t nonZeros() const
Returns the number of non-zero elements in the vector.
Definition: InitializerVector.h:564
Header file for the exception macros of the math module.
bool isAliased(const Other *alias) const noexcept
Returns whether the vector is aliased with the given address alias.
Definition: InitializerVector.h:637
Base template for the ElementsTrait class.
Definition: ElementsTrait.h:108
const Type & ReturnType
Return type for expression template evaluations.
Definition: InitializerVector.h:186
Type ElementType
Type of the vector elements.
Definition: InitializerVector.h:185
Compile time check for custom data types.This type trait tests whether the given data type represents...
Definition: IsInitializer.h:82
Base template for the LowType type trait.
Definition: LowType.h:133
Header file for the HasConstDataAccess type trait.
ConstIterator begin() const noexcept
Returns an iterator to the first element of the initializer vector.
Definition: InitializerVector.h:449
const Type & ConstReference
Reference to a constant vector value.
Definition: InitializerVector.h:190
size_t spacing() const noexcept
Returns the minimum capacity of the vector.
Definition: InitializerVector.h:533
Header file for run time assertion macros.
Constraint on the data type.
Rebind mechanism to obtain a InitializerVector with different data/element type.
Definition: InitializerVector.h:202
Resize mechanism to obtain a InitializerVector with a different fixed number of elements.
Definition: InitializerVector.h:211
#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
Header file for the isDefault shim.
Constraint on the data type.
void swap(InitializerVector &v) noexcept
Swapping the contents of two vectors.
Definition: InitializerVector.h:586
const Type * Pointer
Pointer to a non-constant vector value.
Definition: InitializerVector.h:191
static const Type zero_
Neutral element for accesses to zero elements.
Definition: InitializerVector.h:299
Header file for the default transpose flag for all vectors of the Blaze library.
ConstReference operator[](size_t index) const noexcept
Subscript operator for the direct access to the vector elements.
Definition: InitializerVector.h:390
size_t size() const noexcept
Returns the current size/dimension of the vector.
Definition: InitializerVector.h:516
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:254
bool canAlias(const Other *alias) const noexcept
Returns whether the vector can alias with the given address alias.
Definition: InitializerVector.h:617
bool isDefault(const DiagonalProxy< MT > &proxy)
Returns whether the represented element is in default state.
Definition: DiagonalProxy.h:628
void UNUSED_PARAMETER(const Args &...)
Suppression of unused parameter warnings.
Definition: Unused.h:81
ConstPointer data() const noexcept
Low-level data access to the vector elements.
Definition: InitializerVector.h:434
Header file for the HighType type trait.
Header file for the TrueType type/value trait base class.