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>
56 #include <blaze/util/Assert.h>
61 #include <blaze/util/TrueType.h>
62 #include <blaze/util/Types.h>
63 #include <blaze/util/Unused.h>
64 
65 
66 namespace blaze {
67 
68 //=================================================================================================
69 //
70 // CLASS DEFINITION
71 //
72 //=================================================================================================
73 
74 //*************************************************************************************************
172 template< typename Type // Data type of the vector
173  , bool TF = defaultTransposeFlag > // Transpose flag
175  : public DenseVector< InitializerVector<Type,TF>, TF >
176 {
177  public:
178  //**Type definitions****************************************************************************
183  using ElementType = Type;
184  using ReturnType = const Type&;
186 
187  using Reference = const Type&;
188  using ConstReference = const Type&;
189  using Pointer = const Type*;
190  using ConstPointer = const Type*;
191 
194  //**********************************************************************************************
195 
196  //**Rebind struct definition********************************************************************
199  template< typename NewType > // Data type of the other vector
200  struct Rebind {
202  };
203  //**********************************************************************************************
204 
205  //**Resize struct definition********************************************************************
208  template< size_t NewN > // Number of elements of the other vector
209  struct Resize {
211  };
212  //**********************************************************************************************
213 
214  //**Compilation flags***************************************************************************
216 
220  static constexpr bool simdEnabled = false;
221 
223 
226  static constexpr bool smpAssignable = false;
227  //**********************************************************************************************
228 
229  //**Constructors********************************************************************************
232  explicit inline InitializerVector( initializer_list<Type> list ) noexcept;
233  explicit inline InitializerVector( initializer_list<Type> list, size_t n );
234 
235  InitializerVector( const InitializerVector& ) = default;
237  //**********************************************************************************************
238 
239  //**Destructor**********************************************************************************
242  ~InitializerVector() = default;
244  //**********************************************************************************************
245 
246  //**Data access functions***********************************************************************
249  inline ConstReference operator[]( size_t index ) const noexcept;
250  inline ConstReference at( size_t index ) const;
251  inline ConstPointer data () const noexcept;
252  inline ConstIterator begin () const noexcept;
253  inline ConstIterator cbegin() const noexcept;
254  inline ConstIterator end () const noexcept;
255  inline ConstIterator cend () const noexcept;
257  //**********************************************************************************************
258 
259  //**Assignment operators************************************************************************
262  InitializerVector& operator=( const InitializerVector& ) = delete;
264  //**********************************************************************************************
265 
266  //**Utility functions***************************************************************************
269  inline size_t size() const noexcept;
270  inline size_t spacing() const noexcept;
271  inline size_t capacity() const noexcept;
272  inline size_t nonZeros() const;
273  inline void swap( InitializerVector& v ) noexcept;
275  //**********************************************************************************************
276 
277  //**Expression template evaluation functions****************************************************
280  template< typename Other > inline bool canAlias ( const Other* alias ) const noexcept;
281  template< typename Other > inline bool isAliased( const Other* alias ) const noexcept;
283  //**********************************************************************************************
284 
285  private:
286  //**Type definitions****************************************************************************
287  using ListType = initializer_list<Type>;
288  //**********************************************************************************************
289 
290  //**Member variables****************************************************************************
293  size_t size_;
295 
301  static const Type zero_;
302 
303  //**********************************************************************************************
304 
305  //**Compile time checks*************************************************************************
312  //**********************************************************************************************
313 };
314 //*************************************************************************************************
315 
316 
317 
318 
319 //=================================================================================================
320 //
321 // DEFINITION AND INITIALIZATION OF THE STATIC MEMBER VARIABLES
322 //
323 //=================================================================================================
324 
325 template< typename Type // Data type of the vector
326  , bool TF > // Transpose flag
327 const Type InitializerVector<Type,TF>::zero_{};
328 
329 
330 
331 
332 //=================================================================================================
333 //
334 // CONSTRUCTORS
335 //
336 //=================================================================================================
337 
338 //*************************************************************************************************
343 template< typename Type // Data type of the vector
344  , bool TF > // Transpose flag
346  : size_( list.size() ) // The current size/dimension of the vector
347  , list_( list ) // The initializer list represented by the vector
348 {}
349 //*************************************************************************************************
350 
351 
352 //*************************************************************************************************
359 template< typename Type // Data type of the vector
360  , bool TF > // Transpose flag
362  : size_( n ) // The current size/dimension of the vector
363  , list_( list ) // The initializer list represented by the vector
364 {
365  if( n < list.size() ) {
366  BLAZE_THROW_INVALID_ARGUMENT( "Invalid initializer list dimension" );
367  }
368 }
369 //*************************************************************************************************
370 
371 
372 
373 
374 //=================================================================================================
375 //
376 // DATA ACCESS FUNCTIONS
377 //
378 //=================================================================================================
379 
380 //*************************************************************************************************
389 template< typename Type // Data type of the vector
390  , bool TF > // Transpose flag
392  InitializerVector<Type,TF>::operator[]( size_t index ) const noexcept
393 {
394  BLAZE_USER_ASSERT( index < size_, "Invalid vector access index" );
395  if( index < list_.size() )
396  return list_.begin()[index];
397  else
398  return zero_;
399 }
400 //*************************************************************************************************
401 
402 
403 //*************************************************************************************************
413 template< typename Type // Data type of the vector
414  , bool TF > // Transpose flag
416  InitializerVector<Type,TF>::at( size_t index ) const
417 {
418  if( index >= size_ ) {
419  BLAZE_THROW_OUT_OF_RANGE( "Invalid vector access index" );
420  }
421  return (*this)[index];
422 }
423 //*************************************************************************************************
424 
425 
426 //*************************************************************************************************
433 template< typename Type // Data type of the vector
434  , bool TF > // Transpose flag
437 {
438  return list_.begin();
439 }
440 //*************************************************************************************************
441 
442 
443 //*************************************************************************************************
448 template< typename Type // Data type of the vector
449  , bool TF > // Transpose flag
452 {
453  return ConstIterator( 0UL, list_ );;
454 }
455 //*************************************************************************************************
456 
457 
458 //*************************************************************************************************
463 template< typename Type // Data type of the vector
464  , bool TF > // Transpose flag
467 {
468  return ConstIterator( 0UL, list_ );
469 }
470 //*************************************************************************************************
471 
472 
473 //*************************************************************************************************
478 template< typename Type // Data type of the vector
479  , bool TF > // Transpose flag
482 {
483  return ConstIterator( size_, list_ );
484 }
485 //*************************************************************************************************
486 
487 
488 //*************************************************************************************************
493 template< typename Type // Data type of the vector
494  , bool TF > // Transpose flag
497 {
498  return ConstIterator( size_, list_ );
499 }
500 //*************************************************************************************************
501 
502 
503 
504 
505 //=================================================================================================
506 //
507 // UTILITY FUNCTIONS
508 //
509 //=================================================================================================
510 
511 //*************************************************************************************************
516 template< typename Type // Data type of the vector
517  , bool TF > // Transpose flag
518 inline size_t InitializerVector<Type,TF>::size() const noexcept
519 {
520  return size_;
521 }
522 //*************************************************************************************************
523 
524 
525 //*************************************************************************************************
533 template< typename Type // Data type of the vector
534  , bool TF > // Transpose flag
535 inline size_t InitializerVector<Type,TF>::spacing() const noexcept
536 {
537  return size_;
538 }
539 //*************************************************************************************************
540 
541 
542 //*************************************************************************************************
547 template< typename Type // Data type of the vector
548  , bool TF > // Transpose flag
549 inline size_t InitializerVector<Type,TF>::capacity() const noexcept
550 {
551  return size_;
552 }
553 //*************************************************************************************************
554 
555 
556 //*************************************************************************************************
564 template< typename Type // Data type of the vector
565  , bool TF > // Transpose flag
567 {
568  size_t nonzeros( 0 );
569 
570  for( size_t i=0UL; i<list_.size(); ++i ) {
571  if( !isDefault( list_.begin()[i] ) )
572  ++nonzeros;
573  }
574 
575  return nonzeros;
576 }
577 //*************************************************************************************************
578 
579 
580 //*************************************************************************************************
586 template< typename Type // Data type of the vector
587  , bool TF > // Transpose flag
589 {
590  using std::swap;
591 
592  swap( size_, v.size_ );
593  swap( list_, v.list_ );
594 }
595 //*************************************************************************************************
596 
597 
598 
599 
600 //=================================================================================================
601 //
602 // EXPRESSION TEMPLATE EVALUATION FUNCTIONS
603 //
604 //=================================================================================================
605 
606 //*************************************************************************************************
616 template< typename Type // Data type of the vector
617  , bool TF > // Transpose flag
618 template< typename Other > // Data type of the foreign expression
619 inline bool InitializerVector<Type,TF>::canAlias( const Other* alias ) const noexcept
620 {
621  return static_cast<const void*>( this ) == static_cast<const void*>( alias );
622 }
623 //*************************************************************************************************
624 
625 
626 //*************************************************************************************************
636 template< typename Type // Data type of the vector
637  , bool TF > // Transpose flag
638 template< typename Other > // Data type of the foreign expression
639 inline bool InitializerVector<Type,TF>::isAliased( const Other* alias ) const noexcept
640 {
641  return static_cast<const void*>( this ) == static_cast<const void*>( alias );
642 }
643 //*************************************************************************************************
644 
645 
646 
647 
648 //=================================================================================================
649 //
650 // INITIALIZERVECTOR OPERATORS
651 //
652 //=================================================================================================
653 
654 //*************************************************************************************************
657 template< typename Type, bool TF >
658 bool isIntact( const InitializerVector<Type,TF>& v ) noexcept;
659 
660 template< typename Type, bool TF >
663 //*************************************************************************************************
664 
665 
666 //*************************************************************************************************
684 template< typename Type // Data type of the vector
685  , bool TF > // Transpose flag
686 inline bool isIntact( const InitializerVector<Type,TF>& v ) noexcept
687 {
688  UNUSED_PARAMETER( v );
689 
690  return true;
691 }
692 //*************************************************************************************************
693 
694 
695 //*************************************************************************************************
703 template< typename Type // Data type of the vector
704  , bool TF > // Transpose flag
706 {
707  a.swap( b );
708 }
709 //*************************************************************************************************
710 
711 
712 
713 
714 //=================================================================================================
715 //
716 // HASCONSTDATAACCESS SPECIALIZATIONS
717 //
718 //=================================================================================================
719 
720 //*************************************************************************************************
722 template< typename T, bool TF >
723 struct HasConstDataAccess< InitializerVector<T,TF> >
724  : public TrueType
725 {};
727 //*************************************************************************************************
728 
729 
730 
731 
732 //=================================================================================================
733 //
734 // ISINITIALIZER SPECIALIZATIONS
735 //
736 //=================================================================================================
737 
738 //*************************************************************************************************
740 template< typename T, bool TF >
741 struct IsInitializer< InitializerVector<T,TF> >
742  : public TrueType
743 {};
745 //*************************************************************************************************
746 
747 
748 
749 
750 //=================================================================================================
751 //
752 // HIGHTYPE SPECIALIZATIONS
753 //
754 //=================================================================================================
755 
756 //*************************************************************************************************
758 template< typename T1, bool TF, typename T2 >
759 struct HighType< InitializerVector<T1,TF>, InitializerVector<T2,TF> >
760 {
761  using Type = InitializerVector< typename HighType<T1,T2>::Type, TF >;
762 };
764 //*************************************************************************************************
765 
766 
767 
768 
769 //=================================================================================================
770 //
771 // LOWTYPE SPECIALIZATIONS
772 //
773 //=================================================================================================
774 
775 //*************************************************************************************************
777 template< typename T1, bool TF, typename T2 >
778 struct LowType< InitializerVector<T1,TF>, InitializerVector<T2,TF> >
779 {
780  using Type = InitializerVector< typename LowType<T1,T2>::Type, TF >;
781 };
783 //*************************************************************************************************
784 
785 } // namespace blaze
786 
787 #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:416
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
ListType list_
The initializer list represented by the vector.
Definition: InitializerVector.h:294
const Type * ConstPointer
Pointer to a constant vector value.
Definition: InitializerVector.h:190
size_t size_
The current size/dimension of the vector.
Definition: InitializerVector.h:293
Dense vector representation of an initializer list.The InitializerVector class template is a dense ve...
Definition: InitializerVector.h:174
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:481
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
static constexpr bool simdEnabled
Compilation flag for SIMD optimization.
Definition: InitializerVector.h:220
Efficient implementation of an arbitrary sized vector.The DynamicVector class template is the represe...
Definition: DynamicVector.h:197
InitializerVector(initializer_list< Type > list) noexcept
Constructor for InitializerVector.
Definition: InitializerVector.h:345
Header file for the extended initializer_list functionality.
constexpr void UNUSED_PARAMETER(const Args &...)
Suppression of unused parameter warnings.
Definition: Unused.h:81
Constraint on the data type.
Header file for the LowType type trait.
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
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:549
ConstIterator cbegin() const noexcept
Returns an iterator to the first element of the initializer vector.
Definition: InitializerVector.h:466
const Element * ConstIterator
Iterator over constant elements.
Definition: CompressedMatrix.h:3086
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:187
ConstIterator cend() const noexcept
Returns an iterator just past the last element of the initializer vector.
Definition: InitializerVector.h:496
size_t nonZeros() const
Returns the number of non-zero elements in the vector.
Definition: InitializerVector.h:566
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:639
const Type & ReturnType
Return type for expression template evaluations.
Definition: InitializerVector.h:184
Type ElementType
Type of the vector elements.
Definition: InitializerVector.h:183
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:451
const Type & ConstReference
Reference to a constant vector value.
Definition: InitializerVector.h:188
size_t spacing() const noexcept
Returns the minimum capacity of the vector.
Definition: InitializerVector.h:535
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:200
Resize mechanism to obtain a InitializerVector with a different fixed number of elements.
Definition: InitializerVector.h:209
#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.
void swap(DiagonalMatrix< MT, SO, DF > &a, DiagonalMatrix< MT, SO, DF > &b) noexcept
Swapping the contents of two matrices.
Definition: DiagonalMatrix.h:281
static constexpr bool smpAssignable
Compilation flag for SMP assignments.
Definition: InitializerVector.h:226
Constraint on the data type.
void swap(InitializerVector &v) noexcept
Swapping the contents of two vectors.
Definition: InitializerVector.h:588
const Type * Pointer
Pointer to a non-constant vector value.
Definition: InitializerVector.h:189
static const Type zero_
Neutral element for accesses to zero elements.
Definition: InitializerVector.h:301
Header file for the default transpose flag for all vectors of the Blaze library.
static const Type zero_
Neutral element for accesses to zero elements.
Definition: CompressedMatrix.h:3295
ConstReference operator[](size_t index) const noexcept
Subscript operator for the direct access to the vector elements.
Definition: InitializerVector.h:392
size_t size() const noexcept
Returns the current size/dimension of the vector.
Definition: InitializerVector.h:518
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 canAlias(const Other *alias) const noexcept
Returns whether the vector can alias with the given address alias.
Definition: InitializerVector.h:619
bool isDefault(const DiagonalProxy< MT > &proxy)
Returns whether the represented element is in default state.
Definition: DiagonalProxy.h:631
ConstPointer data() const noexcept
Low-level data access to the vector elements.
Definition: InitializerVector.h:436
Header file for the HighType type trait.
Header file for the TrueType type/value trait base class.