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 ElementType& 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*****************************************************************************
204  inline void append( size_t index, const ElementType& value ) {
205  sv_.append( index, value );
206  }
207  //**********************************************************************************************
208 
209  //**IsAliased function**************************************************************************
215  template< typename Other > // Data type of the foreign expression
216  inline bool isAliased( const Other* alias ) const
217  {
218  return sv_.isAliased( alias );
219  }
220  //**********************************************************************************************
221 
222  //**Transpose assignment of dense vectors*******************************************************
233  template< typename VT2 > // Type of the right-hand side dense vector
234  inline void assign( const DenseVector<VT2,TF>& rhs )
235  {
237 
238  BLAZE_INTERNAL_ASSERT( sv_.size() == (~rhs).size(), "Invalid vector sizes" );
239 
240  size_t nonzeros( 0UL );
241 
242  for( size_t i=0UL; i<sv_.size(); ++i ) {
243  if( !isDefault( (~rhs)[i] ) ) {
244  if( nonzeros++ == sv_.capacity() )
245  sv_.reserve( extendCapacity() );
246  sv_.append( i, (~rhs)[i] );
247  }
248  }
249  }
250  //**********************************************************************************************
251 
252  //**Transpose assignment of sparse vectors******************************************************
263  template< typename VT2 > // Type of the right-hand side sparse vector
264  inline void assign( const SparseVector<VT2,TF>& rhs )
265  {
267 
268  BLAZE_INTERNAL_ASSERT( sv_.size() == (~rhs).size(), "Invalid vector sizes" );
269 
270  // Using the following formulation instead of a std::copy function call of the form
271  //
272  // end_ = std::copy( (~rhs).begin(), (~rhs).end(), begin_ );
273  //
274  // results in much less requirements on the ConstIterator type provided from the right-hand
275  // sparse vector type
276  for( typename VT2::ConstIterator element=(~rhs).begin(); element!=(~rhs).end(); ++element )
277  sv_.append( element->index(), element->value() );
278  }
279  //**********************************************************************************************
280 
281  private:
282  //**********************************************************************************************
290  inline size_t extendCapacity() const
291  {
292  using blaze::max;
293  using blaze::min;
294 
295  size_t nonzeros( 2UL*sv_.capacity()+1UL );
296  nonzeros = max( nonzeros, 7UL );
297  nonzeros = min( nonzeros, sv_.size() );
298 
299  BLAZE_INTERNAL_ASSERT( nonzeros > sv_.capacity(), "Invalid capacity value" );
300 
301  return nonzeros;
302  }
303  //**********************************************************************************************
304 
305  //**Member variables****************************************************************************
306  VT& sv_;
307  //**********************************************************************************************
308 
309  //**Compile time checks*************************************************************************
315  //**********************************************************************************************
316 };
317 //*************************************************************************************************
318 
319 
320 
321 
322 //=================================================================================================
323 //
324 // GLOBAL OPERATORS
325 //
326 //=================================================================================================
327 
328 //*************************************************************************************************
336 template< typename VT // Type of the sparse vector
337  , bool TF > // Transpose flag
338 inline void reset( SVecTransposer<VT,TF>& v )
339 {
340  v.reset();
341 }
343 //*************************************************************************************************
344 
345 } // namespace blaze
346 
347 #endif