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 <algorithm>
45 #include <blaze/math/proxy/Proxy.h>
46 #include <blaze/math/shims/Clear.h>
49 #include <blaze/math/shims/IsNaN.h>
50 #include <blaze/math/shims/IsOne.h>
53 #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>, typename VT::ElementType >
101 {
102  public:
103  //**Type definitions****************************************************************************
104  typedef typename VT::ElementType RepresentedType;
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  template< typename T > inline const VectorAccessProxy& operator= ( const T& value ) const;
128  template< typename T > inline const VectorAccessProxy& operator+=( const T& value ) const;
129  template< typename T > inline const VectorAccessProxy& operator-=( const T& value ) const;
130  template< typename T > inline const VectorAccessProxy& operator*=( const T& value ) const;
131  template< typename T > inline const VectorAccessProxy& operator/=( const T& value ) const;
133  //**********************************************************************************************
134 
135  //**Utility functions***************************************************************************
138  inline RawReference get() const;
139  inline bool isRestricted() const;
141  //**********************************************************************************************
142 
143  //**Conversion operator*************************************************************************
146  inline operator RawReference() const;
148  //**********************************************************************************************
149 
150  private:
151  //**Member variables****************************************************************************
154  VT& sv_;
155  size_t i_;
156 
157  //**********************************************************************************************
158 
159  //**Forbidden operations************************************************************************
162  void* operator&() const;
163 
164  //**********************************************************************************************
165 
166  //**Compile time checks*************************************************************************
170  //**********************************************************************************************
171 };
172 //*************************************************************************************************
173 
174 
175 
176 
177 //=================================================================================================
178 //
179 // CONSTRUCTORS
180 //
181 //=================================================================================================
182 
183 //*************************************************************************************************
189 template< typename VT > // Type of the sparse vector
191  : sv_( sv ) // Reference to the accessed sparse vector
192  , i_ ( i ) // Index of the accessed sparse vector element
193 {
194  const typename VT::Iterator element( sv_.find( i_ ) );
195  if( element == sv_.end() )
196  sv_.insert( i_, RepresentedType() );
197 }
198 //*************************************************************************************************
199 
200 
201 //*************************************************************************************************
206 template< typename VT > // Type of the sparse vector
208  : sv_( vap.sv_ ) // Reference to the accessed sparse vector
209  , i_ ( vap.i_ ) // Index of the accessed sparse vector element
210 {
211  BLAZE_INTERNAL_ASSERT( sv_.find( i_ ) != sv_.end(), "Missing vector element detected" );
212 }
213 //*************************************************************************************************
214 
215 
216 
217 
218 //=================================================================================================
219 //
220 // DESTRUCTOR
221 //
222 //=================================================================================================
223 
224 //*************************************************************************************************
227 template< typename VT > // Type of the sparse vector
229 {
230  const typename VT::Iterator element( sv_.find( i_ ) );
231  if( element != sv_.end() && isDefault( element->value() ) )
232  sv_.erase( element );
233 }
234 //*************************************************************************************************
235 
236 
237 
238 
239 //=================================================================================================
240 //
241 // OPERATORS
242 //
243 //=================================================================================================
244 
245 //*************************************************************************************************
251 template< typename VT > // Type of the sparse vector
253 {
254  get() = vap.get();
255  return *this;
256 }
257 //*************************************************************************************************
258 
259 
260 //*************************************************************************************************
266 template< typename VT > // Type of the sparse vector
267 template< typename T > // Type of the right-hand side value
268 inline const VectorAccessProxy<VT>& VectorAccessProxy<VT>::operator=( const T& value ) const
269 {
270  get() = value;
271  return *this;
272 }
273 //*************************************************************************************************
274 
275 
276 //*************************************************************************************************
282 template< typename VT > // Type of the sparse vector
283 template< typename T > // Type of the right-hand side value
284 inline const VectorAccessProxy<VT>& VectorAccessProxy<VT>::operator+=( const T& value ) const
285 {
286  get() += value;
287  return *this;
288 }
289 //*************************************************************************************************
290 
291 
292 //*************************************************************************************************
298 template< typename VT > // Type of the sparse vector
299 template< typename T > // Type of the right-hand side value
300 inline const VectorAccessProxy<VT>& VectorAccessProxy<VT>::operator-=( const T& value ) const
301 {
302  get() -= value;
303  return *this;
304 }
305 //*************************************************************************************************
306 
307 
308 //*************************************************************************************************
314 template< typename VT > // Type of the sparse vector
315 template< typename T > // Type of the right-hand side value
316 inline const VectorAccessProxy<VT>& VectorAccessProxy<VT>::operator*=( const T& value ) const
317 {
318  get() *= value;
319  return *this;
320 }
321 //*************************************************************************************************
322 
323 
324 //*************************************************************************************************
330 template< typename VT > // Type of the sparse vector
331 template< typename T > // Type of the right-hand side value
332 inline const VectorAccessProxy<VT>& VectorAccessProxy<VT>::operator/=( const T& value ) const
333 {
334  get() /= value;
335  return *this;
336 }
337 //*************************************************************************************************
338 
339 
340 
341 
342 //=================================================================================================
343 //
344 // UTILITY FUNCTIONS
345 //
346 //=================================================================================================
347 
348 //*************************************************************************************************
353 template< typename VT > // Type of the sparse vector
355 {
356  const typename VT::Iterator element( sv_.find( i_ ) );
357  BLAZE_INTERNAL_ASSERT( element != sv_.end(), "Missing vector element detected" );
358  return element->value();
359 }
360 //*************************************************************************************************
361 
362 
363 //*************************************************************************************************
368 template< typename VT > // Type of the sparse vector
370 {
371  return false;
372 }
373 //*************************************************************************************************
374 
375 
376 
377 
378 //=================================================================================================
379 //
380 // CONVERSION OPERATOR
381 //
382 //=================================================================================================
383 
384 //*************************************************************************************************
389 template< typename VT > // Type of the sparse vector
391 {
392  return get();
393 }
394 //*************************************************************************************************
395 
396 
397 
398 
399 //=================================================================================================
400 //
401 // GLOBAL FUNCTIONS
402 //
403 //=================================================================================================
404 
405 //*************************************************************************************************
408 template< typename VT >
410  conj( const VectorAccessProxy<VT>& proxy );
411 
412 template< typename VT >
413 inline void reset( const VectorAccessProxy<VT>& proxy );
414 
415 template< typename VT >
416 inline void clear( const VectorAccessProxy<VT>& proxy );
417 
418 template< typename VT >
419 inline bool isDefault( const VectorAccessProxy<VT>& proxy );
420 
421 template< typename VT >
422 inline bool isReal( const VectorAccessProxy<VT>& proxy );
423 
424 template< typename VT >
425 inline bool isZero( const VectorAccessProxy<VT>& proxy );
426 
427 template< typename VT >
428 inline bool isOne( const VectorAccessProxy<VT>& proxy );
429 
430 template< typename VT >
431 inline bool isnan( const VectorAccessProxy<VT>& proxy );
432 
433 template< typename VT >
434 inline void swap( const VectorAccessProxy<VT>& a, const VectorAccessProxy<VT>& b ) /* throw() */;
435 
436 template< typename VT, typename T >
437 inline void swap( const VectorAccessProxy<VT>& a, T& b ) /* throw() */;
438 
439 template< typename T, typename VT >
440 inline void swap( T& a, const VectorAccessProxy<VT>& v ) /* throw() */;
442 //*************************************************************************************************
443 
444 
445 //*************************************************************************************************
456 template< typename VT >
458  conj( const VectorAccessProxy<VT>& proxy )
459 {
460  using blaze::conj;
461 
462  return conj( (~proxy).get() );
463 }
464 //*************************************************************************************************
465 
466 
467 //*************************************************************************************************
474 template< typename VT >
475 inline void reset( const VectorAccessProxy<VT>& proxy )
476 {
477  using blaze::reset;
478 
479  reset( proxy.get() );
480 }
481 //*************************************************************************************************
482 
483 
484 //*************************************************************************************************
491 template< typename VT >
492 inline void clear( const VectorAccessProxy<VT>& proxy )
493 {
494  using blaze::clear;
495 
496  clear( proxy.get() );
497 }
498 //*************************************************************************************************
499 
500 
501 //*************************************************************************************************
511 template< typename VT >
512 inline bool isDefault( const VectorAccessProxy<VT>& proxy )
513 {
514  using blaze::isDefault;
515 
516  return isDefault( proxy.get() );
517 }
518 //*************************************************************************************************
519 
520 
521 //*************************************************************************************************
533 template< typename VT >
534 inline bool isReal( const VectorAccessProxy<VT>& proxy )
535 {
536  using blaze::isReal;
537 
538  return isReal( proxy.get() );
539 }
540 //*************************************************************************************************
541 
542 
543 //*************************************************************************************************
553 template< typename VT >
554 inline bool isZero( const VectorAccessProxy<VT>& proxy )
555 {
556  using blaze::isZero;
557 
558  return isZero( proxy.get() );
559 }
560 //*************************************************************************************************
561 
562 
563 //*************************************************************************************************
573 template< typename VT >
574 inline bool isOne( const VectorAccessProxy<VT>& proxy )
575 {
576  using blaze::isOne;
577 
578  return isOne( proxy.get() );
579 }
580 //*************************************************************************************************
581 
582 
583 //*************************************************************************************************
593 template< typename VT >
594 inline bool isnan( const VectorAccessProxy<VT>& proxy )
595 {
596  using blaze::isnan;
597 
598  return isnan( proxy.get() );
599 }
600 //*************************************************************************************************
601 
602 
603 //*************************************************************************************************
612 template< typename VT >
613 inline void swap( const VectorAccessProxy<VT>& a, const VectorAccessProxy<VT>& b ) /* throw() */
614 {
615  using std::swap;
616 
617  swap( a.get(), b.get() );
618 }
619 //*************************************************************************************************
620 
621 
622 //*************************************************************************************************
631 template< typename VT, typename T >
632 inline void swap( const VectorAccessProxy<VT>& a, T& b ) /* throw() */
633 {
634  using std::swap;
635 
636  swap( a.get(), b );
637 }
638 //*************************************************************************************************
639 
640 
641 //*************************************************************************************************
650 template< typename T, typename VT >
651 inline void swap( T& a, const VectorAccessProxy<VT>& b ) /* throw() */
652 {
653  using std::swap;
654 
655  swap( a, b.get() );
656 }
657 //*************************************************************************************************
658 
659 } // namespace blaze
660 
661 #endif
Header file for the isnan shim.
bool isOne(const DiagonalProxy< MT > &proxy)
Returns whether the represented element is 1.
Definition: DiagonalProxy.h:609
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:569
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:507
VT & sv_
Reference to the accessed sparse vector.
Definition: VectorAccessProxy.h:154
VectorAccessProxy(VT &sv, size_t i)
Initialization constructor for a VectorAccessProxy.
Definition: VectorAccessProxy.h:190
size_t i_
Index of the accessed sparse vector element.
Definition: VectorAccessProxy.h:155
bool isRestricted() const
Returns whether the proxy represents a restricted sparse vector element..
Definition: VectorAccessProxy.h:369
Header file for the Proxy class.
bool isDefault(const DiagonalProxy< MT > &proxy)
Returns whether the represented element is in default state.
Definition: DiagonalProxy.h:547
ConjExprTrait< typename DiagonalProxy< MT >::RepresentedType >::Type conj(const DiagonalProxy< MT > &proxy)
Computing the complex conjugate of the represented element.
Definition: DiagonalProxy.h:487
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)
Swapping the contents of two compressed matrices.
Definition: CompressedMatrix.h:4998
Header file for the isZero shim.
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:252
#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:79
bool isnan(const DiagonalProxy< MT > &proxy)
Returns whether the represented element is not a number.
Definition: DiagonalProxy.h:629
Type ElementType
Type of the sparse matrix elements.
Definition: CompressedMatrix.h:2586
Constraint on the data type.
void clear(const DiagonalProxy< MT > &proxy)
Clearing the represented element.
Definition: DiagonalProxy.h:527
Header file for the isOne shim.
Header file for the conjugate shim.
Evaluation of the return type of a complex conjugate expression.Via this type trait it is possible to...
Definition: ConjExprTrait.h:87
Header file for run time assertion macros.
Header file for the reset shim.
Element * Iterator
Iterator over non-constant elements.
Definition: CompressedMatrix.h:2591
Header file for the isDefault shim.
void swap(DiagonalMatrix< MT, SO, DF > &a, DiagonalMatrix< MT, SO, DF > &b)
Swapping the contents of two matrices.
Definition: DiagonalMatrix.h:256
RawReference get() const
Returning the value of the accessed sparse vector element.
Definition: VectorAccessProxy.h:354
VT::ElementType RepresentedType
Type of the represented sparse vector element.
Definition: VectorAccessProxy.h:104
~VectorAccessProxy()
The destructor for VectorAccessProxy.
Definition: VectorAccessProxy.h:228
bool isZero(const DiagonalProxy< MT > &proxy)
Returns whether the represented element is 0.
Definition: DiagonalProxy.h:589
Header file for the ConjExprTrait class template.
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
void * operator&() const
Address operator (private & undefined)