Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members  

/magnus/back_end/general/include/DArray.h

Go to the documentation of this file.
00001 /*
00002  *   $Id: 
00003  */
00004 
00005 // Copyright (C) 1996 The New York Group Theory Cooperative
00006 // See magnus/doc/COPYRIGHT for the full notice.
00007 //
00008 // Contents: Definition of class DArray.
00009 //
00010 // Principal Author:  Alexey Myasnikov
00011 //
00012 // Status: Useable
00013 //
00014 // Usage:
00015 //
00016 // Revision History:
00017 //
00018 //
00019 
00020 
00021 #ifndef _DARRAY_H_
00022 #define _DARRAY_H_
00023 
00024 #include "Vector.h"
00025 #include "IStreamPoll.h"
00026 #include "WordParser.h"
00027 
00028 template <class R> class MatrixRep;
00029 template <class R> class MatrixComputations;
00030 template <class R> class MatrixRow;
00031 //@db porting
00032 template <class R> class DArrayParser;
00033 template <class R> class MatrixCell;
00034 
00035 
00036 //------------------------------------------------------------------------//
00037 //------------------------------- DArrayRep ------------------------------//
00038 //------------------------------------------------------------------------//
00039 
00040 template <class R> class DArrayRep : public PureRep {
00041 
00042 public:
00043 
00044 
00045   /////////////////////////////////////////////////////////////////////////
00046   //                                                                     //
00047   // Constructors:                                                       //
00048   //                                                                     //
00049   /////////////////////////////////////////////////////////////////////////
00050   DArrayRep( int height, int width): theArray(NULL)
00051   {
00052    #if SAFETY > 0
00053     if( (width < 0)||(height < 0) )
00054       error("incorrect matrix size in constructor DArray( int )");
00055   #endif  
00056     makeArray(height,width);
00057   }
00058 
00059   DArrayRep( int n ): theArray(NULL) 
00060   {
00061   #if SAFETY > 0
00062     if( n < 0 )
00063       error("incorrect matrix size in constructor DArray( int )");
00064   #endif  
00065     makeArray(n,n);
00066   }
00067 
00068   DArrayRep( const DArrayRep& );
00069 
00070   ~DArrayRep( ) { deleteArray(); }
00071 
00072   DArrayRep<R>* clone( ) const { return new DArrayRep<R>(*this); }
00073 
00074   /////////////////////////////////////////////////////////////////////////
00075   //                                                                     //
00076   // Operators:                                                          //
00077   //                                                                     //
00078   /////////////////////////////////////////////////////////////////////////
00079 
00080   bool operator == ( const DArrayRep& DA ) const;
00081   
00082   MatrixRow<R>& operator []( int i ) { 
00083       #if ( SAFETY > 0 )
00084     if(!inBounds(i)){
00085           error("DArray index out of bounds DArrayRep::operator [](int i)");
00086     }
00087           
00088       #endif
00089       return theArray[rowBegin+i];
00090    }
00091 
00092   /////////////////////////////////////////////////////////////////////////
00093   //                                                                     //
00094   // Accessors:                                                          //
00095   //                                                                     //
00096   /////////////////////////////////////////////////////////////////////////
00097 
00098   inline int getWidth( ) const { return width; }  
00099     
00100   inline int getHeight( ) const { return height; }  
00101 
00102   VectorOf<R> getRow(int i) const;
00103 
00104   VectorOf<R> getCol(int i) const;
00105 
00106   void assignCol(int i,const VectorOf<R>& vc);
00107 
00108   // Here functions, which allows to change the size of theArray in different
00109   // ways
00110   void reSize(int newHeight,int newWidth);
00111 
00112   void colInsBefore(int col,int colsNum);
00113 
00114   void colInsAfter(int col,int colsNum);
00115 
00116   void rowInsBefore(int col,int colsNum);
00117 
00118   void rowInsAfter(int col,int colsNum);
00119 
00120   void colDelBefore(int col,int colsNum, bool reAlloc);
00121 
00122   void colDelAfter(int col,int colsNum, bool reAlloc);
00123 
00124   void colDelRange(int col1,int col2, bool reAlloc);
00125 
00126   void rowDelBefore(int row,int rowsNum, bool reAlloc);
00127 
00128   void rowDelAfter(int row,int rowsNum, bool reAlloc);
00129 
00130   void rowDelRange(int row1,int row2, bool reAlloc);
00131 
00132   /////////////////////////////////////////////////////////////////////////
00133   //                                                                     //
00134   // I/O:                                                                //
00135   //                                                                     //
00136   /////////////////////////////////////////////////////////////////////////
00137 
00138   bool readFrom( istream& istr, Chars& errMesg ) 
00139   {
00140     MatrixRow<R>* matrix;
00141     int matrixHeight,matrixWidth;
00142     
00143     DArrayParser<R> P(istr);
00144     bool result;
00145     result = P.parseDArray(errMesg, &matrix, matrixHeight,matrixWidth);
00146     if (result){
00147        height = matrixHeight;
00148        width = matrixWidth;
00149        rowBegin = 0;
00150        colBegin = 0;
00151        theArray = matrix;
00152     }else {
00153 #if SAFETY > 0
00154       error("DArray reading from stream aborted in DArrayRep::readFrom =");
00155 #endif
00156      };
00157     return result;
00158   }
00159 
00160   /////////////////////////////////////////////////////////////////////////
00161   //                                                                     //
00162   // IPC tools:                                                          //
00163   //                                                                     //
00164   /////////////////////////////////////////////////////////////////////////
00165   // I did not want to write width for each MatrixRef in the theArray
00166   // that why MatrixRef does not have it's own operators < and >
00167 
00168   virtual void write( ostream& ostr ) const{
00169      ostr < height < width;
00170      for (int i=rowBegin; i< rowBegin+height;i++)
00171        for (int j=colBegin;j<colBegin+width;j++)
00172            ostr < theArray[i].row[j]; 
00173   };
00174  
00175   virtual void read( istream& istr ){
00176      istr > height > width;
00177      makeArray(height,width);
00178      for (int i=0; i< height;i++){
00179        for (int j=0;j<width;j++)
00180            istr > theArray[i].row[j]; 
00181      }
00182      rowBegin = 0;
00183      colBegin = 0;
00184   }
00185 private:
00186 
00187   friend class MatrixRep<R>;
00188   friend class MatrixComputations<R>;
00189 
00190   /////////////////////////////////////////////////////////////////////////
00191   //                                                                     //
00192   // Data Members:                                                       //
00193   //                                                                     //
00194   /////////////////////////////////////////////////////////////////////////
00195 
00196   int width, height;
00197   // Sizes of matrix
00198    
00199   int rowBegin, colBegin;
00200   // The numbers of column and row, where the data begins.
00201   // It's changed in delay functions when you don't reallocate the memory.
00202 
00203 
00204   MatrixRow<R>* theArray;
00205   // Store the matrix as the array of MatrixRow, see below
00206 
00207   /////////////////////////////////////////////////////////////////////////
00208   //                                                                     //
00209   // Private functions:                                                  //
00210   //                                                                     //
00211   /////////////////////////////////////////////////////////////////////////
00212     void makeArray( int mHeight, int mWidth);
00213     //inicializes theArray, and sizes    
00214 
00215     void deleteArray( );
00216     // deletes theArray
00217 
00218     bool inBounds(int i) const {
00219                 if (i<0 || i>=height) {return false;}
00220                 else return true;
00221     }
00222     //checks the height bounds, width bounds usualy are checked in MatrixRow
00223 
00224   /////////////////////////////////////////////////////////////////////////
00225   //                                                                     //
00226   //  Debugging stuff:                                                   //
00227   //                                                                     //
00228   /////////////////////////////////////////////////////////////////////////
00229 
00230 #ifdef DEBUG
00231 
00232     //friend int main( );
00233 
00234 #endif
00235 
00236 };
00237 
00238 //------------------------------------------------------------------------//
00239 //------------------------------- DArray  --------------------------------//
00240 //------------------------------------------------------------------------//
00241 
00242 
00243 //@db 2.91
00244 
00245 template <class R> class DArray;
00246 
00247 template <class R>
00248 ostream& operator < ( ostream& ostr, const DArray<R>& DA )
00249 {
00250   DA.look()->write(ostr);
00251   return ostr;
00252 }
00253 
00254 template <class R>
00255 istream& operator > ( istream& istr, DArray<R>& DA)
00256 {
00257   DA.change()->read(istr);
00258   return istr;
00259 }
00260 
00261 //@db end 
00262 
00263 
00264 template <class R> class DArray : public ObjectOf< DArrayRep<R> > {
00265 
00266 public:
00267 
00268 
00269   /////////////////////////////////////////////////////////////////////////
00270   //                                                                     //
00271   // Constructors:                                                       //
00272   //                                                                     //
00273   /////////////////////////////////////////////////////////////////////////
00274 
00275   DArray( int n = 0 ) : ObjectOf<DArrayRep <R> >( new DArrayRep<R>(n) ) { }
00276   // Default constructor makes empty square DArray  of size n.
00277 
00278   DArray( int h, int w) : ObjectOf<DArrayRep <R> >( new DArrayRep<R>(h,w) ) { }
00279   // constructor makes DArray of size h x w.
00280 
00281   // Copy constructor, operator=, and destructor supplied by compiler.
00282 
00283   /////////////////////////////////////////////////////////////////////////
00284   //                                                                     //
00285   // Operators:                                                          //
00286   //                                                                     //
00287   /////////////////////////////////////////////////////////////////////////
00288 
00289   bool operator == ( const DArray& DA ) const {
00290       return *look() == *(DA.look());
00291   }
00292 
00293   VectorOf<R> operator []( int i ) const { return look()->getRow(i); }
00294 
00295   MatrixRow< R >& operator [] ( int i ) { return change()->operator [](i); }
00296 
00297   /////////////////////////////////////////////////////////////////////////
00298   //                                                                     //
00299   // Accessors:                                                          //
00300   //                                                                     //
00301   /////////////////////////////////////////////////////////////////////////
00302 
00303   VectorOf<R> getRow( int i ) const {
00304      return look()->getRow(i);
00305   }
00306   // Returns  i's row
00307 
00308   VectorOf<R> getCol( int i ) const {
00309      return look()->getCol(i);
00310   }
00311   // Returns  i's column
00312  
00313   void assignCol( int i, const VectorOf<R>& vc ){
00314      change()->assignCol(i,vc);
00315   }
00316   // Assigns i's column with vc.
00317 
00318  // Changes height and width. Puts data in up left corner.
00319   void reSize(int newHeight,int newWidth){
00320      change()->reSize(newHeight,newWidth);
00321   }
00322 
00323  // Functions below are inserts, corresponding to function names, before(after) 'col'
00324  // 'colsNum' columns(rows). 
00325   void colInsBefore(int col,int colsNum){
00326      change()->colInsBefore(col,colsNum);
00327   }
00328   void colInsAfter(int col,int colsNum){
00329      change()->colInsAfter(col,colsNum);
00330   }
00331   void rowInsBefore(int row,int rowsNum){
00332      change()->rowInsBefore(row,rowsNum);
00333   }
00334   void rowInsAfter(int row,int rowsNum){
00335      change()->rowInsAfter(row,rowsNum);
00336   }
00337 
00338  // These methods deletes number of rows(columns) before, after or between
00339  // the columns. If reAlloc = true - memory realocates for array, if not
00340  // the bounds are changed, but fisicaly memory does not realocating.
00341   void colDelBefore(int col,int colsNum,bool reAlloc = TRUE){
00342      change()->colDelBefore(col,colsNum,reAlloc);
00343   }
00344   void colDelAfter(int col,int colsNum, bool reAlloc =TRUE){
00345      change()->colDelAfter(col,colsNum,reAlloc);
00346   }
00347   void colDelRange(int col1,int col2, bool reAlloc =TRUE){
00348      change()->colDelRange(col1,col2,reAlloc);
00349   }
00350   void rowDelBefore(int row,int rowsNum,bool reAlloc = TRUE){
00351      change()->rowDelBefore(row,rowsNum,reAlloc);
00352   }
00353   void rowDelAfter(int row,int rowsNum, bool reAlloc =TRUE){
00354      change()->rowDelAfter(row,rowsNum,reAlloc);
00355   }
00356   void rowDelRange(int row1,int row2, bool reAlloc =TRUE){
00357      change()->rowDelRange(row1,row2,reAlloc);
00358   }
00359 
00360   int width( ) const { return look()->getWidth(); }  
00361     
00362   int height( ) const { return look()->getHeight(); }  
00363 
00364   // Index begins from 0, so the last column or row number differs from the 
00365   // height or width
00366   int lastCol( ) const { return look()->getWidth()-1; }  
00367     
00368   int lastRow( ) const { return look()->getHeight()-1; }  
00369   /////////////////////////////////////////////////////////////////////////
00370   //                                                                     //
00371   // I/O:                                                                //
00372   //                                                                     //
00373   /////////////////////////////////////////////////////////////////////////
00374 
00375   friend IStreamPoll operator >> ( istream& istr, DArray& M )
00376   {
00377     Chars errMesg;
00378     
00379     M.change()->readFrom(istr, errMesg);
00380     return IStreamPoll( istr, errMesg );
00381   }
00382  
00383   inline friend ostream& operator << ( ostream& o, const DArray& v ) {
00384  
00385         if (v.height()==0 || v.width()==0)   {o << "| |"<<endl; return o;}
00386 
00387         for ( int i = 0; i < v.height(); i++ ){
00388               o << "| ";
00389               for (int j=0;j<v.width();j++)
00390                        o << v[i][j] << ", ";
00391               if (i==v.height()-1) { o << "|."<<endl;}
00392                else {o <<"|"<< endl;};
00393         }
00394         return o;
00395     }
00396 
00397   /////////////////////////////////////////////////////////////////////////
00398   //                                                                     //
00399   // IPC tools:                                                          //
00400   //                                                                     //
00401   /////////////////////////////////////////////////////////////////////////
00402 
00403   friend ostream& operator < <R>( ostream& ostr, const DArray& DA );
00404   
00405   friend istream& operator > <R>( istream& istr, DArray& DA);
00406 
00407 
00408   protected :
00409         DArray( DArrayRep<R>* newrep ) : ObjectOf<DArrayRep <R> >(newrep) { }
00410 
00411 };
00412 //-----------------------------------------------------------------------//
00413 //---------------------- MatrixRow --------------------------------------//
00414 //-----------------------------------------------------------------------//
00415 // This is a private class specialy for DArray
00416 // It was made to provide operators to work with rows and to safe acces to elements
00417 
00418 template <class R> class MatrixRow {
00419 
00420 public:
00421    
00422    R& operator [] (int i) {   
00423     #if SAFETY > 0
00424          if (i < 0 || i >=width)
00425              error("DArray index out of bounds in MatrixRow::operator [] (int i)");
00426     #endif
00427     return row[rowBegin+i];
00428    }
00429    MatrixRow<R>& operator = (const MatrixRow<R>& mr){
00430      #if SAFETY > 0
00431          if (width != mr.width)
00432              error("DArray not equal length of rows in MatrixRow::operator ="
00433                                     "(const MatrixRow<R>&");
00434     #endif
00435     for (int i=0;i<width;i++)
00436          row[i+rowBegin] = mr.row[i+mr.rowBegin];
00437     return *this;
00438    }
00439 
00440    // Row of DArray is supposed to work with VectorOf, so we have
00441    // operators = and  operator VectorOf<R>()
00442    MatrixRow<R>& operator = (const VectorOf<R>& vc){
00443      #if SAFETY > 0
00444          if (width != vc.length())
00445              error("DArray not equal length of vector and row in MatrixRow::operator ="
00446                                     "(const VectorOf<R>&");
00447     #endif
00448     for (int i=rowBegin;i<rowBegin+width;i++)
00449          row[i] = vc[i-rowBegin];
00450     return *this;
00451    }
00452 
00453    operator VectorOf<R>(){
00454       VectorOf<R> tmpVc(width);
00455       for (int i=rowBegin;i<rowBegin+width;i++)
00456            tmpVc[i-rowBegin]=row[i];
00457       return tmpVc;
00458    }
00459   
00460    int length() const { return width; }
00461 private:
00462 
00463    friend class DArrayParser<R>;
00464    friend class DArrayRep<R>;
00465    friend class MatrixCell<R>;
00466 
00467 // Constructors, are hide because of private acces   
00468    MatrixRow(const int& len) : width(len) {
00469        if (len==0) return;
00470        row = new R[len];
00471        rowBegin = 0;
00472    }
00473    MatrixRow(const MatrixRow<R>& mr) {
00474       width = mr.width;
00475       row = new R[width];
00476       rowBegin = 0;
00477       for (int i=0;i<width;i++)
00478              row[i] = mr.row[i];
00479    }
00480    MatrixRow(const VectorOf<R>& vc) {
00481       width = vc.length();
00482       row = new R[width];
00483       rowBegin = 0;
00484       for (int i=0;i<width;i++)
00485              row[i] = vc[i];
00486    }
00487 
00488   ~MatrixRow() { delete [] row;}
00489 
00490    R* row;
00491 
00492    // Actualy width is needed only because we have to check the equality
00493    // of length between rows and vectors. rowBegin points to the first
00494    // row number, if there were deletions without reallocation of memory
00495    int width, rowBegin;
00496 };
00497 
00498 //-------------------------------------------------------------------------------//
00499 //------------------------------- DArrayParser ----------------------------------//
00500 //-------------------------------------------------------------------------------//
00501 
00502 
00503 // The DArray must have the form like 
00504 // [
00505 //   [ x11, x12, x13 ],
00506 //   [ x21, x22, x23 ],
00507 //   [ x31, x32, x33 ]
00508 // ]
00509 // The elements of DArray must have operator and default constructor
00510 
00511 template <class R> class DArrayParser : public WordParser {
00512 
00513 public:
00514 
00515   //////////////////////////////////////////////////////////////
00516   //                                                          //
00517   // Constructors:                                            //
00518   //                                                          //
00519   //////////////////////////////////////////////////////////////
00520 
00521   DArrayParser(istream &istr) : WordParser(istr) { }
00522   // Initialize the parser with the istream from which to read.
00523 
00524   // Destructor supplied by compiler.
00525 
00526   //////////////////////////////////////////////////////////////
00527   //                                                          //
00528   // Parser function:                                         //
00529   //                                                          //
00530   //////////////////////////////////////////////////////////////
00531     
00532   bool parseDArray( Chars& errMesg, MatrixRow<R> **M, int& MHeight,int& MWidth );
00533 
00534 protected:
00535   VectorOf<R> parseMatrixRow( Chars& errMesg );
00536 
00537 private:
00538 
00539   //////////////////////////////////////////////////////////////
00540   //                                                          //
00541   // Private functions:                                       //
00542   //                                                          //
00543   //////////////////////////////////////////////////////////////
00544 
00545   void deleteRows(  MatrixCell<R>* rows);
00546 
00547   /////////////////////////////////////////////////////////////////////////
00548   //                                                                     //
00549   //  Debugging stuff:                                                   //
00550   //                                                                     //
00551   /////////////////////////////////////////////////////////////////////////
00552 
00553 #ifdef DEBUG
00554 
00555   // friend int main( );
00556 
00557 #endif
00558 
00559 };
00560 // This class is a presentation of cell in list of MatrixRows.
00561 // It's used in parser.
00562 template <class R> class MatrixCell{
00563  private:
00564    friend class DArrayParser<R>;
00565    MatrixCell(const MatrixRow<R>& mr):content(mr),nextCell(NULL){}
00566    MatrixRow<R> content;
00567    MatrixCell* nextCell;
00568 };
00569 #endif
00570 
00571 
00572 
00573 
00574 
00575 
00576 
00577 
00578 
00579 

Generated at Tue Jun 19 09:49:35 2001 for Magnus Classes by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001