// ******** // ARRAYS.H // ******** #ifndef ARRAYS_H #define ARRAYS_H //#define PC #include #include "errors.h" // this is a template for all classes which use an array of objects // it is dynamically allocated template class Array { public: Array() {Init();} Array(const Array &t) {Init(t);} // the copy constructor Array(int asize) {Init(); Allocate(asize);} // creates an array of size "asize" ~Array(); // the destructor deletes all internal // objects void Allocate(int asize); // allocates asize objects // if data was already allocated, // deletes and re-allocates T &operator[](int i) const; Array &operator = (const Array &t); // copies one array to another, // requires that the assignment // operator be defined for array // elements void Set(T t); // sets all elements to t int Size() const; // returns the size of the array friend ostream &operator<<(ostream &s, const Array &t); protected: T &Data(int i); // method derived class can use for // accessing data void Init() {size = 0; data = NULL;}// default intialisor void Init(const Array &t); // implements copy constructor private: int size; // the size of the array T *data; // ptr to the array of objects }; // this is a template for sortable arrays of objects, i.e. the objects provide // a less than comparison operator, which is used in the Sort method to perform // a heap sort template class SortableArray : public Array { public: SortableArray() {Init();} SortableArray(const SortableArray &t) {Init(t);} SortableArray(int asize) {Allocate(asize);} void Sort(); // performs a heapsort on the data, // using the < operator protected: void Adjust(int root, int last); // for the heap sort }; // this is a template for sortable arrays of objects, but the comparison // operator is provided by another class C template class CompSortableArray : public Array { public: CompSortableArray() {Init();} CompSortableArray(int asize, C *c_ptr) {Allocate(asize); comp_ptr = c_ptr;} void Sort(); // performs a heapsort on the data, // using comp_ptr->Compare protected: C *comp_ptr; // a ptr to the object with the Compare // method void Adjust(int root, int last); }; // this is a template for a multidimensional array of one type of object // when declaring this one must specify the number of dimensions first, // followed by the size for each array dimension /* template class MultiArray { public: MultiArray() {Init();} MultiArray(const MultiArray &t) {Init(t);} // the copy constructor MultiArray(int dims, int x, ...); // a variable number of parameters {Init(); Allocate(xsize, ysize);} ~Array2D(); // the destructor deletes all internal // objects void Allocate(int xsize, ...); // allocates x, y, ... size array // if data was already allocated, // deletes and re-allocates T Data(int x, int y); // returns object in x,y location Array2D &operator = (const Array2D &t); // copies one array to another, // requires that the assignment // operator be defined for array // elements void Set(T t); // sets all elements to t int XSize(); // returns the x size of the array int YSize(); // returns the y size of the array friend ostream &operator<<(ostream &s, const Array2D &t); protected: T &Data(int i); // method derived class can use for // accessing data void Init() {size = 0; data = NULL;}// default intialisor void Init(const Array &t); // implements copy constructor protected: Array > data; }; */ #endif