/* * Copyright (C) 1995, 1996 Peter Bouthoorn. * * This software may be freely distributed and modified provided * this copyright message is left intact. The copyright message must be * included both with this (the original) software and with any modified * copies of this software or with any new software based on this software. * Furthermore any modified copies of this software must carry prominent * notices stating the software was changed and the date of any change. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. In no event * will the copyright holder be liable for any damage arising out of * the use of this software. * * As a matter of courtesy, the author requests to be informed about * any bugs found in this software and about improvements that may be of * general interest. * * Peter Bouthoorn * peter@obelix.icce.rug.nl */ #ifndef _ARRAY_H_ #define _ARRAY_H_ #include #define NoDel 0 #define DoDel 1 #define DontManage 0 #define DoManage 1 template class Array { public: Array(); Array(int); ~Array(); Array(const Array &); Array &operator=(const Array &); int operator==(const Array &) const; int operator!=(const Array &) const; T &operator[](int); const T &operator[](int) const; void operator+=(const T &); int getsize() const; void grow(int); int find(const T &) const; void clear(); private: void copy_arr(const Array &); int size; T *array; }; template class PtrArray { public: PtrArray(int sz = 0, int do_manage = 1); ~PtrArray(); PtrArray(const PtrArray &); PtrArray &operator=(const PtrArray &); int operator==(const PtrArray &) const; int operator!=(const PtrArray &) const; T *&operator[](int); const T *operator[](int) const; void operator+=(T *); int getsize() const; void grow(int); int find(const T *) const; void clear(int); private: void copy_arr(const PtrArray &); int manage; int size; T **array; }; template inline Array::Array() { array = 0; size = 0; } template inline Array::Array(int sz) { size = sz; if (size) array = new T[size]; else array= 0; } template inline Array::~Array() { delete [] array; } template Array::Array(const Array &other) { copy_arr(other); } template Array &Array::operator=(const Array &other) { if (this != &other) { delete [] array; copy_arr(other); } return(*this); } template void Array::copy_arr(const Array &other) { size = other.size; if (size) { array = new T[size]; for (int i= 0; i < size; i++) array[i] = other.array[i]; } else array = 0; } template int Array::operator==(const Array &other) const { if (this == &other) return(1); if (size != other.size) return(0); for (int i = 0; i < size; i++) if (!(array[i] == other.array[i])) return(0); return(1); } template inline int Array::operator!=(const Array &other) const { return(!(*this == other)); } template inline T &Array::operator[](int i) { return(array[i]); } template inline const T &Array::operator[](int i) const { return(array[i]); } template inline void Array::operator+=(const T &addme) { grow(1); array[size - 1] = addme; } template inline int Array::getsize() const { return(size); } template void Array::grow(int gz) { T *oldarray = array; int newsize = size + gz; array = new T[newsize]; for (int i = 0; i < size; i++) array[i] = oldarray[i]; size = newsize; delete [] oldarray; } template int Array::find(const T &findme) const { for (int i = 0; i < size; i++) if (array[i] == findme) return(i); return(-1); } template void Array::clear() { delete [] array; size = 0; } template PtrArray::PtrArray(int sz, int do_manage) { size = sz; manage = do_manage; if (size) { array = new T*[size]; for (int i = 0; i < size; i++) array[i] = 0; } else array = 0; } template PtrArray::~PtrArray() { clear(manage); } template PtrArray::PtrArray(const PtrArray &other) { copy_arr(other); } template PtrArray &PtrArray::operator=(const PtrArray &other) { if (this != &other) { clear(manage); copy_arr(other); } return(*this); } template void PtrArray::copy_arr(const PtrArray &other) { size = other.size; manage = other.manage; if (size) { int i; array = new T*[size]; if (manage) for (i = 0; i < size; i++) array[i] = other.array[i] ? other.array[i]->clone() : 0; else for (i = 0; i < size; i++) array[i] = other.array[i]; } else array = 0; } /* * operator== assumes that both arrays don't contain gaps * (i.e no 0 pointers) */ template int PtrArray::operator==(const PtrArray &other) const { if (this == &other) return(1); if (size != other.size) return(0); for (int i = 0; i < size; i++) if (!(*array[i] == *other.array[i])) return(0); return(1); } template inline int PtrArray::operator!=(const PtrArray &other) const { return(!(*this == other)); } template inline T *&PtrArray::operator[](int i) { return(array[i]); } template inline const T *PtrArray::operator[](int i) const { return(array[i]); } template inline void PtrArray::operator+=(T *addme) { grow(1); array[size - 1] = addme; } template inline int PtrArray::getsize() const { return(size); } template void PtrArray::grow(int gz) { T **oldarray = array; int i, newsize = size + gz; array = new T*[newsize]; for (i = 0; i < size; i++) array[i] = oldarray[i]; for (; i < newsize; i++) array[i] = 0; size = newsize; delete [] oldarray; } template int PtrArray::find(const T *find_me) const { for (int i = 0; i < size; i++) if (array[i]) if (*array[i] == *find_me) return(i); return(-1); } template void PtrArray::clear(int clr) { if (clr) for (int i = 0; i < size; i++) delete array[i]; delete [] array; array = 0; size = 0; } #endif