All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SVecTransposer.h
Go to the documentation of this file.
1 //=================================================================================================
20 //=================================================================================================
21 
22 #ifndef _BLAZE_MATH_EXPRESSIONS_SVECTRANSPOSER_H_
23 #define _BLAZE_MATH_EXPRESSIONS_SVECTRANSPOSER_H_
24 
25 
26 //*************************************************************************************************
27 // Includes
28 //*************************************************************************************************
29 
34 #include <blaze/math/Functions.h>
36 #include <blaze/util/Assert.h>
37 #include <blaze/util/Types.h>
38 
39 
40 namespace blaze {
41 
42 //=================================================================================================
43 //
44 // CLASS SVECTRANSPOSER
45 //
46 //=================================================================================================
47 
48 //*************************************************************************************************
54 template< typename VT // Type of the sparse vector
55  , bool TF > // Transpose flag
56 class SVecTransposer : public SparseVector< SVecTransposer<VT,TF>, TF >
57 {
58  public:
59  //**Type definitions****************************************************************************
61  typedef typename VT::TransposeType ResultType;
62  typedef typename VT::ResultType TransposeType;
63  typedef typename VT::ElementType ElementType;
64  typedef typename VT::ReturnType ReturnType;
65  typedef const This& CompositeType;
66  typedef typename VT::Reference Reference;
67  typedef typename VT::ConstReference ConstReference;
68  typedef typename VT::Iterator Iterator;
69  typedef typename VT::ConstIterator ConstIterator;
70  //**********************************************************************************************
71 
72  //**Constructor*********************************************************************************
77  explicit inline SVecTransposer( VT& sv )
78  : sv_( sv ) // The sparse vector operand
79  {}
80  //**********************************************************************************************
81 
82  //**Subscript operator**************************************************************************
88  inline Reference operator[]( size_t index ) {
89  BLAZE_USER_ASSERT( index < sv_.size(), "Invalid vector access index" );
90  return sv_[index];
91  }
92  //**********************************************************************************************
93 
94  //**Begin function******************************************************************************
99  inline ConstIterator begin() const {
100  return sv_.begin();
101  }
102  //**********************************************************************************************
103 
104  //**End function********************************************************************************
109  inline ConstIterator end() const {
110  return sv_.end();
111  }
112  //**********************************************************************************************
113 
114  //**Size function*******************************************************************************
119  inline size_t size() const {
120  return sv_.size();
121  }
122  //**********************************************************************************************
123 
124  //**Reset function******************************************************************************
129  inline void reset() {
130  return sv_.reset();
131  }
132  //**********************************************************************************************
133 
134  //**Insert function*****************************************************************************
146  inline Iterator insert( size_t index, const ElementType& value ) {
147  return sv_.insert( index, value );
148  }
149  //**********************************************************************************************
150 
151  //**Find function*******************************************************************************
164  inline Iterator find( size_t index ) {
165  return sv_.find( index );
166  }
167  //**********************************************************************************************
168 
169  //**Reserve function****************************************************************************
178  inline void reserve( size_t nonzeros ) {
179  sv_.reserve( nonzeros );
180  }
181  //**********************************************************************************************
182 
183  //**Append function*****************************************************************************
208  inline void append( size_t index, const ElementType& value, bool check=false ) {
209  sv_.append( index, value, check );
210  }
211  //**********************************************************************************************
212 
213  //**IsAliased function**************************************************************************
219  template< typename Other > // Data type of the foreign expression
220  inline bool canAlias( const Other* alias ) const
221  {
222  return sv_.canAlias( alias );
223  }
224  //**********************************************************************************************
225 
226  //**IsAliased function**************************************************************************
232  template< typename Other > // Data type of the foreign expression
233  inline bool isAliased( const Other* alias ) const
234  {
235  return sv_.isAliased( alias );
236  }
237  //**********************************************************************************************
238 
239  //**Transpose assignment of dense vectors*******************************************************
250  template< typename VT2 > // Type of the right-hand side dense vector
251  inline void assign( const DenseVector<VT2,TF>& rhs )
252  {
254 
255  BLAZE_INTERNAL_ASSERT( sv_.size() == (~rhs).size(), "Invalid vector sizes" );
256 
257  size_t nonzeros( 0UL );
258 
259  for( size_t i=0UL; i<sv_.size(); ++i ) {
260  if( !isDefault( (~rhs)[i] ) ) {
261  if( nonzeros++ == sv_.capacity() )
262  sv_.reserve( extendCapacity() );
263  sv_.append( i, (~rhs)[i] );
264  }
265  }
266  }
267  //**********************************************************************************************
268 
269  //**Transpose assignment of sparse vectors******************************************************
280  template< typename VT2 > // Type of the right-hand side sparse vector
281  inline void assign( const SparseVector<VT2,TF>& rhs )
282  {
284 
285  BLAZE_INTERNAL_ASSERT( sv_.size() == (~rhs).size(), "Invalid vector sizes" );
286 
287  // Using the following formulation instead of a std::copy function call of the form
288  //
289  // end_ = std::copy( (~rhs).begin(), (~rhs).end(), begin_ );
290  //
291  // results in much less requirements on the ConstIterator type provided from the right-hand
292  // sparse vector type
293  for( typename VT2::ConstIterator element=(~rhs).begin(); element!=(~rhs).end(); ++element )
294  sv_.append( element->index(), element->value() );
295  }
296  //**********************************************************************************************
297 
298  private:
299  //**********************************************************************************************
307  inline size_t extendCapacity() const
308  {
309  using blaze::max;
310  using blaze::min;
311 
312  size_t nonzeros( 2UL*sv_.capacity()+1UL );
313  nonzeros = max( nonzeros, 7UL );
314  nonzeros = min( nonzeros, sv_.size() );
315 
316  BLAZE_INTERNAL_ASSERT( nonzeros > sv_.capacity(), "Invalid capacity value" );
317 
318  return nonzeros;
319  }
320  //**********************************************************************************************
321 
322  //**Member variables****************************************************************************
323  VT& sv_;
324  //**********************************************************************************************
325 
326  //**Compile time checks*************************************************************************
332  //**********************************************************************************************
333 };
334 //*************************************************************************************************
335 
336 
337 
338 
339 //=================================================================================================
340 //
341 // GLOBAL OPERATORS
342 //
343 //=================================================================================================
344 
345 //*************************************************************************************************
353 template< typename VT // Type of the sparse vector
354  , bool TF > // Transpose flag
355 inline void reset( SVecTransposer<VT,TF>& v )
356 {
357  v.reset();
358 }
360 //*************************************************************************************************
361 
362 } // namespace blaze
363 
364 #endif