CompressedVector.h
Go to the documentation of this file.
1 //=================================================================================================
33 //=================================================================================================
34 
35 #ifndef _BLAZE_MATH_SPARSE_COMPRESSEDVECTOR_H_
36 #define _BLAZE_MATH_SPARSE_COMPRESSEDVECTOR_H_
37 
38 
39 //*************************************************************************************************
40 // Includes
41 //*************************************************************************************************
42 
43 #include <algorithm>
44 #include <utility>
45 #include <blaze/math/Aliases.h>
49 #include <blaze/math/Exception.h>
52 #include <blaze/math/Forward.h>
86 #include <blaze/util/Assert.h>
92 #include <blaze/util/DisableIf.h>
93 #include <blaze/util/EnableIf.h>
94 #include <blaze/util/Memory.h>
95 #include <blaze/util/Types.h>
100 
101 
102 namespace blaze {
103 
104 //=================================================================================================
105 //
106 // CLASS DEFINITION
107 //
108 //=================================================================================================
109 
110 //*************************************************************************************************
204 template< typename Type // Data type of the vector
205  , bool TF = defaultTransposeFlag > // Transpose flag
207  : public SparseVector< CompressedVector<Type,TF>, TF >
208 {
209  private:
210  //**Type definitions****************************************************************************
213  //**********************************************************************************************
214 
215  //**Private class Element***********************************************************************
222  struct Element
223  : public ElementBase
224  {
225  //**Constructors*****************************************************************************
226  explicit Element() = default;
227  Element( const Element& rhs ) = default;
228  Element( Element&& rhs ) = default;
229  //*******************************************************************************************
230 
231  //**Assignment operators*********************************************************************
232  inline Element& operator=( const Element& rhs )
233  {
234  this->value_ = rhs.value_;
235  return *this;
236  }
237 
238  inline Element& operator=( Element&& rhs )
239  {
240  this->value_ = std::move( rhs.value_ );
241  return *this;
242  }
243 
244  template< typename Other >
245  inline auto operator=( const Other& rhs )
247  {
248  this->value_ = rhs.value();
249  return *this;
250  }
251 
252  template< typename Other >
253  inline auto operator=( Other&& rhs )
255  IsRValueReference_v<Other&&>, Element& >
256  {
257  this->value_ = std::move( rhs.value() );
258  return *this;
259  }
260 
261  template< typename Other >
262  inline auto operator=( const Other& v )
264  {
265  this->value_ = v;
266  return *this;
267  }
268 
269  template< typename Other >
270  inline auto operator=( Other&& v )
272  IsRValueReference_v<Other&&>, Element& >
273  {
274  this->value_ = std::move( v );
275  return *this;
276  }
277  //*******************************************************************************************
278 
279  //**Friend declarations**********************************************************************
280  friend class CompressedVector;
281  //*******************************************************************************************
282  };
284  //**********************************************************************************************
285 
286  public:
287  //**Type definitions****************************************************************************
290  using ResultType = This;
292  using ElementType = Type;
293  using ReturnType = const Type&;
296  using ConstReference = const Type&;
297  using Iterator = Element*;
298  using ConstIterator = const Element*;
299  //**********************************************************************************************
300 
301  //**Rebind struct definition********************************************************************
304  template< typename NewType > // Data type of the other vector
305  struct Rebind {
307  };
308  //**********************************************************************************************
309 
310  //**Resize struct definition********************************************************************
313  template< size_t NewN > // Number of elements of the other vector
314  struct Resize {
316  };
317  //**********************************************************************************************
318 
319  //**Compilation flags***************************************************************************
321 
324  static constexpr bool smpAssignable = !IsSMPAssignable_v<Type>;
325  //**********************************************************************************************
326 
327  //**Constructors********************************************************************************
330  explicit inline CompressedVector() noexcept;
331  explicit inline CompressedVector( size_t size ) noexcept;
332  explicit inline CompressedVector( size_t size, size_t nonzeros );
333  explicit inline CompressedVector( initializer_list<Type> list );
334 
335  inline CompressedVector( const CompressedVector& sv );
336  inline CompressedVector( CompressedVector&& sv ) noexcept;
337 
338  template< typename VT > inline CompressedVector( const DenseVector<VT,TF>& dv );
339  template< typename VT > inline CompressedVector( const SparseVector<VT,TF>& sv );
341  //**********************************************************************************************
342 
343  //**Destructor**********************************************************************************
346  inline ~CompressedVector();
348  //**********************************************************************************************
349 
350  //**Data access functions***********************************************************************
353  inline Reference operator[]( size_t index ) noexcept;
354  inline ConstReference operator[]( size_t index ) const noexcept;
355  inline Reference at( size_t index );
356  inline ConstReference at( size_t index ) const;
357  inline Iterator begin () noexcept;
358  inline ConstIterator begin () const noexcept;
359  inline ConstIterator cbegin() const noexcept;
360  inline Iterator end () noexcept;
361  inline ConstIterator end () const noexcept;
362  inline ConstIterator cend () const noexcept;
364  //**********************************************************************************************
365 
366  //**Assignment operators************************************************************************
369  inline CompressedVector& operator=( initializer_list<Type> list );
370  inline CompressedVector& operator=( const CompressedVector& rhs );
371  inline CompressedVector& operator=( CompressedVector&& rhs ) noexcept;
372 
373  template< typename VT > inline CompressedVector& operator= ( const DenseVector<VT,TF>& rhs );
374  template< typename VT > inline CompressedVector& operator= ( const SparseVector<VT,TF>& rhs );
375  template< typename VT > inline CompressedVector& operator+=( const Vector<VT,TF>& rhs );
376  template< typename VT > inline CompressedVector& operator-=( const Vector<VT,TF>& rhs );
377  template< typename VT > inline CompressedVector& operator*=( const DenseVector<VT,TF>& rhs );
378  template< typename VT > inline CompressedVector& operator*=( const SparseVector<VT,TF>& rhs );
379  template< typename VT > inline CompressedVector& operator/=( const DenseVector<VT,TF>& rhs );
380  template< typename VT > inline CompressedVector& operator%=( const Vector<VT,TF>& rhs );
382  //**********************************************************************************************
383 
384  //**Utility functions***************************************************************************
387  inline size_t size() const noexcept;
388  inline size_t capacity() const noexcept;
389  inline size_t nonZeros() const;
390  inline void reset();
391  inline void clear();
392  inline void resize( size_t n, bool preserve=true );
393  void reserve( size_t n );
394  inline void shrinkToFit();
395  inline void swap( CompressedVector& sv ) noexcept;
397  //**********************************************************************************************
398 
399  //**Insertion functions*************************************************************************
402  inline Iterator set ( size_t index, const Type& value );
403  inline Iterator insert( size_t index, const Type& value );
404  inline void append( size_t index, const Type& value, bool check=false );
406  //**********************************************************************************************
407 
408  //**Erase functions*****************************************************************************
411  inline void erase( size_t index );
412  inline Iterator erase( Iterator pos );
413  inline Iterator erase( Iterator first, Iterator last );
414 
415  template< typename Pred, typename = DisableIf_t< IsIntegral_v<Pred> > >
416  inline void erase( Pred predicate );
417 
418  template< typename Pred >
419  inline void erase( Iterator first, Iterator last, Pred predicate );
421  //**********************************************************************************************
422 
423  //**Lookup functions****************************************************************************
426  inline Iterator find ( size_t index );
427  inline ConstIterator find ( size_t index ) const;
428  inline Iterator lowerBound( size_t index );
429  inline ConstIterator lowerBound( size_t index ) const;
430  inline Iterator upperBound( size_t index );
431  inline ConstIterator upperBound( size_t index ) const;
433  //**********************************************************************************************
434 
435  //**Numeric functions***************************************************************************
438  template< typename Other > inline CompressedVector& scale( const Other& scalar );
440  //**********************************************************************************************
441 
442  //**Expression template evaluation functions****************************************************
445  template< typename Other > inline bool canAlias ( const Other* alias ) const noexcept;
446  template< typename Other > inline bool isAliased( const Other* alias ) const noexcept;
447 
448  inline bool canSMPAssign() const noexcept;
449 
450  template< typename VT > inline void assign ( const DenseVector <VT,TF>& rhs );
451  template< typename VT > inline void assign ( const SparseVector<VT,TF>& rhs );
452  template< typename VT > inline void addAssign ( const DenseVector <VT,TF>& rhs );
453  template< typename VT > inline void addAssign ( const SparseVector<VT,TF>& rhs );
454  template< typename VT > inline void subAssign ( const DenseVector <VT,TF>& rhs );
455  template< typename VT > inline void subAssign ( const SparseVector<VT,TF>& rhs );
456  template< typename VT > inline void multAssign( const DenseVector <VT,TF>& rhs );
457  template< typename VT > inline void divAssign ( const DenseVector <VT,TF>& rhs );
459  //**********************************************************************************************
460 
461  private:
462  //**Utility functions***************************************************************************
465  inline size_t extendCapacity() const noexcept;
466  inline Iterator castDown( IteratorBase it ) const noexcept;
467  inline IteratorBase castUp ( Iterator it ) const noexcept;
469  //**********************************************************************************************
470 
471  //**Insertion functions***************************************************************************
474  Iterator insert( Iterator pos, size_t index, const Type& value );
476  //**********************************************************************************************
477 
478  //**Member variables****************************************************************************
481  size_t size_;
482  size_t capacity_;
485 
486  static const Type zero_;
487 
488  //**********************************************************************************************
489 
490  //**Compile time checks*************************************************************************
498  //**********************************************************************************************
499 };
500 //*************************************************************************************************
501 
502 
503 
504 
505 //=================================================================================================
506 //
507 // DEFINITION AND INITIALIZATION OF THE STATIC MEMBER VARIABLES
508 //
509 //=================================================================================================
510 
511 template< typename Type // Data type of the vector
512  , bool TF > // Transpose flag
513 const Type CompressedVector<Type,TF>::zero_{};
514 
515 
516 
517 
518 //=================================================================================================
519 //
520 // CONSTRUCTORS
521 //
522 //=================================================================================================
523 
524 //*************************************************************************************************
527 template< typename Type // Data type of the vector
528  , bool TF > // Transpose flag
530  : size_ ( 0UL ) // The current size/dimension of the compressed vector
531  , capacity_( 0UL ) // The maximum capacity of the compressed vector
532  , begin_ ( nullptr ) // Pointer to the first non-zero element of the compressed vector
533  , end_ ( nullptr ) // Pointer to the last non-zero element of the compressed vector
534 {}
535 //*************************************************************************************************
536 
537 
538 //*************************************************************************************************
543 template< typename Type // Data type of the vector
544  , bool TF > // Transpose flag
546  : size_ ( n ) // The current size/dimension of the compressed vector
547  , capacity_( 0UL ) // The maximum capacity of the compressed vector
548  , begin_ ( nullptr ) // Pointer to the first non-zero element of the compressed vector
549  , end_ ( nullptr ) // Pointer to the last non-zero element of the compressed vector
550 {}
551 //*************************************************************************************************
552 
553 
554 //*************************************************************************************************
560 template< typename Type // Data type of the vector
561  , bool TF > // Transpose flag
562 inline CompressedVector<Type,TF>::CompressedVector( size_t n, size_t nonzeros )
563  : size_ ( n ) // The current size/dimension of the compressed vector
564  , capacity_( nonzeros ) // The maximum capacity of the compressed vector
565  , begin_ ( allocate<Element>( capacity_ ) ) // Pointer to the first non-zero element of the compressed vector
566  , end_ ( begin_ ) // Pointer to the last non-zero element of the compressed vector
567 {}
568 //*************************************************************************************************
569 
570 
571 //*************************************************************************************************
586 template< typename Type // Data type of the vector
587  , bool TF > // Transpose flag
589  : CompressedVector( list.size(), blaze::nonZeros( list ) )
590 {
591  size_t i( 0UL );
592 
593  for( const Type& element : list ) {
594  if( !isDefault<strict>( element ) )
595  append( i, element );
596  ++i;
597  }
598 }
599 //*************************************************************************************************
600 
601 
602 //*************************************************************************************************
610 template< typename Type // Data type of the vector
611  , bool TF > // Transpose flag
613  : CompressedVector( sv.size_, sv.nonZeros() )
614 {
615  end_ = begin_ + capacity_;
616  std::copy( sv.begin_, sv.end_, castUp( begin_ ) );
617 }
618 //*************************************************************************************************
619 
620 
621 //*************************************************************************************************
626 template< typename Type // Data type of the vector
627  , bool TF > // Transpose flag
629  : size_ ( sv.size_ ) // The current size/dimension of the compressed vector
630  , capacity_( sv.capacity_ ) // The maximum capacity of the compressed vector
631  , begin_ ( sv.begin_ ) // Pointer to the first non-zero element of the compressed vector
632  , end_ ( sv.end_ ) // Pointer to the last non-zero element of the compressed vector
633 {
634  sv.size_ = 0UL;
635  sv.capacity_ = 0UL;
636  sv.begin_ = nullptr;
637  sv.end_ = nullptr;
638 }
639 //*************************************************************************************************
640 
641 
642 //*************************************************************************************************
647 template< typename Type // Data type of the vector
648  , bool TF > // Transpose flag
649 template< typename VT > // Type of the foreign dense vector
651  : CompressedVector( (~dv).size() )
652 {
653  using blaze::assign;
654 
655  assign( *this, ~dv );
656 }
657 //*************************************************************************************************
658 
659 
660 //*************************************************************************************************
665 template< typename Type // Data type of the vector
666  , bool TF > // Transpose flag
667 template< typename VT > // Type of the foreign sparse vector
669  : CompressedVector( (~sv).size(), (~sv).nonZeros() )
670 {
671  using blaze::assign;
672 
673  assign( *this, ~sv );
674 }
675 //*************************************************************************************************
676 
677 
678 
679 
680 //=================================================================================================
681 //
682 // DESTRUCTOR
683 //
684 //=================================================================================================
685 
686 //*************************************************************************************************
689 template< typename Type // Data type of the vector
690  , bool TF > // Transpose flag
692 {
693  deallocate( begin_ );
694 }
695 //*************************************************************************************************
696 
697 
698 
699 
700 //=================================================================================================
701 //
702 // DATA ACCESS FUNCTIONS
703 //
704 //=================================================================================================
705 
706 //*************************************************************************************************
717 template< typename Type // Data type of the vector
718  , bool TF > // Transpose flag
720  CompressedVector<Type,TF>::operator[]( size_t index ) noexcept
721 {
722  BLAZE_USER_ASSERT( index < size_, "Invalid compressed vector access index" );
723 
724  return Reference( *this, index );
725 }
726 //*************************************************************************************************
727 
728 
729 //*************************************************************************************************
735 template< typename Type // Data type of the vector
736  , bool TF > // Transpose flag
738  CompressedVector<Type,TF>::operator[]( size_t index ) const noexcept
739 {
740  BLAZE_USER_ASSERT( index < size_, "Invalid compressed vector access index" );
741 
742  const ConstIterator pos( lowerBound( index ) );
743 
744  if( pos == end_ || pos->index_ != index )
745  return zero_;
746  else
747  return pos->value_;
748 }
749 //*************************************************************************************************
750 
751 
752 //*************************************************************************************************
764 template< typename Type // Data type of the vector
765  , bool TF > // Transpose flag
768 {
769  if( index >= size_ ) {
770  BLAZE_THROW_OUT_OF_RANGE( "Invalid compressed vector access index" );
771  }
772  return (*this)[index];
773 }
774 //*************************************************************************************************
775 
776 
777 //*************************************************************************************************
787 template< typename Type // Data type of the vector
788  , bool TF > // Transpose flag
790  CompressedVector<Type,TF>::at( size_t index ) const
791 {
792  if( index >= size_ ) {
793  BLAZE_THROW_OUT_OF_RANGE( "Invalid compressed vector access index" );
794  }
795  return (*this)[index];
796 }
797 //*************************************************************************************************
798 
799 
800 //*************************************************************************************************
805 template< typename Type // Data type of the vector
806  , bool TF > // Transpose flag
808 {
809  return Iterator( begin_ );
810 }
811 //*************************************************************************************************
812 
813 
814 //*************************************************************************************************
819 template< typename Type // Data type of the vector
820  , bool TF > // Transpose flag
823 {
824  return ConstIterator( begin_ );
825 }
826 //*************************************************************************************************
827 
828 
829 //*************************************************************************************************
834 template< typename Type // Data type of the vector
835  , bool TF > // Transpose flag
838 {
839  return ConstIterator( begin_ );
840 }
841 //*************************************************************************************************
842 
843 
844 //*************************************************************************************************
849 template< typename Type // Data type of the vector
850  , bool TF > // Transpose flag
852 {
853  return Iterator( end_ );
854 }
855 //*************************************************************************************************
856 
857 
858 //*************************************************************************************************
863 template< typename Type // Data type of the vector
864  , bool TF > // Transpose flag
867 {
868  return ConstIterator( end_ );
869 }
870 //*************************************************************************************************
871 
872 
873 //*************************************************************************************************
878 template< typename Type // Data type of the vector
879  , bool TF > // Transpose flag
882 {
883  return ConstIterator( end_ );
884 }
885 //*************************************************************************************************
886 
887 
888 
889 
890 //=================================================================================================
891 //
892 // ASSIGNMENT OPERATORS
893 //
894 //=================================================================================================
895 
896 //*************************************************************************************************
912 template< typename Type // Data type of the vector
913  , bool TF > // Transpose flag
916 {
917  using blaze::nonZeros;
918 
919  resize( list.size(), false );
920  reserve( nonZeros( list ) );
921 
922  size_t i( 0UL );
923 
924  for( const Type& element : list ) {
925  if( !isDefault<strict>( element ) )
926  append( i, element );
927  ++i;
928  }
929 
930  return *this;
931 }
932 //*************************************************************************************************
933 
934 
935 //*************************************************************************************************
944 template< typename Type // Data type of the vector
945  , bool TF > // Transpose flag
948 {
949  using std::swap;
950 
951  if( &rhs == this ) return *this;
952 
953  const size_t nonzeros( rhs.nonZeros() );
954 
955  if( nonzeros > capacity_ ) {
956  Iterator newBegin( allocate<Element>( nonzeros ) );
957  end_ = castDown( std::copy( rhs.begin_, rhs.end_, castUp( newBegin ) ) );
958  swap( begin_, newBegin );
959  deallocate( newBegin );
960 
961  size_ = rhs.size_;
962  capacity_ = nonzeros;
963  }
964  else {
965  end_ = castDown( std::copy( rhs.begin_, rhs.end_, castUp( begin_ ) ) );
966  size_ = rhs.size_;
967  }
968 
969  return *this;
970 }
971 //*************************************************************************************************
972 
973 
974 //*************************************************************************************************
980 template< typename Type // Data type of the vector
981  , bool TF > // Transpose flag
984 {
985  deallocate( begin_ );
986 
987  size_ = rhs.size_;
988  capacity_ = rhs.capacity_;
989  begin_ = rhs.begin_;
990  end_ = rhs.end_;
991 
992  rhs.size_ = 0UL;
993  rhs.capacity_ = 0UL;
994  rhs.begin_ = nullptr;
995  rhs.end_ = nullptr;
996 
997  return *this;
998 }
999 //*************************************************************************************************
1000 
1001 
1002 //*************************************************************************************************
1011 template< typename Type // Data type of the vector
1012  , bool TF > // Transpose flag
1013 template< typename VT > // Type of the right-hand side dense vector
1016 {
1017  using blaze::assign;
1018 
1019  if( (~rhs).canAlias( this ) ) {
1020  CompressedVector tmp( ~rhs );
1021  swap( tmp );
1022  }
1023  else {
1024  size_ = (~rhs).size();
1025  end_ = begin_;
1026  assign( *this, ~rhs );
1027  }
1028 
1029  return *this;
1030 }
1031 //*************************************************************************************************
1032 
1033 
1034 //*************************************************************************************************
1043 template< typename Type // Data type of the vector
1044  , bool TF > // Transpose flag
1045 template< typename VT > // Type of the right-hand side sparse vector
1048 {
1049  using blaze::assign;
1050 
1051  if( (~rhs).canAlias( this ) || (~rhs).nonZeros() > capacity_ ) {
1052  CompressedVector tmp( ~rhs );
1053  swap( tmp );
1054  }
1055  else {
1056  size_ = (~rhs).size();
1057  end_ = begin_;
1058  assign( *this, ~rhs );
1059  }
1060 
1061  return *this;
1062 }
1063 //*************************************************************************************************
1064 
1065 
1066 //*************************************************************************************************
1076 template< typename Type // Data type of the vector
1077  , bool TF > // Transpose flag
1078 template< typename VT > // Type of the right-hand side vector
1080 {
1081  using blaze::addAssign;
1082 
1083  if( (~rhs).size() != size_ ) {
1084  BLAZE_THROW_INVALID_ARGUMENT( "Vector sizes do not match" );
1085  }
1086 
1087  addAssign( *this, ~rhs );
1088 
1089  return *this;
1090 }
1091 //*************************************************************************************************
1092 
1093 
1094 //*************************************************************************************************
1104 template< typename Type // Data type of the vector
1105  , bool TF > // Transpose flag
1106 template< typename VT > // Type of the right-hand side vector
1108 {
1109  using blaze::subAssign;
1110 
1111  if( (~rhs).size() != size_ ) {
1112  BLAZE_THROW_INVALID_ARGUMENT( "Vector sizes do not match" );
1113  }
1114 
1115  subAssign( *this, ~rhs );
1116 
1117  return *this;
1118 }
1119 //*************************************************************************************************
1120 
1121 
1122 //*************************************************************************************************
1133 template< typename Type // Data type of the vector
1134  , bool TF > // Transpose flag
1135 template< typename VT > // Type of the right-hand side vector
1138 {
1139  using blaze::multAssign;
1140 
1141  if( (~rhs).size() != size_ ) {
1142  BLAZE_THROW_INVALID_ARGUMENT( "Vector sizes do not match" );
1143  }
1144 
1145  if( (~rhs).canAlias( this ) ) {
1146  CompressedVector tmp( *this * (~rhs) );
1147  swap( tmp );
1148  }
1149  else {
1150  CompositeType_t<VT> tmp( ~rhs );
1151  multAssign( *this, tmp );
1152  }
1153 
1154  return *this;
1155 }
1156 //*************************************************************************************************
1157 
1158 
1159 //*************************************************************************************************
1170 template< typename Type // Data type of the vector
1171  , bool TF > // Transpose flag
1172 template< typename VT > // Type of the right-hand side vector
1175 {
1176  if( (~rhs).size() != size_ ) {
1177  BLAZE_THROW_INVALID_ARGUMENT( "Vector sizes do not match" );
1178  }
1179 
1180  CompressedVector tmp( *this * (~rhs) );
1181  swap( tmp );
1182 
1183  return *this;
1184 }
1185 //*************************************************************************************************
1186 
1187 
1188 //*************************************************************************************************
1198 template< typename Type // Data type of the vector
1199  , bool TF > // Transpose flag
1200 template< typename VT > // Type of the right-hand side vector
1202 {
1203  using blaze::divAssign;
1204 
1205  if( (~rhs).size() != size_ ) {
1206  BLAZE_THROW_INVALID_ARGUMENT( "Vector sizes do not match" );
1207  }
1208 
1209  if( (~rhs).canAlias( this ) ) {
1210  CompressedVector tmp( *this / (~rhs) );
1211  swap( tmp );
1212  }
1213  else {
1214  CompositeType_t<VT> tmp( ~rhs );
1215  divAssign( *this, tmp );
1216  }
1217 
1218  return *this;
1219 }
1220 //*************************************************************************************************
1221 
1222 
1223 //*************************************************************************************************
1234 template< typename Type // Data type of the vector
1235  , bool TF > // Transpose flag
1236 template< typename VT > // Type of the right-hand side vector
1238 {
1239  using blaze::assign;
1240 
1243 
1244  using CrossType = CrossTrait_t< This, ResultType_t<VT> >;
1245 
1249 
1250  if( size_ != 3UL || (~rhs).size() != 3UL ) {
1251  BLAZE_THROW_INVALID_ARGUMENT( "Invalid vector size for cross product" );
1252  }
1253 
1254  const CrossType tmp( *this % (~rhs) );
1255  end_ = begin_;
1256  assign( *this, tmp );
1257 
1258  return *this;
1259 }
1260 //*************************************************************************************************
1261 
1262 
1263 
1264 
1265 //=================================================================================================
1266 //
1267 // UTILITY FUNCTIONS
1268 //
1269 //=================================================================================================
1270 
1271 //*************************************************************************************************
1276 template< typename Type // Data type of the vector
1277  , bool TF > // Transpose flag
1278 inline size_t CompressedVector<Type,TF>::size() const noexcept
1279 {
1280  return size_;
1281 }
1282 //*************************************************************************************************
1283 
1284 
1285 //*************************************************************************************************
1290 template< typename Type // Data type of the vector
1291  , bool TF > // Transpose flag
1292 inline size_t CompressedVector<Type,TF>::capacity() const noexcept
1293 {
1294  return capacity_;
1295 }
1296 //*************************************************************************************************
1297 
1298 
1299 //*************************************************************************************************
1307 template< typename Type // Data type of the vector
1308  , bool TF > // Transpose flag
1310 {
1311  return end_ - begin_;
1312 }
1313 //*************************************************************************************************
1314 
1315 
1316 //*************************************************************************************************
1321 template< typename Type // Data type of the vector
1322  , bool TF > // Transpose flag
1324 {
1325  end_ = begin_;
1326 }
1327 //*************************************************************************************************
1328 
1329 
1330 //*************************************************************************************************
1337 template< typename Type // Data type of the vector
1338  , bool TF > // Transpose flag
1340 {
1341  size_ = 0UL;
1342  end_ = begin_;
1343 }
1344 //*************************************************************************************************
1345 
1346 
1347 //*************************************************************************************************
1361 template< typename Type // Data type of the vector
1362  , bool TF > // Transpose flag
1363 inline void CompressedVector<Type,TF>::resize( size_t n, bool preserve )
1364 {
1365  if( preserve ) {
1366  end_ = lowerBound( n );
1367  }
1368  else {
1369  end_ = begin_;
1370  }
1371 
1372  size_ = n;
1373 }
1374 //*************************************************************************************************
1375 
1376 
1377 //*************************************************************************************************
1386 template< typename Type // Data type of the vector
1387  , bool TF > // Transpose flag
1389 {
1390  using std::swap;
1391 
1392  if( n > capacity_ ) {
1393  const size_t newCapacity( n );
1394 
1395  // Allocating a new data and index array
1396  Iterator newBegin = allocate<Element>( newCapacity );
1397 
1398  // Replacing the old data and index array
1399  end_ = castDown( transfer( begin_, end_, castUp( newBegin ) ) );
1400  swap( newBegin, begin_ );
1401  capacity_ = newCapacity;
1402  deallocate( newBegin );
1403  }
1404 }
1405 //*************************************************************************************************
1406 
1407 
1408 //*************************************************************************************************
1417 template< typename Type // Data type of the vector
1418  , bool TF > // Transpose flag
1420 {
1421  if( nonZeros() < capacity_ ) {
1422  CompressedVector( *this ).swap( *this );
1423  }
1424 }
1425 //*************************************************************************************************
1426 
1427 
1428 //*************************************************************************************************
1434 template< typename Type // Data type of the vector
1435  , bool TF > // Transpose flag
1437 {
1438  using std::swap;
1439 
1440  swap( size_, sv.size_ );
1441  swap( capacity_, sv.capacity_ );
1442  swap( begin_, sv.begin_ );
1443  swap( end_, sv.end_ );
1444 }
1445 //*************************************************************************************************
1446 
1447 
1448 //*************************************************************************************************
1456 template< typename Type // Data type of the vector
1457  , bool TF > // Transpose flag
1458 inline size_t CompressedVector<Type,TF>::extendCapacity() const noexcept
1459 {
1460  using blaze::max;
1461  using blaze::min;
1462 
1463  size_t nonzeros( 2UL*capacity_+1UL );
1464  nonzeros = max( nonzeros, 7UL );
1465  nonzeros = min( nonzeros, size_ );
1466 
1467  BLAZE_INTERNAL_ASSERT( nonzeros > capacity_, "Invalid capacity value" );
1468 
1469  return nonzeros;
1470 }
1471 //*************************************************************************************************
1472 
1473 
1474 //*************************************************************************************************
1482 template< typename Type // Data type of the vector
1483  , bool TF > // Transpose flag
1486 {
1487  return static_cast<Iterator>( it );
1488 }
1489 //*************************************************************************************************
1490 
1491 
1492 //*************************************************************************************************
1500 template< typename Type // Data type of the vector
1501  , bool TF > // Transpose flag
1504 {
1505  return static_cast<IteratorBase>( it );
1506 }
1507 //*************************************************************************************************
1508 
1509 
1510 
1511 
1512 //=================================================================================================
1513 //
1514 // INSERTION FUNCTIONS
1515 //
1516 //=================================================================================================
1517 
1518 //*************************************************************************************************
1529 template< typename Type // Data type of the vector
1530  , bool TF > // Transpose flag
1532  CompressedVector<Type,TF>::set( size_t index, const Type& value )
1533 {
1534  BLAZE_USER_ASSERT( index < size_, "Invalid compressed vector access index" );
1535 
1536  const Iterator pos( lowerBound( index ) );
1537 
1538  if( pos != end_ && pos->index_ == index ) {
1539  pos->value() = value;
1540  return pos;
1541  }
1542  else return insert( pos, index, value );
1543 }
1544 //*************************************************************************************************
1545 
1546 
1547 //*************************************************************************************************
1559 template< typename Type // Data type of the vector
1560  , bool TF > // Transpose flag
1562  CompressedVector<Type,TF>::insert( size_t index, const Type& value )
1563 {
1564  BLAZE_USER_ASSERT( index < size_, "Invalid compressed vector access index" );
1565 
1566  const Iterator pos( lowerBound( index ) );
1567 
1568  if( pos != end_ && pos->index_ == index ) {
1569  BLAZE_THROW_INVALID_ARGUMENT( "Bad access index" );
1570  }
1571 
1572  return insert( pos, index, value );
1573 }
1574 //*************************************************************************************************
1575 
1576 
1577 //*************************************************************************************************
1586 template< typename Type // Data type of the vector
1587  , bool TF > // Transpose flag
1589  CompressedVector<Type,TF>::insert( Iterator pos, size_t index, const Type& value )
1590 {
1591  using std::swap;
1592 
1593  if( nonZeros() != capacity_ ) {
1594  std::move_backward( pos, end_, castUp( end_+1 ) );
1595  pos->value_ = value;
1596  pos->index_ = index;
1597  ++end_;
1598 
1599  return pos;
1600  }
1601  else {
1602  size_t newCapacity( extendCapacity() );
1603 
1604  Iterator newBegin = allocate<Element>( newCapacity );
1605  Iterator tmp = castDown( std::move( begin_, pos, castUp( newBegin ) ) );
1606  tmp->value_ = value;
1607  tmp->index_ = index;
1608  end_ = castDown( std::move( pos, end_, castUp( tmp+1 ) ) );
1609 
1610  swap( newBegin, begin_ );
1611  deallocate( newBegin );
1612  capacity_ = newCapacity;
1613 
1614  return tmp;
1615  }
1616 }
1617 //*************************************************************************************************
1618 
1619 
1620 //*************************************************************************************************
1644 template< typename Type // Data type of the vector
1645  , bool TF > // Transpose flag
1646 inline void CompressedVector<Type,TF>::append( size_t index, const Type& value, bool check )
1647 {
1648  BLAZE_USER_ASSERT( index < size_, "Invalid compressed vector access index" );
1649  BLAZE_USER_ASSERT( nonZeros() < capacity(), "Not enough reserved capacity" );
1650  BLAZE_USER_ASSERT( begin_ == end_ || (end_-1UL)->index_ < index, "Index is not strictly increasing" );
1651 
1652  end_->value_ = value;
1653 
1654  if( !check || !isDefault<strict>( end_->value_ ) ) {
1655  end_->index_ = index;
1656  ++end_;
1657  }
1658 }
1659 //*************************************************************************************************
1660 
1661 
1662 
1663 
1664 //=================================================================================================
1665 //
1666 // ERASE FUNCTIONS
1667 //
1668 //=================================================================================================
1669 
1670 //*************************************************************************************************
1678 template< typename Type // Data type of the vector
1679  , bool TF > // Transpose flag
1680 inline void CompressedVector<Type,TF>::erase( size_t index )
1681 {
1682  BLAZE_USER_ASSERT( index < size_, "Invalid compressed vector access index" );
1683 
1684  const Iterator pos( find( index ) );
1685  if( pos != end_ )
1686  end_ = castDown( std::move( pos+1, end_, castUp( pos ) ) );
1687 }
1688 //*************************************************************************************************
1689 
1690 
1691 //*************************************************************************************************
1699 template< typename Type // Data type of the vector
1700  , bool TF > // Transpose flag
1703 {
1704  BLAZE_USER_ASSERT( pos >= begin_ && pos <= end_, "Invalid compressed vector iterator" );
1705 
1706  if( pos != end_ )
1707  end_ = castDown( std::move( pos+1, end_, castUp( pos ) ) );
1708  return pos;
1709 }
1710 //*************************************************************************************************
1711 
1712 
1713 //*************************************************************************************************
1722 template< typename Type // Data type of the vector
1723  , bool TF > // Transpose flag
1726 {
1727  BLAZE_USER_ASSERT( first <= last, "Invalid iterator range" );
1728  BLAZE_USER_ASSERT( first >= begin_ && first <= end_, "Invalid compressed vector iterator" );
1729  BLAZE_USER_ASSERT( last >= begin_ && last <= end_, "Invalid compressed vector iterator" );
1730 
1731  if( first != last )
1732  end_ = castDown( std::move( last, end_, castUp( first ) ) );
1733  return first;
1734 }
1735 //*************************************************************************************************
1736 
1737 
1738 //*************************************************************************************************
1759 template< typename Type // Data type of the vector
1760  , bool TF > // Transpose flag
1761 template< typename Pred // Type of the unary predicate
1762  , typename > // Type restriction on the unary predicate
1763 inline void CompressedVector<Type,TF>::erase( Pred predicate )
1764 {
1765  end_ = castDown( std::remove_if( castUp( begin_ ), castUp( end_ ),
1766  [predicate=predicate]( const ElementBase& element ) {
1767  return predicate( element.value() );
1768  } ) );
1769 }
1770 //*************************************************************************************************
1771 
1772 
1773 //*************************************************************************************************
1796 template< typename Type // Data type of the vector
1797  , bool TF > // Transpose flag
1798 template< typename Pred > // Type of the unary predicate
1799 inline void CompressedVector<Type,TF>::erase( Iterator first, Iterator last, Pred predicate )
1800 {
1801  BLAZE_USER_ASSERT( first <= last, "Invalid iterator range" );
1802  BLAZE_USER_ASSERT( first >= begin_ && first <= end_, "Invalid compressed vector iterator" );
1803  BLAZE_USER_ASSERT( last >= begin_ && last <= end_, "Invalid compressed vector iterator" );
1804 
1805  const auto pos = std::remove_if( castUp( first ), castUp( last ),
1806  [predicate=predicate]( const ElementBase& element ) {
1807  return predicate( element.value() );
1808  } );
1809 
1810  end_ = castDown( std::move( last, end_, pos ) );
1811 }
1812 //*************************************************************************************************
1813 
1814 
1815 
1816 
1817 //=================================================================================================
1818 //
1819 // LOOKUP FUNCTIONS
1820 //
1821 //=================================================================================================
1822 
1823 //*************************************************************************************************
1836 template< typename Type // Data type of the vector
1837  , bool TF > // Transpose flag
1839 {
1840  return const_cast<Iterator>( const_cast<const This&>( *this ).find( index ) );
1841 }
1842 //*************************************************************************************************
1843 
1844 
1845 //*************************************************************************************************
1858 template< typename Type // Data type of the vector
1859  , bool TF > // Transpose flag
1861 {
1862  const ConstIterator pos( lowerBound( index ) );
1863  if( pos != end_ && pos->index_ == index )
1864  return pos;
1865  else return end_;
1866 }
1867 //*************************************************************************************************
1868 
1869 
1870 //*************************************************************************************************
1882 template< typename Type // Data type of the vector
1883  , bool TF > // Transpose flag
1886 {
1887  return const_cast<Iterator>( const_cast<const This&>( *this ).lowerBound( index ) );
1888 }
1889 //*************************************************************************************************
1890 
1891 
1892 //*************************************************************************************************
1904 template< typename Type // Data type of the vector
1905  , bool TF > // Transpose flag
1908 {
1909  return std::lower_bound( begin_, end_, index,
1910  []( const Element& element, size_t i )
1911  {
1912  return element.index() < i;
1913  } );
1914 }
1915 //*************************************************************************************************
1916 
1917 
1918 //*************************************************************************************************
1930 template< typename Type // Data type of the vector
1931  , bool TF > // Transpose flag
1934 {
1935  return const_cast<Iterator>( const_cast<const This&>( *this ).upperBound( index ) );
1936 }
1937 //*************************************************************************************************
1938 
1939 
1940 //*************************************************************************************************
1952 template< typename Type // Data type of the vector
1953  , bool TF > // Transpose flag
1956 {
1957  return std::upper_bound( begin_, end_, index,
1958  []( size_t i, const Element& element )
1959  {
1960  return i < element.index();
1961  } );
1962 }
1963 //*************************************************************************************************
1964 
1965 
1966 
1967 
1968 //=================================================================================================
1969 //
1970 // NUMERIC FUNCTIONS
1971 //
1972 //=================================================================================================
1973 
1974 //*************************************************************************************************
1991 template< typename Type // Data type of the vector
1992  , bool TF > // Transpose flag
1993 template< typename Other > // Data type of the scalar value
1995 {
1996  for( auto element=begin_; element!=end_; ++element )
1997  element->value_ *= scalar;
1998  return *this;
1999 }
2000 //*************************************************************************************************
2001 
2002 
2003 
2004 
2005 //=================================================================================================
2006 //
2007 // EXPRESSION TEMPLATE EVALUATION FUNCTIONS
2008 //
2009 //=================================================================================================
2010 
2011 //*************************************************************************************************
2021 template< typename Type // Data type of the vector
2022  , bool TF > // Transpose flag
2023 template< typename Other > // Data type of the foreign expression
2024 inline bool CompressedVector<Type,TF>::canAlias( const Other* alias ) const noexcept
2025 {
2026  return static_cast<const void*>( this ) == static_cast<const void*>( alias );
2027 }
2028 //*************************************************************************************************
2029 
2030 
2031 //*************************************************************************************************
2041 template< typename Type // Data type of the vector
2042  , bool TF > // Transpose flag
2043 template< typename Other > // Data type of the foreign expression
2044 inline bool CompressedVector<Type,TF>::isAliased( const Other* alias ) const noexcept
2045 {
2046  return static_cast<const void*>( this ) == static_cast<const void*>( alias );
2047 }
2048 //*************************************************************************************************
2049 
2050 
2051 //*************************************************************************************************
2061 template< typename Type // Data type of the vector
2062  , bool TF > // Transpose flag
2063 inline bool CompressedVector<Type,TF>::canSMPAssign() const noexcept
2064 {
2065  return false;
2066 }
2067 //*************************************************************************************************
2068 
2069 
2070 //*************************************************************************************************
2081 template< typename Type // Data type of the vector
2082  , bool TF > // Transpose flag
2083 template< typename VT > // Type of the right-hand side dense vector
2085 {
2086  BLAZE_INTERNAL_ASSERT( size_ == (~rhs).size(), "Invalid vector sizes" );
2087  BLAZE_INTERNAL_ASSERT( nonZeros() == 0UL, "Invalid non-zero elements detected" );
2088 
2089  size_t nonzeros( 0UL );
2090 
2091  for( size_t i=0UL; i<size_; ++i )
2092  {
2093  if( nonzeros == capacity_ )
2094  reserve( extendCapacity() );
2095 
2096  end_->value_ = (~rhs)[i];
2097 
2098  if( !isDefault<strict>( end_->value_ ) ) {
2099  end_->index_ = i;
2100  ++end_;
2101  ++nonzeros;
2102  }
2103  }
2104 }
2105 //*************************************************************************************************
2106 
2107 
2108 //*************************************************************************************************
2119 template< typename Type // Data type of the vector
2120  , bool TF > // Transpose flag
2121 template< typename VT > // Type of the right-hand side sparse vector
2123 {
2124  BLAZE_INTERNAL_ASSERT( size_ == (~rhs).size(), "Invalid vector sizes" );
2125  BLAZE_INTERNAL_ASSERT( nonZeros() == 0UL, "Invalid non-zero elements detected" );
2126 
2127  // Using the following formulation instead of a std::copy function call of the form
2128  //
2129  // end_ = std::copy( (~rhs).begin(), (~rhs).end(), begin_ );
2130  //
2131  // results in much less requirements on the ConstIterator type provided from the right-hand
2132  // sparse vector type
2133  for( auto element=(~rhs).begin(); element!=(~rhs).end(); ++element )
2134  append( element->index(), element->value() );
2135 }
2136 //*************************************************************************************************
2137 
2138 
2139 //*************************************************************************************************
2150 template< typename Type // Data type of the vector
2151  , bool TF > // Transpose flag
2152 template< typename VT > // Type of the right-hand side dense vector
2154 {
2155  using AddType = AddTrait_t< This, ResultType_t<VT> >;
2156 
2160 
2161  BLAZE_INTERNAL_ASSERT( size_ == (~rhs).size(), "Invalid vector sizes" );
2162 
2163  const AddType tmp( serial( *this + (~rhs) ) );
2164  reset();
2165  assign( tmp );
2166 }
2167 //*************************************************************************************************
2168 
2169 
2170 //*************************************************************************************************
2181 template< typename Type // Data type of the vector
2182  , bool TF > // Transpose flag
2183 template< typename VT > // Type of the right-hand side sparse vector
2185 {
2186  BLAZE_INTERNAL_ASSERT( size_ == (~rhs).size(), "Invalid vector sizes" );
2187 
2188  CompressedVector tmp( serial( *this + (~rhs) ) );
2189  swap( tmp );
2190 }
2191 //*************************************************************************************************
2192 
2193 
2194 //*************************************************************************************************
2205 template< typename Type // Data type of the vector
2206  , bool TF > // Transpose flag
2207 template< typename VT > // Type of the right-hand side dense vector
2209 {
2210  using SubType = SubTrait_t< This, ResultType_t<VT> >;
2211 
2215 
2216  BLAZE_INTERNAL_ASSERT( size_ == (~rhs).size(), "Invalid vector sizes" );
2217 
2218  const SubType tmp( serial( *this - (~rhs) ) );
2219  reset();
2220  assign( tmp );
2221 }
2222 //*************************************************************************************************
2223 
2224 
2225 //*************************************************************************************************
2236 template< typename Type // Data type of the vector
2237  , bool TF > // Transpose flag
2238 template< typename VT > // Type of the right-hand side sparse vector
2240 {
2241  BLAZE_INTERNAL_ASSERT( size_ == (~rhs).size(), "Invalid vector sizes" );
2242 
2243  CompressedVector tmp( serial( *this - (~rhs) ) );
2244  swap( tmp );
2245 }
2246 //*************************************************************************************************
2247 
2248 
2249 //*************************************************************************************************
2260 template< typename Type // Data type of the vector
2261  , bool TF > // Transpose flag
2262 template< typename VT > // Type of the right-hand side dense vector
2264 {
2265  BLAZE_INTERNAL_ASSERT( size_ == (~rhs).size(), "Invalid vector sizes" );
2266 
2268 
2269  for( auto element=begin_; element!=end_; ++element ) {
2270  element->value_ *= (~rhs)[element->index_];
2271  }
2272 }
2273 //*************************************************************************************************
2274 
2275 
2276 //*************************************************************************************************
2287 template< typename Type // Data type of the vector
2288  , bool TF > // Transpose flag
2289 template< typename VT > // Type of the right-hand side dense vector
2291 {
2292  BLAZE_INTERNAL_ASSERT( size_ == (~rhs).size(), "Invalid vector sizes" );
2293 
2295 
2296  for( auto element=begin_; element!=end_; ++element ) {
2297  element->value_ /= (~rhs)[element->index_];
2298  }
2299 }
2300 //*************************************************************************************************
2301 
2302 
2303 
2304 
2305 //=================================================================================================
2306 //
2307 // COMPRESSEDVECTOR OPERATORS
2308 //
2309 //=================================================================================================
2310 
2311 //*************************************************************************************************
2314 template< typename Type, bool TF >
2315 void reset( CompressedVector<Type,TF>& v );
2316 
2317 template< typename Type, bool TF >
2318 void clear( CompressedVector<Type,TF>& v );
2319 
2320 template< bool RF, typename Type, bool TF >
2321 bool isDefault( const CompressedVector<Type,TF>& v );
2322 
2323 template< typename Type, bool TF >
2324 bool isIntact( const CompressedVector<Type,TF>& v ) noexcept;
2325 
2326 template< typename Type, bool TF >
2329 //*************************************************************************************************
2330 
2331 
2332 //*************************************************************************************************
2339 template< typename Type // Data type of the vector
2340  , bool TF > // Transpose flag
2342 {
2343  v.reset();
2344 }
2345 //*************************************************************************************************
2346 
2347 
2348 //*************************************************************************************************
2355 template< typename Type // Data type of the vector
2356  , bool TF > // Transpose flag
2358 {
2359  v.clear();
2360 }
2361 //*************************************************************************************************
2362 
2363 
2364 //*************************************************************************************************
2388 template< bool RF // Relaxation flag
2389  , typename Type // Data type of the vector
2390  , bool TF > // Transpose flag
2391 inline bool isDefault( const CompressedVector<Type,TF>& v )
2392 {
2393  return ( v.size() == 0UL );
2394 }
2395 //*************************************************************************************************
2396 
2397 
2398 //*************************************************************************************************
2416 template< typename Type // Data type of the vector
2417  , bool TF > // Transpose flag
2418 inline bool isIntact( const CompressedVector<Type,TF>& v ) noexcept
2419 {
2420  return ( v.nonZeros() <= v.capacity() );
2421 }
2422 //*************************************************************************************************
2423 
2424 
2425 //*************************************************************************************************
2433 template< typename Type // Data type of the vector
2434  , bool TF > // Transpose flag
2436 {
2437  a.swap( b );
2438 }
2439 //*************************************************************************************************
2440 
2441 
2442 
2443 
2444 //=================================================================================================
2445 //
2446 // ISRESIZABLE SPECIALIZATIONS
2447 //
2448 //=================================================================================================
2449 
2450 //*************************************************************************************************
2452 template< typename T, bool TF >
2453 struct IsResizable< CompressedVector<T,TF> >
2454  : public TrueType
2455 {};
2457 //*************************************************************************************************
2458 
2459 
2460 
2461 
2462 //=================================================================================================
2463 //
2464 // ISSHRINKABLE SPECIALIZATIONS
2465 //
2466 //=================================================================================================
2467 
2468 //*************************************************************************************************
2470 template< typename T, bool TF >
2471 struct IsShrinkable< CompressedVector<T,TF> >
2472  : public TrueType
2473 {};
2475 //*************************************************************************************************
2476 
2477 
2478 
2479 
2480 //=================================================================================================
2481 //
2482 // ADDTRAIT SPECIALIZATIONS
2483 //
2484 //=================================================================================================
2485 
2486 //*************************************************************************************************
2488 template< typename T1, typename T2 >
2489 struct AddTraitEval2< T1, T2
2490  , EnableIf_t< IsSparseVector_v<T1> && IsSparseVector_v<T2> > >
2491 {
2492  using ET1 = ElementType_t<T1>;
2493  using ET2 = ElementType_t<T2>;
2494 
2495  using Type = CompressedVector< AddTrait_t<ET1,ET2>, TransposeFlag_v<T1> >;
2496 };
2498 //*************************************************************************************************
2499 
2500 
2501 
2502 
2503 //=================================================================================================
2504 //
2505 // SUBTRAIT SPECIALIZATIONS
2506 //
2507 //=================================================================================================
2508 
2509 //*************************************************************************************************
2511 template< typename T1, typename T2 >
2512 struct SubTraitEval2< T1, T2
2513  , EnableIf_t< IsSparseVector_v<T1> && IsSparseVector_v<T2> > >
2514 {
2515  using ET1 = ElementType_t<T1>;
2516  using ET2 = ElementType_t<T2>;
2517 
2518  using Type = CompressedVector< SubTrait_t<ET1,ET2>, TransposeFlag_v<T1> >;
2519 };
2521 //*************************************************************************************************
2522 
2523 
2524 
2525 
2526 //=================================================================================================
2527 //
2528 // MULTTRAIT SPECIALIZATIONS
2529 //
2530 //=================================================================================================
2531 
2532 //*************************************************************************************************
2534 template< typename T1, typename T2 >
2535 struct MultTraitEval2< T1, T2
2536  , EnableIf_t< IsSparseVector_v<T1> && IsNumeric_v<T2> > >
2537 {
2538  using ET1 = ElementType_t<T1>;
2539 
2540  using Type = CompressedVector< MultTrait_t<ET1,T2>, TransposeFlag_v<T1> >;
2541 };
2542 
2543 template< typename T1, typename T2 >
2544 struct MultTraitEval2< T1, T2
2545  , EnableIf_t< IsNumeric_v<T1> && IsSparseVector_v<T2> > >
2546 {
2547  using ET2 = ElementType_t<T2>;
2548 
2549  using Type = CompressedVector< MultTrait_t<T1,ET2>, TransposeFlag_v<T2> >;
2550 };
2551 
2552 template< typename T1, typename T2 >
2553 struct MultTraitEval2< T1, T2
2554  , EnableIf_t< ( ( IsRowVector_v<T1> && IsRowVector_v<T2> ) ||
2555  ( IsColumnVector_v<T1> && IsColumnVector_v<T2> ) ) &&
2556  ( IsSparseVector_v<T1> || IsSparseVector_v<T2> ) > >
2557 {
2558  using ET1 = ElementType_t<T1>;
2559  using ET2 = ElementType_t<T2>;
2560 
2561  using Type = CompressedVector< MultTrait_t<ET1,ET2>, TransposeFlag_v<T1> >;
2562 };
2563 
2564 template< typename T1, typename T2 >
2565 struct MultTraitEval2< T1, T2
2566  , EnableIf_t< IsSparseMatrix_v<T1> &&
2567  IsSparseVector_v<T2> &&
2568  IsColumnVector_v<T2> > >
2569 {
2570  using ET1 = ElementType_t<T1>;
2571  using ET2 = ElementType_t<T2>;
2572 
2573  using Type = CompressedVector< MultTrait_t<ET1,ET2>, false >;
2574 };
2575 
2576 template< typename T1, typename T2 >
2577 struct MultTraitEval2< T1, T2
2578  , EnableIf_t< IsSparseVector_v<T1> &&
2579  IsRowVector_v<T1> &&
2580  IsSparseMatrix_v<T2> > >
2581 {
2582  using ET1 = ElementType_t<T1>;
2583  using ET2 = ElementType_t<T2>;
2584 
2585  using Type = CompressedVector< MultTrait_t<ET1,ET2>, true >;
2586 };
2588 //*************************************************************************************************
2589 
2590 
2591 
2592 
2593 //=================================================================================================
2594 //
2595 // DIVTRAIT SPECIALIZATIONS
2596 //
2597 //=================================================================================================
2598 
2599 //*************************************************************************************************
2601 template< typename T1, typename T2 >
2602 struct DivTraitEval2< T1, T2
2603  , EnableIf_t< IsSparseVector_v<T1> && IsNumeric_v<T2> > >
2604 {
2605  using ET1 = ElementType_t<T1>;
2606 
2607  using Type = CompressedVector< DivTrait_t<ET1,T2>, TransposeFlag_v<T1> >;
2608 };
2609 
2610 template< typename T1, typename T2 >
2611 struct DivTraitEval2< T1, T2
2612  , EnableIf_t< IsSparseVector_v<T1> && IsDenseVector_v<T2> > >
2613 {
2614  using ET1 = ElementType_t<T1>;
2615  using ET2 = ElementType_t<T2>;
2616 
2617  using Type = CompressedVector< DivTrait_t<ET1,ET2>, TransposeFlag_v<T1> >;
2618 };
2620 //*************************************************************************************************
2621 
2622 
2623 
2624 
2625 //=================================================================================================
2626 //
2627 // MAPTRAIT SPECIALIZATIONS
2628 //
2629 //=================================================================================================
2630 
2631 //*************************************************************************************************
2633 template< typename T, typename OP >
2634 struct UnaryMapTraitEval2< T, OP
2635  , EnableIf_t< IsSparseVector_v<T> > >
2636 {
2637  using ET = ElementType_t<T>;
2638 
2639  using Type = CompressedVector< MapTrait_t<ET,OP>, TransposeFlag_v<T> >;
2640 };
2642 //*************************************************************************************************
2643 
2644 
2645 
2646 
2647 //=================================================================================================
2648 //
2649 // HIGHTYPE SPECIALIZATIONS
2650 //
2651 //=================================================================================================
2652 
2653 //*************************************************************************************************
2655 template< typename T1, bool TF, typename T2 >
2656 struct HighType< CompressedVector<T1,TF>, CompressedVector<T2,TF> >
2657 {
2658  using Type = CompressedVector< typename HighType<T1,T2>::Type, TF >;
2659 };
2661 //*************************************************************************************************
2662 
2663 
2664 
2665 
2666 //=================================================================================================
2667 //
2668 // MATHTRAIT SPECIALIZATIONS
2669 //
2670 //=================================================================================================
2671 
2672 //*************************************************************************************************
2674 template< typename T1, bool TF, typename T2 >
2675 struct LowType< CompressedVector<T1,TF>, CompressedVector<T2,TF> >
2676 {
2677  using Type = CompressedVector< typename LowType<T1,T2>::Type, TF >;
2678 };
2680 //*************************************************************************************************
2681 
2682 
2683 
2684 
2685 //=================================================================================================
2686 //
2687 // SUBVECTORTRAIT SPECIALIZATIONS
2688 //
2689 //=================================================================================================
2690 
2691 //*************************************************************************************************
2693 template< typename VT, size_t I, size_t N >
2694 struct SubvectorTraitEval2< VT, I, N
2695  , EnableIf_t< IsSparseVector_v<VT> > >
2696 {
2697  using Type = CompressedVector< RemoveConst_t< ElementType_t<VT> >, TransposeFlag_v<VT> >;
2698 };
2700 //*************************************************************************************************
2701 
2702 
2703 
2704 
2705 //=================================================================================================
2706 //
2707 // ELEMENTSTRAIT SPECIALIZATIONS
2708 //
2709 //=================================================================================================
2710 
2711 //*************************************************************************************************
2713 template< typename VT, size_t N >
2714 struct ElementsTraitEval2< VT, N
2715  , EnableIf_t< IsSparseVector_v<VT> > >
2716 {
2717  using Type = CompressedVector< RemoveConst_t< ElementType_t<VT> >, TransposeFlag_v<VT> >;
2718 };
2720 //*************************************************************************************************
2721 
2722 
2723 
2724 
2725 //=================================================================================================
2726 //
2727 // ROWTRAIT SPECIALIZATIONS
2728 //
2729 //=================================================================================================
2730 
2731 //*************************************************************************************************
2733 template< typename MT, size_t I >
2734 struct RowTraitEval2< MT, I
2735  , EnableIf_t< IsSparseMatrix_v<MT> > >
2736 {
2737  using Type = CompressedVector< RemoveConst_t< ElementType_t<MT> >, true >;
2738 };
2740 //*************************************************************************************************
2741 
2742 
2743 
2744 
2745 //=================================================================================================
2746 //
2747 // COLUMNTRAIT SPECIALIZATIONS
2748 //
2749 //=================================================================================================
2750 
2751 //*************************************************************************************************
2753 template< typename MT, size_t I >
2754 struct ColumnTraitEval2< MT, I
2755  , EnableIf_t< IsSparseMatrix_v<MT> > >
2756 {
2757  using Type = CompressedVector< RemoveConst_t< ElementType_t<MT> >, false >;
2758 };
2760 //*************************************************************************************************
2761 
2762 
2763 
2764 
2765 //=================================================================================================
2766 //
2767 // BANDTRAIT SPECIALIZATIONS
2768 //
2769 //=================================================================================================
2770 
2771 //*************************************************************************************************
2773 template< typename MT, ptrdiff_t I >
2774 struct BandTraitEval2< MT, I
2775  , EnableIf_t< IsSparseMatrix_v<MT> > >
2776 {
2777  using Type = CompressedVector< RemoveConst_t< ElementType_t<MT> >, defaultTransposeFlag >;
2778 };
2780 //*************************************************************************************************
2781 
2782 } // namespace blaze
2783 
2784 #endif
#define BLAZE_CONSTRAINT_MUST_NOT_BE_CONST(T)
Constraint on the data type.In case the given data type is a const-qualified type, a compilation error is created.
Definition: Const.h:79
#define BLAZE_THROW_INVALID_ARGUMENT(MESSAGE)
Macro for the emission of a std::invalid_argument exception.This macro encapsulates the default way o...
Definition: Exception.h:235
Header file for auxiliary alias declarations.
Headerfile for the generic min algorithm.
#define BLAZE_USER_ASSERT(expr, msg)
Run time assertion macro for user checks.In case of an invalid run time expression, the program execution is terminated. The BLAZE_USER_ASSERT macro can be disabled by setting the BLAZE_USER_ASSERT flag to zero or by defining NDEBUG during the compilation.
Definition: Assert.h:117
typename SubTrait< T1, T2 >::Type SubTrait_t
Auxiliary alias declaration for the SubTrait class template.The SubTrait_t alias declaration provides...
Definition: SubTrait.h:238
constexpr bool IsSparseMatrix_v
Auxiliary variable template for the IsSparseMatrix type trait.The IsSparseMatrix_v variable template ...
Definition: IsSparseMatrix.h:139
Header file for the subtraction trait.
size_t capacity(const Matrix< MT, SO > &matrix) noexcept
Returns the maximum capacity of the matrix.
Definition: Matrix.h:546
Header file for basic type definitions.
Header file for the SparseVector base class.
Header file for the row trait.
void assign(const DenseVector< VT, TF > &rhs)
Default implementation of the assignment of a dense vector.
Definition: CompressedVector.h:2084
Iterator upperBound(size_t index)
Returns an iterator to the first index greater then the given index.
Definition: CompressedVector.h:1933
typename T::ResultType ResultType_t
Alias declaration for nested ResultType type definitions.The ResultType_t alias declaration provides ...
Definition: Aliases.h:390
Resize mechanism to obtain a CompressedVector with a different fixed number of elements.
Definition: CompressedVector.h:314
Header file for the IsSparseMatrix type trait.
Header file for the serial shim.
MT::Iterator begin(Matrix< MT, SO > &matrix, size_t i)
Returns an iterator to the first element of row/column i.
Definition: Matrix.h:372
Access proxy for sparse, N-dimensional vectors.The VectorAccessProxy provides safe access to the elem...
Definition: VectorAccessProxy.h:101
void reset(const DiagonalProxy< MT > &proxy)
Resetting the represented element to the default initial values.
Definition: DiagonalProxy.h:591
Header file for the IsRowVector type trait.
Header file for the IsIntegral type trait.
IteratorBase castUp(Iterator it) const noexcept
Performs an up-cast of the given iterator.
Definition: CompressedVector.h:1503
Header file for the DenseVector base class.
void addAssign(const DenseVector< VT, TF > &rhs)
Default implementation of the addition assignment of a dense vector.
Definition: CompressedVector.h:2153
#define BLAZE_CONSTRAINT_MUST_NOT_BE_VOLATILE(T)
Constraint on the data type.In case the given data type is a volatile-qualified type, a compilation error is created.
Definition: Volatile.h:79
void clear()
Clearing the compressed vector.
Definition: CompressedVector.h:1339
size_t nonZeros(const Matrix< MT, SO > &matrix)
Returns the total number of non-zero elements in the matrix.
Definition: Matrix.h:584
BoolConstant< true > TrueType
Type traits base class.The TrueType class is used as base class for type traits and value traits that...
Definition: TrueType.h:61
Element * Iterator
Iterator over non-constant elements.
Definition: CompressedMatrix.h:3085
Header file for memory allocation and deallocation functionality.
Header file for the extended initializer_list functionality.
size_t size_
The current size/dimension of the compressed vector.
Definition: CompressedVector.h:481
Header file for the elements trait.
Reference operator[](size_t index) noexcept
Subscript operator for the direct access to the compressed vector elements.
Definition: CompressedVector.h:720
Header file for the band trait.
Constraint on the data type.
size_t capacity_
The current capacity of the pointer array.
Definition: CompressedMatrix.h:3291
typename EnableIf< Condition, T >::Type EnableIf_t
Auxiliary type for the EnableIf class template.The EnableIf_t alias declaration provides a convenient...
Definition: EnableIf.h:138
Type ElementType
Type of the compressed vector elements.
Definition: CompressedVector.h:292
Headerfile for the generic max algorithm.
Header file for the ValueIndexPair class.
Rebind mechanism to obtain a CompressedVector with different data/element type.
Definition: CompressedVector.h:305
CompressedVector() noexcept
The default constructor for CompressedVector.
Definition: CompressedVector.h:529
Header file for the DisableIf class template.
Iterator set(size_t index, const Type &value)
Setting an element of the compressed vector.
Definition: CompressedVector.h:1532
Header file for the LowType type trait.
void subAssign(const DenseVector< VT, TF > &rhs)
Default implementation of the subtraction assignment of a dense vector.
Definition: CompressedVector.h:2208
void append(size_t index, const Type &value, bool check=false)
Appending an element to the compressed vector.
Definition: CompressedVector.h:1646
Header file for the multiplication trait.
MatrixAccessProxy< This > Reference
Reference to a non-constant matrix value.
Definition: CompressedMatrix.h:3083
Namespace of the Blaze C++ math library.
Definition: Blaze.h:58
void swap(CompressedMatrix< Type, SO > &a, CompressedMatrix< Type, SO > &b) noexcept
Swapping the contents of two compressed matrices.
Definition: CompressedMatrix.h:5907
Header file for the IsFloatingPoint type trait.
Header file for the IsShrinkable type trait.
Header file for all forward declarations of the math module.
Iterator insert(size_t index, const Type &value)
Inserting an element into the compressed vector.
Definition: CompressedVector.h:1562
#define BLAZE_CONSTRAINT_MUST_NOT_BE_POINTER_TYPE(T)
Constraint on the data type.In case the given data type T is not a pointer type, a compilation error ...
Definition: Pointer.h:79
Header file for the IsSMPAssignable type trait.
decltype(auto) min(const DenseMatrix< MT1, SO1 > &lhs, const DenseMatrix< MT2, SO2 > &rhs)
Computes the componentwise minimum of the dense matrices lhs and rhs.
Definition: DMatDMatMapExpr.h:1147
Iterator * end_
Pointers one past the last non-zero element of each column.
Definition: CompressedMatrix.h:3293
Iterator begin_
Pointer to the first non-zero element of the compressed vector.
Definition: CompressedVector.h:483
#define BLAZE_THROW_OUT_OF_RANGE(MESSAGE)
Macro for the emission of a std::out_of_range exception.This macro encapsulates the default way of Bl...
Definition: Exception.h:331
size_t size() const noexcept
Returns the current size/dimension of the compressed vector.
Definition: CompressedVector.h:1278
const Type & ReturnType
Return type for expression template evaluations.
Definition: CompressedVector.h:293
#define BLAZE_CONSTRAINT_MUST_HAVE_SAME_SIZE(T1, T2)
Constraint on the size of two data types.In case the types T1 and T2 don&#39;t have the same size...
Definition: SameSize.h:60
const Element * ConstIterator
Iterator over constant elements.
Definition: CompressedMatrix.h:3086
void shrinkToFit()
Requesting the removal of unused capacity.
Definition: CompressedVector.h:1419
Header file for the subvector trait.
constexpr bool IsNumeric_v
Auxiliary variable template for the IsNumeric type trait.The IsNumeric_v variable template provides a...
Definition: IsNumeric.h:143
size_t nonZeros() const
Returns the number of non-zero elements in the compressed vector.
Definition: CompressedVector.h:1309
Iterator castDown(IteratorBase it) const noexcept
Performs a down-cast of the given iterator.
Definition: CompressedVector.h:1485
Constraint on the data type.
Base class for N-dimensional dense vectors.The DenseVector class is a base class for all arbitrarily ...
Definition: DenseVector.h:76
typename AddTrait< T1, T2 >::Type AddTrait_t
Auxiliary alias declaration for the AddTrait class template.The AddTrait_t alias declaration provides...
Definition: AddTrait.h:238
Constraint on the data type.
constexpr bool IsSparseVector_v
Auxiliary variable template for the IsSparseVector type trait.The IsSparseVector_v variable template ...
Definition: IsSparseVector.h:139
#define BLAZE_CONSTRAINT_MUST_BE_REFERENCE_TYPE(T)
Constraint on the data type.In case the given data type T is not a reference type, a compilation error is created.
Definition: Reference.h:60
Header file for the TransposeFlag type trait.
Constraint on the data type.
Header file for the exception macros of the math module.
decltype(auto) max(const DenseMatrix< MT1, SO1 > &lhs, const DenseMatrix< MT2, SO2 > &rhs)
Computes the componentwise maximum of the dense matrices lhs and rhs.
Definition: DMatDMatMapExpr.h:1179
void resize(Matrix< MT, SO > &matrix, size_t rows, size_t columns, bool preserve=true)
Changing the size of the matrix.
Definition: Matrix.h:738
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:438
Constraint on the data type.
Header file for the EnableIf class template.
void clear(const DiagonalProxy< MT > &proxy)
Clearing the represented element.
Definition: DiagonalProxy.h:611
size_t capacity_
The maximum capacity of the compressed vector.
Definition: CompressedVector.h:482
void erase(size_t index)
Erasing an element from the compressed vector.
Definition: CompressedVector.h:1680
typename CrossTrait< T1, T2 >::Type CrossTrait_t
Auxiliary alias declaration for the CrossTrait class template.The CrossTrait_t alias declaration prov...
Definition: CrossTrait.h:165
Iterator lowerBound(size_t index)
Returns an iterator to the first index not less then the given index.
Definition: CompressedVector.h:1885
Iterator find(size_t index)
Searches for a specific vector element.
Definition: CompressedVector.h:1838
Header file for the IsNumeric type trait.
void reset()
Reset to the default initial values.
Definition: CompressedVector.h:1323
Header file for the RemoveConst type trait.
void resize(size_t n, bool preserve=true)
Changing the size of the compressed vector.
Definition: CompressedVector.h:1363
Header file for the IsSparseVector type trait.
const Element * ConstIterator
Iterator over constant elements.
Definition: CompressedVector.h:298
Header file for run time assertion macros.
Header file for the relaxation flag types.
CompressedVector & operator=(initializer_list< Type > list)
List assignment to all vector elements.
Definition: CompressedVector.h:915
typename T::CompositeType CompositeType_t
Alias declaration for nested CompositeType type definitions.The CompositeType_t alias declaration pro...
Definition: Aliases.h:90
Header file for the addition trait.
Header file for the cross product trait.
Header file for the division trait.
Constraint on the data type.
size_t extendCapacity() const noexcept
Calculating a new vector capacity.
Definition: CompressedVector.h:1458
Reference at(size_t index)
Checked access to the compressed vector elements.
Definition: CompressedVector.h:767
Headerfile for the generic transfer algorithm.
#define BLAZE_CONSTRAINT_MUST_NOT_BE_REFERENCE_TYPE(T)
Constraint on the data type.In case the given data type T is not a reference type, a compilation error is created.
Definition: Reference.h:79
static constexpr bool smpAssignable
Compilation flag for SMP assignments.
Definition: CompressedVector.h:324
Header file for the column trait.
Header file for the VectorAccessProxy class.
Header file for the isDefault shim.
void swap(DiagonalMatrix< MT, SO, DF > &a, DiagonalMatrix< MT, SO, DF > &b) noexcept
Swapping the contents of two matrices.
Definition: DiagonalMatrix.h:281
constexpr size_t size(const Matrix< MT, SO > &matrix) noexcept
Returns the total number of elements of the matrix.
Definition: Matrix.h:530
constexpr Reference value() noexcept
Access to the current value of the value-index-pair.
Definition: ValueIndexPair.h:370
Constraint on the data type.
decltype(auto) serial(const DenseMatrix< MT, SO > &dm)
Forces the serial evaluation of the given dense matrix expression dm.
Definition: DMatSerialExpr.h:808
Iterator * begin_
Pointers to the first non-zero element of each column.
Definition: CompressedMatrix.h:3292
Element * Iterator
Iterator over non-constant elements.
Definition: CompressedVector.h:297
#define BLAZE_CONSTRAINT_MUST_NOT_REQUIRE_EVALUATION(T)
Constraint on the data type.In case the given data type T requires an intermediate evaluation within ...
Definition: RequiresEvaluation.h:81
void multAssign(const DenseVector< VT, TF > &rhs)
Default implementation of the multiplication assignment of a dense vector.
Definition: CompressedVector.h:2263
void divAssign(const DenseVector< VT, TF > &rhs)
Default implementation of the division assignment of a dense vector.
Definition: CompressedVector.h:2290
bool isAliased(const Other *alias) const noexcept
Returns whether the vector is aliased with the given address alias.
Definition: CompressedVector.h:2044
Header file for the IsDenseVector type trait.
const Type & ConstReference
Reference to a constant vector value.
Definition: CompressedVector.h:296
void swap(CompressedVector &sv) noexcept
Swapping the contents of two compressed vectors.
Definition: CompressedVector.h:1436
ConstIterator cbegin() const noexcept
Returns an iterator to the first non-zero element of the compressed vector.
Definition: CompressedVector.h:837
#define BLAZE_CONSTRAINT_MUST_BE_DENSE_VECTOR_TYPE(T)
Constraint on the data type.In case the given data type T is not a dense, N-dimensional vector type...
Definition: DenseVector.h:61
Iterator end() noexcept
Returns an iterator just past the last non-zero element of the compressed vector. ...
Definition: CompressedVector.h:851
Index-value-pair for sparse vectors and matrices.The ValueIndexPair class represents a single index-v...
Definition: ValueIndexPair.h:73
Constraint on the size of two data types.
constexpr bool IsRowVector_v
Auxiliary variable template for the IsRowVector type trait.The IsRowVector_v variable template provid...
Definition: IsRowVector.h:143
Base class for N-dimensional vectors.The Vector class is a base class for all arbitrarily sized (N-di...
Definition: Forward.h:186
EnableIf_t< IsBuiltin_v< T > > deallocate(T *address) noexcept
Deallocation of memory for built-in data types.
Definition: Memory.h:224
Header file for the default transpose flag for all vectors of the Blaze library.
Initializer list type of the Blaze library.
~CompressedVector()
The destructor for CompressedVector.
Definition: CompressedVector.h:691
static const Type zero_
Neutral element for accesses to zero elements.
Definition: CompressedMatrix.h:3295
size_t capacity() const noexcept
Returns the maximum capacity of the compressed vector.
Definition: CompressedVector.h:1292
Base class for sparse vectors.The SparseVector class is a base class for all arbitrarily sized (N-dim...
Definition: Forward.h:138
Iterator end_
Pointer one past the last non-zero element of the compressed vector.
Definition: CompressedVector.h:484
CompressedVector< Type, TF > This
Type of this CompressedVector instance.
Definition: CompressedVector.h:288
Header file for the map trait.
bool isIntact(const DiagonalMatrix< MT, SO, DF > &m)
Returns whether the invariants of the given diagonal matrix are intact.
Definition: DiagonalMatrix.h:263
ConstIterator cend() const noexcept
Returns an iterator just past the last non-zero element of the compressed vector. ...
Definition: CompressedVector.h:881
bool isDefault(const DiagonalProxy< MT > &proxy)
Returns whether the represented element is in default state.
Definition: DiagonalProxy.h:631
constexpr bool IsIntegral_v
Auxiliary variable template for the IsIntegral type trait.The IsIntegral_v variable template provides...
Definition: IsIntegral.h:95
typename DisableIf< Condition, T >::Type DisableIf_t
Auxiliary type for the DisableIf class template.The DisableIf_t alias declaration provides a convenie...
Definition: DisableIf.h:138
Header file for the IsColumnVector type trait.
OutputIterator transfer(InputIterator first, InputIterator last, OutputIterator dest)
Transfers the elements from the given source range to the destination range.
Definition: Transfer.h:70
Iterator begin() noexcept
Returns an iterator to the first non-zero element of the compressed vector.
Definition: CompressedVector.h:807
static const Type zero_
Neutral element for accesses to zero elements.
Definition: CompressedVector.h:486
bool canAlias(const Other *alias) const noexcept
Returns whether the vector can alias with the given address alias.
Definition: CompressedVector.h:2024
Header file for the IsResizable type trait.
bool canSMPAssign() const noexcept
Returns whether the vector can be used in SMP assignments.
Definition: CompressedVector.h:2063
EnableIf_t< IsBuiltin_v< T >, T *> allocate(size_t size)
Aligned array allocation for built-in data types.
Definition: Memory.h:156
#define BLAZE_CONSTRAINT_MUST_BE_VECTOR_WITH_TRANSPOSE_FLAG(T, TF)
Constraint on the data type.In case the given data type T is not a dense or sparse vector type and in...
Definition: TransposeFlag.h:63
Header file for the thresholds for matrix/vector and matrix/matrix multiplications.
#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
Efficient implementation of an arbitrary sized sparse vector.The CompressedVector class is the repres...
Definition: CompressedVector.h:206
void reserve(size_t n)
Setting the minimum capacity of the compressed vector.
Definition: CompressedVector.h:1388
Header file for the HighType type trait.