All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SparseRow.h
Go to the documentation of this file.
1 //=================================================================================================
33 //=================================================================================================
34 
35 #ifndef _BLAZE_MATH_VIEWS_SPARSEROW_H_
36 #define _BLAZE_MATH_VIEWS_SPARSEROW_H_
37 
38 
39 //*************************************************************************************************
40 // Includes
41 //*************************************************************************************************
42 
43 #include <iterator>
44 #include <stdexcept>
55 #include <blaze/math/Functions.h>
57 #include <blaze/math/shims/Reset.h>
66 #include <blaze/util/Assert.h>
67 #include <blaze/util/DisableIf.h>
68 #include <blaze/util/EnableIf.h>
70 #include <blaze/util/mpl/If.h>
71 #include <blaze/util/mpl/Or.h>
72 #include <blaze/util/SelectType.h>
73 #include <blaze/util/Types.h>
77 #include <blaze/util/Unused.h>
78 
79 
80 namespace blaze {
81 
82 //=================================================================================================
83 //
84 // CLASS DEFINITION
85 //
86 //=================================================================================================
87 
88 //*************************************************************************************************
337 template< typename MT // Type of the sparse matrix
338  , bool SO = IsRowMajorMatrix<MT>::value > // Storage order
339 class SparseRow : public SparseVector< SparseRow<MT,SO>, true >
340  , private Row
341 {
342  private:
343  //**Type definitions****************************************************************************
345  typedef typename SelectType< IsExpression<MT>::value, MT, MT& >::Type Operand;
346  //**********************************************************************************************
347 
348  //**********************************************************************************************
350 
356  enum { useConst = IsConst<MT>::value };
357  //**********************************************************************************************
358 
359  public:
360  //**Type definitions****************************************************************************
362  typedef typename RowTrait<MT>::Type ResultType;
364  typedef typename MT::ElementType ElementType;
365  typedef typename MT::ReturnType ReturnType;
366  typedef const SparseRow& CompositeType;
367 
370 
373 
376 
379  //**********************************************************************************************
380 
381  //**Compilation flags***************************************************************************
383  enum { smpAssignable = 0 };
384  //**********************************************************************************************
385 
386  //**Constructors********************************************************************************
389  explicit inline SparseRow( MT& matrix, size_t index );
390  // No explicitly declared copy constructor.
392  //**********************************************************************************************
393 
394  //**Destructor**********************************************************************************
395  // No explicitly declared destructor.
396  //**********************************************************************************************
397 
398  //**Data access functions***********************************************************************
401  inline Reference operator[]( size_t index );
402  inline ConstReference operator[]( size_t index ) const;
403  inline Iterator begin ();
404  inline ConstIterator begin () const;
405  inline ConstIterator cbegin() const;
406  inline Iterator end ();
407  inline ConstIterator end () const;
408  inline ConstIterator cend () const;
410  //**********************************************************************************************
411 
412  //**Assignment operators************************************************************************
415  inline SparseRow& operator= ( const SparseRow& rhs );
416  template< typename VT > inline SparseRow& operator= ( const DenseVector <VT,true>& rhs );
417  template< typename VT > inline SparseRow& operator= ( const SparseVector<VT,true>& rhs );
418  template< typename VT > inline SparseRow& operator+=( const Vector<VT,true>& rhs );
419  template< typename VT > inline SparseRow& operator-=( const Vector<VT,true>& rhs );
420  template< typename VT > inline SparseRow& operator*=( const Vector<VT,true>& rhs );
421 
422  template< typename Other >
423  inline typename EnableIf< IsNumeric<Other>, SparseRow >::Type&
424  operator*=( Other rhs );
425 
426  template< typename Other >
427  inline typename EnableIf< IsNumeric<Other>, SparseRow >::Type&
428  operator/=( Other rhs );
430  //**********************************************************************************************
431 
432  //**Utility functions***************************************************************************
435  inline size_t size() const;
436  inline size_t capacity() const;
437  inline size_t nonZeros() const;
438  inline void reset();
439  inline Iterator insert ( size_t index, const ElementType& value );
440  inline void erase ( size_t index );
441  inline Iterator erase ( Iterator pos );
442  inline Iterator erase ( Iterator first, Iterator last );
443  inline void reserve( size_t n );
444  template< typename Other > inline SparseRow& scale ( Other scalar );
446  //**********************************************************************************************
447 
448  //**Lookup functions****************************************************************************
451  inline Iterator find ( size_t index );
452  inline ConstIterator find ( size_t index ) const;
453  inline Iterator lowerBound( size_t index );
454  inline ConstIterator lowerBound( size_t index ) const;
455  inline Iterator upperBound( size_t index );
456  inline ConstIterator upperBound( size_t index ) const;
458  //**********************************************************************************************
459 
460  //**Low-level utility functions*****************************************************************
463  inline void append( size_t index, const ElementType& value, bool check=false );
465  //**********************************************************************************************
466 
467  //**Expression template evaluation functions****************************************************
470  template< typename Other > inline bool canAlias ( const Other* alias ) const;
471  template< typename Other > inline bool isAliased( const Other* alias ) const;
472  template< typename VT > inline void assign ( const DenseVector <VT,true>& rhs );
473  template< typename VT > inline void assign ( const SparseVector<VT,true>& rhs );
474  template< typename VT > inline void addAssign( const DenseVector <VT,true>& rhs );
475  template< typename VT > inline void addAssign( const SparseVector<VT,true>& rhs );
476  template< typename VT > inline void subAssign( const DenseVector <VT,true>& rhs );
477  template< typename VT > inline void subAssign( const SparseVector<VT,true>& rhs );
479  //**********************************************************************************************
480 
481  private:
482  //**Utility functions***************************************************************************
485  inline size_t extendCapacity() const;
487  //**********************************************************************************************
488 
489  //**Member variables****************************************************************************
493  const size_t row_;
494 
495  //**********************************************************************************************
496 
497  //**Compile time checks*************************************************************************
504  //**********************************************************************************************
505 };
506 //*************************************************************************************************
507 
508 
509 
510 
511 //=================================================================================================
512 //
513 // CONSTRUCTOR
514 //
515 //=================================================================================================
516 
517 //*************************************************************************************************
524 template< typename MT // Type of the sparse matrix
525  , bool SO > // Storage order
526 inline SparseRow<MT,SO>::SparseRow( MT& matrix, size_t index )
527  : matrix_( matrix ) // The sparse matrix containing the row
528  , row_ ( index ) // The index of the row in the matrix
529 {
530  if( matrix_.rows() <= index )
531  throw std::invalid_argument( "Invalid row access index" );
532 }
533 //*************************************************************************************************
534 
535 
536 
537 
538 //=================================================================================================
539 //
540 // DATA ACCESS FUNCTIONS
541 //
542 //=================================================================================================
543 
544 //*************************************************************************************************
550 template< typename MT // Type of the sparse matrix
551  , bool SO > // Storage order
553 {
554  BLAZE_USER_ASSERT( index < size(), "Invalid row access index" );
555  return matrix_(row_,index);
556 }
557 //*************************************************************************************************
558 
559 
560 //*************************************************************************************************
566 template< typename MT // Type of the sparse matrix
567  , bool SO > // Storage order
569 {
570  BLAZE_USER_ASSERT( index < size(), "Invalid row access index" );
571  return const_cast<const MT&>( matrix_ )(row_,index);
572 }
573 //*************************************************************************************************
574 
575 
576 //*************************************************************************************************
583 template< typename MT // Type of the sparse matrix
584  , bool SO > // Storage order
586 {
587  return matrix_.begin( row_ );
588 }
589 //*************************************************************************************************
590 
591 
592 //*************************************************************************************************
599 template< typename MT // Type of the sparse matrix
600  , bool SO > // Storage order
602 {
603  return matrix_.cbegin( row_ );
604 }
605 //*************************************************************************************************
606 
607 
608 //*************************************************************************************************
615 template< typename MT // Type of the sparse matrix
616  , bool SO > // Storage order
618 {
619  return matrix_.cbegin( row_ );
620 }
621 //*************************************************************************************************
622 
623 
624 //*************************************************************************************************
631 template< typename MT // Type of the sparse matrix
632  , bool SO > // Storage order
634 {
635  return matrix_.end( row_ );
636 }
637 //*************************************************************************************************
638 
639 
640 //*************************************************************************************************
647 template< typename MT // Type of the sparse matrix
648  , bool SO > // Storage order
650 {
651  return matrix_.cend( row_ );
652 }
653 //*************************************************************************************************
654 
655 
656 //*************************************************************************************************
663 template< typename MT // Type of the sparse matrix
664  , bool SO > // Storage order
666 {
667  return matrix_.cend( row_ );
668 }
669 //*************************************************************************************************
670 
671 
672 
673 
674 //=================================================================================================
675 //
676 // ASSIGNMENT OPERATORS
677 //
678 //=================================================================================================
679 
680 //*************************************************************************************************
690 template< typename MT // Type of the sparse matrix
691  , bool SO > // Storage order
693 {
694  using blaze::assign;
695 
699 
700  if( this == &rhs || ( &matrix_ == &rhs.matrix_ && row_ == rhs.row_ ) )
701  return *this;
702 
703  if( size() != rhs.size() )
704  throw std::invalid_argument( "Row sizes do not match" );
705 
706  if( rhs.canAlias( &matrix_ ) ) {
707  const ResultType tmp( rhs );
708  matrix_.reset ( row_ );
709  matrix_.reserve( row_, tmp.nonZeros() );
710  assign( *this, tmp );
711  }
712  else {
713  matrix_.reset ( row_ );
714  matrix_.reserve( row_, rhs.nonZeros() );
715  assign( *this, rhs );
716  }
717 
718  return *this;
719 }
720 //*************************************************************************************************
721 
722 
723 //*************************************************************************************************
733 template< typename MT // Type of the sparse matrix
734  , bool SO > // Storage order
735 template< typename VT > // Type of the right-hand side dense vector
737 {
738  using blaze::assign;
739 
743 
744  if( size() != (~rhs).size() )
745  throw std::invalid_argument( "Vector sizes do not match" );
746 
747  if( (~rhs).canAlias( &matrix_ ) ) {
748  const typename VT::ResultType tmp( ~rhs );
749  matrix_.reset( row_ );
750  assign( *this, tmp );
751  }
752  else {
753  matrix_.reset( row_ );
754  assign( *this, ~rhs );
755  }
756 
757  return *this;
758 }
759 //*************************************************************************************************
760 
761 
762 //*************************************************************************************************
772 template< typename MT // Type of the sparse matrix
773  , bool SO > // Storage order
774 template< typename VT > // Type of the right-hand side sparse vector
776 {
777  using blaze::assign;
778 
782 
783  if( size() != (~rhs).size() )
784  throw std::invalid_argument( "Vector sizes do not match" );
785 
786  if( (~rhs).canAlias( &matrix_ ) ) {
787  const typename VT::ResultType tmp( ~rhs );
788  matrix_.reset ( row_ );
789  matrix_.reserve( row_, tmp.nonZeros() );
790  assign( *this, tmp );
791  }
792  else {
793  matrix_.reset ( row_ );
794  matrix_.reserve( row_, (~rhs).nonZeros() );
795  assign( *this, ~rhs );
796  }
797 
798  return *this;
799 }
800 //*************************************************************************************************
801 
802 
803 //*************************************************************************************************
813 template< typename MT // Type of the sparse matrix
814  , bool SO > // Storage order
815 template< typename VT > // Type of the right-hand side vector
817 {
818  using blaze::addAssign;
819 
820  if( size() != (~rhs).size() )
821  throw std::invalid_argument( "Vector sizes do not match" );
822 
823  addAssign( *this, ~rhs );
824 
825  return *this;
826 }
827 //*************************************************************************************************
828 
829 
830 //*************************************************************************************************
840 template< typename MT // Type of the sparse matrix
841  , bool SO > // Storage order
842 template< typename VT > // Type of the right-hand side vector
844 {
845  using blaze::subAssign;
846 
847  if( size() != (~rhs).size() )
848  throw std::invalid_argument( "Vector sizes do not match" );
849 
850  subAssign( *this, ~rhs );
851 
852  return *this;
853 }
854 //*************************************************************************************************
855 
856 
857 //*************************************************************************************************
868 template< typename MT // Type of the sparse matrix
869  , bool SO > // Storage order
870 template< typename VT > // Type of the right-hand side vector
872 {
873  if( size() != (~rhs).size() )
874  throw std::invalid_argument( "Vector sizes do not match" );
875 
876  typedef typename MultTrait<ResultType,typename VT::ResultType>::Type MultType;
877 
880 
881  const MultType tmp( *this * (~rhs) );
882  matrix_.reset( row_ );
883  assign( tmp );
884 
885  return *this;
886 }
887 //*************************************************************************************************
888 
889 
890 //*************************************************************************************************
901 template< typename MT // Type of the sparse matrix
902  , bool SO > // Storage order
903 template< typename Other > // Data type of the right-hand side scalar
904 inline typename EnableIf< IsNumeric<Other>, SparseRow<MT,SO> >::Type&
906 {
907  for( Iterator element=begin(); element!=end(); ++element )
908  element->value() *= rhs;
909  return *this;
910 }
911 //*************************************************************************************************
912 
913 
914 //*************************************************************************************************
926 template< typename MT // Type of the sparse matrix
927  , bool SO > // Storage order
928 template< typename Other > // Data type of the right-hand side scalar
929 inline typename EnableIf< IsNumeric<Other>, SparseRow<MT,SO> >::Type&
931 {
932  BLAZE_USER_ASSERT( rhs != Other(0), "Division by zero detected" );
933 
934  typedef typename DivTrait<ElementType,Other>::Type DT;
935  typedef typename If< IsNumeric<DT>, DT, Other >::Type Tmp;
936 
937  // Depending on the two involved data types, an integer division is applied or a
938  // floating point division is selected.
940  const Tmp tmp( Tmp(1)/static_cast<Tmp>( rhs ) );
941  for( Iterator element=begin(); element!=end(); ++element )
942  element->value() *= tmp;
943  }
944  else {
945  for( Iterator element=begin(); element!=end(); ++element )
946  element->value() /= rhs;
947  }
948 
949  return *this;
950 }
951 //*************************************************************************************************
952 
953 
954 
955 
956 //=================================================================================================
957 //
958 // UTILITY FUNCTIONS
959 //
960 //=================================================================================================
961 
962 //*************************************************************************************************
967 template< typename MT // Type of the sparse matrix
968  , bool SO > // Storage order
969 inline size_t SparseRow<MT,SO>::size() const
970 {
971  return matrix_.columns();
972 }
973 //*************************************************************************************************
974 
975 
976 //*************************************************************************************************
981 template< typename MT // Type of the sparse matrix
982  , bool SO > // Storage order
983 inline size_t SparseRow<MT,SO>::capacity() const
984 {
985  return matrix_.capacity( row_ );
986 }
987 //*************************************************************************************************
988 
989 
990 //*************************************************************************************************
998 template< typename MT // Type of the sparse matrix
999  , bool SO > // Storage order
1000 inline size_t SparseRow<MT,SO>::nonZeros() const
1001 {
1002  return matrix_.nonZeros( row_ );
1003 }
1004 //*************************************************************************************************
1005 
1006 
1007 //*************************************************************************************************
1012 template< typename MT // Type of the sparse matrix
1013  , bool SO > // Storage order
1015 {
1016  matrix_.reset( row_ );
1017 }
1018 //*************************************************************************************************
1019 
1020 
1021 //*************************************************************************************************
1033 template< typename MT // Type of the sparse matrix
1034  , bool SO > // Storage order
1035 inline typename SparseRow<MT,SO>::Iterator
1036  SparseRow<MT,SO>::insert( size_t index, const ElementType& value )
1037 {
1038  return matrix_.insert( row_, index, value );
1039 }
1040 //*************************************************************************************************
1041 
1042 
1043 //*************************************************************************************************
1051 template< typename MT // Type of the sparse matrix
1052  , bool SO > // Storage order
1053 inline void SparseRow<MT,SO>::erase( size_t index )
1054 {
1055  matrix_.erase( row_, index );
1056 }
1057 //*************************************************************************************************
1058 
1059 
1060 //*************************************************************************************************
1068 template< typename MT // Type of the sparse matrix
1069  , bool SO > // Storage order
1071 {
1072  return matrix_.erase( row_, pos );
1073 }
1074 //*************************************************************************************************
1075 
1076 
1077 //*************************************************************************************************
1086 template< typename MT // Type of the sparse matrix
1087  , bool SO > // Storage order
1089 {
1090  return matrix_.erase( row_, first, last );
1091 }
1092 //*************************************************************************************************
1093 
1094 
1095 //*************************************************************************************************
1104 template< typename MT // Type of the sparse matrix
1105  , bool SO > // Storage order
1107 {
1108  matrix_.reserve( row_, n );
1109 }
1110 //*************************************************************************************************
1111 
1112 
1113 //*************************************************************************************************
1119 template< typename MT // Type of the sparse matrix
1120  , bool SO > // Storage order
1121 template< typename Other > // Data type of the scalar value
1123 {
1124  for( Iterator element=begin(); element!=end(); ++element )
1125  element->value() *= scalar;
1126  return *this;
1127 }
1128 //*************************************************************************************************
1129 
1130 
1131 //*************************************************************************************************
1139 template< typename MT // Type of the sparse matrix
1140  , bool SO > // Storage order
1142 {
1143  using blaze::max;
1144  using blaze::min;
1145 
1146  size_t nonzeros( 2UL*capacity()+1UL );
1147  nonzeros = max( nonzeros, 7UL );
1148  nonzeros = min( nonzeros, size() );
1149 
1150  BLAZE_INTERNAL_ASSERT( nonzeros > capacity(), "Invalid capacity value" );
1151 
1152  return nonzeros;
1153 }
1154 //*************************************************************************************************
1155 
1156 
1157 
1158 
1159 //=================================================================================================
1160 //
1161 // LOOKUP FUNCTIONS
1162 //
1163 //=================================================================================================
1164 
1165 //*************************************************************************************************
1178 template< typename MT // Type of the sparse matrix
1179  , bool SO > // Storage order
1181 {
1182  return matrix_.find( row_, index );
1183 }
1184 //*************************************************************************************************
1185 
1186 
1187 //*************************************************************************************************
1200 template< typename MT // Type of the sparse matrix
1201  , bool SO > // Storage order
1202 inline typename SparseRow<MT,SO>::ConstIterator SparseRow<MT,SO>::find( size_t index ) const
1203 {
1204  return matrix_.find( row_, index );
1205 }
1206 //*************************************************************************************************
1207 
1208 
1209 //*************************************************************************************************
1221 template< typename MT // Type of the sparse matrix
1222  , bool SO > // Storage order
1224 {
1225  return matrix_.lowerBound( row_, index );
1226 }
1227 //*************************************************************************************************
1228 
1229 
1230 //*************************************************************************************************
1242 template< typename MT // Type of the sparse matrix
1243  , bool SO > // Storage order
1245 {
1246  return matrix_.lowerBound( row_, index );
1247 }
1248 //*************************************************************************************************
1249 
1250 
1251 //*************************************************************************************************
1263 template< typename MT // Type of the sparse matrix
1264  , bool SO > // Storage order
1266 {
1267  return matrix_.upperBound( row_, index );
1268 }
1269 //*************************************************************************************************
1270 
1271 
1272 //*************************************************************************************************
1284 template< typename MT // Type of the sparse matrix
1285  , bool SO > // Storage order
1287 {
1288  return matrix_.upperBound( row_, index );
1289 }
1290 //*************************************************************************************************
1291 
1292 
1293 
1294 
1295 //=================================================================================================
1296 //
1297 // LOW-LEVEL UTILITY FUNCTIONS
1298 //
1299 //=================================================================================================
1300 
1301 //*************************************************************************************************
1325 template< typename MT // Type of the sparse matrix
1326  , bool SO > // Storage order
1327 inline void SparseRow<MT,SO>::append( size_t index, const ElementType& value, bool check )
1328 {
1329  matrix_.append( row_, index, value, check );
1330 }
1331 //*************************************************************************************************
1332 
1333 
1334 
1335 
1336 //=================================================================================================
1337 //
1338 // EXPRESSION TEMPLATE EVALUATION FUNCTIONS
1339 //
1340 //=================================================================================================
1341 
1342 //*************************************************************************************************
1352 template< typename MT // Type of the sparse matrix
1353  , bool SO > // Storage order
1354 template< typename Other > // Data type of the foreign expression
1355 inline bool SparseRow<MT,SO>::canAlias( const Other* alias ) const
1356 {
1357  return static_cast<const void*>( &matrix_ ) == static_cast<const void*>( alias );
1358 }
1359 //*************************************************************************************************
1360 
1361 
1362 //*************************************************************************************************
1372 template< typename MT // Type of the sparse matrix
1373  , bool SO > // Storage order
1374 template< typename Other > // Data type of the foreign expression
1375 inline bool SparseRow<MT,SO>::isAliased( const Other* alias ) const
1376 {
1377  return static_cast<const void*>( &matrix_ ) == static_cast<const void*>( alias );
1378 }
1379 //*************************************************************************************************
1380 
1381 
1382 //*************************************************************************************************
1393 template< typename MT // Type of the sparse matrix
1394  , bool SO > // Storage order
1395 template< typename VT > // Type of the right-hand side dense vector
1397 {
1398  BLAZE_INTERNAL_ASSERT( size() == (~rhs).size(), "Invalid vector sizes" );
1399  BLAZE_INTERNAL_ASSERT( nonZeros() == 0UL, "Invalid non-zero elements detected" );
1400 
1401  for( size_t j=0UL; j<size(); ++j )
1402  {
1403  if( matrix_.nonZeros( row_ ) == matrix_.capacity( row_ ) )
1404  matrix_.reserve( row_, extendCapacity() );
1405 
1406  matrix_.append( row_, j, (~rhs)[j], true );
1407  }
1408 }
1409 //*************************************************************************************************
1410 
1411 
1412 //*************************************************************************************************
1423 template< typename MT // Type of the sparse matrix
1424  , bool SO > // Storage order
1425 template< typename VT > // Type of the right-hand side sparse vector
1427 {
1428  BLAZE_INTERNAL_ASSERT( size() == (~rhs).size(), "Invalid vector sizes" );
1429  BLAZE_INTERNAL_ASSERT( nonZeros() == 0UL, "Invalid non-zero elements detected" );
1430 
1431  for( typename VT::ConstIterator element=(~rhs).begin(); element!=(~rhs).end(); ++element ) {
1432  matrix_.append( row_, element->index(), element->value() );
1433  }
1434 }
1435 //*************************************************************************************************
1436 
1437 
1438 //*************************************************************************************************
1449 template< typename MT // Type of the sparse matrix
1450  , bool SO > // Storage order
1451 template< typename VT > // Type of the right-hand side dense vector
1453 {
1454  typedef typename AddTrait<ResultType,typename VT::ResultType>::Type AddType;
1455 
1459 
1460  BLAZE_INTERNAL_ASSERT( size() == (~rhs).size(), "Invalid vector sizes" );
1461 
1462  const AddType tmp( *this + (~rhs) );
1463  matrix_.reset( row_ );
1464  assign( tmp );
1465 }
1466 //*************************************************************************************************
1467 
1468 
1469 //*************************************************************************************************
1480 template< typename MT // Type of the sparse matrix
1481  , bool SO > // Storage order
1482 template< typename VT > // Type of the right-hand side sparse vector
1484 {
1485  typedef typename AddTrait<ResultType,typename VT::ResultType>::Type AddType;
1486 
1490 
1491  BLAZE_INTERNAL_ASSERT( size() == (~rhs).size(), "Invalid vector sizes" );
1492 
1493  const AddType tmp( *this + (~rhs) );
1494  matrix_.reset ( row_ );
1495  matrix_.reserve( row_, tmp.nonZeros() );
1496  assign( tmp );
1497 }
1498 //*************************************************************************************************
1499 
1500 
1501 //*************************************************************************************************
1512 template< typename MT // Type of the sparse matrix
1513  , bool SO > // Storage order
1514 template< typename VT > // Type of the right-hand side dense vector
1516 {
1517  typedef typename SubTrait<ResultType,typename VT::ResultType>::Type SubType;
1518 
1522 
1523  BLAZE_INTERNAL_ASSERT( size() == (~rhs).size(), "Invalid vector sizes" );
1524 
1525  const SubType tmp( *this - (~rhs) );
1526  matrix_.reset ( row_ );
1527  assign( tmp );
1528 }
1529 //*************************************************************************************************
1530 
1531 
1532 //*************************************************************************************************
1543 template< typename MT // Type of the sparse matrix
1544  , bool SO > // Storage order
1545 template< typename VT > // Type of the right-hand side sparse vector
1547 {
1548  typedef typename SubTrait<ResultType,typename VT::ResultType>::Type SubType;
1549 
1553 
1554  BLAZE_INTERNAL_ASSERT( size() == (~rhs).size(), "Invalid vector sizes" );
1555 
1556  const SubType tmp( *this - (~rhs) );
1557  matrix_.reset ( row_ );
1558  matrix_.reserve( row_, tmp.nonZeros() );
1559  assign( tmp );
1560 }
1561 //*************************************************************************************************
1562 
1563 
1564 
1565 
1566 
1567 
1568 
1569 
1570 //=================================================================================================
1571 //
1572 // CLASS TEMPLATE SPECIALIZATION FOR COLUMN-MAJOR MATRICES
1573 //
1574 //=================================================================================================
1575 
1576 //*************************************************************************************************
1584 template< typename MT > // Type of the sparse matrix
1585 class SparseRow<MT,false> : public SparseVector< SparseRow<MT,false>, true >
1586  , private Row
1587 {
1588  private:
1589  //**Type definitions****************************************************************************
1591  typedef typename SelectType< IsExpression<MT>::value, MT, MT& >::Type Operand;
1592  //**********************************************************************************************
1593 
1594  //**********************************************************************************************
1596 
1602  enum { useConst = IsConst<MT>::value };
1603  //**********************************************************************************************
1604 
1605  public:
1606  //**Type definitions****************************************************************************
1607  typedef SparseRow<MT,false> This;
1608  typedef typename RowTrait<MT>::Type ResultType;
1609  typedef typename ResultType::TransposeType TransposeType;
1610  typedef typename MT::ElementType ElementType;
1611  typedef typename MT::ReturnType ReturnType;
1612  typedef const ResultType CompositeType;
1613 
1615  typedef typename MT::ConstReference ConstReference;
1616 
1618  typedef typename SelectType< useConst, ConstReference, typename MT::Reference >::Type Reference;
1619  //**********************************************************************************************
1620 
1621  //**RowElement class definition*****************************************************************
1624  template< typename MatrixType // Type of the sparse matrix
1625  , typename IteratorType > // Type of the sparse matrix iterator
1626  class RowElement
1627  {
1628  private:
1629  //*******************************************************************************************
1631 
1636  enum { returnConst = IsConst<MatrixType>::value };
1637  //*******************************************************************************************
1638 
1639  public:
1640  //**Type definitions*************************************************************************
1641  typedef typename SelectType< returnConst, const ElementType&, ElementType& >::Type ReferenceType;
1642  //*******************************************************************************************
1643 
1644  //**Constructor******************************************************************************
1650  inline RowElement( IteratorType pos, size_t column )
1651  : pos_ ( pos ) // Iterator to the current position within the sparse row
1652  , column_( column ) // Index of the according column
1653  {}
1654  //*******************************************************************************************
1655 
1656  //**Assignment operator**********************************************************************
1662  template< typename T > inline RowElement& operator=( const T& v ) {
1663  *pos_ = v;
1664  return *this;
1665  }
1666  //*******************************************************************************************
1667 
1668  //**Addition assignment operator*************************************************************
1674  template< typename T > inline RowElement& operator+=( const T& v ) {
1675  *pos_ += v;
1676  return *this;
1677  }
1678  //*******************************************************************************************
1679 
1680  //**Subtraction assignment operator**********************************************************
1686  template< typename T > inline RowElement& operator-=( const T& v ) {
1687  *pos_ -= v;
1688  return *this;
1689  }
1690  //*******************************************************************************************
1691 
1692  //**Multiplication assignment operator*******************************************************
1698  template< typename T > inline RowElement& operator*=( const T& v ) {
1699  *pos_ *= v;
1700  return *this;
1701  }
1702  //*******************************************************************************************
1703 
1704  //**Division assignment operator*************************************************************
1710  template< typename T > inline RowElement& operator/=( const T& v ) {
1711  *pos_ /= v;
1712  return *this;
1713  }
1714  //*******************************************************************************************
1715 
1716  //**Element access operator******************************************************************
1721  inline const RowElement* operator->() const {
1722  return this;
1723  }
1724  //*******************************************************************************************
1725 
1726  //**Value function***************************************************************************
1731  inline ReferenceType value() const {
1732  return pos_->value();
1733  }
1734  //*******************************************************************************************
1735 
1736  //**Index function***************************************************************************
1741  inline size_t index() const {
1742  return column_;
1743  }
1744  //*******************************************************************************************
1745 
1746  private:
1747  //**Member variables*************************************************************************
1748  IteratorType pos_;
1749  size_t column_;
1750  //*******************************************************************************************
1751  };
1752  //**********************************************************************************************
1753 
1754  //**RowIterator class definition****************************************************************
1757  template< typename MatrixType // Type of the sparse matrix
1758  , typename IteratorType > // Type of the sparse matrix iterator
1759  class RowIterator
1760  {
1761  public:
1762  //**Type definitions*************************************************************************
1763  typedef std::forward_iterator_tag IteratorCategory;
1764  typedef RowElement<MatrixType,IteratorType> ValueType;
1765  typedef ValueType PointerType;
1766  typedef ValueType ReferenceType;
1767  typedef ptrdiff_t DifferenceType;
1768 
1769  // STL iterator requirements
1770  typedef IteratorCategory iterator_category;
1771  typedef ValueType value_type;
1772  typedef PointerType pointer;
1773  typedef ReferenceType reference;
1774  typedef DifferenceType difference_type;
1775  //*******************************************************************************************
1776 
1777  //**Constructor******************************************************************************
1784  inline RowIterator( MatrixType& matrix, size_t row, size_t column )
1785  : matrix_( matrix ) // The sparse matrix containing the row.
1786  , row_ ( row ) // The current row index.
1787  , column_( column ) // The current column index.
1788  , pos_ () // Iterator to the current sparse element.
1789  {
1790  for( ; column_<matrix_.columns(); ++column_ ) {
1791  pos_ = matrix_.find( row_, column_ );
1792  if( pos_ != matrix_.end( column_ ) ) break;
1793  }
1794  }
1795  //*******************************************************************************************
1796 
1797  //**Constructor******************************************************************************
1805  inline RowIterator( MatrixType& matrix, size_t row, size_t column, IteratorType pos )
1806  : matrix_( matrix ) // The sparse matrix containing the row.
1807  , row_ ( row ) // The current row index.
1808  , column_( column ) // The current column index.
1809  , pos_ ( pos ) // Iterator to the current sparse element.
1810  {
1811  BLAZE_INTERNAL_ASSERT( matrix.find( row, column ) == pos, "Invalid initial iterator position" );
1812  }
1813  //*******************************************************************************************
1814 
1815  //**Constructor******************************************************************************
1820  template< typename MatrixType2, typename IteratorType2 >
1821  inline RowIterator( const RowIterator<MatrixType2,IteratorType2>& it )
1822  : matrix_( it.matrix_ ) // The sparse matrix containing the row.
1823  , row_ ( it.row_ ) // The current row index.
1824  , column_( it.column_ ) // The current column index.
1825  , pos_ ( it.pos_ ) // Iterator to the current sparse element.
1826  {}
1827  //*******************************************************************************************
1828 
1829  //**Prefix increment operator****************************************************************
1834  inline RowIterator& operator++() {
1835  ++column_;
1836  for( ; column_<matrix_.columns(); ++column_ ) {
1837  pos_ = matrix_.find( row_, column_ );
1838  if( pos_ != matrix_.end( column_ ) ) break;
1839  }
1840 
1841  return *this;
1842  }
1843  //*******************************************************************************************
1844 
1845  //**Postfix increment operator***************************************************************
1850  inline const RowIterator operator++( int ) {
1851  const RowIterator tmp( *this );
1852  ++(*this);
1853  return tmp;
1854  }
1855  //*******************************************************************************************
1856 
1857  //**Element access operator******************************************************************
1862  inline ReferenceType operator*() const {
1863  return ReferenceType( pos_, column_ );
1864  }
1865  //*******************************************************************************************
1866 
1867  //**Element access operator******************************************************************
1872  inline PointerType operator->() const {
1873  return PointerType( pos_, column_ );
1874  }
1875  //*******************************************************************************************
1876 
1877  //**Equality operator************************************************************************
1883  template< typename MatrixType2, typename IteratorType2 >
1884  inline bool operator==( const RowIterator<MatrixType2,IteratorType2>& rhs ) const {
1885  return ( &matrix_ == &rhs.matrix_ ) && ( row_ == rhs.row_ ) && ( column_ == rhs.column_ );
1886  }
1887  //*******************************************************************************************
1888 
1889  //**Inequality operator**********************************************************************
1895  template< typename MatrixType2, typename IteratorType2 >
1896  inline bool operator!=( const RowIterator<MatrixType2,IteratorType2>& rhs ) const {
1897  return !( *this == rhs );
1898  }
1899  //*******************************************************************************************
1900 
1901  //**Subtraction operator*********************************************************************
1907  inline DifferenceType operator-( const RowIterator& rhs ) const {
1908  size_t counter( 0UL );
1909  for( size_t j=rhs.column_; j<column_; ++j ) {
1910  if( matrix_.find( row_, j ) != matrix_.end( j ) )
1911  ++counter;
1912  }
1913  return counter;
1914  }
1915  //*******************************************************************************************
1916 
1917  private:
1918  //**Member variables*************************************************************************
1919  MatrixType& matrix_;
1920  size_t row_;
1921  size_t column_;
1922  IteratorType pos_;
1923  //*******************************************************************************************
1924 
1925  //**Friend declarations**********************************************************************
1926  template< typename MatrixType2, typename IteratorType2 > friend class RowIterator;
1927  template< typename MT2, bool SO2 > friend class SparseRow;
1928  //*******************************************************************************************
1929  };
1930  //**********************************************************************************************
1931 
1932  //**Type definitions****************************************************************************
1934  typedef RowIterator<const MT,typename MT::ConstIterator> ConstIterator;
1935 
1937  typedef typename SelectType< useConst, ConstIterator, RowIterator<MT,typename MT::Iterator> >::Type Iterator;
1938  //**********************************************************************************************
1939 
1940  //**Compilation flags***************************************************************************
1942  enum { smpAssignable = 0 };
1943  //**********************************************************************************************
1944 
1945  //**Constructors********************************************************************************
1948  explicit inline SparseRow( MT& matrix, size_t index );
1949  // No explicitly declared copy constructor.
1951  //**********************************************************************************************
1952 
1953  //**Destructor**********************************************************************************
1954  // No explicitly declared destructor.
1955  //**********************************************************************************************
1956 
1957  //**Data access functions***********************************************************************
1960  inline Reference operator[]( size_t index );
1961  inline ConstReference operator[]( size_t index ) const;
1962  inline Iterator begin ();
1963  inline ConstIterator begin () const;
1964  inline ConstIterator cbegin() const;
1965  inline Iterator end ();
1966  inline ConstIterator end () const;
1967  inline ConstIterator cend () const;
1969  //**********************************************************************************************
1970 
1971  //**Assignment operators************************************************************************
1974  inline SparseRow& operator= ( const SparseRow& rhs );
1975  template< typename VT > inline SparseRow& operator= ( const Vector<VT,true>& rhs );
1976  template< typename VT > inline SparseRow& operator+=( const Vector<VT,true>& rhs );
1977  template< typename VT > inline SparseRow& operator-=( const Vector<VT,true>& rhs );
1978  template< typename VT > inline SparseRow& operator*=( const Vector<VT,true>& rhs );
1979 
1980  template< typename Other >
1981  inline typename EnableIf< IsNumeric<Other>, SparseRow >::Type&
1982  operator*=( Other rhs );
1983 
1984  template< typename Other >
1985  inline typename EnableIf< IsNumeric<Other>, SparseRow >::Type&
1986  operator/=( Other rhs );
1988  //**********************************************************************************************
1989 
1990  //**Utility functions***************************************************************************
1993  inline size_t size() const;
1994  inline size_t capacity() const;
1995  inline size_t nonZeros() const;
1996  inline void reset();
1997  inline Iterator insert ( size_t index, const ElementType& value );
1998  inline void erase ( size_t index );
1999  inline Iterator erase ( Iterator pos );
2000  inline Iterator erase ( Iterator first, Iterator last );
2001  inline void reserve( size_t n );
2002  template< typename Other > inline SparseRow& scale ( Other scalar );
2004  //**********************************************************************************************
2005 
2006  //**Lookup functions****************************************************************************
2009  inline Iterator find ( size_t index );
2010  inline ConstIterator find ( size_t index ) const;
2011  inline Iterator lowerBound( size_t index );
2012  inline ConstIterator lowerBound( size_t index ) const;
2013  inline Iterator upperBound( size_t index );
2014  inline ConstIterator upperBound( size_t index ) const;
2016  //**********************************************************************************************
2017 
2018  //**Low-level utility functions*****************************************************************
2021  inline void append( size_t index, const ElementType& value, bool check=false );
2023  //**********************************************************************************************
2024 
2025  //**Expression template evaluation functions****************************************************
2028  template< typename Other > inline bool canAlias ( const Other* alias ) const;
2029  template< typename Other > inline bool isAliased( const Other* alias ) const;
2030  template< typename VT > inline void assign ( const DenseVector <VT,true>& rhs );
2031  template< typename VT > inline void assign ( const SparseVector<VT,true>& rhs );
2032  template< typename VT > inline void addAssign( const Vector<VT,true>& rhs );
2033  template< typename VT > inline void subAssign( const Vector<VT,true>& rhs );
2035  //**********************************************************************************************
2036 
2037  private:
2038  //**Member variables****************************************************************************
2041  Operand matrix_;
2042  const size_t row_;
2043 
2044  //**********************************************************************************************
2045 
2046  //**Compile time checks*************************************************************************
2051  //**********************************************************************************************
2052 };
2054 //*************************************************************************************************
2055 
2056 
2057 
2058 
2059 //=================================================================================================
2060 //
2061 // CONSTRUCTOR
2062 //
2063 //=================================================================================================
2064 
2065 //*************************************************************************************************
2073 template< typename MT > // Type of the sparse matrix
2074 inline SparseRow<MT,false>::SparseRow( MT& matrix, size_t index )
2075  : matrix_( matrix ) // The sparse matrix containing the row
2076  , row_ ( index ) // The index of the row in the matrix
2077 {
2078  if( matrix_.rows() <= index )
2079  throw std::invalid_argument( "Invalid row access index" );
2080 }
2082 //*************************************************************************************************
2083 
2084 
2085 
2086 
2087 //=================================================================================================
2088 //
2089 // DATA ACCESS FUNCTIONS
2090 //
2091 //=================================================================================================
2092 
2093 //*************************************************************************************************
2100 template< typename MT > // Type of the sparse matrix
2102 {
2103  BLAZE_USER_ASSERT( index < size(), "Invalid row access index" );
2104  return matrix_(row_,index);
2105 }
2107 //*************************************************************************************************
2108 
2109 
2110 //*************************************************************************************************
2117 template< typename MT > // Type of the sparse matrix
2118 inline typename SparseRow<MT,false>::ConstReference SparseRow<MT,false>::operator[]( size_t index ) const
2119 {
2120  BLAZE_USER_ASSERT( index < size(), "Invalid row access index" );
2121  return const_cast<const MT&>( matrix_ )(row_,index);
2122 }
2124 //*************************************************************************************************
2125 
2126 
2127 //*************************************************************************************************
2135 template< typename MT > // Type of the sparse matrix
2137 {
2138  return Iterator( matrix_, row_, 0UL );
2139 }
2141 //*************************************************************************************************
2142 
2143 
2144 //*************************************************************************************************
2152 template< typename MT > // Type of the sparse matrix
2154 {
2155  return ConstIterator( matrix_, row_, 0UL );
2156 }
2158 //*************************************************************************************************
2159 
2160 
2161 //*************************************************************************************************
2169 template< typename MT > // Type of the sparse matrix
2171 {
2172  return ConstIterator( matrix_, row_, 0UL );
2173 }
2175 //*************************************************************************************************
2176 
2177 
2178 //*************************************************************************************************
2186 template< typename MT > // Type of the sparse matrix
2188 {
2189  return Iterator( matrix_, row_, size() );
2190 }
2192 //*************************************************************************************************
2193 
2194 
2195 //*************************************************************************************************
2203 template< typename MT > // Type of the sparse matrix
2205 {
2206  return ConstIterator( matrix_, row_, size() );
2207 }
2209 //*************************************************************************************************
2210 
2211 
2212 //*************************************************************************************************
2220 template< typename MT > // Type of the sparse matrix
2222 {
2223  return ConstIterator( matrix_, row_, size() );
2224 }
2226 //*************************************************************************************************
2227 
2228 
2229 
2230 
2231 //=================================================================================================
2232 //
2233 // ASSIGNMENT OPERATORS
2234 //
2235 //=================================================================================================
2236 
2237 //*************************************************************************************************
2248 template< typename MT > // Type of the sparse matrix
2249 inline SparseRow<MT,false>& SparseRow<MT,false>::operator=( const SparseRow& rhs )
2250 {
2251  using blaze::assign;
2252 
2256 
2257  if( this == &rhs || ( &matrix_ == &rhs.matrix_ && row_ == rhs.row_ ) )
2258  return *this;
2259 
2260  if( size() != rhs.size() )
2261  throw std::invalid_argument( "Row sizes do not match" );
2262 
2263  if( rhs.canAlias( &matrix_ ) ) {
2264  const ResultType tmp( rhs );
2265  assign( *this, tmp );
2266  }
2267  else {
2268  assign( *this, rhs );
2269  }
2270 
2271  return *this;
2272 }
2274 //*************************************************************************************************
2275 
2276 
2277 //*************************************************************************************************
2288 template< typename MT > // Type of the sparse matrix
2289 template< typename VT > // Type of the right-hand side vector
2290 inline SparseRow<MT,false>& SparseRow<MT,false>::operator=( const Vector<VT,true>& rhs )
2291 {
2292  using blaze::assign;
2293 
2294  if( size() != (~rhs).size() )
2295  throw std::invalid_argument( "Vector sizes do not match" );
2296 
2297  const typename VT::CompositeType tmp( ~rhs );
2298  assign( *this, tmp );
2299 
2300  return *this;
2301 }
2303 //*************************************************************************************************
2304 
2305 
2306 //*************************************************************************************************
2317 template< typename MT > // Type of the sparse matrix
2318 template< typename VT > // Type of the right-hand side vector
2319 inline SparseRow<MT,false>& SparseRow<MT,false>::operator+=( const Vector<VT,true>& rhs )
2320 {
2321  using blaze::addAssign;
2322 
2323  if( size() != (~rhs).size() )
2324  throw std::invalid_argument( "Vector sizes do not match" );
2325 
2326  addAssign( *this, ~rhs );
2327 
2328  return *this;
2329 }
2331 //*************************************************************************************************
2332 
2333 
2334 //*************************************************************************************************
2345 template< typename MT > // Type of the sparse matrix
2346 template< typename VT > // Type of the right-hand side vector
2347 inline SparseRow<MT,false>& SparseRow<MT,false>::operator-=( const Vector<VT,true>& rhs )
2348 {
2349  using blaze::subAssign;
2350 
2351  if( size() != (~rhs).size() )
2352  throw std::invalid_argument( "Vector sizes do not match" );
2353 
2354  subAssign( *this, ~rhs );
2355 
2356  return *this;
2357 }
2359 //*************************************************************************************************
2360 
2361 
2362 //*************************************************************************************************
2374 template< typename MT > // Type of the sparse matrix
2375 template< typename VT > // Type of the right-hand side vector
2376 inline SparseRow<MT,false>& SparseRow<MT,false>::operator*=( const Vector<VT,true>& rhs )
2377 {
2378  if( size() != (~rhs).size() )
2379  throw std::invalid_argument( "Vector sizes do not match" );
2380 
2381  typedef typename MultTrait<ResultType,typename VT::ResultType>::Type MultType;
2382 
2385 
2386  const MultType tmp( *this * (~rhs) );
2387  assign( tmp );
2388 
2389  return *this;
2390 }
2392 //*************************************************************************************************
2393 
2394 
2395 //*************************************************************************************************
2407 template< typename MT > // Type of the sparse matrix
2408 template< typename Other > // Data type of the right-hand side scalar
2409 inline typename EnableIf< IsNumeric<Other>, SparseRow<MT,false> >::Type&
2410  SparseRow<MT,false>::operator*=( Other rhs )
2411 {
2412  for( Iterator element=begin(); element!=end(); ++element )
2413  element->value() *= rhs;
2414  return *this;
2415 }
2417 //*************************************************************************************************
2418 
2419 
2420 //*************************************************************************************************
2433 template< typename MT > // Type of the sparse matrix
2434 template< typename Other > // Data type of the right-hand side scalar
2435 inline typename EnableIf< IsNumeric<Other>, SparseRow<MT,false> >::Type&
2436  SparseRow<MT,false>::operator/=( Other rhs )
2437 {
2438  BLAZE_USER_ASSERT( rhs != Other(0), "Division by zero detected" );
2439 
2440  typedef typename DivTrait<ElementType,Other>::Type DT;
2441  typedef typename If< IsNumeric<DT>, DT, Other >::Type Tmp;
2442 
2443  // Depending on the two involved data types, an integer division is applied or a
2444  // floating point division is selected.
2445  if( IsNumeric<DT>::value && IsFloatingPoint<DT>::value ) {
2446  const Tmp tmp( Tmp(1)/static_cast<Tmp>( rhs ) );
2447  for( Iterator element=begin(); element!=end(); ++element )
2448  element->value() *= tmp;
2449  }
2450  else {
2451  for( Iterator element=begin(); element!=end(); ++element )
2452  element->value() /= rhs;
2453  }
2454 
2455  return *this;
2456 }
2458 //*************************************************************************************************
2459 
2460 
2461 
2462 
2463 //=================================================================================================
2464 //
2465 // UTILITY FUNCTIONS
2466 //
2467 //=================================================================================================
2468 
2469 //*************************************************************************************************
2475 template< typename MT > // Type of the sparse matrix
2476 inline size_t SparseRow<MT,false>::size() const
2477 {
2478  return matrix_.columns();
2479 }
2481 //*************************************************************************************************
2482 
2483 
2484 //*************************************************************************************************
2490 template< typename MT > // Type of the sparse matrix
2491 inline size_t SparseRow<MT,false>::capacity() const
2492 {
2493  return matrix_.columns();
2494 }
2496 //*************************************************************************************************
2497 
2498 
2499 //*************************************************************************************************
2508 template< typename MT > // Type of the sparse matrix
2509 inline size_t SparseRow<MT,false>::nonZeros() const
2510 {
2511  size_t counter( 0UL );
2512  for( ConstIterator element=begin(); element!=end(); ++element ) {
2513  ++counter;
2514  }
2515  return counter;
2516 }
2518 //*************************************************************************************************
2519 
2520 
2521 //*************************************************************************************************
2527 template< typename MT > // Type of the sparse matrix
2528 inline void SparseRow<MT,false>::reset()
2529 {
2530  for( size_t j=0UL; j<size(); ++j ) {
2531  matrix_.erase( row_, j );
2532  }
2533 }
2535 //*************************************************************************************************
2536 
2537 
2538 //*************************************************************************************************
2551 template< typename MT > // Type of the sparse matrix
2552 inline typename SparseRow<MT,false>::Iterator
2553  SparseRow<MT,false>::insert( size_t index, const ElementType& value )
2554 {
2555  return Iterator( matrix_, row_, index, matrix_.insert( row_, index, value ) );
2556 }
2558 //*************************************************************************************************
2559 
2560 
2561 //*************************************************************************************************
2570 template< typename MT > // Type of the sparse matrix
2571 inline void SparseRow<MT,false>::erase( size_t index )
2572 {
2573  matrix_.erase( row_, index );
2574 }
2576 //*************************************************************************************************
2577 
2578 
2579 //*************************************************************************************************
2588 template< typename MT > // Type of the sparse matrix
2590 {
2591  const size_t column( pos.column_ );
2592 
2593  if( column == size() )
2594  return pos;
2595 
2596  matrix_.erase( column, pos.pos_ );
2597  return Iterator( matrix_, row_, column+1UL );
2598 }
2600 //*************************************************************************************************
2601 
2602 
2603 //*************************************************************************************************
2613 template< typename MT > // Type of the sparse matrix
2615 {
2616  for( ; first!=last; ++first ) {
2617  matrix_.erase( first.column_, first.pos_ );
2618  }
2619  return last;
2620 }
2622 //*************************************************************************************************
2623 
2624 
2625 //*************************************************************************************************
2635 template< typename MT > // Type of the sparse matrix
2636 void SparseRow<MT,false>::reserve( size_t n )
2637 {
2638  UNUSED_PARAMETER( n );
2639  return;
2640 }
2642 //*************************************************************************************************
2643 
2644 
2645 //*************************************************************************************************
2652 template< typename MT > // Type of the sparse matrix
2653 template< typename Other > // Data type of the scalar value
2654 inline SparseRow<MT,false>& SparseRow<MT,false>::scale( Other scalar )
2655 {
2656  for( Iterator element=begin(); element!=end(); ++element )
2657  element->value() *= scalar;
2658  return *this;
2659 }
2661 //*************************************************************************************************
2662 
2663 
2664 
2665 
2666 //=================================================================================================
2667 //
2668 // LOOKUP FUNCTIONS
2669 //
2670 //=================================================================================================
2671 
2672 //*************************************************************************************************
2686 template< typename MT > // Type of the sparse matrix
2687 inline typename SparseRow<MT,false>::Iterator SparseRow<MT,false>::find( size_t index )
2688 {
2689  const typename MT::Iterator pos( matrix_.find( row_, index ) );
2690 
2691  if( pos != matrix_.end( index ) )
2692  return Iterator( matrix_, row_, index, pos );
2693  else
2694  return end();
2695 }
2697 //*************************************************************************************************
2698 
2699 
2700 //*************************************************************************************************
2714 template< typename MT > // Type of the sparse matrix
2715 inline typename SparseRow<MT,false>::ConstIterator SparseRow<MT,false>::find( size_t index ) const
2716 {
2717  const typename MT::ConstIterator pos( matrix_.find( row_, index ) );
2718 
2719  if( pos != matrix_.end( index ) )
2720  return ConstIterator( matrix_, row_, index, pos );
2721  else
2722  return end();
2723 }
2725 //*************************************************************************************************
2726 
2727 
2728 //*************************************************************************************************
2741 template< typename MT > // Type of the sparse matrix
2742 inline typename SparseRow<MT,false>::Iterator SparseRow<MT,false>::lowerBound( size_t index )
2743 {
2744  for( size_t i=index; i<size(); ++i )
2745  {
2746  const typename MT::Iterator pos( matrix_.find( row_, i ) );
2747 
2748  if( pos != matrix_.end( i ) )
2749  return Iterator( matrix_, row_, i, pos );
2750  }
2751 
2752  return end();
2753 }
2755 //*************************************************************************************************
2756 
2757 
2758 //*************************************************************************************************
2771 template< typename MT > // Type of the sparse matrix
2772 inline typename SparseRow<MT,false>::ConstIterator SparseRow<MT,false>::lowerBound( size_t index ) const
2773 {
2774  for( size_t i=index; i<size(); ++i )
2775  {
2776  const typename MT::ConstIterator pos( matrix_.find( row_, i ) );
2777 
2778  if( pos != matrix_.end( i ) )
2779  return ConstIterator( matrix_, row_, i, pos );
2780  }
2781 
2782  return end();
2783 }
2785 //*************************************************************************************************
2786 
2787 
2788 //*************************************************************************************************
2801 template< typename MT > // Type of the sparse matrix
2802 inline typename SparseRow<MT,false>::Iterator SparseRow<MT,false>::upperBound( size_t index )
2803 {
2804  for( size_t i=index+1UL; i<size(); ++i )
2805  {
2806  const typename MT::Iterator pos( matrix_.find( row_, i ) );
2807 
2808  if( pos != matrix_.end( i ) )
2809  return Iterator( matrix_, row_, i, pos );
2810  }
2811 
2812  return end();
2813 }
2815 //*************************************************************************************************
2816 
2817 
2818 //*************************************************************************************************
2831 template< typename MT > // Type of the sparse matrix
2832 inline typename SparseRow<MT,false>::ConstIterator SparseRow<MT,false>::upperBound( size_t index ) const
2833 {
2834  for( size_t i=index+1UL; i<size(); ++i )
2835  {
2836  const typename MT::ConstIterator pos( matrix_.find( row_, i ) );
2837 
2838  if( pos != matrix_.end( i ) )
2839  return ConstIterator( matrix_, row_, i, pos );
2840  }
2841 
2842  return end();
2843 }
2845 //*************************************************************************************************
2846 
2847 
2848 
2849 
2850 //=================================================================================================
2851 //
2852 // LOW-LEVEL UTILITY FUNCTIONS
2853 //
2854 //=================================================================================================
2855 
2856 //*************************************************************************************************
2881 template< typename MT > // Type of the sparse matrix
2882 inline void SparseRow<MT,false>::append( size_t index, const ElementType& value, bool check )
2883 {
2884  if( !check || !isDefault( value ) )
2885  matrix_.insert( row_, index, value );
2886 }
2888 //*************************************************************************************************
2889 
2890 
2891 
2892 
2893 //=================================================================================================
2894 //
2895 // EXPRESSION TEMPLATE EVALUATION FUNCTIONS
2896 //
2897 //=================================================================================================
2898 
2899 //*************************************************************************************************
2910 template< typename MT > // Type of the sparse matrix
2911 template< typename Other > // Data type of the foreign expression
2912 inline bool SparseRow<MT,false>::canAlias( const Other* alias ) const
2913 {
2914  return static_cast<const void*>( &matrix_ ) == static_cast<const void*>( alias );
2915 }
2917 //*************************************************************************************************
2918 
2919 
2920 //*************************************************************************************************
2927 template< typename MT > // Type of the sparse matrix
2928 template< typename Other > // Data type of the foreign expression
2929 inline bool SparseRow<MT,false>::isAliased( const Other* alias ) const
2930 {
2931  return static_cast<const void*>( &matrix_ ) == static_cast<const void*>( alias );
2932 }
2934 //*************************************************************************************************
2935 
2936 
2937 //*************************************************************************************************
2949 template< typename MT > // Type of the sparse matrix
2950 template< typename VT > // Type of the right-hand side dense vector
2951 inline void SparseRow<MT,false>::assign( const DenseVector<VT,true>& rhs )
2952 {
2953  BLAZE_INTERNAL_ASSERT( size() == (~rhs).size(), "Invalid vector sizes" );
2954 
2955  for( size_t j=0UL; j<(~rhs).size(); ++j ) {
2956  matrix_(row_,j) = (~rhs)[j];
2957  }
2958 }
2960 //*************************************************************************************************
2961 
2962 
2963 //*************************************************************************************************
2975 template< typename MT > // Type of the sparse matrix
2976 template< typename VT > // Type of the right-hand side sparse vector
2977 inline void SparseRow<MT,false>::assign( const SparseVector<VT,true>& rhs )
2978 {
2979  BLAZE_INTERNAL_ASSERT( size() == (~rhs).size(), "Invalid vector sizes" );
2980 
2981  size_t j( 0UL );
2982 
2983  for( typename VT::ConstIterator element=(~rhs).begin(); element!=(~rhs).end(); ++element ) {
2984  for( ; j<element->index(); ++j )
2985  matrix_.erase( row_, j );
2986  matrix_(row_,j++) = element->value();
2987  }
2988  for( ; j<size(); ++j ) {
2989  matrix_.erase( row_, j );
2990  }
2991 }
2993 //*************************************************************************************************
2994 
2995 
2996 //*************************************************************************************************
3008 template< typename MT > // Type of the sparse matrix
3009 template< typename VT > // Type of the right-hand side vector
3010 inline void SparseRow<MT,false>::addAssign( const Vector<VT,true>& rhs )
3011 {
3012  typedef typename AddTrait<ResultType,typename VT::ResultType>::Type AddType;
3013 
3016 
3017  BLAZE_INTERNAL_ASSERT( size() == (~rhs).size(), "Invalid vector sizes" );
3018 
3019  const AddType tmp( *this + (~rhs) );
3020  assign( tmp );
3021 }
3023 //*************************************************************************************************
3024 
3025 
3026 //*************************************************************************************************
3038 template< typename MT > // Type of the sparse matrix
3039 template< typename VT > // Type of the right-hand side vector
3040 inline void SparseRow<MT,false>::subAssign( const Vector<VT,true>& rhs )
3041 {
3042  typedef typename SubTrait<ResultType,typename VT::ResultType>::Type SubType;
3043 
3046 
3047  BLAZE_INTERNAL_ASSERT( size() == (~rhs).size(), "Invalid vector sizes" );
3048 
3049  const SubType tmp( *this - (~rhs) );
3050  assign( tmp );
3051 }
3053 //*************************************************************************************************
3054 
3055 
3056 
3057 
3058 
3059 
3060 
3061 
3062 //=================================================================================================
3063 //
3064 // SPARSEROW OPERATORS
3065 //
3066 //=================================================================================================
3067 
3068 //*************************************************************************************************
3071 template< typename MT, bool SO >
3072 inline void reset( SparseRow<MT,SO>& row );
3073 
3074 template< typename MT, bool SO >
3075 inline void clear( SparseRow<MT,SO>& row );
3076 
3077 template< typename MT, bool SO >
3078 inline bool isDefault( const SparseRow<MT,SO>& row );
3080 //*************************************************************************************************
3081 
3082 
3083 //*************************************************************************************************
3090 template< typename MT // Type of the sparse matrix
3091  , bool SO > // Storage order
3092 inline void reset( SparseRow<MT,SO>& row )
3093 {
3094  row.reset();
3095 }
3096 //*************************************************************************************************
3097 
3098 
3099 //*************************************************************************************************
3108 template< typename MT // Type of the sparse matrix
3109  , bool SO > // Storage order
3110 inline void clear( SparseRow<MT,SO>& row )
3111 {
3112  row.reset();
3113 }
3114 //*************************************************************************************************
3115 
3116 
3117 //*************************************************************************************************
3135 template< typename MT // Type of the sparse matrix
3136  , bool SO > // Storage order
3137 inline bool isDefault( const SparseRow<MT,SO>& row )
3138 {
3140 
3141  const ConstIterator end( row.end() );
3142  for( ConstIterator element=row.begin(); element!=end; ++element )
3143  if( !isDefault( element->value() ) ) return false;
3144  return true;
3145 }
3146 //*************************************************************************************************
3147 
3148 
3149 
3150 
3151 //=================================================================================================
3152 //
3153 // SUBVECTORTRAIT SPECIALIZATIONS
3154 //
3155 //=================================================================================================
3156 
3157 //*************************************************************************************************
3159 template< typename MT, bool SO >
3160 struct SubvectorTrait< SparseRow<MT,SO> >
3161 {
3162  typedef typename SubvectorTrait< typename SparseRow<MT,SO>::ResultType >::Type Type;
3163 };
3165 //*************************************************************************************************
3166 
3167 } // namespace blaze
3168 
3169 #endif
Constraint on the data type.
const size_t row_
The index of the row in the matrix.
Definition: SparseRow.h:493
Iterator end()
Returns an iterator just past the last element of the row.
Definition: SparseRow.h:633
Compile time check for numeric types.This type trait tests whether or not the given template paramete...
Definition: IsNumeric.h:98
Header file for mathematical functions.
size_t nonZeros() const
Returns the number of non-zero elements in the row.
Definition: SparseRow.h:1000
void reset(DynamicMatrix< Type, SO > &m)
Resetting the given dense matrix.
Definition: DynamicMatrix.h:4579
#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
Header file for the UNUSED_PARAMETER function template.
Compile time type selection.The If class template selects one of the two given types T2 and T3 depend...
Definition: If.h:112
const DMatDMatMultExpr< T1, T2 > operator*(const DenseMatrix< T1, false > &lhs, const DenseMatrix< T2, false > &rhs)
Multiplication operator for the multiplication of two row-major dense matrices ( ).
Definition: DMatDMatMultExpr.h:4075
Header file for the subtraction trait.
bool isAliased(const Other *alias) const
Returns whether the sparse row is aliased with the given address alias.
Definition: SparseRow.h:1375
Header file for the SparseVector base class.
MT::ReturnType ReturnType
Return type for expression template evaluations.
Definition: SparseRow.h:365
size_t size() const
Returns the current size/dimension of the sparse row.
Definition: SparseRow.h:969
size_t capacity() const
Returns the maximum capacity of the sparse row.
Definition: SparseRow.h:983
void reset()
Reset to the default initial values.
Definition: SparseRow.h:1014
Header file for the row trait.
bool isDefault(const DynamicMatrix< Type, SO > &m)
Returns whether the given dense matrix is in default state.
Definition: DynamicMatrix.h:4622
Efficient implementation of a compressed matrix.The CompressedMatrix class template is the represent...
Definition: CompressedMatrix.h:197
Header file for the row base class.
#define BLAZE_CONSTRAINT_MUST_NOT_BE_COMPUTATION_TYPE(T)
Constraint on the data type.In case the given data type T is a computational expression (i...
Definition: Computation.h:118
MT::ConstIterator ConstIterator
Iterator over constant elements.
Definition: SparseRow.h:375
const This & CompositeType
Data type for composite expression templates.
Definition: CompressedMatrix.h:2384
Operand matrix_
The sparse matrix containing the row.
Definition: SparseRow.h:492
void erase(size_t index)
Erasing an element from the sparse row.
Definition: SparseRow.h:1053
DisableIf< Or< IsComputation< MT >, IsTransExpr< MT > >, typename ColumnExprTrait< MT >::Type >::Type column(Matrix< MT, SO > &matrix, size_t index)
Creating a view on a specific column of the given matrix.
Definition: Column.h:103
void UNUSED_PARAMETER(const T1 &)
Suppression of unused parameter warnings.
Definition: Unused.h:84
void append(size_t index, const ElementType &value, bool check=false)
Appending an element to the sparse row.
Definition: SparseRow.h:1327
Access proxy for sparse, matrices.The MatrixAccessProxy provides safe access to the elements of a no...
Definition: MatrixAccessProxy.h:86
#define BLAZE_CONSTRAINT_MUST_NOT_BE_TRANSEXPR_TYPE(T)
Constraint on the data type.In case the given data type T is a transposition expression (i...
Definition: TransExpr.h:118
bool canAlias(const Other *alias) const
Returns whether the sparse row can alias with the given address alias.
Definition: SparseRow.h:1355
Iterator upperBound(size_t index)
Returns an iterator to the first index greater then the given index.
Definition: SparseRow.h:1265
void clear(DynamicMatrix< Type, SO > &m)
Clearing the given dense matrix.
Definition: DynamicMatrix.h:4595
void assign(const DenseVector< VT, true > &rhs)
Default implementation of the assignment of a dense vector.
Definition: SparseRow.h:1396
MT::ConstReference ConstReference
Reference to a constant row value.
Definition: SparseRow.h:369
Constraint on the data type.
Iterator begin()
Returns an iterator to the first element of the row.
Definition: SparseRow.h:585
Base template for the RowTrait class.
Definition: RowTrait.h:114
Compile time type selection.The SelectType class template selects one of the two given types T1 and T...
Definition: SelectType.h:59
Header file for the DisableIf class template.
Header file for the multiplication trait.
Header file for the If class template.
ConstIterator cend() const
Returns an iterator just past the last element of the row.
Definition: SparseRow.h:665
Header file for the IsFloatingPoint type trait.
#define BLAZE_CONSTRAINT_MUST_BE_COLUMN_MAJOR_MATRIX_TYPE(T)
Constraint on the data type.In case the given data type T is not a column-major dense or sparse matri...
Definition: StorageOrder.h:161
const Element * ConstIterator
Iterator over constant elements.
Definition: CompressedMatrix.h:2388
Iterator find(size_t index)
Searches for a specific row element.
Definition: SparseRow.h:1180
Header file for the Or class template.
Reference operator[](size_t index)
Subscript operator for the direct access to the row elements.
Definition: SparseRow.h:552
Header file for the subvector trait.
void assign(Matrix< MT1, SO1 > &lhs, const Matrix< MT2, SO2 > &rhs)
Default implementation of the assignment of a matrix to a matrix.
Definition: Matrix.h:179
Base class for N-dimensional dense vectors.The DenseVector class is a base class for all arbitrarily ...
Definition: DenseVector.h:70
#define BLAZE_CONSTRAINT_MUST_BE_SPARSE_VECTOR_TYPE(T)
Constraint on the data type.In case the given data type T is not a sparse, N-dimensional vector type...
Definition: SparseVector.h:79
SparseRow & operator=(const SparseRow &rhs)
Copy assignment operator for SparseRow.
Definition: SparseRow.h:692
void addAssign(const DenseVector< VT, true > &rhs)
Default implementation of the addition assignment of a dense vector.
Definition: SparseRow.h:1452
Constraint on the data type.
Constraint on the data type.
Constraints on the storage order of matrix types.
Constraint on the data type.
Compile time check for floating point data types.This type trait tests whether or not the given templ...
Definition: IsFloatingPoint.h:94
Type ElementType
Type of the sparse matrix elements.
Definition: CompressedMatrix.h:2382
Constraint on the data type.
Header file for the SelectType class template.
Constraint on the data type.
const Type & ConstReference
Reference to a constant matrix value.
Definition: CompressedMatrix.h:2386
Header file for the EnableIf class template.
void subAssign(const DenseVector< VT, true > &rhs)
Default implementation of the subtraction assignment of a dense vector.
Definition: SparseRow.h:1515
void reserve(size_t n)
Setting the minimum capacity of the sparse row.
Definition: SparseRow.h:1106
Header file for the IsNumeric type trait.
DisableIf< Or< IsComputation< MT >, IsTransExpr< MT > >, typename RowExprTrait< MT >::Type >::Type row(Matrix< MT, SO > &matrix, size_t index)
Creating a view on a specific row of the given matrix.
Definition: Row.h:103
Iterator lowerBound(size_t index)
Returns an iterator to the first index not less then the given index.
Definition: SparseRow.h:1223
ConstIterator cbegin() const
Returns an iterator to the first element of the row.
Definition: SparseRow.h:617
#define BLAZE_CONSTRAINT_MUST_BE_ROW_MAJOR_MATRIX_TYPE(T)
Constraint on the data type.In case the given data type T is not a row-major dense or sparse matrix t...
Definition: StorageOrder.h:81
const Type & ReturnType
Return type for expression template evaluations.
Definition: CompressedMatrix.h:2383
Header file for the IsConst type trait.
Base class for all rows.The Row class serves as a tag for all rows (i.e. dense and sparse rows)...
Definition: Row.h:63
Header file for run time assertion macros.
Base template for the AddTrait class.
Definition: AddTrait.h:141
Base template for the MultTrait class.
Definition: MultTrait.h:141
Header file for the addition trait.
Reference to a specific row of a sparse matrix.The SparseRow template represents a reference to a spe...
Definition: Forward.h:52
Header file for the division trait.
size_t extendCapacity() const
Calculating a new sparse row capacity.
Definition: SparseRow.h:1141
void addAssign(Matrix< MT1, SO1 > &lhs, const Matrix< MT2, SO2 > &rhs)
Default implementation of the addition assignment of a matrix to a matrix.
Definition: Matrix.h:209
const DenseIterator< Type > operator-(const DenseIterator< Type > &it, ptrdiff_t inc)
Subtraction between a DenseIterator and an integral value.
Definition: DenseIterator.h:585
Substitution Failure Is Not An Error (SFINAE) class.The EnableIf class template is an auxiliary tool ...
Definition: EnableIf.h:184
ResultType::TransposeType TransposeType
Transpose type for expression template evaluations.
Definition: SparseRow.h:363
Header file for the reset shim.
SparseRow(MT &matrix, size_t index)
The constructor for SparseRow.
Definition: SparseRow.h:526
void subAssign(Matrix< MT1, SO1 > &lhs, const Matrix< MT2, SO2 > &rhs)
Default implementation of the subtraction assignment of a matrix to matrix.
Definition: Matrix.h:239
const VT::ElementType max(const SparseVector< VT, TF > &sv)
Returns the largest element of the sparse vector.
Definition: SparseVector.h:405
Element * Iterator
Iterator over non-constant elements.
Definition: CompressedMatrix.h:2387
Header file for the isDefault shim.
SelectType< useConst, ConstReference, typename MT::Reference >::Type Reference
Reference to a non-constant row value.
Definition: SparseRow.h:372
#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:118
Compile time check for constant data types.The IsConst type trait tests whether or not the given temp...
Definition: IsConst.h:94
Base template for the DivTrait class.
Definition: DivTrait.h:141
#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:79
SelectType< IsExpression< MT >::value, MT, MT & >::Type Operand
Composite data type of the dense matrix expression.
Definition: SparseRow.h:345
Header file for the IsRowMajorMatrix type trait.
Base class for N-dimensional vectors.The Vector class is a base class for all arbitrarily sized (N-di...
Definition: Forward.h:147
Iterator insert(size_t index, const ElementType &value)
Inserting an element into the sparse row.
Definition: SparseRow.h:1036
CompressedMatrix< Type,!SO > TransposeType
Transpose type for expression template evaluations.
Definition: CompressedMatrix.h:248
Base class for sparse vectors.The SparseVector class is a base class for all arbitrarily sized (N-dim...
Definition: Forward.h:105
RowTrait< MT >::Type ResultType
Result type for expression template evaluations.
Definition: SparseRow.h:362
bool operator==(const NegativeAccuracy< A > &lhs, const T &rhs)
Equality comparison between a NegativeAccuracy object and a floating point value. ...
Definition: Accuracy.h:249
This ResultType
Result type for expression template evaluations.
Definition: CompressedMatrix.h:2379
bool operator!=(const NegativeAccuracy< A > &lhs, const T &rhs)
Inequality comparison between a NegativeAccuracy object and a floating point value.
Definition: Accuracy.h:289
Header file for basic type definitions.
#define BLAZE_CONSTRAINT_MUST_BE_ROW_VECTOR_TYPE(T)
Constraint on the data type.In case the given data type T is not a row dense or sparse vector type (i...
Definition: TransposeFlag.h:81
SelectType< useConst, ConstIterator, typename MT::Iterator >::Type Iterator
Iterator over non-constant elements.
Definition: SparseRow.h:378
MatrixAccessProxy< This > Reference
Reference to a non-constant matrix value.
Definition: CompressedMatrix.h:2385
Base template for the SubTrait class.
Definition: SubTrait.h:141
const VT::ElementType min(const SparseVector< VT, TF > &sv)
Returns the smallest element of the sparse vector.
Definition: SparseVector.h:348
MT::ElementType ElementType
Type of the row elements.
Definition: SparseRow.h:364
#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
const SparseRow & CompositeType
Data type for composite expression templates.
Definition: SparseRow.h:366
#define BLAZE_CONSTRAINT_MUST_BE_SPARSE_MATRIX_TYPE(T)
Constraint on the data type.In case the given data type T is not a sparse, N-dimensional matrix type...
Definition: SparseMatrix.h:79
SparseRow< MT, SO > This
Type of this SparseRow instance.
Definition: SparseRow.h:361
Header file for the IsExpression type trait class.
Header file for the FunctionTrace class.