| 1 | /* |
| 2 | * Definition of Lorene class Itbl |
| 3 | * |
| 4 | */ |
| 5 | |
| 6 | /* |
| 7 | * Copyright (c) 1999-2001 Philippe Grandclement |
| 8 | * |
| 9 | * This file is part of LORENE. |
| 10 | * |
| 11 | * LORENE is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; either version 2 of the License, or |
| 14 | * (at your option) any later version. |
| 15 | * |
| 16 | * LORENE is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License |
| 22 | * along with LORENE; if not, write to the Free Software |
| 23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 24 | * |
| 25 | */ |
| 26 | |
| 27 | |
| 28 | #ifndef __ITBL_H_ |
| 29 | #define __ITBL_H_ |
| 30 | |
| 31 | /* |
| 32 | * Classe de tableaux d'entiers de dimension determinee (actuellement 1, 2 et 3) |
| 33 | * |
| 34 | */ |
| 35 | |
| 36 | /* |
| 37 | * $Id: itbl.h,v 1.9 2014/10/13 08:52:35 j_novak Exp $ |
| 38 | * $Log: itbl.h,v $ |
| 39 | * Revision 1.9 2014/10/13 08:52:35 j_novak |
| 40 | * Lorene classes and functions now belong to the namespace Lorene. |
| 41 | * |
| 42 | * Revision 1.8 2014/10/06 15:09:39 j_novak |
| 43 | * Modified #include directives to use c++ syntax. |
| 44 | * |
| 45 | * Revision 1.7 2004/03/24 14:53:39 j_novak |
| 46 | * Double declarations suppressed |
| 47 | * |
| 48 | * Revision 1.6 2004/03/22 13:12:41 j_novak |
| 49 | * Modification of comments to use doxygen instead of doc++ |
| 50 | * |
| 51 | * Revision 1.5 2003/11/06 14:43:37 e_gourgoulhon |
| 52 | * Gave a name to const arguments in certain method prototypes (e.g. |
| 53 | * constructors) to correct a bug of DOC++. |
| 54 | * |
| 55 | * Revision 1.4 2003/10/11 16:43:05 e_gourgoulhon |
| 56 | * |
| 57 | * IMPORTANT CHANGE: the standard constructors sets now the logical state |
| 58 | * to ETATQCQ, and no longer to ETATNONDEF. |
| 59 | * |
| 60 | * Revision 1.3 2002/10/16 14:36:29 j_novak |
| 61 | * Reorganization of #include instructions of standard C++, in order to |
| 62 | * use experimental version 3 of gcc. |
| 63 | * |
| 64 | * Revision 1.2 2002/06/17 14:05:16 j_novak |
| 65 | * friend functions are now also declared outside the class definition |
| 66 | * |
| 67 | * Revision 1.1.1.1 2001/11/20 15:19:27 e_gourgoulhon |
| 68 | * LORENE |
| 69 | * |
| 70 | * Revision 2.2 1999/11/23 13:16:12 eric |
| 71 | * Le constructeur Itbl::Itbl(const Dim_tbl ) devient desormais |
| 72 | * Itbl::Itbl(const Dim_tbl& ). |
| 73 | * Modif commentaires (taille 0 autorisee). |
| 74 | * |
| 75 | * Revision 2.1 1999/11/19 12:02:17 phil |
| 76 | * / |
| 77 | * |
| 78 | * Revision 2.0 1999/11/17 16:04:27 phil |
| 79 | * *** empty log message *** |
| 80 | * |
| 81 | * Revision 1.1 1999/11/17 16:03:06 phil |
| 82 | * Initial revision |
| 83 | * |
| 84 | * |
| 85 | * $Header: /cvsroot/Lorene/C++/Include/itbl.h,v 1.9 2014/10/13 08:52:35 j_novak Exp $ |
| 86 | * |
| 87 | */ |
| 88 | |
| 89 | // Fichiers includes |
| 90 | #include <cassert> |
| 91 | #include <cstdlib> |
| 92 | |
| 93 | #include "type_parite.h" |
| 94 | #include "dim_tbl.h" |
| 95 | |
| 96 | |
| 97 | namespace Lorene { |
| 98 | /** |
| 99 | * Basic integer array class. |
| 100 | * |
| 101 | * This class is essentially an \c int array class. The elements of the |
| 102 | * array are stored continuously using the C convention. |
| 103 | * |
| 104 | * The general logical state of an initialized |
| 105 | * \c Itbl is \c ETATQCQ ; it is the only state for which the memory |
| 106 | * allocation is performed for the \c int array \c t . |
| 107 | * The value zero is treated as a special logical state (\c ETATZERO ), |
| 108 | * without any memory allocation. |
| 109 | * |
| 110 | * Contrary to a \c Tbl , an \c Itbl is initialy created in a |
| 111 | * logical state \c ETATQCQ (i.e. ordinary state, with memory allocated |
| 112 | * for the array \c t ). |
| 113 | * |
| 114 | * Arithmetic operations are provided with the usual meaning (see |
| 115 | * below). |
| 116 | * |
| 117 | * A 1D Itbl can be of dimension 0 (size 0). Its logical state is then |
| 118 | * \c ETATZERO (by convention). |
| 119 | * |
| 120 | * \ingroup (util) |
| 121 | */ |
| 122 | class Itbl { |
| 123 | |
| 124 | // Data : |
| 125 | // ----- |
| 126 | private: |
| 127 | /// logical state (\c ETATNONDEF , \c ETATQCQ or \c ETATZERO ). |
| 128 | int etat ; |
| 129 | |
| 130 | public: |
| 131 | Dim_tbl dim ; ///< Number of dimensions, size,... |
| 132 | int* t ; ///< The array of \c int 's |
| 133 | |
| 134 | // Constructors - Destructor |
| 135 | // ------------------------- |
| 136 | |
| 137 | public: |
| 138 | /** |
| 139 | * 1D constructor. |
| 140 | * This constructor sets the \c Itbl in the logical state \c ETATQCQ , |
| 141 | * so that it is ready for initialization via the method \c set(int ) . |
| 142 | * |
| 143 | * @param size0 [input] Number of elements of the array \c t . |
| 144 | * Will be assigned to \c dim.dim[0] . |
| 145 | * The size 0 is allowed (the corresponding |
| 146 | * logical state will be then ETATZERO). |
| 147 | */ |
| 148 | explicit Itbl(int size0) ; |
| 149 | |
| 150 | /** |
| 151 | * 2D constructor. |
| 152 | * This constructor sets the \c Itbl in the logical state \c ETATQCQ , |
| 153 | * so that it is ready for initialization via the method |
| 154 | * \c set(int, int ) . |
| 155 | * |
| 156 | * @param size1 [input] Defines the range [0, size1-1] of the |
| 157 | * outermost index in the storage of the array \c t . |
| 158 | * Will be assigned to \c dim.dim[1] . |
| 159 | * @param size0 [input] Defines the range [0, size0-1] of the |
| 160 | * innermost index in the storage of the array \c t . |
| 161 | * Will be assigned to \c dim.dim[0] . |
| 162 | */ |
| 163 | Itbl(int size1, int size0) ; |
| 164 | |
| 165 | /** |
| 166 | * 3D constructor |
| 167 | * This constructor sets the \c Itbl in the logical state \c ETATQCQ , |
| 168 | * so that it is ready for initialization via the method |
| 169 | * \c set(int, int, int ) . |
| 170 | * |
| 171 | * @param size2 [input] Defines the range [0, size2-1] of the |
| 172 | * outermost index in the storage of the array \c t . |
| 173 | * Will be assigned to \c dim.dim[2] . |
| 174 | * @param size1 [input] Defines the range [0, size1-1] of the |
| 175 | * intermediate index in the storage of the array \c t . |
| 176 | * Will be assigned to \c dim.dim[1] . |
| 177 | * @param size0 [input] Defines the range [0, size0-1] of the |
| 178 | * innermost index in the storage of the array \c t . |
| 179 | * Will be assigned to \c dim.dim[0] . |
| 180 | */ |
| 181 | Itbl(int size2, int size1, int size0) ; |
| 182 | |
| 183 | |
| 184 | /** Constructor from a \c Dim_tbl . |
| 185 | * This constructor sets the \c Itbl in the logical state \c ETATQCQ , |
| 186 | * so that it is ready for initialization via the method |
| 187 | * \c set . |
| 188 | */ |
| 189 | explicit Itbl(const Dim_tbl& ) ; |
| 190 | |
| 191 | /// Constructor from a file (see \c sauve(FILE* ) ) |
| 192 | explicit Itbl(FILE* ) ; |
| 193 | |
| 194 | Itbl(const Itbl& ) ; ///< Copy constructor |
| 195 | |
| 196 | ~Itbl() ; ///< Destructor |
| 197 | |
| 198 | // Assignement |
| 199 | // ----------- |
| 200 | void operator=(const Itbl& ) ; ///< Assignment to another \c Itbl |
| 201 | void operator=(int ) ; ///< Assignment to a \c int |
| 202 | |
| 203 | // Memory management |
| 204 | // ----------------- |
| 205 | private: |
| 206 | /** Logical destructor: dellocates the memory occupied by the array |
| 207 | * \c t and sets the logical state to ETATNONDEF. |
| 208 | */ |
| 209 | void del_t() ; |
| 210 | |
| 211 | public: |
| 212 | |
| 213 | /** |
| 214 | * Sets the logical state to \c ETATNONDEF (undefined). |
| 215 | * Deallocates the memory occupied by the \c int array \c t . |
| 216 | */ |
| 217 | void set_etat_nondef() ; |
| 218 | |
| 219 | /** |
| 220 | * Sets the logical state to \c ETATZERO (zero). |
| 221 | * Deallocates the memory occupied by the \c int array \c t . |
| 222 | */ |
| 223 | void set_etat_zero() ; |
| 224 | |
| 225 | /** |
| 226 | * Sets the logical state to \c ETATQCQ (ordinary state). |
| 227 | * If the state (member \c etat ) is already \c ETATQCQ , this |
| 228 | * function does nothing. Otherwise, it performs the memory allocation |
| 229 | * for the \c int array \c t . |
| 230 | */ |
| 231 | void set_etat_qcq() ; |
| 232 | |
| 233 | /** |
| 234 | * Sets the \c Itbl to zero in a hard way. |
| 235 | * 1/ Sets the logical state to \c ETATQCQ , i.e. to an ordinary state. |
| 236 | * 2/ Allocates the memory of the \c int array \c t , and fills it |
| 237 | * with zeros. NB: this function must be used for debugging purposes only. |
| 238 | * For other operations, the function \c set_etat_zero() must |
| 239 | * be perferred. |
| 240 | */ |
| 241 | void annule_hard() ; |
| 242 | |
| 243 | // Access to individual elements |
| 244 | // ----------------------------- |
| 245 | public: |
| 246 | /// Read/write of a particular element (index \c i ) (1D case) |
| 247 | int& set(int i) { |
| 248 | assert (etat == ETATQCQ) ; |
| 249 | assert( dim.ndim == 1 ) ; |
| 250 | assert( i >= 0 ) ; |
| 251 | assert( i < dim.dim[0] ) ; |
| 252 | return t[i] ; |
| 253 | } ; |
| 254 | |
| 255 | /// Read-only of a particular element (index \c i ) (1D case) |
| 256 | int operator()(int i) const { |
| 257 | assert(etat != ETATNONDEF) ; |
| 258 | assert( dim.ndim == 1 ) ; |
| 259 | assert( i >= 0 ) ; |
| 260 | assert( i < dim.dim[0] ) ; |
| 261 | if (etat == ETATZERO) { |
| 262 | int zero = 0 ; |
| 263 | return zero ; |
| 264 | } |
| 265 | else return t[i] ; |
| 266 | }; |
| 267 | |
| 268 | /// Read/write of a particular element (index \c (j,i) ) (2D case) |
| 269 | int& set(int j, int i) { |
| 270 | assert (etat == ETATQCQ) ; |
| 271 | assert( dim.ndim == 2 ) ; |
| 272 | assert( (i>=0) && (i<dim.dim[0]) ) ; |
| 273 | assert( (j>=0) && (j<dim.dim[1]) ) ; |
| 274 | return t[dim.dim[0] * j + i] ; |
| 275 | }; |
| 276 | |
| 277 | /// Read-only of a particular element (index \c (j,i) ) (2D case) |
| 278 | int operator()(int j, int i) const { |
| 279 | assert(etat != ETATNONDEF) ; |
| 280 | assert( dim.ndim == 2 ) ; |
| 281 | assert( (i>=0) && (i<dim.dim[0]) ) ; |
| 282 | assert( (j>=0) && (j<dim.dim[1]) ) ; |
| 283 | if (etat == ETATZERO) { |
| 284 | int zero = 0 ; |
| 285 | return zero ; |
| 286 | } |
| 287 | else return t[dim.dim[0] * j + i] ; |
| 288 | }; |
| 289 | |
| 290 | /// Read/write of a particular element (index \c (k,j,i) ) (3D case) |
| 291 | int& set(int k, int j, int i) { |
| 292 | assert (etat == ETATQCQ) ; |
| 293 | assert( dim.ndim == 3 ) ; |
| 294 | assert( (i>=0) && (i<dim.dim[0]) ) ; |
| 295 | assert( (j>=0) && (j<dim.dim[1]) ) ; |
| 296 | assert( (k>=0) && (k<dim.dim[2]) ) ; |
| 297 | return t[dim.dim[1]*dim.dim[0]*k + dim.dim[0]*j + i] ; |
| 298 | }; |
| 299 | |
| 300 | /// Read-only of a particular element (index \c (k,j,i) ) (3D case) |
| 301 | int operator()(int k, int j, int i) const { |
| 302 | assert(etat != ETATNONDEF) ; |
| 303 | assert( dim.ndim == 3 ) ; |
| 304 | assert( (i>=0) && (i<dim.dim[0]) ) ; |
| 305 | assert( (j>=0) && (j<dim.dim[1]) ) ; |
| 306 | assert( (k>=0) && (k<dim.dim[2]) ) ; |
| 307 | if (etat == ETATZERO) { |
| 308 | int zero = 0 ; |
| 309 | return zero ; |
| 310 | } |
| 311 | else return t[dim.dim[1]*dim.dim[0]*k + dim.dim[0]*j + i] ; |
| 312 | }; |
| 313 | |
| 314 | // Extraction of information |
| 315 | // ------------------------- |
| 316 | /// Gives the logical state |
| 317 | int get_etat() const { return etat ; } ; |
| 318 | |
| 319 | /// Gives the total size (ie \c dim.taille ) |
| 320 | int get_taille() const { return dim.taille ; }; |
| 321 | |
| 322 | /// Gives the number of dimensions (ie \c dim.ndim ) |
| 323 | int get_ndim() const { return dim.ndim ; }; |
| 324 | |
| 325 | /// Gives the \c i th dimension (ie {tt dim.dim[i] ) |
| 326 | int get_dim(int i) const { |
| 327 | assert( (i>=0) && (i<dim.ndim) ) ; |
| 328 | return dim.dim[i] ; |
| 329 | }; |
| 330 | |
| 331 | // Outputs |
| 332 | // ------- |
| 333 | public: |
| 334 | void sauve(FILE* ) const ; ///< Save in a file |
| 335 | |
| 336 | /// Display |
| 337 | friend ostream& operator<<(ostream& , const Itbl& ) ; |
| 338 | |
| 339 | // Member arithmetics |
| 340 | // ------------------ |
| 341 | public: |
| 342 | |
| 343 | void operator+=(const Itbl &) ; ///< Addition of a \c Itbl to \c this |
| 344 | void operator+=(int) ; ///< Addition of a \c int to \c this |
| 345 | void operator-=(const Itbl &) ; ///< Subtraction of a \c Itbl to \c this |
| 346 | void operator-=(int) ; ///< Subtraction of a \c int to \c this |
| 347 | void operator*=(const Itbl &) ; ///< Multiplication of \c this by a \c Itbl |
| 348 | void operator*=(int) ; ///< Multiplication of \c this by a \c int |
| 349 | } ; |
| 350 | ostream& operator<<(ostream& , const Itbl& ) ; |
| 351 | |
| 352 | |
| 353 | /** |
| 354 | * \defgroup itb_mat Itbl Mathematics |
| 355 | * \ingroup (util) |
| 356 | * |
| 357 | * @{ |
| 358 | */ |
| 359 | Itbl operator+(const Itbl&) ; ///< + Ibl |
| 360 | Itbl operator-(const Itbl&) ; ///< - Itbl |
| 361 | Itbl operator+(const Itbl&, const Itbl&) ; ///< Itbl + Itbl |
| 362 | Itbl operator+(const Itbl&, int) ; ///< Itbl + int |
| 363 | Itbl operator+(int a, const Itbl& b) ; ///< int + Itbl |
| 364 | Itbl operator-(const Itbl&, const Itbl&) ; ///< Itbl - Itbl |
| 365 | Itbl operator-(const Itbl&, int) ; ///< Itbl - int |
| 366 | Itbl operator-(int, const Itbl&) ; ///< int - Itbl |
| 367 | Itbl operator*(const Itbl&, const Itbl&) ; ///< Itbl * Itbl |
| 368 | Itbl operator*(const Itbl&, int) ; ///< Itbl * int |
| 369 | Itbl operator*(int, const Itbl&) ; ///< int * Itbl |
| 370 | |
| 371 | Itbl abs(const Itbl& ) ; ///< Absolute value |
| 372 | int max(const Itbl& ) ; ///< Maximum value of the \c Itbl elements |
| 373 | int min(const Itbl& ) ; ///< Minimum value of the \c Itbl elements |
| 374 | |
| 375 | /// Sum of the absolute values of all the \c Itbl elements |
| 376 | int norme(const Itbl& ) ; |
| 377 | |
| 378 | /** |
| 379 | * Relative difference between two \c Itbl (norme version). |
| 380 | * Returns \c norme(a-b)/norme(b) unless \c b=0 , in which |
| 381 | * case it returns \c norme(a-b) . |
| 382 | */ |
| 383 | double diffrel(const Itbl& a, const Itbl& b) ; |
| 384 | |
| 385 | /** |
| 386 | * Relative difference between two \c Itbl (max version). |
| 387 | * Returns \c max(abs(a-b))/max(abs(b)) unless \c b=0 , in which |
| 388 | * case it returns \c max(abs(a-b)) . |
| 389 | */ |
| 390 | double diffrelmax(const Itbl& a, const Itbl& b) ; |
| 391 | |
| 392 | /** @}*/ |
| 393 | } |
| 394 | #endif |