VectorAccessProxy.h
Go to the documentation of this file.
1 //=================================================================================================
33 //=================================================================================================
34 
35 #ifndef _BLAZE_MATH_SPARSE_VECTORACCESSPROXY_H_
36 #define _BLAZE_MATH_SPARSE_VECTORACCESSPROXY_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>
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 //*************************************************************************************************
100 template< typename VT > // Type of the sparse vector
101 class VectorAccessProxy : public Proxy< VectorAccessProxy<VT>, ElementType_<VT> >
102 {
103  public:
104  //**Type definitions****************************************************************************
106  typedef RepresentedType& RawReference;
107  //**********************************************************************************************
108 
109  //**Constructors********************************************************************************
112  explicit inline VectorAccessProxy( VT& sv, size_t i );
113  inline VectorAccessProxy( const VectorAccessProxy& vap );
115  //**********************************************************************************************
116 
117  //**Destructor**********************************************************************************
120  inline ~VectorAccessProxy();
122  //**********************************************************************************************
123 
124  //**Operators***********************************************************************************
127  inline const VectorAccessProxy& operator=( const VectorAccessProxy& vap ) const;
128 
129  template< typename T >
130  inline const VectorAccessProxy& operator=( initializer_list<T> list ) const;
131 
132  template< typename T >
133  inline const VectorAccessProxy& operator=( initializer_list< initializer_list<T> > list ) const;
134 
135  template< typename T > inline const VectorAccessProxy& operator= ( const T& value ) const;
136  template< typename T > inline const VectorAccessProxy& operator+=( const T& value ) const;
137  template< typename T > inline const VectorAccessProxy& operator-=( const T& value ) const;
138  template< typename T > inline const VectorAccessProxy& operator*=( const T& value ) const;
139  template< typename T > inline const VectorAccessProxy& operator/=( const T& value ) const;
141  //**********************************************************************************************
142 
143  //**Utility functions***************************************************************************
146  inline RawReference get() const noexcept;
147  inline bool isRestricted() const noexcept;
149  //**********************************************************************************************
150 
151  //**Conversion operator*************************************************************************
154  inline operator RawReference() const noexcept;
156  //**********************************************************************************************
157 
158  private:
159  //**Member variables****************************************************************************
162  VT& sv_;
163  size_t i_;
164 
165  //**********************************************************************************************
166 
167  //**Forbidden operations************************************************************************
170  void* operator&() const;
171 
172  //**********************************************************************************************
173 
174  //**Compile time checks*************************************************************************
178  //**********************************************************************************************
179 };
180 //*************************************************************************************************
181 
182 
183 
184 
185 //=================================================================================================
186 //
187 // CONSTRUCTORS
188 //
189 //=================================================================================================
190 
191 //*************************************************************************************************
197 template< typename VT > // Type of the sparse vector
198 inline VectorAccessProxy<VT>::VectorAccessProxy( VT& sv, size_t i )
199  : sv_( sv ) // Reference to the accessed sparse vector
200  , i_ ( i ) // Index of the accessed sparse vector element
201 {
202  const Iterator_<VT> element( sv_.find( i_ ) );
203  if( element == sv_.end() )
204  sv_.insert( i_, RepresentedType() );
205 }
206 //*************************************************************************************************
207 
208 
209 //*************************************************************************************************
214 template< typename VT > // Type of the sparse vector
216  : sv_( vap.sv_ ) // Reference to the accessed sparse vector
217  , i_ ( vap.i_ ) // Index of the accessed sparse vector element
218 {
219  BLAZE_INTERNAL_ASSERT( sv_.find( i_ ) != sv_.end(), "Missing vector element detected" );
220 }
221 //*************************************************************************************************
222 
223 
224 
225 
226 //=================================================================================================
227 //
228 // DESTRUCTOR
229 //
230 //=================================================================================================
231 
232 //*************************************************************************************************
235 template< typename VT > // Type of the sparse vector
237 {
238  const Iterator_<VT> element( sv_.find( i_ ) );
239  if( element != sv_.end() && isDefault<strict>( element->value() ) )
240  sv_.erase( element );
241 }
242 //*************************************************************************************************
243 
244 
245 
246 
247 //=================================================================================================
248 //
249 // OPERATORS
250 //
251 //=================================================================================================
252 
253 //*************************************************************************************************
259 template< typename VT > // Type of the sparse vector
261 {
262  get() = vap.get();
263  return *this;
264 }
265 //*************************************************************************************************
266 
267 
268 //*************************************************************************************************
274 template< typename VT > // Type of the sparse vector
275 template< typename T > // Type of the right-hand side elements
276 inline const VectorAccessProxy<VT>&
278 {
279  get() = list;
280  return *this;
281 }
282 //*************************************************************************************************
283 
284 
285 //*************************************************************************************************
291 template< typename VT > // Type of the sparse vector
292 template< typename T > // Type of the right-hand side elements
293 inline const VectorAccessProxy<VT>&
295 {
296  get() = list;
297  return *this;
298 }
299 //*************************************************************************************************
300 
301 
302 //*************************************************************************************************
308 template< typename VT > // Type of the sparse vector
309 template< typename T > // Type of the right-hand side value
310 inline const VectorAccessProxy<VT>& VectorAccessProxy<VT>::operator=( const T& value ) const
311 {
312  get() = value;
313  return *this;
314 }
315 //*************************************************************************************************
316 
317 
318 //*************************************************************************************************
324 template< typename VT > // Type of the sparse vector
325 template< typename T > // Type of the right-hand side value
326 inline const VectorAccessProxy<VT>& VectorAccessProxy<VT>::operator+=( const T& value ) const
327 {
328  get() += value;
329  return *this;
330 }
331 //*************************************************************************************************
332 
333 
334 //*************************************************************************************************
340 template< typename VT > // Type of the sparse vector
341 template< typename T > // Type of the right-hand side value
342 inline const VectorAccessProxy<VT>& VectorAccessProxy<VT>::operator-=( const T& value ) const
343 {
344  get() -= value;
345  return *this;
346 }
347 //*************************************************************************************************
348 
349 
350 //*************************************************************************************************
356 template< typename VT > // Type of the sparse vector
357 template< typename T > // Type of the right-hand side value
358 inline const VectorAccessProxy<VT>& VectorAccessProxy<VT>::operator*=( const T& value ) const
359 {
360  get() *= value;
361  return *this;
362 }
363 //*************************************************************************************************
364 
365 
366 //*************************************************************************************************
372 template< typename VT > // Type of the sparse vector
373 template< typename T > // Type of the right-hand side value
374 inline const VectorAccessProxy<VT>& VectorAccessProxy<VT>::operator/=( const T& value ) const
375 {
376  get() /= value;
377  return *this;
378 }
379 //*************************************************************************************************
380 
381 
382 
383 
384 //=================================================================================================
385 //
386 // UTILITY FUNCTIONS
387 //
388 //=================================================================================================
389 
390 //*************************************************************************************************
395 template< typename VT > // Type of the sparse vector
397 {
398  const Iterator_<VT> element( sv_.find( i_ ) );
399  BLAZE_INTERNAL_ASSERT( element != sv_.end(), "Missing vector element detected" );
400  return element->value();
401 }
402 //*************************************************************************************************
403 
404 
405 //*************************************************************************************************
410 template< typename VT > // Type of the sparse vector
411 inline bool VectorAccessProxy<VT>::isRestricted() const noexcept
412 {
413  return false;
414 }
415 //*************************************************************************************************
416 
417 
418 
419 
420 //=================================================================================================
421 //
422 // CONVERSION OPERATOR
423 //
424 //=================================================================================================
425 
426 //*************************************************************************************************
431 template< typename VT > // Type of the sparse vector
433 {
434  return get();
435 }
436 //*************************************************************************************************
437 
438 
439 
440 
441 //=================================================================================================
442 //
443 // GLOBAL FUNCTIONS
444 //
445 //=================================================================================================
446 
447 //*************************************************************************************************
450 template< typename VT >
451 inline void reset( const VectorAccessProxy<VT>& proxy );
452 
453 template< typename VT >
454 inline void clear( const VectorAccessProxy<VT>& proxy );
455 
456 template< bool RF, typename VT >
457 inline bool isDefault( const VectorAccessProxy<VT>& proxy );
458 
459 template< bool RF, typename VT >
460 inline bool isReal( const VectorAccessProxy<VT>& proxy );
461 
462 template< bool RF, typename VT >
463 inline bool isZero( const VectorAccessProxy<VT>& proxy );
464 
465 template< bool RF, typename VT >
466 inline bool isOne( const VectorAccessProxy<VT>& proxy );
467 
468 template< typename VT >
469 inline bool isnan( const VectorAccessProxy<VT>& proxy );
470 
471 template< typename VT >
472 inline void swap( const VectorAccessProxy<VT>& a, const VectorAccessProxy<VT>& b ) noexcept;
473 
474 template< typename VT, typename T >
475 inline void swap( const VectorAccessProxy<VT>& a, T& b ) noexcept;
476 
477 template< typename T, typename VT >
478 inline void swap( T& a, const VectorAccessProxy<VT>& v ) noexcept;
480 //*************************************************************************************************
481 
482 
483 //*************************************************************************************************
490 template< typename VT >
491 inline void reset( const VectorAccessProxy<VT>& proxy )
492 {
493  using blaze::reset;
494 
495  reset( proxy.get() );
496 }
497 //*************************************************************************************************
498 
499 
500 //*************************************************************************************************
507 template< typename VT >
508 inline void clear( const VectorAccessProxy<VT>& proxy )
509 {
510  using blaze::clear;
511 
512  clear( proxy.get() );
513 }
514 //*************************************************************************************************
515 
516 
517 //*************************************************************************************************
527 template< bool RF, typename VT >
528 inline bool isDefault( const VectorAccessProxy<VT>& proxy )
529 {
530  using blaze::isDefault;
531 
532  return isDefault<RF>( proxy.get() );
533 }
534 //*************************************************************************************************
535 
536 
537 //*************************************************************************************************
549 template< bool RF, typename VT >
550 inline bool isReal( const VectorAccessProxy<VT>& proxy )
551 {
552  using blaze::isReal;
553 
554  return isReal<RF>( proxy.get() );
555 }
556 //*************************************************************************************************
557 
558 
559 //*************************************************************************************************
569 template< bool RF, typename VT >
570 inline bool isZero( const VectorAccessProxy<VT>& proxy )
571 {
572  using blaze::isZero;
573 
574  return isZero<RF>( proxy.get() );
575 }
576 //*************************************************************************************************
577 
578 
579 //*************************************************************************************************
589 template< bool RF, typename VT >
590 inline bool isOne( const VectorAccessProxy<VT>& proxy )
591 {
592  using blaze::isOne;
593 
594  return isOne<RF>( proxy.get() );
595 }
596 //*************************************************************************************************
597 
598 
599 //*************************************************************************************************
609 template< typename VT >
610 inline bool isnan( const VectorAccessProxy<VT>& proxy )
611 {
612  using blaze::isnan;
613 
614  return isnan( proxy.get() );
615 }
616 //*************************************************************************************************
617 
618 
619 //*************************************************************************************************
627 template< typename VT >
628 inline void swap( const VectorAccessProxy<VT>& a, const VectorAccessProxy<VT>& b ) noexcept
629 {
630  using std::swap;
631 
632  swap( a.get(), b.get() );
633 }
634 //*************************************************************************************************
635 
636 
637 //*************************************************************************************************
645 template< typename VT, typename T >
646 inline void swap( const VectorAccessProxy<VT>& a, T& b ) noexcept
647 {
648  using std::swap;
649 
650  swap( a.get(), b );
651 }
652 //*************************************************************************************************
653 
654 
655 //*************************************************************************************************
663 template< typename T, typename VT >
664 inline void swap( T& a, const VectorAccessProxy<VT>& b ) noexcept
665 {
666  using std::swap;
667 
668  swap( a, b.get() );
669 }
670 //*************************************************************************************************
671 
672 } // namespace blaze
673 
674 #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.
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
Access proxy for sparse, N-dimensional vectors.The VectorAccessProxy provides safe access to the elem...
Definition: VectorAccessProxy.h:101
void reset(const DiagonalProxy< MT > &proxy)
Resetting the represented element to the default initial values.
Definition: DiagonalProxy.h:533
VT & sv_
Reference to the accessed sparse vector.
Definition: VectorAccessProxy.h:162
VectorAccessProxy(VT &sv, size_t i)
Initialization constructor for a VectorAccessProxy.
Definition: VectorAccessProxy.h:198
size_t i_
Index of the accessed sparse vector element.
Definition: VectorAccessProxy.h:163
Header file for the Proxy class.
Header file for the std::initializer_list aliases.
bool isZero(const DiagonalProxy< MT > &proxy)
Returns whether the represented element is 0.
Definition: DiagonalProxy.h:615
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
ElementType_< VT > RepresentedType
Type of the represented sparse vector element.
Definition: VectorAccessProxy.h:105
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
RepresentedType & RawReference
Raw reference to the represented element.
Definition: VectorAccessProxy.h:106
#define BLAZE_CONSTRAINT_MUST_BE_SPARSE_VECTOR_TYPE(T)
Constraint on the data type.In case the given data type T is not a sparse, N-dimensional vector type...
Definition: SparseVector.h:61
bool isnan(const DiagonalProxy< MT > &proxy)
Returns whether the represented element is not a number.
Definition: DiagonalProxy.h:655
Constraint on the data type.
const VectorAccessProxy & operator=(const VectorAccessProxy &vap) const
Copy assignment operator for VectorAccessProxy.
Definition: VectorAccessProxy.h:260
void clear(const DiagonalProxy< MT > &proxy)
Clearing the represented element.
Definition: DiagonalProxy.h:553
Header file for the isOne shim.
Header file for run time assertion macros.
Header file for the relaxation flag types.
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
Initializer list type of the Blaze library.
RawReference get() const noexcept
Returning the value of the accessed sparse vector element.
Definition: VectorAccessProxy.h:396
~VectorAccessProxy()
The destructor for VectorAccessProxy.
Definition: VectorAccessProxy.h:236
bool isRestricted() const noexcept
Returns whether the proxy represents a restricted sparse vector element..
Definition: VectorAccessProxy.h:411
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