MatrixAccessProxy.h
Go to the documentation of this file.
1 //=================================================================================================
33 //=================================================================================================
34 
35 #ifndef _BLAZE_MATH_SPARSE_MATRIXACCESSPROXY_H_
36 #define _BLAZE_MATH_SPARSE_MATRIXACCESSPROXY_H_
37 
38 
39 //*************************************************************************************************
40 // Includes
41 //*************************************************************************************************
42 
43 #include <algorithm>
44 #include <ostream>
46 #include <blaze/math/proxy/Proxy.h>
47 #include <blaze/math/shims/Clear.h>
49 #include <blaze/math/shims/Reset.h>
51 #include <blaze/util/Assert.h>
52 #include <blaze/util/Types.h>
53 
54 
55 namespace blaze {
56 
57 //=================================================================================================
58 //
59 // CLASS DEFINITION
60 //
61 //=================================================================================================
62 
63 //*************************************************************************************************
94 template< typename MT > // Type of the sparse matrix
95 class MatrixAccessProxy : public Proxy< MatrixAccessProxy<MT>, typename MT::ElementType >
96 {
97  private:
98  //**Enumerations********************************************************************************
100  enum { rmm = IsRowMajorMatrix<MT>::value };
101  //**********************************************************************************************
102 
103  public:
104  //**Type definitions****************************************************************************
105  typedef typename MT::ElementType RepresentedType;
106  typedef RepresentedType& RawReference;
107  //**********************************************************************************************
108 
109  //**Constructors********************************************************************************
112  explicit inline MatrixAccessProxy( MT& sm, size_t i, size_t j );
113  inline MatrixAccessProxy( const MatrixAccessProxy& map );
115  //**********************************************************************************************
116 
117  //**Destructor**********************************************************************************
120  inline ~MatrixAccessProxy();
122  //**********************************************************************************************
123 
124  //**Operators***********************************************************************************
127  inline const MatrixAccessProxy& operator= ( const MatrixAccessProxy& map ) const;
128  template< typename T > inline const MatrixAccessProxy& operator= ( const T& value ) const;
129  template< typename T > inline const MatrixAccessProxy& operator+=( const T& value ) const;
130  template< typename T > inline const MatrixAccessProxy& operator-=( const T& value ) const;
131  template< typename T > inline const MatrixAccessProxy& operator*=( const T& value ) const;
132  template< typename T > inline const MatrixAccessProxy& operator/=( const T& value ) const;
134  //**********************************************************************************************
135 
136  //**Utility functions***************************************************************************
139  inline RawReference get() const;
140  inline bool isRestricted() const;
142  //**********************************************************************************************
143 
144  //**Conversion operator*************************************************************************
147  inline operator RawReference() const;
149  //**********************************************************************************************
150 
151  private:
152  //**Member variables****************************************************************************
155  MT& sm_;
156  size_t i_;
157  size_t j_;
158 
159  //**********************************************************************************************
160 
161  //**Forbidden operations************************************************************************
164  void* operator&() const;
165 
166  //**********************************************************************************************
167 
168  //**Compile time checks*************************************************************************
172  //**********************************************************************************************
173 };
174 //*************************************************************************************************
175 
176 
177 
178 
179 //=================================================================================================
180 //
181 // CONSTRUCTORS
182 //
183 //=================================================================================================
184 
185 //*************************************************************************************************
192 template< typename MT > // Type of the sparse matrix
193 inline MatrixAccessProxy<MT>::MatrixAccessProxy( MT& sm, size_t i, size_t j )
194  : sm_( sm ) // Reference to the accessed sparse matrix
195  , i_ ( i ) // Row-index of the accessed sparse matrix element
196  , j_ ( j ) // Column-index of the accessed sparse matrix element
197 {
198  const typename MT::Iterator element( sm_.find( i_, j_ ) );
199  const size_t index( rmm ? i_ : j_ );
200  if( element == sm_.end(index) )
201  sm_.insert( i_, j_, RepresentedType() );
202 }
203 //*************************************************************************************************
204 
205 
206 //*************************************************************************************************
211 template< typename MT > // Type of the sparse matrix
213  : sm_( map.sm_ ) // Reference to the accessed sparse matrix
214  , i_ ( map.i_ ) // Row-index of the accessed sparse matrix element
215  , j_ ( map.j_ ) // Column-index of the accessed sparse matrix element
216 {
217  BLAZE_INTERNAL_ASSERT( sm_.find(i_,j_) != sm_.end( rmm ? i_ : j_ ), "Missing matrix element detected" );
218 }
219 //*************************************************************************************************
220 
221 
222 
223 
224 //=================================================================================================
225 //
226 // DESTRUCTORS
227 //
228 //=================================================================================================
229 
230 //*************************************************************************************************
233 template< typename MT > // Type of the sparse matrix
235 {
236  const typename MT::Iterator element( sm_.find( i_, j_ ) );
237  const size_t index( rmm ? i_ : j_ );
238  if( element != sm_.end( index ) && isDefault( element->value() ) )
239  sm_.erase( index, element );
240 }
241 //*************************************************************************************************
242 
243 
244 
245 
246 //=================================================================================================
247 //
248 // OPERATORS
249 //
250 //=================================================================================================
251 
252 //*************************************************************************************************
258 template< typename MT > // Type of the sparse matrix
260 {
261  get() = map.get();
262  return *this;
263 }
264 //*************************************************************************************************
265 
266 
267 //*************************************************************************************************
273 template< typename MT > // Type of the sparse matrix
274 template< typename T > // Type of the right-hand side value
275 inline const MatrixAccessProxy<MT>& MatrixAccessProxy<MT>::operator=( const T& value ) const
276 {
277  get() = value;
278  return *this;
279 }
280 //*************************************************************************************************
281 
282 
283 //*************************************************************************************************
289 template< typename MT > // Type of the sparse matrix
290 template< typename T > // Type of the right-hand side value
291 inline const MatrixAccessProxy<MT>& MatrixAccessProxy<MT>::operator+=( const T& value ) const
292 {
293  get() += value;
294  return *this;
295 }
296 //*************************************************************************************************
297 
298 
299 //*************************************************************************************************
305 template< typename MT > // Type of the sparse matrix
306 template< typename T > // Type of the right-hand side value
307 inline const MatrixAccessProxy<MT>& MatrixAccessProxy<MT>::operator-=( const T& value ) const
308 {
309  get() -= value;
310  return *this;
311 }
312 //*************************************************************************************************
313 
314 
315 //*************************************************************************************************
321 template< typename MT > // Type of the sparse matrix
322 template< typename T > // Type of the right-hand side value
323 inline const MatrixAccessProxy<MT>& MatrixAccessProxy<MT>::operator*=( const T& value ) const
324 {
325  get() *= value;
326  return *this;
327 }
328 //*************************************************************************************************
329 
330 
331 //*************************************************************************************************
337 template< typename MT > // Type of the sparse matrix
338 template< typename T > // Type of the right-hand side value
339 inline const MatrixAccessProxy<MT>& MatrixAccessProxy<MT>::operator/=( const T& value ) const
340 {
341  get() /= value;
342  return *this;
343 }
344 //*************************************************************************************************
345 
346 
347 
348 
349 //=================================================================================================
350 //
351 // UTILITY FUNCTIONS
352 //
353 //=================================================================================================
354 
355 //*************************************************************************************************
360 template< typename MT > // Type of the sparse matrix
362 {
363  const typename MT::Iterator element( sm_.find( i_, j_ ) );
364  BLAZE_INTERNAL_ASSERT( element != sm_.end( rmm ? i_ : j_ ), "Missing matrix element detected" );
365  return element->value();
366 }
367 //*************************************************************************************************
368 
369 
370 //*************************************************************************************************
375 template< typename MT > // Type of the sparse matrix
377 {
378  return false;
379 }
380 //*************************************************************************************************
381 
382 
383 
384 
385 //=================================================================================================
386 //
387 // CONVERSION OPERATOR
388 //
389 //=================================================================================================
390 
391 //*************************************************************************************************
396 template< typename MT > // Type of the sparse matrix
398 {
399  return get();
400 }
401 //*************************************************************************************************
402 
403 
404 
405 
406 //=================================================================================================
407 //
408 // GLOBAL OPERATORS
409 //
410 //=================================================================================================
411 
412 //*************************************************************************************************
415 template< typename MT1, typename MT2 >
416 inline bool operator==( const MatrixAccessProxy<MT1>& lhs, const MatrixAccessProxy<MT2>& rhs );
417 
418 template< typename MT, typename T >
419 inline bool operator==( const MatrixAccessProxy<MT>& lhs, const T& rhs );
420 
421 template< typename T, typename MT >
422 inline bool operator==( const T& lhs, const MatrixAccessProxy<MT>& rhs );
423 
424 template< typename MT1, typename MT2 >
425 inline bool operator!=( const MatrixAccessProxy<MT1>& lhs, const MatrixAccessProxy<MT2>& rhs );
426 
427 template< typename MT, typename T >
428 inline bool operator!=( const MatrixAccessProxy<MT>& lhs, const T& rhs );
429 
430 template< typename T, typename MT >
431 inline bool operator!=( const T& lhs, const MatrixAccessProxy<MT>& rhs );
432 
433 template< typename MT1, typename MT2 >
434 inline bool operator<( const MatrixAccessProxy<MT1>& lhs, const MatrixAccessProxy<MT2>& rhs );
435 
436 template< typename MT, typename T >
437 inline bool operator<( const MatrixAccessProxy<MT>& lhs, const T& rhs );
438 
439 template< typename T, typename MT >
440 inline bool operator<( const T& lhs, const MatrixAccessProxy<MT>& rhs );
441 
442 template< typename MT1, typename MT2 >
443 inline bool operator>( const MatrixAccessProxy<MT1>& lhs, const MatrixAccessProxy<MT2>& rhs );
444 
445 template< typename MT, typename T >
446 inline bool operator>( const MatrixAccessProxy<MT>& lhs, const T& rhs );
447 
448 template< typename T, typename MT >
449 inline bool operator>( const T& lhs, const MatrixAccessProxy<MT>& rhs );
450 
451 template< typename MT1, typename MT2 >
452 inline bool operator<=( const MatrixAccessProxy<MT1>& lhs, const MatrixAccessProxy<MT2>& rhs );
453 
454 template< typename MT, typename T >
455 inline bool operator<=( const MatrixAccessProxy<MT>& lhs, const T& rhs );
456 
457 template< typename T, typename MT >
458 inline bool operator<=( const T& lhs, const MatrixAccessProxy<MT>& rhs );
459 
460 template< typename MT1, typename MT2 >
461 inline bool operator>=( const MatrixAccessProxy<MT1>& lhs, const MatrixAccessProxy<MT2>& rhs );
462 
463 template< typename MT, typename T >
464 inline bool operator>=( const MatrixAccessProxy<MT>& lhs, const T& rhs );
465 
466 template< typename T, typename MT >
467 inline bool operator>=( const T& lhs, const MatrixAccessProxy<MT>& rhs );
468 
469 template< typename MT >
470 inline std::ostream& operator<<( std::ostream& os, const MatrixAccessProxy<MT>& proxy );
472 //*************************************************************************************************
473 
474 
475 //*************************************************************************************************
483 template< typename MT1, typename MT2 >
484 inline bool operator==( const MatrixAccessProxy<MT1>& lhs, const MatrixAccessProxy<MT2>& rhs )
485 {
486  return ( lhs.get() == rhs.get() );
487 }
488 //*************************************************************************************************
489 
490 
491 //*************************************************************************************************
499 template< typename MT, typename T >
500 inline bool operator==( const MatrixAccessProxy<MT>& lhs, const T& rhs )
501 {
502  return ( lhs.get() == rhs );
503 }
504 //*************************************************************************************************
505 
506 
507 //*************************************************************************************************
515 template< typename T, typename MT >
516 inline bool operator==( const T& lhs, const MatrixAccessProxy<MT>& rhs )
517 {
518  return ( lhs == rhs.get() );
519 }
520 //*************************************************************************************************
521 
522 
523 //*************************************************************************************************
531 template< typename MT1, typename MT2 >
532 inline bool operator!=( const MatrixAccessProxy<MT1>& lhs, const MatrixAccessProxy<MT2>& rhs )
533 {
534  return ( lhs.get() != rhs.get() );
535 }
536 //*************************************************************************************************
537 
538 
539 //*************************************************************************************************
547 template< typename MT, typename T >
548 inline bool operator!=( const MatrixAccessProxy<MT>& lhs, const T& rhs )
549 {
550  return ( lhs.get() != rhs );
551 }
552 //*************************************************************************************************
553 
554 
555 //*************************************************************************************************
563 template< typename T, typename MT >
564 inline bool operator!=( const T& lhs, const MatrixAccessProxy<MT>& rhs )
565 {
566  return ( lhs != rhs.get() );
567 }
568 //*************************************************************************************************
569 
570 
571 //*************************************************************************************************
579 template< typename MT1, typename MT2 >
580 inline bool operator<( const MatrixAccessProxy<MT1>& lhs, const MatrixAccessProxy<MT2>& rhs )
581 {
582  return ( lhs.get() < rhs.get() );
583 }
584 //*************************************************************************************************
585 
586 
587 //*************************************************************************************************
595 template< typename MT, typename T >
596 inline bool operator<( const MatrixAccessProxy<MT>& lhs, const T& rhs )
597 {
598  return ( lhs.get() < rhs );
599 }
600 //*************************************************************************************************
601 
602 
603 //*************************************************************************************************
611 template< typename T, typename MT >
612 inline bool operator<( const T& lhs, const MatrixAccessProxy<MT>& rhs )
613 {
614  return ( lhs < rhs.get() );
615 }
616 //*************************************************************************************************
617 
618 
619 //*************************************************************************************************
627 template< typename MT1, typename MT2 >
628 inline bool operator>( const MatrixAccessProxy<MT1>& lhs, const MatrixAccessProxy<MT2>& rhs )
629 {
630  return ( lhs.get() > rhs.get() );
631 }
632 //*************************************************************************************************
633 
634 
635 //*************************************************************************************************
643 template< typename MT, typename T >
644 inline bool operator>( const MatrixAccessProxy<MT>& lhs, const T& rhs )
645 {
646  return ( lhs.get() > rhs );
647 }
648 //*************************************************************************************************
649 
650 
651 //*************************************************************************************************
659 template< typename T, typename MT >
660 inline bool operator>( const T& lhs, const MatrixAccessProxy<MT>& rhs )
661 {
662  return ( lhs > rhs.get() );
663 }
664 //*************************************************************************************************
665 
666 
667 //*************************************************************************************************
675 template< typename MT1, typename MT2 >
676 inline bool operator<=( const MatrixAccessProxy<MT1>& lhs, const MatrixAccessProxy<MT2>& rhs )
677 {
678  return ( lhs.get() <= rhs.get() );
679 }
680 //*************************************************************************************************
681 
682 
683 //*************************************************************************************************
691 template< typename MT, typename T >
692 inline bool operator<=( const MatrixAccessProxy<MT>& lhs, const T& rhs )
693 {
694  return ( lhs.get() <= rhs );
695 }
696 //*************************************************************************************************
697 
698 
699 //*************************************************************************************************
707 template< typename T, typename MT >
708 inline bool operator<=( const T& lhs, const MatrixAccessProxy<MT>& rhs )
709 {
710  return ( lhs <= rhs.get() );
711 }
712 //*************************************************************************************************
713 
714 
715 //*************************************************************************************************
723 template< typename MT1, typename MT2 >
724 inline bool operator>=( const MatrixAccessProxy<MT1>& lhs, const MatrixAccessProxy<MT2>& rhs )
725 {
726  return ( lhs.get() >= rhs.get() );
727 }
728 //*************************************************************************************************
729 
730 
731 //*************************************************************************************************
739 template< typename MT, typename T >
740 inline bool operator>=( const MatrixAccessProxy<MT>& lhs, const T& rhs )
741 {
742  return ( lhs.get() >= rhs );
743 }
744 //*************************************************************************************************
745 
746 
747 //*************************************************************************************************
755 template< typename T, typename MT >
756 inline bool operator>=( const T& lhs, const MatrixAccessProxy<MT>& rhs )
757 {
758  return ( lhs >= rhs.get() );
759 }
760 //*************************************************************************************************
761 
762 
763 //*************************************************************************************************
771 template< typename MT >
772 inline std::ostream& operator<<( std::ostream& os, const MatrixAccessProxy<MT>& proxy )
773 {
774  return os << proxy.get();
775 }
776 //*************************************************************************************************
777 
778 
779 
780 
781 //=================================================================================================
782 //
783 // GLOBAL FUNCTIONS
784 //
785 //=================================================================================================
786 
787 //*************************************************************************************************
790 template< typename MT >
791 inline void reset( const MatrixAccessProxy<MT>& proxy );
792 
793 template< typename MT >
794 inline void clear( const MatrixAccessProxy<MT>& proxy );
795 
796 template< typename MT >
797 inline bool isDefault( const MatrixAccessProxy<MT>& proxy );
798 
799 template< typename MT >
800 inline void swap( const MatrixAccessProxy<MT>& a, const MatrixAccessProxy<MT>& b ) /* throw() */;
801 
802 template< typename MT, typename T >
803 inline void swap( const MatrixAccessProxy<MT>& a, T& b ) /* throw() */;
804 
805 template< typename T, typename MT >
806 inline void swap( T& a, const MatrixAccessProxy<MT>& v ) /* throw() */;
808 //*************************************************************************************************
809 
810 
811 //*************************************************************************************************
823 template< typename MT >
824 inline void reset( const MatrixAccessProxy<MT>& proxy )
825 {
826  using blaze::reset;
827 
828  reset( proxy.get() );
829 }
830 //*************************************************************************************************
831 
832 
833 //*************************************************************************************************
844 template< typename MT >
845 inline void clear( const MatrixAccessProxy<MT>& proxy )
846 {
847  using blaze::clear;
848 
849  clear( proxy.get() );
850 }
851 //*************************************************************************************************
852 
853 
854 //*************************************************************************************************
864 template< typename MT >
865 inline bool isDefault( const MatrixAccessProxy<MT>& proxy )
866 {
867  using blaze::isDefault;
868 
869  return isDefault( proxy.get() );
870 }
871 //*************************************************************************************************
872 
873 
874 //*************************************************************************************************
883 template< typename MT >
884 inline void swap( const MatrixAccessProxy<MT>& a, const MatrixAccessProxy<MT>& b ) /* throw() */
885 {
886  using std::swap;
887 
888  swap( a.get(), b.get() );
889 }
890 //*************************************************************************************************
891 
892 
893 //*************************************************************************************************
902 template< typename MT, typename T >
903 inline void swap( const MatrixAccessProxy<MT>& a, T& b ) /* throw() */
904 {
905  using std::swap;
906 
907  swap( a.get(), b );
908 }
909 //*************************************************************************************************
910 
911 
912 //*************************************************************************************************
921 template< typename T, typename MT >
922 inline void swap( T& a, const MatrixAccessProxy<MT>& b ) /* throw() */
923 {
924  using std::swap;
925 
926  swap( a, b.get() );
927 }
928 //*************************************************************************************************
929 
930 } // namespace blaze
931 
932 #endif
size_t i_
Row-index of the accessed sparse matrix element.
Definition: MatrixAccessProxy.h:156
bool isRestricted() const
Returns whether the proxy represents a restricted sparse matrix element..
Definition: MatrixAccessProxy.h:376
Header file for basic type definitions.
Proxy base class.The Proxy class is a base class for all proxy classes within the Blaze library that ...
Definition: Proxy.h:99
void reset(const DiagonalProxy< MT > &proxy)
Resetting the represented element to the default initial values.
Definition: DiagonalProxy.h:821
void * operator&() const
Address operator (private & undefined)
bool operator>(const NegativeAccuracy< A > &lhs, const T &rhs)
Greater-than comparison between a NegativeAccuracy object and a floating point value.
Definition: Accuracy.h:366
bool operator>=(const NegativeAccuracy< A > &, const T &rhs)
Greater-or-equal-than comparison between a NegativeAccuracy object and a floating point value...
Definition: Accuracy.h:442
Access proxy for sparse, matrices.The MatrixAccessProxy provides safe access to the elements of a no...
Definition: MatrixAccessProxy.h:95
bool isDefault(const DiagonalProxy< MT > &proxy)
Returns whether the represented element is in default state.
Definition: DiagonalProxy.h:861
MT::ElementType RepresentedType
Type of the represented sparse matrix element.
Definition: MatrixAccessProxy.h:105
Constraint on the data type.
Header file for the clear shim.
Namespace of the Blaze C++ math library.
Definition: Blaze.h:57
const MatrixAccessProxy & operator=(const MatrixAccessProxy &map) const
Copy assignment operator for MatrixAccessProxy.
Definition: MatrixAccessProxy.h:259
Compile time check for row-major matrix types.This type trait tests whether or not the given template...
Definition: IsRowMajorMatrix.h:104
void swap(CompressedMatrix< Type, SO > &a, CompressedMatrix< Type, SO > &b)
Swapping the contents of two compressed matrices.
Definition: CompressedMatrix.h:4807
MatrixAccessProxy(MT &sm, size_t i, size_t j)
Initialization constructor for a MatrixAccessProxy.
Definition: MatrixAccessProxy.h:193
RawReference get() const
Returning the value of the accessed sparse matrix element.
Definition: MatrixAccessProxy.h:361
Type ElementType
Type of the sparse matrix elements.
Definition: CompressedMatrix.h:2505
void clear(const DiagonalProxy< MT > &proxy)
Clearing the represented element.
Definition: DiagonalProxy.h:841
~MatrixAccessProxy()
The destructor for MatrixAccessProxy.
Definition: MatrixAccessProxy.h:234
Header file for run time assertion macros.
Header file for the Proxy class.
MT & sm_
Reference to the accessed sparse matrix.
Definition: MatrixAccessProxy.h:155
Header file for the reset shim.
Element * Iterator
Iterator over non-constant elements.
Definition: CompressedMatrix.h:2510
Header file for the isDefault shim.
void swap(DiagonalMatrix< MT, SO, DF > &a, DiagonalMatrix< MT, SO, DF > &b)
Swapping the contents of two matrices.
Definition: DiagonalMatrix.h:200
size_t j_
Column-index of the accessed sparse matrix element.
Definition: MatrixAccessProxy.h:157
Header file for the IsRowMajorMatrix type trait.
RepresentedType & RawReference
Raw reference to the represented element.
Definition: MatrixAccessProxy.h:106
bool operator==(const NegativeAccuracy< A > &lhs, const T &rhs)
Equality comparison between a NegativeAccuracy object and a floating point value. ...
Definition: Accuracy.h:249
bool operator!=(const NegativeAccuracy< A > &lhs, const T &rhs)
Inequality comparison between a NegativeAccuracy object and a floating point value.
Definition: Accuracy.h:289
#define BLAZE_INTERNAL_ASSERT(expr, msg)
Run time assertion macro for internal checks.In case of an invalid run time expression, the program execution is terminated. The BLAZE_INTERNAL_ASSERT macro can be disabled by setting the BLAZE_USER_ASSERTION flag to zero or by defining NDEBUG during the compilation.
Definition: Assert.h:101
#define BLAZE_CONSTRAINT_MUST_BE_SPARSE_MATRIX_TYPE(T)
Constraint on the data type.In case the given data type T is not a sparse, N-dimensional matrix type...
Definition: SparseMatrix.h:79