#ifndef ARRAYS_HDR #define ARRAYS_HDR #include #include //For Shared Objects: //#include "SafeList.h" //#define TAG :public shObj //#define GetMem CAVEMalloc //#define FreeMem CAVEFree //For Non-shared Objects: #define TAG /* An flexarray is a dynamic flexarray of objecs. No upper limit, * the ability to add and remove objects on the fly, etc. * * Rundown of routines: * Constructor - initializes an empty flexarray with increment 10 * Destructor - Deletes the flexarray. Does NOT delete objects in the flexarray * copy constructor/ = :deep copy (override X::operator= if you need * deep copies of the elements) * * Information: * int getLength()/getSize() - returns #objects * int getActualSize() - returns #slots allocated * int getIncrement() - returns #slots allocated at a time * int empty() - true if no elts * * Access: * X& [idx] - return indexed reference, unsafe * X* getData(idx) - Ptr to indexed elt. * X* getDataConst - same as above, but everything is const qualified * * Manip: * setIncrement(int) - * clear() - empties the array * fill(X&, N) - sets the array to N copies of the X * setData(X&, idx) - change entry at index * insertElement(X&, idx) - insert element BEFORE index * (if index=-1, append element); * append(X&) - duh. * InsertElements(start, X*, N) - insert #elements before * start... read values from X*. * removeElements(start, N) - remove N elements starting with start */ template class flexarray TAG { int Length, Size, Increment; X* data; public: flexarray(){Length=Size=0; Increment=10;data=NULL;} flexarray(const flexarray &src){ Length=src.Length;Size=src.Size;Increment=src.Increment; if (Size==0) data=NULL; else { data=getNewArray(Size); memcpy(this->data, src.data, this->Length*sizeof(X)); } } ~flexarray(){if (data) delete[] data;} flexarray &operator=(const flexarray &src){ if (data!=NULL) delete[] data; Length=src.Length;Size=src.Size;Increment=src.Increment; if (Size==0) data=NULL; else { data=getNewArray(Size); memcpy(this->data, src.data, this->Length*sizeof(X)); } return *this; } int getActualSize() const{return Size;} int getSize() const{return Length;} int getLength() const{return Length;} int getIncrement() const{return Increment;} int empty() const{return (Size==0);} const X & operator()(int idx) const{return data[idx];} X & operator[](int idx) const{return data[idx];} X* getData(int idx)const{ if (idx==-1) idx=Length-1; if (idx>=0 && idx=0 && idx=0 && idxLength) return; if (startidx==-1) startidx=Length; if (this->data==NULL) data=getNewArray(Increment); if (Size0) memcpy(tflexarray, data, startidx*sizeof(X)); if (startidx=Length) return; if (startidx==-1) startidx=Length-1; if (startidx+howmany>=Length) howmany=Length-startidx; memcpy(data+startidx, data+startidx+howmany, (Length-(startidx+howmany))*sizeof(X)); Length-=howmany; } private: X* getNewArray(int newSize){ return new X [newSize]; } void resizeArray(int startidx, int numelts){ int NSize=Length+numelts; if (NSize>Size){ Size=Increment * (NSize/Increment +1); X* tflexarray=getNewArray(Size); if (startidx>0) memcpy(tflexarray, data, startidx*sizeof(X)); if (startidx