#ifndef ARRAY_H #define ARRAY_H #include #include #include #include // for macros #include "MatrixDef.h" namespace ATC_matrix { /** * @class Array * @brief Base class for creating, sizing and operating on 1-D arrays of data */ template class Array { public: Array(); Array(int len); Array(const Array& A); virtual ~Array(); // Resize and reinitialize the array virtual void reset(int len); //* resizes the matrix, copy what fits default to OFF virtual void resize(int len, bool copy=false); // Access method to get the element i: T& operator() (int i); const T& operator() (int i) const; // Assignment virtual Array& operator= (const Array &other); virtual Array& operator= (const T &value); // Get length of array int size() const; // Do I have this element? bool has_member(T val) const; // range bool check_range(T min, T max) const; void range(T & min, T & max) const; // search an ordered array int index(T& val) const; // Return pointer to internal data const T* data() const; T* 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; protected: int len_; T *data_; }; template class AliasArray { public: AliasArray(); AliasArray(const Array& A); AliasArray(const AliasArray& A); AliasArray(int len, T * A); virtual ~AliasArray(); virtual AliasArray& operator= (const Array &other); virtual AliasArray& operator= (const T &value); const T& operator() (int i) const; int size() const; T* ptr() const; protected: 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 Array::~Array() { if (data_ != NULL) delete[] data_; } template 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 void Array::resize(int len, bool copy) { if (len_ == len) { // no size change; don't realloc memory return; } else { // size change, realloc memory len_ = len; if (len_ > 0) { if (copy && data_ != NULL) { Array temp(*this); delete[] data_; data_ = new T[len_]; for (int i = 0 ; i < len_; i++) { if (i < temp.size()) data_[i] = temp.data_[i]; } } else { if (data_ != NULL) delete[] data_; data_ = new T[len_]; } } else { data_ = NULL; len_ = 0; } } } template T& Array::operator() (int i) { 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 { return data_[i]; } 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 bool Array::check_range(T min, T max) const { int i; for(i=0;i max) return false; else if (val < min) return false; } return true; } template void Array::range(T& min, T& max) const { int i; min = max = data_[0]; for(i=1;i max) max = val; else if (val < min) min = val; } } template int Array::index(T& val) const { int idx = -1; int i; 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::data() const { return data_; } template T* Array::ptr() const { return data_; } template void Array::print(std::string name) const { std::cout << "------- Begin "< AliasArray::AliasArray(void) { } template AliasArray::AliasArray(const AliasArray & other) { len_ = other.size(); data_ = other.ptr(); } // for a mem continguous slice template AliasArray::AliasArray(int len, T * ptr) { len_ = len; data_ = ptr; } template AliasArray::AliasArray(const Array& A) { len_ = A.len_; data_ = A.ptr(); } template AliasArray::~AliasArray(void) { len_ = 0; data_ = NULL; // trick base class into not deleting parent data } template AliasArray& AliasArray::operator= (const Array &other) { len_ = other.size(); data_ = other.ptr(); return *this; } template AliasArray& AliasArray::operator= (const T &value) { for(int i=0;i < len_;i++) data_[i] = value; return *this; } template const T& AliasArray::operator() (int i) const { return data_[i]; } template int AliasArray::size(void) const { return len_; } template T* AliasArray::ptr() const { return data_; } } // end namespace #endif // Array.h