#ifndef ARRAY2D_H #define ARRAY2D_H #include #include #include #include #include "Array.h" // for macros #include "MatrixDef.h" namespace ATC_matrix { /** * @class Array2D * @brief Base class for creating, sizing and operating on 2-D arrays of data */ template class Array2D { public: Array2D(); Array2D(int nrows, int ncols); Array2D(const Array2D& A); // copy constructor ~Array2D(); // Resize and reinitalize matrix void reset(int nrows, int ncols); // Access method to get the (i,j) element: T& operator() (int i, int j); // Access method to get the i-th col AliasArray column(int i) const; // Access method to get the (i,j) element: const T& operator() (int i, int j) const; // Copy operator Array2D& operator= (const Array2D& other); // assignment operator Array2D& operator= (const T other); // Get size of Array2D int nRows() const; int nCols() const; // Do I have this element? bool has_member(T val) 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 nrows_, ncols_; T *data_; }; template Array2D::Array2D() { nrows_ = 0; ncols_ = 0; data_ = NULL; } template Array2D::Array2D(int nrows, int ncols) { nrows_ = nrows; ncols_ = ncols; data_ = new T[nrows_ * ncols_]; } template Array2D::Array2D(const Array2D& A) { nrows_ = A.nrows_; ncols_ = A.ncols_; if (A.data_==NULL) data_ = NULL; else { data_ = new T[nrows_ * ncols_]; for(int i=0;i void Array2D::reset(int nrows, int ncols) { if (nrows_ == nrows && ncols_ == ncols) { // no size change; don't realloc memory return; } else { // size changed; realloc memory nrows_ = nrows; ncols_ = ncols; if (data_ != NULL) delete [] data_; if (ncols_ > 0 && nrows_ > 0) data_ = new T[nrows_ * ncols_]; else { data_ = NULL; nrows_ = 0; ncols_ = 0; } } } template T& Array2D::operator() (int row, int col) { // Array bounds checking return data_[col*nrows_ + row]; } template const T& Array2D::operator() (int row, int col) const { // Array bounds checking return data_[col*nrows_ + row]; } template AliasArray Array2D::column(int col) const { // Array bounds checking return AliasArray(nrows_,&(data_[col*nrows_])); } template Array2D& Array2D::operator= (const Array2D& other) { if (data_ == NULL) { // initialize my internal storage to match LHS nrows_ = other.nrows_; ncols_ = other.ncols_; if (other.data_==NULL) data_ = NULL; else data_ = new T[nrows_ * ncols_]; } for(int i=0;i Array2D& Array2D::operator= (const T other) { for(int i=0;i int Array2D::nRows() const { return nrows_; } template int Array2D::nCols() const { return ncols_; } template bool Array2D::has_member(T val) const { int i; bool retval = false; for(i=0;i void Array2D::write_restart(FILE *f) const { fwrite(&nrows_,sizeof(int),1,f); fwrite(&ncols_,sizeof(int),1,f); if (nrows_*ncols_ > 0) fwrite(data_,sizeof(T),nrows_*ncols_,f); } template Array2D::~Array2D() { if (data_ != NULL) delete[] data_; } template void Array2D::print(std::string name) const { std::cout << "------- Begin "<