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