dim_tbl.C

00001 /*
00002  * Methods of class Dim_tbl
00003  *
00004  *  (see file dim_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 dim_tbl[] = "$Header: /cvsroot/Lorene/C++/Source/Tbl/dim_tbl.C,v 1.5 2006/09/26 07:21:07 p_grandclement Exp $" ;
00032 
00033 /*
00034  * $Id: dim_tbl.C,v 1.5 2006/09/26 07:21:07 p_grandclement Exp $
00035  * $Log: dim_tbl.C,v $
00036  * Revision 1.5  2006/09/26 07:21:07  p_grandclement
00037  * Minor change in the indices
00038  *
00039  * Revision 1.4  2006/09/25 10:01:50  p_grandclement
00040  * Addition of N-dimensional Tbl
00041  *
00042  * Revision 1.3  2002/10/16 14:37:13  j_novak
00043  * Reorganization of #include instructions of standard C++, in order to
00044  * use experimental version 3 of gcc.
00045  *
00046  * Revision 1.2  2001/12/04 21:27:54  e_gourgoulhon
00047  *
00048  * All writing/reading to a binary file are now performed according to
00049  * the big endian convention, whatever the system is big endian or
00050  * small endian, thanks to the functions fwrite_be and fread_be
00051  *
00052  * Revision 1.1  2001/11/23 09:37:51  e_gourgoulhon
00053  * dim_tbl.C now in directory Tbl
00054  *
00055  * Revision 1.1.1.1  2001/11/20 15:19:30  e_gourgoulhon
00056  * LORENE
00057  *
00058  * Revision 2.5  1999/11/23  12:16:49  eric
00059  * Dimension 0 autorisee dans le constructeur 1D.
00060  *
00061  * Revision 2.4  1999/09/24  14:24:09  eric
00062  * Declaration de methodes const.
00063  *
00064  * Revision 2.3  1999/09/22  11:24:53  eric
00065  * Correction erreur ecriture/lecture fichier de taille (double->int).
00066  * Initialisation de ndim.
00067  *
00068  * Revision 2.2  1999/09/16  16:24:08  eric
00069  * *** empty log message ***
00070  *
00071  * Revision 2.1  1999/03/01  14:56:17  eric
00072  * *** empty log message ***
00073  *
00074  * Revision 2.0  1999/02/15  10:42:45  hyc
00075  * *** empty log message ***
00076  *
00077  * $Header: /cvsroot/Lorene/C++/Source/Tbl/dim_tbl.C,v 1.5 2006/09/26 07:21:07 p_grandclement Exp $
00078  *
00079  */
00080 
00081 // Headers C
00082 #include <assert.h>
00083 
00084 // Headers Lorene
00085 #include "dim_tbl.h"
00086 #include "utilitaires.h"
00087 
00088             //---------------//
00089             // Constructeurs //
00090             //---------------//
00091 
00092 // 1D constructor
00093 Dim_tbl::Dim_tbl(int i) : ndim(1) {
00094     assert(i >= 0) ;            // The dimension 0 is allowed
00095     dim = new int[ndim] ;
00096     dim[0] = i ;
00097     taille = i ;
00098 }
00099 // 2D constructor
00100 Dim_tbl::Dim_tbl(int j, int i) : ndim(2) {
00101     assert(j > 0) ;
00102     assert(i > 0) ;
00103     dim = new int[ndim] ;
00104     dim[0] = i ; dim[1] = j ;
00105     taille = i * j ;
00106 }
00107 // 3D constructor
00108 Dim_tbl::Dim_tbl(int k, int j, int i) : ndim(3) {
00109     assert(k > 0) ;
00110     assert(j > 0) ;
00111     assert(i > 0) ;
00112     dim = new int[ndim] ;
00113     dim[0] = i ; dim[1] = j ; dim[2] = k ;
00114     taille = i * j * k ;
00115 }
00116     
00117 // N-dimensional constructor
00118 Dim_tbl::Dim_tbl(int n, int* sizes) : ndim(n) {
00119     for (int i=0 ; i<ndim ; i++)
00120         assert(sizes[i] > 0) ;
00121     dim = new int[ndim] ;
00122     taille = 1 ;
00123     for (int i=0 ; i<ndim ; i++) {
00124         dim[i] = sizes[ndim-i-1] ;
00125     taille *= sizes[i] ;
00126     }
00127 }
00128 
00129 // Copy
00130 Dim_tbl::Dim_tbl(const Dim_tbl & titi) : ndim(titi.ndim) {
00131     dim = new int[ndim] ;
00132     for (int i=0 ; i<ndim ; i++) {
00133         dim[i] = titi.dim[i] ;
00134     }
00135     taille = titi.taille ;
00136 }
00137     
00138 // From a file
00139 Dim_tbl::Dim_tbl(FILE* fd) {
00140     fread_be(&ndim, sizeof(int), 1, fd) ;       // ndim
00141     dim = new int[ndim] ;
00142     fread_be(dim, sizeof(int), ndim, fd) ;      // dim[]
00143     taille = dim[0] ;
00144     for (int i=1; i<ndim; i++) {
00145     taille *= dim[i] ; 
00146     }
00147 }
00148 
00149             //--------------//
00150             // Destructeurs //
00151             //--------------//
00152 
00153 // Destructeur
00154 Dim_tbl::~Dim_tbl() {
00155     delete [] dim ;
00156 }
00157 
00158             //-------------//
00159             // Affectation //
00160             //-------------//
00161 
00162 // From Dim_tbl
00163 void Dim_tbl::operator=(const Dim_tbl & titi) {
00164     ndim = titi.ndim ;
00165     delete [] dim ;
00166     dim = new int[ndim] ;
00167     for (int i=0 ; i<ndim ; i++) {
00168         dim[i] = titi.dim[i] ;
00169     }
00170     taille = titi.taille ;
00171 }
00172     
00173             //------------//
00174             // Sauvegarde //
00175             //------------//
00176 
00177 // Save in a file
00178 void Dim_tbl::sauve(FILE* fd) const {
00179     fwrite_be(&ndim, sizeof(int), 1, fd) ;          // ndim
00180     fwrite_be(dim, sizeof(int), ndim, fd) ;     // dim[]
00181 }
00182     
00183             //------------//
00184             // Impression //
00185             //------------//
00186 
00187 // Operateurs <<
00188 ostream& operator<<(ostream& o, const Dim_tbl & titi) {
00189     o << titi.ndim << " dimension(s):" ;
00190     for (int i=0 ; i<titi.ndim ; i++) {
00191         o << " " << titi.dim[i] ;
00192     }
00193     return o ;
00194 }
00195 
00196 
00197             //---------------------//
00198                 // Operateurs logiques //
00199             //---------------------//
00200 
00201 bool Dim_tbl::operator==(const Dim_tbl & ti) const {
00202     
00203     // Peut-etre faux ?
00204     if (ndim != ti.ndim) return false ;
00205     for (int i=0 ; i<ndim ; i++) {
00206         if (dim[i] != ti.dim[i]) return false ;
00207     }
00208     
00209     // Non ! juste
00210     return true ;
00211 }

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