#ifndef VECTOR_H #define VECTOR_H // __________________________________________________________________________ // Basic 1-dimensional array #include #include #include template class vector { private: int nx; T* sto; vector(const vector& v); public: vector(int x = 0) : nx(x) { assert(x >= 0); sto = (x == 0 ? 0 : new T[x]); } ~vector() { delete [] sto; } int dim() const { return nx; } void init(int x = 0) { assert(x >= 0); if (sto != 0) delete [] sto; nx = x; sto = (x == 0 ? 0 : new T[x]); } operator T*() const { return sto; } T& operator[](int x) const { assert(x >= 0 && x < nx); return sto[x]; } T& operator()(int x) const { assert(x >= 0 && x < nx); return sto[x]; } void operator=(const vector& v) { assert(nx == v.nx); memcpy(sto, v.sto, nx * sizeof(T)); } void operator+=(const vector& v) { assert(nx == v.nx); for (int i = 0; i < nx; ++i) sto[i] += v.sto[i]; } int read(const char* file);//Andrew adds this at Ken's suggestion 8/12/99 void clear(const T& val) { for (int i = 0; i < nx; ++i) sto[i] = val; } //void clear(const T& val = 0) { //for (int i = 0; i < nx; ++i) sto[i] = val; //} int maxind() const { int maxi = 0; T max = sto[0]; for (int i = 0; i < nx; ++i) if (sto[i] > max) { max = sto[i]; maxi = i; } return maxi; } }; // class vector //Andrew adds this at Ken's suggestion 8/12/99 template int vector::read(const char *file) { Ifstream(ifs, file); if (!ifs) return 0; int dim; ifs >> dim; init(dim); for (int i = 0; i < dim; ++i) ifs >> sto[i]; return 1; } // __________________________________________________________________________ // Basic defines typedef vector cvector; typedef vector ucvector; typedef vector ivector; typedef vector uivector; typedef vector lvector; typedef vector fvector; typedef vector dvector; typedef vector cvectors; typedef vector ucvectors; typedef vector ivectors; typedef vector uivectors; typedef vector lvectors; typedef vector fvectors; typedef vector dvectors; //Andrew adds these 8/3/00 typedef vector dvectorss; typedef vector ivectorss; // __________________________________________________________________________ // vector.h #endif