#ifndef __ARRAY_H #define __ARRAY_H /** * array * * Implements a simple array with size in a constant attribute. Objects (or * basic data-types) are stored by value, just like in an ordinary array. */ template class array { T *A_data; public: /** * constructor * * Constructs an array of given size. * * @param The size of the array * @precond 0 < sz */ array(unsigned int sz) : size(sz), A_data(new T[sz]) {} /** * operator[] * * Allows the objects in the array to be selected using the array[i] syntax. * * @param The index of the object to be selected * @return A reference to the selected object * @precond 0 <= i < size */ T &operator[](unsigned int i) {return(A_data[i]);} /** * operator[] const * * Same as above, except doesn't allow to change the data contained within. * * @param The index of the object to be selected * @return A const reference to the selected object * @precond 0 <= i < size */ const T &operator[](unsigned int i) const {return(A_data[i]);} /** * operator= * * Copies the values from another array, until either array ends. * * @param A const reference to the array on the right side of the * equals-sign * @return A const reference to this array after the operation * @precond The substitution parameter of rv must match that of this array */ array &operator=(const array &rv) { for(unsigned int i = 0; i < size && i < rv.size; i++) A_data[i] = rv[i]; return(*this); } /** * destructor * * Releases the memory of the array and as such deletes all contained objects */ ~array() {delete[] A_data;} /** * size * * Holds the size of the array in a constant. Since it is a public attribure, * it can be accessed straight without a function call. * * @example array arr(10); * cout << arr.size << endl; */ const unsigned int size; }; /** * ptrArray * * Implements an array that stores pointers to objects (or basic data-types). * Has a public constant size-attribute. * * @todo Add a constructor, which takes a variable argument list and * initializes the array from the objects in that list */ template class ptrArray { T **A_data; public: /** * constructor * * Constructs an array of given size. * * @param The size of the array * @precond 0 < sz */ ptrArray(unsigned int sz) : size(sz), A_data(new T*[sz]) {} /** * operator[] * * Returns a reference to the pointer of the selected object. * * @param The index of the object to be selected * @return A reference to the pointer of the object * @precond 0 <= i < size */ T *&operator[](unsigned int i) {return(A_data[i]);} /** * operator[] const * * Same as above, but doesn't allow any modifications to the pointer. * * @param The index of the object to be selected * @return A const reference to the pointer of the object * @precond 0 <= i < size */ const T *&operator[](unsigned int i) const {return(A_data[i]);} /** * operator= * * Copies the pointers from another array, until either array ends. * * @param A const reference to the array on the right side of the * equals-sign * @return A reference to this array * @precond The substitution parameter of rv must match that of this array */ ptrArray &operator=(const ptrArray &rv) { for(unsigned int i = 0; i < size && i < rv.size; i++) A_data[i] = rv[i]; return(*this); } /** * pop * * Returns a pointer to the selected object and removes if from the array. * * @param The index of the object to be popped * @return A pointer to the object * @precond 0 <= i < size */ T *pop(unsigned int i) {T *temp = A_data[i]; A_data[i] = 0; return(temp);} /** * set * * Sets the given array location to point to the given object. Takes the * object by reference. * * @param The index of the location to be pointed to the object * @param A reference to the object * @return void * @precond 0 <= i < size */ void set(unsigned int i, T &obj) {A_data[i] = &obj; return;} /** * set * * Same as above, except this one takes the object by pointer. * * @param The index of the location to be pointed to the object * @param A pointer to the object * @return void * @precond 0 <= i < size */ void set(unsigned int i, T *obj) {A_data[i] = obj; return;} /** * destructor * * Releases the memory used to store the pointers to the objects. */ ~ptrArray() {delete[] A_data;} /** * size * * Holds the size of the array in a constant. Since it is a public attribure, * it can be accessed straight without a function call. * * @example array arr(10); * cout << arr.size << endl; */ const unsigned int size; }; #endif