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>
49 #include <blaze/math/shims/Clear.h>
51 #include <blaze/math/shims/IsNaN.h>
52 #include <blaze/math/shims/IsOne.h>
55 #include <blaze/math/shims/Reset.h>
57 #include <blaze/util/Assert.h>
58 #include <blaze/util/Types.h>
59 
60 
61 namespace blaze {
62 
63 //=================================================================================================
64 //
65 // CLASS DEFINITION
66 //
67 //=================================================================================================
68 
69 //*************************************************************************************************
100 template< typename MT > // Type of the sparse matrix
102  : public Proxy< MatrixAccessProxy<MT>, ElementType_t<MT> >
103 {
104  private:
105  //**Enumerations********************************************************************************
107  static constexpr bool rmm = IsRowMajorMatrix_v<MT>;
108  //**********************************************************************************************
109 
110  public:
111  //**Type definitions****************************************************************************
114  //**********************************************************************************************
115 
116  //**Constructors********************************************************************************
119  explicit inline MatrixAccessProxy( MT& sm, size_t i, size_t j );
120  inline MatrixAccessProxy( const MatrixAccessProxy& map );
122  //**********************************************************************************************
123 
124  //**Destructor**********************************************************************************
127  inline ~MatrixAccessProxy();
129  //**********************************************************************************************
130 
131  //**Operators***********************************************************************************
134  inline const MatrixAccessProxy& operator=( const MatrixAccessProxy& map ) const;
135 
136  template< typename T >
137  inline const MatrixAccessProxy& operator=( initializer_list<T> list ) const;
138 
139  template< typename T >
140  inline const MatrixAccessProxy& operator=( initializer_list< initializer_list<T> > list ) const;
141 
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;
145  template< typename T > inline const MatrixAccessProxy& operator*=( const T& value ) const;
146  template< typename T > inline const MatrixAccessProxy& operator/=( const T& value ) const;
147  template< typename T > inline const MatrixAccessProxy& operator%=( const T& value ) const;
149  //**********************************************************************************************
150 
151  //**Utility functions***************************************************************************
154  inline RawReference get() const noexcept;
155  inline bool isRestricted() const noexcept;
157  //**********************************************************************************************
158 
159  //**Conversion operator*************************************************************************
162  inline operator RawReference() const noexcept;
164  //**********************************************************************************************
165 
166  private:
167  //**Member variables****************************************************************************
170  MT& sm_;
171  size_t i_;
172  size_t j_;
173 
174  //**********************************************************************************************
175 
176  //**Forbidden operations************************************************************************
179  void* operator&() const;
180 
181  //**********************************************************************************************
182 
183  //**Compile time checks*************************************************************************
187  //**********************************************************************************************
188 };
189 //*************************************************************************************************
190 
191 
192 
193 
194 //=================================================================================================
195 //
196 // CONSTRUCTORS
197 //
198 //=================================================================================================
199 
200 //*************************************************************************************************
207 template< typename MT > // Type of the sparse matrix
208 inline MatrixAccessProxy<MT>::MatrixAccessProxy( MT& sm, size_t i, size_t j )
209  : sm_( sm ) // Reference to the accessed sparse matrix
210  , i_ ( i ) // Row-index of the accessed sparse matrix element
211  , j_ ( j ) // Column-index of the accessed sparse matrix element
212 {
213  const Iterator_t<MT> element( sm_.find( i_, j_ ) );
214  const size_t index( rmm ? i_ : j_ );
215  if( element == sm_.end(index) )
216  sm_.insert( i_, j_, RepresentedType() );
217 }
218 //*************************************************************************************************
219 
220 
221 //*************************************************************************************************
226 template< typename MT > // Type of the sparse matrix
228  : sm_( map.sm_ ) // Reference to the accessed sparse matrix
229  , i_ ( map.i_ ) // Row-index of the accessed sparse matrix element
230  , j_ ( map.j_ ) // Column-index of the accessed sparse matrix element
231 {
232  BLAZE_INTERNAL_ASSERT( sm_.find(i_,j_) != sm_.end( rmm ? i_ : j_ ), "Missing matrix element detected" );
233 }
234 //*************************************************************************************************
235 
236 
237 
238 
239 //=================================================================================================
240 //
241 // DESTRUCTORS
242 //
243 //=================================================================================================
244 
245 //*************************************************************************************************
248 template< typename MT > // Type of the sparse matrix
250 {
251  const Iterator_t<MT> element( sm_.find( i_, j_ ) );
252  const size_t index( rmm ? i_ : j_ );
253  if( element != sm_.end( index ) && isDefault<strict>( element->value() ) )
254  sm_.erase( index, element );
255 }
256 //*************************************************************************************************
257 
258 
259 
260 
261 //=================================================================================================
262 //
263 // OPERATORS
264 //
265 //=================================================================================================
266 
267 //*************************************************************************************************
273 template< typename MT > // Type of the sparse matrix
275 {
276  get() = map.get();
277  return *this;
278 }
279 //*************************************************************************************************
280 
281 
282 //*************************************************************************************************
288 template< typename VT > // Type of the sparse matrix
289 template< typename T > // Type of the right-hand side elements
290 inline const MatrixAccessProxy<VT>&
292 {
293  get() = list;
294  return *this;
295 }
296 //*************************************************************************************************
297 
298 
299 //*************************************************************************************************
305 template< typename VT > // Type of the sparse matrix
306 template< typename T > // Type of the right-hand side elements
307 inline const MatrixAccessProxy<VT>&
309 {
310  get() = list;
311  return *this;
312 }
313 //*************************************************************************************************
314 
315 
316 //*************************************************************************************************
322 template< typename MT > // Type of the sparse matrix
323 template< typename T > // Type of the right-hand side value
324 inline const MatrixAccessProxy<MT>& MatrixAccessProxy<MT>::operator=( const T& value ) const
325 {
326  get() = value;
327  return *this;
328 }
329 //*************************************************************************************************
330 
331 
332 //*************************************************************************************************
338 template< typename MT > // Type of the sparse matrix
339 template< typename T > // Type of the right-hand side value
340 inline const MatrixAccessProxy<MT>& MatrixAccessProxy<MT>::operator+=( const T& value ) const
341 {
342  get() += value;
343  return *this;
344 }
345 //*************************************************************************************************
346 
347 
348 //*************************************************************************************************
354 template< typename MT > // Type of the sparse matrix
355 template< typename T > // Type of the right-hand side value
356 inline const MatrixAccessProxy<MT>& MatrixAccessProxy<MT>::operator-=( const T& value ) const
357 {
358  get() -= value;
359  return *this;
360 }
361 //*************************************************************************************************
362 
363 
364 //*************************************************************************************************
370 template< typename MT > // Type of the sparse matrix
371 template< typename T > // Type of the right-hand side value
372 inline const MatrixAccessProxy<MT>& MatrixAccessProxy<MT>::operator*=( const T& value ) const
373 {
374  get() *= value;
375  return *this;
376 }
377 //*************************************************************************************************
378 
379 
380 //*************************************************************************************************
386 template< typename MT > // Type of the sparse matrix
387 template< typename T > // Type of the right-hand side value
388 inline const MatrixAccessProxy<MT>& MatrixAccessProxy<MT>::operator/=( const T& value ) const
389 {
390  get() /= value;
391  return *this;
392 }
393 //*************************************************************************************************
394 
395 
396 //*************************************************************************************************
406 template< typename MT > // Type of the sparse matrix
407 template< typename T > // Type of the right-hand side value
408 inline const MatrixAccessProxy<MT>& MatrixAccessProxy<MT>::operator%=( const T& value ) const
409 {
410  get() %= value;
411  return *this;
412 }
413 //*************************************************************************************************
414 
415 
416 
417 
418 //=================================================================================================
419 //
420 // UTILITY FUNCTIONS
421 //
422 //=================================================================================================
423 
424 //*************************************************************************************************
429 template< typename MT > // Type of the sparse matrix
431 {
432  const Iterator_t<MT> element( sm_.find( i_, j_ ) );
433  BLAZE_INTERNAL_ASSERT( element != sm_.end( rmm ? i_ : j_ ), "Missing matrix element detected" );
434  return element->value();
435 }
436 //*************************************************************************************************
437 
438 
439 //*************************************************************************************************
444 template< typename MT > // Type of the sparse matrix
445 inline bool MatrixAccessProxy<MT>::isRestricted() const noexcept
446 {
447  return false;
448 }
449 //*************************************************************************************************
450 
451 
452 
453 
454 //=================================================================================================
455 //
456 // CONVERSION OPERATOR
457 //
458 //=================================================================================================
459 
460 //*************************************************************************************************
465 template< typename MT > // Type of the sparse matrix
467 {
468  return get();
469 }
470 //*************************************************************************************************
471 
472 
473 
474 
475 //=================================================================================================
476 //
477 // GLOBAL FUNCTIONS
478 //
479 //=================================================================================================
480 
481 //*************************************************************************************************
484 template< typename MT >
485 void reset( const MatrixAccessProxy<MT>& proxy );
486 
487 template< typename MT >
488 void clear( const MatrixAccessProxy<MT>& proxy );
489 
490 template< bool RF, typename MT >
491 bool isDefault( const MatrixAccessProxy<MT>& proxy );
492 
493 template< bool RF, typename MT >
494 bool isReal( const MatrixAccessProxy<MT>& proxy );
495 
496 template< bool RF, typename MT >
497 bool isZero( const MatrixAccessProxy<MT>& proxy );
498 
499 template< bool RF, typename MT >
500 bool isOne( const MatrixAccessProxy<MT>& proxy );
501 
502 template< typename MT >
503 bool isnan( const MatrixAccessProxy<MT>& proxy );
504 
505 template< typename MT >
506 void swap( const MatrixAccessProxy<MT>& a, const MatrixAccessProxy<MT>& b ) noexcept;
507 
508 template< typename MT, typename T >
509 void swap( const MatrixAccessProxy<MT>& a, T& b ) noexcept;
510 
511 template< typename T, typename MT >
512 void swap( T& a, const MatrixAccessProxy<MT>& v ) noexcept;
514 //*************************************************************************************************
515 
516 
517 //*************************************************************************************************
529 template< typename MT >
530 inline void reset( const MatrixAccessProxy<MT>& proxy )
531 {
532  using blaze::reset;
533 
534  reset( proxy.get() );
535 }
536 //*************************************************************************************************
537 
538 
539 //*************************************************************************************************
550 template< typename MT >
551 inline void clear( const MatrixAccessProxy<MT>& proxy )
552 {
553  using blaze::clear;
554 
555  clear( proxy.get() );
556 }
557 //*************************************************************************************************
558 
559 
560 //*************************************************************************************************
570 template< bool RF, typename MT >
571 inline bool isDefault( const MatrixAccessProxy<MT>& proxy )
572 {
573  using blaze::isDefault;
574 
575  return isDefault<RF>( proxy.get() );
576 }
577 //*************************************************************************************************
578 
579 
580 //*************************************************************************************************
592 template< bool RF, typename MT >
593 inline bool isReal( const MatrixAccessProxy<MT>& proxy )
594 {
595  using blaze::isReal;
596 
597  return isReal<RF>( proxy.get() );
598 }
599 //*************************************************************************************************
600 
601 
602 //*************************************************************************************************
612 template< bool RF, typename MT >
613 inline bool isZero( const MatrixAccessProxy<MT>& proxy )
614 {
615  using blaze::isZero;
616 
617  return isZero<RF>( proxy.get() );
618 }
619 //*************************************************************************************************
620 
621 
622 //*************************************************************************************************
632 template< bool RF, typename MT >
633 inline bool isOne( const MatrixAccessProxy<MT>& proxy )
634 {
635  using blaze::isOne;
636 
637  return isOne<RF>( proxy.get() );
638 }
639 //*************************************************************************************************
640 
641 
642 //*************************************************************************************************
652 template< typename MT >
653 inline bool isnan( const MatrixAccessProxy<MT>& proxy )
654 {
655  using blaze::isnan;
656 
657  return isnan( proxy.get() );
658 }
659 //*************************************************************************************************
660 
661 
662 //*************************************************************************************************
670 template< typename MT >
671 inline void swap( const MatrixAccessProxy<MT>& a, const MatrixAccessProxy<MT>& b ) noexcept
672 {
673  using std::swap;
674 
675  swap( a.get(), b.get() );
676 }
677 //*************************************************************************************************
678 
679 
680 //*************************************************************************************************
688 template< typename MT, typename T >
689 inline void swap( const MatrixAccessProxy<MT>& a, T& b ) noexcept
690 {
691  using std::swap;
692 
693  swap( a.get(), b );
694 }
695 //*************************************************************************************************
696 
697 
698 //*************************************************************************************************
706 template< typename T, typename MT >
707 inline void swap( T& a, const MatrixAccessProxy<MT>& b ) noexcept
708 {
709  using std::swap;
710 
711  swap( a, b.get() );
712 }
713 //*************************************************************************************************
714 
715 } // namespace blaze
716 
717 #endif
bool isReal(const DiagonalProxy< MT > &proxy)
Returns whether the matrix element represents a real number.
Definition: DiagonalProxy.h:653
Header file for the isnan shim.
size_t i_
Row-index of the accessed sparse matrix element.
Definition: MatrixAccessProxy.h:171
Header file for auxiliary alias declarations.
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
Header file for the isZero shim.
void reset(const DiagonalProxy< MT > &proxy)
Resetting the represented element to the default initial values.
Definition: DiagonalProxy.h:591
RawReference get() const noexcept
Returning the value of the accessed sparse matrix element.
Definition: MatrixAccessProxy.h:430
Access proxy for sparse, matrices.The MatrixAccessProxy provides safe access to the elements of a no...
Definition: MatrixAccessProxy.h:101
ElementType_t< MT > RepresentedType
Type of the represented sparse matrix element.
Definition: MatrixAccessProxy.h:112
Header file for the reset shim.
Header file for the extended initializer_list functionality.
Header file for the Proxy class.
typename T::ElementType ElementType_t
Alias declaration for nested ElementType type definitions.The ElementType_t alias declaration provide...
Definition: Aliases.h:170
Constraint on the data type.
bool isZero(const DiagonalProxy< MT > &proxy)
Returns whether the represented element is 0.
Definition: DiagonalProxy.h:673
const MatrixAccessProxy & operator=(const MatrixAccessProxy &map) const
Copy assignment operator for MatrixAccessProxy.
Definition: MatrixAccessProxy.h:274
Namespace of the Blaze C++ math library.
Definition: Blaze.h:58
void swap(CompressedMatrix< Type, SO > &a, CompressedMatrix< Type, SO > &b) noexcept
Swapping the contents of two compressed matrices.
Definition: CompressedMatrix.h:5907
MatrixAccessProxy(MT &sm, size_t i, size_t j)
Initialization constructor for a MatrixAccessProxy.
Definition: MatrixAccessProxy.h:208
static constexpr bool rmm
Compile time flag indicating whether the given matrix type is a row-major matrix. ...
Definition: MatrixAccessProxy.h:107
bool isnan(const DiagonalProxy< MT > &proxy)
Returns whether the represented element is not a number.
Definition: DiagonalProxy.h:713
void clear(const DiagonalProxy< MT > &proxy)
Clearing the represented element.
Definition: DiagonalProxy.h:611
Header file for the isOne shim.
~MatrixAccessProxy()
The destructor for MatrixAccessProxy.
Definition: MatrixAccessProxy.h:249
typename T::Iterator Iterator_t
Alias declaration for nested Iterator type definitions.The Iterator_t alias declaration provides a co...
Definition: Aliases.h:190
Header file for run time assertion macros.
Header file for the relaxation flag types.
MT & sm_
Reference to the accessed sparse matrix.
Definition: MatrixAccessProxy.h:170
bool isRestricted() const noexcept
Returns whether the proxy represents a restricted sparse matrix element..
Definition: MatrixAccessProxy.h:445
bool isOne(const DiagonalProxy< MT > &proxy)
Returns whether the represented element is 1.
Definition: DiagonalProxy.h:693
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:281
RepresentedType & RawReference
Raw reference to the represented element.
Definition: MatrixAccessProxy.h:113
size_t j_
Column-index of the accessed sparse matrix element.
Definition: MatrixAccessProxy.h:172
Header file for the IsRowMajorMatrix type trait.
Initializer list type of the Blaze library.
bool isDefault(const DiagonalProxy< MT > &proxy)
Returns whether the represented element is in default state.
Definition: DiagonalProxy.h:631
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
Header file for the clear shim.
decltype(auto) map(const DenseMatrix< MT1, SO > &lhs, const DenseMatrix< MT2, SO > &rhs, OP op)
Evaluates the given binary operation on each single element of the dense matrices lhs and rhs...
Definition: DMatDMatMapExpr.h:1110