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/DisableIf.h>
52 #include <blaze/util/Types.h>
54 #include <blaze/util/Unused.h>
55 
56 
57 namespace blaze {
58 
59 //=================================================================================================
60 //
61 // CLASS DEFINITION
62 //
63 //=================================================================================================
64 
65 //*************************************************************************************************
73 template< typename PT // Type of the proxy
74  , typename VT > // Type of the sparse vector
75 class SparseVectorProxy
76  : public SparseVector< PT, IsRowVector<VT>::value >
77 {
78  public:
79  //**Type definitions****************************************************************************
89  //**********************************************************************************************
90 
91  //**Compilation flags***************************************************************************
93  enum : bool { smpAssignable = VT::smpAssignable };
94  //**********************************************************************************************
95 
96  //**Data access functions***********************************************************************
99  inline Reference operator[]( size_t index ) const;
100  inline Reference at( size_t index ) const;
101 
102  inline Iterator begin () const;
103  inline ConstIterator cbegin() const;
104  inline Iterator end () const;
105  inline ConstIterator cend () const;
107  //**********************************************************************************************
108 
109  //**Utility functions***************************************************************************
112  inline size_t size() const;
113  inline size_t capacity() const;
114  inline size_t nonZeros() const;
115  inline void reset() const;
116  inline void clear() const;
117  inline void resize( size_t n, bool preserve=true ) const;
118  inline void reserve( size_t n ) const;
120  //**********************************************************************************************
121 
122  //**Insertion functions*************************************************************************
125  inline Iterator set ( size_t index, const ElementType& value ) const;
126  inline Iterator insert( size_t index, const ElementType& value ) const;
127  inline void append( size_t index, const ElementType& value, bool check=false ) const;
129  //**********************************************************************************************
130 
131  //**Erase functions*****************************************************************************
134  inline void erase( size_t index ) const;
135  inline Iterator erase( Iterator pos ) const;
136  inline Iterator erase( Iterator first, Iterator last ) const;
137 
138  template< typename Pred, typename = DisableIf_< IsIntegral<Pred> > >
139  inline void erase( Pred predicate );
140 
141  template< typename Pred >
142  inline void erase( Iterator first, Iterator last, Pred predicate );
144  //**********************************************************************************************
145 
146  //**Lookup functions****************************************************************************
149  inline Iterator find ( size_t index ) const;
150  inline Iterator find ( size_t i, size_t j ) const;
151  inline Iterator lowerBound( size_t index ) const;
152  inline Iterator lowerBound( size_t i, size_t j ) const;
153  inline Iterator upperBound( size_t index ) const;
154  inline Iterator upperBound( size_t i, size_t j ) const;
156  //**********************************************************************************************
157 
158  //**Numeric functions***************************************************************************
161  template< typename Other > inline void scale( const Other& scalar ) const;
163  //**********************************************************************************************
164 
165  private:
166  //**Compile time checks*************************************************************************
170  //**********************************************************************************************
171 };
172 //*************************************************************************************************
173 
174 
175 
176 
177 //=================================================================================================
178 //
179 // DATA ACCESS FUNCTIONS
180 //
181 //=================================================================================================
182 
183 //*************************************************************************************************
195 template< typename PT // Type of the proxy
196  , typename VT > // Type of the sparse vector
199 {
200  if( (~*this).isRestricted() ) {
201  BLAZE_THROW_INVALID_ARGUMENT( "Invalid access to restricted element" );
202  }
203 
204  return (~*this).get()[index];
205 }
206 //*************************************************************************************************
207 
208 
209 //*************************************************************************************************
222 template< typename PT // Type of the proxy
223  , typename VT > // Type of the sparse vector
225  SparseVectorProxy<PT,VT>::at( size_t index ) const
226 {
227  if( (~*this).isRestricted() ) {
228  BLAZE_THROW_INVALID_ARGUMENT( "Invalid access to restricted element" );
229  }
230 
231  return (~*this).get().at( index );
232 }
233 //*************************************************************************************************
234 
235 
236 //*************************************************************************************************
241 template< typename PT // Type of the proxy
242  , typename VT > // Type of the sparse vector
244 {
245  return (~*this).get().begin();
246 }
247 //*************************************************************************************************
248 
249 
250 //*************************************************************************************************
255 template< typename PT // Type of the proxy
256  , typename VT > // Type of the sparse vector
258 {
259  return (~*this).get().cbegin();
260 }
261 //*************************************************************************************************
262 
263 
264 //*************************************************************************************************
269 template< typename PT // Type of the proxy
270  , typename VT > // Type of the sparse vector
272 {
273  return (~*this).get().end();
274 }
275 //*************************************************************************************************
276 
277 
278 //*************************************************************************************************
283 template< typename PT // Type of the proxy
284  , typename VT > // Type of the sparse vector
286 {
287  return (~*this).get().cend();
288 }
289 //*************************************************************************************************
290 
291 
292 
293 
294 //=================================================================================================
295 //
296 // UTILITY FUNCTIONS
297 //
298 //=================================================================================================
299 
300 //*************************************************************************************************
305 template< typename PT // Type of the proxy
306  , typename VT > // Type of the sparse vector
307 inline size_t SparseVectorProxy<PT,VT>::size() const
308 {
309  return (~*this).get().size();
310 }
311 //*************************************************************************************************
312 
313 
314 //*************************************************************************************************
319 template< typename PT // Type of the proxy
320  , typename VT > // Type of the sparse vector
322 {
323  return (~*this).get().capacity();
324 }
325 //*************************************************************************************************
326 
327 
328 //*************************************************************************************************
336 template< typename PT // Type of the proxy
337  , typename VT > // Type of the sparse vector
339 {
340  return (~*this).get().nonZeros();
341 }
342 //*************************************************************************************************
343 
344 
345 //*************************************************************************************************
352 template< typename PT // Type of the proxy
353  , typename VT > // Type of the sparse vector
355 {
356  using blaze::reset;
357 
358  reset( (~*this).get() );
359 }
360 //*************************************************************************************************
361 
362 
363 //*************************************************************************************************
370 template< typename PT // Type of the proxy
371  , typename VT > // Type of the sparse vector
373 {
374  using blaze::clear;
375 
376  clear( (~*this).get() );
377 }
378 //*************************************************************************************************
379 
380 
381 //*************************************************************************************************
394 template< typename PT // Type of the proxy
395  , typename VT > // Type of the sparse vector
397  SparseVectorProxy<PT,VT>::set( size_t index, const ElementType& value ) const
398 {
399  if( (~*this).isRestricted() ) {
400  BLAZE_THROW_INVALID_ARGUMENT( "Invalid access to restricted element" );
401  }
402 
403  return (~*this).get().set( index, value );
404 }
405 //*************************************************************************************************
406 
407 
408 //*************************************************************************************************
421 template< typename PT // Type of the proxy
422  , typename VT > // Type of the sparse vector
424  SparseVectorProxy<PT,VT>::insert( size_t index, const ElementType& value ) const
425 {
426  if( (~*this).isRestricted() ) {
427  BLAZE_THROW_INVALID_ARGUMENT( "Invalid access to restricted element" );
428  }
429 
430  return (~*this).get().insert( index, value );
431 }
432 //*************************************************************************************************
433 
434 
435 //*************************************************************************************************
460 template< typename PT // Type of the proxy
461  , typename VT > // Type of the sparse vector
462 inline void SparseVectorProxy<PT,VT>::append( size_t index, const ElementType& value, bool check ) const
463 {
464  if( (~*this).isRestricted() ) {
465  BLAZE_THROW_INVALID_ARGUMENT( "Invalid access to restricted element" );
466  }
467 
468  (~*this).get().append( index, value, check );
469 }
470 //*************************************************************************************************
471 
472 
473 //*************************************************************************************************
488 template< typename PT // Type of the proxy
489  , typename VT > // Type of the sparse vector
490 inline void SparseVectorProxy<PT,VT>::resize( size_t n, bool preserve ) const
491 {
492  if( (~*this).isRestricted() ) {
493  BLAZE_THROW_INVALID_ARGUMENT( "Invalid access to restricted element" );
494  }
495 
496  (~*this).get().resize( n, preserve );
497 }
498 //*************************************************************************************************
499 
500 
501 //*************************************************************************************************
511 template< typename PT // Type of the proxy
512  , typename VT > // Type of the sparse vector
513 inline void SparseVectorProxy<PT,VT>::reserve( size_t n ) const
514 {
515  if( (~*this).isRestricted() ) {
516  BLAZE_THROW_INVALID_ARGUMENT( "Invalid access to restricted element" );
517  }
518 
519  (~*this).get().reserve( n );
520 }
521 //*************************************************************************************************
522 
523 
524 //*************************************************************************************************
535 template< typename PT // Type of the proxy
536  , typename VT > // Type of the sparse vector
537 template< typename Other > // Data type of the scalar value
538 inline void SparseVectorProxy<PT,VT>::scale( const Other& scalar ) const
539 {
540  if( (~*this).isRestricted() ) {
541  BLAZE_THROW_INVALID_ARGUMENT( "Invalid access to restricted element" );
542  }
543 
544  (~*this).get().scale( scalar );
545 }
546 //*************************************************************************************************
547 
548 
549 
550 
551 //=================================================================================================
552 //
553 // ERASE FUNCTIONS
554 //
555 //=================================================================================================
556 
557 //*************************************************************************************************
566 template< typename PT // Type of the proxy
567  , typename VT > // Type of the sparse vector
568 inline void SparseVectorProxy<PT,VT>::erase( size_t index ) const
569 {
570  if( (~*this).isRestricted() ) {
571  BLAZE_THROW_INVALID_ARGUMENT( "Invalid access to restricted element" );
572  }
573 
574  (~*this).get().erase( index );
575 }
576 //*************************************************************************************************
577 
578 
579 //*************************************************************************************************
588 template< typename PT // Type of the proxy
589  , typename VT > // Type of the sparse vector
591 {
592  if( (~*this).isRestricted() ) {
593  BLAZE_THROW_INVALID_ARGUMENT( "Invalid access to restricted element" );
594  }
595 
596  return (~*this).get().erase( pos );
597 }
598 //*************************************************************************************************
599 
600 
601 //*************************************************************************************************
611 template< typename PT // Type of the proxy
612  , typename VT > // Type of the sparse vector
615 {
616  if( (~*this).isRestricted() ) {
617  BLAZE_THROW_INVALID_ARGUMENT( "Invalid access to restricted element" );
618  }
619 
620  return (~*this).get().erase( first, last );
621 }
622 //*************************************************************************************************
623 
624 
625 //*************************************************************************************************
638 template< typename PT // Type of the proxy
639  , typename VT > // Type of the sparse vector
640 template< typename Pred // Type of the unary predicate
641  , typename > // Type restriction on the unary predicate
642 inline void SparseVectorProxy<PT,VT>::erase( Pred predicate )
643 {
644  if( (~*this).isRestricted() ) {
645  BLAZE_THROW_INVALID_ARGUMENT( "Invalid access to restricted element" );
646  }
647 
648  (~*this).get().erase( predicate );
649 }
650 //*************************************************************************************************
651 
652 
653 //*************************************************************************************************
668 template< typename PT // Type of the proxy
669  , typename VT > // Type of the sparse vector
670 template< typename Pred > // Type of the unary predicate
671 inline void SparseVectorProxy<PT,VT>::erase( Iterator first, Iterator last, Pred predicate )
672 {
673  if( (~*this).isRestricted() ) {
674  BLAZE_THROW_INVALID_ARGUMENT( "Invalid access to restricted element" );
675  }
676 
677  (~*this).get().erase( first, last, predicate );
678 }
679 //*************************************************************************************************
680 
681 
682 
683 
684 //=================================================================================================
685 //
686 // LOOKUP FUNCTIONS
687 //
688 //=================================================================================================
689 
690 //*************************************************************************************************
703 template< typename PT // Type of the proxy
704  , typename VT > // Type of the sparse vector
706  SparseVectorProxy<PT,VT>::find( size_t index ) const
707 {
708  return (~*this).get().find( index );
709 }
710 //*************************************************************************************************
711 
712 
713 //*************************************************************************************************
725 template< typename PT // Type of the proxy
726  , typename VT > // Type of the sparse vector
729 {
730  return (~*this).get().lowerBound( index );
731 }
732 //*************************************************************************************************
733 
734 
735 //*************************************************************************************************
747 template< typename PT // Type of the proxy
748  , typename VT > // Type of the sparse vector
751 {
752  return (~*this).get().upperBound( index );
753 }
754 //*************************************************************************************************
755 
756 
757 
758 
759 //=================================================================================================
760 //
761 // GLOBAL FUNCTIONS
762 //
763 //=================================================================================================
764 
765 //*************************************************************************************************
768 template< typename PT, typename VT >
770  begin( const SparseVectorProxy<PT,VT>& proxy );
771 
772 template< typename PT, typename VT >
774  cbegin( const SparseVectorProxy<PT,VT>& proxy );
775 
776 template< typename PT, typename VT >
778  end( const SparseVectorProxy<PT,VT>& proxy );
779 
780 template< typename PT, typename VT >
782  cend( const SparseVectorProxy<PT,VT>& proxy );
783 
784 template< typename PT, typename VT >
785 BLAZE_ALWAYS_INLINE size_t size( const SparseVectorProxy<PT,VT>& proxy );
786 
787 template< typename PT, typename VT >
789 
790 template< typename PT, typename VT >
792 
793 template< typename PT, typename VT >
794 BLAZE_ALWAYS_INLINE void resize( const SparseVectorProxy<PT,VT>& proxy, size_t n, bool preserve=true );
795 
796 template< typename PT, typename VT >
798 
799 template< typename PT, typename VT >
802 //*************************************************************************************************
803 
804 
805 //*************************************************************************************************
812 template< typename PT // Type of the proxy
813  , typename VT > // Type of the sparse vector
816 {
817  return proxy.begin();
818 }
819 //*************************************************************************************************
820 
821 
822 //*************************************************************************************************
829 template< typename PT // Type of the proxy
830  , typename VT > // Type of the sparse vector
833 {
834  return proxy.cbegin();
835 }
836 //*************************************************************************************************
837 
838 
839 //*************************************************************************************************
846 template< typename PT // Type of the proxy
847  , typename VT > // Type of the sparse vector
850 {
851  return proxy.end();
852 }
853 //*************************************************************************************************
854 
855 
856 //*************************************************************************************************
863 template< typename PT // Type of the proxy
864  , typename VT > // Type of the sparse vector
867 {
868  return proxy.cend();
869 }
870 //*************************************************************************************************
871 
872 
873 //*************************************************************************************************
880 template< typename PT // Type of the proxy
881  , typename VT > // Type of the sparse vector
883 {
884  return proxy.size();
885 }
886 //*************************************************************************************************
887 
888 
889 //*************************************************************************************************
896 template< typename PT // Type of the proxy
897  , typename VT > // Type of the sparse vector
899 {
900  return proxy.capacity();
901 }
902 //*************************************************************************************************
903 
904 
905 //*************************************************************************************************
915 template< typename PT // Type of the proxy
916  , typename VT > // Type of the sparse vector
918 {
919  return proxy.nonZeros();
920 }
921 //*************************************************************************************************
922 
923 
924 //*************************************************************************************************
935 template< typename PT // Type of the proxy
936  , typename VT > // Type of the sparse vector
937 BLAZE_ALWAYS_INLINE void resize( const SparseVectorProxy<PT,VT>& proxy, size_t n, bool preserve )
938 {
939  proxy.resize( n, preserve );
940 }
941 //*************************************************************************************************
942 
943 
944 //*************************************************************************************************
953 template< typename PT // Type of the proxy
954  , typename VT > // Type of the sparse vector
956 {
957  proxy.reset();
958 }
959 //*************************************************************************************************
960 
961 
962 //*************************************************************************************************
971 template< typename PT // Type of the proxy
972  , typename VT > // Type of the sparse vector
974 {
975  proxy.clear();
976 }
977 //*************************************************************************************************
978 
979 } // namespace blaze
980 
981 #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
Header file for auxiliary alias declarations.
Header file for the UNUSED_PARAMETER function template.
Header file for basic type definitions.
Header file for the SparseVector base class.
Iterator end() const
Returns an iterator just past the last element of the represented vector.
Definition: SparseVectorProxy.h:271
void reset(const DiagonalProxy< MT > &proxy)
Resetting the represented element to the default initial values.
Definition: DiagonalProxy.h:588
Header file for the IsRowVector type trait.
Header file for the IsIntegral type trait.
ConstIterator cbegin() const
Returns an iterator to the first element of the represented vector.
Definition: SparseVectorProxy.h:257
void resize(size_t n, bool preserve=true) const
Changing the size of the represented vector.
Definition: SparseVectorProxy.h:490
typename T::ResultType ResultType_
Alias declaration for nested ResultType type definitions.The ResultType_ alias declaration provides a...
Definition: Aliases.h:343
TransposeType_< VT > TransposeType
Transpose type for expression template evaluations.
Definition: SparseVectorProxy.h:81
ConstIterator cend() const
Returns an iterator just past the last element of the represented vector.
Definition: SparseVectorProxy.h:285
void append(size_t index, const ElementType &value, bool check=false) const
Appending an element to the represented sparse vector.
Definition: SparseVectorProxy.h:462
typename T::ReturnType ReturnType_
Alias declaration for nested ReturnType type definitions.The ReturnType_ alias declaration provides a...
Definition: Aliases.h:363
size_t nonZeros() const
Returns the number of non-zero elements in the represented vector.
Definition: SparseVectorProxy.h:338
Reference operator[](size_t index) const
Subscript operator for the direct access to vector elements.
Definition: SparseVectorProxy.h:198
typename T::CompositeType CompositeType_
Alias declaration for nested CompositeType type definitions.The CompositeType_ alias declaration prov...
Definition: Aliases.h:83
Header file for the DisableIf class template.
Header file for the clear shim.
Namespace of the Blaze C++ math library.
Definition: Blaze.h:58
#define BLAZE_ALWAYS_INLINE
Platform dependent setup of an enforced inline keyword.
Definition: Inline.h:85
ReturnType_< VT > ReturnType
Return type for expression template evaluations.
Definition: SparseVectorProxy.h:83
Iterator upperBound(size_t index) const
Returns an iterator to the first index greater then the given index.
Definition: SparseVectorProxy.h:750
void clear() const
Clearing the represented vector.
Definition: SparseVectorProxy.h:372
Iterator find(size_t index) const
Searches for a specific vector element.
Definition: SparseVectorProxy.h:706
void erase(size_t index) const
Erasing an element from the sparse vector.
Definition: SparseVectorProxy.h:568
typename T::ElementType ElementType_
Alias declaration for nested ElementType type definitions.The ElementType_ alias declaration provides...
Definition: Aliases.h:163
ConstReference_< VT > ConstReference
Reference to a constant vector value.
Definition: SparseVectorProxy.h:86
#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
size_t capacity() const
Returns the maximum capacity of the represented vector.
Definition: SparseVectorProxy.h:321
Header file for the exception macros of the math module.
Iterator insert(size_t index, const ElementType &value) const
Inserting an element into the represented sparse vector.
Definition: SparseVectorProxy.h:424
Constraint on the data type.
typename T::Reference Reference_
Alias declaration for nested Reference type definitions.The Reference_ alias declaration provides a c...
Definition: Aliases.h:303
void clear(const DiagonalProxy< MT > &proxy)
Clearing the represented element.
Definition: DiagonalProxy.h:608
void reset() const
Reset to the default initial value.
Definition: SparseVectorProxy.h:354
ConstIterator_< VT > ConstIterator
Iterator over constant elements.
Definition: SparseVectorProxy.h:88
Iterator_< VT > Iterator
Iterator over non-constant elements.
Definition: SparseVectorProxy.h:87
void scale(const Other &scalar) const
Scaling of the sparse vector by the scalar value scalar ( ).
Definition: SparseVectorProxy.h:538
Iterator set(size_t index, const ElementType &value) const
Setting an element of the represented sparse vector.
Definition: SparseVectorProxy.h:397
ElementType_< VT > ElementType
Type of the sparse vector elements.
Definition: SparseVectorProxy.h:82
Iterator begin() const
Returns an iterator to the first element of the represented vector.
Definition: SparseVectorProxy.h:243
Header file for the reset shim.
CompositeType_< VT > CompositeType
Data type for composite expression templates.
Definition: SparseVectorProxy.h:84
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
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
void reserve(size_t n) const
Setting the minimum capacity of the represented vector.
Definition: SparseVectorProxy.h:513
size_t size() const
Returns the current size/dimension of the represented vector.
Definition: SparseVectorProxy.h:307
ResultType_< VT > ResultType
Result 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:728
Reference at(size_t index) const
Checked access to the vector elements.
Definition: SparseVectorProxy.h:225
typename T::TransposeType TransposeType_
Alias declaration for nested TransposeType type definitions.The TransposeType_ alias declaration prov...
Definition: Aliases.h:423
Reference_< VT > Reference
Reference to a non-constant vector value.
Definition: SparseVectorProxy.h:85
System settings for the inline keywords.