itbl.C

00001 /*
00002  * Methods of class Itbl
00003  *
00004  *  (see file itbl.h for documentation)
00005  *
00006  */
00007 
00008 /*
00009  *   Copyright (c) 1999-2001 Philippe Grandclement
00010  *   Copyright (c) 1999-2003 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 itbl_C[] = "$Header: /cvsroot/Lorene/C++/Source/Itbl/itbl.C,v 1.6 2008/02/18 13:53:40 j_novak Exp $" ;
00032 
00033 /*
00034  * $Id: itbl.C,v 1.6 2008/02/18 13:53:40 j_novak Exp $
00035  * $Log: itbl.C,v $
00036  * Revision 1.6  2008/02/18 13:53:40  j_novak
00037  * Removal of special indentation instructions.
00038  *
00039  * Revision 1.5  2003/10/12 20:34:47  e_gourgoulhon
00040  * Suppressed the call to set_etat_zero() in the 1D constructor with
00041  * dimension 0, and replaced it by etat = ETATZERO.
00042  *
00043  * Revision 1.4  2003/10/11 16:44:17  e_gourgoulhon
00044  *
00045  * IMPORTANT CHANGE: the standard constructors set now the logical state
00046  * to ETATQCQ, and no longer to ETATNONDEF.
00047  *
00048  * Revision 1.3  2002/10/16 14:36:37  j_novak
00049  * Reorganization of #include instructions of standard C++, in order to
00050  * use experimental version 3 of gcc.
00051  *
00052  * Revision 1.2  2001/12/04 21:27:53  e_gourgoulhon
00053  *
00054  * All writing/reading to a binary file are now performed according to
00055  * the big endian convention, whatever the system is big endian or
00056  * small endian, thanks to the functions fwrite_be and fread_be
00057  *
00058  * Revision 1.1.1.1  2001/11/20 15:19:27  e_gourgoulhon
00059  * LORENE
00060  *
00061  * Revision 2.1  1999/11/23  13:17:09  eric
00062  * Le constructeur Itbl::Itbl(const Dim_tbl ) devient desormais
00063  *   tbl::Itbl(const Dim_tbl& ).
00064  * La taille zero est autorisee par le constructeur 1D.
00065  * Modif affichage.
00066  *
00067  * Revision 2.0  1999/11/17  16:04:38  phil
00068  * *** empty log message ***
00069  *
00070  *
00071  * $Header: /cvsroot/Lorene/C++/Source/Itbl/itbl.C,v 1.6 2008/02/18 13:53:40 j_novak Exp $
00072  *
00073  */
00074 
00075 
00076 // headers C
00077 #include <math.h>
00078 
00079 // headers Lorene
00080 #include "itbl.h"
00081 #include "utilitaires.h"
00082 
00083 
00084             //---------------//
00085             // Constructeurs //
00086             //---------------//
00087 
00088 
00089 // Constructeur 1D
00090 Itbl::Itbl(int n1) : etat(ETATQCQ), dim(n1) {
00091 
00092     if (n1 == 0) {
00093         t = 0x0 ; 
00094         etat = ETATZERO ;
00095     }
00096     else {
00097         assert(n1 > 0) ; 
00098         t = new int[n1] ;
00099     }
00100 }
00101 
00102 // Constructeur 2D
00103 Itbl::Itbl(int n1, int n0) : etat(ETATQCQ), dim(n1, n0) {
00104 
00105     t = new int[get_taille()] ;
00106 }
00107 
00108 // Constructeur 3D
00109 Itbl::Itbl(int n2, int n1, int n0) : etat(ETATQCQ), dim(n2, n1, n0) {
00110     
00111     t = new int[get_taille()] ;
00112 }
00113 
00114 // Constructeur a partir d'un Dim_tbl
00115 Itbl::Itbl(const Dim_tbl& dt) : etat(ETATQCQ), dim(dt) {
00116 
00117     if (get_taille() == 0) {
00118         set_etat_zero() ; 
00119     }
00120     else {
00121         t = new int[get_taille()] ;
00122     }
00123 }
00124 
00125 // Copie
00126 Itbl::Itbl(const Itbl& tc) : etat(tc.etat), dim(tc.dim) {
00127 
00128     // La valeur eventuelle
00129     if (tc.etat == ETATQCQ) {
00130     t = new int[get_taille()] ;
00131     for (int i=0 ; i<get_taille() ; i++) {
00132         t[i] = tc.t[i] ;
00133     }
00134     }
00135     else{
00136     t = 0x0 ; 
00137     }
00138     
00139 }
00140 
00141 // From file
00142 Itbl::Itbl(FILE* fd) : dim(fd) {
00143 
00144     fread_be(&etat, sizeof(int), 1, fd) ;       // etat
00145     
00146     // Le tableau
00147     if (etat == ETATQCQ) {
00148     t = new int[get_taille()] ;
00149     fread_be(t, sizeof(int), get_taille(), fd) ;        // le tableau
00150     }
00151     else{
00152     t = 0x0 ; 
00153     }
00154 }
00155 
00156             //-------------//
00157             // Destructeur //
00158             //-------------//
00159 
00160 Itbl::~Itbl() {
00161     if (t != 0x0) delete [] t ;
00162 }
00163 
00164             //-------------//
00165             // Affectation //
00166             //-------------//
00167 
00168 // From Itbl
00169 void Itbl::operator=(const Itbl& tx)
00170 {
00171     // Protection
00172     assert( dim == tx.dim ) ;
00173     assert(tx.get_etat() != ETATNONDEF) ;
00174 
00175     int n = get_taille() ;
00176     switch (tx.etat) {
00177     case ETATZERO:
00178     set_etat_zero() ;
00179     break ;
00180     
00181     case ETATQCQ:
00182     set_etat_qcq() ;
00183     for (int i=0 ; i<n ; i++) {
00184         t[i] = tx.t[i] ;
00185     }
00186     break ;
00187     
00188     default:
00189     cout << "Erreur bizarre !" << endl ;
00190     abort() ;
00191     break ;
00192     }
00193 }
00194 
00195 // From int
00196 void Itbl::operator=(int a)
00197 {
00198     if ( a == 0 ) {
00199     set_etat_zero() ;
00200     }
00201     else {
00202     int n = get_taille() ;
00203     if (n > 0) {
00204         set_etat_qcq() ;
00205         for (int i=0 ; i<n ; i++) {
00206         t[i] = a ;
00207         }
00208     }
00209     }
00210 }
00211 
00212    
00213             //------------//
00214             // Sauvegarde //
00215             //------------//
00216 
00217 // save in a file
00218 
00219 void Itbl::sauve(FILE* fd) const {
00220 
00221     dim.sauve(fd) ;                             // dim
00222     fwrite_be(&etat, sizeof(int), 1, fd) ;          // etat
00223     if (etat == ETATQCQ) {
00224     fwrite_be(t, sizeof(int), get_taille(), fd) ;       // le tableau
00225     }
00226 }
00227     
00228             //-----------------//
00229             // Gestion memoire //
00230             //-----------------//
00231 
00232 // Destructeur logique
00233 void Itbl::del_t() {
00234     if (t != 0x0) delete [] t ;
00235     t = 0x0 ;
00236     etat = ETATNONDEF ;
00237 }
00238 
00239 // ETATZERO
00240 void Itbl::set_etat_zero() {
00241     if (etat == ETATZERO) return ;
00242     del_t() ;
00243     etat = ETATZERO ;
00244 }
00245 
00246 // ETATNONDEF
00247 void Itbl::set_etat_nondef() {
00248     if (etat == ETATNONDEF) return ;
00249     del_t() ;
00250     etat = ETATNONDEF ;
00251 }
00252 
00253 // ETATQCQ
00254 void Itbl::set_etat_qcq() {
00255     if (etat == ETATQCQ) return ;
00256 
00257     // Protection
00258     assert( (etat == ETATZERO) || (etat == ETATNONDEF) ) ; // sinon...
00259 
00260     t = new int[get_taille()] ;
00261     etat = ETATQCQ ;
00262 }
00263 
00264 // ZERO hard
00265 void Itbl::annule_hard() {
00266     if (t == 0x0) {
00267     t = new int[get_taille()] ;
00268     }
00269     for (int i=0 ; i<get_taille() ; i++) {
00270     t[i] = 0 ;
00271     }
00272     etat = ETATQCQ ;
00273 }
00274 
00275 
00276             //------------------------//
00277             //        Display         //
00278             //------------------------//
00279             
00280 //-----------           
00281 // Operator<<
00282 //-----------           
00283 
00284 ostream& operator<<(ostream& o, const Itbl& t) {
00285     
00286     int ndim = t.get_ndim() ;
00287     o.precision(4);
00288     o.setf(ios::showpoint);
00289     o << "*** Itbl " << ndim << "D" << "   size: " ; 
00290     for (int i = 0; i<ndim-1; i++) {
00291     o << t.get_dim(i) << " x " ;
00292     } 
00293     o << t.get_dim(ndim-1) << endl ;
00294 
00295     if (t.get_etat() == ETATZERO) {
00296     o << "Identically ZERO" << endl ;
00297     return o ;
00298     }
00299 
00300     if (t.get_etat() == ETATNONDEF) {
00301     o << "UNDEFINED STATE" << endl ;
00302     return o ;
00303     }
00304 
00305     assert(t.etat == ETATQCQ) ;
00306     switch (ndim) {
00307 
00308     case 1 : {
00309         for (int i=0 ; i<t.get_dim(0) ; i++) {
00310         o << " " << t(i)  ;
00311         }
00312         o << endl ;
00313         break ;
00314     }
00315 
00316 
00317     case 2 : {
00318         for (int j=0 ; j<t.get_dim(1) ; j++) {
00319         o << " J = " << j << " : " << endl ;
00320         for (int i=0 ; i<t.get_dim(0) ; i++) {
00321             o << " " << t(j, i)  ;
00322         }
00323         o << endl ;
00324         }
00325         o << endl ;
00326         break ;
00327     }
00328         
00329     case 3 : {
00330         for (int k=0 ; k<t.get_dim(2) ; k++) {
00331         o << " K = " << k << " : " << endl ;
00332         for (int j=0 ; j<t.get_dim(1) ; j++) {
00333             o << " J = " << j << " : "  ;
00334             for (int i=0 ; i<t.get_dim(0) ; i++) {
00335             o << " " << t(k, j, i)  ;
00336             }
00337             o << endl ;
00338         }
00339         o << endl ;
00340         }
00341         o << endl ;
00342         break ;
00343     }
00344         
00345     default : {
00346         cout << "operator<< Itbl : unexpected dimension !" << endl ;
00347         cout << " ndim = " << ndim << endl ;    
00348         abort() ;
00349         break ;
00350     }
00351     }
00352     return o ;
00353 }

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