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>
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>
55 #include <blaze/util/Assert.h>
56 #include <blaze/util/Types.h>
57 
58 
59 namespace blaze {
60 
61 //=================================================================================================
62 //
63 // CLASS DEFINITION
64 //
65 //=================================================================================================
66 
67 //*************************************************************************************************
99 template< typename VT > // Type of the sparse vector
100 class VectorAccessProxy : public Proxy< VectorAccessProxy<VT>, ElementType_<VT> >
101 {
102  public:
103  //**Type definitions****************************************************************************
105  typedef RepresentedType& RawReference;
106  //**********************************************************************************************
107 
108  //**Constructors********************************************************************************
111  explicit inline VectorAccessProxy( VT& sv, size_t i );
112  inline VectorAccessProxy( const VectorAccessProxy& vap );
114  //**********************************************************************************************
115 
116  //**Destructor**********************************************************************************
119  inline ~VectorAccessProxy();
121  //**********************************************************************************************
122 
123  //**Operators***********************************************************************************
126  inline const VectorAccessProxy& operator=( const VectorAccessProxy& vap ) const;
127 
128  template< typename T >
129  inline const VectorAccessProxy& operator=( initializer_list<T> list ) const;
130 
131  template< typename T >
132  inline const VectorAccessProxy& operator=( initializer_list< initializer_list<T> > list ) const;
133 
134  template< typename T > inline const VectorAccessProxy& operator= ( const T& value ) const;
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;
140  //**********************************************************************************************
141 
142  //**Utility functions***************************************************************************
145  inline RawReference get() const noexcept;
146  inline bool isRestricted() const noexcept;
148  //**********************************************************************************************
149 
150  //**Conversion operator*************************************************************************
153  inline operator RawReference() const noexcept;
155  //**********************************************************************************************
156 
157  private:
158  //**Member variables****************************************************************************
161  VT& sv_;
162  size_t i_;
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 //*************************************************************************************************
196 template< typename VT > // Type of the sparse vector
197 inline VectorAccessProxy<VT>::VectorAccessProxy( VT& sv, size_t i )
198  : sv_( sv ) // Reference to the accessed sparse vector
199  , i_ ( i ) // Index of the accessed sparse vector element
200 {
201  const Iterator_<VT> element( sv_.find( i_ ) );
202  if( element == sv_.end() )
203  sv_.insert( i_, RepresentedType() );
204 }
205 //*************************************************************************************************
206 
207 
208 //*************************************************************************************************
213 template< typename VT > // Type of the sparse vector
215  : sv_( vap.sv_ ) // Reference to the accessed sparse vector
216  , i_ ( vap.i_ ) // Index of the accessed sparse vector element
217 {
218  BLAZE_INTERNAL_ASSERT( sv_.find( i_ ) != sv_.end(), "Missing vector element detected" );
219 }
220 //*************************************************************************************************
221 
222 
223 
224 
225 //=================================================================================================
226 //
227 // DESTRUCTOR
228 //
229 //=================================================================================================
230 
231 //*************************************************************************************************
234 template< typename VT > // Type of the sparse vector
236 {
237  const Iterator_<VT> element( sv_.find( i_ ) );
238  if( element != sv_.end() && isDefault( element->value() ) )
239  sv_.erase( element );
240 }
241 //*************************************************************************************************
242 
243 
244 
245 
246 //=================================================================================================
247 //
248 // OPERATORS
249 //
250 //=================================================================================================
251 
252 //*************************************************************************************************
258 template< typename VT > // Type of the sparse vector
260 {
261  get() = vap.get();
262  return *this;
263 }
264 //*************************************************************************************************
265 
266 
267 //*************************************************************************************************
273 template< typename VT > // Type of the sparse vector
274 template< typename T > // Type of the right-hand side elements
275 inline const VectorAccessProxy<VT>&
277 {
278  get() = list;
279  return *this;
280 }
281 //*************************************************************************************************
282 
283 
284 //*************************************************************************************************
290 template< typename VT > // Type of the sparse vector
291 template< typename T > // Type of the right-hand side elements
292 inline const VectorAccessProxy<VT>&
294 {
295  get() = list;
296  return *this;
297 }
298 //*************************************************************************************************
299 
300 
301 //*************************************************************************************************
307 template< typename VT > // Type of the sparse vector
308 template< typename T > // Type of the right-hand side value
309 inline const VectorAccessProxy<VT>& VectorAccessProxy<VT>::operator=( const T& value ) const
310 {
311  get() = value;
312  return *this;
313 }
314 //*************************************************************************************************
315 
316 
317 //*************************************************************************************************
323 template< typename VT > // Type of the sparse vector
324 template< typename T > // Type of the right-hand side value
325 inline const VectorAccessProxy<VT>& VectorAccessProxy<VT>::operator+=( const T& value ) const
326 {
327  get() += value;
328  return *this;
329 }
330 //*************************************************************************************************
331 
332 
333 //*************************************************************************************************
339 template< typename VT > // Type of the sparse vector
340 template< typename T > // Type of the right-hand side value
341 inline const VectorAccessProxy<VT>& VectorAccessProxy<VT>::operator-=( const T& value ) const
342 {
343  get() -= value;
344  return *this;
345 }
346 //*************************************************************************************************
347 
348 
349 //*************************************************************************************************
355 template< typename VT > // Type of the sparse vector
356 template< typename T > // Type of the right-hand side value
357 inline const VectorAccessProxy<VT>& VectorAccessProxy<VT>::operator*=( const T& value ) const
358 {
359  get() *= value;
360  return *this;
361 }
362 //*************************************************************************************************
363 
364 
365 //*************************************************************************************************
371 template< typename VT > // Type of the sparse vector
372 template< typename T > // Type of the right-hand side value
373 inline const VectorAccessProxy<VT>& VectorAccessProxy<VT>::operator/=( const T& value ) const
374 {
375  get() /= value;
376  return *this;
377 }
378 //*************************************************************************************************
379 
380 
381 
382 
383 //=================================================================================================
384 //
385 // UTILITY FUNCTIONS
386 //
387 //=================================================================================================
388 
389 //*************************************************************************************************
394 template< typename VT > // Type of the sparse vector
396 {
397  const Iterator_<VT> element( sv_.find( i_ ) );
398  BLAZE_INTERNAL_ASSERT( element != sv_.end(), "Missing vector element detected" );
399  return element->value();
400 }
401 //*************************************************************************************************
402 
403 
404 //*************************************************************************************************
409 template< typename VT > // Type of the sparse vector
410 inline bool VectorAccessProxy<VT>::isRestricted() const noexcept
411 {
412  return false;
413 }
414 //*************************************************************************************************
415 
416 
417 
418 
419 //=================================================================================================
420 //
421 // CONVERSION OPERATOR
422 //
423 //=================================================================================================
424 
425 //*************************************************************************************************
430 template< typename VT > // Type of the sparse vector
431 inline VectorAccessProxy<VT>::operator RawReference() const noexcept
432 {
433  return get();
434 }
435 //*************************************************************************************************
436 
437 
438 
439 
440 //=================================================================================================
441 //
442 // GLOBAL FUNCTIONS
443 //
444 //=================================================================================================
445 
446 //*************************************************************************************************
449 template< typename VT >
450 inline void reset( const VectorAccessProxy<VT>& proxy );
451 
452 template< typename VT >
453 inline void clear( const VectorAccessProxy<VT>& proxy );
454 
455 template< typename VT >
456 inline bool isDefault( const VectorAccessProxy<VT>& proxy );
457 
458 template< typename VT >
459 inline bool isReal( const VectorAccessProxy<VT>& proxy );
460 
461 template< typename VT >
462 inline bool isZero( const VectorAccessProxy<VT>& proxy );
463 
464 template< typename VT >
465 inline bool isOne( const VectorAccessProxy<VT>& proxy );
466 
467 template< typename VT >
468 inline bool isnan( const VectorAccessProxy<VT>& proxy );
469 
470 template< typename VT >
471 inline void swap( const VectorAccessProxy<VT>& a, const VectorAccessProxy<VT>& b ) noexcept;
472 
473 template< typename VT, typename T >
474 inline void swap( const VectorAccessProxy<VT>& a, T& b ) noexcept;
475 
476 template< typename T, typename VT >
477 inline void swap( T& a, const VectorAccessProxy<VT>& v ) noexcept;
479 //*************************************************************************************************
480 
481 
482 //*************************************************************************************************
489 template< typename VT >
490 inline void reset( const VectorAccessProxy<VT>& proxy )
491 {
492  using blaze::reset;
493 
494  reset( proxy.get() );
495 }
496 //*************************************************************************************************
497 
498 
499 //*************************************************************************************************
506 template< typename VT >
507 inline void clear( const VectorAccessProxy<VT>& proxy )
508 {
509  using blaze::clear;
510 
511  clear( proxy.get() );
512 }
513 //*************************************************************************************************
514 
515 
516 //*************************************************************************************************
526 template< typename VT >
527 inline bool isDefault( const VectorAccessProxy<VT>& proxy )
528 {
529  using blaze::isDefault;
530 
531  return isDefault( proxy.get() );
532 }
533 //*************************************************************************************************
534 
535 
536 //*************************************************************************************************
548 template< typename VT >
549 inline bool isReal( const VectorAccessProxy<VT>& proxy )
550 {
551  using blaze::isReal;
552 
553  return isReal( proxy.get() );
554 }
555 //*************************************************************************************************
556 
557 
558 //*************************************************************************************************
568 template< typename VT >
569 inline bool isZero( const VectorAccessProxy<VT>& proxy )
570 {
571  using blaze::isZero;
572 
573  return isZero( proxy.get() );
574 }
575 //*************************************************************************************************
576 
577 
578 //*************************************************************************************************
588 template< typename VT >
589 inline bool isOne( const VectorAccessProxy<VT>& proxy )
590 {
591  using blaze::isOne;
592 
593  return isOne( proxy.get() );
594 }
595 //*************************************************************************************************
596 
597 
598 //*************************************************************************************************
608 template< typename VT >
609 inline bool isnan( const VectorAccessProxy<VT>& proxy )
610 {
611  using blaze::isnan;
612 
613  return isnan( proxy.get() );
614 }
615 //*************************************************************************************************
616 
617 
618 //*************************************************************************************************
626 template< typename VT >
627 inline void swap( const VectorAccessProxy<VT>& a, const VectorAccessProxy<VT>& b ) noexcept
628 {
629  using std::swap;
630 
631  swap( a.get(), b.get() );
632 }
633 //*************************************************************************************************
634 
635 
636 //*************************************************************************************************
644 template< typename VT, typename T >
645 inline void swap( const VectorAccessProxy<VT>& a, T& b ) noexcept
646 {
647  using std::swap;
648 
649  swap( a.get(), b );
650 }
651 //*************************************************************************************************
652 
653 
654 //*************************************************************************************************
662 template< typename T, typename VT >
663 inline void swap( T& a, const VectorAccessProxy<VT>& b ) noexcept
664 {
665  using std::swap;
666 
667  swap( a, b.get() );
668 }
669 //*************************************************************************************************
670 
671 } // namespace blaze
672 
673 #endif
Header file for the isnan shim.
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.
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
Access proxy for sparse, N-dimensional vectors.The VectorAccessProxy provides safe access to the elem...
Definition: VectorAccessProxy.h:100
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:161
VectorAccessProxy(VT &sv, size_t i)
Initialization constructor for a VectorAccessProxy.
Definition: VectorAccessProxy.h:197
size_t i_
Index of the accessed sparse vector element.
Definition: VectorAccessProxy.h:162
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.
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
ElementType_< VT > RepresentedType
Type of the represented sparse vector element.
Definition: VectorAccessProxy.h:104
bool isRestricted() const noexcept
Returns whether the proxy represents a restricted sparse vector element..
Definition: VectorAccessProxy.h:410
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:105
const VectorAccessProxy & operator=(const VectorAccessProxy &vap) const
Copy assignment operator for VectorAccessProxy.
Definition: VectorAccessProxy.h:259
#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.
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.
RawReference get() const noexcept
Returning the value of the accessed sparse vector element.
Definition: VectorAccessProxy.h:395
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
Initializer list type of the Blaze library.
~VectorAccessProxy()
The destructor for VectorAccessProxy.
Definition: VectorAccessProxy.h:235
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