SparseVectorProxy.h
Go to the documentation of this file.
1 //=================================================================================================
33 //=================================================================================================
34 
35 #ifndef _BLAZE_MATH_PROXY_SPARSEVECTORPROXY_H_
36 #define _BLAZE_MATH_PROXY_SPARSEVECTORPROXY_H_
37 
38 
39 //*************************************************************************************************
40 // Includes
41 //*************************************************************************************************
42 
45 #include <blaze/math/shims/Clear.h>
46 #include <blaze/math/shims/Reset.h>
48 #include <blaze/system/Inline.h>
49 #include <blaze/util/Exception.h>
50 #include <blaze/util/Types.h>
51 #include <blaze/util/Unused.h>
52 
53 
54 namespace blaze {
55 
56 //=================================================================================================
57 //
58 // CLASS DEFINITION
59 //
60 //=================================================================================================
61 
62 //*************************************************************************************************
70 template< typename PT // Type of the proxy
71  , typename VT > // Type of the sparse vector
72 class SparseVectorProxy : public SparseVector< PT, IsRowVector<VT>::value >
73 {
74  public:
75  //**Type definitions****************************************************************************
76  typedef typename VT::ResultType ResultType;
77  typedef typename VT::TransposeType TransposeType;
78  typedef typename VT::ElementType ElementType;
79  typedef typename VT::ReturnType ReturnType;
80  typedef typename VT::CompositeType CompositeType;
81  typedef typename VT::Reference Reference;
83  typedef typename VT::Iterator Iterator;
84  typedef typename VT::ConstIterator ConstIterator;
85  //**********************************************************************************************
86 
87  //**Compilation flags***************************************************************************
89  enum { smpAssignable = VT::smpAssignable };
90  //**********************************************************************************************
91 
92  //**Data access functions***********************************************************************
95  inline Reference operator[]( size_t index ) const;
96 
97  inline Iterator begin () const;
98  inline ConstIterator cbegin() const;
99  inline Iterator end () const;
100  inline ConstIterator cend () const;
102  //**********************************************************************************************
103 
104  //**Utility functions***************************************************************************
107  inline size_t size() const;
108  inline size_t capacity() const;
109  inline size_t nonZeros() const;
110  inline void reset() const;
111  inline void clear() const;
112  inline Iterator set( size_t index, const ElementType& value ) const;
113  inline Iterator insert( size_t index, const ElementType& value ) const;
114  inline void append( size_t index, const ElementType& value, bool check=false ) const;
115  inline void erase( size_t index ) const;
116  inline Iterator erase( Iterator pos ) const;
117  inline Iterator erase( Iterator first, Iterator last ) const;
118  inline void resize( size_t n, bool preserve=true ) const;
119  inline void reserve( size_t n ) const;
120 
121  template< typename Other > inline void scale( const Other& scalar ) const;
123  //**********************************************************************************************
124 
125  //**Lookup functions****************************************************************************
128  inline Iterator find ( size_t index ) const;
129  inline Iterator find ( size_t i, size_t j ) const;
130  inline Iterator lowerBound( size_t index ) const;
131  inline Iterator lowerBound( size_t i, size_t j ) const;
132  inline Iterator upperBound( size_t index ) const;
133  inline Iterator upperBound( size_t i, size_t j ) const;
135  //**********************************************************************************************
136 
137  private:
138  //**Compile time checks*************************************************************************
142  //**********************************************************************************************
143 };
144 //*************************************************************************************************
145 
146 
147 
148 
149 //=================================================================================================
150 //
151 // DATA ACCESS FUNCTIONS
152 //
153 //=================================================================================================
154 
155 //*************************************************************************************************
166 template< typename PT // Type of the proxy
167  , typename VT > // Type of the sparse vector
170 {
171  if( (~*this).isRestricted() ) {
172  BLAZE_THROW_INVALID_ARGUMENT( "Invalid access to restricted element" );
173  }
174 
175  return (~*this).get()[index];
176 }
177 //*************************************************************************************************
178 
179 
180 //*************************************************************************************************
185 template< typename PT // Type of the proxy
186  , typename VT > // Type of the sparse vector
188 {
189  return (~*this).get().begin();
190 }
191 //*************************************************************************************************
192 
193 
194 //*************************************************************************************************
199 template< typename PT // Type of the proxy
200  , typename VT > // Type of the sparse vector
202 {
203  return (~*this).get().cbegin();
204 }
205 //*************************************************************************************************
206 
207 
208 //*************************************************************************************************
213 template< typename PT // Type of the proxy
214  , typename VT > // Type of the sparse vector
216 {
217  return (~*this).get().end();
218 }
219 //*************************************************************************************************
220 
221 
222 //*************************************************************************************************
227 template< typename PT // Type of the proxy
228  , typename VT > // Type of the sparse vector
230 {
231  return (~*this).get().cend();
232 }
233 //*************************************************************************************************
234 
235 
236 
237 
238 //=================================================================================================
239 //
240 // UTILITY FUNCTIONS
241 //
242 //=================================================================================================
243 
244 //*************************************************************************************************
249 template< typename PT // Type of the proxy
250  , typename VT > // Type of the sparse vector
251 inline size_t SparseVectorProxy<PT,VT>::size() const
252 {
253  return (~*this).get().size();
254 }
255 //*************************************************************************************************
256 
257 
258 //*************************************************************************************************
263 template< typename PT // Type of the proxy
264  , typename VT > // Type of the sparse vector
266 {
267  return (~*this).get().capacity();
268 }
269 //*************************************************************************************************
270 
271 
272 //*************************************************************************************************
280 template< typename PT // Type of the proxy
281  , typename VT > // Type of the sparse vector
283 {
284  return (~*this).get().nonZeros();
285 }
286 //*************************************************************************************************
287 
288 
289 //*************************************************************************************************
296 template< typename PT // Type of the proxy
297  , typename VT > // Type of the sparse vector
299 {
300  using blaze::reset;
301 
302  reset( (~*this).get() );
303 }
304 //*************************************************************************************************
305 
306 
307 //*************************************************************************************************
314 template< typename PT // Type of the proxy
315  , typename VT > // Type of the sparse vector
317 {
318  using blaze::clear;
319 
320  clear( (~*this).get() );
321 }
322 //*************************************************************************************************
323 
324 
325 //*************************************************************************************************
337 template< typename PT // Type of the proxy
338  , typename VT > // Type of the sparse vector
340  SparseVectorProxy<PT,VT>::set( size_t index, const ElementType& value ) const
341 {
342  if( (~*this).isRestricted() ) {
343  BLAZE_THROW_INVALID_ARGUMENT( "Invalid access to restricted element" );
344  }
345 
346  return (~*this).get().set( index, value );
347 }
348 //*************************************************************************************************
349 
350 
351 //*************************************************************************************************
363 template< typename PT // Type of the proxy
364  , typename VT > // Type of the sparse vector
366  SparseVectorProxy<PT,VT>::insert( size_t index, const ElementType& value ) const
367 {
368  if( (~*this).isRestricted() ) {
369  BLAZE_THROW_INVALID_ARGUMENT( "Invalid access to restricted element" );
370  }
371 
372  return (~*this).get().insert( index, value );
373 }
374 //*************************************************************************************************
375 
376 
377 //*************************************************************************************************
401 template< typename PT // Type of the proxy
402  , typename VT > // Type of the sparse vector
403 inline void SparseVectorProxy<PT,VT>::append( size_t index, const ElementType& value, bool check ) const
404 {
405  if( (~*this).isRestricted() ) {
406  BLAZE_THROW_INVALID_ARGUMENT( "Invalid access to restricted element" );
407  }
408 
409  (~*this).get().append( index, value, check );
410 }
411 //*************************************************************************************************
412 
413 
414 //*************************************************************************************************
422 template< typename PT // Type of the proxy
423  , typename VT > // Type of the sparse vector
424 inline void SparseVectorProxy<PT,VT>::erase( size_t index ) const
425 {
426  if( (~*this).isRestricted() ) {
427  BLAZE_THROW_INVALID_ARGUMENT( "Invalid access to restricted element" );
428  }
429 
430  (~*this).get().erase( index );
431 }
432 //*************************************************************************************************
433 
434 
435 //*************************************************************************************************
443 template< typename PT // Type of the proxy
444  , typename VT > // Type of the sparse vector
446 {
447  if( (~*this).isRestricted() ) {
448  BLAZE_THROW_INVALID_ARGUMENT( "Invalid access to restricted element" );
449  }
450 
451  return (~*this).get().erase( pos );
452 }
453 //*************************************************************************************************
454 
455 
456 //*************************************************************************************************
465 template< typename PT // Type of the proxy
466  , typename VT > // Type of the sparse vector
469 {
470  if( (~*this).isRestricted() ) {
471  BLAZE_THROW_INVALID_ARGUMENT( "Invalid access to restricted element" );
472  }
473 
474  return (~*this).get().erase( first, last );
475 }
476 //*************************************************************************************************
477 
478 
479 //*************************************************************************************************
493 template< typename PT // Type of the proxy
494  , typename VT > // Type of the sparse vector
495 inline void SparseVectorProxy<PT,VT>::resize( size_t n, bool preserve ) const
496 {
497  if( (~*this).isRestricted() ) {
498  BLAZE_THROW_INVALID_ARGUMENT( "Invalid access to restricted element" );
499  }
500 
501  (~*this).get().resize( n, preserve );
502 }
503 //*************************************************************************************************
504 
505 
506 //*************************************************************************************************
515 template< typename PT // Type of the proxy
516  , typename VT > // Type of the sparse vector
517 inline void SparseVectorProxy<PT,VT>::reserve( size_t n ) const
518 {
519  if( (~*this).isRestricted() ) {
520  BLAZE_THROW_INVALID_ARGUMENT( "Invalid access to restricted element" );
521  }
522 
523  (~*this).get().reserve( n );
524 }
525 //*************************************************************************************************
526 
527 
528 //*************************************************************************************************
534 template< typename PT // Type of the proxy
535  , typename VT > // Type of the sparse vector
536 template< typename Other > // Data type of the scalar value
537 inline void SparseVectorProxy<PT,VT>::scale( const Other& scalar ) const
538 {
539  if( (~*this).isRestricted() ) {
540  BLAZE_THROW_INVALID_ARGUMENT( "Invalid access to restricted element" );
541  }
542 
543  (~*this).get().scale( scalar );
544 }
545 //*************************************************************************************************
546 
547 
548 
549 
550 //=================================================================================================
551 //
552 // LOOKUP FUNCTIONS
553 //
554 //=================================================================================================
555 
556 //*************************************************************************************************
569 template< typename PT // Type of the proxy
570  , typename VT > // Type of the sparse vector
572  SparseVectorProxy<PT,VT>::find( size_t index ) const
573 {
574  return (~*this).get().find( index );
575 }
576 //*************************************************************************************************
577 
578 
579 //*************************************************************************************************
591 template< typename PT // Type of the proxy
592  , typename VT > // Type of the sparse vector
595 {
596  return (~*this).get().lowerBound( index );
597 }
598 //*************************************************************************************************
599 
600 
601 //*************************************************************************************************
613 template< typename PT // Type of the proxy
614  , typename VT > // Type of the sparse vector
617 {
618  return (~*this).get().upperBound( index );
619 }
620 //*************************************************************************************************
621 
622 
623 
624 
625 //=================================================================================================
626 //
627 // GLOBAL FUNCTIONS
628 //
629 //=================================================================================================
630 
631 //*************************************************************************************************
634 template< typename PT, typename VT >
636  begin( const SparseVectorProxy<PT,VT>& proxy );
637 
638 template< typename PT, typename VT >
640  cbegin( const SparseVectorProxy<PT,VT>& proxy );
641 
642 template< typename PT, typename VT >
644  end( const SparseVectorProxy<PT,VT>& proxy );
645 
646 template< typename PT, typename VT >
648  cend( const SparseVectorProxy<PT,VT>& proxy );
649 
650 template< typename PT, typename VT >
651 BLAZE_ALWAYS_INLINE size_t size( const SparseVectorProxy<PT,VT>& proxy );
652 
653 template< typename PT, typename VT >
655 
656 template< typename PT, typename VT >
658 
659 template< typename PT, typename VT >
660 BLAZE_ALWAYS_INLINE void resize( const SparseVectorProxy<PT,VT>& proxy, size_t n, bool preserve=true );
661 
662 template< typename PT, typename VT >
664 
665 template< typename PT, typename VT >
668 //*************************************************************************************************
669 
670 
671 //*************************************************************************************************
678 template< typename PT // Type of the proxy
679  , typename VT > // Type of the sparse vector
682 {
683  return proxy.begin();
684 }
685 //*************************************************************************************************
686 
687 
688 //*************************************************************************************************
695 template< typename PT // Type of the proxy
696  , typename VT > // Type of the sparse vector
699 {
700  return proxy.cbegin();
701 }
702 //*************************************************************************************************
703 
704 
705 //*************************************************************************************************
712 template< typename PT // Type of the proxy
713  , typename VT > // Type of the sparse vector
716 {
717  return proxy.end();
718 }
719 //*************************************************************************************************
720 
721 
722 //*************************************************************************************************
729 template< typename PT // Type of the proxy
730  , typename VT > // Type of the sparse vector
733 {
734  return proxy.cend();
735 }
736 //*************************************************************************************************
737 
738 
739 //*************************************************************************************************
746 template< typename PT // Type of the proxy
747  , typename VT > // Type of the sparse vector
749 {
750  return proxy.size();
751 }
752 //*************************************************************************************************
753 
754 
755 //*************************************************************************************************
762 template< typename PT // Type of the proxy
763  , typename VT > // Type of the sparse vector
765 {
766  return proxy.capacity();
767 }
768 //*************************************************************************************************
769 
770 
771 //*************************************************************************************************
781 template< typename PT // Type of the proxy
782  , typename VT > // Type of the sparse vector
784 {
785  return proxy.nonZeros();
786 }
787 //*************************************************************************************************
788 
789 
790 //*************************************************************************************************
801 template< typename PT // Type of the proxy
802  , typename VT > // Type of the sparse vector
803 BLAZE_ALWAYS_INLINE void resize( const SparseVectorProxy<PT,VT>& proxy, size_t n, bool preserve )
804 {
805  proxy.resize( n, preserve );
806 }
807 //*************************************************************************************************
808 
809 
810 //*************************************************************************************************
819 template< typename PT // Type of the proxy
820  , typename VT > // Type of the sparse vector
822 {
823  proxy.reset();
824 }
825 //*************************************************************************************************
826 
827 
828 //*************************************************************************************************
837 template< typename PT // Type of the proxy
838  , typename VT > // Type of the sparse vector
840 {
841  proxy.clear();
842 }
843 //*************************************************************************************************
844 
845 } // namespace blaze
846 
847 #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.
Header file for the SparseVector base class.
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
void append(size_t index, const ElementType &value, bool check=false) const
Appending an element to the represented sparse vector.
Definition: SparseVectorProxy.h:403
VT::Iterator Iterator
Iterator over non-constant elements.
Definition: SparseVectorProxy.h:83
void reset(const DiagonalProxy< MT > &proxy)
Resetting the represented element to the default initial values.
Definition: DiagonalProxy.h:507
const This & CompositeType
Data type for composite expression templates.
Definition: CompressedMatrix.h:2588
BLAZE_ALWAYS_INLINE size_t capacity(const Matrix< MT, SO > &matrix)
Returns the maximum capacity of the matrix.
Definition: Matrix.h:340
Header file for the IsRowVector type trait.
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
void reset() const
Reset to the default initial value.
Definition: SparseVectorProxy.h:298
Iterator end() const
Returns an iterator just past the last element of the represented vector.
Definition: SparseVectorProxy.h:215
Iterator lowerBound(size_t index) const
Returns an iterator to the first index not less then the given index.
Definition: SparseVectorProxy.h:594
size_t capacity() const
Returns the maximum capacity of the represented vector.
Definition: SparseVectorProxy.h:265
Iterator upperBound(size_t index) const
Returns an iterator to the first index greater then the given index.
Definition: SparseVectorProxy.h:616
Header file for the clear shim.
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
VT::ReturnType ReturnType
Return type for expression template evaluations.
Definition: SparseVectorProxy.h:79
const Element * ConstIterator
Iterator over constant elements.
Definition: CompressedMatrix.h:2592
Iterator set(size_t index, const ElementType &value) const
Setting an element of the represented sparse vector.
Definition: SparseVectorProxy.h:340
void erase(size_t index) const
Erasing an element from the sparse vector.
Definition: SparseVectorProxy.h:424
VT::Reference Reference
Reference to a non-constant vector value.
Definition: SparseVectorProxy.h:81
VT::TransposeType TransposeType
Transpose type for expression template evaluations.
Definition: SparseVectorProxy.h:77
ConstIterator cend() const
Returns an iterator just past the last element of the represented vector.
Definition: SparseVectorProxy.h:229
void resize(size_t n, bool preserve=true) const
Changing the size of the represented vector.
Definition: SparseVectorProxy.h:495
#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
VT::ResultType ResultType
Result type for expression template evaluations.
Definition: SparseVectorProxy.h:76
Reference operator[](size_t index) const
Subscript operator for the direct access to vector elements.
Definition: SparseVectorProxy.h:169
CompressedMatrix< Type, false > TransposeType
Transpose type for expression template evaluations.
Definition: CompressedMatrix.h:2585
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
Type ElementType
Type of the sparse matrix elements.
Definition: CompressedMatrix.h:2586
Constraint on the data type.
const Type & ConstReference
Reference to a constant matrix value.
Definition: CompressedMatrix.h:2590
size_t nonZeros() const
Returns the number of non-zero elements in the represented vector.
Definition: SparseVectorProxy.h:282
void clear(const DiagonalProxy< MT > &proxy)
Clearing the represented element.
Definition: DiagonalProxy.h:527
Iterator begin() const
Returns an iterator to the first element of the represented vector.
Definition: SparseVectorProxy.h:187
void scale(const Other &scalar) const
Scaling of the sparse vector by the scalar value scalar ( ).
Definition: SparseVectorProxy.h:537
VT::ElementType ElementType
Type of the sparse vector elements.
Definition: SparseVectorProxy.h:78
Iterator find(size_t index) const
Searches for a specific vector element.
Definition: SparseVectorProxy.h:572
Iterator insert(size_t index, const ElementType &value) const
Inserting an element into the represented sparse vector.
Definition: SparseVectorProxy.h:366
const Type & ReturnType
Return type for expression template evaluations.
Definition: CompressedMatrix.h:2587
void clear() const
Clearing the represented vector.
Definition: SparseVectorProxy.h:316
Header file for the reset shim.
Element * Iterator
Iterator over non-constant elements.
Definition: CompressedMatrix.h:2591
Proxy backend for sparse vector types.The SparseVectorProxy class serves as a backend for the Proxy c...
Definition: Forward.h:53
VT::ConstIterator ConstIterator
Iterator over constant elements.
Definition: SparseVectorProxy.h:84
VT::ConstReference ConstReference
Reference to a constant vector value.
Definition: SparseVectorProxy.h:82
This ResultType
Result type for expression template evaluations.
Definition: CompressedMatrix.h:2583
size_t size() const
Returns the current size/dimension of the represented vector.
Definition: SparseVectorProxy.h:251
MatrixAccessProxy< This > Reference
Reference to a non-constant matrix value.
Definition: CompressedMatrix.h:2589
Header file for exception macros.
System settings for the inline keywords.
ConstIterator cbegin() const
Returns an iterator to the first element of the represented vector.
Definition: SparseVectorProxy.h:201
VT::CompositeType CompositeType
Data type for composite expression templates.
Definition: SparseVectorProxy.h:80
void reserve(size_t n) const
Setting the minimum capacity of the represented vector.
Definition: SparseVectorProxy.h:517