Simple2DArray.h

00001 /*  _________________________________________________________________________
00002  *
00003  *  UTILIB: A utility library for developing portable C++ codes.
00004  *  Copyright (c) 2001, Sandia National Laboratories.
00005  *  This software is distributed under the GNU Lesser General Public License.
00006  *  For more information, see the README file in the top UTILIB directory.
00007  *  _________________________________________________________________________
00008  */
00009 
00010 //
00011 // Simple2DArray.h
00012 //
00022 #ifndef __Simple2DArray_h
00023 #define __Simple2DArray_h
00024 
00025 #ifdef __GNUC__
00026 #pragma interface
00027 #endif
00028 
00029 #ifdef NON_ANSI
00030 #include <iostream.h>
00031 #else
00032 #include <iostream>
00033 using namespace std;
00034 #endif
00035 #ifndef ANSI_HDRS
00036 #include <stdlib.h>
00037 #include <assert.h>
00038 #else
00039 #include <cstdlib>
00040 #include <cassert>
00041 #endif
00042 
00043 #include "_generic.h"
00044 #include "Basic2DArray.h"
00045 #include "PackBuf.h"
00046 
00047 #if !defined(Simple2DArrayT)
00048 #ifdef USING_TEMPLATES
00049 #define Simple2DArrayT(T) Simple2DArray<##T##>
00050 #else
00051 #define Simple2DArrayT(T) Simple2DArray_##T
00052 #endif
00053 #endif
00054 
00055 
00056 #ifdef SWIG
00057 template <class T>
00058 class Simple2DArray : public Basic2DArray<T>
00059 #else
00060 template <class T>
00061 class UTILIB_API Simple2DArray : public Basic2DArray<T>
00062 #endif
00063 {
00064 public:
00065 
00067   Simple2DArray()  
00068                 : Basic2DArray<T>() {}
00074   Simple2DArray(const BasicArray<T>& array, const size_type nrows=1,
00075                 const EnumDataOwned own=DataNotOwned)
00076                 : Basic2DArray<T>(array,nrows,own) {}
00082   Simple2DArray(const size_type nrows, const size_type ncols, T *d=((T*)0),
00083                 const EnumDataOwned own=DataNotOwned)
00084                 : Basic2DArray<T>(nrows,ncols,d,own) {}
00090   Simple2DArray(const size_type nrows, const size_type ncols, const BasicArray<T>& array,
00091                 const EnumDataOwned own=DataNotOwned)
00092                 : Basic2DArray<T>(nrows,ncols,array,own) {}
00094   Simple2DArray(const Simple2DArray<T>& array)
00095                 : Basic2DArray<T>(array) {}
00096  
00098   Simple2DArray<T>& operator=(const Simple2DArray<T>& array)
00099                 {Basic2DArray<T>::operator=(array); return *this;}
00101   Simple2DArray<T>& operator=(const T& val)
00102                 {Basic2DArray<T>::operator=(val); return *this;}
00103 
00105   int  operator== (const Simple2DArray<T>& array) const;
00107   int  operator!= (const Simple2DArray<T>& array) const;
00115   int  compare(const Simple2DArray<T>& array) const;
00116 /* BUG!  Let's see where this is used!
00117   int  operator<(const Simple2DArray<T>& array) const
00118                 {return compare(array);}
00119 */
00120 
00126   virtual int write(ostream& output) const;       
00128   virtual int read(istream& input);              
00130   virtual int write(PackBuffer& output) const;  
00132   virtual int read(UnPackBuffer& input);       
00133 
00134 };
00135 
00136  
00137 
00139 template <class T>
00140 inline UTILIB_API ostream& operator<<(ostream& output, const Simple2DArray<T>& array)
00141 { array.write(output); return(output); }
00142  
00144 template <class T>
00145 inline UTILIB_API istream& operator>>(istream& input, Simple2DArray<T>& array)
00146 { array.read(input); return(input); }
00147  
00149 template <class T>
00150 inline UTILIB_API PackBuffer& operator<<(PackBuffer& output, const Simple2DArray<T>& array)
00151 { array.write(output); return(output); }
00152  
00154 template <class T>
00155 inline UTILIB_API UnPackBuffer& operator>>(UnPackBuffer& input, Simple2DArray<T>& array)
00156 { array.read(input); return(input); }
00157 
00158 
00159 
00160 template <class T>
00161 int Simple2DArray<T>::compare(const Simple2DArray<T>& array) const
00162 {
00163 if (array.a == a)
00164    return 0;
00165  
00166 for (size_type i=0; i<min(a->Nrows,array.a->Nrows); i++)
00167   for (size_type j=0; j<min(a->Ncols,array.a->Nrows); j++)
00168     if (a->Data[i][j] < array.a->Data[i][j])
00169        return -1;
00170     else if (a->Data[i][j] > array.a->Data[i][j])
00171        return 1;
00172 
00173 if ((array.a->Nrows == a->Nrows) && (array.a->Ncols == a->Ncols))
00174    return 0;
00175 
00176 if ((array.a->Nrows < a->Nrows) || (array.a->Nrows < a->Nrows))
00177    return -1;
00178 return 1;
00179 }
00180 
00181 
00182 template <class T>
00183 int Simple2DArray<T>::operator==(const Simple2DArray<T>& array) const
00184 {
00185 if (array.a == a)
00186    return 1;
00187 if ((array.a->Nrows != a->Nrows) || (array.a->Ncols != a->Ncols))
00188    return 0;
00189  
00190 for (size_type i=0; i<a->Nrows; i++)
00191   for (size_type j=0; j<a->Ncols; j++)
00192     if (a->Data[i][j] != array.a->Data[i][j])
00193        return 0;
00194  
00195 return 1;
00196 }
00197 
00198 
00199 template <class T>
00200 int Simple2DArray<T>::operator!=(const Simple2DArray<T>& array) const
00201 {
00202 if ((array.a->Nrows != a->Nrows) || (array.a->Ncols != a->Ncols))
00203    return 1;
00204  
00205 for (size_type i=0; i<a->Nrows; i++)
00206   for (size_type j=0; j<a->Ncols; j++)
00207     if (a->Data[i][j] != array.a->Data[i][j])
00208        return 1;
00209  
00210 return 0;
00211 }
00212 
00213 
00214 template <class T>
00215 int Simple2DArray<T>::write(PackBuffer& os) const
00216 {
00217 os << a->Nrows << a->Ncols;
00218 if (a->Data) {
00219    for (size_type i=0; i<a->Nrows; i++)
00220      for (size_type j=0; j<a->Ncols; j++)
00221        os << a->Data[i][j];
00222    }
00223 return OK;
00224 }
00225 
00226 
00227 template <class T>
00228 int Simple2DArray<T>::write(ostream& os) const
00229 {
00230 os << a->Nrows << " " << a->Ncols << ": ";
00231 if (a->Data) {
00232    for (size_type i=0; i<a->Nrows; i++) {
00233      for (size_type j=0; j<a->Ncols; j++)
00234        os << a->Data[i][j] << " ";
00235      os << endl;
00236      }
00237    }
00238 return OK;
00239 }
00240 
00241 
00242 template <class T>
00243 int Simple2DArray<T>::read(UnPackBuffer& is)
00244 {
00245 size_type nrows,ncols;
00246 is >> nrows >> ncols;
00247 resize(nrows,ncols);
00248 for (size_type i=0; i<a->Nrows; i++)
00249   for (size_type j=0; j<a->Ncols; j++)
00250     is >> a->Data[i][j];
00251 
00252 return OK;
00253 }
00254 
00255 
00256 
00257 template <class T>
00258 int Simple2DArray<T>::read(istream& /*is*/)
00259 {
00260 free();
00261 
00262 //
00263 // TODO
00264 //
00265 
00266 return OK;
00267 }
00268 #endif