SparseVectorProxy.h
Go to the documentation of this file.
1 //=================================================================================================
33 //=================================================================================================
34 
35 #ifndef _BLAZE_MATH_PROXY_SPARSEVECTORPROXY_H_
36 #define _BLAZE_MATH_PROXY_SPARSEVECTORPROXY_H_
37 
38 
39 //*************************************************************************************************
40 // Includes
41 //*************************************************************************************************
42 
43 #include <stdexcept>
46 #include <blaze/math/shims/Clear.h>
47 #include <blaze/math/shims/Reset.h>
49 #include <blaze/system/Inline.h>
50 #include <blaze/util/Types.h>
51 
52 
53 namespace blaze {
54 
55 //=================================================================================================
56 //
57 // CLASS DEFINITION
58 //
59 //=================================================================================================
60 
61 //*************************************************************************************************
69 template< typename PT // Type of the proxy
70  , typename VT > // Type of the sparse vector
71 class SparseVectorProxy : public SparseVector< PT, IsRowVector<VT>::value >
72 {
73  public:
74  //**Type definitions****************************************************************************
75  typedef typename VT::ElementType ElementType;
76  typedef typename VT::Reference Reference;
78  typedef typename VT::Iterator Iterator;
79  typedef typename VT::ConstIterator ConstIterator;
80  //**********************************************************************************************
81 
82  //**Data access functions***********************************************************************
85  inline Reference operator[]( size_t index ) const;
86 
87  inline Iterator begin () const;
88  inline ConstIterator cbegin() const;
89  inline Iterator end () const;
90  inline ConstIterator cend () const;
92  //**********************************************************************************************
93 
94  //**Utility functions***************************************************************************
97  inline size_t size() const;
98  inline size_t capacity() const;
99  inline size_t nonZeros() const;
100  inline void reset() const;
101  inline void clear() const;
102  inline Iterator set( size_t index, const ElementType& value ) const;
103  inline Iterator insert( size_t index, const ElementType& value ) const;
104  inline void append( size_t index, const ElementType& value, bool check=false ) const;
105  inline void erase( size_t index ) const;
106  inline Iterator erase( Iterator pos ) const;
107  inline Iterator erase( Iterator first, Iterator last ) const;
108  inline void resize( size_t n, bool preserve=true ) const;
109  inline void reserve( size_t n ) const;
110 
111  template< typename Other > inline void scale( const Other& scalar ) const;
113  //**********************************************************************************************
114 
115  //**Lookup functions****************************************************************************
118  inline Iterator find ( size_t index ) const;
119  inline Iterator find ( size_t i, size_t j ) const;
120  inline Iterator lowerBound( size_t index ) const;
121  inline Iterator lowerBound( size_t i, size_t j ) const;
122  inline Iterator upperBound( size_t index ) const;
123  inline Iterator upperBound( size_t i, size_t j ) const;
125  //**********************************************************************************************
126 
127  private:
128  //**Compile time checks*************************************************************************
132  //**********************************************************************************************
133 };
134 //*************************************************************************************************
135 
136 
137 
138 
139 //=================================================================================================
140 //
141 // DATA ACCESS FUNCTIONS
142 //
143 //=================================================================================================
144 
145 //*************************************************************************************************
156 template< typename PT // Type of the proxy
157  , typename VT > // Type of the sparse vector
160 {
161  if( (~*this).isRestricted() )
162  throw std::invalid_argument( "Invalid access to restricted element" );
163 
164  return (~*this).get()[index];
165 }
166 //*************************************************************************************************
167 
168 
169 //*************************************************************************************************
174 template< typename PT // Type of the proxy
175  , typename VT > // Type of the sparse vector
177 {
178  return (~*this).get().begin();
179 }
180 //*************************************************************************************************
181 
182 
183 //*************************************************************************************************
188 template< typename PT // Type of the proxy
189  , typename VT > // Type of the sparse vector
191 {
192  return (~*this).get().cbegin();
193 }
194 //*************************************************************************************************
195 
196 
197 //*************************************************************************************************
202 template< typename PT // Type of the proxy
203  , typename VT > // Type of the sparse vector
205 {
206  return (~*this).get().end();
207 }
208 //*************************************************************************************************
209 
210 
211 //*************************************************************************************************
216 template< typename PT // Type of the proxy
217  , typename VT > // Type of the sparse vector
219 {
220  return (~*this).get().cend();
221 }
222 //*************************************************************************************************
223 
224 
225 
226 
227 //=================================================================================================
228 //
229 // UTILITY FUNCTIONS
230 //
231 //=================================================================================================
232 
233 //*************************************************************************************************
238 template< typename PT // Type of the proxy
239  , typename VT > // Type of the sparse vector
240 inline size_t SparseVectorProxy<PT,VT>::size() const
241 {
242  return (~*this).get().size();
243 }
244 //*************************************************************************************************
245 
246 
247 //*************************************************************************************************
252 template< typename PT // Type of the proxy
253  , typename VT > // Type of the sparse vector
255 {
256  return (~*this).get().capacity();
257 }
258 //*************************************************************************************************
259 
260 
261 //*************************************************************************************************
269 template< typename PT // Type of the proxy
270  , typename VT > // Type of the sparse vector
272 {
273  return (~*this).get().nonZeros();
274 }
275 //*************************************************************************************************
276 
277 
278 //*************************************************************************************************
285 template< typename PT // Type of the proxy
286  , typename VT > // Type of the sparse vector
288 {
289  using blaze::reset;
290 
291  reset( (~*this).get() );
292 }
293 //*************************************************************************************************
294 
295 
296 //*************************************************************************************************
303 template< typename PT // Type of the proxy
304  , typename VT > // Type of the sparse vector
306 {
307  using blaze::clear;
308 
309  clear( (~*this).get() );
310 }
311 //*************************************************************************************************
312 
313 
314 //*************************************************************************************************
326 template< typename PT // Type of the proxy
327  , typename VT > // Type of the sparse vector
329  SparseVectorProxy<PT,VT>::set( size_t index, const ElementType& value ) const
330 {
331  if( (~*this).isRestricted() )
332  throw std::invalid_argument( "Invalid access to restricted element" );
333 
334  return (~*this).get().set( index, value );
335 }
336 //*************************************************************************************************
337 
338 
339 //*************************************************************************************************
351 template< typename PT // Type of the proxy
352  , typename VT > // Type of the sparse vector
354  SparseVectorProxy<PT,VT>::insert( size_t index, const ElementType& value ) const
355 {
356  if( (~*this).isRestricted() )
357  throw std::invalid_argument( "Invalid access to restricted element" );
358 
359  return (~*this).get().insert( index, value );
360 }
361 //*************************************************************************************************
362 
363 
364 //*************************************************************************************************
388 template< typename PT // Type of the proxy
389  , typename VT > // Type of the sparse vector
390 inline void SparseVectorProxy<PT,VT>::append( size_t index, const ElementType& value, bool check ) const
391 {
392  if( (~*this).isRestricted() )
393  throw std::invalid_argument( "Invalid access to restricted element" );
394 
395  (~*this).get().append( index, value, check );
396 }
397 //*************************************************************************************************
398 
399 
400 //*************************************************************************************************
408 template< typename PT // Type of the proxy
409  , typename VT > // Type of the sparse vector
410 inline void SparseVectorProxy<PT,VT>::erase( size_t index ) const
411 {
412  if( (~*this).isRestricted() )
413  throw std::invalid_argument( "Invalid access to restricted element" );
414 
415  (~*this).get().erase( index );
416 }
417 //*************************************************************************************************
418 
419 
420 //*************************************************************************************************
428 template< typename PT // Type of the proxy
429  , typename VT > // Type of the sparse vector
431 {
432  if( (~*this).isRestricted() )
433  throw std::invalid_argument( "Invalid access to restricted element" );
434 
435  return (~*this).get().erase( pos );
436 }
437 //*************************************************************************************************
438 
439 
440 //*************************************************************************************************
449 template< typename PT // Type of the proxy
450  , typename VT > // Type of the sparse vector
453 {
454  if( (~*this).isRestricted() )
455  throw std::invalid_argument( "Invalid access to restricted element" );
456 
457  return (~*this).get().erase( first, last );
458 }
459 //*************************************************************************************************
460 
461 
462 //*************************************************************************************************
476 template< typename PT // Type of the proxy
477  , typename VT > // Type of the sparse vector
478 inline void SparseVectorProxy<PT,VT>::resize( size_t n, bool preserve ) const
479 {
480  if( (~*this).isRestricted() )
481  throw std::invalid_argument( "Invalid access to restricted element" );
482 
483  (~*this).get().resize( n, preserve );
484 }
485 //*************************************************************************************************
486 
487 
488 //*************************************************************************************************
497 template< typename PT // Type of the proxy
498  , typename VT > // Type of the sparse vector
499 inline void SparseVectorProxy<PT,VT>::reserve( size_t n ) const
500 {
501  if( (~*this).isRestricted() )
502  throw std::invalid_argument( "Invalid access to restricted element" );
503 
504  (~*this).get().reserve( n );
505 }
506 //*************************************************************************************************
507 
508 
509 //*************************************************************************************************
515 template< typename PT // Type of the proxy
516  , typename VT > // Type of the sparse vector
517 template< typename Other > // Data type of the scalar value
518 inline void SparseVectorProxy<PT,VT>::scale( const Other& scalar ) const
519 {
520  if( (~*this).isRestricted() )
521  throw std::invalid_argument( "Invalid access to restricted element" );
522 
523  (~*this).get().scale( scalar );
524 }
525 //*************************************************************************************************
526 
527 
528 
529 
530 //=================================================================================================
531 //
532 // LOOKUP FUNCTIONS
533 //
534 //=================================================================================================
535 
536 //*************************************************************************************************
549 template< typename PT // Type of the proxy
550  , typename VT > // Type of the sparse vector
552  SparseVectorProxy<PT,VT>::find( size_t index ) const
553 {
554  return (~*this).get().find( index );
555 }
556 //*************************************************************************************************
557 
558 
559 //*************************************************************************************************
571 template< typename PT // Type of the proxy
572  , typename VT > // Type of the sparse vector
575 {
576  return (~*this).get().lowerBound( index );
577 }
578 //*************************************************************************************************
579 
580 
581 //*************************************************************************************************
593 template< typename PT // Type of the proxy
594  , typename VT > // Type of the sparse vector
597 {
598  return (~*this).get().upperBound( index );
599 }
600 //*************************************************************************************************
601 
602 
603 
604 
605 //=================================================================================================
606 //
607 // GLOBAL FUNCTIONS
608 //
609 //=================================================================================================
610 
611 //*************************************************************************************************
614 template< typename PT, typename VT >
616  begin( const SparseVectorProxy<PT,VT>& proxy );
617 
618 template< typename PT, typename VT >
620  cbegin( const SparseVectorProxy<PT,VT>& proxy );
621 
622 template< typename PT, typename VT >
624  end( const SparseVectorProxy<PT,VT>& proxy );
625 
626 template< typename PT, typename VT >
628  cend( const SparseVectorProxy<PT,VT>& proxy );
629 
630 template< typename PT, typename VT >
631 BLAZE_ALWAYS_INLINE size_t size( const SparseVectorProxy<PT,VT>& proxy );
632 
633 template< typename PT, typename VT >
635 
636 template< typename PT, typename VT >
638 
639 template< typename PT, typename VT >
641 
642 template< typename PT, typename VT >
645 //*************************************************************************************************
646 
647 
648 //*************************************************************************************************
655 template< typename PT // Type of the proxy
656  , typename VT > // Type of the sparse vector
659 {
660  return proxy.begin();
661 }
662 //*************************************************************************************************
663 
664 
665 //*************************************************************************************************
672 template< typename PT // Type of the proxy
673  , typename VT > // Type of the sparse vector
676 {
677  return proxy.cbegin();
678 }
679 //*************************************************************************************************
680 
681 
682 //*************************************************************************************************
689 template< typename PT // Type of the proxy
690  , typename VT > // Type of the sparse vector
693 {
694  return proxy.end();
695 }
696 //*************************************************************************************************
697 
698 
699 //*************************************************************************************************
706 template< typename PT // Type of the proxy
707  , typename VT > // Type of the sparse vector
710 {
711  return proxy.cend();
712 }
713 //*************************************************************************************************
714 
715 
716 //*************************************************************************************************
723 template< typename PT // Type of the proxy
724  , typename VT > // Type of the sparse vector
726 {
727  return proxy.size();
728 }
729 //*************************************************************************************************
730 
731 
732 //*************************************************************************************************
739 template< typename PT // Type of the proxy
740  , typename VT > // Type of the sparse vector
742 {
743  return proxy.capacity();
744 }
745 //*************************************************************************************************
746 
747 
748 //*************************************************************************************************
758 template< typename PT // Type of the proxy
759  , typename VT > // Type of the sparse vector
761 {
762  return proxy.nonZeros();
763 }
764 //*************************************************************************************************
765 
766 
767 //*************************************************************************************************
776 template< typename PT // Type of the proxy
777  , typename VT > // Type of the sparse vector
779 {
780  proxy.reset();
781 }
782 //*************************************************************************************************
783 
784 
785 //*************************************************************************************************
794 template< typename PT // Type of the proxy
795  , typename VT > // Type of the sparse vector
797 {
798  proxy.clear();
799 }
800 //*************************************************************************************************
801 
802 } // namespace blaze
803 
804 #endif
Header file for basic type definitions.
Header file for the SparseVector base class.
BLAZE_ALWAYS_INLINE MT::ConstIterator cbegin(const Matrix< MT, SO > &matrix, size_t i)
Returns an iterator to the first element of row/column i.
Definition: Matrix.h:237
BLAZE_ALWAYS_INLINE size_t size(const Vector< VT, TF > &vector)
Returns the current size/dimension of the vector.
Definition: Vector.h:264
BLAZE_ALWAYS_INLINE MT::ConstIterator cend(const Matrix< MT, SO > &matrix, size_t i)
Returns an iterator just past the last element of row/column i.
Definition: Matrix.h:300
BLAZE_ALWAYS_INLINE MT::Iterator end(Matrix< MT, SO > &matrix, size_t i)
Returns an iterator just past the last element of row/column i.
Definition: Matrix.h:258
void append(size_t index, const ElementType &value, bool check=false) const
Appending an element to the represented sparse vector.
Definition: SparseVectorProxy.h:390
VT::Iterator Iterator
Iterator over non-constant elements.
Definition: SparseVectorProxy.h:78
void reset(const DiagonalProxy< MT > &proxy)
Resetting the represented element to the default initial values.
Definition: DiagonalProxy.h:821
BLAZE_ALWAYS_INLINE size_t capacity(const Matrix< MT, SO > &matrix)
Returns the maximum capacity of the matrix.
Definition: Matrix.h:348
Header file for the IsRowVector type trait.
BLAZE_ALWAYS_INLINE size_t nonZeros(const Matrix< MT, SO > &matrix)
Returns the total number of non-zero elements in the matrix.
Definition: Matrix.h:386
void reset() const
Reset to the default initial value.
Definition: SparseVectorProxy.h:287
Iterator end() const
Returns an iterator just past the last element of the represented vector.
Definition: SparseVectorProxy.h:204
Iterator lowerBound(size_t index) const
Returns an iterator to the first index not less then the given index.
Definition: SparseVectorProxy.h:574
size_t capacity() const
Returns the maximum capacity of the represented vector.
Definition: SparseVectorProxy.h:254
Iterator upperBound(size_t index) const
Returns an iterator to the first index greater then the given index.
Definition: SparseVectorProxy.h:596
Header file for the clear shim.
Namespace of the Blaze C++ math library.
Definition: Blaze.h:57
#define BLAZE_ALWAYS_INLINE
Platform dependent setup of an enforced inline keyword.
Definition: Inline.h:85
const Element * ConstIterator
Iterator over constant elements.
Definition: CompressedMatrix.h:2511
Iterator set(size_t index, const ElementType &value) const
Setting an element of the represented sparse vector.
Definition: SparseVectorProxy.h:329
void erase(size_t index) const
Erasing an element from the sparse vector.
Definition: SparseVectorProxy.h:410
VT::Reference Reference
Reference to a non-constant vector value.
Definition: SparseVectorProxy.h:76
ConstIterator cend() const
Returns an iterator just past the last element of the represented vector.
Definition: SparseVectorProxy.h:218
void resize(size_t n, bool preserve=true) const
Changing the size of the represented vector.
Definition: SparseVectorProxy.h:478
#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
Reference operator[](size_t index) const
Subscript operator for the direct access to vector elements.
Definition: SparseVectorProxy.h:159
BLAZE_ALWAYS_INLINE MT::Iterator begin(Matrix< MT, SO > &matrix, size_t i)
Returns an iterator to the first element of row/column i.
Definition: Matrix.h:195
Type ElementType
Type of the sparse matrix elements.
Definition: CompressedMatrix.h:2505
Constraint on the data type.
const Type & ConstReference
Reference to a constant matrix value.
Definition: CompressedMatrix.h:2509
size_t nonZeros() const
Returns the number of non-zero elements in the represented vector.
Definition: SparseVectorProxy.h:271
void clear(const DiagonalProxy< MT > &proxy)
Clearing the represented element.
Definition: DiagonalProxy.h:841
Iterator begin() const
Returns an iterator to the first element of the represented vector.
Definition: SparseVectorProxy.h:176
void scale(const Other &scalar) const
Scaling of the sparse vector by the scalar value scalar ( ).
Definition: SparseVectorProxy.h:518
VT::ElementType ElementType
Type of the sparse vector elements.
Definition: SparseVectorProxy.h:75
Iterator find(size_t index) const
Searches for a specific vector element.
Definition: SparseVectorProxy.h:552
Iterator insert(size_t index, const ElementType &value) const
Inserting an element into the represented sparse vector.
Definition: SparseVectorProxy.h:354
void clear() const
Clearing the represented vector.
Definition: SparseVectorProxy.h:305
Header file for the reset shim.
Element * Iterator
Iterator over non-constant elements.
Definition: CompressedMatrix.h:2510
Proxy backend for sparse vector types.The SparseVectorProxy class serves as a backend for the Proxy c...
Definition: SparseVectorProxy.h:71
VT::ConstIterator ConstIterator
Iterator over constant elements.
Definition: SparseVectorProxy.h:79
VT::ConstReference ConstReference
Reference to a constant vector value.
Definition: SparseVectorProxy.h:77
Base class for sparse vectors.The SparseVector class is a base class for all arbitrarily sized (N-dim...
Definition: Forward.h:108
size_t size() const
Returns the current size/dimension of the represented vector.
Definition: SparseVectorProxy.h:240
MatrixAccessProxy< This > Reference
Reference to a non-constant matrix value.
Definition: CompressedMatrix.h:2508
System settings for the inline keywords.
ConstIterator cbegin() const
Returns an iterator to the first element of the represented vector.
Definition: SparseVectorProxy.h:190
void reserve(size_t n) const
Setting the minimum capacity of the represented vector.
Definition: SparseVectorProxy.h:499