00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifdef __GNUG__
00029 #pragma implementation
00030 #endif
00031
00032 template <class Type>
00033 class Array {
00034 protected:
00035 int _length;
00036 int _managed;
00037 Type * _array;
00038 public:
00039 Array():_length(0),_managed(0),_array(0) {}
00040 Array(const Array<Type>&a):_length(0),_managed(0),_array(0) {operator=(a);}
00041 Array(Type* data,int size):_length(size),_managed(0),_array(data){}
00042 Array(int size):_length(0),_managed(0),_array(0) { set_length(size); }
00043 ~Array() { clear(); }
00044
00045 int size() const { return _length; };
00046 void clear() { set_length(0); }
00047 void set_length(int size) {
00048 if (_array && _managed) delete[] _array;
00049 _managed = 1;
00050 if (size) _array = new Type [ size ];
00051 else _array = 0;
00052 _length = size;
00053 }
00054 void resize(int size) {
00055 Type*tmp=_array;
00056 if (size) _array = new Type [ size ];
00057 else _array = 0;
00058 int maxi;
00059 if (size < _length) maxi = size;
00060 else maxi = _length;
00061 for (int i=0; i<maxi; i++) _array[i] = tmp[i];
00062 if (_managed && tmp) delete[] tmp;
00063 _managed = 1;
00064 _length = size;
00065 }
00066 Array<Type>& operator = (const Array<Type> & s) {
00067 if (_managed && _array) delete[] _array;
00068 _managed = 1;
00069 _length = s._length;
00070 if (_length) _array = new Type [ _length ];
00071 else _array = 0;
00072 for (int i=0; i<_length; i++) {
00073 _array[i] = s._array[i];
00074 }
00075 return (*this);
00076 }
00077 Type& operator[] (int i) const {
00078 if (i<0 || i>=_length) {
00079 ExEnv::err() << "Array::operator[](" << i << ") "
00080 << "out of range (" << _length << "0" << std::endl;
00081 abort();
00082 };
00083 return _array[i];
00084 }
00085 Type& operator() (int i) const {
00086 if (i<0 || i>=_length) {
00087 ExEnv::err() << "Array::operator()(" << i << ") "
00088 << "out of range (" << _length << "0" << std::endl;
00089 abort();
00090 };
00091 return _array[i];
00092 }
00093 void push_back(const Type &d) {
00094 resize(_length+1);
00095 _array[_length-1] = d;
00096 }
00097 void pop_back() {
00098 resize(_length-1);
00099 }
00100 };
00101
00102
00103
00104
00105
00106
00107