All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SVecTransposer.h
Go to the documentation of this file.
1 //=================================================================================================
33 //=================================================================================================
34 
35 #ifndef _BLAZE_MATH_EXPRESSIONS_SVECTRANSPOSER_H_
36 #define _BLAZE_MATH_EXPRESSIONS_SVECTRANSPOSER_H_
37 
38 
39 //*************************************************************************************************
40 // Includes
41 //*************************************************************************************************
42 
47 #include <blaze/math/Functions.h>
50 #include <blaze/util/Assert.h>
51 #include <blaze/util/EnableIf.h>
52 #include <blaze/util/Types.h>
54 
55 
56 namespace blaze {
57 
58 //=================================================================================================
59 //
60 // CLASS SVECTRANSPOSER
61 //
62 //=================================================================================================
63 
64 //*************************************************************************************************
70 template< typename VT // Type of the sparse vector
71  , bool TF > // Transpose flag
72 class SVecTransposer : public SparseVector< SVecTransposer<VT,TF>, TF >
73 {
74  public:
75  //**Type definitions****************************************************************************
77  typedef typename VT::TransposeType ResultType;
78  typedef typename VT::ResultType TransposeType;
79  typedef typename VT::ElementType ElementType;
80  typedef typename VT::ReturnType ReturnType;
81  typedef const This& CompositeType;
82  typedef typename VT::Reference Reference;
84  typedef typename VT::Iterator Iterator;
85  typedef typename VT::ConstIterator ConstIterator;
86  //**********************************************************************************************
87 
88  //**Compilation flags***************************************************************************
90 
93  enum { smpAssignable = VT::smpAssignable };
94  //**********************************************************************************************
95 
96  //**Constructor*********************************************************************************
101  explicit inline SVecTransposer( VT& sv )
102  : sv_( sv ) // The sparse vector operand
103  {}
104  //**********************************************************************************************
105 
106  //**Subscript operator**************************************************************************
112  inline ConstReference operator[]( size_t index ) const {
113  BLAZE_USER_ASSERT( index < sv_.size(), "Invalid vector access index" );
114  return sv_[index];
115  }
116  //**********************************************************************************************
117 
118  //**Begin function******************************************************************************
123  inline Iterator begin() {
124  return sv_.begin();
125  }
126  //**********************************************************************************************
127 
128  //**Begin function******************************************************************************
133  inline ConstIterator begin() const {
134  return sv_.cbegin();
135  }
136  //**********************************************************************************************
137 
138  //**Cbegin function*****************************************************************************
143  inline ConstIterator cbegin() const {
144  return sv_.cbegin();
145  }
146  //**********************************************************************************************
147 
148  //**End function********************************************************************************
153  inline Iterator end() {
154  return sv_.end();
155  }
156  //**********************************************************************************************
157 
158  //**End function********************************************************************************
163  inline ConstIterator end() const {
164  return sv_.cend();
165  }
166  //**********************************************************************************************
167 
168  //**Cend function*******************************************************************************
173  inline ConstIterator cend() const {
174  return sv_.cend();
175  }
176  //**********************************************************************************************
177 
178  //**Multiplication assignment operator**********************************************************
185  template< typename Other > // Data type of the right-hand side scalar
186  inline typename EnableIf< IsNumeric<Other>, SVecTransposer >::Type& operator*=( Other rhs )
187  {
188  (~sv_) *= rhs;
189  return *this;
190  }
191  //**********************************************************************************************
192 
193  //**Division assignment operator****************************************************************
202  template< typename Other > // Data type of the right-hand side scalar
203  inline typename EnableIf< IsNumeric<Other>, SVecTransposer >::Type& operator/=( Other rhs )
204  {
205  BLAZE_USER_ASSERT( rhs != Other(0), "Division by zero detected" );
206 
207  (~sv_) /= rhs;
208  return *this;
209  }
210  //**********************************************************************************************
211 
212  //**Size function*******************************************************************************
217  inline size_t size() const {
218  return sv_.size();
219  }
220  //**********************************************************************************************
221 
222  //**Reset function******************************************************************************
227  inline void reset() {
228  return sv_.reset();
229  }
230  //**********************************************************************************************
231 
232  //**Insert function*****************************************************************************
244  inline Iterator insert( size_t index, const ElementType& value ) {
245  return sv_.insert( index, value );
246  }
247  //**********************************************************************************************
248 
249  //**Find function*******************************************************************************
262  inline Iterator find( size_t index ) {
263  return sv_.find( index );
264  }
265  //**********************************************************************************************
266 
267  //**Reserve function****************************************************************************
276  inline void reserve( size_t nonzeros ) {
277  sv_.reserve( nonzeros );
278  }
279  //**********************************************************************************************
280 
281  //**Append function*****************************************************************************
306  inline void append( size_t index, const ElementType& value, bool check=false ) {
307  sv_.append( index, value, check );
308  }
309  //**********************************************************************************************
310 
311  //**CanAlias function***************************************************************************
317  template< typename Other > // Data type of the foreign expression
318  inline bool canAlias( const Other* alias ) const
319  {
320  return sv_.canAlias( alias );
321  }
322  //**********************************************************************************************
323 
324  //**IsAliased function**************************************************************************
330  template< typename Other > // Data type of the foreign expression
331  inline bool isAliased( const Other* alias ) const
332  {
333  return sv_.isAliased( alias );
334  }
335  //**********************************************************************************************
336 
337  //**CanSMPAssign function***********************************************************************
342  inline bool canSMPAssign() const
343  {
344  return sv_.canSMPAssign();
345  }
346  //**********************************************************************************************
347 
348  //**Transpose assignment of dense vectors*******************************************************
359  template< typename VT2 > // Type of the right-hand side dense vector
360  inline void assign( const DenseVector<VT2,TF>& rhs )
361  {
363 
364  BLAZE_INTERNAL_ASSERT( sv_.size() == (~rhs).size(), "Invalid vector sizes" );
365 
366  size_t nonzeros( 0UL );
367 
368  for( size_t i=0UL; i<sv_.size(); ++i ) {
369  if( !isDefault( (~rhs)[i] ) ) {
370  if( nonzeros++ == sv_.capacity() )
371  sv_.reserve( extendCapacity() );
372  sv_.append( i, (~rhs)[i] );
373  }
374  }
375  }
376  //**********************************************************************************************
377 
378  //**Transpose assignment of sparse vectors******************************************************
389  template< typename VT2 > // Type of the right-hand side sparse vector
390  inline void assign( const SparseVector<VT2,TF>& rhs )
391  {
393 
394  BLAZE_INTERNAL_ASSERT( sv_.size() == (~rhs).size(), "Invalid vector sizes" );
395 
396  // Using the following formulation instead of a std::copy function call of the form
397  //
398  // end_ = std::copy( (~rhs).begin(), (~rhs).end(), begin_ );
399  //
400  // results in much less requirements on the ConstIterator type provided from the right-hand
401  // sparse vector type
402  for( typename VT2::ConstIterator element=(~rhs).begin(); element!=(~rhs).end(); ++element )
403  sv_.append( element->index(), element->value() );
404  }
405  //**********************************************************************************************
406 
407  private:
408  //**********************************************************************************************
416  inline size_t extendCapacity() const
417  {
418  using blaze::max;
419  using blaze::min;
420 
421  size_t nonzeros( 2UL*sv_.capacity()+1UL );
422  nonzeros = max( nonzeros, 7UL );
423  nonzeros = min( nonzeros, sv_.size() );
424 
425  BLAZE_INTERNAL_ASSERT( nonzeros > sv_.capacity(), "Invalid capacity value" );
426 
427  return nonzeros;
428  }
429  //**********************************************************************************************
430 
431  //**Member variables****************************************************************************
432  VT& sv_;
433  //**********************************************************************************************
434 
435  //**Compile time checks*************************************************************************
441  //**********************************************************************************************
442 };
443 //*************************************************************************************************
444 
445 
446 
447 
448 //=================================================================================================
449 //
450 // GLOBAL OPERATORS
451 //
452 //=================================================================================================
453 
454 //*************************************************************************************************
462 template< typename VT // Type of the sparse vector
463  , bool TF > // Transpose flag
464 inline void reset( SVecTransposer<VT,TF>& v )
465 {
466  v.reset();
467 }
469 //*************************************************************************************************
470 
471 
472 
473 
474 //=================================================================================================
475 //
476 // SUBVECTORTRAIT SPECIALIZATIONS
477 //
478 //=================================================================================================
479 
480 //*************************************************************************************************
482 template< typename VT, bool TF >
483 struct SubvectorTrait< SVecTransposer<VT,TF> >
484 {
486 };
488 //*************************************************************************************************
489 
490 } // namespace blaze
491 
492 #endif
Constraint on the data type.
const MT::ElementType max(const DenseMatrix< MT, SO > &dm)
Returns the largest element of the dense matrix.
Definition: DenseMatrix.h:994
Header file for mathematical functions.
#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 SparseVector base class.
bool canAlias(const Other *alias) const
Returns whether the vector can alias with the given address alias.
Definition: SVecTransposer.h:318
void assign(const DenseVector< VT2, TF > &rhs)
Implementation of the transpose assignment of a dense vector.
Definition: SVecTransposer.h:360
size_t size() const
Returns the current size/dimension of the vector.
Definition: SVecTransposer.h:217
ConstIterator begin() const
Returns an iterator to the first non-zero element of the sparse vector.
Definition: SVecTransposer.h:133
VT::ElementType ElementType
Resulting element type.
Definition: SVecTransposer.h:79
ConstIterator cend() const
Returns an iterator just past the last non-zero element of the sparse vector.
Definition: SVecTransposer.h:173
#define BLAZE_CONSTRAINT_MUST_NOT_BE_COMPUTATION_TYPE(T)
Constraint on the data type.In case the given data type T is a computational expression (i...
Definition: Computation.h:118
Iterator find(size_t index)
Inserting an element into the sparse vector.
Definition: SVecTransposer.h:262
VT::TransposeType ResultType
Result type for expression template evaluations.
Definition: SVecTransposer.h:77
VT & sv_
The sparse vector operand.
Definition: SVecTransposer.h:432
void append(size_t index, const ElementType &value, bool check=false)
Appending an element to the sparse vector.
Definition: SVecTransposer.h:306
void reserve(size_t nonzeros)
Setting the minimum capacity of the sparse vector.
Definition: SVecTransposer.h:276
bool isAliased(const Other *alias) const
Returns whether the vector is aliased with the given address alias.
Definition: SVecTransposer.h:331
EnableIf< IsNumeric< Other >, SVecTransposer >::Type & operator/=(Other rhs)
Division assignment operator for the division of a vector by a scalar value ( ).
Definition: SVecTransposer.h:203
void assign(const SparseVector< VT2, TF > &rhs)
Implementation of the transpose assignment of a sparse vector.
Definition: SVecTransposer.h:390
ConstIterator cbegin() const
Returns an iterator to the first non-zero element of the sparse vector.
Definition: SVecTransposer.h:143
const Element * ConstIterator
Iterator over constant elements.
Definition: CompressedMatrix.h:2482
const MT::ElementType min(const DenseMatrix< MT, SO > &dm)
Returns the smallest element of the dense matrix.
Definition: DenseMatrix.h:947
Header file for the subvector trait.
Expression object for the transposition of a sparse vector.The SVecTransposer class is a wrapper obje...
Definition: Forward.h:123
VT::ReturnType ReturnType
Return type for expression template evaluations.
Definition: SVecTransposer.h:80
Base class for N-dimensional dense vectors.The DenseVector class is a base class for all arbitrarily ...
Definition: DenseVector.h:70
#define BLAZE_CONSTRAINT_MUST_BE_SPARSE_VECTOR_TYPE(T)
Constraint on the data type.In case the given data type T is not a sparse, N-dimensional vector type...
Definition: SparseVector.h:79
Constraint on the data type.
CompressedMatrix< Type, false > TransposeType
Transpose type for expression template evaluations.
Definition: CompressedMatrix.h:2475
Type ElementType
Type of the sparse matrix elements.
Definition: CompressedMatrix.h:2476
SVecTransposer(VT &sv)
Constructor for the SVecTransposer class.
Definition: SVecTransposer.h:101
Iterator begin()
Returns an iterator to the first non-zero element of the sparse vector.
Definition: SVecTransposer.h:123
VT::ConstReference ConstReference
Reference to a constant matrix value.
Definition: SVecTransposer.h:83
Constraint on the data type.
const Type & ConstReference
Reference to a constant matrix value.
Definition: CompressedMatrix.h:2480
Header file for the EnableIf class template.
Header file for the IsNumeric type trait.
Iterator insert(size_t index, const ElementType &value)
Inserting an element into the sparse vector.
Definition: SVecTransposer.h:244
VT::ConstIterator ConstIterator
Iterator over constant elements.
Definition: SVecTransposer.h:85
const Type & ReturnType
Return type for expression template evaluations.
Definition: CompressedMatrix.h:2477
Header file for run time assertion macros.
Substitution Failure Is Not An Error (SFINAE) class.The EnableIf class template is an auxiliary tool ...
Definition: EnableIf.h:184
EnableIf< IsNumeric< Other >, SVecTransposer >::Type & operator*=(Other rhs)
Multiplication assignment operator for the multiplication between a vector and a scalar value ( )...
Definition: SVecTransposer.h:186
Element * Iterator
Iterator over non-constant elements.
Definition: CompressedMatrix.h:2481
Header file for the isDefault shim.
BLAZE_ALWAYS_INLINE bool isDefault(const NonNumericProxy< MT > &proxy)
Returns whether the represented element is in default state.
Definition: NonNumericProxy.h:874
BLAZE_ALWAYS_INLINE void reset(const NonNumericProxy< MT > &proxy)
Resetting the represented element to the default initial values.
Definition: NonNumericProxy.h:833
Iterator end()
Returns an iterator just past the last non-zero element of the sparse vector.
Definition: SVecTransposer.h:153
SVecTransposer< VT, TF > This
Type of this SVecTransposer instance.
Definition: SVecTransposer.h:76
VT::Reference Reference
Reference to a non-constant matrix value.
Definition: SVecTransposer.h:82
void reset()
Resets the vector elements.
Definition: SVecTransposer.h:227
VT::ResultType TransposeType
Transpose type for expression template evaluations.
Definition: SVecTransposer.h:78
ConstReference operator[](size_t index) const
Subscript operator for the direct access to the vector elements.
Definition: SVecTransposer.h:112
bool canSMPAssign() const
Returns whether the vector can be used in SMP assignments.
Definition: SVecTransposer.h:342
VT::Iterator Iterator
Iterator over non-constant elements.
Definition: SVecTransposer.h:84
Base class for sparse vectors.The SparseVector class is a base class for all arbitrarily sized (N-dim...
Definition: Forward.h:108
This ResultType
Result type for expression template evaluations.
Definition: CompressedMatrix.h:2473
Header file for basic type definitions.
MatrixAccessProxy< This > Reference
Reference to a non-constant matrix value.
Definition: CompressedMatrix.h:2479
const This & CompositeType
Data type for composite expression templates.
Definition: SVecTransposer.h:81
ConstIterator end() const
Returns an iterator just past the last non-zero element of the sparse vector.
Definition: SVecTransposer.h:163
#define BLAZE_CONSTRAINT_MUST_BE_VECTOR_WITH_TRANSPOSE_FLAG(T, TF)
Constraint on the data type.In case the given data type T is not a dense or sparse vector type and in...
Definition: TransposeFlag.h:238
#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
size_t extendCapacity() const
Calculating a new vector capacity.
Definition: SVecTransposer.h:416