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 : public SparseVector< PT, IsRowVector<VT>::value >
76 {
77  public:
78  //**Type definitions****************************************************************************
88  //**********************************************************************************************
89 
90  //**Compilation flags***************************************************************************
92  enum : bool { smpAssignable = VT::smpAssignable };
93  //**********************************************************************************************
94 
95  //**Data access functions***********************************************************************
98  inline Reference operator[]( size_t index ) const;
99  inline Reference at( size_t index ) const;
100 
101  inline Iterator begin () const;
102  inline ConstIterator cbegin() const;
103  inline Iterator end () const;
104  inline ConstIterator cend () const;
106  //**********************************************************************************************
107 
108  //**Utility functions***************************************************************************
111  inline size_t size() const;
112  inline size_t capacity() const;
113  inline size_t nonZeros() const;
114  inline void reset() const;
115  inline void clear() const;
116  inline void resize( size_t n, bool preserve=true ) const;
117  inline void reserve( size_t n ) const;
119  //**********************************************************************************************
120 
121  //**Insertion functions*************************************************************************
124  inline Iterator set ( size_t index, const ElementType& value ) const;
125  inline Iterator insert( size_t index, const ElementType& value ) const;
126  inline void append( size_t index, const ElementType& value, bool check=false ) const;
128  //**********************************************************************************************
129 
130  //**Erase functions*****************************************************************************
133  inline void erase( size_t index ) const;
134  inline Iterator erase( Iterator pos ) const;
135  inline Iterator erase( Iterator first, Iterator last ) const;
136 
137  template< typename Pred, typename = DisableIf_< IsIntegral<Pred> > >
138  inline void erase( Pred predicate );
139 
140  template< typename Pred >
141  inline void erase( Iterator first, Iterator last, Pred predicate );
143  //**********************************************************************************************
144 
145  //**Lookup functions****************************************************************************
148  inline Iterator find ( size_t index ) const;
149  inline Iterator find ( size_t i, size_t j ) const;
150  inline Iterator lowerBound( size_t index ) const;
151  inline Iterator lowerBound( size_t i, size_t j ) const;
152  inline Iterator upperBound( size_t index ) const;
153  inline Iterator upperBound( size_t i, size_t j ) const;
155  //**********************************************************************************************
156 
157  //**Numeric functions***************************************************************************
160  template< typename Other > inline void scale( const Other& scalar ) const;
162  //**********************************************************************************************
163 
164  private:
165  //**Compile time checks*************************************************************************
169  //**********************************************************************************************
170 };
171 //*************************************************************************************************
172 
173 
174 
175 
176 //=================================================================================================
177 //
178 // DATA ACCESS FUNCTIONS
179 //
180 //=================================================================================================
181 
182 //*************************************************************************************************
194 template< typename PT // Type of the proxy
195  , typename VT > // Type of the sparse vector
198 {
199  if( (~*this).isRestricted() ) {
200  BLAZE_THROW_INVALID_ARGUMENT( "Invalid access to restricted element" );
201  }
202 
203  return (~*this).get()[index];
204 }
205 //*************************************************************************************************
206 
207 
208 //*************************************************************************************************
221 template< typename PT // Type of the proxy
222  , typename VT > // Type of the sparse vector
224  SparseVectorProxy<PT,VT>::at( size_t index ) const
225 {
226  if( (~*this).isRestricted() ) {
227  BLAZE_THROW_INVALID_ARGUMENT( "Invalid access to restricted element" );
228  }
229 
230  return (~*this).get().at( index );
231 }
232 //*************************************************************************************************
233 
234 
235 //*************************************************************************************************
240 template< typename PT // Type of the proxy
241  , typename VT > // Type of the sparse vector
243 {
244  return (~*this).get().begin();
245 }
246 //*************************************************************************************************
247 
248 
249 //*************************************************************************************************
254 template< typename PT // Type of the proxy
255  , typename VT > // Type of the sparse vector
257 {
258  return (~*this).get().cbegin();
259 }
260 //*************************************************************************************************
261 
262 
263 //*************************************************************************************************
268 template< typename PT // Type of the proxy
269  , typename VT > // Type of the sparse vector
271 {
272  return (~*this).get().end();
273 }
274 //*************************************************************************************************
275 
276 
277 //*************************************************************************************************
282 template< typename PT // Type of the proxy
283  , typename VT > // Type of the sparse vector
285 {
286  return (~*this).get().cend();
287 }
288 //*************************************************************************************************
289 
290 
291 
292 
293 //=================================================================================================
294 //
295 // UTILITY FUNCTIONS
296 //
297 //=================================================================================================
298 
299 //*************************************************************************************************
304 template< typename PT // Type of the proxy
305  , typename VT > // Type of the sparse vector
306 inline size_t SparseVectorProxy<PT,VT>::size() const
307 {
308  return (~*this).get().size();
309 }
310 //*************************************************************************************************
311 
312 
313 //*************************************************************************************************
318 template< typename PT // Type of the proxy
319  , typename VT > // Type of the sparse vector
321 {
322  return (~*this).get().capacity();
323 }
324 //*************************************************************************************************
325 
326 
327 //*************************************************************************************************
335 template< typename PT // Type of the proxy
336  , typename VT > // Type of the sparse vector
338 {
339  return (~*this).get().nonZeros();
340 }
341 //*************************************************************************************************
342 
343 
344 //*************************************************************************************************
351 template< typename PT // Type of the proxy
352  , typename VT > // Type of the sparse vector
354 {
355  using blaze::reset;
356 
357  reset( (~*this).get() );
358 }
359 //*************************************************************************************************
360 
361 
362 //*************************************************************************************************
369 template< typename PT // Type of the proxy
370  , typename VT > // Type of the sparse vector
372 {
373  using blaze::clear;
374 
375  clear( (~*this).get() );
376 }
377 //*************************************************************************************************
378 
379 
380 //*************************************************************************************************
393 template< typename PT // Type of the proxy
394  , typename VT > // Type of the sparse vector
396  SparseVectorProxy<PT,VT>::set( size_t index, const ElementType& value ) const
397 {
398  if( (~*this).isRestricted() ) {
399  BLAZE_THROW_INVALID_ARGUMENT( "Invalid access to restricted element" );
400  }
401 
402  return (~*this).get().set( index, value );
403 }
404 //*************************************************************************************************
405 
406 
407 //*************************************************************************************************
420 template< typename PT // Type of the proxy
421  , typename VT > // Type of the sparse vector
423  SparseVectorProxy<PT,VT>::insert( size_t index, const ElementType& value ) const
424 {
425  if( (~*this).isRestricted() ) {
426  BLAZE_THROW_INVALID_ARGUMENT( "Invalid access to restricted element" );
427  }
428 
429  return (~*this).get().insert( index, value );
430 }
431 //*************************************************************************************************
432 
433 
434 //*************************************************************************************************
459 template< typename PT // Type of the proxy
460  , typename VT > // Type of the sparse vector
461 inline void SparseVectorProxy<PT,VT>::append( size_t index, const ElementType& value, bool check ) const
462 {
463  if( (~*this).isRestricted() ) {
464  BLAZE_THROW_INVALID_ARGUMENT( "Invalid access to restricted element" );
465  }
466 
467  (~*this).get().append( index, value, check );
468 }
469 //*************************************************************************************************
470 
471 
472 //*************************************************************************************************
487 template< typename PT // Type of the proxy
488  , typename VT > // Type of the sparse vector
489 inline void SparseVectorProxy<PT,VT>::resize( size_t n, bool preserve ) const
490 {
491  if( (~*this).isRestricted() ) {
492  BLAZE_THROW_INVALID_ARGUMENT( "Invalid access to restricted element" );
493  }
494 
495  (~*this).get().resize( n, preserve );
496 }
497 //*************************************************************************************************
498 
499 
500 //*************************************************************************************************
510 template< typename PT // Type of the proxy
511  , typename VT > // Type of the sparse vector
512 inline void SparseVectorProxy<PT,VT>::reserve( size_t n ) const
513 {
514  if( (~*this).isRestricted() ) {
515  BLAZE_THROW_INVALID_ARGUMENT( "Invalid access to restricted element" );
516  }
517 
518  (~*this).get().reserve( n );
519 }
520 //*************************************************************************************************
521 
522 
523 //*************************************************************************************************
530 template< typename PT // Type of the proxy
531  , typename VT > // Type of the sparse vector
532 template< typename Other > // Data type of the scalar value
533 inline void SparseVectorProxy<PT,VT>::scale( const Other& scalar ) const
534 {
535  if( (~*this).isRestricted() ) {
536  BLAZE_THROW_INVALID_ARGUMENT( "Invalid access to restricted element" );
537  }
538 
539  (~*this).get().scale( scalar );
540 }
541 //*************************************************************************************************
542 
543 
544 
545 
546 //=================================================================================================
547 //
548 // ERASE FUNCTIONS
549 //
550 //=================================================================================================
551 
552 //*************************************************************************************************
561 template< typename PT // Type of the proxy
562  , typename VT > // Type of the sparse vector
563 inline void SparseVectorProxy<PT,VT>::erase( size_t index ) const
564 {
565  if( (~*this).isRestricted() ) {
566  BLAZE_THROW_INVALID_ARGUMENT( "Invalid access to restricted element" );
567  }
568 
569  (~*this).get().erase( index );
570 }
571 //*************************************************************************************************
572 
573 
574 //*************************************************************************************************
583 template< typename PT // Type of the proxy
584  , typename VT > // Type of the sparse vector
586 {
587  if( (~*this).isRestricted() ) {
588  BLAZE_THROW_INVALID_ARGUMENT( "Invalid access to restricted element" );
589  }
590 
591  return (~*this).get().erase( pos );
592 }
593 //*************************************************************************************************
594 
595 
596 //*************************************************************************************************
606 template< typename PT // Type of the proxy
607  , typename VT > // Type of the sparse vector
610 {
611  if( (~*this).isRestricted() ) {
612  BLAZE_THROW_INVALID_ARGUMENT( "Invalid access to restricted element" );
613  }
614 
615  return (~*this).get().erase( first, last );
616 }
617 //*************************************************************************************************
618 
619 
620 //*************************************************************************************************
633 template< typename PT // Type of the proxy
634  , typename VT > // Type of the sparse vector
635 template< typename Pred // Type of the unary predicate
636  , typename > // Type restriction on the unary predicate
637 inline void SparseVectorProxy<PT,VT>::erase( Pred predicate )
638 {
639  if( (~*this).isRestricted() ) {
640  BLAZE_THROW_INVALID_ARGUMENT( "Invalid access to restricted element" );
641  }
642 
643  (~*this).get().erase( predicate );
644 }
645 //*************************************************************************************************
646 
647 
648 //*************************************************************************************************
663 template< typename PT // Type of the proxy
664  , typename VT > // Type of the sparse vector
665 template< typename Pred > // Type of the unary predicate
666 inline void SparseVectorProxy<PT,VT>::erase( Iterator first, Iterator last, Pred predicate )
667 {
668  if( (~*this).isRestricted() ) {
669  BLAZE_THROW_INVALID_ARGUMENT( "Invalid access to restricted element" );
670  }
671 
672  (~*this).get().erase( first, last, predicate );
673 }
674 //*************************************************************************************************
675 
676 
677 
678 
679 //=================================================================================================
680 //
681 // LOOKUP FUNCTIONS
682 //
683 //=================================================================================================
684 
685 //*************************************************************************************************
698 template< typename PT // Type of the proxy
699  , typename VT > // Type of the sparse vector
701  SparseVectorProxy<PT,VT>::find( size_t index ) const
702 {
703  return (~*this).get().find( index );
704 }
705 //*************************************************************************************************
706 
707 
708 //*************************************************************************************************
720 template< typename PT // Type of the proxy
721  , typename VT > // Type of the sparse vector
724 {
725  return (~*this).get().lowerBound( index );
726 }
727 //*************************************************************************************************
728 
729 
730 //*************************************************************************************************
742 template< typename PT // Type of the proxy
743  , typename VT > // Type of the sparse vector
746 {
747  return (~*this).get().upperBound( index );
748 }
749 //*************************************************************************************************
750 
751 
752 
753 
754 //=================================================================================================
755 //
756 // GLOBAL FUNCTIONS
757 //
758 //=================================================================================================
759 
760 //*************************************************************************************************
763 template< typename PT, typename VT >
765  begin( const SparseVectorProxy<PT,VT>& proxy );
766 
767 template< typename PT, typename VT >
769  cbegin( const SparseVectorProxy<PT,VT>& proxy );
770 
771 template< typename PT, typename VT >
773  end( const SparseVectorProxy<PT,VT>& proxy );
774 
775 template< typename PT, typename VT >
777  cend( const SparseVectorProxy<PT,VT>& proxy );
778 
779 template< typename PT, typename VT >
780 BLAZE_ALWAYS_INLINE size_t size( const SparseVectorProxy<PT,VT>& proxy );
781 
782 template< typename PT, typename VT >
784 
785 template< typename PT, typename VT >
787 
788 template< typename PT, typename VT >
789 BLAZE_ALWAYS_INLINE void resize( const SparseVectorProxy<PT,VT>& proxy, size_t n, bool preserve=true );
790 
791 template< typename PT, typename VT >
793 
794 template< typename PT, typename VT >
797 //*************************************************************************************************
798 
799 
800 //*************************************************************************************************
807 template< typename PT // Type of the proxy
808  , typename VT > // Type of the sparse vector
811 {
812  return proxy.begin();
813 }
814 //*************************************************************************************************
815 
816 
817 //*************************************************************************************************
824 template< typename PT // Type of the proxy
825  , typename VT > // Type of the sparse vector
828 {
829  return proxy.cbegin();
830 }
831 //*************************************************************************************************
832 
833 
834 //*************************************************************************************************
841 template< typename PT // Type of the proxy
842  , typename VT > // Type of the sparse vector
845 {
846  return proxy.end();
847 }
848 //*************************************************************************************************
849 
850 
851 //*************************************************************************************************
858 template< typename PT // Type of the proxy
859  , typename VT > // Type of the sparse vector
862 {
863  return proxy.cend();
864 }
865 //*************************************************************************************************
866 
867 
868 //*************************************************************************************************
875 template< typename PT // Type of the proxy
876  , typename VT > // Type of the sparse vector
878 {
879  return proxy.size();
880 }
881 //*************************************************************************************************
882 
883 
884 //*************************************************************************************************
891 template< typename PT // Type of the proxy
892  , typename VT > // Type of the sparse vector
894 {
895  return proxy.capacity();
896 }
897 //*************************************************************************************************
898 
899 
900 //*************************************************************************************************
910 template< typename PT // Type of the proxy
911  , typename VT > // Type of the sparse vector
913 {
914  return proxy.nonZeros();
915 }
916 //*************************************************************************************************
917 
918 
919 //*************************************************************************************************
930 template< typename PT // Type of the proxy
931  , typename VT > // Type of the sparse vector
932 BLAZE_ALWAYS_INLINE void resize( const SparseVectorProxy<PT,VT>& proxy, size_t n, bool preserve )
933 {
934  proxy.resize( n, preserve );
935 }
936 //*************************************************************************************************
937 
938 
939 //*************************************************************************************************
948 template< typename PT // Type of the proxy
949  , typename VT > // Type of the sparse vector
951 {
952  proxy.reset();
953 }
954 //*************************************************************************************************
955 
956 
957 //*************************************************************************************************
966 template< typename PT // Type of the proxy
967  , typename VT > // Type of the sparse vector
969 {
970  proxy.clear();
971 }
972 //*************************************************************************************************
973 
974 } // namespace blaze
975 
976 #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:84
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.
CompositeType_< VT > CompositeType
Data type for composite expression templates.
Definition: SparseVectorProxy.h:83
Iterator end() const
Returns an iterator just past the last element of the represented vector.
Definition: SparseVectorProxy.h:270
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.
Header file for the IsIntegral type trait.
Iterator_< VT > Iterator
Iterator over non-constant elements.
Definition: SparseVectorProxy.h:86
ConstIterator cbegin() const
Returns an iterator to the first element of the represented vector.
Definition: SparseVectorProxy.h:256
TransposeType_< VT > TransposeType
Transpose type for expression template evaluations.
Definition: SparseVectorProxy.h:80
void resize(size_t n, bool preserve=true) const
Changing the size of the represented vector.
Definition: SparseVectorProxy.h:489
typename T::ResultType ResultType_
Alias declaration for nested ResultType type definitions.The ResultType_ alias declaration provides a...
Definition: Aliases.h:323
ConstIterator cend() const
Returns an iterator just past the last element of the represented vector.
Definition: SparseVectorProxy.h:284
void append(size_t index, const ElementType &value, bool check=false) const
Appending an element to the represented sparse vector.
Definition: SparseVectorProxy.h:461
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:82
size_t nonZeros() const
Returns the number of non-zero elements in the represented vector.
Definition: SparseVectorProxy.h:337
Reference operator[](size_t index) const
Subscript operator for the direct access to vector elements.
Definition: SparseVectorProxy.h:197
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:57
#define BLAZE_ALWAYS_INLINE
Platform dependent setup of an enforced inline keyword.
Definition: Inline.h:85
Iterator upperBound(size_t index) const
Returns an iterator to the first index greater then the given index.
Definition: SparseVectorProxy.h:745
void clear() const
Clearing the represented vector.
Definition: SparseVectorProxy.h:371
Iterator find(size_t index) const
Searches for a specific vector element.
Definition: SparseVectorProxy.h:701
ConstReference_< VT > ConstReference
Reference to a constant vector value.
Definition: SparseVectorProxy.h:85
ConstIterator_< VT > ConstIterator
Iterator over constant elements.
Definition: SparseVectorProxy.h:87
void erase(size_t index) const
Erasing an element from the sparse vector.
Definition: SparseVectorProxy.h:563
typename T::ElementType ElementType_
Alias declaration for nested ElementType type definitions.The ElementType_ alias declaration provides...
Definition: Aliases.h:163
#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:320
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:423
ResultType_< VT > ResultType
Result type for expression template evaluations.
Definition: SparseVectorProxy.h:79
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:283
void clear(const DiagonalProxy< MT > &proxy)
Clearing the represented element.
Definition: DiagonalProxy.h:553
void reset() const
Reset to the default initial value.
Definition: SparseVectorProxy.h:353
void scale(const Other &scalar) const
Scaling of the sparse vector by the scalar value scalar ( ).
Definition: SparseVectorProxy.h:533
Iterator set(size_t index, const ElementType &value) const
Setting an element of the represented sparse vector.
Definition: SparseVectorProxy.h:396
Iterator begin() const
Returns an iterator to the first element of the represented vector.
Definition: SparseVectorProxy.h:242
Header file for the reset shim.
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:81
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:512
size_t size() const
Returns the current size/dimension of the represented vector.
Definition: SparseVectorProxy.h:306
Iterator lowerBound(size_t index) const
Returns an iterator to the first index not less then the given index.
Definition: SparseVectorProxy.h:723
Reference at(size_t index) const
Checked access to the vector elements.
Definition: SparseVectorProxy.h:224
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.