All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CompressedVector.h
Go to the documentation of this file.
1 //=================================================================================================
20 //=================================================================================================
21 
22 #ifndef _BLAZE_MATH_SPARSE_COMPRESSEDVECTOR_H_
23 #define _BLAZE_MATH_SPARSE_COMPRESSEDVECTOR_H_
24 
25 
26 //*************************************************************************************************
27 // Includes
28 //*************************************************************************************************
29 
30 #include <algorithm>
31 #include <cmath>
32 #include <functional>
33 #include <stdexcept>
37 #include <blaze/math/Functions.h>
39 #include <blaze/math/shims/IsNaN.h>
42 #include <blaze/math/Types.h>
53 #include <blaze/system/Precision.h>
55 #include <blaze/util/Assert.h>
64 #include <blaze/util/EnableIf.h>
65 #include <blaze/util/mpl/If.h>
66 #include <blaze/util/Null.h>
67 #include <blaze/util/Types.h>
70 
71 
72 namespace blaze {
73 
74 //=================================================================================================
75 //
76 // CLASS DEFINITION
77 //
78 //=================================================================================================
79 
80 //*************************************************************************************************
171 template< typename Type // Data type of the vector
172  , bool TF = defaultTransposeFlag > // Transpose flag
173 class CompressedVector : public SparseVector< CompressedVector<Type,TF>, TF >
174 {
175  private:
176  //**Type definitions****************************************************************************
178  //**********************************************************************************************
179 
180  //**Private class Element***********************************************************************
184  struct Element : public ElementBase
185  {
186  using ElementBase::operator=;
187  friend class CompressedVector;
188  };
190  //**********************************************************************************************
191 
192  //**Private class FindIndex*********************************************************************
196  struct FindIndex : public std::binary_function<Element,size_t,bool>
197  {
198  inline bool operator()( const Element& element, size_t index ) const {
199  return element.index() < index;
200  }
201  inline bool operator()( size_t index, const Element& element ) const {
202  return index < element.index();
203  }
204  inline bool operator()( const Element& element1, const Element& element2 ) const {
205  return element1.index() < element2.index();
206  }
207  };
209  //**********************************************************************************************
210 
211  public:
212  //**Type definitions****************************************************************************
214  typedef This ResultType;
216  typedef Type ElementType;
217  typedef const Type& ReturnType;
220  typedef const Type& ConstReference;
221  typedef Element* Iterator;
222  typedef const Element* ConstIterator;
223 
225 
227  //**********************************************************************************************
228 
229  //**Compilation flags***************************************************************************
231 
234  enum { canAlias = 0 };
235  //**********************************************************************************************
236 
237  //**Constructors********************************************************************************
240  explicit inline CompressedVector();
241  explicit inline CompressedVector( size_t size );
242  explicit inline CompressedVector( size_t size, size_t nonzeros );
243  inline CompressedVector( const CompressedVector& sv );
244  template< typename VT > inline CompressedVector( const DenseVector<VT,TF>& dv );
245  template< typename VT > inline CompressedVector( const SparseVector<VT,TF>& sv );
247  //**********************************************************************************************
248 
249  //**Destructor**********************************************************************************
252  inline ~CompressedVector();
254  //**********************************************************************************************
255 
256  //**Data access functions***********************************************************************
259  inline Reference operator[]( size_t index );
260  inline ConstReference operator[]( size_t index ) const;
261  inline Iterator begin ();
262  inline ConstIterator begin () const;
263  inline ConstIterator cbegin() const;
264  inline Iterator end ();
265  inline ConstIterator end () const;
266  inline ConstIterator cend () const;
268  //**********************************************************************************************
269 
270  //**Assignment operators************************************************************************
273  inline CompressedVector& operator= ( const CompressedVector& rhs );
274  template< typename VT > inline CompressedVector& operator= ( const DenseVector<VT,TF>& rhs );
275  template< typename VT > inline CompressedVector& operator= ( const SparseVector<VT,TF>& rhs );
276  template< typename VT > inline CompressedVector& operator+=( const Vector<VT,TF>& rhs );
277  template< typename VT > inline CompressedVector& operator-=( const Vector<VT,TF>& rhs );
278  template< typename VT > inline CompressedVector& operator*=( const Vector<VT,TF>& rhs );
279 
280  template< typename Other >
281  inline typename EnableIf< IsNumeric<Other>, CompressedVector >::Type&
282  operator*=( Other rhs );
283 
284  template< typename Other >
285  inline typename EnableIf< IsNumeric<Other>, CompressedVector >::Type&
286  operator/=( Other rhs );
288  //**********************************************************************************************
289 
290  //**Utility functions***************************************************************************
293  inline size_t size() const;
294  inline size_t capacity() const;
295  inline size_t nonZeros() const;
296  inline void reset();
297  inline void clear();
298  void append( size_t index, const Type& value );
299  Type& insert( size_t index, const Type& value );
300  inline Iterator find ( size_t index );
301  inline ConstIterator find ( size_t index ) const;
302  inline void resize( size_t n, bool preserve=true );
303  inline void reserve( size_t n );
304  inline LengthType length() const;
305  inline const Type sqrLength() const;
306  inline CompressedVector& normalize();
307  inline const CompressedVector getNormalized() const;
308  template< typename Other > inline CompressedVector& scale( Other scalar );
309  inline void swap( CompressedVector& sv ) /* throw() */;
311  //**********************************************************************************************
312 
313  //**Expression template evaluation functions****************************************************
316  template< typename Other > inline bool isAliased( const Other* alias ) const;
317  template< typename VT > inline void assign ( const DenseVector <VT,TF>& rhs );
318  template< typename VT > inline void assign ( const SparseVector<VT,TF>& rhs );
319  template< typename VT > inline void addAssign( const DenseVector <VT,TF>& rhs );
320  template< typename VT > inline void addAssign( const SparseVector<VT,TF>& rhs );
321  template< typename VT > inline void subAssign( const DenseVector <VT,TF>& rhs );
322  template< typename VT > inline void subAssign( const SparseVector<VT,TF>& rhs );
324  //**********************************************************************************************
325 
326  private:
327  //**Utility functions***************************************************************************
330  inline size_t extendCapacity() const;
332  //**********************************************************************************************
333 
334  //**Member variables****************************************************************************
337  size_t size_;
338  size_t capacity_;
341 
342  static const Type zero_;
343 
344  //**********************************************************************************************
345 
346  //**Compile time checks*************************************************************************
354  //**********************************************************************************************
355 };
356 //*************************************************************************************************
357 
358 
359 
360 
361 //=================================================================================================
362 //
363 // DEFINITION AND INITIALIZATION OF THE STATIC MEMBER VARIABLES
364 //
365 //=================================================================================================
366 
367 template< typename Type // Data type of the vector
368  , bool TF > // Transpose flag
369 const Type CompressedVector<Type,TF>::zero_ = Type();
370 
371 
372 
373 
374 //=================================================================================================
375 //
376 // CONSTRUCTORS
377 //
378 //=================================================================================================
379 
380 //*************************************************************************************************
383 template< typename Type // Data type of the vector
384  , bool TF > // Transpose flag
386  : size_ ( 0UL ) // The current size/dimension of the compressed vector
387  , capacity_( 0UL ) // The maximum capacity of the compressed vector
388  , begin_ ( NULL ) // Pointer to the first non-zero element of the compressed vector
389  , end_ ( NULL ) // Pointer to the last non-zero element of the compressed vector
390 {}
391 //*************************************************************************************************
392 
393 
394 //*************************************************************************************************
399 template< typename Type // Data type of the vector
400  , bool TF > // Transpose flag
402  : size_ ( n ) // The current size/dimension of the compressed vector
403  , capacity_( 0UL ) // The maximum capacity of the compressed vector
404  , begin_ ( NULL ) // Pointer to the first non-zero element of the compressed vector
405  , end_ ( NULL ) // Pointer to the last non-zero element of the compressed vector
406 {}
407 //*************************************************************************************************
408 
409 
410 //*************************************************************************************************
416 template< typename Type // Data type of the vector
417  , bool TF > // Transpose flag
418 inline CompressedVector<Type,TF>::CompressedVector( size_t n, size_t nonzeros )
419  : size_ ( n ) // The current size/dimension of the compressed vector
420  , capacity_( nonzeros ) // The maximum capacity of the compressed vector
421  , begin_ ( new Element[capacity_] ) // Pointer to the first non-zero element of the compressed vector
422  , end_ ( begin_ ) // Pointer to the last non-zero element of the compressed vector
423 {}
424 //*************************************************************************************************
425 
426 
427 //*************************************************************************************************
435 template< typename Type // Data type of the vector
436  , bool TF > // Transpose flag
438  : size_ ( sv.size_ ) // The current size/dimension of the compressed vector
439  , capacity_( sv.nonZeros() ) // The maximum capacity of the compressed vector
440  , begin_ ( new Element[capacity_] ) // Pointer to the first non-zero element of the compressed vector
441  , end_ ( begin_+capacity_ ) // Pointer to the last non-zero element of the compressed vector
442 {
443  std::copy( sv.begin_, sv.end_, begin_ );
444 }
445 //*************************************************************************************************
446 
447 
448 //*************************************************************************************************
453 template< typename Type // Data type of the vector
454  , bool TF > // Transpose flag
455 template< typename VT > // Type of the foreign dense vector
457  : size_ ( (~dv).size() ) // The current size/dimension of the compressed vector
458  , capacity_( 0UL ) // The maximum capacity of the compressed vector
459  , begin_ ( NULL ) // Pointer to the first non-zero element of the compressed vector
460  , end_ ( NULL ) // Pointer to the last non-zero element of the compressed vector
461 {
462  using blaze::assign;
463  assign( *this, ~dv );
464 }
465 //*************************************************************************************************
466 
467 
468 //*************************************************************************************************
473 template< typename Type // Data type of the vector
474  , bool TF > // Transpose flag
475 template< typename VT > // Type of the foreign sparse vector
477  : size_ ( (~sv).size() ) // The current size/dimension of the compressed vector
478  , capacity_( (~sv).nonZeros() ) // The maximum capacity of the compressed vector
479  , begin_ ( new Element[capacity_] ) // Pointer to the first non-zero element of the compressed vector
480  , end_ ( begin_ ) // Pointer to the last non-zero element of the compressed vector
481 {
482  using blaze::assign;
483  assign( *this, ~sv );
484 }
485 //*************************************************************************************************
486 
487 
488 
489 
490 //=================================================================================================
491 //
492 // DESTRUCTOR
493 //
494 //=================================================================================================
495 
496 //*************************************************************************************************
499 template< typename Type // Data type of the vector
500  , bool TF > // Transpose flag
502 {
503  delete [] begin_;
504 }
505 //*************************************************************************************************
506 
507 
508 
509 
510 //=================================================================================================
511 //
512 // DATA ACCESS FUNCTIONS
513 //
514 //=================================================================================================
515 
516 //*************************************************************************************************
527 template< typename Type // Data type of the vector
528  , bool TF > // Transpose flag
531 {
532  BLAZE_USER_ASSERT( index < size_, "Invalid compressed vector access index" );
533 
534  return Reference( *this, index );
535 }
536 //*************************************************************************************************
537 
538 
539 //*************************************************************************************************
545 template< typename Type // Data type of the vector
546  , bool TF > // Transpose flag
549 {
550  BLAZE_USER_ASSERT( index < size_, "Invalid compressed vector access index" );
551 
552  const Iterator pos( std::lower_bound( begin_, end_, index, FindIndex() ) );
553 
554  if( pos == end_ || pos->index_ != index )
555  return zero_;
556  else
557  return pos->value_;
558 }
559 //*************************************************************************************************
560 
561 
562 //*************************************************************************************************
567 template< typename Type // Data type of the vector
568  , bool TF > // Transpose flag
570 {
571  return Iterator( begin_ );
572 }
573 //*************************************************************************************************
574 
575 
576 //*************************************************************************************************
581 template< typename Type // Data type of the vector
582  , bool TF > // Transpose flag
584 {
585  return ConstIterator( begin_ );
586 }
587 //*************************************************************************************************
588 
589 
590 //*************************************************************************************************
595 template< typename Type // Data type of the vector
596  , bool TF > // Transpose flag
598 {
599  return ConstIterator( begin_ );
600 }
601 //*************************************************************************************************
602 
603 
604 //*************************************************************************************************
609 template< typename Type // Data type of the vector
610  , bool TF > // Transpose flag
612 {
613  return Iterator( end_ );
614 }
615 //*************************************************************************************************
616 
617 
618 //*************************************************************************************************
623 template< typename Type // Data type of the vector
624  , bool TF > // Transpose flag
626 {
627  return ConstIterator( end_ );
628 }
629 //*************************************************************************************************
630 
631 
632 //*************************************************************************************************
637 template< typename Type // Data type of the vector
638  , bool TF > // Transpose flag
640 {
641  return ConstIterator( end_ );
642 }
643 //*************************************************************************************************
644 
645 
646 
647 
648 //=================================================================================================
649 //
650 // ASSIGNMENT OPERATORS
651 //
652 //=================================================================================================
653 
654 //*************************************************************************************************
663 template< typename Type // Data type of the vector
664  , bool TF > // Transpose flag
667 {
668  if( &rhs == this ) return *this;
669 
670  const size_t nonzeros( rhs.nonZeros() );
671 
672  if( nonzeros > capacity_ ) {
673  Iterator newBegin( new Element[nonzeros] );
674  end_ = std::copy( rhs.begin_, rhs.end_, newBegin );
675  std::swap( begin_, newBegin );
676  delete [] newBegin;
677 
678  size_ = rhs.size_;
679  capacity_ = nonzeros;
680  }
681  else {
682  end_ = std::copy( rhs.begin_, rhs.end_, begin_ );
683  size_ = rhs.size_;
684  }
685 
686  return *this;
687 }
688 //*************************************************************************************************
689 
690 
691 //*************************************************************************************************
700 template< typename Type // Data type of the vector
701  , bool TF > // Transpose flag
702 template< typename VT > // Type of the right-hand side dense vector
705 {
706  using blaze::assign;
707 
708  if( CanAlias<VT>::value && (~rhs).isAliased( this ) ) {
709  CompressedVector tmp( rhs );
710  swap( tmp );
711  }
712  else {
713  size_ = (~rhs).size();
714  end_ = begin_;
715  assign( *this, ~rhs );
716  }
717 
718  return *this;
719 }
720 //*************************************************************************************************
721 
722 
723 //*************************************************************************************************
732 template< typename Type // Data type of the vector
733  , bool TF > // Transpose flag
734 template< typename VT > // Type of the right-hand side sparse vector
737 {
738  using blaze::assign;
739 
740  if( ( CanAlias<VT>::value && (~rhs).isAliased( this ) ) ||
741  (~rhs).nonZeros() > capacity_ ) {
742  CompressedVector tmp( rhs );
743  swap( tmp );
744  }
745  else {
746  size_ = (~rhs).size();
747  end_ = begin_;
748  assign( *this, ~rhs );
749  }
750 
751  return *this;
752 }
753 //*************************************************************************************************
754 
755 
756 //*************************************************************************************************
766 template< typename Type // Data type of the vector
767  , bool TF > // Transpose flag
768 template< typename VT > // Type of the right-hand side vector
770 {
771  using blaze::addAssign;
772 
773  if( (~rhs).size() != size_ )
774  throw std::invalid_argument( "Vector sizes do not match" );
775 
776  addAssign( *this, ~rhs );
777 
778  return *this;
779 }
780 //*************************************************************************************************
781 
782 
783 //*************************************************************************************************
793 template< typename Type // Data type of the vector
794  , bool TF > // Transpose flag
795 template< typename VT > // Type of the right-hand side vector
797 {
798  using blaze::subAssign;
799 
800  if( (~rhs).size() != size_ )
801  throw std::invalid_argument( "Vector sizes do not match" );
802 
803  subAssign( *this, ~rhs );
804 
805  return *this;
806 }
807 //*************************************************************************************************
808 
809 
810 //*************************************************************************************************
821 template< typename Type // Data type of the vector
822  , bool TF > // Transpose flag
823 template< typename VT > // Type of the right-hand side vector
825 {
826  if( (~rhs).size() != size_ )
827  throw std::invalid_argument( "Vector sizes do not match" );
828 
829  CompressedVector<Type,TF> tmp( *this * (~rhs) );
830  swap( tmp );
831 
832  return *this;
833 }
834 //*************************************************************************************************
835 
836 
837 //*************************************************************************************************
848 template< typename Type // Data type of the vector
849  , bool TF > // Transpose flag
850 template< typename Other > // Data type of the right-hand side scalar
853 {
854  for( Iterator element=begin_; element<end_; ++element )
855  element->value_ *= rhs;
856  return *this;
857 }
858 //*************************************************************************************************
859 
860 
861 //*************************************************************************************************
873 template< typename Type // Data type of the vector
874  , bool TF > // Transpose flag
875 template< typename Other > // Data type of the right-hand side scalar
878 {
879  BLAZE_USER_ASSERT( rhs != Other(0), "Division by zero detected" );
880 
881  typedef typename DivTrait<Type,Other>::Type DT;
882  typedef typename If< IsNumeric<DT>, DT, Other >::Type Tmp;
883 
884  // Depending on the two involved data types, an integer division is applied or a
885  // floating point division is selected.
887  const Tmp tmp( Tmp(1)/static_cast<Tmp>( rhs ) );
888  for( Iterator element=begin_; element!=end_; ++element )
889  element->value_ *= tmp;
890  }
891  else {
892  for( Iterator element=begin_; element!=end_; ++element )
893  element->value_ /= rhs;
894  }
895 
896  return *this;
897 }
898 //*************************************************************************************************
899 
900 
901 
902 
903 //=================================================================================================
904 //
905 // UTILITY FUNCTIONS
906 //
907 //=================================================================================================
908 
909 //*************************************************************************************************
914 template< typename Type // Data type of the vector
915  , bool TF > // Transpose flag
916 inline size_t CompressedVector<Type,TF>::size() const
917 {
918  return size_;
919 }
920 //*************************************************************************************************
921 
922 
923 //*************************************************************************************************
928 template< typename Type // Data type of the vector
929  , bool TF > // Transpose flag
931 {
932  return capacity_;
933 }
934 //*************************************************************************************************
935 
936 
937 //*************************************************************************************************
945 template< typename Type // Data type of the vector
946  , bool TF > // Transpose flag
948 {
949  return end_ - begin_;
950 }
951 //*************************************************************************************************
952 
953 
954 //*************************************************************************************************
959 template< typename Type // Data type of the vector
960  , bool TF > // Transpose flag
962 {
963  end_ = begin_;
964 }
965 //*************************************************************************************************
966 
967 
968 //*************************************************************************************************
975 template< typename Type // Data type of the vector
976  , bool TF > // Transpose flag
978 {
979  size_ = 0UL;
980  end_ = begin_;
981 }
982 //*************************************************************************************************
983 
984 
985 //*************************************************************************************************
1006 template< typename Type // Data type of the vector
1007  , bool TF > // Transpose flag
1008 void CompressedVector<Type,TF>::append( size_t index, const Type& value )
1009 {
1010  BLAZE_USER_ASSERT( index < size_, "Invalid compressed vector access index" );
1011  BLAZE_USER_ASSERT( nonZeros() < capacity(), "Not enough reserved space" );
1012  BLAZE_USER_ASSERT( begin_ == end_ || (end_-1UL)->index_ < index, "Index is not strictly increasing" );
1013 
1014  end_->value_ = value;
1015  end_->index_ = index;
1016  ++end_;
1017 }
1018 //*************************************************************************************************
1019 
1020 
1021 //*************************************************************************************************
1033 template< typename Type // Data type of the vector
1034  , bool TF > // Transpose flag
1035 Type& CompressedVector<Type,TF>::insert( size_t index, const Type& value )
1036 {
1037  BLAZE_USER_ASSERT( index < size_, "Invalid compressed vector access index" );
1038 
1039  const Iterator pos( std::lower_bound( begin_, end_, index, FindIndex() ) );
1040 
1041  if( pos != end_ && pos->index_ == index )
1042  throw std::invalid_argument( "Bad access index" );
1043 
1044  if( nonZeros() != capacity_ ) {
1045  std::copy_backward( pos, end_, end_+1 );
1046  pos->value_ = value;
1047  pos->index_ = index;
1048  ++end_;
1049 
1050  return pos->value_;
1051  }
1052  else {
1053  size_t newCapacity( extendCapacity() );
1054 
1055  Iterator newBegin = new Element[newCapacity];
1056  Iterator tmp = std::copy( begin_, pos, newBegin );
1057  tmp->value_ = value;
1058  tmp->index_ = index;
1059  end_ = std::copy( pos, end_, tmp+1 );
1060 
1061  std::swap( newBegin, begin_ );
1062  delete [] newBegin;
1063  capacity_ = newCapacity;
1064 
1065  return tmp->value_;
1066  }
1067 }
1068 //*************************************************************************************************
1069 
1070 
1071 //*************************************************************************************************
1084 template< typename Type // Data type of the vector
1085  , bool TF > // Transpose flag
1087 {
1088  return const_cast<Iterator>( const_cast<const This&>( *this ).find( index ) );
1089 }
1090 //*************************************************************************************************
1091 
1092 
1093 //*************************************************************************************************
1106 template< typename Type // Data type of the vector
1107  , bool TF > // Transpose flag
1109 {
1110  const Iterator pos( std::lower_bound( begin_, end_, index, FindIndex() ) );
1111  if( pos != end_ && pos->index_ == index )
1112  return pos;
1113  else return end_;
1114 }
1115 //*************************************************************************************************
1116 
1117 
1118 //*************************************************************************************************
1130 template< typename Type // Data type of the vector
1131  , bool TF > // Transpose flag
1132 inline void CompressedVector<Type,TF>::resize( size_t n, bool preserve )
1133 {
1134  if( preserve ) {
1135  end_ = std::lower_bound( begin_, end_, n, FindIndex() );
1136  }
1137  else {
1138  end_ = begin_;
1139  }
1140 
1141  size_ = n;
1142 }
1143 //*************************************************************************************************
1144 
1145 
1146 //*************************************************************************************************
1155 template< typename Type // Data type of the vector
1156  , bool TF > // Transpose flag
1157 inline void CompressedVector<Type,TF>::reserve( size_t n )
1158 {
1159  if( n > capacity_ ) {
1160  const size_t newCapacity( n );
1161 
1162  // Allocating a new data and index array
1163  Iterator newBegin = new Element[newCapacity];
1164 
1165  // Replacing the old data and index array
1166  end_ = std::copy( begin_, end_, newBegin );
1167  std::swap( newBegin, begin_ );
1168  capacity_ = newCapacity;
1169  delete [] newBegin;
1170  }
1171 }
1172 //*************************************************************************************************
1173 
1174 
1175 //*************************************************************************************************
1205 template< typename Type // Data type of the vector
1206  , bool TF > // Transpose flag
1207 #ifndef WIN32
1209 #else
1211 #endif
1212 {
1214 
1215  LengthType sum( 0 );
1216  for( Iterator element=begin_; element!=end_; ++element )
1217  sum += element->value_ * element->value_;
1218  return std::sqrt( sum );
1219 }
1220 //*************************************************************************************************
1221 
1222 
1223 //*************************************************************************************************
1233 template< typename Type // Data type of the vector
1234  , bool TF > // Transpose flag
1235 inline const Type CompressedVector<Type,TF>::sqrLength() const
1236 {
1238 
1239  Type sum( 0 );
1240  for( Iterator element=begin_; element!=end_; ++element )
1241  sum += element->value_ * element->value_;
1242  return sum;
1243 }
1244 //*************************************************************************************************
1245 
1246 
1247 //*************************************************************************************************
1256 template< typename Type // Data type of the vector
1257  , bool TF > // Transpose flag
1259 {
1261 
1262  const Type len( length() );
1263 
1264  if( len == Type(0) )
1265  return *this;
1266 
1267  const Type ilen( Type(1) / len );
1268 
1269  for( Iterator element=begin_; element!=end_; ++element )
1270  element->value_ *= ilen;
1271 
1272  return *this;
1273 }
1274 //*************************************************************************************************
1275 
1276 
1277 //*************************************************************************************************
1286 template< typename Type // Data type of the vector
1287  , bool TF > // Transpose flag
1289 {
1291 
1292  const Type len( length() );
1293 
1294  if( len == Type(0) )
1295  return *this;
1296 
1297  const Type ilen( Type(1) / len );
1298  CompressedVector tmp( *this );
1299 
1300  for( Iterator element=tmp.begin_; element!=tmp.end_; ++element ) {
1301  element->value_ *= ilen;
1302  }
1303 
1304  return tmp;
1305 }
1306 //*************************************************************************************************
1307 
1308 
1309 //*************************************************************************************************
1315 template< typename Type // Data type of the vector
1316  , bool TF > // Transpose flag
1317 template< typename Other > // Data type of the scalar value
1319 {
1320  for( Iterator element=begin_; element!=end_; ++element )
1321  element->value_ *= scalar;
1322  return *this;
1323 }
1324 //*************************************************************************************************
1325 
1326 
1327 //*************************************************************************************************
1334 template< typename Type // Data type of the vector
1335  , bool TF > // Transpose flag
1336 inline void CompressedVector<Type,TF>::swap( CompressedVector& sv ) /* throw() */
1337 {
1338  std::swap( size_, sv.size_ );
1339  std::swap( capacity_, sv.capacity_ );
1340  std::swap( begin_, sv.begin_ );
1341  std::swap( end_, sv.end_ );
1342 }
1343 //*************************************************************************************************
1344 
1345 
1346 //*************************************************************************************************
1354 template< typename Type // Data type of the vector
1355  , bool TF > // Transpose flag
1357 {
1358  using blaze::max;
1359  using blaze::min;
1360 
1361  size_t nonzeros( 2UL*capacity_+1UL );
1362  nonzeros = max( nonzeros, 7UL );
1363  nonzeros = min( nonzeros, size_ );
1364 
1365  BLAZE_INTERNAL_ASSERT( nonzeros > capacity_, "Invalid capacity value" );
1366 
1367  return nonzeros;
1368 }
1369 //*************************************************************************************************
1370 
1371 
1372 
1373 
1374 //=================================================================================================
1375 //
1376 // EXPRESSION TEMPLATE EVALUATION FUNCTIONS
1377 //
1378 //=================================================================================================
1379 
1380 //*************************************************************************************************
1386 template< typename Type // Data type of the vector
1387  , bool TF > // Transpose flag
1388 template< typename Other > // Data type of the foreign expression
1389 inline bool CompressedVector<Type,TF>::isAliased( const Other* alias ) const
1390 {
1391  return static_cast<const void*>( this ) == static_cast<const void*>( alias );
1392 }
1393 //*************************************************************************************************
1394 
1395 
1396 //*************************************************************************************************
1407 template< typename Type // Data type of the vector
1408  , bool TF > // Transpose flag
1409 template< typename VT > // Type of the right-hand side dense vector
1411 {
1412  BLAZE_INTERNAL_ASSERT( size_ == (~rhs).size(), "Invalid vector sizes" );
1413 
1414  size_t nonzeros( 0UL );
1415 
1416  for( size_t i=0UL; i<size_; ++i )
1417  {
1418  if( nonzeros == capacity_ )
1419  reserve( extendCapacity() );
1420 
1421  end_->value_ = (~rhs)[i];
1422 
1423  if( !isDefault( end_->value_ ) ) {
1424  end_->index_ = i;
1425  ++end_;
1426  ++nonzeros;
1427  }
1428  }
1429 }
1430 //*************************************************************************************************
1431 
1432 
1433 //*************************************************************************************************
1444 template< typename Type // Data type of the vector
1445  , bool TF > // Transpose flag
1446 template< typename VT > // Type of the right-hand side sparse vector
1448 {
1449  BLAZE_INTERNAL_ASSERT( size_ == (~rhs).size(), "Invalid vector sizes" );
1450 
1451  // Using the following formulation instead of a std::copy function call of the form
1452  //
1453  // end_ = std::copy( (~rhs).begin(), (~rhs).end(), begin_ );
1454  //
1455  // results in much less requirements on the ConstIterator type provided from the right-hand
1456  // sparse vector type
1457  for( typename VT::ConstIterator element=(~rhs).begin(); element!=(~rhs).end(); ++element )
1458  append( element->index(), element->value() );
1459 }
1460 //*************************************************************************************************
1461 
1462 
1463 //*************************************************************************************************
1474 template< typename Type // Data type of the vector
1475  , bool TF > // Transpose flag
1476 template< typename VT > // Type of the right-hand side dense vector
1478 {
1479  typedef typename AddTrait<This,typename VT::ResultType>::Type AddType;
1480 
1483  BLAZE_CONSTRAINT_MUST_BE_REFERENCE_TYPE( typename AddType::CompositeType );
1484 
1485  BLAZE_INTERNAL_ASSERT( size_ == (~rhs).size(), "Invalid vector sizes" );
1486 
1487  AddType tmp( *this + (~rhs) );
1488  reset();
1489  assign( tmp );
1490 }
1491 //*************************************************************************************************
1492 
1493 
1494 //*************************************************************************************************
1505 template< typename Type // Data type of the vector
1506  , bool TF > // Transpose flag
1507 template< typename VT > // Type of the right-hand side sparse vector
1509 {
1510  BLAZE_INTERNAL_ASSERT( size_ == (~rhs).size(), "Invalid vector sizes" );
1511 
1512  CompressedVector<Type,TF> tmp( *this + (~rhs) );
1513  swap( tmp );
1514 }
1515 //*************************************************************************************************
1516 
1517 
1518 //*************************************************************************************************
1529 template< typename Type // Data type of the vector
1530  , bool TF > // Transpose flag
1531 template< typename VT > // Type of the right-hand side dense vector
1533 {
1534  typedef typename SubTrait<This,typename VT::ResultType>::Type SubType;
1535 
1538  BLAZE_CONSTRAINT_MUST_BE_REFERENCE_TYPE( typename SubType::CompositeType );
1539 
1540  BLAZE_INTERNAL_ASSERT( size_ == (~rhs).size(), "Invalid vector sizes" );
1541 
1542  SubType tmp( *this - (~rhs) );
1543  reset();
1544  assign( tmp );
1545 }
1546 //*************************************************************************************************
1547 
1548 
1549 //*************************************************************************************************
1560 template< typename Type // Data type of the vector
1561  , bool TF > // Transpose flag
1562 template< typename VT > // Type of the right-hand side sparse vector
1564 {
1565  BLAZE_INTERNAL_ASSERT( size_ == (~rhs).size(), "Invalid vector sizes" );
1566 
1567  CompressedVector<Type,TF> tmp( *this - (~rhs) );
1568  swap( tmp );
1569 }
1570 //*************************************************************************************************
1571 
1572 
1573 
1574 
1575 //=================================================================================================
1576 //
1577 // GLOBAL OPERATORS
1578 //
1579 //=================================================================================================
1580 
1581 //*************************************************************************************************
1584 template< typename Type, bool TF >
1585 inline void reset( CompressedVector<Type,TF>& v );
1586 
1587 template< typename Type, bool TF >
1588 inline void clear( CompressedVector<Type,TF>& v );
1589 
1590 template< typename Type, bool TF >
1591 inline bool isnan( const CompressedVector<Type,TF>& v );
1592 
1593 template< typename Type, bool TF >
1594 inline bool isDefault( const CompressedVector<Type,TF>& v );
1595 
1596 template< typename Type, bool TF >
1597 inline void swap( CompressedVector<Type,TF>& a, CompressedVector<Type,TF>& b ) /* throw() */;
1599 //*************************************************************************************************
1600 
1601 
1602 //*************************************************************************************************
1609 template< typename Type // Data type of the vector
1610  , bool TF > // Transpose flag
1612 {
1613  v.reset();
1614 }
1615 //*************************************************************************************************
1616 
1617 
1618 //*************************************************************************************************
1625 template< typename Type // Data type of the vector
1626  , bool TF > // Transpose flag
1628 {
1629  v.clear();
1630 }
1631 //*************************************************************************************************
1632 
1633 
1634 //*************************************************************************************************
1651 template< typename Type // Data type of the vector
1652  , bool TF > // Transpose flag
1653 inline bool isnan( const CompressedVector<Type,TF>& v )
1654 {
1655  typedef typename CompressedVector<Type,TF>::ConstIterator ConstIterator;
1656 
1657  const ConstIterator end( v.end() );
1658  for( ConstIterator element=v.begin(); element!=end; ++element ) {
1659  if( isnan( element->value() ) ) return true;
1660  }
1661  return false;
1662 }
1663 //*************************************************************************************************
1664 
1665 
1666 //*************************************************************************************************
1685 template< typename Type // Data type of the vector
1686  , bool TF > // Transpose flag
1687 inline bool isDefault( const CompressedVector<Type,TF>& v )
1688 {
1689  typedef typename CompressedVector<Type,TF>::ConstIterator ConstIterator;
1690 
1691  for( ConstIterator element=v.begin(); element!=v.end(); ++element )
1692  if( !isDefault( element->value() ) ) return false;
1693  return true;
1694 }
1695 //*************************************************************************************************
1696 
1697 
1698 //*************************************************************************************************
1707 template< typename Type // Data type of the vector
1708  , bool TF > // Transpose flag
1709 inline void swap( CompressedVector<Type,TF>& a, CompressedVector<Type,TF>& b ) /* throw() */
1710 {
1711  a.swap( b );
1712 }
1713 //*************************************************************************************************
1714 
1715 
1716 
1717 
1718 //=================================================================================================
1719 //
1720 // ISRESIZABLE SPECIALIZATIONS
1721 //
1722 //=================================================================================================
1723 
1724 //*************************************************************************************************
1726 template< typename T, bool TF >
1727 struct IsResizable< CompressedVector<T,TF> > : public TrueType
1728 {
1729  enum { value = 1 };
1730  typedef TrueType Type;
1731 };
1732 
1733 template< typename T, bool TF >
1734 struct IsResizable< const CompressedVector<T,TF> > : public TrueType
1735 {
1736  enum { value = 1 };
1737  typedef TrueType Type;
1738 };
1739 
1740 template< typename T, bool TF >
1741 struct IsResizable< volatile CompressedVector<T,TF> > : public TrueType
1742 {
1743  enum { value = 1 };
1744  typedef TrueType Type;
1745 };
1746 
1747 template< typename T, bool TF >
1748 struct IsResizable< const volatile CompressedVector<T,TF> > : public TrueType
1749 {
1750  enum { value = 1 };
1751  typedef TrueType Type;
1752 };
1754 //*************************************************************************************************
1755 
1756 
1757 
1758 
1759 //=================================================================================================
1760 //
1761 // ADDTRAIT SPECIALIZATIONS
1762 //
1763 //=================================================================================================
1764 
1765 //*************************************************************************************************
1767 template< typename T1, bool TF, typename T2, size_t N >
1768 struct AddTrait< CompressedVector<T1,TF>, StaticVector<T2,N,TF> >
1769 {
1770  typedef StaticVector< typename AddTrait<T1,T2>::Type, N, TF > Type;
1771 };
1772 
1773 template< typename T1, size_t N, bool TF, typename T2 >
1774 struct AddTrait< StaticVector<T1,N,TF>, CompressedVector<T2,TF> >
1775 {
1776  typedef StaticVector< typename AddTrait<T1,T2>::Type, N, TF > Type;
1777 };
1778 
1779 template< typename T1, bool TF, typename T2 >
1780 struct AddTrait< CompressedVector<T1,TF>, DynamicVector<T2,TF> >
1781 {
1782  typedef DynamicVector< typename AddTrait<T1,T2>::Type, TF > Type;
1783 };
1784 
1785 template< typename T1, bool TF, typename T2 >
1786 struct AddTrait< DynamicVector<T1,TF>, CompressedVector<T2,TF> >
1787 {
1788  typedef DynamicVector< typename AddTrait<T1,T2>::Type, TF > Type;
1789 };
1790 
1791 template< typename T1, bool TF, typename T2 >
1792 struct AddTrait< CompressedVector<T1,TF>, CompressedVector<T2,TF> >
1793 {
1794  typedef CompressedVector< typename AddTrait<T1,T2>::Type, TF > Type;
1795 };
1797 //*************************************************************************************************
1798 
1799 
1800 
1801 
1802 //=================================================================================================
1803 //
1804 // SUBTRAIT SPECIALIZATIONS
1805 //
1806 //=================================================================================================
1807 
1808 //*************************************************************************************************
1810 template< typename T1, bool TF, typename T2, size_t N >
1811 struct SubTrait< CompressedVector<T1,TF>, StaticVector<T2,N,TF> >
1812 {
1813  typedef StaticVector< typename SubTrait<T1,T2>::Type, N, TF > Type;
1814 };
1815 
1816 template< typename T1, size_t N, bool TF, typename T2 >
1817 struct SubTrait< StaticVector<T1,N,TF>, CompressedVector<T2,TF> >
1818 {
1819  typedef StaticVector< typename SubTrait<T1,T2>::Type, N, TF > Type;
1820 };
1821 
1822 template< typename T1, bool TF, typename T2 >
1823 struct SubTrait< CompressedVector<T1,TF>, DynamicVector<T2,TF> >
1824 {
1825  typedef DynamicVector< typename SubTrait<T1,T2>::Type, TF > Type;
1826 };
1827 
1828 template< typename T1, bool TF, typename T2 >
1829 struct SubTrait< DynamicVector<T1,TF>, CompressedVector<T2,TF> >
1830 {
1831  typedef DynamicVector< typename SubTrait<T1,T2>::Type, TF > Type;
1832 };
1833 
1834 template< typename T1, bool TF, typename T2 >
1835 struct SubTrait< CompressedVector<T1,TF>, CompressedVector<T2,TF> >
1836 {
1837  typedef CompressedVector< typename SubTrait<T1,T2>::Type, TF > Type;
1838 };
1840 //*************************************************************************************************
1841 
1842 
1843 
1844 
1845 //=================================================================================================
1846 //
1847 // MULTTRAIT SPECIALIZATIONS
1848 //
1849 //=================================================================================================
1850 
1851 //*************************************************************************************************
1853 template< typename T1, bool TF, typename T2 >
1854 struct MultTrait< CompressedVector<T1,TF>, T2 >
1855 {
1856  typedef CompressedVector< typename MultTrait<T1,T2>::Type, TF > Type;
1858 };
1859 
1860 template< typename T1, typename T2, bool TF >
1861 struct MultTrait< T1, CompressedVector<T2,TF> >
1862 {
1863  typedef CompressedVector< typename MultTrait<T1,T2>::Type, TF > Type;
1865 };
1866 
1867 template< typename T1, bool TF, typename T2, size_t N >
1868 struct MultTrait< CompressedVector<T1,TF>, StaticVector<T2,N,TF> >
1869 {
1870  typedef CompressedVector< typename MultTrait<T1,T2>::Type, TF > Type;
1871 };
1872 
1873 template< typename T1, typename T2, size_t N >
1874 struct MultTrait< CompressedVector<T1,false>, StaticVector<T2,N,true> >
1875 {
1876  typedef CompressedMatrix< typename MultTrait<T1,T2>::Type, true > Type;
1877 };
1878 
1879 template< typename T1, typename T2, size_t N >
1880 struct MultTrait< CompressedVector<T1,true>, StaticVector<T2,N,false> >
1881 {
1882  typedef typename MultTrait<T1,T2>::Type Type;
1883 };
1884 
1885 template< typename T1, size_t N, bool TF, typename T2 >
1886 struct MultTrait< StaticVector<T1,N,TF>, CompressedVector<T2,TF> >
1887 {
1888  typedef CompressedVector< typename MultTrait<T1,T2>::Type, TF > Type;
1889 };
1890 
1891 template< typename T1, size_t N, typename T2 >
1892 struct MultTrait< StaticVector<T1,N,false>, CompressedVector<T2,true> >
1893 {
1894  typedef CompressedMatrix< typename MultTrait<T1,T2>::Type, false > Type;
1895 };
1896 
1897 template< typename T1, size_t N, typename T2 >
1898 struct MultTrait< StaticVector<T1,N,true>, CompressedVector<T2,false> >
1899 {
1900  typedef typename MultTrait<T1,T2>::Type Type;
1901 };
1902 
1903 template< typename T1, bool TF, typename T2 >
1904 struct MultTrait< CompressedVector<T1,TF>, DynamicVector<T2,TF> >
1905 {
1906  typedef CompressedVector< typename MultTrait<T1,T2>::Type, TF > Type;
1907 };
1908 
1909 template< typename T1, typename T2 >
1910 struct MultTrait< CompressedVector<T1,false>, DynamicVector<T2,true> >
1911 {
1912  typedef CompressedMatrix< typename MultTrait<T1,T2>::Type, true > Type;
1913 };
1914 
1915 template< typename T1, typename T2 >
1916 struct MultTrait< CompressedVector<T1,true>, DynamicVector<T2,false> >
1917 {
1918  typedef typename MultTrait<T1,T2>::Type Type;
1919 };
1920 
1921 template< typename T1, bool TF, typename T2 >
1922 struct MultTrait< DynamicVector<T1,TF>, CompressedVector<T2,TF> >
1923 {
1924  typedef CompressedVector< typename MultTrait<T1,T2>::Type, TF > Type;
1925 };
1926 
1927 template< typename T1, typename T2 >
1928 struct MultTrait< DynamicVector<T1,false>, CompressedVector<T2,true> >
1929 {
1930  typedef CompressedMatrix< typename MultTrait<T1,T2>::Type, false > Type;
1931 };
1932 
1933 template< typename T1, typename T2 >
1934 struct MultTrait< DynamicVector<T1,true>, CompressedVector<T2,false> >
1935 {
1936  typedef typename MultTrait<T1,T2>::Type Type;
1937 };
1938 
1939 template< typename T1, bool TF, typename T2 >
1940 struct MultTrait< CompressedVector<T1,TF>, CompressedVector<T2,TF> >
1941 {
1942  typedef CompressedVector< typename MultTrait<T1,T2>::Type, TF > Type;
1943 };
1944 
1945 template< typename T1, typename T2 >
1946 struct MultTrait< CompressedVector<T1,false>, CompressedVector<T2,true> >
1947 {
1948  typedef CompressedMatrix< typename MultTrait<T1,T2>::Type, false > Type;
1949 };
1950 
1951 template< typename T1, typename T2 >
1952 struct MultTrait< CompressedVector<T1,true>, CompressedVector<T2,false> >
1953 {
1954  typedef typename MultTrait<T1,T2>::Type Type;
1955 };
1957 //*************************************************************************************************
1958 
1959 
1960 
1961 
1962 //=================================================================================================
1963 //
1964 // CROSSTRAIT SPECIALIZATIONS
1965 //
1966 //=================================================================================================
1967 
1968 //*************************************************************************************************
1970 template< typename T1, typename T2 >
1971 struct CrossTrait< CompressedVector<T1,false>, StaticVector<T2,3UL,false> >
1972 {
1973  private:
1974  typedef typename MultTrait<T1,T2>::Type T;
1975 
1976  public:
1977  typedef StaticVector< typename SubTrait<T,T>::Type, 3UL, false > Type;
1978 };
1979 
1980 template< typename T1, typename T2 >
1981 struct CrossTrait< StaticVector<T1,3UL,false>, CompressedVector<T2,false> >
1982 {
1983  private:
1984  typedef typename MultTrait<T1,T2>::Type T;
1985 
1986  public:
1987  typedef StaticVector< typename SubTrait<T,T>::Type, 3UL, false > Type;
1988 };
1989 
1990 template< typename T1, typename T2 >
1991 struct CrossTrait< CompressedVector<T1,false>, DynamicVector<T2,false> >
1992 {
1993  private:
1994  typedef typename MultTrait<T1,T2>::Type T;
1995 
1996  public:
1997  typedef StaticVector< typename SubTrait<T,T>::Type, 3UL, false > Type;
1998 };
1999 
2000 template< typename T1, typename T2 >
2001 struct CrossTrait< DynamicVector<T1,false>, CompressedVector<T2,false> >
2002 {
2003  private:
2004  typedef typename MultTrait<T1,T2>::Type T;
2005 
2006  public:
2007  typedef StaticVector< typename SubTrait<T,T>::Type, 3UL, false > Type;
2008 };
2009 
2010 template< typename T1, typename T2 >
2011 struct CrossTrait< CompressedVector<T1,false>, CompressedVector<T2,false> >
2012 {
2013  private:
2014  typedef typename MultTrait<T1,T2>::Type T;
2015 
2016  public:
2017  typedef StaticVector< typename SubTrait<T,T>::Type, 3UL, false > Type;
2018 };
2020 //*************************************************************************************************
2021 
2022 
2023 
2024 
2025 //=================================================================================================
2026 //
2027 // DIVTRAIT SPECIALIZATIONS
2028 //
2029 //=================================================================================================
2030 
2031 //*************************************************************************************************
2033 template< typename T1, bool TF, typename T2 >
2034 struct DivTrait< CompressedVector<T1,TF>, T2 >
2035 {
2036  typedef CompressedVector< typename DivTrait<T1,T2>::Type, TF > Type;
2038 };
2040 //*************************************************************************************************
2041 
2042 
2043 
2044 
2045 //=================================================================================================
2046 //
2047 // MATHTRAIT SPECIALIZATIONS
2048 //
2049 //=================================================================================================
2050 
2051 //*************************************************************************************************
2053 template< typename T1, bool TF, typename T2 >
2054 struct MathTrait< CompressedVector<T1,TF>, CompressedVector<T2,TF> >
2055 {
2056  typedef CompressedVector< typename MathTrait<T1,T2>::HighType, TF > HighType;
2057  typedef CompressedVector< typename MathTrait<T1,T2>::LowType , TF > LowType;
2058 };
2060 //*************************************************************************************************
2061 
2062 } // namespace blaze
2063 
2064 #endif