Blaze 3.9
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>
48#include <blaze/math/Forward.h>
55#include <blaze/util/Assert.h>
62#include <blaze/util/Types.h>
63
64
65namespace blaze {
66
67//=================================================================================================
68//
69// CLASS DEFINITION
70//
71//=================================================================================================
72
73//*************************************************************************************************
177template< typename Type // Data type of the vector
178 , bool TF // Transpose flag
179 , typename Tag > // Type tag
181 : public DenseVector< InitializerVector<Type,TF,Tag>, TF >
182{
183 public:
184 //**Type definitions****************************************************************************
187
190
193
194 using ElementType = Type;
195 using TagType = Tag;
196 using ReturnType = const Type&;
198
199 using Reference = const Type&;
200 using ConstReference = const Type&;
201 using Pointer = const Type*;
202 using ConstPointer = const Type*;
203
206 //**********************************************************************************************
207
208 //**Rebind struct definition********************************************************************
211 template< typename NewType > // Data type of the other vector
212 struct Rebind {
214 };
215 //**********************************************************************************************
216
217 //**Resize struct definition********************************************************************
220 template< size_t NewN > // Number of elements of the other vector
221 struct Resize {
223 };
224 //**********************************************************************************************
225
226 //**Compilation flags***************************************************************************
228
232 static constexpr bool simdEnabled = false;
233
235
238 static constexpr bool smpAssignable = false;
239 //**********************************************************************************************
240
241 //**Constructors********************************************************************************
244 inline InitializerVector( initializer_list<Type> list ) noexcept;
245 inline InitializerVector( initializer_list<Type> list, size_t n );
246
247 InitializerVector( const InitializerVector& ) = default;
249 //**********************************************************************************************
250
251 //**Destructor**********************************************************************************
254 ~InitializerVector() = default;
256 //**********************************************************************************************
257
258 //**Data access functions***********************************************************************
261 inline ConstReference operator[]( size_t index ) const noexcept;
262 inline ConstReference at( size_t index ) const;
263 inline ConstPointer data () const noexcept;
264 inline ConstIterator begin () const noexcept;
265 inline ConstIterator cbegin() const noexcept;
266 inline ConstIterator end () const noexcept;
267 inline ConstIterator cend () const noexcept;
269 //**********************************************************************************************
270
271 //**Assignment operators************************************************************************
274 InitializerVector& operator=( const InitializerVector& ) = delete;
276 //**********************************************************************************************
277
278 //**Utility functions***************************************************************************
281 inline size_t size() const noexcept;
282 inline size_t spacing() const noexcept;
283 inline size_t capacity() const noexcept;
284 inline size_t nonZeros() const;
285 inline void swap( InitializerVector& v ) noexcept;
287 //**********************************************************************************************
288
289 //**Expression template evaluation functions****************************************************
292 template< typename Other > inline bool canAlias ( const Other* alias ) const noexcept;
293 template< typename Other > inline bool isAliased( const Other* alias ) const noexcept;
295 //**********************************************************************************************
296
297 private:
298 //**Type definitions****************************************************************************
299 using ListType = initializer_list<Type>;
300 //**********************************************************************************************
301
302 //**Member variables****************************************************************************
305 size_t size_;
313 static const Type zero_;
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
337template< typename Type // Data type of the vector
338 , bool TF // Transpose flag
339 , typename Tag > // Type tag
340const Type InitializerVector<Type,TF,Tag>::zero_{};
341
342
343
344
345//=================================================================================================
346//
347// CONSTRUCTORS
348//
349//=================================================================================================
350
351//*************************************************************************************************
356template< typename Type // Data type of the vector
357 , bool TF // Transpose flag
358 , typename Tag > // Type tag
359inline InitializerVector<Type,TF,Tag>::InitializerVector( initializer_list<Type> list ) noexcept
360 : size_( list.size() ) // The current size/dimension of the vector
361 , list_( list ) // The initializer list represented by the vector
362{}
363//*************************************************************************************************
364
365
366//*************************************************************************************************
373template< typename Type // Data type of the vector
374 , bool TF // Transpose flag
375 , typename Tag > // Type tag
376inline InitializerVector<Type,TF,Tag>::InitializerVector( initializer_list<Type> list, size_t n )
377 : size_( n ) // The current size/dimension of the vector
378 , list_( list ) // The initializer list represented by the vector
379{
380 if( n < list.size() ) {
381 BLAZE_THROW_INVALID_ARGUMENT( "Invalid initializer list dimension" );
382 }
383}
384//*************************************************************************************************
385
386
387
388
389//=================================================================================================
390//
391// DATA ACCESS FUNCTIONS
392//
393//=================================================================================================
394
395//*************************************************************************************************
404template< typename Type // Data type of the vector
405 , bool TF // Transpose flag
406 , typename Tag > // Type tag
408 InitializerVector<Type,TF,Tag>::operator[]( size_t index ) const noexcept
409{
410 BLAZE_USER_ASSERT( index < size_, "Invalid vector access index" );
411 if( index < list_.size() )
412 return list_.begin()[index];
413 else
414 return zero_;
415}
416//*************************************************************************************************
417
418
419//*************************************************************************************************
429template< typename Type // Data type of the vector
430 , bool TF // Transpose flag
431 , typename Tag > // Type tag
434{
435 if( index >= size_ ) {
436 BLAZE_THROW_OUT_OF_RANGE( "Invalid vector access index" );
437 }
438 return (*this)[index];
439}
440//*************************************************************************************************
441
442
443//*************************************************************************************************
450template< typename Type // Data type of the vector
451 , bool TF // Transpose flag
452 , typename Tag > // Type tag
455{
456 return list_.begin();
457}
458//*************************************************************************************************
459
460
461//*************************************************************************************************
466template< typename Type // Data type of the vector
467 , bool TF // Transpose flag
468 , typename Tag > // Type tag
471{
472 return ConstIterator( 0UL, list_ );;
473}
474//*************************************************************************************************
475
476
477//*************************************************************************************************
482template< typename Type // Data type of the vector
483 , bool TF // Transpose flag
484 , typename Tag > // Type tag
487{
488 return ConstIterator( 0UL, list_ );
489}
490//*************************************************************************************************
491
492
493//*************************************************************************************************
498template< typename Type // Data type of the vector
499 , bool TF // Transpose flag
500 , typename Tag > // Type tag
503{
504 return ConstIterator( size_, list_ );
505}
506//*************************************************************************************************
507
508
509//*************************************************************************************************
514template< typename Type // Data type of the vector
515 , bool TF // Transpose flag
516 , typename Tag > // Type tag
519{
520 return ConstIterator( size_, list_ );
521}
522//*************************************************************************************************
523
524
525
526
527//=================================================================================================
528//
529// UTILITY FUNCTIONS
530//
531//=================================================================================================
532
533//*************************************************************************************************
538template< typename Type // Data type of the vector
539 , bool TF // Transpose flag
540 , typename Tag > // Type tag
541inline size_t InitializerVector<Type,TF,Tag>::size() const noexcept
542{
543 return size_;
544}
545//*************************************************************************************************
546
547
548//*************************************************************************************************
556template< typename Type // Data type of the vector
557 , bool TF // Transpose flag
558 , typename Tag > // Type tag
559inline size_t InitializerVector<Type,TF,Tag>::spacing() const noexcept
560{
561 return size_;
562}
563//*************************************************************************************************
564
565
566//*************************************************************************************************
571template< typename Type // Data type of the vector
572 , bool TF // Transpose flag
573 , typename Tag > // Type tag
574inline size_t InitializerVector<Type,TF,Tag>::capacity() const noexcept
575{
576 return size_;
577}
578//*************************************************************************************************
579
580
581//*************************************************************************************************
590template< typename Type // Data type of the vector
591 , bool TF // Transpose flag
592 , typename Tag > // Type tag
594{
595 size_t nonzeros( 0 );
596
597 for( size_t i=0UL; i<list_.size(); ++i ) {
598 if( !isDefault<strict>( list_.begin()[i] ) )
599 ++nonzeros;
600 }
601
602 return nonzeros;
603}
604//*************************************************************************************************
605
606
607//*************************************************************************************************
613template< typename Type // Data type of the vector
614 , bool TF // Transpose flag
615 , typename Tag > // Type tag
617{
618 using std::swap;
619
620 swap( size_, v.size_ );
621 swap( list_, v.list_ );
622}
623//*************************************************************************************************
624
625
626
627
628//=================================================================================================
629//
630// EXPRESSION TEMPLATE EVALUATION FUNCTIONS
631//
632//=================================================================================================
633
634//*************************************************************************************************
644template< typename Type // Data type of the vector
645 , bool TF // Transpose flag
646 , typename Tag > // Type tag
647template< typename Other > // Data type of the foreign expression
648inline bool InitializerVector<Type,TF,Tag>::canAlias( const Other* alias ) const noexcept
649{
650 return static_cast<const void*>( this ) == static_cast<const void*>( alias );
651}
652//*************************************************************************************************
653
654
655//*************************************************************************************************
665template< typename Type // Data type of the vector
666 , bool TF // Transpose flag
667 , typename Tag > // Type tag
668template< typename Other > // Data type of the foreign expression
669inline bool InitializerVector<Type,TF,Tag>::isAliased( const Other* alias ) const noexcept
670{
671 return static_cast<const void*>( this ) == static_cast<const void*>( alias );
672}
673//*************************************************************************************************
674
675
676
677
678//=================================================================================================
679//
680// INITIALIZERVECTOR OPERATORS
681//
682//=================================================================================================
683
684//*************************************************************************************************
687template< typename Type, bool TF, typename Tag >
688bool isIntact( const InitializerVector<Type,TF,Tag>& v ) noexcept;
689
690template< typename Type, bool TF, typename Tag >
693//*************************************************************************************************
694
695
696//*************************************************************************************************
714template< typename Type // Data type of the vector
715 , bool TF // Transpose flag
716 , typename Tag > // Type tag
717inline bool isIntact( const InitializerVector<Type,TF,Tag>& v ) noexcept
718{
719 MAYBE_UNUSED( v );
720
721 return true;
722}
723//*************************************************************************************************
724
725
726//*************************************************************************************************
734template< typename Type // Data type of the vector
735 , bool TF // Transpose flag
736 , typename Tag > // Type tag
738{
739 a.swap( b );
740}
741//*************************************************************************************************
742
743
744
745
746//=================================================================================================
747//
748// HASCONSTDATAACCESS SPECIALIZATIONS
749//
750//=================================================================================================
751
752//*************************************************************************************************
754template< typename T, bool TF, typename Tag >
755struct HasConstDataAccess< InitializerVector<T,TF,Tag> >
756 : public TrueType
757{};
759//*************************************************************************************************
760
761
762
763
764//=================================================================================================
765//
766// ISINITIALIZER SPECIALIZATIONS
767//
768//=================================================================================================
769
770//*************************************************************************************************
772template< typename T, bool TF, typename Tag >
773struct IsInitializer< InitializerVector<T,TF,Tag> >
774 : public TrueType
775{};
777//*************************************************************************************************
778
779
780
781
782//=================================================================================================
783//
784// HIGHTYPE SPECIALIZATIONS
785//
786//=================================================================================================
787
788//*************************************************************************************************
790template< typename T1, bool TF, typename Tag, typename T2 >
791struct HighType< InitializerVector<T1,TF,Tag>, InitializerVector<T2,TF,Tag> >
792{
793 using Type = InitializerVector< typename HighType<T1,T2>::Type, TF, Tag >;
794};
796//*************************************************************************************************
797
798
799
800
801//=================================================================================================
802//
803// LOWTYPE SPECIALIZATIONS
804//
805//=================================================================================================
806
807//*************************************************************************************************
809template< typename T1, bool TF, typename Tag, typename T2 >
810struct LowType< InitializerVector<T1,TF,Tag>, InitializerVector<T2,TF,Tag> >
811{
812 using Type = InitializerVector< typename LowType<T1,T2>::Type, TF, Tag >;
813};
815//*************************************************************************************************
816
817} // namespace blaze
818
819#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 isDefault shim.
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 N-dimensional dense vectors.
Definition: DenseVector.h:77
Efficient implementation of an arbitrary sized vector.
Definition: DynamicVector.h:223
Implementation of an iterator for (extended) initializer lists.
Definition: InitializerIterator.h:57
Dense vector representation of an initializer list.
Definition: InitializerVector.h:182
const Type & Reference
Reference to a non-constant vector value.
Definition: InitializerVector.h:199
static constexpr bool smpAssignable
Compilation flag for SMP assignments.
Definition: InitializerVector.h:238
ConstIterator cbegin() const noexcept
Returns an iterator to the first element of the initializer vector.
Definition: InitializerVector.h:486
initializer_list< Type > ListType
Type of the represented initializer list.
Definition: InitializerVector.h:299
const Type & ReturnType
Return type for expression template evaluations.
Definition: InitializerVector.h:196
const Type & ConstReference
Reference to a constant vector value.
Definition: InitializerVector.h:200
size_t capacity() const noexcept
Returns the maximum capacity of the vector.
Definition: InitializerVector.h:574
Tag TagType
Tag type of this InitializerVector instance.
Definition: InitializerVector.h:195
InitializerVector(initializer_list< Type > list) noexcept
Constructor for InitializerVector.
Definition: InitializerVector.h:359
ConstReference operator[](size_t index) const noexcept
Subscript operator for the direct access to the vector elements.
Definition: InitializerVector.h:408
ConstIterator cend() const noexcept
Returns an iterator just past the last element of the initializer vector.
Definition: InitializerVector.h:518
const Type * Pointer
Pointer to a non-constant vector value.
Definition: InitializerVector.h:201
static const Type zero_
Neutral element for accesses to zero elements.
Definition: InitializerVector.h:313
bool canAlias(const Other *alias) const noexcept
Returns whether the vector can alias with the given address alias.
Definition: InitializerVector.h:648
ConstReference at(size_t index) const
Checked access to the vector elements.
Definition: InitializerVector.h:433
size_t spacing() const noexcept
Returns the minimum capacity of the vector.
Definition: InitializerVector.h:559
const Type * ConstPointer
Pointer to a constant vector value.
Definition: InitializerVector.h:202
size_t nonZeros() const
Returns the number of non-zero elements in the vector.
Definition: InitializerVector.h:593
ConstPointer data() const noexcept
Low-level data access to the vector elements.
Definition: InitializerVector.h:454
Type ElementType
Type of the vector elements.
Definition: InitializerVector.h:194
void swap(InitializerVector &v) noexcept
Swapping the contents of two vectors.
Definition: InitializerVector.h:616
size_t size_
The current size/dimension of the vector.
Definition: InitializerVector.h:305
bool isAliased(const Other *alias) const noexcept
Returns whether the vector is aliased with the given address alias.
Definition: InitializerVector.h:669
static constexpr bool simdEnabled
Compilation flag for SIMD optimization.
Definition: InitializerVector.h:232
size_t size() const noexcept
Returns the current size/dimension of the vector.
Definition: InitializerVector.h:541
ConstIterator begin() const noexcept
Returns an iterator to the first element of the initializer vector.
Definition: InitializerVector.h:470
ConstIterator end() const noexcept
Returns an iterator just past the last element of the initializer vector.
Definition: InitializerVector.h:502
ListType list_
The initializer list represented by the vector.
Definition: InitializerVector.h:306
Header file for the DenseVector 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(InitializerVector< Type, TF, Tag > &a, InitializerVector< Type, TF, Tag > &b) noexcept
Swapping the contents of two vectors.
Definition: InitializerVector.h:737
bool isIntact(const InitializerVector< Type, TF, Tag > &v) noexcept
Returns whether the invariants of the given initializer vector are intact.
Definition: InitializerVector.h:717
#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 InitializerVector with different data/element type.
Definition: InitializerVector.h:212
Resize mechanism to obtain a InitializerVector with a different fixed number of elements.
Definition: InitializerVector.h:221
Header file for basic type definitions.