LORENE
itbl.C
1 /*
2  * Methods of class Itbl
3  *
4  * (see file itbl.h for documentation)
5  *
6  */
7 
8 /*
9  * Copyright (c) 1999-2001 Philippe Grandclement
10  * Copyright (c) 1999-2003 Eric Gourgoulhon
11  *
12  * This file is part of LORENE.
13  *
14  * LORENE is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 2 of the License, or
17  * (at your option) any later version.
18  *
19  * LORENE is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with LORENE; if not, write to the Free Software
26  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27  *
28  */
29 
30 
31 char itbl_C[] = "$Header: /cvsroot/Lorene/C++/Source/Itbl/itbl.C,v 1.8 2014/10/13 08:53:01 j_novak Exp $" ;
32 
33 /*
34  * $Id: itbl.C,v 1.8 2014/10/13 08:53:01 j_novak Exp $
35  * $Log: itbl.C,v $
36  * Revision 1.8 2014/10/13 08:53:01 j_novak
37  * Lorene classes and functions now belong to the namespace Lorene.
38  *
39  * Revision 1.7 2014/10/06 15:13:11 j_novak
40  * Modified #include directives to use c++ syntax.
41  *
42  * Revision 1.6 2008/02/18 13:53:40 j_novak
43  * Removal of special indentation instructions.
44  *
45  * Revision 1.5 2003/10/12 20:34:47 e_gourgoulhon
46  * Suppressed the call to set_etat_zero() in the 1D constructor with
47  * dimension 0, and replaced it by etat = ETATZERO.
48  *
49  * Revision 1.4 2003/10/11 16:44:17 e_gourgoulhon
50  *
51  * IMPORTANT CHANGE: the standard constructors set now the logical state
52  * to ETATQCQ, and no longer to ETATNONDEF.
53  *
54  * Revision 1.3 2002/10/16 14:36:37 j_novak
55  * Reorganization of #include instructions of standard C++, in order to
56  * use experimental version 3 of gcc.
57  *
58  * Revision 1.2 2001/12/04 21:27:53 e_gourgoulhon
59  *
60  * All writing/reading to a binary file are now performed according to
61  * the big endian convention, whatever the system is big endian or
62  * small endian, thanks to the functions fwrite_be and fread_be
63  *
64  * Revision 1.1.1.1 2001/11/20 15:19:27 e_gourgoulhon
65  * LORENE
66  *
67  * Revision 2.1 1999/11/23 13:17:09 eric
68  * Le constructeur Itbl::Itbl(const Dim_tbl ) devient desormais
69  * tbl::Itbl(const Dim_tbl& ).
70  * La taille zero est autorisee par le constructeur 1D.
71  * Modif affichage.
72  *
73  * Revision 2.0 1999/11/17 16:04:38 phil
74  * *** empty log message ***
75  *
76  *
77  * $Header: /cvsroot/Lorene/C++/Source/Itbl/itbl.C,v 1.8 2014/10/13 08:53:01 j_novak Exp $
78  *
79  */
80 
81 
82 // headers C
83 #include <cmath>
84 
85 // headers Lorene
86 #include "itbl.h"
87 #include "utilitaires.h"
88 
89 
90  //---------------//
91  // Constructeurs //
92  //---------------//
93 
94 
95 // Constructeur 1D
96 namespace Lorene {
97 Itbl::Itbl(int n1) : etat(ETATQCQ), dim(n1) {
98 
99  if (n1 == 0) {
100  t = 0x0 ;
101  etat = ETATZERO ;
102  }
103  else {
104  assert(n1 > 0) ;
105  t = new int[n1] ;
106  }
107 }
108 
109 // Constructeur 2D
110 Itbl::Itbl(int n1, int n0) : etat(ETATQCQ), dim(n1, n0) {
111 
112  t = new int[get_taille()] ;
113 }
114 
115 // Constructeur 3D
116 Itbl::Itbl(int n2, int n1, int n0) : etat(ETATQCQ), dim(n2, n1, n0) {
117 
118  t = new int[get_taille()] ;
119 }
120 
121 // Constructeur a partir d'un Dim_tbl
122 Itbl::Itbl(const Dim_tbl& dt) : etat(ETATQCQ), dim(dt) {
123 
124  if (get_taille() == 0) {
125  set_etat_zero() ;
126  }
127  else {
128  t = new int[get_taille()] ;
129  }
130 }
131 
132 // Copie
133 Itbl::Itbl(const Itbl& tc) : etat(tc.etat), dim(tc.dim) {
134 
135  // La valeur eventuelle
136  if (tc.etat == ETATQCQ) {
137  t = new int[get_taille()] ;
138  for (int i=0 ; i<get_taille() ; i++) {
139  t[i] = tc.t[i] ;
140  }
141  }
142  else{
143  t = 0x0 ;
144  }
145 
146 }
147 
148 // From file
149 Itbl::Itbl(FILE* fd) : dim(fd) {
150 
151  fread_be(&etat, sizeof(int), 1, fd) ; // etat
152 
153  // Le tableau
154  if (etat == ETATQCQ) {
155  t = new int[get_taille()] ;
156  fread_be(t, sizeof(int), get_taille(), fd) ; // le tableau
157  }
158  else{
159  t = 0x0 ;
160  }
161 }
162 
163  //-------------//
164  // Destructeur //
165  //-------------//
166 
168  if (t != 0x0) delete [] t ;
169 }
170 
171  //-------------//
172  // Affectation //
173  //-------------//
174 
175 // From Itbl
176 void Itbl::operator=(const Itbl& tx)
177 {
178  // Protection
179  assert( dim == tx.dim ) ;
180  assert(tx.get_etat() != ETATNONDEF) ;
181 
182  int n = get_taille() ;
183  switch (tx.etat) {
184  case ETATZERO:
185  set_etat_zero() ;
186  break ;
187 
188  case ETATQCQ:
189  set_etat_qcq() ;
190  for (int i=0 ; i<n ; i++) {
191  t[i] = tx.t[i] ;
192  }
193  break ;
194 
195  default:
196  cout << "Erreur bizarre !" << endl ;
197  abort() ;
198  break ;
199  }
200 }
201 
202 // From int
203 void Itbl::operator=(int a)
204 {
205  if ( a == 0 ) {
206  set_etat_zero() ;
207  }
208  else {
209  int n = get_taille() ;
210  if (n > 0) {
211  set_etat_qcq() ;
212  for (int i=0 ; i<n ; i++) {
213  t[i] = a ;
214  }
215  }
216  }
217 }
218 
219 
220  //------------//
221  // Sauvegarde //
222  //------------//
223 
224 // save in a file
225 
226 void Itbl::sauve(FILE* fd) const {
227 
228  dim.sauve(fd) ; // dim
229  fwrite_be(&etat, sizeof(int), 1, fd) ; // etat
230  if (etat == ETATQCQ) {
231  fwrite_be(t, sizeof(int), get_taille(), fd) ; // le tableau
232  }
233 }
234 
235  //-----------------//
236  // Gestion memoire //
237  //-----------------//
238 
239 // Destructeur logique
240 void Itbl::del_t() {
241  if (t != 0x0) delete [] t ;
242  t = 0x0 ;
243  etat = ETATNONDEF ;
244 }
245 
246 // ETATZERO
248  if (etat == ETATZERO) return ;
249  del_t() ;
250  etat = ETATZERO ;
251 }
252 
253 // ETATNONDEF
255  if (etat == ETATNONDEF) return ;
256  del_t() ;
257  etat = ETATNONDEF ;
258 }
259 
260 // ETATQCQ
262  if (etat == ETATQCQ) return ;
263 
264  // Protection
265  assert( (etat == ETATZERO) || (etat == ETATNONDEF) ) ; // sinon...
266 
267  t = new int[get_taille()] ;
268  etat = ETATQCQ ;
269 }
270 
271 // ZERO hard
273  if (t == 0x0) {
274  t = new int[get_taille()] ;
275  }
276  for (int i=0 ; i<get_taille() ; i++) {
277  t[i] = 0 ;
278  }
279  etat = ETATQCQ ;
280 }
281 
282 
283  //------------------------//
284  // Display //
285  //------------------------//
286 
287 //-----------
288 // Operator<<
289 //-----------
290 
291 ostream& operator<<(ostream& o, const Itbl& t) {
292 
293  int ndim = t.get_ndim() ;
294  o.precision(4);
295  o.setf(ios::showpoint);
296  o << "*** Itbl " << ndim << "D" << " size: " ;
297  for (int i = 0; i<ndim-1; i++) {
298  o << t.get_dim(i) << " x " ;
299  }
300  o << t.get_dim(ndim-1) << endl ;
301 
302  if (t.get_etat() == ETATZERO) {
303  o << "Identically ZERO" << endl ;
304  return o ;
305  }
306 
307  if (t.get_etat() == ETATNONDEF) {
308  o << "UNDEFINED STATE" << endl ;
309  return o ;
310  }
311 
312  assert(t.etat == ETATQCQ) ;
313  switch (ndim) {
314 
315  case 1 : {
316  for (int i=0 ; i<t.get_dim(0) ; i++) {
317  o << " " << t(i) ;
318  }
319  o << endl ;
320  break ;
321  }
322 
323 
324  case 2 : {
325  for (int j=0 ; j<t.get_dim(1) ; j++) {
326  o << " J = " << j << " : " << endl ;
327  for (int i=0 ; i<t.get_dim(0) ; i++) {
328  o << " " << t(j, i) ;
329  }
330  o << endl ;
331  }
332  o << endl ;
333  break ;
334  }
335 
336  case 3 : {
337  for (int k=0 ; k<t.get_dim(2) ; k++) {
338  o << " K = " << k << " : " << endl ;
339  for (int j=0 ; j<t.get_dim(1) ; j++) {
340  o << " J = " << j << " : " ;
341  for (int i=0 ; i<t.get_dim(0) ; i++) {
342  o << " " << t(k, j, i) ;
343  }
344  o << endl ;
345  }
346  o << endl ;
347  }
348  o << endl ;
349  break ;
350  }
351 
352  default : {
353  cout << "operator<< Itbl : unexpected dimension !" << endl ;
354  cout << " ndim = " << ndim << endl ;
355  abort() ;
356  break ;
357  }
358  }
359  return o ;
360 }
361 }
void del_t()
Logical destructor: dellocates the memory occupied by the array t and sets the logical state to ETATN...
Definition: itbl.C:240
Itbl(int size0)
1D constructor.
Definition: itbl.C:97
void sauve(FILE *) const
Save in a file.
Definition: dim_tbl.C:185
int get_taille() const
Gives the total size (ie dim.taille )
Definition: itbl.h:320
int get_ndim() const
Gives the number of dimensions (ie dim.ndim )
Definition: itbl.h:323
Lorene prototypes.
Definition: app_hor.h:64
void annule_hard()
Sets the Itbl to zero in a hard way.
Definition: itbl.C:272
~Itbl()
Destructor.
Definition: itbl.C:167
Basic integer array class.
Definition: itbl.h:122
Dim_tbl dim
Number of dimensions, size,...
Definition: itbl.h:131
friend ostream & operator<<(ostream &, const Itbl &)
Display.
Definition: itbl.C:291
void sauve(FILE *) const
Save in a file.
Definition: itbl.C:226
void set_etat_nondef()
Sets the logical state to ETATNONDEF (undefined).
Definition: itbl.C:254
void set_etat_qcq()
Sets the logical state to ETATQCQ (ordinary state).
Definition: itbl.C:261
int fwrite_be(const int *aa, int size, int nb, FILE *fich)
Writes integer(s) into a binary file according to the big endian convention.
Definition: fwrite_be.C:70
int get_etat() const
Gives the logical state.
Definition: itbl.h:317
int fread_be(int *aa, int size, int nb, FILE *fich)
Reads integer(s) from a binary file according to the big endian convention.
Definition: fread_be.C:69
Storage of array dimensions.
Definition: dim_tbl.h:99
void operator=(const Itbl &)
Assignment to another Itbl.
Definition: itbl.C:176
int get_dim(int i) const
Gives the i th dimension (ie {tt dim.dim[i] )
Definition: itbl.h:326
int * t
The array of int &#39;s.
Definition: itbl.h:132
void set_etat_zero()
Sets the logical state to ETATZERO (zero).
Definition: itbl.C:247
int etat
logical state (ETATNONDEF , ETATQCQ or ETATZERO ).
Definition: itbl.h:128