tbl.C

00001 /*
00002  * Methods of class Tbl
00003  *
00004  *  (see file tbl.h for documentation)
00005  *
00006  */
00007 
00008 /*
00009  *   Copyright (c) 1999-2000 Jean-Alain Marck
00010  *   Copyright (c) 1999-2001 Eric Gourgoulhon
00011  *
00012  *   This file is part of LORENE.
00013  *
00014  *   LORENE is free software; you can redistribute it and/or modify
00015  *   it under the terms of the GNU General Public License as published by
00016  *   the Free Software Foundation; either version 2 of the License, or
00017  *   (at your option) any later version.
00018  *
00019  *   LORENE is distributed in the hope that it will be useful,
00020  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00021  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00022  *   GNU General Public License for more details.
00023  *
00024  *   You should have received a copy of the GNU General Public License
00025  *   along with LORENE; if not, write to the Free Software
00026  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00027  *
00028  */
00029 
00030 
00031 char tbl_C[] = "$Header: /cvsroot/Lorene/C++/Source/Tbl/tbl.C,v 1.9 2008/02/18 13:53:47 j_novak Exp $" ;
00032 
00033 /*
00034  * $Id: tbl.C,v 1.9 2008/02/18 13:53:47 j_novak Exp $
00035  * $Log: tbl.C,v $
00036  * Revision 1.9  2008/02/18 13:53:47  j_novak
00037  * Removal of special indentation instructions.
00038  *
00039  * Revision 1.8  2006/09/25 10:01:50  p_grandclement
00040  * Addition of N-dimensional Tbl
00041  *
00042  * Revision 1.7  2003/11/03 13:53:20  j_novak
00043  * Yet another efficiency improvement.
00044  *
00045  * Revision 1.6  2003/10/19 19:58:56  e_gourgoulhon
00046  * Slightly improved operator<<.
00047  *
00048  * Revision 1.5  2003/02/26 10:47:11  j_novak
00049  * The copy of a Tbl to another has been improved in speed.
00050  *
00051  * Revision 1.4  2002/10/16 14:37:13  j_novak
00052  * Reorganization of #include instructions of standard C++, in order to
00053  * use experimental version 3 of gcc.
00054  *
00055  * Revision 1.3  2002/09/24 08:32:07  e_gourgoulhon
00056  *
00057  * Added constructor from Matrice.
00058  *
00059  * Revision 1.2  2001/12/04 21:27:54  e_gourgoulhon
00060  *
00061  * All writing/reading to a binary file are now performed according to
00062  * the big endian convention, whatever the system is big endian or
00063  * small endian, thanks to the functions fwrite_be and fread_be
00064  *
00065  * Revision 1.1.1.1  2001/11/20 15:19:27  e_gourgoulhon
00066  * LORENE
00067  *
00068  * Revision 2.13  1999/11/24  16:00:43  eric
00069  * Modif affichage dimensions dans affiche_seuil.
00070  *
00071  * Revision 2.12  1999/11/23  13:32:44  eric
00072  * Le constructeur Tbl::Tbl(const Dim_tbl ) devient Tbl::Tbl(const Dim_tbl& ).
00073  * Le constructeur Tbl::Tbl(const Grille3d* ) devient
00074  *   Tbl(const Grille3d& ).
00075  * Modif affichage.
00076  *
00077  * Revision 2.11  1999/11/15  16:36:36  eric
00078  * Le membre dim est desormais un Dim_tbl et non plus un pointeur sur un
00079  * Dim_tbl.
00080  *
00081  * Revision 2.10  1999/10/29  15:45:44  eric
00082  * Ajout des cas 1-D et 2-D dans operator<<
00083  *
00084  * Revision 2.9  1999/10/29  15:05:16  eric
00085  * Suppression des fonctions membres min() et max():
00086  * elles deviennent des fonctions externes.
00087  *
00088  * Revision 2.8  1999/10/21  14:37:34  eric
00089  * *** empty log message ***
00090  *
00091  * Revision 2.7  1999/10/18  15:06:13  eric
00092  * La fonction membre annule() est rebaptisee annule_hard().
00093  *
00094  * Revision 2.6  1999/10/15  13:58:39  eric
00095  * Modification de l'affichage (operator<<).
00096  *
00097  * Revision 2.5  1999/10/01  12:36:08  eric
00098  * Ajout de la fonction affiche_seuil.
00099  *
00100  * Revision 2.4  1999/10/01  10:09:52  eric
00101  * 0 -> double(0)
00102  *
00103  * Revision 2.3  1999/09/30  12:51:46  eric
00104  * Enleve l'include "grilles.h" non necessaire.
00105  *
00106  * Revision 2.2  1999/09/24  14:24:23  eric
00107  * Depoussierage, changement de prototypes, etc...
00108  *
00109  * Revision 2.1  1999/03/02  16:49:42  eric
00110  * *** empty log message ***
00111  *
00112  * Revision 2.0  1999/02/15  10:42:45  hyc
00113  * *** empty log message ***
00114  *
00115  *
00116  * $Header: /cvsroot/Lorene/C++/Source/Tbl/tbl.C,v 1.9 2008/02/18 13:53:47 j_novak Exp $
00117  *
00118  */
00119 
00120 // headers C
00121 #include <math.h>
00122 
00123 // headers Lorene
00124 #include "itbl.h"
00125 #include "tbl.h"
00126 #include "grilles.h"
00127 #include "matrice.h"
00128 #include "utilitaires.h"
00129 
00130 
00131             //---------------//
00132             // Constructeurs //
00133             //---------------//
00134 
00135 
00136 // Constructeur 1D
00137 Tbl::Tbl(int n1) : etat(ETATNONDEF), dim(n1), t(0x0) {}
00138 
00139 // Constructeur 2D
00140 Tbl::Tbl(int n1, int n0) : etat(ETATNONDEF), dim(n1, n0), t(0x0) {}
00141 
00142 // Constructeur 3D
00143 Tbl::Tbl(int n2, int n1, int n0) : etat(ETATNONDEF), dim(n2, n1, n0), t(0x0) {}
00144 
00145 // Constructeur a partir d'une grille 3D
00146 Tbl::Tbl(const Grille3d& g) : etat(ETATNONDEF), 
00147                   dim(g.get_np(), g.get_nt(), g.get_nr()), 
00148                   t(0x0) {}
00149                   
00150 // Constructeur a partir d'un Itbl
00151 Tbl::Tbl(Itbl sizes) : etat(ETATNONDEF), 
00152                   dim(1), 
00153                   t(0x0) {
00154                   
00155     int n = sizes.get_dim(0) ;
00156     int* dims = new int[n] ;
00157     for (int i=0 ; i<n ; i++)
00158         dims[i] = sizes(i) ;
00159     Dim_tbl new_dim (n, dims) ;
00160     dim = new_dim ; 
00161     delete [] dims ;
00162 }
00163                   
00164 // Constructeur a partir d'un Dim_tbl
00165 Tbl::Tbl(const Dim_tbl& dt) : etat(ETATNONDEF), dim(dt), t(0x0) {}
00166 
00167 // Copie
00168 Tbl::Tbl(const Tbl& tc) : etat(tc.etat), dim(tc.dim) {
00169 
00170     // La valeur eventuelle
00171     if (tc.etat == ETATQCQ) {
00172       int n = dim.taille ;
00173     t = new double[n] ;
00174     double* tin = tc.t ; 
00175     double* tout = t ;
00176     for (int i=0 ; i<n ; i++) {
00177       *tout = *tin ;
00178       tout++ ; tin++ ; 
00179     }
00180     }
00181     else{
00182     t = 0x0 ; 
00183     }
00184     
00185 }
00186 
00187 // From file
00188 Tbl::Tbl(FILE* fd) : dim(fd) {
00189 
00190     fread_be(&etat, sizeof(int), 1, fd) ;       // etat
00191     
00192     // Le tableau
00193     if (etat == ETATQCQ) {
00194     t = new double[get_taille()] ;
00195     fread_be(t, sizeof(double), get_taille(), fd) ;     // le tableau
00196     }
00197     else{
00198     t = 0x0 ; 
00199     }
00200 }
00201 
00202 // From a matrix
00203 Tbl::Tbl(const Matrice& aa) : etat(aa.get_etat()),
00204                               dim( (aa.get_array()).dim ) {
00205 
00206     int nbl = dim.dim[1] ;
00207     int nbc = dim.dim[0] ;
00208     
00209     // Special case of one row :
00210     if (nbl == 1)  {
00211         dim.ndim = 1 ;
00212     }
00213     
00214     // Special case of one column :
00215     if (nbc == 1)  {
00216         dim.ndim = 1 ;
00217         dim.dim[0] = dim.dim[1] ;
00218         dim.dim[1] = 1 ;
00219     }
00220     
00221     if (etat == ETATQCQ) {
00222 
00223         t = new double[get_taille()] ;
00224         
00225         Tbl taa = aa.get_array() ;
00226         double* ta = taa.t ;
00227         
00228         for (int i=0 ; i<get_taille() ; i++) {
00229             t[i] = ta[i] ;
00230         }
00231         
00232     }
00233     else{
00234         t = 0x0 ;
00235     }
00236 
00237 }
00238 
00239             //-------------//
00240             // Destructeur //
00241             //-------------//
00242 
00243 Tbl::~Tbl() {
00244     delete [] t ;
00245 }
00246 
00247             //-------------//
00248             // Affectation //
00249             //-------------//
00250 
00251 // From Tbl
00252 void Tbl::operator=(const Tbl& tx)
00253 {
00254     // Protection
00255     assert( dim == tx.dim ) ;
00256     assert(tx.get_etat() != ETATNONDEF) ;
00257 
00258     switch (tx.etat) {
00259     case ETATZERO:
00260     set_etat_zero() ;
00261     break ;
00262     
00263     case ETATQCQ: {
00264       set_etat_qcq() ;
00265       int n = get_taille() ;
00266       double* tin = tx.t ;
00267       double* tout = t ;
00268       for (int i=0 ; i<n ; i++) {
00269     *tout = *tin ;
00270     tout++ ;
00271     tin ++ ;
00272       }
00273       break ;
00274     }
00275     default:
00276       cout << "Erreur bizarre !" << endl ;
00277       abort() ;
00278       break ;
00279     }
00280 }
00281 
00282 // From double
00283 void Tbl::operator=(double a)
00284 {
00285     if ( a == double(0) ) {
00286     set_etat_zero() ;
00287     }
00288     else {
00289     int n = get_taille() ;
00290     set_etat_qcq() ;
00291     for (int i=0 ; i<n ; i++) {
00292         t[i] = a ;
00293     }
00294     }
00295 }
00296 
00297 // From int
00298 void Tbl::operator=(int m)
00299 {
00300     if (m == 0) {
00301     set_etat_zero() ;
00302     }
00303     else {
00304     int n = get_taille() ;
00305     set_etat_qcq() ;
00306     for (int i=0 ; i<n ; i++) {
00307         t[i] = m ;
00308     }
00309     }
00310 }
00311 
00312     
00313             //------------//
00314             // Sauvegarde //
00315             //------------//
00316 
00317 // save in a file
00318 
00319 void Tbl::sauve(FILE* fd) const {
00320 
00321     dim.sauve(fd) ;                             // dim
00322     fwrite_be(&etat, sizeof(int), 1, fd) ;          // etat
00323     if (etat == ETATQCQ) {
00324     fwrite_be(t, sizeof(double), get_taille(), fd) ;   // le tableau
00325     }
00326 }
00327     
00328             //-----------------//
00329                     // Gestion memoire //
00330             //-----------------//
00331 
00332 // Destructeur logique
00333 void Tbl::del_t() {
00334     delete [] t ;
00335     t = 0x0 ;
00336     etat = ETATNONDEF ;
00337 }
00338 
00339 // ETATZERO
00340 void Tbl::set_etat_zero() {
00341     if (etat == ETATZERO) return ;
00342     del_t() ;
00343     etat = ETATZERO ;
00344 }
00345 
00346 // ETATNONDEF
00347 void Tbl::set_etat_nondef() {
00348     if (etat == ETATNONDEF) return ;
00349     del_t() ;
00350     etat = ETATNONDEF ;
00351 }
00352 
00353 // ETATQCQ
00354 void Tbl::set_etat_qcq() {
00355     if (etat == ETATQCQ) return ;
00356 
00357     // Protection
00358     assert( (etat == ETATZERO) || (etat == ETATNONDEF) ) ; // sinon...
00359 
00360     t = new double[get_taille()] ;
00361     etat = ETATQCQ ;
00362 }
00363 
00364 // ZERO hard
00365 void Tbl::annule_hard() {
00366     if (t == 0x0) {
00367     t = new double[get_taille()] ;
00368     }
00369     for (int i=0 ; i<get_taille() ; i++) {
00370     t[i] = 0. ;
00371     }
00372     etat = ETATQCQ ;
00373 }
00374 
00375 
00376             //------------------------//
00377             //  Display       //
00378             //------------------------//
00379             
00380 //-----------           
00381 // Operator<<
00382 //-----------           
00383 
00384 ostream& operator<<(ostream& o, const Tbl& t) {
00385     
00386     int ndim = t.get_ndim() ;
00387     o.precision(4);
00388     o.setf(ios::showpoint);
00389     o << "*** Tbl " << ndim << "D" << "   size: " ; 
00390     for (int i = 0; i<ndim-1; i++) {
00391     o << t.get_dim(i) << " x " ;
00392     } 
00393     o << t.get_dim(ndim-1) << "  =  " << t.get_taille() 
00394       << endl ;
00395 
00396 
00397     if (t.get_etat() == ETATZERO) {
00398     o << "Identically ZERO" << endl ;
00399     return o ;
00400     }
00401 
00402     if (t.get_etat() == ETATNONDEF) {
00403     o << "UNDEFINED STATE" << endl ;
00404     return o ;
00405     }
00406 
00407     assert(t.etat == ETATQCQ) ;
00408     switch (ndim) {
00409 
00410     case 1 : {
00411         for (int i=0 ; i<t.get_dim(0) ; i++) {
00412         o << " " << t(i)  ;
00413         }
00414         o << endl ;
00415         break ;
00416     }
00417 
00418 
00419     case 2 : {
00420         for (int j=0 ; j<t.get_dim(1) ; j++) {
00421         o << " j = " << j << " : " << endl ;
00422         for (int i=0 ; i<t.get_dim(0) ; i++) {
00423             o << " " << t(j, i)  ;
00424         }
00425         o << endl ;
00426         }
00427         o << endl ;
00428         break ;
00429     }
00430         
00431     case 3 : {
00432         for (int k=0 ; k<t.get_dim(2) ; k++) {
00433         o << "k = " << k << " : " << endl ;
00434         for (int j=0 ; j<t.get_dim(1) ; j++) {
00435             o << "j = " << j << " : "  ;
00436             for (int i=0 ; i<t.get_dim(0) ; i++) {
00437             o << " " << t(k, j, i)  ;
00438             }
00439             o << endl ;
00440         }
00441         o << endl ;
00442         }
00443         o << endl ;
00444         break ;
00445     }
00446         
00447     default : {
00448         cout << "operator<< Tbl : unexpected dimension !" << endl ;
00449         cout << " ndim = " << ndim << endl ;    
00450         abort() ;
00451         break ;
00452     }
00453     }
00454     return o ;
00455 }
00456 
00457 //---------------
00458 // Affiche_seuil
00459 //---------------
00460 
00461 void Tbl::affiche_seuil(ostream& ost, int precis,  double seuil) const {
00462 
00463     int ndim = get_ndim() ;
00464     ost << "*** Tbl " << ndim << "D" << "   size: " ; 
00465     for (int i = 0; i<ndim-1; i++) {
00466     ost << get_dim(i) << " x " ;
00467     } 
00468     ost << get_dim(ndim-1) << "  =  " << get_taille() << endl ; 
00469 
00470     // Cas particuliers
00471     //-----------------
00472 
00473     if (etat == ETATNONDEF) {
00474     ost << "    state: UNDEFINED" << endl ;
00475     return ;
00476     }
00477 
00478     if (etat == ETATZERO) {
00479     ost << "    state: ZERO" << endl ;
00480     return ;
00481     }
00482     
00483     // Affichage des elements du tableau 
00484     //----------------------------------
00485     
00486     ost << "    threshold for display : " << seuil << endl ; 
00487     ost.precision(precis);
00488     ost.setf(ios::showpoint);
00489 
00490     switch (get_ndim()) {
00491     case 1 : {                  // cas 1-D
00492 
00493         for (int i=0; i<get_dim(0); i++) {
00494         ost <<  " " << setw(precis) << (*this)(i)  ;
00495         }
00496         ost << endl ;
00497         break ;
00498     }
00499 
00500     case 2 : {              // cas 2-D
00501 
00502         for (int j=0; j<get_dim(1); j++) {
00503         ost <<  " #j=" << j << " : "  ;
00504         for (int i=0; i<get_dim(0); i++){
00505             ost <<  " " << setw(precis) << (*this)(j, i)  ;
00506         }
00507         ost << endl;
00508         }
00509         ost << endl;
00510         break;
00511     }
00512 
00513     case 3 : {              // cas 3-D
00514         for (int k=0; k<get_dim(2); k++) {
00515         for (int j=0; j<get_dim(1); j++){
00516             int test_imp = 0 ;
00517             for (int i=0; i<get_dim(0); i++){
00518             if ( fabs( (*this)(k, j, i) ) >= seuil ) 
00519                     test_imp = 1 ; 
00520             }
00521             if (test_imp == 1 ) {
00522             ost <<  " #k=" << k <<",j=" << j << " : "  ;
00523             for (int i=0; i<get_dim(0); i++){
00524                 ost <<  " " << setw(precis) << (*this)(k, j, i) ;
00525             }
00526             ost << endl ;
00527             }
00528         }
00529         }
00530         ost << endl;
00531         break;
00532     }
00533     
00534     default : {
00535         cout << "Tbl:affiche_seuil : unexpected dimension !" << endl ;
00536         cout << " get_ndim() = " << get_ndim() << endl ;    
00537         abort() ; 
00538         break;
00539     }               
00540     
00541     }     // fin du switch sur le nombre de dimensions
00542 
00543     // On restaure l'etat du flot ost a ses valeurs standards:
00544     ost.precision(6);
00545     ost.unsetf(ios::showpoint);
00546 }
00547 
00548 
00549 

Generated on Tue Feb 7 01:35:20 2012 for LORENE by  doxygen 1.4.6