From 898645ed94c24201561821edd7cf29ded19c682d Mon Sep 17 00:00:00 2001 From: Jiri Pittner Date: Sat, 15 Nov 2025 20:57:57 +0100 Subject: [PATCH] improved out of range diagnostics --- mat.h | 38 +++++++++++++++++++++++++++++--------- smat.h | 27 +++++++++++++++++++++------ vec.h | 12 ++++++++---- 3 files changed, 58 insertions(+), 19 deletions(-) diff --git a/mat.h b/mat.h index d50c384..e0d9a3f 100644 --- a/mat.h +++ b/mat.h @@ -744,7 +744,8 @@ template inline T* NRMat::operator[](const int i) { #ifdef DEBUG if (_LA_count_check && *count != 1) laerror("matrix with *count>1 used as l-value"); - if (i < 0 || i >= nn) laerror("Mat [] out of range"); + if (i < 0) laerror("Mat [] out of range - low"); + if (i >= nn) laerror("Mat [] out of range - high"); if (!v) laerror("unallocated matrix"); #endif #ifdef MATPTR @@ -761,7 +762,8 @@ inline T* NRMat::operator[](const int i) { template inline const T* NRMat::operator[](const int i) const { #ifdef DEBUG - if (i < 0 || i >= nn) laerror("index out of range"); + if (i < 0) laerror("index out of range - low"); + if (i >= nn) laerror("index out of range - high"); if (!v) laerror("unallocated matrix"); #endif NOT_GPU(*this); @@ -783,7 +785,10 @@ template inline T& NRMat::operator()(const int i, const int j){ #ifdef DEBUG if (_LA_count_check && *count != 1) laerror("NRMat::operator(,) used as l-value for a matrix with count > 1"); - if (i < 0 || i >= nn && nn > 0 || j < 0 || j >= mm && mm > 0) laerror("index out of range"); + if (i < 0) laerror("first index out of range - low"); + if (i >= nn) laerror("first index out of range - high"); + if (j < 0) laerror("second index out of range - low"); + if (j >= mm) laerror("second index out of range - high"); if (!v) laerror("unallocated matrix"); #endif NOT_GPU(*this); @@ -804,7 +809,10 @@ template inline const T& NRMat::operator()(const int i, const int j) const{ T ret; #ifdef DEBUG - if (i<0 || i>=nn && nn>0 || j<0 || j>=mm && mm>0) laerror("index out of range"); + if (i < 0) laerror("first index out of range - low"); + if (i >= nn) laerror("first index out of range - high"); + if (j < 0) laerror("second index out of range - low"); + if (j >= mm) laerror("second index out of range - high"); if (!v) laerror("unallocated matrix"); #endif NOT_GPU(*this); @@ -825,7 +833,10 @@ template inline const T NRMat::get_ij(const int i, const int j) const{ T ret; #ifdef DEBUG - if (i<0 || i>=nn || j<0 || j>=mm) laerror("index out of range"); + if (i < 0) laerror("first index out of range - low"); + if (i >= nn) laerror("first index out of range - high"); + if (j < 0) laerror("second index out of range - low"); + if (j >= mm) laerror("second index out of range - high"); if (!v) laerror("unallocated matrix"); #endif #ifdef CUDALA @@ -1388,8 +1399,11 @@ public: inline const T& operator() (const int i, const int j) const { #ifdef DEBUG - if (i<1 || i>NRMat::nn || j<1 || j>NRMat::mm) laerror("index out of range"); - if (!NRMat::v) laerror("unallocated matrix"); + if (i < 1) laerror("first index out of range - low"); + if (i > NRMat::nn) laerror("first index out of range - high"); + if (j < 1) laerror("second index out of range - low"); + if (j > NRMat::mm) laerror("second index out of range - high"); + if (!NRMat::v) laerror("unallocated matrix"); #endif NOT_GPU(*this); #ifdef MATPTR @@ -1402,7 +1416,10 @@ public: inline T& operator() (const int i, const int j) { #ifdef DEBUG if (_LA_count_check && *NRMat::count != 1) laerror("matrix with *count > 1 used as l-value"); - if (i<1 || i>NRMat::nn || j<1 || j>NRMat::mm) laerror("index out of range"); + if (i < 1) laerror("first index out of range - low"); + if (i > NRMat::nn) laerror("first index out of range - high"); + if (j < 1) laerror("second index out of range - low"); + if (j > NRMat::mm) laerror("second index out of range - high"); if (!NRMat::v) laerror("unallocated matrix"); #endif NOT_GPU(*this); @@ -1416,7 +1433,10 @@ public: inline const T get_ij(const int i, const int j) const { T ret; #ifdef DEBUG - if (i<1 || i>NRMat::nn || j<1 || j>NRMat::mm) laerror("index out of range"); + if (i < 1) laerror("first index out of range - low"); + if (i > NRMat::nn) laerror("first index out of range - high"); + if (j < 1) laerror("second index out of range - low"); + if (j > NRMat::mm) laerror("second index out of range - high"); if (!NRMat::v) laerror("unallocated matrix"); #endif #ifdef CUDALA diff --git a/smat.h b/smat.h index 085220a..b51fef7 100644 --- a/smat.h +++ b/smat.h @@ -626,7 +626,8 @@ template inline T& NRSMat::operator[](const size_t ij) { #ifdef DEBUG if(_LA_count_check && *count != 1) laerror("T& NRSMat::operator[] used for matrix with count>1"); - if(ij<0 || ij>=NN2) laerror("T& NRSMat::operator[] out of range"); + if(ij<0) laerror("T& NRSMat::operator[] out of range - low"); + if(ij>=NN2) laerror("T& NRSMat::operator[] out of range - high"); if(!v) laerror("T& NRSMat::operator[] used for unallocated NRSmat object"); #endif NOT_GPU(*this); @@ -645,7 +646,8 @@ inline T& NRSMat::operator[](const size_t ij) { template inline const T & NRSMat::operator[](const size_t ij) const { #ifdef DEBUG - if(ij<0 || ij>=NN2) laerror("T& NRSMat::operator[] out of range"); + if(ij<0) laerror("T& NRSMat::operator[] out of range - low"); + if(ij>=NN2) laerror("T& NRSMat::operator[] out of range - high"); if(!v) laerror("T& NRSMat::operator[] used for unallocated NRSmat object"); #endif NOT_GPU(*this); @@ -752,7 +754,10 @@ template inline T & NRSMat::operator()(const int i, const int j) { #ifdef DEBUG if(_LA_count_check && *count != 1) laerror("T & NRSMat::operator()(const int, const int) used for matrix with count > 1"); - if(i<0 || i>=nn || j<0 || j>=nn) laerror("T & NRSMat::operator()(const int, const int) out of range"); + if(i<0) laerror("T & NRSMat::operator()(const int, const int) first index out of range - low"); + if(i>=nn) laerror("T & NRSMat::operator()(const int, const int) first index out of range - high"); + if(j<0) laerror("T & NRSMat::operator()(const int, const int) second index out of range - low"); + if(j>=nn) laerror("T & NRSMat::operator()(const int, const int) second index out of range - high"); if(!v) laerror("T & NRSMat::operator()(const int, const int) used for unallocated NRSmat object"); #endif NOT_GPU(*this); @@ -770,7 +775,11 @@ inline T & NRSMat::operator()(const int i, const int j) { template inline const T & NRSMat::operator()(const int i, const int j) const { #ifdef DEBUG - if(i<0 || i>=nn || j<0 || j>=nn) laerror("T & NRSMat::operator()(const int, const int) out of range"); + if(i<0) laerror("T & NRSMat::operator()(const int, const int) first index out of range - low"); + if(i>=nn) laerror("T & NRSMat::operator()(const int, const int) first index out of range - high"); + if(j<0) laerror("T & NRSMat::operator()(const int, const int) second index out of range - low"); + if(j>=nn) laerror("T & NRSMat::operator()(const int, const int) second index out of range - high"); + if(!v) laerror("T & NRSMat::operator()(const int, const int) used for unallocated NRSmat object"); if(!v) laerror("T & NRSMat::operator()(const int, const int) used for unallocated NRSmat object"); #endif NOT_GPU(*this); @@ -1304,14 +1313,20 @@ public: inline const T& operator() (const int i, const int j) const { #ifdef DEBUG - if(i<=0||j<=0||i>NRSMat::nn||j>NRSMat::nn) laerror("index in const T& NRSMat::operator() (const int, const int) out of range"); + if(i<=0) laerror("index in const T& NRSMat_from1::operator() (const int, const int) first indexout of range - low"); + if(i>NRSMat::nn) laerror("index in const T& NRSMat_from1::operator() (const int, const int) first indexout of range - high"); + if(j<=0) laerror("index in const T& NRSMat_from1::operator() (const int, const int) second index out of range - low"); + if(j>NRSMat::nn) laerror("index in const T& NRSMat_from1::operator() (const int, const int) second index out of range - high"); #endif return NRSMat::v[SMat_index_1(i,j)]; } inline T& operator() (const int i, const int j){ #ifdef DEBUG - if(i<=0||j<=0||i>NRSMat::nn||j>NRSMat::nn) laerror("index in T& NRSMat::operator() (const int, const int) out of range"); + if(i<=0) laerror("index in const T& NRSMat_from1::operator() (const int, const int) first indexout of range - low"); + if(i>NRSMat::nn) laerror("index in const T& NRSMat_from1::operator() (const int, const int) first indexout of range - high"); + if(j<=0) laerror("index in const T& NRSMat_from1::operator() (const int, const int) second index out of range - low"); + if(j>NRSMat::nn) laerror("index in const T& NRSMat_from1::operator() (const int, const int) second index out of range - high"); #endif return NRSMat::v[SMat_index_1(i,j)]; } diff --git a/vec.h b/vec.h index 5c90867..e0d176a 100644 --- a/vec.h +++ b/vec.h @@ -620,7 +620,8 @@ template inline T& NRVec_from1::operator[](const int i) { #ifdef DEBUG if(_LA_count_check && *NRVec::count != 1) laerror("possible use of NRVec[] with count>1 as l-value"); - if(i < 1 || i > NRVec::nn) laerror("out of range"); + if(i < 1) laerror("out of range - low"); + if(i > NRVec::nn) laerror("out of range - high"); if(!NRVec::v) laerror("unallocated NRVec"); #endif NOT_GPU(*this); @@ -637,7 +638,8 @@ inline T& NRVec_from1::operator[](const int i) { template inline const T& NRVec_from1::operator[](const int i) const { #ifdef DEBUG - if(i < 1 || i > NRVec::nn) laerror("out of range"); + if(i < 1) laerror("out of range - low"); + if(i > NRVec::nn) laerror("out of range - high"); if(!NRVec::v) laerror("unallocated NRVec"); #endif NOT_GPU(*this); @@ -1080,7 +1082,8 @@ template inline T& NRVec::operator[](const int i) { #ifdef DEBUG if(_LA_count_check && *count != 1) laerror("possible use of NRVec[] with count>1 as l-value"); - if(i < 0 || i >= nn) laerror("out of range"); + if(i < 0) laerror("out of range - low"); + if(i >= nn) laerror("out of range - high"); if(!v) laerror("unallocated NRVec"); #endif NOT_GPU(*this); @@ -1097,7 +1100,8 @@ inline T& NRVec::operator[](const int i) { template inline const T& NRVec::operator[](const int i) const { #ifdef DEBUG - if(i < 0 || i >= nn) laerror("out of range"); + if(i < 0) laerror("out of range - low"); + if(i >= nn) laerror("out of range - high"); if(!v) laerror("unallocated NRVec"); #endif NOT_GPU(*this);