From 4310648979b9c849d893287cb4deda1d03c359c0 Mon Sep 17 00:00:00 2001
From: Tomas Oberhuber <tomas.oberhuber@fjfi.cvut.cz>
Date: Wed, 15 Jan 2020 14:45:12 +0100
Subject: [PATCH] Changing Matrix::[set,add]Element from bool to void with
 expcetion throwing.

---
 src/TNL/Matrices/Dense.h                   |  4 ++--
 src/TNL/Matrices/Dense.hpp                 | 20 +++++++++++---------
 src/TNL/Matrices/DenseMatrixView.h         |  4 ++--
 src/TNL/Matrices/DenseMatrixView.hpp       | 20 +++++++++++---------
 src/TNL/Matrices/Matrix.hpp                |  6 +++---
 src/TNL/Matrices/SparseMatrixView.h        |  4 ++--
 src/TNL/Matrices/SparseMatrixView.hpp      | 20 ++++++++++++--------
 src/TNL/Matrices/Tridiagonal.hpp           |  4 ++--
 src/TNL/Matrices/TridiagonalMatrixView.hpp |  2 --
 9 files changed, 45 insertions(+), 39 deletions(-)

diff --git a/src/TNL/Matrices/Dense.h b/src/TNL/Matrices/Dense.h
index 778fd0bd4a..485c947010 100644
--- a/src/TNL/Matrices/Dense.h
+++ b/src/TNL/Matrices/Dense.h
@@ -107,11 +107,11 @@ class Dense : public Matrix< Real, Device, Index >
       const Real& operator()( const IndexType row,
                               const IndexType column ) const;
 
