/* -*- C++ -*- */ /************************************************************************* * Copyright(c) 1995~2005 Masaharu Goto (root-cint@cern.ch) * * For the licensing terms see the file COPYING * ************************************************************************/ /************************************************************************** * Array.h * * Array class template * **************************************************************************/ #ifndef ARRAY_H #define ARRAY_H #include #include #include #include #include "Fundament.h" // #define G__GARIONIS #if defined(_WINDOWS) || defined(_WIN32) || defined(__GNUC__) #define G__NOSTATICMEMBER extern int G__defaultsize; #endif #if defined(_WINDOWS) || defined(_WIN32) #define G__NOFRIENDS #endif template class Array; template Array operator+(Array& a,Array& b); template Array operator-(Array& a,Array& b); template Array operator*(Array& a,Array& b); template Array operator/(Array& a,Array& b); template Array exp(Array& a); template Array abs(Array& a); /********************************************************** * definition of array class **********************************************************/ template class Array { public: Array(T start,T stop,int ndat); Array(T x); Array(Array const & X); Array(void); Array(Array& X,int offset,int ndat); ~Array(); Array& operator =(Array& a); Array operator()(int from,int to); T& operator[](int index); int getsize(void) { return(n); } int resize(int size); static void setdefaultsize(int size) { G__defaultsize = size; } // Ralf Garionis's bug report T*& newdat(T* d); void disp(ostream& ostr=std::cout); #ifndef __CINT__ friend Array operator+(Array& a,Array& b); friend Array operator-(Array& a,Array& b); friend Array operator*(Array& a,Array& b); friend Array operator/(Array& a,Array& b); friend Array exp(Array& a); friend Array abs(Array& a); #endif #ifdef NOT_READY_YET friend Array log(Array& a); friend Array log10(Array& a); friend Array sinc(Array& a); friend Array sin(Array& a); friend Array cos(Array& a); friend Array tan(Array& a); friend Array asin(Array& a); friend Array acos(Array& a); friend Array atan(Array& a); friend Array sinh(Array& a); friend Array cosh(Array& a); friend Array tanh(Array& a); friend Array sqrt(Array& a); friend Array rect(Array& a); friend Array square(Array& a); friend Array rand(Array& a); friend Array conv(Array& a,Array& b); friend Array integ(Array& x,Array& y); friend Array diff(Array& x,Array& y); #endif #ifndef G__NOFRIENDS private: #endif int n; // number of data T *dat; // pointer to data Array #ifndef G__NOSTATICMEMBER static int G__defaultsize; #endif int malloced; enum { ISOLD, ISNEW }; Array(T *p,int ndat,int isnew /* =0 */); public: #if 0 operator T () const; operator T* () const; #endif } ; /*********************************************** * Ralf Garionis's bug report ***********************************************/ template T*& Array::newdat(T* d) { dat = d; return dat; } #if 0 /*********************************************** * conversion (scalar type) ***********************************************/ template Array::operator T () const { printf("###\n"); return dat[0]; } /*********************************************** * conversion (pointer type) ***********************************************/ template Array::operator T* () const { return dat; } #endif #ifndef G__NOSTATICMEMBER template int Array::G__defaultsize = 100; #endif /*********************************************** * Destructor ***********************************************/ template Array::~Array() { if(malloced) delete[] dat; } /*********************************************** * Copy constructor ***********************************************/ template Array::Array(Array const & X) { if(X.malloced) { dat = new T[X.n]; memcpy(dat,X.dat,X.n*sizeof(T)); n = X.n; malloced=1; } else { dat=X.dat; n = X.n; malloced=0; } } /*********************************************** * Implicit conversion constructor ***********************************************/ template Array::Array(T x) { n=G__defaultsize; dat = new T[n]; malloced=1; for(int i=0;i Array::Array(T start,T stop,int ndat) { if(ndat<=0) { cerr << "Error: Size of array 0>=" << ndat << ". default " << G__defaultsize << "used.\n"; } else { G__defaultsize=ndat; } n = ndat; dat = new T[n]; malloced=1; T res ; res = (stop-start)/(n-1); for(int i=0;i Array::Array(void) { n=G__defaultsize; dat = new T[n]; malloced=1; } /*********************************************** * constructor ***********************************************/ template Array::Array(T *p,int ndat,int isnew) { if(ndat<=0) { cerr << "Error: Size of array 0>=" << ndat << ". default " << G__defaultsize << "used.\n"; } else { G__defaultsize=ndat; } if(0==isnew) { dat = p; n = G__defaultsize; malloced=0; } else { n=G__defaultsize; dat = new T[ndat]; malloced=1; memcpy(dat,p,ndat*sizeof(T)); } } /*********************************************** * constructor for rvalue subArray ***********************************************/ template Array::Array(Array& X,int offset,int ndat) { int i; if(offset<0||offset>=X.n) { cerr << "Illegal offset. Set to 0\n"; offset = 0; } if(ndat<=0) { n=X.n-offset; } else { n = ndat; } dat = new T[n]; malloced=1; if(offset+n>X.n) { memcpy(dat,X.dat+offset,(X.n-offset)*sizeof(T)); for(i=X.n-offset;i int Array::resize(int size) { if(size<=0) { cerr << "Resize failed. Size of array 0>=" << size ; } else if(size!=n) { if(malloced) delete[] dat; n=size; malloced=1; dat=new T[n]; } return(n); } /********************************************************** * operator = as member function **********************************************************/ template Array& Array::operator =(Array& a) { if(malloced && a.malloced) { if(a.n c=Array(a.dat,a.n,ISNEW); if(c.n Array Array::operator()(int from,int to) { if(from<0 || n<=to) { fprintf(stderr,"Error: Array index out of range (%d,%d),%d\n" ,from,to,n); return(*this); } else { Array c=Array(dat+from,to-from+1,ISOLD); return(c); } } /********************************************************** * operator [] as member function **********************************************************/ template T& Array::operator[](int index) { if(index<0||n<=index) { fprintf(stderr,"Error: Array index out of range %d/%d\n" ,index,n); return(dat[0]); } return(dat[index]); } /********************************************************** * disp() **********************************************************/ template void Array::disp(ostream& ostr) { ostr << "size=" << n << "\n"; for(int i=0;i Array operator +(Array& a,Array& b) { int minn; if(a.n c; for(int i=0;i Array operator-(Array& a,Array& b) { int minn; if(a.n c; for(int i=0;i Array operator*(Array& a,Array& b) { int minn; if(a.n c; for(int i=0;i Array operator /(Array& a,Array& b) { int minn; if(a.n c; for(int i=0;i Array exp(Array& a) { a.setdefaultsize(a.n); Array c; for(int i=0;i Array abs(Array& a) { a.setdefaultsize(a.n); Array c; for(int i=0;i Array log(Array& a) { a.setdefaultsize(a.n); Array c; for(int i=0;i Array log10(Array& a) { a.setdefaultsize(a.n); Array c; for(int i=0;i Array sinc(Array& a) { a.setdefaultsize(a.n); Array c; for(int i=0;i Array sin(Array& a) { a.setdefaultsize(a.n); Array c; for(int i=0;i Array cos(Array& a) { a.setdefaultsize(a.n); Array c; for(int i=0;i Array tan(Array& a) { a.setdefaultsize(a.n); Array c; for(int i=0;i Array asin(Array& a) { a.setdefaultsize(a.n); Array c; for(int i=0;i Array acos(Array& a) { a.setdefaultsize(a.n); Array c; for(int i=0;i Array atan(Array& a) { a.setdefaultsize(a.n); Array c; for(int i=0;i Array sinh(Array& a) { a.setdefaultsize(a.n); Array c; for(int i=0;i Array cosh(Array& a) { a.setdefaultsize(a.n); Array c; for(int i=0;i Array tanh(Array& a) { a.setdefaultsize(a.n); Array c; for(int i=0;i Array sqrt(Array& a) { a.setdefaultsize(a.n); Array c; for(int i=0;i Array rect(Array& a) { a.setdefaultsize(a.n); Array c; for(int i=0;i Array square(Array& a) { a.setdefaultsize(a.n); Array c; for(int i=0;i Array rand(Array& a) { a.setdefaultsize(a.n); Array c; for(int i=0;i Array conv(Array& a,Array& b) { a.setdefaultsize(a.n); Array c; int i,j,k; int f,t; f = b.n/2; t = b.n-f; for(i=0;i=a.n) c[i] += a[a.n-1]*b[j]; else c[i] += a[k]*b[j]; } } return(c); } /*********************************************** * integ ***********************************************/ template Array integ(Array& x,Array& y) { x.setdefaultsize(x.n); Array c; int i; T integ=0; for(i=0;i Array diff(Array& x,Array& y) { x.setdefaultsize(x.n); Array c; int i; T integ=0; for(i=0;i ostream& operator<<(ostream& ostr,Array& x) { x.disp(ostr); return(ostr); } /************************************************************************** * int dummy **************************************************************************/ Array exp(Array& a); /************************************************************************** * template instanciation **************************************************************************/ typedef Array iarray; typedef Array darray; typedef Array carray; // ostream& operator<<(ostream& ostr,Array& x); // ostream& operator<<(ostream& ostr,Array& x); // ostream& operator<<(ostream& ostr,Array& x); #ifdef G__GARIONIS // Ralf Garionis's bug report template class Matrix : public Array { public: Matrix() { } Matrix(double a) { } Matrix(int a) { } friend Matrix operator-(Matrix& a,Matrix& b); friend Matrix operator+(Matrix& a,Matrix& b); friend Matrix operator*(Matrix& a,Matrix& b); friend Matrix operator/(Matrix& a,Matrix& b); }; template Matrix operator-(Matrix& a,Matrix& b) {Matrix c; return(c);} template Matrix operator+(Matrix& a,Matrix& b) {Matrix c; return(c);} template Matrix operator*(Matrix& a,Matrix& b) {Matrix c; return(c);} template Matrix operator/(Matrix& a,Matrix& b) {Matrix c; return(c);} typedef Matrix imatrix; typedef Matrix dmatrix; typedef Matrix cmatrix; // another bug report from Ralf Garionis typedef Array iiarray; typedef Array > aiarray; typedef Array > miarray; #endif #endif