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>
45 #include <blaze/math/proxy/Proxy.h>
46 #include <blaze/math/shims/Clear.h>
49 #include <blaze/math/shims/IsNaN.h>
50 #include <blaze/math/shims/IsOne.h>
53 #include <blaze/math/shims/Reset.h>
56 #include <blaze/util/Assert.h>
57 #include <blaze/util/Types.h>
58 
59 
60 namespace blaze {
61 
62 //=================================================================================================
63 //
64 // CLASS DEFINITION
65 //
66 //=================================================================================================
67 
68 //*************************************************************************************************
99 template< typename MT > // Type of the sparse matrix
100 class MatrixAccessProxy : public Proxy< MatrixAccessProxy<MT>, typename MT::ElementType >
101 {
102  private:
103  //**Enumerations********************************************************************************
105  enum { rmm = IsRowMajorMatrix<MT>::value };
106  //**********************************************************************************************
107 
108  public:
109  //**Type definitions****************************************************************************
110  typedef typename MT::ElementType RepresentedType;
111  typedef RepresentedType& RawReference;
112  //**********************************************************************************************
113 
114  //**Constructors********************************************************************************
117  explicit inline MatrixAccessProxy( MT& sm, size_t i, size_t j );
118  inline MatrixAccessProxy( const MatrixAccessProxy& map );
120  //**********************************************************************************************
121 
122  //**Destructor**********************************************************************************
125  inline ~MatrixAccessProxy();
127  //**********************************************************************************************
128 
129  //**Operators***********************************************************************************
132  inline const MatrixAccessProxy& operator= ( const MatrixAccessProxy& map ) const;
133  template< typename T > inline const MatrixAccessProxy& operator= ( const T& value ) const;
134  template< typename T > inline const MatrixAccessProxy& operator+=( const T& value ) const;
135  template< typename T > inline const MatrixAccessProxy& operator-=( const T& value ) const;
136  template< typename T > inline const MatrixAccessProxy& operator*=( const T& value ) const;
137  template< typename T > inline const MatrixAccessProxy& operator/=( const T& value ) const;
139  //**********************************************************************************************
140 
141  //**Utility functions***************************************************************************
144  inline RawReference get() const;
145  inline bool isRestricted() const;
147  //**********************************************************************************************
148 
149  //**Conversion operator*************************************************************************
152  inline operator RawReference() const;
154  //**********************************************************************************************
155 
156  private:
157  //**Member variables****************************************************************************
160  MT& sm_;
161  size_t i_;
162  size_t j_;
163 
164  //**********************************************************************************************
165 
166  //**Forbidden operations************************************************************************
169  void* operator&() const;
170 
171  //**********************************************************************************************
172 
173  //**Compile time checks*************************************************************************
177  //**********************************************************************************************
178 };
179 //*************************************************************************************************
180 
181 
182 
183 
184 //=================================================================================================
185 //
186 // CONSTRUCTORS
187 //
188 //=================================================================================================
189 
190 //*************************************************************************************************
197 template< typename MT > // Type of the sparse matrix
198 inline MatrixAccessProxy<MT>::MatrixAccessProxy( MT& sm, size_t i, size_t j )
199  : sm_( sm ) // Reference to the accessed sparse matrix
200  , i_ ( i ) // Row-index of the accessed sparse matrix element
201  , j_ ( j ) // Column-index of the accessed sparse matrix element
202 {
203  const typename MT::Iterator element( sm_.find( i_, j_ ) );
204  const size_t index( rmm ? i_ : j_ );
205  if( element == sm_.end(index) )
206  sm_.insert( i_, j_, RepresentedType() );
207 }
208 //*************************************************************************************************
209 
210 
211 //*************************************************************************************************
216 template< typename MT > // Type of the sparse matrix
218  : sm_( map.sm_ ) // Reference to the accessed sparse matrix
219  , i_ ( map.i_ ) // Row-index of the accessed sparse matrix element
220  , j_ ( map.j_ ) // Column-index of the accessed sparse matrix element
221 {
222  BLAZE_INTERNAL_ASSERT( sm_.find(i_,j_) != sm_.end( rmm ? i_ : j_ ), "Missing matrix element detected" );
223 }
224 //*************************************************************************************************
225 
226 
227 
228 
229 //=================================================================================================
230 //
231 // DESTRUCTORS
232 //
233 //=================================================================================================
234 
235 //*************************************************************************************************
238 template< typename MT > // Type of the sparse matrix
240 {
241  const typename MT::Iterator element( sm_.find( i_, j_ ) );
242  const size_t index( rmm ? i_ : j_ );
243  if( element != sm_.end( index ) && isDefault( element->value() ) )
244  sm_.erase( index, element );
245 }
246 //*************************************************************************************************
247 
248 
249 
250 
251 //=================================================================================================
252 //
253 // OPERATORS
254 //
255 //=================================================================================================
256 
257 //*************************************************************************************************
263 template< typename MT > // Type of the sparse matrix
265 {
266  get() = map.get();
267  return *this;
268 }
269 //*************************************************************************************************
270 
271 
272 //*************************************************************************************************
278 template< typename MT > // Type of the sparse matrix
279 template< typename T > // Type of the right-hand side value
280 inline const MatrixAccessProxy<MT>& MatrixAccessProxy<MT>::operator=( const T& value ) const
281 {
282  get() = value;
283  return *this;
284 }
285 //*************************************************************************************************
286 
287 
288 //*************************************************************************************************
294 template< typename MT > // Type of the sparse matrix
295 template< typename T > // Type of the right-hand side value
296 inline const MatrixAccessProxy<MT>& MatrixAccessProxy<MT>::operator+=( const T& value ) const
297 {
298  get() += value;
299  return *this;
300 }
301 //*************************************************************************************************
302 
303 
304 //*************************************************************************************************
310 template< typename MT > // Type of the sparse matrix
311 template< typename T > // Type of the right-hand side value
312 inline const MatrixAccessProxy<MT>& MatrixAccessProxy<MT>::operator-=( const T& value ) const
313 {
314  get() -= value;
315  return *this;
316 }
317 //*************************************************************************************************
318 
319 
320 //*************************************************************************************************
326 template< typename MT > // Type of the sparse matrix
327 template< typename T > // Type of the right-hand side value
328 inline const MatrixAccessProxy<MT>& MatrixAccessProxy<MT>::operator*=( const T& value ) const
329 {
330  get() *= value;
331  return *this;
332 }
333 //*************************************************************************************************
334 
335 
336 //*************************************************************************************************
342 template< typename MT > // Type of the sparse matrix
343 template< typename T > // Type of the right-hand side value
344 inline const MatrixAccessProxy<MT>& MatrixAccessProxy<MT>::operator/=( const T& value ) const
345 {
346  get() /= value;
347  return *this;
348 }
349 //*************************************************************************************************
350 
351 
352 
353 
354 //=================================================================================================
355 //
356 // UTILITY FUNCTIONS
357 //
358 //=================================================================================================
359 
360 //*************************************************************************************************
365 template< typename MT > // Type of the sparse matrix
367 {
368  const typename MT::Iterator element( sm_.find( i_, j_ ) );
369  BLAZE_INTERNAL_ASSERT( element != sm_.end( rmm ? i_ : j_ ), "Missing matrix element detected" );
370  return element->value();
371 }
372 //*************************************************************************************************
373 
374 
375 //*************************************************************************************************
380 template< typename MT > // Type of the sparse matrix
382 {
383  return false;
384 }
385 //*************************************************************************************************
386 
387 
388 
389 
390 //=================================================================================================
391 //
392 // CONVERSION OPERATOR
393 //
394 //=================================================================================================
395 
396 //*************************************************************************************************
401 template< typename MT > // Type of the sparse matrix
403 {
404  return get();
405 }
406 //*************************************************************************************************
407 
408 
409 
410 
411 //=================================================================================================
412 //
413 // GLOBAL FUNCTIONS
414 //
415 //=================================================================================================
416 
417 //*************************************************************************************************
420 template< typename MT >
422  conj( const MatrixAccessProxy<MT>& proxy );
423 
424 template< typename MT >
425 inline void reset( const MatrixAccessProxy<MT>& proxy );
426 
427 template< typename MT >
428 inline void clear( const MatrixAccessProxy<MT>& proxy );
429 
430 template< typename MT >
431 inline bool isDefault( const MatrixAccessProxy<MT>& proxy );
432 
433 template< typename MT >
434 inline bool isReal( const MatrixAccessProxy<MT>& proxy );
435 
436 template< typename MT >
437 inline bool isZero( const MatrixAccessProxy<MT>& proxy );
438 
439 template< typename MT >
440 inline bool isOne( const MatrixAccessProxy<MT>& proxy );
441 
442 template< typename MT >
443 inline bool isnan( const MatrixAccessProxy<MT>& proxy );
444 
445 template< typename MT >
446 inline void swap( const MatrixAccessProxy<MT>& a, const MatrixAccessProxy<MT>& b ) /* throw() */;
447 
448 template< typename MT, typename T >
449 inline void swap( const MatrixAccessProxy<MT>& a, T& b ) /* throw() */;
450 
451 template< typename T, typename MT >
452 inline void swap( T& a, const MatrixAccessProxy<MT>& v ) /* throw() */;
454 //*************************************************************************************************
455 
456 
457 //*************************************************************************************************
468 template< typename MT >
470  conj( const MatrixAccessProxy<MT>& proxy )
471 {
472  using blaze::conj;
473 
474  return conj( (~proxy).get() );
475 }
476 //*************************************************************************************************
477 
478 
479 //*************************************************************************************************
491 template< typename MT >
492 inline void reset( const MatrixAccessProxy<MT>& proxy )
493 {
494  using blaze::reset;
495 
496  reset( proxy.get() );
497 }
498 //*************************************************************************************************
499 
500 
501 //*************************************************************************************************
512 template< typename MT >
513 inline void clear( const MatrixAccessProxy<MT>& proxy )
514 {
515  using blaze::clear;
516 
517  clear( proxy.get() );
518 }
519 //*************************************************************************************************
520 
521 
522 //*************************************************************************************************
532 template< typename MT >
533 inline bool isDefault( const MatrixAccessProxy<MT>& proxy )
534 {
535  using blaze::isDefault;
536 
537  return isDefault( proxy.get() );
538 }
539 //*************************************************************************************************
540 
541 
542 //*************************************************************************************************
554 template< typename MT >
555 inline bool isReal( const MatrixAccessProxy<MT>& proxy )
556 {
557  using blaze::isReal;
558 
559  return isReal( proxy.get() );
560 }
561 //*************************************************************************************************
562 
563 
564 //*************************************************************************************************
574 template< typename MT >
575 inline bool isZero( const MatrixAccessProxy<MT>& proxy )
576 {
577  using blaze::isZero;
578 
579  return isZero( proxy.get() );
580 }
581 //*************************************************************************************************
582 
583 
584 //*************************************************************************************************
594 template< typename MT >
595 inline bool isOne( const MatrixAccessProxy<MT>& proxy )
596 {
597  using blaze::isOne;
598 
599  return isOne( proxy.get() );
600 }
601 //*************************************************************************************************
602 
603 
604 //*************************************************************************************************
614 template< typename MT >
615 inline bool isnan( const MatrixAccessProxy<MT>& proxy )
616 {
617  using blaze::isnan;
618 
619  return isnan( proxy.get() );
620 }
621 //*************************************************************************************************
622 
623 
624 //*************************************************************************************************
633 template< typename MT >
634 inline void swap( const MatrixAccessProxy<MT>& a, const MatrixAccessProxy<MT>& b ) /* throw() */
635 {
636  using std::swap;
637 
638  swap( a.get(), b.get() );
639 }
640 //*************************************************************************************************
641 
642 
643 //*************************************************************************************************
652 template< typename MT, typename T >
653 inline void swap( const MatrixAccessProxy<MT>& a, T& b ) /* throw() */
654 {
655  using std::swap;
656 
657  swap( a.get(), b );
658 }
659 //*************************************************************************************************
660 
661 
662 //*************************************************************************************************
671 template< typename T, typename MT >
672 inline void swap( T& a, const MatrixAccessProxy<MT>& b ) /* throw() */
673 {
674  using std::swap;
675 
676  swap( a, b.get() );
677 }
678 //*************************************************************************************************
679 
680 } // namespace blaze
681 
682 #endif
Header file for the isnan shim.
size_t i_
Row-index of the accessed sparse matrix element.
Definition: MatrixAccessProxy.h:161
bool isOne(const DiagonalProxy< MT > &proxy)
Returns whether the represented element is 1.
Definition: DiagonalProxy.h:609
bool isRestricted() const
Returns whether the proxy represents a restricted sparse matrix element..
Definition: MatrixAccessProxy.h:381
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: Forward.h:51
bool isReal(const DiagonalProxy< MT > &proxy)
Returns whether the matrix element represents a real number.
Definition: DiagonalProxy.h:569
void reset(const DiagonalProxy< MT > &proxy)
Resetting the represented element to the default initial values.
Definition: DiagonalProxy.h:507
void * operator&() const
Address operator (private & undefined)
Access proxy for sparse, matrices.The MatrixAccessProxy provides safe access to the elements of a no...
Definition: MatrixAccessProxy.h:100
Header file for the Proxy class.
bool isDefault(const DiagonalProxy< MT > &proxy)
Returns whether the represented element is in default state.
Definition: DiagonalProxy.h:547
MT::ElementType RepresentedType
Type of the represented sparse matrix element.
Definition: MatrixAccessProxy.h:110
ConjExprTrait< typename DiagonalProxy< MT >::RepresentedType >::Type conj(const DiagonalProxy< MT > &proxy)
Computing the complex conjugate of the represented element.
Definition: DiagonalProxy.h:487
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:264
Compile time check for row-major matrix types.This type trait tests whether or not the given template...
Definition: IsRowMajorMatrix.h:110
void swap(CompressedMatrix< Type, SO > &a, CompressedMatrix< Type, SO > &b)
Swapping the contents of two compressed matrices.
Definition: CompressedMatrix.h:4998
Header file for the isZero shim.
MatrixAccessProxy(MT &sm, size_t i, size_t j)
Initialization constructor for a MatrixAccessProxy.
Definition: MatrixAccessProxy.h:198
RawReference get() const
Returning the value of the accessed sparse matrix element.
Definition: MatrixAccessProxy.h:366
bool isnan(const DiagonalProxy< MT > &proxy)
Returns whether the represented element is not a number.
Definition: DiagonalProxy.h:629
Type ElementType
Type of the sparse matrix elements.
Definition: CompressedMatrix.h:2586
void clear(const DiagonalProxy< MT > &proxy)
Clearing the represented element.
Definition: DiagonalProxy.h:527
Header file for the isOne shim.
~MatrixAccessProxy()
The destructor for MatrixAccessProxy.
Definition: MatrixAccessProxy.h:239
Header file for the conjugate shim.
Evaluation of the return type of a complex conjugate expression.Via this type trait it is possible to...
Definition: ConjExprTrait.h:87
Header file for run time assertion macros.
MT & sm_
Reference to the accessed sparse matrix.
Definition: MatrixAccessProxy.h:160
Header file for the reset shim.
Element * Iterator
Iterator over non-constant elements.
Definition: CompressedMatrix.h:2591
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:256
size_t j_
Column-index of the accessed sparse matrix element.
Definition: MatrixAccessProxy.h:162
Header file for the IsRowMajorMatrix type trait.
RepresentedType & RawReference
Raw reference to the represented element.
Definition: MatrixAccessProxy.h:111
bool isZero(const DiagonalProxy< MT > &proxy)
Returns whether the represented element is 0.
Definition: DiagonalProxy.h:589
Header file for the ConjExprTrait class template.
Header file for the isReal shim.
#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