efficient swap operation for vector and matrix classes

This commit is contained in:
Jiri Pittner 2021-06-29 14:39:04 +02:00
parent 3288e51fba
commit 9d0249cdc4
5 changed files with 57 additions and 0 deletions

View File

@ -299,6 +299,15 @@ public:
l=l->next; l=l->next;
} }
} }
void swap(fourindex &rhs) //more efficient swap than via tmp and constructors and operator=
{
I tmpnn=nn; nn=rhs.nn; rhs.nn=tmpnn;
I tmpterminator=terminator; terminator=rhs.terminator; rhs.terminator=tmpterminator;
int *tmpcount=count; count=rhs.count; rhs.count=tmpcount;
matel4<I,T> *tmplist=list; list=rhs.list; rhs.list=tmplist;
bool tmpdoscaling=doscaling; doscaling=rhs.doscaling; rhs.doscaling=tmpdoscaling;
fourindexsymtype tmpsymmetry=symmetry; symmetry=rhs.symmetry; rhs.symmetry=tmpsymmetry;
}
}; };

16
mat.h
View File

@ -390,6 +390,22 @@ public:
void strassen(const T beta, const NRMat &a, const char transa, const NRMat &b, const char transb, const T alpha); void strassen(const T beta, const NRMat &a, const char transa, const NRMat &b, const char transb, const T alpha);
void s_cutoff(const int,const int,const int,const int) const; void s_cutoff(const int,const int,const int,const int) const;
#endif #endif
void swap(NRMat &rhs) //more efficient swap than via tmp and constructors and operator=
{
int tmpnn=nn; nn=rhs.nn; rhs.nn=tmpnn;
int tmpmm=mm; mm=rhs.mm; rhs.mm=tmpmm;
#ifdef MATPTR
T **
#else
T *
#endif
tmpv=v; v=rhs.v; rhs.v=tmpv;
int *tmpcount=count; count=rhs.count; rhs.count=tmpcount;
#ifdef CUDALA
GPUID tmplocation=location; location=rhs.location; rhs.location=tmplocation;
#endif
}
}; };
}//namespace }//namespace

10
smat.h
View File

@ -176,6 +176,16 @@ public:
explicit NRSMat(const SparseSMat<T> &rhs); // dense from sparse explicit NRSMat(const SparseSMat<T> &rhs); // dense from sparse
inline void simplify() {}; //just for compatibility with sparse ones inline void simplify() {}; //just for compatibility with sparse ones
bool issymmetric() const {return 1;} bool issymmetric() const {return 1;}
void swap(NRSMat &rhs) //more efficient swap than via tmp and constructors and operator=
{
int tmpnn=nn; nn=rhs.nn; rhs.nn=tmpnn;
T *tmpv=v; v=rhs.v; rhs.v=tmpv;
int *tmpcount=count; count=rhs.count; rhs.count=tmpcount;
#ifdef CUDALA
GPUID tmplocation=location; location=rhs.location; rhs.location=tmplocation;
#endif
}
}; };
}//namespace }//namespace

View File

@ -146,6 +146,17 @@ public:
unsigned int sort(int type) const; unsigned int sort(int type) const;
inline void add(const SPMatindex n, const SPMatindex m, const T elem) {matel<T> *ltmp= new matel<T>; ltmp->next=list; list=ltmp; list->row=n; list->col=m; list->elem=elem;} inline void add(const SPMatindex n, const SPMatindex m, const T elem) {matel<T> *ltmp= new matel<T>; ltmp->next=list; list=ltmp; list->row=n; list->col=m; list->elem=elem;}
void addsafe(const SPMatindex n, const SPMatindex m, const T elem); void addsafe(const SPMatindex n, const SPMatindex m, const T elem);
void swap(SparseMat &rhs) //more efficient swap than via tmp and constructors and operator=
{
SPMatindex tmpnn=nn; nn=rhs.nn; rhs.nn=tmpnn;
SPMatindex tmpmm=mm; mm=rhs.mm; rhs.mm=tmpmm;
int *tmpcount=count; count=rhs.count; rhs.count=tmpcount;
bool tmpsymmetric=symmetric; symmetric=rhs.symmetric; rhs.symmetric=tmpsymmetric;
int tmpnonzero=nonzero; nonzero=rhs.nonzero; rhs.nonzero=tmpnonzero;
matel<T> *tmplist=list; list=rhs.list; rhs.list=tmplist;
matel<T> **tmprowsorted=rowsorted; rowsorted=rhs.rowsorted; rhs.rowsorted=tmprowsorted;
matel<T> **tmpcolsorted=colsorted; colsorted=rhs.colsorted; rhs.colsorted=tmpcolsorted;
}
}; };
}//namespace }//namespace

11
vec.h
View File

@ -383,6 +383,17 @@ public:
for(int i=0; i<nn; ++i) v[i] = _F(v[i]); for(int i=0; i<nn; ++i) v[i] = _F(v[i]);
return *this; return *this;
}; };
void swap(NRVec &rhs) //more efficient swap than via tmp and constructors and operator=
{
int tmpnn=nn; nn=rhs.nn; rhs.nn=tmpnn;
T *tmpv=v; v=rhs.v; rhs.v=tmpv;
int *tmpcount=count; count=rhs.count; rhs.count=tmpcount;
#ifdef CUDALA
GPUID tmplocation=location; location=rhs.location; rhs.location=tmplocation;
#endif
}
}; };