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 
43 #include <blaze/math/Aliases.h>
45 #include <blaze/math/Exception.h>
47 #include <blaze/math/shims/Clear.h>
48 #include <blaze/math/shims/Reset.h>
50 #include <blaze/system/Inline.h>
51 #include <blaze/util/Types.h>
52 #include <blaze/util/Unused.h>
53 
54 
55 namespace blaze {
56 
57 //=================================================================================================
58 //
59 // CLASS DEFINITION
60 //
61 //=================================================================================================
62 
63 //*************************************************************************************************
71 template< typename PT // Type of the proxy
72  , typename VT > // Type of the sparse vector
73 class SparseVectorProxy : public SparseVector< PT, IsRowVector<VT>::value >
74 {
75  public:
76  //**Type definitions****************************************************************************
86  //**********************************************************************************************
87 
88  //**Compilation flags***************************************************************************
90  enum : bool { smpAssignable = VT::smpAssignable };
91  //**********************************************************************************************
92 
93  //**Data access functions***********************************************************************
96  inline Reference operator[]( size_t index ) const;
97  inline Reference at( size_t index ) const;
98 
99  inline Iterator begin () const;
100  inline ConstIterator cbegin() const;
101  inline Iterator end () const;
102  inline ConstIterator cend () const;
104  //**********************************************************************************************
105 
106  //**Utility functions***************************************************************************
109  inline size_t size() const;
110  inline size_t capacity() const;
111  inline size_t nonZeros() const;
112  inline void reset() const;
113  inline void clear() const;
114  inline Iterator set( size_t index, const ElementType& value ) const;
115  inline Iterator insert( size_t index, const ElementType& value ) const;
116  inline void append( size_t index, const ElementType& value, bool check=false ) const;
117  inline void erase( size_t index ) const;
118  inline Iterator erase( Iterator pos ) const;
119  inline Iterator erase( Iterator first, Iterator last ) const;
120  inline void resize( size_t n, bool preserve=true ) const;
121  inline void reserve( size_t n ) const;
122 
123  template< typename Other > inline void scale( const Other& scalar ) const;
125  //**********************************************************************************************
126 
127  //**Lookup functions****************************************************************************
130  inline Iterator find ( size_t index ) const;
131  inline Iterator find ( size_t i, size_t j ) const;
132  inline Iterator lowerBound( size_t index ) const;
133  inline Iterator lowerBound( size_t i, size_t j ) const;
134  inline Iterator upperBound( size_t index ) const;
135  inline Iterator upperBound( size_t i, size_t j ) const;
137  //**********************************************************************************************
138 
139  private:
140  //**Compile time checks*************************************************************************
144  //**********************************************************************************************
145 };
146 //*************************************************************************************************
147 
148 
149 
150 
151 //=================================================================================================
152 //
153 // DATA ACCESS FUNCTIONS
154 //
155 //=================================================================================================
156 
157 //*************************************************************************************************
169 template< typename PT // Type of the proxy
170  , typename VT > // Type of the sparse vector
173 {
174  if( (~*this).isRestricted() ) {
175  BLAZE_THROW_INVALID_ARGUMENT( "Invalid access to restricted element" );
176  }
177 
178  return (~*this).get()[index];
179 }
180 //*************************************************************************************************
181 
182 
183 //*************************************************************************************************
196 template< typename PT // Type of the proxy
197  , typename VT > // Type of the sparse vector
199  SparseVectorProxy<PT,VT>::at( size_t index ) const
200 {
201  if( (~*this).isRestricted() ) {
202  BLAZE_THROW_INVALID_ARGUMENT( "Invalid access to restricted element" );
203  }
204 
205  return (~*this).get().at( index );
206 }
207 //*************************************************************************************************
208 
209 
210 //*************************************************************************************************
215 template< typename PT // Type of the proxy
216  , typename VT > // Type of the sparse vector
218 {
219  return (~*this).get().begin();
220 }
221 //*************************************************************************************************
222 
223 
224 //*************************************************************************************************
229 template< typename PT // Type of the proxy
230  , typename VT > // Type of the sparse vector
232 {
233  return (~*this).get().cbegin();
234 }
235 //*************************************************************************************************
236 
237 
238 //*************************************************************************************************
243 template< typename PT // Type of the proxy
244  , typename VT > // Type of the sparse vector
246 {
247  return (~*this).get().end();
248 }
249 //*************************************************************************************************
250 
251 
252 //*************************************************************************************************
257 template< typename PT // Type of the proxy
258  , typename VT > // Type of the sparse vector
260 {
261  return (~*this).get().cend();
262 }
263 //*************************************************************************************************
264 
265 
266 
267 
268 //=================================================================================================
269 //
270 // UTILITY FUNCTIONS
271 //
272 //=================================================================================================
273 
274 //*************************************************************************************************
279 template< typename PT // Type of the proxy
280  , typename VT > // Type of the sparse vector
281 inline size_t SparseVectorProxy<PT,VT>::size() const
282 {
283  return (~*this).get().size();
284 }
285 //*************************************************************************************************
286 
287 
288 //*************************************************************************************************
293 template< typename PT // Type of the proxy
294  , typename VT > // Type of the sparse vector
296 {
297  return (~*this).get().capacity();
298 }
299 //*************************************************************************************************
300 
301 
302 //*************************************************************************************************
310 template< typename PT // Type of the proxy
311  , typename VT > // Type of the sparse vector
313 {
314  return (~*this).get().nonZeros();
315 }
316 //*************************************************************************************************
317 
318 
319 //*************************************************************************************************
326 template< typename PT // Type of the proxy
327  , typename VT > // Type of the sparse vector
329 {
330  using blaze::reset;
331 
332  reset( (~*this).get() );
333 }
334 //*************************************************************************************************
335 
336 
337 //*************************************************************************************************
344 template< typename PT // Type of the proxy
345  , typename VT > // Type of the sparse vector
347 {
348  using blaze::clear;
349 
350  clear( (~*this).get() );
351 }
352 //*************************************************************************************************
353 
354 
355 //*************************************************************************************************
368 template< typename PT // Type of the proxy
369  , typename VT > // Type of the sparse vector
371  SparseVectorProxy<PT,VT>::set( size_t index, const ElementType& value ) const
372 {
373  if( (~*this).isRestricted() ) {
374  BLAZE_THROW_INVALID_ARGUMENT( "Invalid access to restricted element" );
375  }
376 
377  return (~*this).get().set( index, value );
378 }
379 //*************************************************************************************************
380 
381 
382 //*************************************************************************************************
395 template< typename PT // Type of the proxy
396  , typename VT > // Type of the sparse vector
398  SparseVectorProxy<PT,VT>::insert( size_t index, const ElementType& value ) const
399 {
400  if( (~*this).isRestricted() ) {
401  BLAZE_THROW_INVALID_ARGUMENT( "Invalid access to restricted element" );
402  }
403 
404  return (~*this).get().insert( index, value );
405 }
406 //*************************************************************************************************
407 
408 
409 //*************************************************************************************************
434 template< typename PT // Type of the proxy
435  , typename VT > // Type of the sparse vector
436 inline void SparseVectorProxy<PT,VT>::append( size_t index, const ElementType& value, bool check ) const
437 {
438  if( (~*this).isRestricted() ) {
439  BLAZE_THROW_INVALID_ARGUMENT( "Invalid access to restricted element" );
440  }
441 
442  (~*this).get().append( index, value, check );
443 }
444 //*************************************************************************************************
445 
446 
447 //*************************************************************************************************
456 template< typename PT // Type of the proxy
457  , typename VT > // Type of the sparse vector
458 inline void SparseVectorProxy<PT,VT>::erase( size_t index ) const
459 {
460  if( (~*this).isRestricted() ) {
461  BLAZE_THROW_INVALID_ARGUMENT( "Invalid access to restricted element" );
462  }
463 
464  (~*this).get().erase( index );
465 }
466 //*************************************************************************************************
467 
468 
469 //*************************************************************************************************
478 template< typename PT // Type of the proxy
479  , typename VT > // Type of the sparse vector
481 {
482  if( (~*this).isRestricted() ) {
483  BLAZE_THROW_INVALID_ARGUMENT( "Invalid access to restricted element" );
484  }
485 
486  return (~*this).get().erase( pos );
487 }
488 //*************************************************************************************************
489 
490 
491 //*************************************************************************************************
501 template< typename PT // Type of the proxy
502  , typename VT > // Type of the sparse vector
505 {
506  if( (~*this).isRestricted() ) {
507  BLAZE_THROW_INVALID_ARGUMENT( "Invalid access to restricted element" );
508  }
509 
510  return (~*this).get().erase( first, last );
511 }
512 //*************************************************************************************************
513 
514 
515 //*************************************************************************************************
530 template< typename PT // Type of the proxy
531  , typename VT > // Type of the sparse vector
532 inline void SparseVectorProxy<PT,VT>::resize( size_t n, bool preserve ) const
533 {
534  if( (~*this).isRestricted() ) {
535  BLAZE_THROW_INVALID_ARGUMENT( "Invalid access to restricted element" );
536  }
537 
538  (~*this).get().resize( n, preserve );
539 }
540 //*************************************************************************************************
541 
542 
543 //*************************************************************************************************
553 template< typename PT // Type of the proxy
554  , typename VT > // Type of the sparse vector
555 inline void SparseVectorProxy<PT,VT>::reserve( size_t n ) const
556 {
557  if( (~*this).isRestricted() ) {
558  BLAZE_THROW_INVALID_ARGUMENT( "Invalid access to restricted element" );
559  }
560 
561  (~*this).get().reserve( n );
562 }
563 //*************************************************************************************************
564 
565 
566 //*************************************************************************************************
573 template< typename PT // Type of the proxy
574  , typename VT > // Type of the sparse vector
575 template< typename Other > // Data type of the scalar value
576 inline void SparseVectorProxy<PT,VT>::scale( const Other& scalar ) const
577 {
578  if( (~*this).isRestricted() ) {
579  BLAZE_THROW_INVALID_ARGUMENT( "Invalid access to restricted element" );
580  }
581 
582  (~*this).get().scale( scalar );
583 }
584 //*************************************************************************************************
585 
586 
587 
588 
589 //=================================================================================================
590 //
591 // LOOKUP FUNCTIONS
592 //
593 //=================================================================================================
594 
595 //*************************************************************************************************
608 template< typename PT // Type of the proxy
609  , typename VT > // Type of the sparse vector
611  SparseVectorProxy<PT,VT>::find( size_t index ) const
612 {
613  return (~*this).get().find( index );
614 }
615 //*************************************************************************************************
616 
617 
618 //*************************************************************************************************
630 template< typename PT // Type of the proxy
631  , typename VT > // Type of the sparse vector
634 {
635  return (~*this).get().lowerBound( index );
636 }
637 //*************************************************************************************************
638 
639 
640 //*************************************************************************************************
652 template< typename PT // Type of the proxy
653  , typename VT > // Type of the sparse vector
656 {
657  return (~*this).get().upperBound( index );
658 }
659 //*************************************************************************************************
660 
661 
662 
663 
664 //=================================================================================================
665 //
666 // GLOBAL FUNCTIONS
667 //
668 //=================================================================================================
669 
670 //*************************************************************************************************
673 template< typename PT, typename VT >
675  begin( const SparseVectorProxy<PT,VT>& proxy );
676 
677 template< typename PT, typename VT >
679  cbegin( const SparseVectorProxy<PT,VT>& proxy );
680 
681 template< typename PT, typename VT >
683  end( const SparseVectorProxy<PT,VT>& proxy );
684 
685 template< typename PT, typename VT >
687  cend( const SparseVectorProxy<PT,VT>& proxy );
688 
689 template< typename PT, typename VT >
690 BLAZE_ALWAYS_INLINE size_t size( const SparseVectorProxy<PT,VT>& proxy );
691 
692 template< typename PT, typename VT >
694 
695 template< typename PT, typename VT >
697 
698 template< typename PT, typename VT >
699 BLAZE_ALWAYS_INLINE void resize( const SparseVectorProxy<PT,VT>& proxy, size_t n, bool preserve=true );
700 
701 template< typename PT, typename VT >
703 
704 template< typename PT, typename VT >
707 //*************************************************************************************************
708 
709 
710 //*************************************************************************************************
717 template< typename PT // Type of the proxy
718  , typename VT > // Type of the sparse vector
721 {
722  return proxy.begin();
723 }
724 //*************************************************************************************************
725 
726 
727 //*************************************************************************************************
734 template< typename PT // Type of the proxy
735  , typename VT > // Type of the sparse vector
738 {
739  return proxy.cbegin();
740 }
741 //*************************************************************************************************
742 
743 
744 //*************************************************************************************************
751 template< typename PT // Type of the proxy
752  , typename VT > // Type of the sparse vector
755 {
756  return proxy.end();
757 }
758 //*************************************************************************************************
759 
760 
761 //*************************************************************************************************
768 template< typename PT // Type of the proxy
769  , typename VT > // Type of the sparse vector
772 {
773  return proxy.cend();
774 }
775 //*************************************************************************************************
776 
777 
778 //*************************************************************************************************
785 template< typename PT // Type of the proxy
786  , typename VT > // Type of the sparse vector
788 {
789  return proxy.size();
790 }
791 //*************************************************************************************************
792 
793 
794 //*************************************************************************************************
801 template< typename PT // Type of the proxy
802  , typename VT > // Type of the sparse vector
804 {
805  return proxy.capacity();
806 }
807 //*************************************************************************************************
808 
809 
810 //*************************************************************************************************
820 template< typename PT // Type of the proxy
821  , typename VT > // Type of the sparse vector
823 {
824  return proxy.nonZeros();
825 }
826 //*************************************************************************************************
827 
828 
829 //*************************************************************************************************
840 template< typename PT // Type of the proxy
841  , typename VT > // Type of the sparse vector
842 BLAZE_ALWAYS_INLINE void resize( const SparseVectorProxy<PT,VT>& proxy, size_t n, bool preserve )
843 {
844  proxy.resize( n, preserve );
845 }
846 //*************************************************************************************************
847 
848 
849 //*************************************************************************************************
858 template< typename PT // Type of the proxy
859  , typename VT > // Type of the sparse vector
861 {
862  proxy.reset();
863 }
864 //*************************************************************************************************
865 
866 
867 //*************************************************************************************************
876 template< typename PT // Type of the proxy
877  , typename VT > // Type of the sparse vector
879 {
880  proxy.clear();
881 }
882 //*************************************************************************************************
883 
884 } // namespace blaze
885 
886 #endif
#define BLAZE_THROW_INVALID_ARGUMENT(MESSAGE)
Macro for the emission of a std::invalid_argument exception.This macro encapsulates the default way o...
Definition: Exception.h:235
Reference_< VT > Reference
Reference to a non-constant vector value.
Definition: SparseVectorProxy.h:82
Header file for auxiliary alias declarations.
Header file for the UNUSED_PARAMETER function template.
BLAZE_ALWAYS_INLINE size_t capacity(const Matrix< MT, SO > &matrix) noexcept
Returns the maximum capacity of the matrix.
Definition: Matrix.h:346
Header file for basic type definitions.
Header file for the SparseVector base class.
CompositeType_< VT > CompositeType
Data type for composite expression templates.
Definition: SparseVectorProxy.h:81
void append(size_t index, const ElementType &value, bool check=false) const
Appending an element to the represented sparse vector.
Definition: SparseVectorProxy.h:436
BLAZE_ALWAYS_INLINE size_t size(const Vector< VT, TF > &vector) noexcept
Returns the current size/dimension of the vector.
Definition: Vector.h:258
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:188
void reset(const DiagonalProxy< MT > &proxy)
Resetting the represented element to the default initial values.
Definition: DiagonalProxy.h:533
Header file for the IsRowVector type trait.
Iterator_< VT > Iterator
Iterator over non-constant elements.
Definition: SparseVectorProxy.h:84
TransposeType_< VT > TransposeType
Transpose type for expression template evaluations.
Definition: SparseVectorProxy.h:78
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:384
typename T::ResultType ResultType_
Alias declaration for nested ResultType type definitions.The ResultType_ alias declaration provides a...
Definition: Aliases.h:323
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:298
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:232
void reset() const
Reset to the default initial value.
Definition: SparseVectorProxy.h:328
Iterator end() const
Returns an iterator just past the last element of the represented vector.
Definition: SparseVectorProxy.h:245
typename T::ReturnType ReturnType_
Alias declaration for nested ReturnType type definitions.The ReturnType_ alias declaration provides a...
Definition: Aliases.h:343
ReturnType_< VT > ReturnType
Return type for expression template evaluations.
Definition: SparseVectorProxy.h:80
Iterator lowerBound(size_t index) const
Returns an iterator to the first index not less then the given index.
Definition: SparseVectorProxy.h:633
typename T::CompositeType CompositeType_
Alias declaration for nested CompositeType type definitions.The CompositeType_ alias declaration prov...
Definition: Aliases.h:83
Reference at(size_t index) const
Checked access to the vector elements.
Definition: SparseVectorProxy.h:199
size_t capacity() const
Returns the maximum capacity of the represented vector.
Definition: SparseVectorProxy.h:295
Iterator upperBound(size_t index) const
Returns an iterator to the first index greater then the given index.
Definition: SparseVectorProxy.h:655
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
const Element * ConstIterator
Iterator over constant elements.
Definition: CompressedMatrix.h:2647
ConstReference_< VT > ConstReference
Reference to a constant vector value.
Definition: SparseVectorProxy.h:83
ConstIterator_< VT > ConstIterator
Iterator over constant elements.
Definition: SparseVectorProxy.h:85
Iterator set(size_t index, const ElementType &value) const
Setting an element of the represented sparse vector.
Definition: SparseVectorProxy.h:371
void erase(size_t index) const
Erasing an element from the sparse vector.
Definition: SparseVectorProxy.h:458
typename T::ElementType ElementType_
Alias declaration for nested ElementType type definitions.The ElementType_ alias declaration provides...
Definition: Aliases.h:163
ConstIterator cend() const
Returns an iterator just past the last element of the represented vector.
Definition: SparseVectorProxy.h:259
void resize(size_t n, bool preserve=true) const
Changing the size of the represented vector.
Definition: SparseVectorProxy.h:532
#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
Reference operator[](size_t index) const
Subscript operator for the direct access to vector elements.
Definition: SparseVectorProxy.h:172
Header file for the exception macros of the math module.
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:538
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:254
ResultType_< VT > ResultType
Result type for expression template evaluations.
Definition: SparseVectorProxy.h:77
Constraint on the data type.
size_t nonZeros() const
Returns the number of non-zero elements in the represented vector.
Definition: SparseVectorProxy.h:312
typename T::Reference Reference_
Alias declaration for nested Reference type definitions.The Reference_ alias declaration provides a c...
Definition: Aliases.h:283
void clear(const DiagonalProxy< MT > &proxy)
Clearing the represented element.
Definition: DiagonalProxy.h:553
Iterator begin() const
Returns an iterator to the first element of the represented vector.
Definition: SparseVectorProxy.h:217
void scale(const Other &scalar) const
Scaling of the sparse vector by the scalar value scalar ( ).
Definition: SparseVectorProxy.h:576
Iterator find(size_t index) const
Searches for a specific vector element.
Definition: SparseVectorProxy.h:611
Iterator insert(size_t index, const ElementType &value) const
Inserting an element into the represented sparse vector.
Definition: SparseVectorProxy.h:398
void clear() const
Clearing the represented vector.
Definition: SparseVectorProxy.h:346
Header file for the reset shim.
Element * Iterator
Iterator over non-constant elements.
Definition: CompressedMatrix.h:2646
Proxy backend for sparse vector types.The SparseVectorProxy class serves as a backend for the Proxy c...
Definition: Forward.h:53
typename T::Iterator Iterator_
Alias declaration for nested Iterator type definitions.The Iterator_ alias declaration provides a con...
Definition: Aliases.h:183
ElementType_< VT > ElementType
Type of the sparse vector elements.
Definition: SparseVectorProxy.h:79
typename T::ConstReference ConstReference_
Alias declaration for nested ConstReference type definitions.The ConstReference_ alias declaration pr...
Definition: Aliases.h:143
typename T::ConstIterator ConstIterator_
Alias declaration for nested ConstIterator type definitions.The ConstIterator_ alias declaration prov...
Definition: Aliases.h:103
size_t size() const
Returns the current size/dimension of the represented vector.
Definition: SparseVectorProxy.h:281
MatrixAccessProxy< This > Reference
Reference to a non-constant matrix value.
Definition: CompressedMatrix.h:2644
typename T::TransposeType TransposeType_
Alias declaration for nested TransposeType type definitions.The TransposeType_ alias declaration prov...
Definition: Aliases.h:403
System settings for the inline keywords.
ConstIterator cbegin() const
Returns an iterator to the first element of the represented vector.
Definition: SparseVectorProxy.h:231
void reserve(size_t n) const
Setting the minimum capacity of the represented vector.
Definition: SparseVectorProxy.h:555