-      bool setElement( const IndexType row,
+      void setElement( const IndexType row,
                        const IndexType column,
                        const RealType& value );
 
-      bool addElement( const IndexType row,
+      void addElement( const IndexType row,
                        const IndexType column,
                        const RealType& value,
                        const RealType& thisElementMultiplicator = 1.0 );
diff --git a/src/TNL/Matrices/Dense.hpp b/src/TNL/Matrices/Dense.hpp
index c4deeb6fa4..7e6f379486 100644
--- a/src/TNL/Matrices/Dense.hpp
+++ b/src/TNL/Matrices/Dense.hpp
@@ -289,12 +289,13 @@ template< typename Real,
           typename Index,
           bool RowMajorOrder,
           typename RealAllocator >
-bool Dense< Real, Device, Index, RowMajorOrder, RealAllocator >::setElement( const IndexType row,
-                                               const IndexType column,
-                                               const RealType& value )
+void
+Dense< Real, Device, Index, RowMajorOrder, RealAllocator >::
+setElement( const IndexType row,
+            const IndexType column,
+            const RealType& value )
 {
    this->values.setElement( this->getElementIndex( row, column ), value );
-   return true;
 }
 
 template< typename Real,
@@ -302,10 +303,12 @@ template< typename Real,
           typename Index,
           bool RowMajorOrder,
           typename RealAllocator >
-bool Dense< Real, Device, Index, RowMajorOrder, RealAllocator >::addElement( const IndexType row,
-                                                        const IndexType column,
-                                                        const RealType& value,
-                                                        const RealType& thisElementMultiplicator )
+void
+Dense< Real, Device, Index, RowMajorOrder, RealAllocator >::
+addElement( const IndexType row,
+            const IndexType column,
+            const RealType& value,
+            const RealType& thisElementMultiplicator )
 {
    const IndexType elementIndex = this->getElementIndex( row, column );
    if( thisElementMultiplicator == 1.0 )
@@ -314,7 +317,6 @@ bool Dense< Real, Device, Index, RowMajorOrder, RealAllocator >::addElement( con
    else
       this->values.setElement( elementIndex,
                                thisElementMultiplicator * this->values.getElement( elementIndex ) + value );
-   return true;
 }
 
 template< typename Real,
diff --git a/src/TNL/Matrices/DenseMatrixView.h b/src/TNL/Matrices/DenseMatrixView.h
index 23f5d73178..9bad424b2a 100644
--- a/src/TNL/Matrices/DenseMatrixView.h
+++ b/src/TNL/Matrices/DenseMatrixView.h
@@ -111,11 +111,11 @@ class DenseMatrixView : public MatrixView< Real, Device, Index >
       const Real& operator()( const IndexType row,
                               const IndexType column ) const;
 
-      bool setElement( const IndexType row,
+      void setElement( const IndexType row,
                        const IndexType column,
                        const RealType& value );
 
-      bool addElement( const IndexType row,
+      void addElement( const IndexType row,
                        const IndexType column,
                        const RealType& value,
                        const RealType& thisElementMultiplicator = 1.0 );
diff --git a/src/TNL/Matrices/DenseMatrixView.hpp b/src/TNL/Matrices/DenseMatrixView.hpp
index 48c0ccdc32..21f6d79ef5 100644
--- a/src/TNL/Matrices/DenseMatrixView.hpp
+++ b/src/TNL/Matrices/DenseMatrixView.hpp
@@ -240,22 +240,25 @@ template< typename Real,
           typename Device,
           typename Index,
           bool RowMajorOrder >
-bool DenseMatrixView< Real, Device, Index, RowMajorOrder >::setElement( const IndexType row,
-                                               const IndexType column,
-                                               const RealType& value )
+void
+DenseMatrixView< Real, Device, Index, RowMajorOrder >::
+setElement( const IndexType row,
+            const IndexType column,
+            const RealType& value )
 {
    this->values.setElement( this->getElementIndex( row, column ), value );
-   return true;
 }
 
 template< typename Real,
           typename Device,
           typename Index,
           bool RowMajorOrder >
-bool DenseMatrixView< Real, Device, Index, RowMajorOrder >::addElement( const IndexType row,
-                                                        const IndexType column,
-                                                        const RealType& value,
-                                                        const RealType& thisElementMultiplicator )
+void
+DenseMatrixView< Real, Device, Index, RowMajorOrder >::
+addElement( const IndexType row,
+            const IndexType column,
+            const RealType& value,
+            const RealType& thisElementMultiplicator )
 {
    const IndexType elementIndex = this->getElementIndex( row, column );
    if( thisElementMultiplicator == 1.0 )
@@ -264,7 +267,6 @@ bool DenseMatrixView< Real, Device, Index, RowMajorOrder >::addElement( const In
    else
       this->values.setElement( elementIndex,
                                thisElementMultiplicator * this->values.getElement( elementIndex ) + value );
-   return true;
 }
 
 template< typename Real,
diff --git a/src/TNL/Matrices/Matrix.hpp b/src/TNL/Matrices/Matrix.hpp
index 0710ca829d..2d5906d236 100644
--- a/src/TNL/Matrices/Matrix.hpp
+++ b/src/TNL/Matrices/Matrix.hpp
@@ -139,12 +139,12 @@ getValues() const
 {
    return this->values;
 }
-   
+
 template< typename Real,
           typename Device,
           typename Index,
           typename RealAllocator >
-typename Matrix< Real, Device, Index, RealAllocator >::ValuesVector& 
+typename Matrix< Real, Device, Index, RealAllocator >::ValuesVector&
 Matrix< Real, Device, Index, RealAllocator >::
 getValues()
 {
@@ -237,7 +237,7 @@ template< typename Real,
           typename Device,
           typename Index,
           typename RealAllocator >
-void 
+void
 Matrix< Real, Device, Index, RealAllocator >::
 computeColorsVector(Containers::Vector<Index, Device, Index> &colorsVector)
 {
diff --git a/src/TNL/Matrices/SparseMatrixView.h b/src/TNL/Matrices/SparseMatrixView.h
index 1f587acf33..aba3b46423 100644
--- a/src/TNL/Matrices/SparseMatrixView.h
+++ b/src/TNL/Matrices/SparseMatrixView.h
@@ -92,11 +92,11 @@ class SparseMatrixView : public MatrixView< Real, Device, Index >
       __cuda_callable__
       RowView getRow( const IndexType& rowIdx );
 
-      bool setElement( const IndexType row,
+      void setElement( const IndexType row,
                        const IndexType column,
                        const RealType& value );
 
-      bool addElement( const IndexType row,
+      void addElement( const IndexType row,
                        const IndexType column,
                        const RealType& value,
                        const RealType& thisElementMultiplicator = 1.0 );
diff --git a/src/TNL/Matrices/SparseMatrixView.hpp b/src/TNL/Matrices/SparseMatrixView.hpp
index d836fe5e97..3b192b4e95 100644
--- a/src/TNL/Matrices/SparseMatrixView.hpp
+++ b/src/TNL/Matrices/SparseMatrixView.hpp
@@ -54,7 +54,7 @@ auto
 SparseMatrixView< Real, Device, Index, MatrixType, SegmentsView >::
 getView() -> ViewType
 {
-   return ViewType( this->getRows(), 
+   return ViewType( this->getRows(),
                     this->getColumns(),
                     this->getValues().getView(),
                     this->columnIndexes.getView(),
@@ -204,13 +204,13 @@ template< typename Real,
           typename Index,
           typename MatrixType,
           template< typename, typename > class SegmentsView >
-bool
+void
 SparseMatrixView< Real, Device, Index, MatrixType, SegmentsView >::
 setElement( const IndexType row,
             const IndexType column,
             const RealType& value )
 {
-   return this->addElement( row, column, value, 0.0 );
+   this->addElement( row, column, value, 0.0 );
 }
 
 template< typename Real,
@@ -218,7 +218,7 @@ template< typename Real,
           typename Index,
           typename MatrixType,
           template< typename, typename > class SegmentsView >
-bool
+void
 SparseMatrixView< Real, Device, Index, MatrixType, SegmentsView >::
 addElement( const IndexType row,
             const IndexType column,
@@ -244,18 +244,22 @@ addElement( const IndexType row,
       if( col == column )
       {
          this->values.setElement( globalIdx, thisElementMultiplicator * this->values.getElement( globalIdx ) + value );
-         return true;
+         return;
       }
       if( col == this->getPaddingIndex() || col > column )
          break;
    }
    if( i == rowSize )
-      return false;
+   {
+      std::stringstream msg;
+      msg << "The capacity of the sparse matrix row number "  << row << " was exceeded.";
+      throw std::logic_error( msg.str() );
+   }
    if( col == this->getPaddingIndex() )
    {
       this->columnIndexes.setElement( globalIdx, column );
       this->values.setElement( globalIdx, value );
-      return true;
+      return;
    }
    else
    {
@@ -273,7 +277,7 @@ addElement( const IndexType row,
 
       this->columnIndexes.setElement( globalIdx, column );
       this->values.setElement( globalIdx, value );
-      return true;
+      return;
    }
 }
 
diff --git a/src/TNL/Matrices/Tridiagonal.hpp b/src/TNL/Matrices/Tridiagonal.hpp
index 41d722c6a7..8f4f4e190b 100644
--- a/src/TNL/Matrices/Tridiagonal.hpp
+++ b/src/TNL/Matrices/Tridiagonal.hpp
@@ -404,7 +404,7 @@ template< typename Real,
           typename RealAllocator >
 template< typename Vector >
 __cuda_callable__
-typename Vector::RealType 
+typename Vector::RealType
 Tridiagonal< Real, Device, Index, RowMajorOrder, RealAllocator >::
 rowVectorProduct( const IndexType row, const Vector& vector ) const
 {
@@ -418,7 +418,7 @@ template< typename Real,
           typename RealAllocator >
    template< typename InVector,
              typename OutVector >
-void 
+void
 Tridiagonal< Real, Device, Index, RowMajorOrder, RealAllocator >::
 vectorProduct( const InVector& inVector, OutVector& outVector ) const
 {
diff --git a/src/TNL/Matrices/TridiagonalMatrixView.hpp b/src/TNL/Matrices/TridiagonalMatrixView.hpp
index e851d2a1f9..008becb092 100644
--- a/src/TNL/Matrices/TridiagonalMatrixView.hpp
+++ b/src/TNL/Matrices/TridiagonalMatrixView.hpp
@@ -228,7 +228,6 @@ setElement( const IndexType row, const IndexType column, const RealType& value )
       throw std::logic_error( msg.str() );
    }
    this->values.setElement( this->getElementIndex( row, column ), value );
-   return true;
 }
 
 template< typename Real,
@@ -254,7 +253,6 @@ addElement( const IndexType row,
    }
    const Index i = this->getElementIndex( row, column );
    this->values.setElement( i, thisElementMultiplicator * this->values.getElement( i ) + value );
-   return true;
 }
 
 template< typename Real,
-- 
GitLab