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 
44 #include <blaze/system/Inline.h>
45 #include <blaze/util/Assert.h>
46 #include <blaze/util/DisableIf.h>
47 #include <blaze/util/EnableIf.h>
48 #include <blaze/util/Exception.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***************************************************************
89  return *static_cast<VectorType*>( this );
90  }
91  //**********************************************************************************************
92 
93  //**Const conversion operators******************************************************************
98  BLAZE_ALWAYS_INLINE const VectorType& operator~() const {
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 );
137 
138 template< typename VT, bool TF >
139 BLAZE_ALWAYS_INLINE size_t capacity( const Vector<VT,TF>& vector );
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 );
150 //*************************************************************************************************
151 
152 
153 //*************************************************************************************************
159 template< typename VT // Type of the vector
160  , bool TF > // Transpose flag of the vector
162 {
163  return (~vector).begin();
164 }
165 //*************************************************************************************************
166 
167 
168 //*************************************************************************************************
174 template< typename VT // Type of the vector
175  , bool TF > // Transpose flag of the vector
177 {
178  return (~vector).begin();
179 }
180 //*************************************************************************************************
181 
182 
183 //*************************************************************************************************
189 template< typename VT // Type of the vector
190  , bool TF > // Transpose flag of the vector
192 {
193  return (~vector).begin();
194 }
195 //*************************************************************************************************
196 
197 
198 //*************************************************************************************************
204 template< typename VT // Type of the vector
205  , bool TF > // Transpose flag of the vector
207 {
208  return (~vector).end();
209 }
210 //*************************************************************************************************
211 
212 
213 //*************************************************************************************************
219 template< typename VT // Type of the vector
220  , bool TF > // Transpose flag of the vector
222 {
223  return (~vector).end();
224 }
225 //*************************************************************************************************
226 
227 
228 //*************************************************************************************************
234 template< typename VT // Type of the vector
235  , bool TF > // Transpose flag of the vector
237 {
238  return (~vector).end();
239 }
240 //*************************************************************************************************
241 
242 
243 //*************************************************************************************************
250 template< typename VT // Type of the vector
251  , bool TF > // Transpose flag of the vector
252 BLAZE_ALWAYS_INLINE size_t size( const Vector<VT,TF>& vector )
253 {
254  return (~vector).size();
255 }
256 //*************************************************************************************************
257 
258 
259 //*************************************************************************************************
266 template< typename VT // Type of the vector
267  , bool TF > // Transpose flag of the vector
269 {
270  return (~vector).capacity();
271 }
272 //*************************************************************************************************
273 
274 
275 //*************************************************************************************************
285 template< typename VT // Type of the vector
286  , bool TF > // Transpose flag of the vector
288 {
289  return (~vector).nonZeros();
290 }
291 //*************************************************************************************************
292 
293 
294 //*************************************************************************************************
308 template< typename VT // Type of the vector
309  , bool TF > // Transpose flag of the vector
310 BLAZE_ALWAYS_INLINE typename DisableIf< IsResizable<VT> >::Type
311  resize_backend( Vector<VT,TF>& vector, size_t n, bool preserve )
312 {
313  UNUSED_PARAMETER( preserve );
314 
315  if( (~vector).size() != n ) {
316  BLAZE_THROW_INVALID_ARGUMENT( "Vector cannot be resized" );
317  }
318 }
320 //*************************************************************************************************
321 
322 
323 //*************************************************************************************************
335 template< typename VT // Type of the vector
336  , bool TF > // Transpose flag of the vector
337 BLAZE_ALWAYS_INLINE typename EnableIf< IsResizable<VT> >::Type
338  resize_backend( Vector<VT,TF>& vector, size_t n, bool preserve )
339 {
340  (~vector).resize( n, preserve );
341 }
343 //*************************************************************************************************
344 
345 
346 //*************************************************************************************************
374 template< typename VT // Type of the vector
375  , bool TF > // Transpose flag of the vector
376 BLAZE_ALWAYS_INLINE void resize( Vector<VT,TF>& vector, size_t n, bool preserve )
377 {
378  resize_backend( vector, n, preserve );
379 }
380 //*************************************************************************************************
381 
382 
383 //*************************************************************************************************
416 template< typename VT1 // Type of the left-hand side vector
417  , bool TF1 // Transpose flag of the left-hand side vector
418  , typename VT2 // Type of the right-hand side vector
419  , bool TF2 > // Transpose flag of the right-hand side vector
421 {
422  return ( IsSame<VT1,VT2>::value &&
423  reinterpret_cast<const void*>( &a ) == reinterpret_cast<const void*>( &b ) );
424 }
425 //*************************************************************************************************
426 
427 
428 //*************************************************************************************************
443 template< typename VT1 // Type of the left-hand side vector
444  , bool TF1 // Transpose flag of the left-hand side vector
445  , typename VT2 // Type of the right-hand side vector
446  , bool TF2 > // Transpose flag of the right-hand side vector
447 BLAZE_ALWAYS_INLINE void assign( Vector<VT1,TF1>& lhs, const Vector<VT2,TF2>& rhs )
448 {
450 
451  BLAZE_INTERNAL_ASSERT( (~lhs).size() == (~rhs).size(), "Invalid vector sizes" );
452  (~lhs).assign( ~rhs );
453 }
455 //*************************************************************************************************
456 
457 
458 //*************************************************************************************************
473 template< typename VT1 // Type of the left-hand side vector
474  , bool TF1 // Transpose flag of the left-hand side vector
475  , typename VT2 // Type of the right-hand side vector
476  , bool TF2 > // Transpose flag of the right-hand side vector
477 BLAZE_ALWAYS_INLINE void addAssign( Vector<VT1,TF1>& lhs, const Vector<VT2,TF2>& rhs )
478 {
480 
481  BLAZE_INTERNAL_ASSERT( (~lhs).size() == (~rhs).size(), "Invalid vector sizes" );
482  (~lhs).addAssign( ~rhs );
483 }
485 //*************************************************************************************************
486 
487 
488 //*************************************************************************************************
503 template< typename VT1 // Type of the left-hand side vector
504  , bool TF1 // Transpose flag of the left-hand side vector
505  , typename VT2 // Type of the right-hand side vector
506  , bool TF2 > // Transpose flag of the right-hand side vector
507 BLAZE_ALWAYS_INLINE void subAssign( Vector<VT1,TF1>& lhs, const Vector<VT2,TF2>& rhs )
508 {
510 
511  BLAZE_INTERNAL_ASSERT( (~lhs).size() == (~rhs).size(), "Invalid vector sizes" );
512  (~lhs).subAssign( ~rhs );
513 }
515 //*************************************************************************************************
516 
517 
518 //*************************************************************************************************
533 template< typename VT1 // Type of the left-hand side vector
534  , bool TF1 // Transpose flag of the left-hand side vector
535  , typename VT2 // Type of the right-hand side vector
536  , bool TF2 > // Transpose flag of the right-hand side vector
537 BLAZE_ALWAYS_INLINE void multAssign( Vector<VT1,TF1>& lhs, const Vector<VT2,TF2>& rhs )
538 {
540 
541  BLAZE_INTERNAL_ASSERT( (~lhs).size() == (~rhs).size(), "Invalid vector sizes" );
542  (~lhs).multAssign( ~rhs );
543 }
545 //*************************************************************************************************
546 
547 
548 //*************************************************************************************************
563 template< typename VT1 // Type of the left-hand side vector
564  , bool TF1 // Transpose flag of the left-hand side vector
565  , typename VT2 // Type of the right-hand side vector
566  , bool TF2 > // Transpose flag of the right-hand side vector
567 BLAZE_ALWAYS_INLINE bool tryAssign( const Vector<VT1,TF1>& lhs, const Vector<VT2,TF2>& rhs, size_t index )
568 {
569  BLAZE_INTERNAL_ASSERT( index <= (~lhs).size(), "Invalid vector access index" );
570  BLAZE_INTERNAL_ASSERT( (~rhs).size() <= (~lhs).size() - index, "Invalid vector size" );
571 
572  UNUSED_PARAMETER( lhs, rhs, index );
573 
574  return true;
575 }
577 //*************************************************************************************************
578 
579 
580 //*************************************************************************************************
595 template< typename VT1 // Type of the left-hand side vector
596  , bool TF1 // Transpose flag of the left-hand side vector
597  , typename VT2 // Type of the right-hand side vector
598  , bool TF2 > // Transpose flag of the right-hand side vector
599 BLAZE_ALWAYS_INLINE bool tryAddAssign( const Vector<VT1,TF1>& lhs, const Vector<VT2,TF2>& rhs, size_t index )
600 {
601  BLAZE_INTERNAL_ASSERT( index <= (~lhs).size(), "Invalid vector access index" );
602  BLAZE_INTERNAL_ASSERT( (~rhs).size() <= (~lhs).size() - index, "Invalid vector size" );
603 
604  UNUSED_PARAMETER( lhs, rhs, index );
605 
606  return true;
607 }
609 //*************************************************************************************************
610 
611 
612 //*************************************************************************************************
627 template< typename VT1 // Type of the left-hand side vector
628  , bool TF1 // Transpose flag of the left-hand side vector
629  , typename VT2 // Type of the right-hand side vector
630  , bool TF2 > // Transpose flag of the right-hand side vector
631 BLAZE_ALWAYS_INLINE bool trySubAssign( const Vector<VT1,TF1>& lhs, const Vector<VT2,TF2>& rhs, size_t index )
632 {
633  BLAZE_INTERNAL_ASSERT( index <= (~lhs).size(), "Invalid vector access index" );
634  BLAZE_INTERNAL_ASSERT( (~rhs).size() <= (~lhs).size() - index, "Invalid vector size" );
635 
636  UNUSED_PARAMETER( lhs, rhs, index );
637 
638  return true;
639 }
641 //*************************************************************************************************
642 
643 
644 //*************************************************************************************************
659 template< typename VT1 // Type of the left-hand side vector
660  , bool TF1 // Transpose flag of the left-hand side vector
661  , typename VT2 // Type of the right-hand side vector
662  , bool TF2 > // Transpose flag of the right-hand side vector
663 BLAZE_ALWAYS_INLINE bool tryMultAssign( const Vector<VT1,TF1>& lhs, const Vector<VT2,TF2>& rhs, size_t index )
664 {
665  BLAZE_INTERNAL_ASSERT( index <= (~lhs).size(), "Invalid vector access index" );
666  BLAZE_INTERNAL_ASSERT( (~rhs).size() <= (~lhs).size() - index, "Invalid vector size" );
667 
668  UNUSED_PARAMETER( lhs, rhs, index );
669 
670  return true;
671 }
673 //*************************************************************************************************
674 
675 
676 //*************************************************************************************************
691 template< typename VT // Type of the vector
692  , bool TF > // Transpose flag of the vector
693 BLAZE_ALWAYS_INLINE VT& derestrict( Vector<VT,TF>& vector )
694 {
695  return ~vector;
696 }
698 //*************************************************************************************************
699 
700 } // namespace blaze
701 
702 #endif
#define BLAZE_THROW_INVALID_ARGUMENT(MESSAGE)
Macro for the emission of a std::invalid_argument exceptionThis macro encapsulates the default way of...
Definition: Exception.h:187
Header file for the UNUSED_PARAMETER function template.
Header file for basic type definitions.
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:229
BLAZE_ALWAYS_INLINE size_t size(const Vector< VT, TF > &vector)
Returns the current size/dimension of the vector.
Definition: Vector.h:252
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:292
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:250
BLAZE_ALWAYS_INLINE bool isSame(const Matrix< MT1, SO1 > &a, const Matrix< MT2, SO2 > &b)
Returns whether the two given matrices represent the same observable state.
Definition: Matrix.h:647
Header file for the IsSame and IsStrictlySame type traits.
BLAZE_ALWAYS_INLINE size_t capacity(const Matrix< MT, SO > &matrix)
Returns the maximum capacity of the matrix.
Definition: Matrix.h:340
void UNUSED_PARAMETER(const T1 &)
Suppression of unused parameter warnings.
Definition: Unused.h:81
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:378
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 enumeration is set to 1, the nested type definition Type is TrueType, and the class derives from TrueType. Otherwise value is set to 0, Type is FalseType, and the class derives from FalseType.
Definition: IsSame.h:158
VT VectorType
Type of the vector.
Definition: Vector.h:80
BLAZE_ALWAYS_INLINE VectorType & operator~()
Conversion operator for non-constant vectors.
Definition: Vector.h:88
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:2592
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:187
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:532
Header file for the EnableIf class template.
Header file for run time assertion macros.
Element * Iterator
Iterator over non-constant elements.
Definition: CompressedMatrix.h:2591
BLAZE_ALWAYS_INLINE const VectorType & operator~() const
Conversion operator for constant vectors.
Definition: Vector.h:98
Base class for N-dimensional vectors.The Vector class is a base class for all arbitrarily sized (N-di...
Definition: Forward.h:164
#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
Header file for exception macros.
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.