#ifndef _Array_h #define _Array_h #pragma interface #include template class Array { int cnt, max; T *a; Array(const Array &); void operator=(const Array &); public: Array(int n = 0); int size() const { return cnt; } void add(const T &r); Array &operator+(const T &r) { this->add(r); return *this; } void del(int pos,int n = 1); void resize(int); Array &clear() { cnt = 0; return *this; } T &operator [] (int i) const { if (i < 0 || i >= cnt) abort(); return a[i]; } ~Array() { delete [] a; } }; template Array::Array(int n) { cnt = max = n; if (n) a = new T[n]; else a = 0; } template Array::Array(const Array &r) { a = 0; *this = r; } template void Array::operator=(const Array &r) { delete [] a; cnt = r.cnt; max = r.max; if (max) a = new T[max]; else a = 0; for (int i = 0; i < cnt; ++i) a[i] = r.a[i]; } template void Array::add(const T &r) { if (cnt >= max) resize(max + max/10 + 10); a[cnt++] = r; } template void Array::del(int pos,int n) { if (n < 0) { pos += n; n = -n; } if (pos < 0) { n += pos; pos = 0; } if (n > 0 && pos < cnt) { if (pos + n > cnt) n = cnt - pos; for (cnt -= n; pos < cnt; ++pos) a[pos] = a[pos + n]; } } template void Array::resize(int n) { T *d = new T[n]; cnt = (cnt < n) ? cnt : n; for (int i = 0; i < cnt; ++i) d[i] = a[i]; delete [] a; a = d; max = n; } #endif