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 <utility>
44 #include <blaze/math/Aliases.h>
47 #include <blaze/math/proxy/Proxy.h>
48 #include <blaze/math/shims/Clear.h>
50 #include <blaze/math/shims/IsNaN.h>
51 #include <blaze/math/shims/IsOne.h>
54 #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>, ElementType_<MT> >
101 {
102  private:
103  //**Enumerations********************************************************************************
105  enum : bool { rmm = IsRowMajorMatrix<MT>::value };
106  //**********************************************************************************************
107 
108  public:
109  //**Type definitions****************************************************************************
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 
134  template< typename T >
135  inline const MatrixAccessProxy& operator=( initializer_list<T> list ) const;
136 
137  template< typename T >
138  inline const MatrixAccessProxy& operator=( initializer_list< initializer_list<T> > list ) const;
139 
140  template< typename T > inline const MatrixAccessProxy& operator= ( const T& value ) const;
141  template< typename T > inline const MatrixAccessProxy& operator+=( const T& value ) const;
142  template< typename T > inline const MatrixAccessProxy& operator-=( const T& value ) const;
143  template< typename T > inline const MatrixAccessProxy& operator*=( const T& value ) const;
144  template< typename T > inline const MatrixAccessProxy& operator/=( const T& value ) const;
146  //**********************************************************************************************
147 
148  //**Utility functions***************************************************************************
151  inline RawReference get() const noexcept;
152  inline bool isRestricted() const noexcept;
154  //**********************************************************************************************
155 
156  //**Conversion operator*************************************************************************
159  inline operator RawReference() const noexcept;
161  //**********************************************************************************************
162 
163  private:
164  //**Member variables****************************************************************************
167  MT& sm_;
168  size_t i_;
169  size_t j_;
170 
171  //**********************************************************************************************
172 
173  //**Forbidden operations************************************************************************
176  void* operator&() const;
177 
178  //**********************************************************************************************
179 
180  //**Compile time checks*************************************************************************
184  //**********************************************************************************************
185 };
186 //*************************************************************************************************
187 
188 
189 
190 
191 //=================================================================================================
192 //
193 // CONSTRUCTORS
194 //
195 //=================================================================================================
196 
197 //*************************************************************************************************
204 template< typename MT > // Type of the sparse matrix
205 inline MatrixAccessProxy<MT>::MatrixAccessProxy( MT& sm, size_t i, size_t j )
206  : sm_( sm ) // Reference to the accessed sparse matrix
207  , i_ ( i ) // Row-index of the accessed sparse matrix element
208  , j_ ( j ) // Column-index of the accessed sparse matrix element
209 {
210  const Iterator_<MT> element( sm_.find( i_, j_ ) );
211  const size_t index( rmm ? i_ : j_ );
212  if( element == sm_.end(index) )
213  sm_.insert( i_, j_, RepresentedType() );
214 }
215 //*************************************************************************************************
216 
217 
218 //*************************************************************************************************
223 template< typename MT > // Type of the sparse matrix
225  : sm_( map.sm_ ) // Reference to the accessed sparse matrix
226  , i_ ( map.i_ ) // Row-index of the accessed sparse matrix element
227  , j_ ( map.j_ ) // Column-index of the accessed sparse matrix element
228 {
229  BLAZE_INTERNAL_ASSERT( sm_.find(i_,j_) != sm_.end( rmm ? i_ : j_ ), "Missing matrix element detected" );
230 }
231 //*************************************************************************************************
232 
233 
234 
235 
236 //=================================================================================================
237 //
238 // DESTRUCTORS
239 //
240 //=================================================================================================
241 
242 //*************************************************************************************************
245 template< typename MT > // Type of the sparse matrix
247 {
248  const Iterator_<MT> element( sm_.find( i_, j_ ) );
249  const size_t index( rmm ? i_ : j_ );
250  if( element != sm_.end( index ) && isDefault( element->value() ) )
251  sm_.erase( index, element );
252 }
253 //*************************************************************************************************
254 
255 
256 
257 
258 //=================================================================================================
259 //
260 // OPERATORS
261 //
262 //=================================================================================================
263 
264 //*************************************************************************************************
270 template< typename MT > // Type of the sparse matrix
272 {
273  get() = map.get();
274  return *this;
275 }
276 //*************************************************************************************************
277 
278 
279 //*************************************************************************************************
285 template< typename VT > // Type of the sparse matrix
286 template< typename T > // Type of the right-hand side elements
287 inline const MatrixAccessProxy<VT>&
289 {
290  get() = list;
291  return *this;
292 }
293 //*************************************************************************************************
294 
295 
296 //*************************************************************************************************
302 template< typename VT > // Type of the sparse matrix
303 template< typename T > // Type of the right-hand side elements
304 inline const MatrixAccessProxy<VT>&
306 {
307  get() = list;
308  return *this;
309 }
310 //*************************************************************************************************
311 
312 
313 //*************************************************************************************************
319 template< typename MT > // Type of the sparse matrix
320 template< typename T > // Type of the right-hand side value
321 inline const MatrixAccessProxy<MT>& MatrixAccessProxy<MT>::operator=( const T& value ) const
322 {
323  get() = value;
324  return *this;
325 }
326 //*************************************************************************************************
327 
328 
329 //*************************************************************************************************
335 template< typename MT > // Type of the sparse matrix
336 template< typename T > // Type of the right-hand side value
337 inline const MatrixAccessProxy<MT>& MatrixAccessProxy<MT>::operator+=( const T& value ) const
338 {
339  get() += value;
340  return *this;
341 }
342 //*************************************************************************************************
343 
344 
345 //*************************************************************************************************
351 template< typename MT > // Type of the sparse matrix
352 template< typename T > // Type of the right-hand side value
353 inline const MatrixAccessProxy<MT>& MatrixAccessProxy<MT>::operator-=( const T& value ) const
354 {
355  get() -= value;
356  return *this;
357 }
358 //*************************************************************************************************
359 
360 
361 //*************************************************************************************************
367 template< typename MT > // Type of the sparse matrix
368 template< typename T > // Type of the right-hand side value
369 inline const MatrixAccessProxy<MT>& MatrixAccessProxy<MT>::operator*=( const T& value ) const
370 {
371  get() *= value;
372  return *this;
373 }
374 //*************************************************************************************************
375 
376 
377 //*************************************************************************************************
383 template< typename MT > // Type of the sparse matrix
384 template< typename T > // Type of the right-hand side value
385 inline const MatrixAccessProxy<MT>& MatrixAccessProxy<MT>::operator/=( const T& value ) const
386 {
387  get() /= value;
388  return *this;
389 }
390 //*************************************************************************************************
391 
392 
393 
394 
395 //=================================================================================================
396 //
397 // UTILITY FUNCTIONS
398 //
399 //=================================================================================================
400 
401 //*************************************************************************************************
406 template< typename MT > // Type of the sparse matrix
408 {
409  const Iterator_<MT> element( sm_.find( i_, j_ ) );
410  BLAZE_INTERNAL_ASSERT( element != sm_.end( rmm ? i_ : j_ ), "Missing matrix element detected" );
411  return element->value();
412 }
413 //*************************************************************************************************
414 
415 
416 //*************************************************************************************************
421 template< typename MT > // Type of the sparse matrix
422 inline bool MatrixAccessProxy<MT>::isRestricted() const noexcept
423 {
424  return false;
425 }
426 //*************************************************************************************************
427 
428 
429 
430 
431 //=================================================================================================
432 //
433 // CONVERSION OPERATOR
434 //
435 //=================================================================================================
436 
437 //*************************************************************************************************
442 template< typename MT > // Type of the sparse matrix
443 inline MatrixAccessProxy<MT>::operator RawReference() const noexcept
444 {
445  return get();
446 }
447 //*************************************************************************************************
448 
449 
450 
451 
452 //=================================================================================================
453 //
454 // GLOBAL FUNCTIONS
455 //
456 //=================================================================================================
457 
458 //*************************************************************************************************
461 template< typename MT >
462 inline void reset( const MatrixAccessProxy<MT>& proxy );
463 
464 template< typename MT >
465 inline void clear( const MatrixAccessProxy<MT>& proxy );
466 
467 template< typename MT >
468 inline bool isDefault( const MatrixAccessProxy<MT>& proxy );
469 
470 template< typename MT >
471 inline bool isReal( const MatrixAccessProxy<MT>& proxy );
472 
473 template< typename MT >
474 inline bool isZero( const MatrixAccessProxy<MT>& proxy );
475 
476 template< typename MT >
477 inline bool isOne( const MatrixAccessProxy<MT>& proxy );
478 
479 template< typename MT >
480 inline bool isnan( const MatrixAccessProxy<MT>& proxy );
481 
482 template< typename MT >
483 inline void swap( const MatrixAccessProxy<MT>& a, const MatrixAccessProxy<MT>& b ) noexcept;
484 
485 template< typename MT, typename T >
486 inline void swap( const MatrixAccessProxy<MT>& a, T& b ) noexcept;
487 
488 template< typename T, typename MT >
489 inline void swap( T& a, const MatrixAccessProxy<MT>& v ) noexcept;
491 //*************************************************************************************************
492 
493 
494 //*************************************************************************************************
506 template< typename MT >
507 inline void reset( const MatrixAccessProxy<MT>& proxy )
508 {
509  using blaze::reset;
510 
511  reset( proxy.get() );
512 }
513 //*************************************************************************************************
514 
515 
516 //*************************************************************************************************
527 template< typename MT >
528 inline void clear( const MatrixAccessProxy<MT>& proxy )
529 {
530  using blaze::clear;
531 
532  clear( proxy.get() );
533 }
534 //*************************************************************************************************
535 
536 
537 //*************************************************************************************************
547 template< typename MT >
548 inline bool isDefault( const MatrixAccessProxy<MT>& proxy )
549 {
550  using blaze::isDefault;
551 
552  return isDefault( proxy.get() );
553 }
554 //*************************************************************************************************
555 
556 
557 //*************************************************************************************************
569 template< typename MT >
570 inline bool isReal( const MatrixAccessProxy<MT>& proxy )
571 {
572  using blaze::isReal;
573 
574  return isReal( proxy.get() );
575 }
576 //*************************************************************************************************
577 
578 
579 //*************************************************************************************************
589 template< typename MT >
590 inline bool isZero( const MatrixAccessProxy<MT>& proxy )
591 {
592  using blaze::isZero;
593 
594  return isZero( proxy.get() );
595 }
596 //*************************************************************************************************
597 
598 
599 //*************************************************************************************************
609 template< typename MT >
610 inline bool isOne( const MatrixAccessProxy<MT>& proxy )
611 {
612  using blaze::isOne;
613 
614  return isOne( proxy.get() );
615 }
616 //*************************************************************************************************
617 
618 
619 //*************************************************************************************************
629 template< typename MT >
630 inline bool isnan( const MatrixAccessProxy<MT>& proxy )
631 {
632  using blaze::isnan;
633 
634  return isnan( proxy.get() );
635 }
636 //*************************************************************************************************
637 
638 
639 //*************************************************************************************************
647 template< typename MT >
648 inline void swap( const MatrixAccessProxy<MT>& a, const MatrixAccessProxy<MT>& b ) noexcept
649 {
650  using std::swap;
651 
652  swap( a.get(), b.get() );
653 }
654 //*************************************************************************************************
655 
656 
657 //*************************************************************************************************
665 template< typename MT, typename T >
666 inline void swap( const MatrixAccessProxy<MT>& a, T& b ) noexcept
667 {
668  using std::swap;
669 
670  swap( a.get(), b );
671 }
672 //*************************************************************************************************
673 
674 
675 //*************************************************************************************************
683 template< typename T, typename MT >
684 inline void swap( T& a, const MatrixAccessProxy<MT>& b ) noexcept
685 {
686  using std::swap;
687 
688  swap( a, b.get() );
689 }
690 //*************************************************************************************************
691 
692 } // namespace blaze
693 
694 #endif
Header file for the isnan shim.
size_t i_
Row-index of the accessed sparse matrix element.
Definition: MatrixAccessProxy.h:168
bool isOne(const DiagonalProxy< MT > &proxy)
Returns whether the represented element is 1.
Definition: DiagonalProxy.h:635
Header file for auxiliary alias declarations.
Header file for basic type definitions.
bool isRestricted() const noexcept
Returns whether the proxy represents a restricted sparse matrix element..
Definition: MatrixAccessProxy.h:422
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:595
void reset(const DiagonalProxy< MT > &proxy)
Resetting the represented element to the default initial values.
Definition: DiagonalProxy.h:533
RawReference get() const noexcept
Returning the value of the accessed sparse matrix element.
Definition: MatrixAccessProxy.h:407
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:573
Header file for the std::initializer_list aliases.
Constraint on the data type.
Header file for the clear shim.
Namespace of the Blaze C++ math library.
Definition: Blaze.h:57
void swap(CompressedMatrix< Type, SO > &a, CompressedMatrix< Type, SO > &b) noexcept
Swapping the contents of two compressed matrices.
Definition: CompressedMatrix.h:5148
const MatrixAccessProxy & operator=(const MatrixAccessProxy &map) const
Copy assignment operator for MatrixAccessProxy.
Definition: MatrixAccessProxy.h:271
Compile time check for row-major matrix types.This type trait tests whether or not the given template...
Definition: IsRowMajorMatrix.h:83
Header file for the isZero shim.
typename T::ElementType ElementType_
Alias declaration for nested ElementType type definitions.The ElementType_ alias declaration provides...
Definition: Aliases.h:163
MatrixAccessProxy(MT &sm, size_t i, size_t j)
Initialization constructor for a MatrixAccessProxy.
Definition: MatrixAccessProxy.h:205
bool isnan(const DiagonalProxy< MT > &proxy)
Returns whether the represented element is not a number.
Definition: DiagonalProxy.h:655
void clear(const DiagonalProxy< MT > &proxy)
Clearing the represented element.
Definition: DiagonalProxy.h:553
Header file for the isOne shim.
ElementType_< MT > RepresentedType
Type of the represented sparse matrix element.
Definition: MatrixAccessProxy.h:110
~MatrixAccessProxy()
The destructor for MatrixAccessProxy.
Definition: MatrixAccessProxy.h:246
Header file for run time assertion macros.
MT & sm_
Reference to the accessed sparse matrix.
Definition: MatrixAccessProxy.h:167
Header file for the reset shim.
Header file for the isDefault shim.
void swap(DiagonalMatrix< MT, SO, DF > &a, DiagonalMatrix< MT, SO, DF > &b) noexcept
Swapping the contents of two matrices.
Definition: DiagonalMatrix.h:258
typename T::Iterator Iterator_
Alias declaration for nested Iterator type definitions.The Iterator_ alias declaration provides a con...
Definition: Aliases.h:183
size_t j_
Column-index of the accessed sparse matrix element.
Definition: MatrixAccessProxy.h:169
Header file for the IsRowMajorMatrix type trait.
Initializer list type of the Blaze library.
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:615
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:61