#if !defined H_ARRAY_H #define H_ARRAY_H #include #include "hbool.h" // HArray can be used to store arbitrary elements of type 'Type' // in one dimensional arrays. // Array size can be defined and redefined during runtime by the // assign functions (old content is lost) or by insert,append,remove functions. // Access to elements or subarrays via the [i] or the (i,j) operators. // Boundary checking is always performed. // Performance is optimized for direct access of elements, not for changing // the size by assign, insert, remove or append operations. // Type specific 'symbolic' code is in harrayfc.h // A HArray containing HArrays as elements behaves like a two dimensional // array with variable row size. // Wolfgang Koenig 4.02.97 (W.Koenig@GSI.de) template class HArray { public: // create an empty 1 dimensional array // needed for statements like p=new HVector []. // Use assign(n) afterwards to create an array with n elements. HArray() : size(0), array(0) {} // create 1 dim array with n elements, initialize all elements with 'x' HArray(Type x, int n); // create one dimensional array with n elements // and initialize with existing array HArray (const Type * anyArray, int n); // create a copy of the array 'hArray' HArray(const HArray& hArray) { size=hArray.size; array=new Type [size]; *this=hArray; } // deallocate memory if it was allocated before ~HArray() {delete [] array;} // allocate memory for a one dimensional array with n elements // Deletes old elements and resizes array, if necessary. // Returns HFAILURE, if n<=0 or memory allocation fails, else HSUCCESS. HArray& assign(int n); // allocate memory for a one dimensional array with n elements // and initialize with 'x'. Deletes old elements, if they exist HArray& assign(const Type& x, int n) {return assign(n)=x;} // allocate memory for n elements and initialize with values of anyArray HArray& assign(const Type * anyArray, int n) {return assign(n)=anyArray;} // allocate memory for n elements and initialize with values of HArray HArray& assign(const HArray& hArray); // copy all elements of array 'hArray'. Resizes left array, if necessary HArray& operator = (const HArray& hArray); // the size of left HArray determines the number of elements // copied from anyArray. Your ploblem, if anyArray is to short. // Use assign(Type *,int n) if you want to adjust the length of the HArray. HArray& operator = (const Type * anyArray); // sets all elements of HArray equal to 'value' HArray& operator = (const Type& value); // delete all elements of the array void free() {delete [] array;array=0;size=0;} // return the number of elements of the array int length() const {return size;} // check (guess what) HBool empty() const {return !size;} // returns element specified by 'index' Type& get(int index) {return (*this)[index];} // returns pointer to element specified by 'index'. Type * getPointer(int index); // access array elements via [index]. Checks boundaries. // Prints error message and exits program, if index is out of range // eg: x=hArray[i]; hArray[i]=y; Type& operator [] (int index); // dito, but using () Type& operator() (int index) {return(*this)[index];} // returns a subset of the array. Subset starts at 'position' and contains // 'nElements' elements. 'nElements' is truncated, if necessary HArray operator() (int position,int nElements); // returns a subset of array consisting of the elements specified by an // index array. Indices can have arbitrary order. HArray operator () (const int * indexArray, int nElements); // inserts one element at specified position. Array remains unchanged, // if 'position' is out of range. HArray& insert(int position, const Type& x); // inserts an array. The numer of elements is increased by the number // of elements of the inserted array HArray& insert(int position, const HArray& hArray); // stores an element x at 'position'. Array remains unchanged, if position // is out of range HArray& replace( int position,const Type& x); // replaces all elements specified by HArray starting at 'position' HArray& replace( int position,const HArray& hArray); // append an element 'x' or an array of elements HArray& append(const Type& x); // append an array of elements HArray& append(const HArray& hArray); // deletes one element or a set of 'nElements' elements from HArray // starting at 'position'. // Array remains unchanged, if position is out of range // The size of HArray is reduced according to the number of deleted elements. HArray& remove(int position, int nElements=1); // executes any function 'func' on all elements of HArray HArray& apply(Type func(Type)); // executes any function 'func' on all elements of HArray HArray& apply(Type* func(Type*)); // executes any function 'func' using all members of 'x' and stores // the result in this vector. Adjusts size automatically. HArray& apply(Type func(Type),HArray & x); // read or print an HArray friend istream& operator >> (istream& get,HArray& h); friend ostream& operator << (ostream& put,const HArray& h); protected: void errIndex(int index); void errSize(int n); int size; Type *array; }; template inline Type& HArray::operator [] (int i) { if(i<0 || i>=size) { errIndex(i); exit(EXIT_FAILURE); } return array[i]; } template inline HArray& HArray::replace(int i, const Type& x) { if(i>=0 && i ostream& operator<< (ostream& put,const HArray &x) { put<<"array size="< istream& operator>> (istream& get,HArray &x) { for(int i=0;i>x.array[i++]); return get; } #endif