#ifndef ARRAY_H #define ARRAY_H //#include //#include #include #include #include template class Array { public: Array(); Array(int len); Array(const Array& A); ~Array(); // Resize and reinitialize the array void reset(int len); // Access method to get the element i: T& operator() (int i); // Access method to get the element i: const T& operator() (int i) const; // Assignment Array& operator= (const Array &other); Array& operator= (const T &value); // Get length of array int get_length() const; int size() const; // Do I have this element? bool has_member(T val) const; // Return pointer to internal data const T* get_data() const; T* get_ptr() const; // print void print(std::string name = "") const; // Dump templated type to disk; operation not safe for all types void write_restart(FILE *f) const; private: int len_; T *data_; }; template Array::Array(void) { len_ = 0; data_ = NULL; } template Array::Array(int len) { len_ = len; data_ = new T[len_]; } template Array::Array(const Array& A) { len_ = A.len_; if (A.data_==NULL) data_ = NULL; else { data_ = new T[len_]; for(int i=0;i void Array::reset(int len) { if (len_ == len) { // no size change; don't realloc memory return; } else { // size change, realloc memory len_ = len; if (data_ != NULL) delete[] data_; if (len_ > 0) data_ = new T[len_]; else { data_ = NULL; len_ = 0; } } } template T& Array::operator() (int i) { // Array bounds checking return data_[i]; } template Array& Array::operator= (const Array &other) { if (data_ == NULL) { // initialize my internal storage to match LHS len_ = other.len_; if (other.data_==NULL) data_ = NULL; else data_ = new T[len_]; } for(int i=0;i Array& Array::operator= (const T &value) { for(int i=0;i const T& Array::operator() (int i) const { // Array bounds checking return data_[i]; } template int Array::get_length(void) const { return len_; } template int Array::size(void) const { return len_; } template bool Array::has_member(T val) const { int i; bool retval = false; for(i=0;i void Array::write_restart(FILE *f) const { fwrite(&len_,sizeof(int),1,f); if (len_ > 0) fwrite(data_,sizeof(T),len_,f); } template const T* Array::get_data() const { return data_; } template T* Array::get_ptr() const { return data_; } template Array::~Array() { if (data_ != NULL) delete[] data_; } template void Array::print(std::string name) const { std::cout << "------- Begin "<