Main Page | Class Hierarchy | Class List | File List | Class Members | File Members | Related Pages

tables.H

Go to the documentation of this file.
00001 
00006 #ifndef _TABLES_H_
00007 #define _TABLES_H_
00008 
00009 #include <fstream>
00010 #include <sstream>
00011 #include <string>
00012 #include <iomanip>
00013 #include <iostream>
00014 #include <list>
00015 using namespace std;
00016 
00017 
00018 
00020 
00027 template <class T> class Tuple {
00028 
00029  protected:
00031   long tuple_size; 
00033   T* data;       
00035   char sep;     
00036   
00037  public:
00039 
00042   Tuple<T>(){
00043     data=0;
00044     tuple_size=0;
00045     sep=' ';
00046   }
00047 
00049 
00052   Tuple<T>(long thesize){
00053     data=new T[thesize];   
00054     tuple_size=thesize;
00055     sep=' ';
00056   }
00057 
00058   Tuple<T>(const Tuple<T>& that){
00059     data=0;
00060     tuple_size=0; 
00061     copy(that);
00062   }
00063 
00065   ~Tuple<T>(){
00066     delete[] data;
00067   }
00068 
00069   void block_copy(T *dest, const T *source, long amount){
00070     long i;
00071 
00072     for(i=0; i < amount; i++)
00073       dest[i]= source[i];
00074   }
00075 
00076 
00078 
00084   void resize(long thesize){
00085     T* temp;
00086     long j;
00087     
00088     if(thesize==tuple_size) {return;}
00089     temp=new T[thesize];
00090     j=(thesize > tuple_size) ? tuple_size : thesize; // j=min taille, tuplue_size
00091     block_copy(temp, data, j);
00092     delete[] data;
00093     data=temp;
00094     tuple_size=thesize; 
00095   }
00097 
00103   void copy(const Tuple<T>& that){
00104     if(tuple_size != that.tuple_size){
00105       tuple_size= that.tuple_size;      
00106       delete[] data;
00107       data=new T[tuple_size];
00108     }
00109     block_copy(data, that.data, tuple_size);
00110     sep=that.sep;
00111   }
00113 
00119   Tuple<T>& operator=(const Tuple<T>& that){
00120     copy(that);
00121     return *this;
00122   }
00124 
00136   void steal(Tuple<T>& that){
00137 
00138     delete[] data;
00139     data= that.data;
00140     tuple_size= that.tuple_size;
00141     // now erase that
00142     that.data= 0;
00143     that.tuple_size= 0;
00144   }
00146   long size() const {return tuple_size;}
00148   void set_sep(const char s){
00149     sep= s;
00150   }
00151 
00153 
00163   T& operator[](long i) {
00164     return data[i];
00165     }
00166 
00167   const T& operator[](long i) const {
00168     return data[i];
00169     }
00170 
00172   bool operator==(const Tuple<T>& that) const {
00173     long i=0;
00174 
00175      if(tuple_size != that.tuple_size){
00176        return false;
00177     }
00178     else {
00179       for(i=0; (i< tuple_size) && (data[i]==that.data[i]);i++);
00180       return i==tuple_size;
00181     }
00182   }
00183 
00184 
00186 
00196   friend ostream& operator<<(ostream& os, const Tuple<T>& M){
00197     long i;
00198 
00199     if(M.tuple_size == 0)
00200       return os;
00201 
00202     if(M.sep != 0) {    
00203       for(i=0; i < M.tuple_size-1; i++ )
00204         os << M[i] << M.sep;
00205       os << M[i];
00206     }
00207     else{
00208       for(i=0; i < M.tuple_size; i++ )
00209         {os << M[i];}
00210     }
00211     
00212     return os;
00213   }
00214 
00216   friend ofstream& operator<<(ofstream& file, Tuple<T>& M){
00217 
00218     file << M.tuple_size << " ";
00219     for(long i=0; i< M.tuple_size; i++)
00220       file << M.data[i] << " ";
00221     return file;
00222   }
00224   friend void operator>>(ifstream& file, Tuple<T>& M){
00225     long n;
00226     
00227     file >> n;
00228     M.resize(n);
00229     for(long i=0; i < M.tuple_size; i++)
00230       file >> M.data[i];
00231   }
00232 
00233 };
00234 
00235 
00236 
00238 
00256 template <class T> class Table : public Tuple<T> {
00257  protected:
00259   T** pdata;                  
00261   long nb_lines, nb_cols;    
00263   char left, right;           
00264   
00266 
00270   long col_width() const {            
00271     string s="";               
00272     ostringstream os;          
00273     long i,j,l;
00274     long n=0;
00275     
00276     for(i=0; i< nb_lines; i++)
00277       for(j=0; j< nb_cols; j++){
00278         os << pdata[i][j];
00279         s= os.str();
00280         l=s.length();
00281         n=(l>n) ? l : n;
00282         s="";
00283         os.str("");
00284       }
00285     
00286     return n; 
00287   }
00288 
00290 
00296   void write_ptrs(){
00297     pdata=new T*[nb_lines];
00298     for(long i=0; i < nb_lines; i++)
00299       pdata[i]=this->data + i*nb_cols;
00300   }
00301   
00302  public:
00304 
00307   Table<T>() : Tuple<T>() {
00308     nb_lines=0;
00309     nb_cols=0;
00310     pdata=0;
00311     left=right='|';
00312   }
00314 
00317   Table<T>(long i, long j) : Tuple<T>(i*j){
00318     nb_lines=i;
00319     nb_cols=j;
00320     write_ptrs();
00321     left=right='|';
00322   }
00324   Table<T>(const Table<T>& that) : Tuple<T>() {
00325     nb_lines=0;
00326     nb_cols=0;
00327     pdata=0;
00328     copy(that);
00329   }
00330 
00332   ~Table<T>(){
00333     delete[] pdata;
00334   }
00335 
00336   
00337 
00339 
00343   void resize(long i, long j){
00344     T *temp, *dest, *source; 
00345     long mi,mj,n,amount;
00346 
00347     if( i==nb_lines && j==nb_cols )
00348       return;
00349     temp=new T[i*j];        // creates new table;
00350     mi= i > nb_lines ? nb_lines : i; 
00351     mj= j > nb_cols ? nb_cols : j;
00352     for(n=0, dest=temp, source=this->data, amount=mj; n < mi; n++){ 
00353       block_copy(dest, source, amount); //populates temp
00354       dest+=j;
00355       source+=nb_cols;
00356     }
00357     delete[] this->data;
00358     this->data=temp;
00359     nb_lines=i;
00360     nb_cols=j;
00361     this->tuple_size=i*j;
00362     delete[] pdata;
00363     write_ptrs();
00364   }
00366   void copy(const Table<T>& that){
00367     Tuple<T>::copy(that);
00368     nb_lines=that.nb_lines;
00369     nb_cols=that.nb_cols;
00370     delete[] pdata;
00371     write_ptrs();
00372     left=that.left;
00373     right=that.right;
00374   }
00376   void steal(Table<T>& that){
00377     Tuple<T>::steal(that);
00378     delete[] pdata;
00379     pdata= that.pdata;
00380     nb_lines= that.nb_lines;
00381     nb_cols= that.nb_cols;
00382     // finish erasing that
00383     that.nb_lines= 0;
00384     that.nb_cols= 0;
00385     that.pdata= 0;
00386   }
00387 
00389   Table<T> operator=(const Table<T>& that){
00390     copy(that);
00391     return *this;
00392   }
00393 
00394   
00396 
00402   void readline(long i, const Tuple<T>& line){
00403     block_copy(pdata[i], &line[0], nb_cols);
00404   }
00405   
00406   void get_data_from_tuple_list(const list< Tuple<T> >& L){
00407     long li, col, i, j;
00408     typename list< Tuple<T> >::const_iterator it;
00409     
00410     //find out number of lines and cols
00411     li= col= 0;
00412     for(it= L.begin(); it!= L.end(); it++){
00413       li++;
00414       col= col > it->size() ? col : it->size();
00415     }
00416     //get the memory for it
00417     delete[] this->data;
00418     this->data= new T[col*li];
00419     nb_lines= li;
00420     nb_cols= col;
00421     this->tuple_size= col*li;
00422     delete[] pdata;
00423     write_ptrs();
00424 
00425     //now copy
00426     for(i=0, it=L.begin(); i<li; i++, it++)
00427       for(j=0; j< it->size(); j++)
00428         pdata[i][j]= (*it)[j];
00429 
00430   }
00431 
00432 
00433   
00435   T& operator()(long i, long j) {
00436     return pdata[i][j];
00437   }
00438 
00439   const T& operator()(long i, long j) const {
00440     return pdata[i][j];
00441   }
00443   long nlines() const {return nb_lines;}
00445   long ncols() const {return nb_cols;}
00447   void set_delim(char theleft, char theright){
00448     left=theleft;
00449     right=theright;
00450   }
00452   friend ostream& operator<<(ostream& os, const Table<T>& M){
00453     long w;
00454     long i,j;
00455     ostringstream s;
00456 
00457     w=M.col_width();
00458     for(i=0; i<M.nb_lines; i++){
00459       os << M.left;
00460       for(j=0; j< M.nb_cols-1; j++){
00461         s << M(i,j);        //using s seems necessary because of setw
00462         os << setw(w) << s.str(); // setw is only for the next operation, 
00463         s.str("");          // and printing M(i,j) might require several.
00464         os << M.sep;}
00465       s << M(i,j);
00466       os << setw(w) << s.str();
00467       s.str("");
00468       os << M.right << endl;
00469     }
00470     return os;
00471   }
00472 
00473 
00474 };
00475 
00476 
00477 #endif

Generated on Wed Jun 18 17:22:41 2008 for Pierre Guillot by  doxygen 1.3.9.1