All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
VectorAccessProxy.h
Go to the documentation of this file.
1 //=================================================================================================
20 //=================================================================================================
21 
22 #ifndef _BLAZE_MATH_SPARSE_VECTORACCESSPROXY_H_
23 #define _BLAZE_MATH_SPARSE_VECTORACCESSPROXY_H_
24 
25 
26 //*************************************************************************************************
27 // Includes
28 //*************************************************************************************************
29 
31 #include <blaze/util/Assert.h>
32 #include <blaze/util/Types.h>
33 
34 
35 namespace blaze {
36 
37 //=================================================================================================
38 //
39 // CLASS DEFINITION
40 //
41 //=================================================================================================
42 
43 //*************************************************************************************************
75 template< typename VT > // Type of the sparse vector
77 {
78  public:
79  //**Type definitions****************************************************************************
80  typedef VT VectorType;
81  typedef typename VT::ElementType ElementType;
83  typedef const ElementType& ConstReference;
84  typedef typename VT::Iterator Iterator;
85  //**********************************************************************************************
86 
87  //**Constructors********************************************************************************
90  explicit inline VectorAccessProxy( VT& sv, size_t i );
91  inline VectorAccessProxy( const VectorAccessProxy& vap );
93  //**********************************************************************************************
94 
95  //**Destructor**********************************************************************************
98  inline ~VectorAccessProxy();
100  //**********************************************************************************************
101 
102  //**Operators***********************************************************************************
105  inline VectorAccessProxy& operator= ( const VectorAccessProxy& vap );
106  template< typename T > inline VectorAccessProxy& operator= ( const T& value );
107  template< typename T > inline VectorAccessProxy& operator+=( const T& value );
108  template< typename T > inline VectorAccessProxy& operator-=( const T& value );
109  template< typename T > inline VectorAccessProxy& operator*=( const T& value );
110  template< typename T > inline VectorAccessProxy& operator/=( const T& value );
112  //**********************************************************************************************
113 
114  //**Conversion operator*************************************************************************
117  inline operator Reference() const;
119  //**********************************************************************************************
120 
121  private:
122  //**Utility functions***************************************************************************
125  Reference get() const;
126  void set( ConstReference value ) const;
128  //**********************************************************************************************
129 
130  //**Member variables****************************************************************************
133  VT& sv_;
134  size_t i_;
135 
136  //**********************************************************************************************
137 };
138 //*************************************************************************************************
139 
140 
141 
142 
143 //=================================================================================================
144 //
145 // CONSTRUCTORS
146 //
147 //=================================================================================================
148 
149 //*************************************************************************************************
155 template< typename VT > // Type of the sparse vector
157  : sv_( sv ) // Reference to the accessed sparse vector
158  , i_ ( i ) // Index of the accessed sparse vector element
159 {
160  const Iterator element( sv_.find( i_ ) );
161  if( element == sv_.end() )
162  sv_.insert( i_, ElementType() );
163 }
164 //*************************************************************************************************
165 
166 
167 //*************************************************************************************************
172 template< typename VT > // Type of the sparse vector
174  : sv_( vap.sv_ ) // Reference to the accessed sparse vector
175  , i_ ( vap.i_ ) // Index of the accessed sparse vector element
176 {
177  BLAZE_INTERNAL_ASSERT( sv_.find( i_ ) != sv_.end(), "Missing vector element detected" );
178 }
179 //*************************************************************************************************
180 
181 
182 
183 
184 //=================================================================================================
185 //
186 // DESTRUCTOR
187 //
188 //=================================================================================================
189 
190 //*************************************************************************************************
193 template< typename VT > // Type of the sparse vector
195 {
196  const Iterator element( sv_.find( i_ ) );
197  BLAZE_INTERNAL_ASSERT( element != sv_.end(), "Missing vector element detected" );
198  if( isDefault( element->value() ) )
199  sv_.erase( element );
200 }
201 //*************************************************************************************************
202 
203 
204 
205 
206 //=================================================================================================
207 //
208 // OPERATORS
209 //
210 //=================================================================================================
211 
212 //*************************************************************************************************
218 template< typename VT > // Type of the sparse vector
220 {
221  set( vap.get() );
222  return *this;
223 }
224 //*************************************************************************************************
225 
226 
227 //*************************************************************************************************
233 template< typename VT > // Type of the sparse vector
234 template< typename T > // Type of the right-hand side value
236 {
237  set( value );
238  return *this;
239 }
240 //*************************************************************************************************
241 
242 
243 //*************************************************************************************************
249 template< typename VT > // Type of the sparse vector
250 template< typename T > // Type of the right-hand side value
252 {
253  get() += value;
254  return *this;
255 }
256 //*************************************************************************************************
257 
258 
259 //*************************************************************************************************
265 template< typename VT > // Type of the sparse vector
266 template< typename T > // Type of the right-hand side value
268 {
269  get() -= value;
270  return *this;
271 }
272 //*************************************************************************************************
273 
274 
275 //*************************************************************************************************
281 template< typename VT > // Type of the sparse vector
282 template< typename T > // Type of the right-hand side value
284 {
285  get() *= value;
286  return *this;
287 }
288 //*************************************************************************************************
289 
290 
291 //*************************************************************************************************
297 template< typename VT > // Type of the sparse vector
298 template< typename T > // Type of the right-hand side value
300 {
301  get() /= value;
302  return *this;
303 }
304 //*************************************************************************************************
305 
306 
307 
308 
309 //=================================================================================================
310 //
311 // CONVERSION OPERATOR
312 //
313 //=================================================================================================
314 
315 //*************************************************************************************************
320 template< typename VT > // Type of the sparse vector
322 {
323  return get();
324 }
325 //*************************************************************************************************
326 
327 
328 
329 
330 //=================================================================================================
331 //
332 // UTILITY FUNCTIONS
333 //
334 //=================================================================================================
335 
336 //*************************************************************************************************
341 template< typename VT > // Type of the sparse vector
343 {
344  const Iterator element( sv_.find( i_ ) );
345  BLAZE_INTERNAL_ASSERT( element != sv_.end(), "Missing vector element detected" );
346  return element->value();
347 }
348 //*************************************************************************************************
349 
350 
351 //*************************************************************************************************
357 template< typename VT > // Type of the sparse vector
358 inline void VectorAccessProxy<VT>::set( ConstReference value ) const
359 {
360  const Iterator element( sv_.find( i_ ) );
361  BLAZE_INTERNAL_ASSERT( element != sv_.end(), "Missing vector element detected" );
362  element->value() = value;
363 }
364 //*************************************************************************************************
365 
366 } // namespace blaze
367 
368 #endif