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>
44 #include <ostream>
46 #include <blaze/math/proxy/Proxy.h>
47 #include <blaze/math/shims/Clear.h>
49 #include <blaze/math/shims/Reset.h>
50 #include <blaze/util/Assert.h>
51 #include <blaze/util/Types.h>
52 
53 
54 namespace blaze {
55 
56 //=================================================================================================
57 //
58 // CLASS DEFINITION
59 //
60 //=================================================================================================
61 
62 //*************************************************************************************************
94 template< typename VT > // Type of the sparse vector
95 class VectorAccessProxy : public Proxy< VectorAccessProxy<VT>, typename VT::ElementType >
96 {
97  public:
98  //**Type definitions****************************************************************************
99  typedef typename VT::ElementType RepresentedType;
100  typedef RepresentedType& RawReference;
101  //**********************************************************************************************
102 
103  //**Constructors********************************************************************************
106  explicit inline VectorAccessProxy( VT& sv, size_t i );
107  inline VectorAccessProxy( const VectorAccessProxy& vap );
109  //**********************************************************************************************
110 
111  //**Destructor**********************************************************************************
114  inline ~VectorAccessProxy();
116  //**********************************************************************************************
117 
118  //**Operators***********************************************************************************
121  inline const VectorAccessProxy& operator= ( const VectorAccessProxy& vap ) const;
122  template< typename T > inline const VectorAccessProxy& operator= ( const T& value ) const;
123  template< typename T > inline const VectorAccessProxy& operator+=( const T& value ) const;
124  template< typename T > inline const VectorAccessProxy& operator-=( const T& value ) const;
125  template< typename T > inline const VectorAccessProxy& operator*=( const T& value ) const;
126  template< typename T > inline const VectorAccessProxy& operator/=( const T& value ) const;
128  //**********************************************************************************************
129 
130  //**Utility functions***************************************************************************
133  inline RawReference get() const;
134  inline bool isRestricted() const;
136  //**********************************************************************************************
137 
138  //**Conversion operator*************************************************************************
141  inline operator RawReference() const;
143  //**********************************************************************************************
144 
145  private:
146  //**Member variables****************************************************************************
149  VT& sv_;
150  size_t i_;
151 
152  //**********************************************************************************************
153 
154  //**Forbidden operations************************************************************************
157  void* operator&() const;
158 
159  //**********************************************************************************************
160 
161  //**Compile time checks*************************************************************************
165  //**********************************************************************************************
166 };
167 //*************************************************************************************************
168 
169 
170 
171 
172 //=================================================================================================
173 //
174 // CONSTRUCTORS
175 //
176 //=================================================================================================
177 
178 //*************************************************************************************************
184 template< typename VT > // Type of the sparse vector
186  : sv_( sv ) // Reference to the accessed sparse vector
187  , i_ ( i ) // Index of the accessed sparse vector element
188 {
189  const typename VT::Iterator element( sv_.find( i_ ) );
190  if( element == sv_.end() )
191  sv_.insert( i_, RepresentedType() );
192 }
193 //*************************************************************************************************
194 
195 
196 //*************************************************************************************************
201 template< typename VT > // Type of the sparse vector
203  : sv_( vap.sv_ ) // Reference to the accessed sparse vector
204  , i_ ( vap.i_ ) // Index of the accessed sparse vector element
205 {
206  BLAZE_INTERNAL_ASSERT( sv_.find( i_ ) != sv_.end(), "Missing vector element detected" );
207 }
208 //*************************************************************************************************
209 
210 
211 
212 
213 //=================================================================================================
214 //
215 // DESTRUCTOR
216 //
217 //=================================================================================================
218 
219 //*************************************************************************************************
222 template< typename VT > // Type of the sparse vector
224 {
225  const typename VT::Iterator element( sv_.find( i_ ) );
226  if( element != sv_.end() && isDefault( element->value() ) )
227  sv_.erase( element );
228 }
229 //*************************************************************************************************
230 
231 
232 
233 
234 //=================================================================================================
235 //
236 // OPERATORS
237 //
238 //=================================================================================================
239 
240 //*************************************************************************************************
246 template< typename VT > // Type of the sparse vector
248 {
249  get() = vap.get();
250  return *this;
251 }
252 //*************************************************************************************************
253 
254 
255 //*************************************************************************************************
261 template< typename VT > // Type of the sparse vector
262 template< typename T > // Type of the right-hand side value
263 inline const VectorAccessProxy<VT>& VectorAccessProxy<VT>::operator=( const T& value ) const
264 {
265  get() = value;
266  return *this;
267 }
268 //*************************************************************************************************
269 
270 
271 //*************************************************************************************************
277 template< typename VT > // Type of the sparse vector
278 template< typename T > // Type of the right-hand side value
279 inline const VectorAccessProxy<VT>& VectorAccessProxy<VT>::operator+=( const T& value ) const
280 {
281  get() += value;
282  return *this;
283 }
284 //*************************************************************************************************
285 
286 
287 //*************************************************************************************************
293 template< typename VT > // Type of the sparse vector
294 template< typename T > // Type of the right-hand side value
295 inline const VectorAccessProxy<VT>& VectorAccessProxy<VT>::operator-=( const T& value ) const
296 {
297  get() -= value;
298  return *this;
299 }
300 //*************************************************************************************************
301 
302 
303 //*************************************************************************************************
309 template< typename VT > // Type of the sparse vector
310 template< typename T > // Type of the right-hand side value
311 inline const VectorAccessProxy<VT>& VectorAccessProxy<VT>::operator*=( const T& value ) const
312 {
313  get() *= value;
314  return *this;
315 }
316 //*************************************************************************************************
317 
318 
319 //*************************************************************************************************
325 template< typename VT > // Type of the sparse vector
326 template< typename T > // Type of the right-hand side value
327 inline const VectorAccessProxy<VT>& VectorAccessProxy<VT>::operator/=( const T& value ) const
328 {
329  get() /= value;
330  return *this;
331 }
332 //*************************************************************************************************
333 
334 
335 
336 
337 //=================================================================================================
338 //
339 // UTILITY FUNCTIONS
340 //
341 //=================================================================================================
342 
343 //*************************************************************************************************
348 template< typename VT > // Type of the sparse vector
350 {
351  const typename VT::Iterator element( sv_.find( i_ ) );
352  BLAZE_INTERNAL_ASSERT( element != sv_.end(), "Missing vector element detected" );
353  return element->value();
354 }
355 //*************************************************************************************************
356 
357 
358 //*************************************************************************************************
363 template< typename VT > // Type of the sparse vector
365 {
366  return false;
367 }
368 //*************************************************************************************************
369 
370 
371 
372 
373 //=================================================================================================
374 //
375 // CONVERSION OPERATOR
376 //
377 //=================================================================================================
378 
379 //*************************************************************************************************
384 template< typename VT > // Type of the sparse vector
386 {
387  return get();
388 }
389 //*************************************************************************************************
390 
391 
392 
393 
394 //=================================================================================================
395 //
396 // GLOBAL OPERATORS
397 //
398 //=================================================================================================
399 
400 //*************************************************************************************************
403 template< typename VT1, typename VT2 >
404 inline bool operator==( const VectorAccessProxy<VT1>& lhs, const VectorAccessProxy<VT2>& rhs );
405 
406 template< typename VT, typename T >
407 inline bool operator==( const VectorAccessProxy<VT>& lhs, const T& rhs );
408 
409 template< typename T, typename VT >
410 inline bool operator==( const T& lhs, const VectorAccessProxy<VT>& rhs );
411 
412 template< typename VT1, typename VT2 >
413 inline bool operator!=( const VectorAccessProxy<VT1>& lhs, const VectorAccessProxy<VT2>& rhs );
414 
415 template< typename VT, typename T >
416 inline bool operator!=( const VectorAccessProxy<VT>& lhs, const T& rhs );
417 
418 template< typename T, typename VT >
419 inline bool operator!=( const T& lhs, const VectorAccessProxy<VT>& rhs );
420 
421 template< typename VT1, typename VT2 >
422 inline bool operator<( const VectorAccessProxy<VT1>& lhs, const VectorAccessProxy<VT2>& rhs );
423 
424 template< typename VT, typename T >
425 inline bool operator<( const VectorAccessProxy<VT>& lhs, const T& rhs );
426 
427 template< typename T, typename VT >
428 inline bool operator<( const T& lhs, const VectorAccessProxy<VT>& rhs );
429 
430 template< typename VT1, typename VT2 >
431 inline bool operator>( const VectorAccessProxy<VT1>& lhs, const VectorAccessProxy<VT2>& rhs );
432 
433 template< typename VT, typename T >
434 inline bool operator>( const VectorAccessProxy<VT>& lhs, const T& rhs );
435 
436 template< typename T, typename VT >
437 inline bool operator>( const T& lhs, const VectorAccessProxy<VT>& rhs );
438 
439 template< typename VT1, typename VT2 >
440 inline bool operator<=( const VectorAccessProxy<VT1>& lhs, const VectorAccessProxy<VT2>& rhs );
441 
442 template< typename VT, typename T >
443 inline bool operator<=( const VectorAccessProxy<VT>& lhs, const T& rhs );
444 
445 template< typename T, typename VT >
446 inline bool operator<=( const T& lhs, const VectorAccessProxy<VT>& rhs );
447 
448 template< typename VT1, typename VT2 >
449 inline bool operator>=( const VectorAccessProxy<VT1>& lhs, const VectorAccessProxy<VT2>& rhs );
450 
451 template< typename VT, typename T >
452 inline bool operator>=( const VectorAccessProxy<VT>& lhs, const T& rhs );
453 
454 template< typename T, typename VT >
455 inline bool operator>=( const T& lhs, const VectorAccessProxy<VT>& rhs );
456 
457 template< typename VT >
458 inline std::ostream& operator<<( std::ostream& os, const VectorAccessProxy<VT>& proxy );
460 //*************************************************************************************************
461 
462 
463 //*************************************************************************************************
471 template< typename VT1, typename VT2 >
472 inline bool operator==( const VectorAccessProxy<VT1>& lhs, const VectorAccessProxy<VT2>& rhs )
473 {
474  return ( lhs.get() == rhs.get() );
475 }
476 //*************************************************************************************************
477 
478 
479 //*************************************************************************************************
487 template< typename VT, typename T >
488 inline bool operator==( const VectorAccessProxy<VT>& lhs, const T& rhs )
489 {
490  return ( lhs.get() == rhs );
491 }
492 //*************************************************************************************************
493 
494 
495 //*************************************************************************************************
503 template< typename T, typename VT >
504 inline bool operator==( const T& lhs, const VectorAccessProxy<VT>& rhs )
505 {
506  return ( lhs == rhs.get() );
507 }
508 //*************************************************************************************************
509 
510 
511 //*************************************************************************************************
519 template< typename VT1, typename VT2 >
520 inline bool operator!=( const VectorAccessProxy<VT1>& lhs, const VectorAccessProxy<VT2>& rhs )
521 {
522  return ( lhs.get() != rhs.get() );
523 }
524 //*************************************************************************************************
525 
526 
527 //*************************************************************************************************
535 template< typename VT, typename T >
536 inline bool operator!=( const VectorAccessProxy<VT>& lhs, const T& rhs )
537 {
538  return ( lhs.get() != rhs );
539 }
540 //*************************************************************************************************
541 
542 
543 //*************************************************************************************************
551 template< typename T, typename VT >
552 inline bool operator!=( const T& lhs, const VectorAccessProxy<VT>& rhs )
553 {
554  return ( lhs != rhs.get() );
555 }
556 //*************************************************************************************************
557 
558 
559 //*************************************************************************************************
567 template< typename VT1, typename VT2 >
568 inline bool operator<( const VectorAccessProxy<VT1>& lhs, const VectorAccessProxy<VT2>& rhs )
569 {
570  return ( lhs.get() < rhs.get() );
571 }
572 //*************************************************************************************************
573 
574 
575 //*************************************************************************************************
583 template< typename VT, typename T >
584 inline bool operator<( const VectorAccessProxy<VT>& lhs, const T& rhs )
585 {
586  return ( lhs.get() < rhs );
587 }
588 //*************************************************************************************************
589 
590 
591 //*************************************************************************************************
599 template< typename T, typename VT >
600 inline bool operator<( const T& lhs, const VectorAccessProxy<VT>& rhs )
601 {
602  return ( lhs < rhs.get() );
603 }
604 //*************************************************************************************************
605 
606 
607 //*************************************************************************************************
615 template< typename VT1, typename VT2 >
616 inline bool operator>( const VectorAccessProxy<VT1>& lhs, const VectorAccessProxy<VT2>& rhs )
617 {
618  return ( lhs.get() > rhs.get() );
619 }
620 //*************************************************************************************************
621 
622 
623 //*************************************************************************************************
631 template< typename VT, typename T >
632 inline bool operator>( const VectorAccessProxy<VT>& lhs, const T& rhs )
633 {
634  return ( lhs.get() > rhs );
635 }
636 //*************************************************************************************************
637 
638 
639 //*************************************************************************************************
647 template< typename T, typename VT >
648 inline bool operator>( const T& lhs, const VectorAccessProxy<VT>& rhs )
649 {
650  return ( lhs > rhs.get() );
651 }
652 //*************************************************************************************************
653 
654 
655 //*************************************************************************************************
663 template< typename VT1, typename VT2 >
664 inline bool operator<=( const VectorAccessProxy<VT1>& lhs, const VectorAccessProxy<VT2>& rhs )
665 {
666  return ( lhs.get() <= rhs.get() );
667 }
668 //*************************************************************************************************
669 
670 
671 //*************************************************************************************************
679 template< typename VT, typename T >
680 inline bool operator<=( const VectorAccessProxy<VT>& lhs, const T& rhs )
681 {
682  return ( lhs.get() <= rhs );
683 }
684 //*************************************************************************************************
685 
686 
687 //*************************************************************************************************
695 template< typename T, typename VT >
696 inline bool operator<=( const T& lhs, const VectorAccessProxy<VT>& rhs )
697 {
698  return ( lhs <= rhs.get() );
699 }
700 //*************************************************************************************************
701 
702 
703 //*************************************************************************************************
711 template< typename VT1, typename VT2 >
712 inline bool operator>=( const VectorAccessProxy<VT1>& lhs, const VectorAccessProxy<VT2>& rhs )
713 {
714  return ( lhs.get() >= rhs.get() );
715 }
716 //*************************************************************************************************
717 
718 
719 //*************************************************************************************************
727 template< typename VT, typename T >
728 inline bool operator>=( const VectorAccessProxy<VT>& lhs, const T& rhs )
729 {
730  return ( lhs.get() >= rhs );
731 }
732 //*************************************************************************************************
733 
734 
735 //*************************************************************************************************
743 template< typename T, typename VT >
744 inline bool operator>=( const T& lhs, const VectorAccessProxy<VT>& rhs )
745 {
746  return ( lhs >= rhs.get() );
747 }
748 //*************************************************************************************************
749 
750 
751 //*************************************************************************************************
759 template< typename VT >
760 inline std::ostream& operator<<( std::ostream& os, const VectorAccessProxy<VT>& proxy )
761 {
762  return os << proxy.get();
763 }
764 //*************************************************************************************************
765 
766 
767 
768 
769 //=================================================================================================
770 //
771 // GLOBAL FUNCTIONS
772 //
773 //=================================================================================================
774 
775 //*************************************************************************************************
778 template< typename VT >
779 inline void reset( const VectorAccessProxy<VT>& proxy );
780 
781 template< typename VT >
782 inline void clear( const VectorAccessProxy<VT>& proxy );
783 
784 template< typename VT >
785 inline bool isDefault( const VectorAccessProxy<VT>& proxy );
786 
787 template< typename VT >
788 inline void swap( const VectorAccessProxy<VT>& a, const VectorAccessProxy<VT>& b ) /* throw() */;
789 
790 template< typename VT, typename T >
791 inline void swap( const VectorAccessProxy<VT>& a, T& b ) /* throw() */;
792 
793 template< typename T, typename VT >
794 inline void swap( T& a, const VectorAccessProxy<VT>& v ) /* throw() */;
796 //*************************************************************************************************
797 
798 
799 //*************************************************************************************************
806 template< typename VT >
807 inline void reset( const VectorAccessProxy<VT>& proxy )
808 {
809  using blaze::reset;
810 
811  reset( proxy.get() );
812 }
813 //*************************************************************************************************
814 
815 
816 //*************************************************************************************************
823 template< typename VT >
824 inline void clear( const VectorAccessProxy<VT>& proxy )
825 {
826  using blaze::clear;
827 
828  clear( proxy.get() );
829 }
830 //*************************************************************************************************
831 
832 
833 //*************************************************************************************************
843 template< typename VT >
844 inline bool isDefault( const VectorAccessProxy<VT>& proxy )
845 {
846  using blaze::isDefault;
847 
848  return isDefault( proxy.get() );
849 }
850 //*************************************************************************************************
851 
852 
853 //*************************************************************************************************
862 template< typename VT >
863 inline void swap( const VectorAccessProxy<VT>& a, const VectorAccessProxy<VT>& b ) /* throw() */
864 {
865  using std::swap;
866 
867  swap( a.get(), b.get() );
868 }
869 //*************************************************************************************************
870 
871 
872 //*************************************************************************************************
881 template< typename VT, typename T >
882 inline void swap( const VectorAccessProxy<VT>& a, T& b ) /* throw() */
883 {
884  using std::swap;
885 
886  swap( a.get(), b );
887 }
888 //*************************************************************************************************
889 
890 
891 //*************************************************************************************************
900 template< typename T, typename VT >
901 inline void swap( T& a, const VectorAccessProxy<VT>& b ) /* throw() */
902 {
903  using std::swap;
904 
905  swap( a, b.get() );
906 }
907 //*************************************************************************************************
908 
909 } // namespace blaze
910 
911 #endif
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: Proxy.h:99
Access proxy for sparse, N-dimensional vectors.The VectorAccessProxy provides safe access to the elem...
Definition: VectorAccessProxy.h:95
void reset(const DiagonalProxy< MT > &proxy)
Resetting the represented element to the default initial values.
Definition: DiagonalProxy.h:821
VT & sv_
Reference to the accessed sparse vector.
Definition: VectorAccessProxy.h:149
bool operator>(const NegativeAccuracy< A > &lhs, const T &rhs)
Greater-than comparison between a NegativeAccuracy object and a floating point value.
Definition: Accuracy.h:366
bool operator>=(const NegativeAccuracy< A > &, const T &rhs)
Greater-or-equal-than comparison between a NegativeAccuracy object and a floating point value...
Definition: Accuracy.h:442
VectorAccessProxy(VT &sv, size_t i)
Initialization constructor for a VectorAccessProxy.
Definition: VectorAccessProxy.h:185
size_t i_
Index of the accessed sparse vector element.
Definition: VectorAccessProxy.h:150
bool isRestricted() const
Returns whether the proxy represents a restricted sparse vector element..
Definition: VectorAccessProxy.h:364
bool isDefault(const DiagonalProxy< MT > &proxy)
Returns whether the represented element is in default state.
Definition: DiagonalProxy.h:861
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:4807
RepresentedType & RawReference
Raw reference to the represented element.
Definition: VectorAccessProxy.h:100
const VectorAccessProxy & operator=(const VectorAccessProxy &vap) const
Copy assignment operator for VectorAccessProxy.
Definition: VectorAccessProxy.h:247
#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
Type ElementType
Type of the sparse matrix elements.
Definition: CompressedMatrix.h:2505
Constraint on the data type.
void clear(const DiagonalProxy< MT > &proxy)
Clearing the represented element.
Definition: DiagonalProxy.h:841
Header file for run time assertion macros.
Header file for the Proxy class.
Header file for the reset shim.
Element * Iterator
Iterator over non-constant elements.
Definition: CompressedMatrix.h:2510
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:200
RawReference get() const
Returning the value of the accessed sparse vector element.
Definition: VectorAccessProxy.h:349
VT::ElementType RepresentedType
Type of the represented sparse vector element.
Definition: VectorAccessProxy.h:99
~VectorAccessProxy()
The destructor for VectorAccessProxy.
Definition: VectorAccessProxy.h:223
bool operator==(const NegativeAccuracy< A > &lhs, const T &rhs)
Equality comparison between a NegativeAccuracy object and a floating point value. ...
Definition: Accuracy.h:249
bool operator!=(const NegativeAccuracy< A > &lhs, const T &rhs)
Inequality comparison between a NegativeAccuracy object and a floating point value.
Definition: Accuracy.h:289
#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)