Vector.h
Go to the documentation of this file.
1 //=================================================================================================
33 //=================================================================================================
34 
35 #ifndef _BLAZE_MATH_EXPRESSIONS_VECTOR_H_
36 #define _BLAZE_MATH_EXPRESSIONS_VECTOR_H_
37 
38 
39 //*************************************************************************************************
40 // Includes
41 //*************************************************************************************************
42 
43 #include <blaze/math/Exception.h>
45 #include <blaze/system/Inline.h>
46 #include <blaze/util/Assert.h>
47 #include <blaze/util/DisableIf.h>
48 #include <blaze/util/EnableIf.h>
50 #include <blaze/util/Types.h>
52 #include <blaze/util/Unused.h>
53 
54 
55 namespace blaze {
56 
57 //=================================================================================================
58 //
59 // CLASS DEFINITION
60 //
61 //=================================================================================================
62 
63 //*************************************************************************************************
75 template< typename VT // Type of the vector
76  , bool TF > // Transpose flag
77 struct Vector
78 {
79  //**Type definitions****************************************************************************
80  typedef VT VectorType;
81  //**********************************************************************************************
82 
83  //**Non-const conversion operator***************************************************************
88  BLAZE_ALWAYS_INLINE VectorType& operator~() noexcept {
89  return *static_cast<VectorType*>( this );
90  }
91  //**********************************************************************************************
92 
93  //**Const conversion operators******************************************************************
98  BLAZE_ALWAYS_INLINE const VectorType& operator~() const noexcept {
99  return *static_cast<const VectorType*>( this );
100  }
101  //**********************************************************************************************
102 };
103 //*************************************************************************************************
104 
105 
106 
107 
108 //=================================================================================================
109 //
110 // GLOBAL FUNCTIONS
111 //
112 //=================================================================================================
113 
114 //*************************************************************************************************
117 template< typename VT, bool TF >
118 BLAZE_ALWAYS_INLINE typename VT::Iterator begin( Vector<VT,TF>& vector );
119 
120 template< typename VT, bool TF >
121 BLAZE_ALWAYS_INLINE typename VT::ConstIterator begin( const Vector<VT,TF>& vector );
122 
123 template< typename VT, bool TF >
124 BLAZE_ALWAYS_INLINE typename VT::ConstIterator cbegin( const Vector<VT,TF>& vector );
125 
126 template< typename VT, bool TF >
127 BLAZE_ALWAYS_INLINE typename VT::Iterator end( Vector<VT,TF>& vector );
128 
129 template< typename VT, bool TF >
130 BLAZE_ALWAYS_INLINE typename VT::ConstIterator end( const Vector<VT,TF>& vector );
131 
132 template< typename VT, bool TF >
133 BLAZE_ALWAYS_INLINE typename VT::ConstIterator cend( const Vector<VT,TF>& vector );
134 
135 template< typename VT, bool TF >
136 BLAZE_ALWAYS_INLINE size_t size( const Vector<VT,TF>& vector ) noexcept;
137 
138 template< typename VT, bool TF >
139 BLAZE_ALWAYS_INLINE size_t capacity( const Vector<VT,TF>& vector ) noexcept;
140 
141 template< typename VT, bool TF >
142 BLAZE_ALWAYS_INLINE size_t nonZeros( const Vector<VT,TF>& vector );
143 
144 template< typename VT, bool TF >
145 BLAZE_ALWAYS_INLINE void resize( Vector<VT,TF>& vector, size_t n, bool preserve=true );
146 
147 template< typename VT1, bool TF1, typename VT2, bool TF2 >
148 BLAZE_ALWAYS_INLINE bool isSame( const Vector<VT1,TF1>& a, const Vector<VT2,TF2>& b ) noexcept;
150 //*************************************************************************************************
151 
152 
153 //*************************************************************************************************
160 template< typename VT // Type of the vector
161  , bool TF > // Transpose flag of the vector
163 {
164  return (~vector).begin();
165 }
166 //*************************************************************************************************
167 
168 
169 //*************************************************************************************************
176 template< typename VT // Type of the vector
177  , bool TF > // Transpose flag of the vector
179 {
180  return (~vector).begin();
181 }
182 //*************************************************************************************************
183 
184 
185 //*************************************************************************************************
192 template< typename VT // Type of the vector
193  , bool TF > // Transpose flag of the vector
195 {
196  return (~vector).begin();
197 }
198 //*************************************************************************************************
199 
200 
201 //*************************************************************************************************
208 template< typename VT // Type of the vector
209  , bool TF > // Transpose flag of the vector
211 {
212  return (~vector).end();
213 }
214 //*************************************************************************************************
215 
216 
217 //*************************************************************************************************
224 template< typename VT // Type of the vector
225  , bool TF > // Transpose flag of the vector
227 {
228  return (~vector).end();
229 }
230 //*************************************************************************************************
231 
232 
233 //*************************************************************************************************
240 template< typename VT // Type of the vector
241  , bool TF > // Transpose flag of the vector
243 {
244  return (~vector).end();
245 }
246 //*************************************************************************************************
247 
248 
249 //*************************************************************************************************
256 template< typename VT // Type of the vector
257  , bool TF > // Transpose flag of the vector
258 BLAZE_ALWAYS_INLINE size_t size( const Vector<VT,TF>& vector ) noexcept
259 {
260  return (~vector).size();
261 }
262 //*************************************************************************************************
263 
264 
265 //*************************************************************************************************
272 template< typename VT // Type of the vector
273  , bool TF > // Transpose flag of the vector
274 BLAZE_ALWAYS_INLINE size_t capacity( const Vector<VT,TF>& vector ) noexcept
275 {
276  return (~vector).capacity();
277 }
278 //*************************************************************************************************
279 
280 
281 //*************************************************************************************************
291 template< typename VT // Type of the vector
292  , bool TF > // Transpose flag of the vector
294 {
295  return (~vector).nonZeros();
296 }
297 //*************************************************************************************************
298 
299 
300 //*************************************************************************************************
314 template< typename VT // Type of the vector
315  , bool TF > // Transpose flag of the vector
316 BLAZE_ALWAYS_INLINE DisableIf_< IsResizable<VT> >
317  resize_backend( Vector<VT,TF>& vector, size_t n, bool preserve )
318 {
319  UNUSED_PARAMETER( preserve );
320 
321  if( (~vector).size() != n ) {
322  BLAZE_THROW_INVALID_ARGUMENT( "Vector cannot be resized" );
323  }
324 }
326 //*************************************************************************************************
327 
328 
329 //*************************************************************************************************
341 template< typename VT // Type of the vector
342  , bool TF > // Transpose flag of the vector
343 BLAZE_ALWAYS_INLINE EnableIf_< IsResizable<VT> >
344  resize_backend( Vector<VT,TF>& vector, size_t n, bool preserve )
345 {
346  (~vector).resize( n, preserve );
347 }
349 //*************************************************************************************************
350 
351 
352 //*************************************************************************************************
380 template< typename VT // Type of the vector
381  , bool TF > // Transpose flag of the vector
382 BLAZE_ALWAYS_INLINE void resize( Vector<VT,TF>& vector, size_t n, bool preserve )
383 {
384  resize_backend( vector, n, preserve );
385 }
386 //*************************************************************************************************
387 
388 
389 //*************************************************************************************************
422 template< typename VT1 // Type of the left-hand side vector
423  , bool TF1 // Transpose flag of the left-hand side vector
424  , typename VT2 // Type of the right-hand side vector
425  , bool TF2 > // Transpose flag of the right-hand side vector
426 BLAZE_ALWAYS_INLINE bool isSame( const Vector<VT1,TF1>& a, const Vector<VT2,TF2>& b ) noexcept
427 {
428  return ( IsSame<VT1,VT2>::value &&
429  reinterpret_cast<const void*>( &a ) == reinterpret_cast<const void*>( &b ) );
430 }
431 //*************************************************************************************************
432 
433 
434 //*************************************************************************************************
449 template< typename VT1 // Type of the left-hand side vector
450  , bool TF1 // Transpose flag of the left-hand side vector
451  , typename VT2 // Type of the right-hand side vector
452  , bool TF2 > // Transpose flag of the right-hand side vector
453 BLAZE_ALWAYS_INLINE void assign( Vector<VT1,TF1>& lhs, const Vector<VT2,TF2>& rhs )
454 {
456 
457  BLAZE_INTERNAL_ASSERT( (~lhs).size() == (~rhs).size(), "Invalid vector sizes" );
458  (~lhs).assign( ~rhs );
459 }
461 //*************************************************************************************************
462 
463 
464 //*************************************************************************************************
479 template< typename VT1 // Type of the left-hand side vector
480  , bool TF1 // Transpose flag of the left-hand side vector
481  , typename VT2 // Type of the right-hand side vector
482  , bool TF2 > // Transpose flag of the right-hand side vector
483 BLAZE_ALWAYS_INLINE void addAssign( Vector<VT1,TF1>& lhs, const Vector<VT2,TF2>& rhs )
484 {
486 
487  BLAZE_INTERNAL_ASSERT( (~lhs).size() == (~rhs).size(), "Invalid vector sizes" );
488  (~lhs).addAssign( ~rhs );
489 }
491 //*************************************************************************************************
492 
493 
494 //*************************************************************************************************
509 template< typename VT1 // Type of the left-hand side vector
510  , bool TF1 // Transpose flag of the left-hand side vector
511  , typename VT2 // Type of the right-hand side vector
512  , bool TF2 > // Transpose flag of the right-hand side vector
513 BLAZE_ALWAYS_INLINE void subAssign( Vector<VT1,TF1>& lhs, const Vector<VT2,TF2>& rhs )
514 {
516 
517  BLAZE_INTERNAL_ASSERT( (~lhs).size() == (~rhs).size(), "Invalid vector sizes" );
518  (~lhs).subAssign( ~rhs );
519 }
521 //*************************************************************************************************
522 
523 
524 //*************************************************************************************************
539 template< typename VT1 // Type of the left-hand side vector
540  , bool TF1 // Transpose flag of the left-hand side vector
541  , typename VT2 // Type of the right-hand side vector
542  , bool TF2 > // Transpose flag of the right-hand side vector
543 BLAZE_ALWAYS_INLINE void multAssign( Vector<VT1,TF1>& lhs, const Vector<VT2,TF2>& rhs )
544 {
546 
547  BLAZE_INTERNAL_ASSERT( (~lhs).size() == (~rhs).size(), "Invalid vector sizes" );
548  (~lhs).multAssign( ~rhs );
549 }
551 //*************************************************************************************************
552 
553 
554 //*************************************************************************************************
569 template< typename VT1 // Type of the left-hand side vector
570  , bool TF1 // Transpose flag of the left-hand side vector
571  , typename VT2 // Type of the right-hand side vector
572  , bool TF2 > // Transpose flag of the right-hand side vector
573 BLAZE_ALWAYS_INLINE void divAssign( Vector<VT1,TF1>& lhs, const Vector<VT2,TF2>& rhs )
574 {
576 
577  BLAZE_INTERNAL_ASSERT( (~lhs).size() == (~rhs).size(), "Invalid vector sizes" );
578  (~lhs).divAssign( ~rhs );
579 }
581 //*************************************************************************************************
582 
583 
584 //*************************************************************************************************
599 template< typename VT1 // Type of the left-hand side vector
600  , bool TF1 // Transpose flag of the left-hand side vector
601  , typename VT2 // Type of the right-hand side vector
602  , bool TF2 > // Transpose flag of the right-hand side vector
603 BLAZE_ALWAYS_INLINE bool tryAssign( const Vector<VT1,TF1>& lhs, const Vector<VT2,TF2>& rhs, size_t index )
604 {
605  BLAZE_INTERNAL_ASSERT( index <= (~lhs).size(), "Invalid vector access index" );
606  BLAZE_INTERNAL_ASSERT( (~rhs).size() <= (~lhs).size() - index, "Invalid vector size" );
607 
608  UNUSED_PARAMETER( lhs, rhs, index );
609 
610  return true;
611 }
613 //*************************************************************************************************
614 
615 
616 //*************************************************************************************************
631 template< typename VT1 // Type of the left-hand side vector
632  , bool TF1 // Transpose flag of the left-hand side vector
633  , typename VT2 // Type of the right-hand side vector
634  , bool TF2 > // Transpose flag of the right-hand side vector
635 BLAZE_ALWAYS_INLINE bool tryAddAssign( const Vector<VT1,TF1>& lhs, const Vector<VT2,TF2>& rhs, size_t index )
636 {
637  BLAZE_INTERNAL_ASSERT( index <= (~lhs).size(), "Invalid vector access index" );
638  BLAZE_INTERNAL_ASSERT( (~rhs).size() <= (~lhs).size() - index, "Invalid vector size" );
639 
640  UNUSED_PARAMETER( lhs, rhs, index );
641 
642  return true;
643 }
645 //*************************************************************************************************
646 
647 
648 //*************************************************************************************************
663 template< typename VT1 // Type of the left-hand side vector
664  , bool TF1 // Transpose flag of the left-hand side vector
665  , typename VT2 // Type of the right-hand side vector
666  , bool TF2 > // Transpose flag of the right-hand side vector
667 BLAZE_ALWAYS_INLINE bool trySubAssign( const Vector<VT1,TF1>& lhs, const Vector<VT2,TF2>& rhs, size_t index )
668 {
669  BLAZE_INTERNAL_ASSERT( index <= (~lhs).size(), "Invalid vector access index" );
670  BLAZE_INTERNAL_ASSERT( (~rhs).size() <= (~lhs).size() - index, "Invalid vector size" );
671 
672  UNUSED_PARAMETER( lhs, rhs, index );
673 
674  return true;
675 }
677 //*************************************************************************************************
678 
679 
680 //*************************************************************************************************
695 template< typename VT1 // Type of the left-hand side vector
696  , bool TF1 // Transpose flag of the left-hand side vector
697  , typename VT2 // Type of the right-hand side vector
698  , bool TF2 > // Transpose flag of the right-hand side vector
699 BLAZE_ALWAYS_INLINE bool tryMultAssign( const Vector<VT1,TF1>& lhs, const Vector<VT2,TF2>& rhs, size_t index )
700 {
701  BLAZE_INTERNAL_ASSERT( index <= (~lhs).size(), "Invalid vector access index" );
702  BLAZE_INTERNAL_ASSERT( (~rhs).size() <= (~lhs).size() - index, "Invalid vector size" );
703 
704  UNUSED_PARAMETER( lhs, rhs, index );
705 
706  return true;
707 }
709 //*************************************************************************************************
710 
711 
712 //*************************************************************************************************
727 template< typename VT1 // Type of the left-hand side vector
728  , bool TF1 // Transpose flag of the left-hand side vector
729  , typename VT2 // Type of the right-hand side vector
730  , bool TF2 > // Transpose flag of the right-hand side vector
731 BLAZE_ALWAYS_INLINE bool tryDivAssign( const Vector<VT1,TF1>& lhs, const Vector<VT2,TF2>& rhs, size_t index )
732 {
733  BLAZE_INTERNAL_ASSERT( index <= (~lhs).size(), "Invalid vector access index" );
734  BLAZE_INTERNAL_ASSERT( (~rhs).size() <= (~lhs).size() - index, "Invalid vector size" );
735 
736  UNUSED_PARAMETER( lhs, rhs, index );
737 
738  return true;
739 }
741 //*************************************************************************************************
742 
743 
744 //*************************************************************************************************
759 template< typename VT // Type of the vector
760  , bool TF > // Transpose flag of the vector
761 BLAZE_ALWAYS_INLINE VT& derestrict( Vector<VT,TF>& vector )
762 {
763  return ~vector;
764 }
766 //*************************************************************************************************
767 
768 } // namespace blaze
769 
770 #endif
#define BLAZE_THROW_INVALID_ARGUMENT(MESSAGE)
Macro for the emission of a std::invalid_argument exception.This macro encapsulates the default way o...
Definition: Exception.h:235
Header file for the UNUSED_PARAMETER function template.
BLAZE_ALWAYS_INLINE size_t capacity(const Matrix< MT, SO > &matrix) noexcept
Returns the maximum capacity of the matrix.
Definition: Matrix.h:346
BLAZE_ALWAYS_INLINE bool isSame(const Matrix< MT1, SO1 > &a, const Matrix< MT2, SO2 > &b) noexcept
Returns whether the two given matrices represent the same observable state.
Definition: Matrix.h:653
Header file for basic type definitions.
BLAZE_ALWAYS_INLINE size_t size(const Vector< VT, TF > &vector) noexcept
Returns the current size/dimension of the vector.
Definition: Vector.h:258
Header file for the IsSame and IsStrictlySame type traits.
BLAZE_ALWAYS_INLINE MT::Iterator begin(Matrix< MT, SO > &matrix, size_t i)
Returns an iterator to the first element of row/column i.
Definition: Matrix.h:188
BLAZE_ALWAYS_INLINE const VectorType & operator~() const noexcept
Conversion operator for constant vectors.
Definition: Vector.h:98
BLAZE_ALWAYS_INLINE size_t nonZeros(const Matrix< MT, SO > &matrix)
Returns the total number of non-zero elements in the matrix.
Definition: Matrix.h:384
Type relationship analysis.This class tests if the two data types A and B are equal. For this type comparison, the cv-qualifiers of both data types are ignored. If A and B are the same data type (ignoring the cv-qualifiers), then the value member constant is set to true, the nested type definition Type is TrueType, and the class derives from TrueType. Otherwise value is set to false, Type is FalseType, and the class derives from FalseType.
Definition: IsSame.h:138
VT VectorType
Type of the vector.
Definition: Vector.h:80
BLAZE_ALWAYS_INLINE VectorType & operator~() noexcept
Conversion operator for non-constant vectors.
Definition: Vector.h:88
BLAZE_ALWAYS_INLINE MT::ConstIterator cend(const Matrix< MT, SO > &matrix, size_t i)
Returns an iterator just past the last element of row/column i.
Definition: Matrix.h:298
BLAZE_ALWAYS_INLINE MT::ConstIterator cbegin(const Matrix< MT, SO > &matrix, size_t i)
Returns an iterator to the first element of row/column i.
Definition: Matrix.h:232
Header file for the DisableIf class template.
Namespace of the Blaze C++ math library.
Definition: Blaze.h:57
#define BLAZE_ALWAYS_INLINE
Platform dependent setup of an enforced inline keyword.
Definition: Inline.h:85
const Element * ConstIterator
Iterator over constant elements.
Definition: CompressedMatrix.h:2647
Header file for the exception macros of the math module.
BLAZE_ALWAYS_INLINE void resize(Matrix< MT, SO > &matrix, size_t rows, size_t columns, bool preserve=true)
Changing the size of the matrix.
Definition: Matrix.h:538
BLAZE_ALWAYS_INLINE MT::Iterator end(Matrix< MT, SO > &matrix, size_t i)
Returns an iterator just past the last element of row/column i.
Definition: Matrix.h:254
Header file for the EnableIf class template.
Header file for run time assertion macros.
Element * Iterator
Iterator over non-constant elements.
Definition: CompressedMatrix.h:2646
Base class for N-dimensional vectors.The Vector class is a base class for all arbitrarily sized (N-di...
Definition: Forward.h:154
#define BLAZE_FUNCTION_TRACE
Function trace macro.This macro can be used to reliably trace function calls. In case function tracin...
Definition: FunctionTrace.h:157
void UNUSED_PARAMETER(const Args &...)
Suppression of unused parameter warnings.
Definition: Unused.h:81
Header file for the IsResizable type trait.
System settings for the inline keywords.
#define BLAZE_INTERNAL_ASSERT(expr, msg)
Run time assertion macro for internal checks.In case of an invalid run time expression, the program execution is terminated. The BLAZE_INTERNAL_ASSERT macro can be disabled by setting the BLAZE_USER_ASSERTION flag to zero or by defining NDEBUG during the compilation.
Definition: Assert.h:101
Header file for the FunctionTrace class.