All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
44 #include <blaze/util/Types.h>
45 
46 
47 namespace blaze {
48 
49 //=================================================================================================
50 //
51 // CLASS DEFINITION
52 //
53 //=================================================================================================
54 
55 //*************************************************************************************************
85 template< typename MT > // Type of the sparse matrix
87 {
88  private:
89  //**Enumerations********************************************************************************
91  enum { rmm = IsRowMajorMatrix<MT>::value };
92  //**********************************************************************************************
93 
94  public:
95  //**Type definitions****************************************************************************
96  typedef MT MatrixType;
97  typedef typename MT::ElementType ElementType;
99  typedef const ElementType& ConstReference;
100  typedef typename MT::Iterator Iterator;
101  //**********************************************************************************************
102 
103  //**Constructors********************************************************************************
106  explicit inline MatrixAccessProxy( MT& sv, size_t i, size_t j );
107  inline MatrixAccessProxy( const MatrixAccessProxy& vap );
109  //**********************************************************************************************
110 
111  //**Destructor**********************************************************************************
114  inline ~MatrixAccessProxy();
116  //**********************************************************************************************
117 
118  //**Operators***********************************************************************************
121  inline MatrixAccessProxy& operator= ( const MatrixAccessProxy& vap );
122  template< typename T > inline MatrixAccessProxy& operator= ( const T& value );
123  template< typename T > inline MatrixAccessProxy& operator+=( const T& value );
124  template< typename T > inline MatrixAccessProxy& operator-=( const T& value );
125  template< typename T > inline MatrixAccessProxy& operator*=( const T& value );
126  template< typename T > inline MatrixAccessProxy& operator/=( const T& value );
128  //**********************************************************************************************
129 
130  //**Conversion operator*************************************************************************
133  inline operator Reference() const;
135  //**********************************************************************************************
136 
137  private:
138  //**Utility functions***************************************************************************
141  Reference get() const;
142  void set( ConstReference value ) const;
144  //**********************************************************************************************
145 
146  //**Member variables****************************************************************************
149  MT& sm_;
150  size_t i_;
151  size_t j_;
152 
153  //**********************************************************************************************
154 };
155 //*************************************************************************************************
156 
157 
158 
159 
160 //=================================================================================================
161 //
162 // CONSTRUCTORS
163 //
164 //=================================================================================================
165 
166 //*************************************************************************************************
173 template< typename MT > // Type of the sparse matrix
174 inline MatrixAccessProxy<MT>::MatrixAccessProxy( MT& sv, size_t i, size_t j )
175  : sm_( sv ) // Reference to the accessed sparse matrix
176  , i_ ( i ) // Row-index of the accessed sparse matrix element
177  , j_ ( j ) // Column-index of the accessed sparse matrix element
178 {
179  const Iterator element( sm_.find( i_, j_ ) );
180  const size_t index( rmm ? i_ : j_ );
181  if( element == sm_.end(index) )
182  sm_.insert( i_, j_, ElementType() );
183 }
184 //*************************************************************************************************
185 
186 
187 //*************************************************************************************************
192 template< typename MT > // Type of the sparse matrix
194  : sm_( map.sm_ ) // Reference to the accessed sparse matrix
195  , i_ ( map.i_ ) // Row-index of the accessed sparse matrix element
196  , j_ ( map.j_ ) // Column-index of the accessed sparse matrix element
197 {
198  BLAZE_INTERNAL_ASSERT( sm_.find(i_,j_) != sm_.end( rmm ? i_ : j_ ), "Missing matrix element detected" );
199 }
200 //*************************************************************************************************
201 
202 
203 
204 
205 //=================================================================================================
206 //
207 // CONSTRUCTORS
208 //
209 //=================================================================================================
210 
211 //*************************************************************************************************
214 template< typename MT > // Type of the sparse matrix
216 {
217  const Iterator element( sm_.find( i_, j_ ) );
218  const size_t index( rmm ? i_ : j_ );
219  if( element != sm_.end( index ) && isDefault( element->value() ) )
220  sm_.erase( index, element );
221 }
222 //*************************************************************************************************
223 
224 
225 
226 
227 //=================================================================================================
228 //
229 // OPERATORS
230 //
231 //=================================================================================================
232 
233 //*************************************************************************************************
239 template< typename MT > // Type of the sparse matrix
241 {
242  set( map.get() );
243  return *this;
244 }
245 //*************************************************************************************************
246 
247 
248 //*************************************************************************************************
254 template< typename MT > // Type of the sparse matrix
255 template< typename T > // Type of the right-hand side value
257 {
258  set( value );
259  return *this;
260 }
261 //*************************************************************************************************
262 
263 
264 //*************************************************************************************************
270 template< typename MT > // Type of the sparse matrix
271 template< typename T > // Type of the right-hand side value
273 {
274  get() += value;
275  return *this;
276 }
277 //*************************************************************************************************
278 
279 
280 //*************************************************************************************************
286 template< typename MT > // Type of the sparse matrix
287 template< typename T > // Type of the right-hand side value
289 {
290  get() -= value;
291  return *this;
292 }
293 //*************************************************************************************************
294 
295 
296 //*************************************************************************************************
302 template< typename MT > // Type of the sparse matrix
303 template< typename T > // Type of the right-hand side value
305 {
306  get() *= value;
307  return *this;
308 }
309 //*************************************************************************************************
310 
311 
312 //*************************************************************************************************
318 template< typename MT > // Type of the sparse matrix
319 template< typename T > // Type of the right-hand side value
321 {
322  get() /= value;
323  return *this;
324 }
325 //*************************************************************************************************
326 
327 
328 
329 
330 //=================================================================================================
331 //
332 // CONVERSION OPERATOR
333 //
334 //=================================================================================================
335 
336 //*************************************************************************************************
341 template< typename MT > // Type of the sparse matrix
343 {
344  return get();
345 }
346 //*************************************************************************************************
347 
348 
349 
350 
351 //=================================================================================================
352 //
353 // UTILITY FUNCTIONS
354 //
355 //=================================================================================================
356 
357 //*************************************************************************************************
362 template< typename MT > // Type of the sparse matrix
364 {
365  const Iterator element( sm_.find( i_, j_ ) );
366  BLAZE_INTERNAL_ASSERT( element != sm_.end( rmm ? i_ : j_ ), "Missing matrix element detected" );
367  return element->value();
368 }
369 //*************************************************************************************************
370 
371 
372 //*************************************************************************************************
378 template< typename MT > // Type of the sparse matrix
379 inline void MatrixAccessProxy<MT>::set( ConstReference value ) const
380 {
381  const Iterator element( sm_.find( i_, j_ ) );
382  BLAZE_INTERNAL_ASSERT( element != sm_.end( rmm ? i_ : j_ ), "Missing matrix element detected" );
383  element->value() = value;
384 }
385 //*************************************************************************************************
386 
387 
388 
389 
390 //=================================================================================================
391 //
392 // GLOBAL OPERATORS
393 //
394 //=================================================================================================
395 
396 //*************************************************************************************************
399 template< typename VT1, typename VT2 >
400 inline bool operator==( const MatrixAccessProxy<VT1>& lhs, const MatrixAccessProxy<VT2>& rhs );
401 
402 template< typename VT, typename T >
403 inline bool operator==( const MatrixAccessProxy<VT>& lhs, const T& rhs );
404 
405 template< typename T, typename VT >
406 inline bool operator==( const T& lhs, const MatrixAccessProxy<VT>& rhs );
407 
408 template< typename VT1, typename VT2 >
409 inline bool operator!=( const MatrixAccessProxy<VT1>& lhs, const MatrixAccessProxy<VT2>& rhs );
410 
411 template< typename VT, typename T >
412 inline bool operator!=( const MatrixAccessProxy<VT>& lhs, const T& rhs );
413 
414 template< typename T, typename VT >
415 inline bool operator!=( const T& lhs, const MatrixAccessProxy<VT>& rhs );
416 
417 template< typename VT1, typename VT2 >
418 inline bool operator<( const MatrixAccessProxy<VT1>& lhs, const MatrixAccessProxy<VT2>& rhs );
419 
420 template< typename VT, typename T >
421 inline bool operator<( const MatrixAccessProxy<VT>& lhs, const T& rhs );
422 
423 template< typename T, typename VT >
424 inline bool operator<( const T& lhs, const MatrixAccessProxy<VT>& rhs );
425 
426 template< typename VT1, typename VT2 >
427 inline bool operator>( const MatrixAccessProxy<VT1>& lhs, const MatrixAccessProxy<VT2>& rhs );
428 
429 template< typename VT, typename T >
430 inline bool operator>( const MatrixAccessProxy<VT>& lhs, const T& rhs );
431 
432 template< typename T, typename VT >
433 inline bool operator>( const T& lhs, const MatrixAccessProxy<VT>& rhs );
434 
435 template< typename VT1, typename VT2 >
436 inline bool operator<=( const MatrixAccessProxy<VT1>& lhs, const MatrixAccessProxy<VT2>& rhs );
437 
438 template< typename VT, typename T >
439 inline bool operator<=( const MatrixAccessProxy<VT>& lhs, const T& rhs );
440 
441 template< typename T, typename VT >
442 inline bool operator<=( const T& lhs, const MatrixAccessProxy<VT>& rhs );
443 
444 template< typename VT1, typename VT2 >
445 inline bool operator>=( const MatrixAccessProxy<VT1>& lhs, const MatrixAccessProxy<VT2>& rhs );
446 
447 template< typename VT, typename T >
448 inline bool operator>=( const MatrixAccessProxy<VT>& lhs, const T& rhs );
449 
450 template< typename T, typename VT >
451 inline bool operator>=( const T& lhs, const MatrixAccessProxy<VT>& rhs );
453 //*************************************************************************************************
454 
455 
456 //*************************************************************************************************
464 template< typename VT1, typename VT2 >
465 inline bool operator==( const MatrixAccessProxy<VT1>& lhs, const MatrixAccessProxy<VT2>& rhs )
466 {
467  typedef typename MatrixAccessProxy<VT1>::Reference LhsReference;
468  typedef typename MatrixAccessProxy<VT2>::Reference RhsReference;
469  return ( static_cast<LhsReference>( lhs ) == static_cast<RhsReference>( rhs ) );
470 }
471 //*************************************************************************************************
472 
473 
474 //*************************************************************************************************
482 template< typename VT, typename T >
483 inline bool operator==( const MatrixAccessProxy<VT>& lhs, const T& rhs )
484 {
486  return ( static_cast<Reference>( lhs ) == rhs );
487 }
488 //*************************************************************************************************
489 
490 
491 //*************************************************************************************************
499 template< typename T, typename VT >
500 inline bool operator==( const T& lhs, const MatrixAccessProxy<VT>& rhs )
501 {
503  return ( lhs == static_cast<Reference>( rhs ) );
504 }
505 //*************************************************************************************************
506 
507 
508 //*************************************************************************************************
516 template< typename VT1, typename VT2 >
517 inline bool operator!=( const MatrixAccessProxy<VT1>& lhs, const MatrixAccessProxy<VT2>& rhs )
518 {
519  typedef typename MatrixAccessProxy<VT1>::Reference LhsReference;
520  typedef typename MatrixAccessProxy<VT2>::Reference RhsReference;
521  return ( static_cast<LhsReference>( lhs ) != static_cast<RhsReference>( rhs ) );
522 }
523 //*************************************************************************************************
524 
525 
526 //*************************************************************************************************
534 template< typename VT, typename T >
535 inline bool operator!=( const MatrixAccessProxy<VT>& lhs, const T& rhs )
536 {
538  return ( static_cast<Reference>( lhs ) != rhs );
539 }
540 //*************************************************************************************************
541 
542 
543 //*************************************************************************************************
551 template< typename T, typename VT >
552 inline bool operator!=( const T& lhs, const MatrixAccessProxy<VT>& rhs )
553 {
555  return ( lhs != static_cast<Reference>( rhs ) );
556 }
557 //*************************************************************************************************
558 
559 
560 //*************************************************************************************************
568 template< typename VT1, typename VT2 >
569 inline bool operator<( const MatrixAccessProxy<VT1>& lhs, const MatrixAccessProxy<VT2>& rhs )
570 {
571  typedef typename MatrixAccessProxy<VT1>::Reference LhsReference;
572  typedef typename MatrixAccessProxy<VT2>::Reference RhsReference;
573  return ( static_cast<LhsReference>( lhs ) < static_cast<RhsReference>( rhs ) );
574 }
575 //*************************************************************************************************
576 
577 
578 //*************************************************************************************************
586 template< typename VT, typename T >
587 inline bool operator<( const MatrixAccessProxy<VT>& lhs, const T& rhs )
588 {
590  return ( static_cast<Reference>( lhs ) < rhs );
591 }
592 //*************************************************************************************************
593 
594 
595 //*************************************************************************************************
603 template< typename T, typename VT >
604 inline bool operator<( const T& lhs, const MatrixAccessProxy<VT>& rhs )
605 {
607  return ( lhs < static_cast<Reference>( rhs ) );
608 }
609 //*************************************************************************************************
610 
611 
612 //*************************************************************************************************
620 template< typename VT1, typename VT2 >
621 inline bool operator>( const MatrixAccessProxy<VT1>& lhs, const MatrixAccessProxy<VT2>& rhs )
622 {
623  typedef typename MatrixAccessProxy<VT1>::Reference LhsReference;
624  typedef typename MatrixAccessProxy<VT2>::Reference RhsReference;
625  return ( static_cast<LhsReference>( lhs ) > static_cast<RhsReference>( rhs ) );
626 }
627 //*************************************************************************************************
628 
629 
630 //*************************************************************************************************
638 template< typename VT, typename T >
639 inline bool operator>( const MatrixAccessProxy<VT>& lhs, const T& rhs )
640 {
642  return ( static_cast<Reference>( lhs ) > rhs );
643 }
644 //*************************************************************************************************
645 
646 
647 //*************************************************************************************************
655 template< typename T, typename VT >
656 inline bool operator>( const T& lhs, const MatrixAccessProxy<VT>& rhs )
657 {
659  return ( lhs > static_cast<Reference>( rhs ) );
660 }
661 //*************************************************************************************************
662 
663 
664 //*************************************************************************************************
672 template< typename VT1, typename VT2 >
673 inline bool operator<=( const MatrixAccessProxy<VT1>& lhs, const MatrixAccessProxy<VT2>& rhs )
674 {
675  typedef typename MatrixAccessProxy<VT1>::Reference LhsReference;
676  typedef typename MatrixAccessProxy<VT2>::Reference RhsReference;
677  return ( static_cast<LhsReference>( lhs ) <= static_cast<RhsReference>( rhs ) );
678 }
679 //*************************************************************************************************
680 
681 
682 //*************************************************************************************************
690 template< typename VT, typename T >
691 inline bool operator<=( const MatrixAccessProxy<VT>& lhs, const T& rhs )
692 {
694  return ( static_cast<Reference>( lhs ) <= rhs );
695 }
696 //*************************************************************************************************
697 
698 
699 //*************************************************************************************************
707 template< typename T, typename VT >
708 inline bool operator<=( const T& lhs, const MatrixAccessProxy<VT>& rhs )
709 {
711  return ( lhs <= static_cast<Reference>( rhs ) );
712 }
713 //*************************************************************************************************
714 
715 
716 //*************************************************************************************************
724 template< typename VT1, typename VT2 >
725 inline bool operator>=( const MatrixAccessProxy<VT1>& lhs, const MatrixAccessProxy<VT2>& rhs )
726 {
727  typedef typename MatrixAccessProxy<VT1>::Reference LhsReference;
728  typedef typename MatrixAccessProxy<VT2>::Reference RhsReference;
729  return ( static_cast<LhsReference>( lhs ) >= static_cast<RhsReference>( rhs ) );
730 }
731 //*************************************************************************************************
732 
733 
734 //*************************************************************************************************
742 template< typename VT, typename T >
743 inline bool operator>=( const MatrixAccessProxy<VT>& lhs, const T& rhs )
744 {
746  return ( static_cast<Reference>( lhs ) >= rhs );
747 }
748 //*************************************************************************************************
749 
750 
751 //*************************************************************************************************
759 template< typename T, typename VT >
760 inline bool operator>=( const T& lhs, const MatrixAccessProxy<VT>& rhs )
761 {
763  return ( lhs >= static_cast<Reference>( rhs ) );
764 }
765 //*************************************************************************************************
766 
767 } // namespace blaze
768 
769 #endif
size_t i_
Row-index of the accessed sparse matrix element.
Definition: MatrixAccessProxy.h:150
bool isDefault(const DynamicMatrix< Type, SO > &m)
Returns whether the given dense matrix is in default state.
Definition: DynamicMatrix.h:4622
MT::Iterator Iterator
Iterator type of the accessed sparse matrix.
Definition: MatrixAccessProxy.h:100
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
MT MatrixType
Type of the accessed sparse matrix.
Definition: MatrixAccessProxy.h:96
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:86
const ElementType & ConstReference
Reference type of the accessed constant element.
Definition: MatrixAccessProxy.h:99
Reference get() const
Returning the value of the accessed sparse matrix element.
Definition: MatrixAccessProxy.h:363
ElementType & Reference
Reference type of the accessed element.
Definition: MatrixAccessProxy.h:98
Compile time check for row-major matrix types.This type trait tests whether or not the given template...
Definition: IsRowMajorMatrix.h:104
MT::ElementType ElementType
Type of the accessed sparse matrix element.
Definition: MatrixAccessProxy.h:97
MatrixAccessProxy & operator=(const MatrixAccessProxy &vap)
Copy assignment operator for MatrixAccessProxy.
Definition: MatrixAccessProxy.h:240
Type ElementType
Type of the sparse matrix elements.
Definition: CompressedMatrix.h:2382
~MatrixAccessProxy()
The destructor for MatrixAccessProxy.
Definition: MatrixAccessProxy.h:215
MT & sm_
Reference to the accessed sparse matrix.
Definition: MatrixAccessProxy.h:149
Element * Iterator
Iterator over non-constant elements.
Definition: CompressedMatrix.h:2387
void set(ConstReference value) const
Setting the value of the accessed sparse matrix element.
Definition: MatrixAccessProxy.h:379
MatrixAccessProxy(MT &sv, size_t i, size_t j)
Initialization constructor for a MatrixAccessProxy.
Definition: MatrixAccessProxy.h:174
size_t j_
Column-index of the accessed sparse matrix element.
Definition: MatrixAccessProxy.h:151
Header file for the IsRowMajorMatrix type trait.
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
Header file for basic type definitions.
MatrixAccessProxy< This > Reference
Reference to a non-constant matrix value.
Definition: CompressedMatrix.h:2385
#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
EnableIf< IsIntegral< T >, Set< T, sizeof(T)> >::Type::Type set(T value)
Sets all values in the vector to the given integral value.
Definition: Set.h:209