// array.h #ifndef array_H #define array_H #undef trf_error #define trf_error #include #include #include template class array { private: int size; protected: T *data; void allocate(int N); virtual void ostr(std::ostream& stream) const; public: void fill(const T& value); array(int N); array(const array& arr); virtual ~array(void); void fill(const T* arr); T min( ) const; T max( ) const; T amin( ) const; T amax( ) const; int get_size( ) const { return size; }; // Return an STL vector. std::vector get_std_vector() const { return std::vector( data, data+get_size() ); } array &operator=(const array& arr); T& operator()( int i ) const { return data[i]; }; array operator-( ); array &operator+=(const array& arr); array &operator-=(const array& arr); array &operator*=(const T& tval); #if defined(DEFECT_GUIDING_DECLS) friend array operator+(const array& arr1, const array& arr2); friend array operator-(const array& arr1, const array& arr2); friend std::ostream& operator<<(std::ostream& stream, const array& arr); friend bool operator==(const array& arr1, const array& arr2); friend bool operator!=(const array& arr1, const array& arr2); friend bool is_equal(const array& arr1, const array& arr2); #elif defined(DEFECT_NO_EXPLICIT_QUALIFICATION) template friend array operator+(const array& arr1, const array& arr2); template friend array operator-(const array& arr1, const array& arr2); template friend std::ostream& operator<<(std::ostream& stream, const array& arr); template friend bool operator==(const array& arr1, const array& arr2); template friend bool operator!=(const array& arr1, const array& arr2); template friend bool is_equal(const array& arr1, const array& arr2); #else friend array operator+ <> (const array& arr1, const array& arr2); friend array operator- <> (const array& arr1, const array& arr2); friend std::ostream& operator<< <> (std::ostream& stream, const array& arr); friend bool operator== <> (const array& arr1, const array& arr2); friend bool operator!= <> (const array& arr1, const array& arr2); friend bool is_equal <> (const array& arr1, const array& arr2); #endif }; template inline array::array(int N) { allocate(N); } template inline array::array(const array& arr) { #ifdef trf_info std::cout << "copying an array\n"; #endif allocate(arr.size); for ( int i=0; i inline array::~array(void) { #ifdef trf_info std::cout << size*sizeof(T) << " bytes deallocated [" << &data[0] << "]\n"; #endif delete [] data; } template inline void array::allocate(int N) { size = N; data = new T[size]; #ifdef trf_info std::cout << size*sizeof(T) << " bytes allocated [" << &data[0] << "]\n"; #endif } template inline void array::ostr(std::ostream& stream) const { stream << "{"; for ( int i=0; i inline void array::fill(const T* arr) { for ( int i=0; i inline void array::fill(const T& value) { for ( int i=0; i inline array &array::operator=(const array& arr) { assert( size == arr.size ); #ifdef trf_error if ( size != arr.size ) { std::cerr << "copy_data:invalid array equality\n"; std::cerr << arr << '\n' << *this << std::endl; } #endif else { for ( int i=0; i inline array &array::operator+=(const array& arr) { assert( size == arr.size ); #ifdef trf_error if ( size != arr.size ) { std::cerr << "operator+=: invalid array equality" << std::endl; } #endif else { for ( int i=0; i inline array &array::operator-=(const array& arr) { assert( size == arr.size ); #ifdef trf_error if ( size != arr.size ) { std::cerr << "operator-=: invalid array equality" << std::endl; } #endif else { for ( int i=0; i inline array &array::operator*=(const T& tval) { for ( int i=0; i inline array operator*(const T& val,const array& arr) { array lhs = arr; lhs *= val; return lhs; } template inline array operator*(const array& arr,const T& val) { array lhs = arr; lhs *= val; return lhs; } template inline array operator+(const array& arr1,const array& arr2) { array sum = arr1; sum += arr2; return sum; } template inline array operator-(const array& arr1,const array& arr2) { array sum = arr1; sum -= arr2; return sum; } #ifndef __xlC__ #include "array.c" #endif #endif