WSL/SLF GitLab Repository

Skip to content
Snippets Groups Projects
Commit 1bcad13b authored by Mathias Bavay's avatar Mathias Bavay
Browse files

Better error messages when compiling in debug as well as for dimensions issues...

Better error messages when compiling in debug as well as for dimensions issues (adding arrays of incompatible dimensions, invalid subset, etc)
parent 2ca6d716
No related branches found
No related tags found
No related merge requests found
......@@ -167,7 +167,9 @@ template<class T> void Array<T>::resize(const unsigned int& asize, const T& init
template<class T> T& Array<T>::operator()(const unsigned int& index) {
#ifndef NOSAFECHECKS
if (index >= nx) {
throw IndexOutOfBoundsException("", AT);
std::stringstream ss;
ss << "Trying to access array(" << index << ")";
throw IndexOutOfBoundsException(ss.str(), AT);
}
#endif
return vecData[index];
......@@ -176,7 +178,9 @@ template<class T> T& Array<T>::operator()(const unsigned int& index) {
template<class T> const T Array<T>::operator()(const unsigned int& index) const {
#ifndef NOSAFECHECKS
if (index >= nx) {
throw IndexOutOfBoundsException("", AT);
std::stringstream ss;
ss << "Trying to access array(" << index << ")";
throw IndexOutOfBoundsException(ss.str(), AT);
}
#endif
return vecData[index];
......@@ -184,22 +188,18 @@ template<class T> const T Array<T>::operator()(const unsigned int& index) const
template<class T> T& Array<T>::operator [](const unsigned int& index) {
#ifndef NOSAFECHECKS
if (index >= nx) {
throw IndexOutOfBoundsException("", AT);
}
#endif
return vecData.at(index);
#else
return vecData[index];
#endif
}
template<class T> const T Array<T>::operator [](const unsigned int& index) const {
#ifndef NOSAFECHECKS
if (index >= nx) {
throw IndexOutOfBoundsException("", AT);
}
#endif
return vecData.at(index);
#else
return vecData[index];
#endif
}
template<class T> void Array<T>::clear() {
......@@ -224,7 +224,9 @@ template<class T> void Array<T>::insertAt(const int& index, T e) {
vecData.insert(vecData.begin() + index, e);
nx++;
} else {
throw IndexOutOfBoundsException("", AT);
std::stringstream ss;
ss << "Inserting an element at (" << index << ") in an array of size (" << nx << ")";
throw IndexOutOfBoundsException(ss.str(), AT);
}
}
......@@ -357,8 +359,12 @@ template<class T> Array<T>& Array<T>::operator=(const T& value) {
template<class T> Array<T>& Array<T>::operator+=(const Array<T>& rhs)
{
//They have to have equal size
if (rhs.nx != nx)
throw IOException("Trying to add two Array objects with different dimensions", AT);
if (rhs.nx != nx) {
std::stringstream ss;
ss << "Trying to add two Array objects with different dimensions: ";
ss << "(" << nx << ") + (" << rhs.nx << ")";
throw IOException(ss.str(), AT);
}
//Add to every single member of the Array<T>
if(keep_nodata==false) {
......@@ -413,8 +419,12 @@ template<class T> const Array<T> Array<T>::operator+(const T& rhs)
template<class T> Array<T>& Array<T>::operator-=(const Array<T>& rhs)
{
//They have to have equal size
if (rhs.nx != nx)
throw IOException("Trying to substract two Array objects with different dimensions", AT);
if (rhs.nx != nx) {
std::stringstream ss;
ss << "Trying to substract two Array objects with different dimensions: ";
ss << "(" << nx << ") - (" << rhs.nx << ")";
throw IOException(ss.str(), AT);
}
//Substract to every single member of the Array<T>
if(keep_nodata==false) {
......@@ -469,9 +479,12 @@ template<class T> const Array<T> Array<T>::operator-(const T& rhs)
template<class T> Array<T>& Array<T>::operator*=(const Array<T>& rhs)
{
//They have to have equal size
if (rhs.nx != nx)
throw IOException("Trying to multiply two Array objects with different dimensions", AT);
if (rhs.nx != nx){
std::stringstream ss;
ss << "Trying to multiply two Array objects with different dimensions: ";
ss << "(" << nx << ") * (" << rhs.nx << ")";
throw IOException(ss.str(), AT);
}
//Multiply every single member of the Array<T>
if(keep_nodata==false) {
for (unsigned int ii=0; ii<nx; ii++) {
......@@ -525,9 +538,12 @@ template<class T> const Array<T> Array<T>::operator*(const T& rhs)
template<class T> Array<T>& Array<T>::operator/=(const Array<T>& rhs)
{
//They have to have equal size
if (rhs.nx != nx)
throw IOException("Trying to divide two Array objects with different dimensions", AT);
if (rhs.nx != nx){
std::stringstream ss;
ss << "Trying to divide two Array objects with different dimensions: ";
ss << "(" << nx << ") / (" << rhs.nx << ")";
throw IOException(ss.str(), AT);
}
//Divide every single member of the Array<T>
if(keep_nodata==false) {
for (unsigned int ii=0; ii<nx; ii++) {
......
......@@ -204,16 +204,26 @@ template<class T> class Array2D {
};
template<class T> T& Array2D<T>::operator()(const unsigned int& i) {
#ifndef NOSAFECHECKS
return vecData.at(i);
#else
return vecData[i];
#endif
}
template<class T> const T Array2D<T>::operator()(const unsigned int& i) const {
#ifndef NOSAFECHECKS
return vecData.at(i);
#else
return vecData[i];
#endif
}
template<class T> T& Array2D<T>::operator()(const unsigned int& x, const unsigned int& y) {
#ifndef NOSAFECHECKS
if ((x >= nx) || (y >= ny)) {
throw IndexOutOfBoundsException("", AT);
std::stringstream ss;
ss << "Trying to access array(" << x << "," << y << ")";
throw IndexOutOfBoundsException(ss.str(), AT);
}
#endif
//ROW-MAJOR alignment of the vector: fully C-compatible memory layout
......@@ -223,7 +233,9 @@ template<class T> T& Array2D<T>::operator()(const unsigned int& x, const unsigne
template<class T> const T Array2D<T>::operator()(const unsigned int& x, const unsigned int& y) const {
#ifndef NOSAFECHECKS
if ((x >= nx) || (y >= ny)) {
throw IndexOutOfBoundsException("", AT);
std::stringstream ss;
ss << "Trying to access array(" << x << "," << y << ")";
throw IndexOutOfBoundsException(ss.str(), AT);
}
#endif
return vecData[x + y*nx];
......@@ -247,11 +259,15 @@ template<class T> Array2D<T>::Array2D(const Array2D<T>& i_array2D, const unsigne
template<class T> void Array2D<T>::subset(const Array2D<T>& i_array2D, const unsigned int& i_nx, const unsigned int& i_ny,
const unsigned int& i_ncols, const unsigned int& i_nrows)
{
if (((i_nx+i_ncols) > i_array2D.nx) || ((i_ny+i_nrows) > i_array2D.ny))
throw IndexOutOfBoundsException("Trying to cut an array to a size bigger than its original size!", AT);
if (((i_nx+i_ncols) > i_array2D.nx) || ((i_ny+i_nrows) > i_array2D.ny)) {
std::stringstream ss;
ss << "Trying to cut an array of size (" << nx << "," << ny << ") ";
ss << "to size (" << i_ncols << "," << i_nrows << ") starting at (" << i_nx << "," << i_ny << ")";
throw IndexOutOfBoundsException(ss.str(), AT);
}
if ((i_ncols == 0) || (i_nrows == 0)) //the plane to copy has to make sense
throw IndexOutOfBoundsException("Copying an array into a null sized array!", AT);
throw IndexOutOfBoundsException("Trying to cut an array into a null sized array!", AT);
resize(i_ncols, i_nrows); //create new Array2D object
if(i_array2D.keep_nodata==false)
......@@ -275,11 +291,16 @@ template<class T> void Array2D<T>::fill(const Array2D<T>& i_array2D, const unsig
template<class T> void Array2D<T>::fill(const Array2D<T>& i_array2D, const unsigned int& i_nx, const unsigned int& i_ny,
const unsigned int& i_ncols, const unsigned int& i_nrows)
{
if (((i_nx+i_ncols) > nx) || ((i_ny+i_nrows) > ny))
throw IndexOutOfBoundsException("Trying to insert an array whose size is too big!", AT);
if (((i_nx+i_ncols) > nx) || ((i_ny+i_nrows) > ny)) {
std::stringstream ss;
ss << "Filling an array of size (" << nx << "," << ny << ") ";
ss << "with an array of size (" << i_ncols << "," << i_nrows << ") ";
ss << "starting at (" << i_nx << "," << i_ny << ")";
throw IndexOutOfBoundsException(ss.str(), AT);
}
if ((i_ncols == 0) || (i_nrows == 0)) //the plane to copy has to make sense
throw IndexOutOfBoundsException("Copying a null sized array!", AT);
throw IndexOutOfBoundsException("Filling an array with a null sized array!", AT);
if(i_array2D.keep_nodata==false)
setKeepNodata(false);
......@@ -476,8 +497,12 @@ template<class T> Array2D<T>& Array2D<T>::operator=(const T& value) {
template<class T> Array2D<T>& Array2D<T>::operator+=(const Array2D<T>& rhs)
{
//They have to have equal size
if ((rhs.nx != nx) || (rhs.ny != ny))
throw IOException("Trying to add two Array2D objects with different dimensions", AT);
if ((rhs.nx != nx) || (rhs.ny != ny)) {
std::stringstream ss;
ss << "Trying to add two Array2D objects with different dimensions: ";
ss << "(" << nx << "," << ny << ") + (" << rhs.nx << "," << rhs.ny << ")";
throw IOException(ss.str(), AT);
}
const unsigned int nxy = nx*ny;
//Add to every single member of the Array2D<T>
......@@ -533,9 +558,12 @@ template<class T> const Array2D<T> Array2D<T>::operator+(const T& rhs)
template<class T> Array2D<T>& Array2D<T>::operator-=(const Array2D<T>& rhs)
{
//They have to have equal size
if ((rhs.nx != nx) || (rhs.ny != ny))
throw IOException("Trying to substract two Array2D objects with different dimensions", AT);
if ((rhs.nx != nx) || (rhs.ny != ny)){
std::stringstream ss;
ss << "Trying to substract two Array2D objects with different dimensions: ";
ss << "(" << nx << "," << ny << ") - (" << rhs.nx << "," << rhs.ny << ")";
throw IOException(ss.str(), AT);
}
//Substract to every single member of the Array2D<T>
const unsigned int nxy = nx*ny;
......@@ -590,9 +618,12 @@ template<class T> const Array2D<T> Array2D<T>::operator-(const T& rhs)
template<class T> Array2D<T>& Array2D<T>::operator*=(const Array2D<T>& rhs)
{
//They have to have equal size
if ((rhs.nx != nx) || (rhs.ny != ny))
throw IOException("Trying to multiply two Array2D objects with different dimensions", AT);
if ((rhs.nx != nx) || (rhs.ny != ny)){
std::stringstream ss;
ss << "Trying to multiply two Array2D objects with different dimensions: ";
ss << "(" << nx << "," << ny << ") * (" << rhs.nx << "," << rhs.ny << ")";
throw IOException(ss.str(), AT);
}
//Add to every single member of the Array2D<T>
const unsigned int nxy = nx*ny;
......@@ -648,9 +679,12 @@ template<class T> const Array2D<T> Array2D<T>::operator*(const T& rhs)
template<class T> Array2D<T>& Array2D<T>::operator/=(const Array2D<T>& rhs)
{
//They have to have equal size
if ((rhs.nx != nx) || (rhs.ny != ny))
throw IOException("Trying to divide two Array2D objects with different dimensions", AT);
if ((rhs.nx != nx) || (rhs.ny != ny)){
std::stringstream ss;
ss << "Trying to divide two Array2D objects with different dimensions: ";
ss << "(" << nx << "," << ny << ") / (" << rhs.nx << "," << rhs.ny << ")";
throw IOException(ss.str(), AT);
}
//Divide every single member of the Array2D<T>
const unsigned int nxy = nx*ny;
......
......@@ -241,17 +241,27 @@ template<class T> class Array3D {
};
template<class T> T& Array3D<T>::operator()(const unsigned int& i) {
#ifndef NOSAFECHECKS
return vecData.at(i);
#else
return vecData[i];
#endif
}
template<class T> const T Array3D<T>::operator()(const unsigned int& i) const {
#ifndef NOSAFECHECKS
return vecData.at(i);
#else
return vecData[i];
#endif
}
template<class T> T& Array3D<T>::operator()(const unsigned int& x, const unsigned int& y, const unsigned int& z) {
#ifndef NOSAFECHECKS
if ((x >= nx) || (y >= ny) || (z >= nz)) {
throw IndexOutOfBoundsException("", AT);
std::stringstream ss;
ss << "Trying to access array(" << x << "," << y << "," << z << ")";
throw IndexOutOfBoundsException(ss.str(), AT);
}
#endif
......@@ -262,7 +272,9 @@ template<class T> T& Array3D<T>::operator()(const unsigned int& x, const unsigne
template<class T> const T Array3D<T>::operator()(const unsigned int& x, const unsigned int& y, const unsigned int& z) const {
#ifndef NOSAFECHECKS
if ((x >= nx) || (y >= ny) || (z >= nz)) {
throw IndexOutOfBoundsException("", AT);
std::stringstream ss;
ss << "Trying to access array(" << x << "," << y << "," << z << ")";
throw IndexOutOfBoundsException(ss.str(), AT);
}
#endif
return vecData[x + y*nx + z*nxny];
......@@ -290,11 +302,16 @@ template<class T> void Array3D<T>::subset(const Array3D<T>& i_array3D,
const unsigned int& i_ncols, const unsigned int& i_nrows, const unsigned int& i_ndepth)
{
if (((i_nx+i_ncols) > i_array3D.nx) || ((i_ny+i_nrows) > i_array3D.ny) || ((i_nz+i_ndepth) > i_array3D.nz))
throw IndexOutOfBoundsException("Trying to cut an array to a size bigger than its original size!", AT);
if (((i_nx+i_ncols) > i_array3D.nx) || ((i_ny+i_nrows) > i_array3D.ny) || ((i_nz+i_ndepth) > i_array3D.nz)) {
std::stringstream ss;
ss << "Trying to cut an array of size (" << nx << "," << ny << "," << nz << ") ";
ss << "to size (" << i_ncols << "," << i_nrows << "," << i_ndepth << ") ";
ss << "starting at (" << i_nx << "," << i_ny << "," << i_nz << ")";
throw IndexOutOfBoundsException(ss.str(), AT);
}
if ((i_ncols == 0) || (i_nrows == 0) || (i_ndepth == 0)) //the space has to make sense
throw IndexOutOfBoundsException("Copying an array into a null sized array!", AT);
throw IndexOutOfBoundsException("Trying to cut an array into a null sized array!", AT);
resize(i_ncols, i_nrows, i_ndepth); //create new Array3D object
if(i_array3D.keep_nodata==false)
......@@ -323,11 +340,16 @@ template<class T> void Array3D<T>::fill(const Array3D<T>& i_array3D,
const unsigned int& i_ncols, const unsigned int& i_nrows, const unsigned int& i_ndepth)
{
if (((i_nx+i_ncols) > i_array3D.nx) || ((i_ny+i_nrows) > i_array3D.ny) || ((i_nz+i_ndepth) > i_array3D.nz))
throw IndexOutOfBoundsException("Trying to insert an array whose size is too big!", AT);
if (((i_nx+i_ncols) > i_array3D.nx) || ((i_ny+i_nrows) > i_array3D.ny) || ((i_nz+i_ndepth) > i_array3D.nz)) {
std::stringstream ss;
ss << "Filling an array of size (" << nx << "," << ny << "," << nz << ") ";
ss << "with an array of size (" << i_ncols << "," << i_nrows << "," << i_ndepth << ") ";
ss << "starting at (" << i_nx << "," << i_ny << "," << i_nz << ")";
throw IndexOutOfBoundsException(ss.str(), AT);
}
if ((i_ncols == 0) || (i_nrows == 0) || (i_ndepth == 0)) //the space has to make sense
throw IndexOutOfBoundsException("Copying a null sized array!", AT);
throw IndexOutOfBoundsException("Filling an array with a null sized array!", AT);
if(i_array3D.keep_nodata==false)
setKeepNodata(false);
......@@ -533,9 +555,12 @@ template<class T> Array3D<T>& Array3D<T>::operator=(const T& value) {
template<class T> Array3D<T>& Array3D<T>::operator+=(const Array3D<T>& rhs)
{
//They have to have equal size
if ((rhs.nx != nx) || (rhs.ny != ny) || (rhs.nz != nz))
throw IOException("Trying to add two Array3D objects with different dimensions", AT);
if ((rhs.nx != nx) || (rhs.ny != ny) || (rhs.nz != nz)) {
std::stringstream ss;
ss << "Trying to add two Array3D objects with different dimensions: ";
ss << "(" << nx << "," << ny << "," << nz << ") + (" << rhs.nx << "," << rhs.ny << "," << rhs.nz << ")";
throw IOException(ss.str(), AT);
}
//Add to every single member of the Array3D<T>
const unsigned int nxyz = nx*ny*nz;
if(keep_nodata==false) {
......@@ -591,9 +616,12 @@ template<class T> const Array3D<T> Array3D<T>::operator+(const T& rhs)
template<class T> Array3D<T>& Array3D<T>::operator-=(const Array3D<T>& rhs)
{
//They have to have equal size
if ((rhs.nx != nx) || (rhs.ny != ny) || (rhs.nz != nz))
throw IOException("Trying to substract two Array3D objects with different dimensions", AT);
if ((rhs.nx != nx) || (rhs.ny != ny) || (rhs.nz != nz)) {
std::stringstream ss;
ss << "Trying to substract two Array3D objects with different dimensions: ";
ss << "(" << nx << "," << ny << "," << nz << ") - (" << rhs.nx << "," << rhs.ny << "," << rhs.nz << ")";
throw IOException(ss.str(), AT);
}
//Substract to every single member of the Array3D<T>
const unsigned int nxyz = nx*ny*nz;
if(keep_nodata==false) {
......@@ -649,9 +677,12 @@ template<class T> const Array3D<T> Array3D<T>::operator-(const T& rhs)
template<class T> Array3D<T>& Array3D<T>::operator*=(const Array3D<T>& rhs)
{
//They have to have equal size
if ((rhs.nx != nx) || (rhs.ny != ny) || (rhs.nz != nz))
throw IOException("Trying to multiply two Array3D objects with different dimensions", AT);
if ((rhs.nx != nx) || (rhs.ny != ny) || (rhs.nz != nz)) {
std::stringstream ss;
ss << "Trying to multiply two Array3D objects with different dimensions: ";
ss << "(" << nx << "," << ny << "," << nz << ") * (" << rhs.nx << "," << rhs.ny << "," << rhs.nz << ")";
throw IOException(ss.str(), AT);
}
//Multiply every single member of the Array3D<T>
const unsigned int nxyz = nx*ny*nz;
if(keep_nodata==false) {
......@@ -707,9 +738,12 @@ template<class T> const Array3D<T> Array3D<T>::operator*(const T& rhs)
template<class T> Array3D<T>& Array3D<T>::operator/=(const Array3D<T>& rhs)
{
//They have to have equal size
if ((rhs.nx != nx) || (rhs.ny != ny) || (rhs.nz != nz))
throw IOException("Trying to divide two Array3D objects with different dimensions", AT);
if ((rhs.nx != nx) || (rhs.ny != ny) || (rhs.nz != nz)) {
std::stringstream ss;
ss << "Trying to divide two Array3D objects with different dimensions: ";
ss << "(" << nx << "," << ny << "," << nz << ") / (" << rhs.nx << "," << rhs.ny << "," << rhs.nz << ")";
throw IOException(ss.str(), AT);
}
//Divide every single member of the Array3D<T>
const unsigned int nxyz = nx*ny*nz;
if(keep_nodata==false) {
......
......@@ -595,7 +595,6 @@ void Matrix::solve(const Matrix& A, const Matrix& B, Matrix& X) {
X.resize(n,m); //we need to ensure that X has the correct dimensions
for(unsigned int i=n; i>=1; i--) { //lines
if(IOUtils::checkEpsilonEquality(U(i,i), 0., epsilon)) { //HACK: actually, only U(n,n) needs checking
std::cout << "Matrix A=" << A;
throw IOException("The given matrix is singular and can not be inversed", AT);
}
for(unsigned int j=1; j<=m; j++) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment