allow trivial copyonwrite() on empty vec/mat/...

This commit is contained in:
Jiri Pittner 2021-11-04 14:21:13 +01:00
parent 9c6ab037cf
commit 6715260da7
7 changed files with 32 additions and 6 deletions

View File

@ -69,7 +69,6 @@ extern "C" {
namespace LA { namespace LA {
extern bool _LA_count_check;
//forward declarations //forward declarations
template<typename C> class NRVec; template<typename C> class NRVec;

View File

@ -44,7 +44,8 @@ namespace LA {
GPU_START gpu_start_instant; GPU_START gpu_start_instant;
#endif #endif
bool _LA_count_check=true; const bool _LA_count_check=true;
const bool _LA_warn_empty_copyonwrite=false;
extern "C" void _findme(void) {}; //for autoconf test we need a function with C linkage extern "C" void _findme(void) {}; //for autoconf test we need a function with C linkage

View File

@ -28,6 +28,10 @@ extern int traceback(int flags);
extern void sigtraceback(int sig,int flags); extern void sigtraceback(int sig,int flags);
} }
extern const bool _LA_warn_empty_copyonwrite;
extern const bool _LA_count_check;
//exception class for laerror //exception class for laerror
class LAerror class LAerror
{ {

8
mat.h
View File

@ -1089,7 +1089,13 @@ NRMat<T> & NRMat<T>::operator|=(const NRMat<T> &rhs) {
******************************************************************************/ ******************************************************************************/
template <typename T> template <typename T>
void NRMat<T>::copyonwrite(bool detachonly) { void NRMat<T>::copyonwrite(bool detachonly) {
if(!count) laerror("attempt to call copyonwrite() for a matrix with count == 0"); if(!count)
{
if(nn||mm) laerror("nonempty matrix without reference count encountered");
if(_LA_warn_empty_copyonwrite) std::cout <<"Warning: copyonwrite of empty matrix\n";
return;
}
if(*count > 1){ if(*count > 1){
(*count)--; (*count)--;
count = new int; count = new int;

7
smat.h
View File

@ -957,7 +957,12 @@ NRSMat<T> & NRSMat<T>::operator=(const NRSMat<T> & rhs) {
******************************************************************************/ ******************************************************************************/
template <typename T> template <typename T>
void NRSMat<T>::copyonwrite(bool detachonly) { void NRSMat<T>::copyonwrite(bool detachonly) {
if(!count) laerror("calling NRSMat<T>::copyonwrite() for undefined NRSMat<T> object"); if(!count)
{
if(nn) laerror("nonempty smat without reference count encountered");
if(_LA_warn_empty_copyonwrite) std::cout <<"Warning: copyonwrite of empty smat\n";
return;
}
if(*count > 1){ if(*count > 1){
(*count)--; (*count)--;
count = new int; count = new int;

View File

@ -145,7 +145,13 @@ while(l)
template <class T> template <class T>
void SparseMat<T>::copyonwrite(bool detachonly) void SparseMat<T>::copyonwrite(bool detachonly)
{ {
if(!count) laerror("probably an assignment to undefined sparse matrix"); if(!count)
{
if(nn||mm) laerror("nonempty sparsemat without reference count encountered");
if(_LA_warn_empty_copyonwrite) std::cout <<"Warning: copyonwrite of empty sparsemat\n";
return;
}
if(*count > 1) if(*count > 1)
{ {
(*count)--; (*count)--;

7
vec.h
View File

@ -1011,7 +1011,12 @@ NRVec<T>::~NRVec() {
******************************************************************************/ ******************************************************************************/
template <typename T> template <typename T>
void NRVec<T>::copyonwrite(bool detachonly) { void NRVec<T>::copyonwrite(bool detachonly) {
if(!count) laerror("copyonwrite of an undefined vector"); if(!count)
{
if(nn) laerror("nonempty vector without reference count encountered");
if(_LA_warn_empty_copyonwrite) std::cout <<"Warning: copyonwrite of empty vector\n";
return;
}
if(*count > 1) { if(*count > 1) {
(*count)--; (*count)--;
count = new int; count = new int;