Blaze  3.6
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 
55 
56 namespace blaze {
57 
58 //=================================================================================================
59 //
60 // CLASS DEFINITION
61 //
62 //=================================================================================================
63 
64 //*************************************************************************************************
72 template< typename PT // Type of the proxy
73  , typename VT > // Type of the sparse vector
74 class SparseVectorProxy
75  : public SparseVector< PT, IsRowVector_v<VT> >
76 {
77  public:
78  //**Type definitions****************************************************************************
88  //**********************************************************************************************
89 
90  //**Compilation flags***************************************************************************
92  static constexpr 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_t< IsIntegral_v<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 //*************************************************************************************************
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 // ERASE FUNCTIONS
553 //
554 //=================================================================================================
555 
556 //*************************************************************************************************
565 template< typename PT // Type of the proxy
566  , typename VT > // Type of the sparse vector
567 inline void SparseVectorProxy<PT,VT>::erase( size_t index ) const
568 {
569  if( (~*this).isRestricted() ) {
570  BLAZE_THROW_INVALID_ARGUMENT( "Invalid access to restricted element" );
571  }
572 
573  (~*this).get().erase( index );
574 }
575 //*************************************************************************************************
576 
577 
578 //*************************************************************************************************
587 template< typename PT // Type of the proxy
588  , typename VT > // Type of the sparse vector
590 {
591  if( (~*this).isRestricted() ) {
592  BLAZE_THROW_INVALID_ARGUMENT( "Invalid access to restricted element" );
593  }
594 
595  return (~*this).get().erase( pos );
596 }
597 //*************************************************************************************************
598 
599 
600 //*************************************************************************************************
610 template< typename PT // Type of the proxy
611  , typename VT > // Type of the sparse vector
614 {
615  if( (~*this).isRestricted() ) {
616  BLAZE_THROW_INVALID_ARGUMENT( "Invalid access to restricted element" );
617  }
618 
619  return (~*this).get().erase( first, last );
620 }
621 //*************************************************************************************************
622 
623 
624 //*************************************************************************************************
637 template< typename PT // Type of the proxy
638  , typename VT > // Type of the sparse vector
639 template< typename Pred // Type of the unary predicate
640  , typename > // Type restriction on the unary predicate
641 inline void SparseVectorProxy<PT,VT>::erase( Pred predicate )
642 {
643  if( (~*this).isRestricted() ) {
644  BLAZE_THROW_INVALID_ARGUMENT( "Invalid access to restricted element" );
645  }
646 
647  (~*this).get().erase( predicate );
648 }
649 //*************************************************************************************************
650 
651 
652 //*************************************************************************************************
667 template< typename PT // Type of the proxy
668  , typename VT > // Type of the sparse vector
669 template< typename Pred > // Type of the unary predicate
670 inline void SparseVectorProxy<PT,VT>::erase( Iterator first, Iterator last, Pred predicate )
671 {
672  if( (~*this).isRestricted() ) {
673  BLAZE_THROW_INVALID_ARGUMENT( "Invalid access to restricted element" );
674  }
675 
676  (~*this).get().erase( first, last, predicate );
677 }
678 //*************************************************************************************************
679 
680 
681 
682 
683 //=================================================================================================
684 //
685 // LOOKUP FUNCTIONS
686 //
687 //=================================================================================================
688 
689 //*************************************************************************************************
702 template< typename PT // Type of the proxy
703  , typename VT > // Type of the sparse vector
705  SparseVectorProxy<PT,VT>::find( size_t index ) const
706 {
707  return (~*this).get().find( index );
708 }
709 //*************************************************************************************************
710 
711 
712 //*************************************************************************************************
724 template< typename PT // Type of the proxy
725  , typename VT > // Type of the sparse vector
728 {
729  return (~*this).get().lowerBound( index );
730 }
731 //*************************************************************************************************
732 
733 
734 //*************************************************************************************************
746 template< typename PT // Type of the proxy
747  , typename VT > // Type of the sparse vector
750 {
751  return (~*this).get().upperBound( index );
752 }
753 //*************************************************************************************************
754 
755 
756 
757 
758 //=================================================================================================
759 //
760 // GLOBAL FUNCTIONS
761 //
762 //=================================================================================================
763 
764 //*************************************************************************************************
767 template< typename PT, typename VT >
769  begin( const SparseVectorProxy<PT,VT>& proxy );
770 
771 template< typename PT, typename VT >
773  cbegin( const SparseVectorProxy<PT,VT>& proxy );
774 
775 template< typename PT, typename VT >
777  end( const SparseVectorProxy<PT,VT>& proxy );
778 
779 template< typename PT, typename VT >
781  cend( const SparseVectorProxy<PT,VT>& proxy );
782 
783 template< typename PT, typename VT >
784 size_t size( const SparseVectorProxy<PT,VT>& proxy );
785 
786 template< typename PT, typename VT >
787 size_t capacity( const SparseVectorProxy<PT,VT>& proxy );
788 
789 template< typename PT, typename VT >
790 size_t nonZeros( const SparseVectorProxy<PT,VT>& proxy );
791 
792 template< typename PT, typename VT >
793 void resize( const SparseVectorProxy<PT,VT>& proxy, size_t n, bool preserve=true );
794 
795 template< typename PT, typename VT >
796 void reset( const SparseVectorProxy<PT,VT>& proxy );
797 
798 template< typename PT, typename VT >
799 void clear( const SparseVectorProxy<PT,VT>& proxy );
801 //*************************************************************************************************
802 
803 
804 //*************************************************************************************************
811 template< typename PT // Type of the proxy
812  , typename VT > // Type of the sparse vector
815 {
816  return proxy.begin();
817 }
818 //*************************************************************************************************
819 
820 
821 //*************************************************************************************************
828 template< typename PT // Type of the proxy
829  , typename VT > // Type of the sparse vector
832 {
833  return proxy.cbegin();
834 }
835 //*************************************************************************************************
836 
837 
838 //*************************************************************************************************
845 template< typename PT // Type of the proxy
846  , typename VT > // Type of the sparse vector
849 {
850  return proxy.end();
851 }
852 //*************************************************************************************************
853 
854 
855 //*************************************************************************************************
862 template< typename PT // Type of the proxy
863  , typename VT > // Type of the sparse vector
866 {
867  return proxy.cend();
868 }
869 //*************************************************************************************************
870 
871 
872 //*************************************************************************************************
879 template< typename PT // Type of the proxy
880  , typename VT > // Type of the sparse vector
882 {
883  return proxy.size();
884 }
885 //*************************************************************************************************
886 
887 
888 //*************************************************************************************************
895 template< typename PT // Type of the proxy
896  , typename VT > // Type of the sparse vector
898 {
899  return proxy.capacity();
900 }
901 //*************************************************************************************************
902 
903 
904 //*************************************************************************************************
914 template< typename PT // Type of the proxy
915  , typename VT > // Type of the sparse vector
917 {
918  return proxy.nonZeros();
919 }
920 //*************************************************************************************************
921 
922 
923 //*************************************************************************************************
934 template< typename PT // Type of the proxy
935  , typename VT > // Type of the sparse vector
936 BLAZE_ALWAYS_INLINE void resize( const SparseVectorProxy<PT,VT>& proxy, size_t n, bool preserve )
937 {
938  proxy.resize( n, preserve );
939 }
940 //*************************************************************************************************
941 
942 
943 //*************************************************************************************************
952 template< typename PT // Type of the proxy
953  , typename VT > // Type of the sparse vector
955 {
956  proxy.reset();
957 }
958 //*************************************************************************************************
959 
960 
961 //*************************************************************************************************
970 template< typename PT // Type of the proxy
971  , typename VT > // Type of the sparse vector
973 {
974  proxy.clear();
975 }
976 //*************************************************************************************************
977 
978 } // namespace blaze
979 
980 #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.
size_t capacity(const Matrix< MT, SO > &matrix) noexcept
Returns the maximum capacity of the matrix.
Definition: Matrix.h:546
Header file for basic type definitions.
Header file for the SparseVector base class.
typename T::ResultType ResultType_t
Alias declaration for nested ResultType type definitions.The ResultType_t alias declaration provides ...
Definition: Aliases.h:390
typename T::Reference Reference_t
Alias declaration for nested Reference type definitions.The Reference_t alias declaration provides a ...
Definition: Aliases.h:330
MT::Iterator begin(Matrix< MT, SO > &matrix, size_t i)
Returns an iterator to the first element of row/column i.
Definition: Matrix.h:372
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:595
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:256
size_t nonZeros(const Matrix< MT, SO > &matrix)
Returns the total number of non-zero elements in the matrix.
Definition: Matrix.h:584
Header file for the reset shim.
ElementType_t< VT > ElementType
Type of the sparse vector elements.
Definition: SparseVectorProxy.h:81
typename T::ReturnType ReturnType_t
Alias declaration for nested ReturnType type definitions.The ReturnType_t alias declaration provides ...
Definition: Aliases.h:410
void resize(size_t n, bool preserve=true) const
Changing the size of the represented vector.
Definition: SparseVectorProxy.h:489
ConstIterator_t< VT > ConstIterator
Iterator over constant elements.
Definition: SparseVectorProxy.h:87
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:482
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:416
typename T::ElementType ElementType_t
Alias declaration for nested ElementType type definitions.The ElementType_t alias declaration provide...
Definition: Aliases.h:170
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
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
Header file for the DisableIf class template.
Reference_t< VT > Reference
Reference to a non-constant vector value.
Definition: SparseVectorProxy.h:84
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
Iterator upperBound(size_t index) const
Returns an iterator to the first index greater then the given index.
Definition: SparseVectorProxy.h:749
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:705
ReturnType_t< VT > ReturnType
Return type for expression template evaluations.
Definition: SparseVectorProxy.h:82
void erase(size_t index) const
Erasing an element from the sparse vector.
Definition: SparseVectorProxy.h:567
ResultType_t< VT > ResultType
Result type for expression template evaluations.
Definition: SparseVectorProxy.h:79
#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
void resize(Matrix< MT, SO > &matrix, size_t rows, size_t columns, bool preserve=true)
Changing the size of the matrix.
Definition: Matrix.h:738
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:438
Constraint on the data type.
void clear(const DiagonalProxy< MT > &proxy)
Clearing the represented element.
Definition: DiagonalProxy.h:615
ConstReference_t< VT > ConstReference
Reference to a constant vector value.
Definition: SparseVectorProxy.h:85
void reset() const
Reset to the default initial value.
Definition: SparseVectorProxy.h:353
Iterator_t< VT > Iterator
Iterator over non-constant elements.
Definition: SparseVectorProxy.h:86
typename T::Iterator Iterator_t
Alias declaration for nested Iterator type definitions.The Iterator_t alias declaration provides a co...
Definition: Aliases.h:190
typename T::TransposeType TransposeType_t
Alias declaration for nested TransposeType type definitions.The TransposeType_t alias declaration pro...
Definition: Aliases.h:470
void scale(const Other &scalar) const
Scaling of the sparse vector by the scalar value scalar ( ).
Definition: SparseVectorProxy.h:537
typename T::CompositeType CompositeType_t
Alias declaration for nested CompositeType type definitions.The CompositeType_t alias declaration pro...
Definition: Aliases.h:90
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
constexpr size_t size(const Matrix< MT, SO > &matrix) noexcept
Returns the total number of elements of the matrix.
Definition: Matrix.h:530
typename T::ConstReference ConstReference_t
Alias declaration for nested ConstReference type definitions.The ConstReference_t alias declaration p...
Definition: Aliases.h:150
Proxy backend for sparse vector types.The SparseVectorProxy class serves as a backend for the Proxy c...
Definition: Forward.h:53
typename T::ConstIterator ConstIterator_t
Alias declaration for nested ConstIterator type definitions.The ConstIterator_t alias declaration pro...
Definition: Aliases.h:110
CompositeType_t< VT > CompositeType
Data type for composite expression templates.
Definition: SparseVectorProxy.h:83
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:727
Reference at(size_t index) const
Checked access to the vector elements.
Definition: SparseVectorProxy.h:224
System settings for the inline keywords.
static constexpr bool smpAssignable
Compilation flag for SMP assignments.
Definition: SparseVectorProxy.h:92
Header file for the clear shim.
TransposeType_t< VT > TransposeType
Transpose type for expression template evaluations.
Definition: SparseVectorProxy.h:80
constexpr Type & get(StaticVector< Type, N, TF > &v) noexcept
Tuple-like index-based access the contents of a static vector.
Definition: StaticVector.h:2704