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
101 class MatrixAccessProxy : public Proxy< MatrixAccessProxy<MT>, ElementType_<MT> >
102 {
103  private:
104  //**Enumerations********************************************************************************
106  enum : bool { rmm = IsRowMajorMatrix<MT>::value };
107  //**********************************************************************************************
108 
109  public:
110  //**Type definitions****************************************************************************
112  typedef RepresentedType& RawReference;
113  //**********************************************************************************************
114 
115  //**Constructors********************************************************************************
118  explicit inline MatrixAccessProxy( MT& sm, size_t i, size_t j );
119  inline MatrixAccessProxy( const MatrixAccessProxy& map );
121  //**********************************************************************************************
122 
123  //**Destructor**********************************************************************************
126  inline ~MatrixAccessProxy();
128  //**********************************************************************************************
129 
130  //**Operators***********************************************************************************
133  inline const MatrixAccessProxy& operator=( const MatrixAccessProxy& map ) const;
134 
135  template< typename T >
136  inline const MatrixAccessProxy& operator=( initializer_list<T> list ) const;
137 
138  template< typename T >
139  inline const MatrixAccessProxy& operator=( initializer_list< initializer_list<T> > list ) const;
140 
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;
145  template< typename T > inline const MatrixAccessProxy& operator/=( const T& value ) const;
147  //**********************************************************************************************
148 
149  //**Utility functions***************************************************************************
152  inline RawReference get() const noexcept;
153  inline bool isRestricted() const noexcept;
155  //**********************************************************************************************
156 
157  //**Conversion operator*************************************************************************
160  inline operator RawReference() const noexcept;
162  //**********************************************************************************************
163 
164  private:
165  //**Member variables****************************************************************************
168  MT& sm_;
169  size_t i_;
170  size_t j_;
171 
172  //**********************************************************************************************
173 
174  //**Forbidden operations************************************************************************
177  void* operator&() const;
178 
179  //**********************************************************************************************
180 
181  //**Compile time checks*************************************************************************
185  //**********************************************************************************************
186 };
187 //*************************************************************************************************
188 
189 
190 
191 
192 //=================================================================================================
193 //
194 // CONSTRUCTORS
195 //
196 //=================================================================================================
197 
198 //*************************************************************************************************
205 template< typename MT > // Type of the sparse matrix
206 inline MatrixAccessProxy<MT>::MatrixAccessProxy( MT& sm, size_t i, size_t j )
207  : sm_( sm ) // Reference to the accessed sparse matrix
208  , i_ ( i ) // Row-index of the accessed sparse matrix element
209  , j_ ( j ) // Column-index of the accessed sparse matrix element
210 {
211  const Iterator_<MT> element( sm_.find( i_, j_ ) );
212  const size_t index( rmm ? i_ : j_ );
213  if( element == sm_.end(index) )
214  sm_.insert( i_, j_, RepresentedType() );
215 }
216 //*************************************************************************************************
217 
218 
219 //*************************************************************************************************
224 template< typename MT > // Type of the sparse matrix
226  : sm_( map.sm_ ) // Reference to the accessed sparse matrix
227  , i_ ( map.i_ ) // Row-index of the accessed sparse matrix element
228  , j_ ( map.j_ ) // Column-index of the accessed sparse matrix element
229 {
230  BLAZE_INTERNAL_ASSERT( sm_.find(i_,j_) != sm_.end( rmm ? i_ : j_ ), "Missing matrix element detected" );
231 }
232 //*************************************************************************************************
233 
234 
235 
236 
237 //=================================================================================================
238 //
239 // DESTRUCTORS
240 //
241 //=================================================================================================
242 
243 //*************************************************************************************************
246 template< typename MT > // Type of the sparse matrix
248 {
249  const Iterator_<MT> element( sm_.find( i_, j_ ) );
250  const size_t index( rmm ? i_ : j_ );
251  if( element != sm_.end( index ) && isDefault<strict>( element->value() ) )
252  sm_.erase( index, element );
253 }
254 //*************************************************************************************************
255 
256 
257 
258 
259 //=================================================================================================
260 //
261 // OPERATORS
262 //
263 //=================================================================================================
264 
265 //*************************************************************************************************
271 template< typename MT > // Type of the sparse matrix
273 {
274  get() = map.get();
275  return *this;
276 }
277 //*************************************************************************************************
278 
279 
280 //*************************************************************************************************
286 template< typename VT > // Type of the sparse matrix
287 template< typename T > // Type of the right-hand side elements
288 inline const MatrixAccessProxy<VT>&
290 {
291  get() = list;
292  return *this;
293 }
294 //*************************************************************************************************
295 
296 
297 //*************************************************************************************************
303 template< typename VT > // Type of the sparse matrix
304 template< typename T > // Type of the right-hand side elements
305 inline const MatrixAccessProxy<VT>&
307 {
308  get() = list;
309  return *this;
310 }
311 //*************************************************************************************************
312 
313 
314 //*************************************************************************************************
320 template< typename MT > // Type of the sparse matrix
321 template< typename T > // Type of the right-hand side value
322 inline const MatrixAccessProxy<MT>& MatrixAccessProxy<MT>::operator=( const T& value ) const
323 {
324  get() = value;
325  return *this;
326 }
327 //*************************************************************************************************
328 
329 
330 //*************************************************************************************************
336 template< typename MT > // Type of the sparse matrix
337 template< typename T > // Type of the right-hand side value
338 inline const MatrixAccessProxy<MT>& MatrixAccessProxy<MT>::operator+=( const T& value ) const
339 {
340  get() += value;
341  return *this;
342 }
343 //*************************************************************************************************
344 
345 
346 //*************************************************************************************************
352 template< typename MT > // Type of the sparse matrix
353 template< typename T > // Type of the right-hand side value
354 inline const MatrixAccessProxy<MT>& MatrixAccessProxy<MT>::operator-=( const T& value ) const
355 {
356  get() -= value;
357  return *this;
358 }
359 //*************************************************************************************************
360 
361 
362 //*************************************************************************************************
368 template< typename MT > // Type of the sparse matrix
369 template< typename T > // Type of the right-hand side value
370 inline const MatrixAccessProxy<MT>& MatrixAccessProxy<MT>::operator*=( const T& value ) const
371 {
372  get() *= value;
373  return *this;
374 }
375 //*************************************************************************************************
376 
377 
378 //*************************************************************************************************
384 template< typename MT > // Type of the sparse matrix
385 template< typename T > // Type of the right-hand side value
386 inline const MatrixAccessProxy<MT>& MatrixAccessProxy<MT>::operator/=( const T& value ) const
387 {
388  get() /= value;
389  return *this;
390 }
391 //*************************************************************************************************
392 
393 
394 
395 
396 //=================================================================================================
397 //
398 // UTILITY FUNCTIONS
399 //
400 //=================================================================================================
401 
402 //*************************************************************************************************
407 template< typename MT > // Type of the sparse matrix
409 {
410  const Iterator_<MT> element( sm_.find( i_, j_ ) );
411  BLAZE_INTERNAL_ASSERT( element != sm_.end( rmm ? i_ : j_ ), "Missing matrix element detected" );
412  return element->value();
413 }
414 //*************************************************************************************************
415 
416 
417 //*************************************************************************************************
422 template< typename MT > // Type of the sparse matrix
423 inline bool MatrixAccessProxy<MT>::isRestricted() const noexcept
424 {
425  return false;
426 }
427 //*************************************************************************************************
428 
429 
430 
431 
432 //=================================================================================================
433 //
434 // CONVERSION OPERATOR
435 //
436 //=================================================================================================
437 
438 //*************************************************************************************************
443 template< typename MT > // Type of the sparse matrix
445 {
446  return get();
447 }
448 //*************************************************************************************************
449 
450 
451 
452 
453 //=================================================================================================
454 //
455 // GLOBAL FUNCTIONS
456 //
457 //=================================================================================================
458 
459 //*************************************************************************************************
462 template< typename MT >
463 inline void reset( const MatrixAccessProxy<MT>& proxy );
464 
465 template< typename MT >
466 inline void clear( const MatrixAccessProxy<MT>& proxy );
467 
468 template< bool RF, typename MT >
469 inline bool isDefault( const MatrixAccessProxy<MT>& proxy );
470 
471 template< bool RF, typename MT >
472 inline bool isReal( const MatrixAccessProxy<MT>& proxy );
473 
474 template< bool RF, typename MT >
475 inline bool isZero( const MatrixAccessProxy<MT>& proxy );
476 
477 template< bool RF, typename MT >
478 inline bool isOne( const MatrixAccessProxy<MT>& proxy );
479 
480 template< typename MT >
481 inline bool isnan( const MatrixAccessProxy<MT>& proxy );
482 
483 template< typename MT >
484 inline void swap( const MatrixAccessProxy<MT>& a, const MatrixAccessProxy<MT>& b ) noexcept;
485 
486 template< typename MT, typename T >
487 inline void swap( const MatrixAccessProxy<MT>& a, T& b ) noexcept;
488 
489 template< typename T, typename MT >
490 inline void swap( T& a, const MatrixAccessProxy<MT>& v ) noexcept;
492 //*************************************************************************************************
493 
494 
495 //*************************************************************************************************
507 template< typename MT >
508 inline void reset( const MatrixAccessProxy<MT>& proxy )
509 {
510  using blaze::reset;
511 
512  reset( proxy.get() );
513 }
514 //*************************************************************************************************
515 
516 
517 //*************************************************************************************************
528 template< typename MT >
529 inline void clear( const MatrixAccessProxy<MT>& proxy )
530 {
531  using blaze::clear;
532 
533  clear( proxy.get() );
534 }
535 //*************************************************************************************************
536 
537 
538 //*************************************************************************************************
548 template< bool RF, typename MT >
549 inline bool isDefault( const MatrixAccessProxy<MT>& proxy )
550 {
551  using blaze::isDefault;
552 
553  return isDefault<RF>( proxy.get() );
554 }
555 //*************************************************************************************************
556 
557 
558 //*************************************************************************************************
570 template< bool RF, typename MT >
571 inline bool isReal( const MatrixAccessProxy<MT>& proxy )
572 {
573  using blaze::isReal;
574 
575  return isReal<RF>( proxy.get() );
576 }
577 //*************************************************************************************************
578 
579 
580 //*************************************************************************************************
590 template< bool RF, typename MT >
591 inline bool isZero( const MatrixAccessProxy<MT>& proxy )
592 {
593  using blaze::isZero;
594 
595  return isZero<RF>( proxy.get() );
596 }
597 //*************************************************************************************************
598 
599 
600 //*************************************************************************************************
610 template< bool RF, typename MT >
611 inline bool isOne( const MatrixAccessProxy<MT>& proxy )
612 {
613  using blaze::isOne;
614 
615  return isOne<RF>( proxy.get() );
616 }
617 //*************************************************************************************************
618 
619 
620 //*************************************************************************************************
630 template< typename MT >
631 inline bool isnan( const MatrixAccessProxy<MT>& proxy )
632 {
633  using blaze::isnan;
634 
635  return isnan( proxy.get() );
636 }
637 //*************************************************************************************************
638 
639 
640 //*************************************************************************************************
648 template< typename MT >
649 inline void swap( const MatrixAccessProxy<MT>& a, const MatrixAccessProxy<MT>& b ) noexcept
650 {
651  using std::swap;
652 
653  swap( a.get(), b.get() );
654 }
655 //*************************************************************************************************
656 
657 
658 //*************************************************************************************************
666 template< typename MT, typename T >
667 inline void swap( const MatrixAccessProxy<MT>& a, T& b ) noexcept
668 {
669  using std::swap;
670 
671  swap( a.get(), b );
672 }
673 //*************************************************************************************************
674 
675 
676 //*************************************************************************************************
684 template< typename T, typename MT >
685 inline void swap( T& a, const MatrixAccessProxy<MT>& b ) noexcept
686 {
687  using std::swap;
688 
689  swap( a, b.get() );
690 }
691 //*************************************************************************************************
692 
693 } // namespace blaze
694 
695 #endif
bool isReal(const DiagonalProxy< MT > &proxy)
Returns whether the matrix element represents a real number.
Definition: DiagonalProxy.h:595
Header file for the isnan shim.
size_t i_
Row-index of the accessed sparse matrix element.
Definition: MatrixAccessProxy.h:169
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
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:408
Access proxy for sparse, matrices.The MatrixAccessProxy provides safe access to the elements of a no...
Definition: MatrixAccessProxy.h:101
Header file for the Proxy class.
Header file for the std::initializer_list aliases.
Constraint on the data type.
bool isZero(const DiagonalProxy< MT > &proxy)
Returns whether the represented element is 0.
Definition: DiagonalProxy.h:615
const MatrixAccessProxy & operator=(const MatrixAccessProxy &map) const
Copy assignment operator for MatrixAccessProxy.
Definition: MatrixAccessProxy.h:272
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:5635
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:206
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:111
~MatrixAccessProxy()
The destructor for MatrixAccessProxy.
Definition: MatrixAccessProxy.h:247
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:168
bool isRestricted() const noexcept
Returns whether the proxy represents a restricted sparse matrix element..
Definition: MatrixAccessProxy.h:423
Header file for the reset shim.
bool isOne(const DiagonalProxy< MT > &proxy)
Returns whether the represented element is 1.
Definition: DiagonalProxy.h:635
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:267
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:170
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:112
bool isDefault(const DiagonalProxy< MT > &proxy)
Returns whether the represented element is in default state.
Definition: DiagonalProxy.h:573
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