/************************************************************************** * darray.h * * Array class * * Constructor, copy constructor, destructor, operator overloading * function overloading, reference type * **************************************************************************/ #ifndef G__DARRAY_H #define G__DARRAY_H /********************************************************** * definition of array class **********************************************************/ int G__arraysize = 100; class array { int malloced; public: double *dat; // pointer to data array int n; // number of data array(double start,double stop,int ndat); array(double x); array(array& X); array(void); array(array& X,int offset,int ndat); array(double *p,int ndat,int isnew=0); ~array(); array& operator =(array& a); array operator()(int from,int to); double& operator[](int index); int resize(int size); int getsize() { return(n); } } ; /*********************************************** * Destructor ***********************************************/ array::~array() { if(malloced) { delete[] dat; } } /*********************************************** * Copy constructor ***********************************************/ array::array(array& X) { int i; if(X.malloced) { dat = new double[X.n]; memcpy(dat,X.dat,X.n*sizeof(double)); n = X.n; malloced=1; } else { dat=X.dat; n = X.n; malloced=0; } } /*********************************************** * Implicit conversion constructor ***********************************************/ array::array(double x) { if(G__arraysize==0) { cerr << "Error: Size of array 0\n"; return; } dat = new double[G__arraysize]; G__ary_assign(dat,x,x,G__arraysize); n=G__arraysize; malloced=1; } /*********************************************** * Constructor ***********************************************/ array::array(double start,double stop,int ndat) { double res; G__arraysize=ndat; dat = new double[G__arraysize]; G__ary_assign(dat,start,stop,G__arraysize); n = G__arraysize; malloced=1; } /*********************************************** * constructor ***********************************************/ array::array(void) { if(G__arraysize==0) { cerr << "Error: Size of array 0\n"; return; } dat = new double[G__arraysize]; n=G__arraysize; malloced=1; } /*********************************************** * constructor ***********************************************/ array::array(double *p,int ndat,int isnew) { G__arraysize=ndat; if(isnew==0) { dat = p; n = G__arraysize; malloced=0; } else { dat = new double[ndat]; memcpy(dat,p,ndat*sizeof(double)); n=G__arraysize; malloced=1; } } /*********************************************** * constructor for rvalue subarray ***********************************************/ array::array(array& X,int offset,int ndat) { int i; dat = new double[ndat]; if(offset+ndat>X.n) { memcpy(dat,X.dat+offset,(X.n-offset)*sizeof(double)); for(i=X.n-offset;in) { temp = new double[size]; memset(temp,0,sizeof(double)*size); memcpy(temp,dat,sizeof(double)*n); if(malloced) delete[] dat; dat=temp; n=size; malloced=1; } return(n); } /********************************************************** * operator = as member function **********************************************************/ array& array::operator =(array& a) { int i; if(malloced && a.malloced) { if(a.n> (shift) ***********************************************/ array operator >>(array& a,int shift) { array c=array(0.0 , 0.0 , a.n); int i; for(i=0;i #endif #endif