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 
43 #include <blaze/math/Aliases.h>
47 #include <blaze/math/Exception.h>
53 #include <blaze/util/Assert.h>
54 #include <blaze/util/EnableIf.h>
55 #include <blaze/util/Types.h>
57 
58 
59 namespace blaze {
60 
61 //=================================================================================================
62 //
63 // CLASS SVECTRANSPOSER
64 //
65 //=================================================================================================
66 
67 //*************************************************************************************************
73 template< typename VT // Type of the sparse vector
74  , bool TF > // Transpose flag
75 class SVecTransposer
76  : public SparseVector< SVecTransposer<VT,TF>, TF >
77 {
78  public:
79  //**Type definitions****************************************************************************
85  using CompositeType = const This&;
90  //**********************************************************************************************
91 
92  //**Compilation flags***************************************************************************
94 
97  enum : bool { smpAssignable = VT::smpAssignable };
98  //**********************************************************************************************
99 
100  //**Constructor*********************************************************************************
105  explicit inline SVecTransposer( VT& sv ) noexcept
106  : sv_( sv ) // The sparse vector operand
107  {}
108  //**********************************************************************************************
109 
110  //**Subscript operator**************************************************************************
116  inline ConstReference operator[]( size_t index ) const {
117  BLAZE_USER_ASSERT( index < sv_.size(), "Invalid vector access index" );
118  return sv_[index];
119  }
120  //**********************************************************************************************
121 
122  //**At function*********************************************************************************
129  inline ConstReference at( size_t index ) const {
130  if( index >= sv_.size() ) {
131  BLAZE_THROW_OUT_OF_RANGE( "Invalid vector access index" );
132  }
133  return (*this)[index];
134  }
135  //**********************************************************************************************
136 
137  //**Begin function******************************************************************************
142  inline Iterator begin() {
143  return sv_.begin();
144  }
145  //**********************************************************************************************
146 
147  //**Begin function******************************************************************************
152  inline ConstIterator begin() const {
153  return sv_.cbegin();
154  }
155  //**********************************************************************************************
156 
157  //**Cbegin function*****************************************************************************
162  inline ConstIterator cbegin() const {
163  return sv_.cbegin();
164  }
165  //**********************************************************************************************
166 
167  //**End function********************************************************************************
172  inline Iterator end() {
173  return sv_.end();
174  }
175  //**********************************************************************************************
176 
177  //**End function********************************************************************************
182  inline ConstIterator end() const {
183  return sv_.cend();
184  }
185  //**********************************************************************************************
186 
187  //**Cend function*******************************************************************************
192  inline ConstIterator cend() const {
193  return sv_.cend();
194  }
195  //**********************************************************************************************
196 
197  //**Multiplication assignment operator**********************************************************
204  template< typename Other > // Data type of the right-hand side scalar
206  {
207  (~sv_) *= rhs;
208  return *this;
209  }
210  //**********************************************************************************************
211 
212  //**Division assignment operator****************************************************************
221  template< typename Other > // Data type of the right-hand side scalar
223  {
224  BLAZE_USER_ASSERT( rhs != Other(0), "Division by zero detected" );
225 
226  (~sv_) /= rhs;
227  return *this;
228  }
229  //**********************************************************************************************
230 
231  //**Size function*******************************************************************************
236  inline size_t size() const noexcept {
237  return sv_.size();
238  }
239  //**********************************************************************************************
240 
241  //**Reset function******************************************************************************
246  inline void reset() {
247  return sv_.reset();
248  }
249  //**********************************************************************************************
250 
251  //**Insert function*****************************************************************************
263  inline Iterator insert( size_t index, const ElementType& value ) {
264  return sv_.insert( index, value );
265  }
266  //**********************************************************************************************
267 
268  //**Find function*******************************************************************************
281  inline Iterator find( size_t index ) {
282  return sv_.find( index );
283  }
284  //**********************************************************************************************
285 
286  //**Reserve function****************************************************************************
295  inline void reserve( size_t nonzeros ) {
296  sv_.reserve( nonzeros );
297  }
298  //**********************************************************************************************
299 
300  //**Append function*****************************************************************************
325  inline void append( size_t index, const ElementType& value, bool check=false ) {
326  sv_.append( index, value, check );
327  }
328  //**********************************************************************************************
329 
330  //**CanAlias function***************************************************************************
336  template< typename Other > // Data type of the foreign expression
337  inline bool canAlias( const Other* alias ) const noexcept
338  {
339  return sv_.canAlias( alias );
340  }
341  //**********************************************************************************************
342 
343  //**IsAliased function**************************************************************************
349  template< typename Other > // Data type of the foreign expression
350  inline bool isAliased( const Other* alias ) const noexcept
351  {
352  return sv_.isAliased( alias );
353  }
354  //**********************************************************************************************
355 
356  //**CanSMPAssign function***********************************************************************
361  inline bool canSMPAssign() const noexcept
362  {
363  return sv_.canSMPAssign();
364  }
365  //**********************************************************************************************
366 
367  //**Transpose assignment of dense vectors*******************************************************
378  template< typename VT2 > // Type of the right-hand side dense vector
379  inline void assign( const DenseVector<VT2,TF>& rhs )
380  {
382 
383  BLAZE_INTERNAL_ASSERT( sv_.size() == (~rhs).size(), "Invalid vector sizes" );
384 
385  size_t nonzeros( 0UL );
386 
387  for( size_t i=0UL; i<sv_.size(); ++i ) {
388  if( !isDefault( (~rhs)[i] ) ) {
389  if( nonzeros++ == sv_.capacity() )
390  sv_.reserve( extendCapacity() );
391  sv_.append( i, (~rhs)[i] );
392  }
393  }
394  }
395  //**********************************************************************************************
396 
397  //**Transpose assignment of sparse vectors******************************************************
408  template< typename VT2 > // Type of the right-hand side sparse vector
409  inline void assign( const SparseVector<VT2,TF>& rhs )
410  {
412 
413  BLAZE_INTERNAL_ASSERT( sv_.size() == (~rhs).size(), "Invalid vector sizes" );
414 
415  // Using the following formulation instead of a std::copy function call of the form
416  //
417  // end_ = std::copy( (~rhs).begin(), (~rhs).end(), begin_ );
418  //
419  // results in much less requirements on the ConstIterator type provided from the right-hand
420  // sparse vector type
421  for( ConstIterator_<VT2> element=(~rhs).begin(); element!=(~rhs).end(); ++element )
422  sv_.append( element->index(), element->value() );
423  }
424  //**********************************************************************************************
425 
426  private:
427  //**********************************************************************************************
435  inline size_t extendCapacity() const noexcept
436  {
437  using blaze::max;
438  using blaze::min;
439 
440  size_t nonzeros( 2UL*sv_.capacity()+1UL );
441  nonzeros = max( nonzeros, 7UL );
442  nonzeros = min( nonzeros, sv_.size() );
443 
444  BLAZE_INTERNAL_ASSERT( nonzeros > sv_.capacity(), "Invalid capacity value" );
445 
446  return nonzeros;
447  }
448  //**********************************************************************************************
449 
450  //**Member variables****************************************************************************
451  VT& sv_;
452  //**********************************************************************************************
453 
454  //**Compile time checks*************************************************************************
460  //**********************************************************************************************
461 };
462 //*************************************************************************************************
463 
464 
465 
466 
467 //=================================================================================================
468 //
469 // GLOBAL OPERATORS
470 //
471 //=================================================================================================
472 
473 //*************************************************************************************************
481 template< typename VT // Type of the sparse vector
482  , bool TF > // Transpose flag
483 inline void reset( SVecTransposer<VT,TF>& v )
484 {
485  v.reset();
486 }
488 //*************************************************************************************************
489 
490 
491 
492 
493 //=================================================================================================
494 //
495 // SUBVECTORTRAIT SPECIALIZATIONS
496 //
497 //=================================================================================================
498 
499 //*************************************************************************************************
501 template< typename VT, bool TF >
502 struct SubvectorTrait< SVecTransposer<VT,TF> >
503 {
505 };
507 //*************************************************************************************************
508 
509 } // namespace blaze
510 
511 #endif
Constraint on the data type.
Iterator find(size_t index)
Inserting an element into the sparse vector.
Definition: SVecTransposer.h:281
Header file for auxiliary alias declarations.
Headerfile for the generic min algorithm.
ConstIterator begin() const
Returns an iterator to the first non-zero element of the sparse vector.
Definition: SVecTransposer.h:152
#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 basic type definitions.
Header file for the SparseVector base class.
size_t size() const noexcept
Returns the current size/dimension of the vector.
Definition: SVecTransposer.h:236
ReturnType_< VT > ReturnType
Return type for expression template evaluations.
Definition: SVecTransposer.h:84
#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:81
ResultType_< VT > TransposeType
Transpose type for expression template evaluations.
Definition: SVecTransposer.h:82
const ElementType_< MT > min(const DenseMatrix< MT, SO > &dm)
Returns the smallest element of the dense matrix.
Definition: DenseMatrix.h:1762
typename T::ResultType ResultType_
Alias declaration for nested ResultType type definitions.The ResultType_ alias declaration provides a...
Definition: Aliases.h:343
const ElementType_< MT > max(const DenseMatrix< MT, SO > &dm)
Returns the largest element of the dense matrix.
Definition: DenseMatrix.h:1809
ConstIterator_< VT > ConstIterator
Iterator over constant elements.
Definition: SVecTransposer.h:89
Reference_< VT > Reference
Reference to a non-constant matrix value.
Definition: SVecTransposer.h:86
typename T::ReturnType ReturnType_
Alias declaration for nested ReturnType type definitions.The ReturnType_ alias declaration provides a...
Definition: Aliases.h:363
Base template for the SubvectorTrait class.
Definition: SubvectorTrait.h:120
Headerfile for the generic max algorithm.
bool isAliased(const Other *alias) const noexcept
Returns whether the vector is aliased with the given address alias.
Definition: SVecTransposer.h:350
void append(size_t index, const ElementType &value, bool check=false)
Appending an element to the sparse vector.
Definition: SVecTransposer.h:325
Namespace of the Blaze C++ math library.
Definition: Blaze.h:57
#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
typename SubvectorTrait< VT >::Type SubvectorTrait_
Auxiliary alias declaration for the SubvectorTrait type trait.The SubvectorTrait_ alias declaration p...
Definition: SubvectorTrait.h:155
Header file for the subvector trait.
typename T::ElementType ElementType_
Alias declaration for nested ElementType type definitions.The ElementType_ alias declaration provides...
Definition: Aliases.h:163
Expression object for the transposition of a sparse vector.The SVecTransposer class is a wrapper obje...
Definition: Forward.h:147
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:61
Constraint on the data type.
Header file for the exception macros of the math module.
ConstIterator end() const
Returns an iterator just past the last non-zero element of the sparse vector.
Definition: SVecTransposer.h:182
Constraint on the data type.
void reset()
Resets the vector elements.
Definition: SVecTransposer.h:246
EnableIf_< IsNumeric< Other >, SVecTransposer > & operator*=(Other rhs)
Multiplication assignment operator for the multiplication between a vector and a scalar value ( )...
Definition: SVecTransposer.h:205
typename T::Reference Reference_
Alias declaration for nested Reference type definitions.The Reference_ alias declaration provides a c...
Definition: Aliases.h:303
Header file for the EnableIf class template.
void reserve(size_t nonzeros)
Setting the minimum capacity of the sparse vector.
Definition: SVecTransposer.h:295
ElementType_< VT > ElementType
Resulting element type.
Definition: SVecTransposer.h:83
void assign(const SparseVector< VT2, TF > &rhs)
Implementation of the transpose assignment of a sparse vector.
Definition: SVecTransposer.h:409
Header file for the IsNumeric type trait.
Header file for run time assertion macros.
void assign(const DenseVector< VT2, TF > &rhs)
Implementation of the transpose assignment of a dense vector.
Definition: SVecTransposer.h:379
ConstReference at(size_t index) const
Checked access to the vector elements.
Definition: SVecTransposer.h:129
ConstIterator cbegin() const
Returns an iterator to the first non-zero element of the sparse vector.
Definition: SVecTransposer.h:162
Iterator end()
Returns an iterator just past the last non-zero element of the sparse vector.
Definition: SVecTransposer.h:172
Header file for the isDefault shim.
TransposeType_< VT > ResultType
Result type for expression template evaluations.
Definition: SVecTransposer.h:81
typename EnableIf< Condition, T >::Type EnableIf_
Auxiliary alias declaration for the EnableIf class template.The EnableIf_ alias declaration provides ...
Definition: EnableIf.h:224
typename T::Iterator Iterator_
Alias declaration for nested Iterator type definitions.The Iterator_ alias declaration provides a con...
Definition: Aliases.h:183
bool canAlias(const Other *alias) const noexcept
Returns whether the vector can alias with the given address alias.
Definition: SVecTransposer.h:337
ConstReference_< VT > ConstReference
Reference to a constant matrix value.
Definition: SVecTransposer.h:87
typename T::ConstReference ConstReference_
Alias declaration for nested ConstReference type definitions.The ConstReference_ alias declaration pr...
Definition: Aliases.h:143
EnableIf_< IsNumeric< Other >, SVecTransposer > & operator/=(Other rhs)
Division assignment operator for the division of a vector by a scalar value ( ).
Definition: SVecTransposer.h:222
typename T::ConstIterator ConstIterator_
Alias declaration for nested ConstIterator type definitions.The ConstIterator_ alias declaration prov...
Definition: Aliases.h:103
VT & sv_
The sparse vector operand.
Definition: SVecTransposer.h:451
Iterator begin()
Returns an iterator to the first non-zero element of the sparse vector.
Definition: SVecTransposer.h:142
Iterator_< VT > Iterator
Iterator over non-constant elements.
Definition: SVecTransposer.h:88
Base class for sparse vectors.The SparseVector class is a base class for all arbitrarily sized (N-dim...
Definition: Forward.h:130
size_t extendCapacity() const noexcept
Calculating a new vector capacity.
Definition: SVecTransposer.h:435
Iterator insert(size_t index, const ElementType &value)
Inserting an element into the sparse vector.
Definition: SVecTransposer.h:263
bool canSMPAssign() const noexcept
Returns whether the vector can be used in SMP assignments.
Definition: SVecTransposer.h:361
ConstIterator cend() const
Returns an iterator just past the last non-zero element of the sparse vector.
Definition: SVecTransposer.h:192
ConstReference operator[](size_t index) const
Subscript operator for the direct access to the vector elements.
Definition: SVecTransposer.h:116
bool isDefault(const DiagonalProxy< MT > &proxy)
Returns whether the represented element is in default state.
Definition: DiagonalProxy.h:600
typename T::TransposeType TransposeType_
Alias declaration for nested TransposeType type definitions.The TransposeType_ alias declaration prov...
Definition: Aliases.h:423
#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:63
#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
SVecTransposer(VT &sv) noexcept
Constructor for the SVecTransposer class.
Definition: SVecTransposer.h:105