]> SALOME platform Git repositories - modules/med.git/blob - src/MEDMEM/MEDMEM_nArray.hxx
Salome HOME
467e59749cce071a32a7b7643fee50fda1f5238a
[modules/med.git] / src / MEDMEM / MEDMEM_nArray.hxx
1 #ifndef MEDMEM_ARRAY_HXX
2 #define MEDMEM_ARRAY_HXX
3
4 #include "MEDMEM_InterlacingPolicy.hxx"
5 #include "MEDMEM_IndexCheckingPolicy.hxx"
6
7 #include "MEDMEM_PointerOf.hxx"
8 #include "MEDMEM_define.hxx"
9
10 namespace MEDMEM {
11
12 class MEDMEM_Array_ {
13 public:
14   //virtual void dummy() {};
15   virtual bool getGaussPresence() const { return false; }
16   virtual MED_EN::medModeSwitch getInterlacingType() const {return MED_EN::MED_UNDEFINED_INTERLACE;}
17   virtual ~MEDMEM_Array_() {}; //Indispensable pour détruire le vrai objet pointé
18 };
19
20 template < class ARRAY_ELEMENT_TYPE,
21            class INTERLACING_POLICY=FullInterlaceNoGaussPolicy,
22            class CHECKING_POLICY=IndexCheckPolicy >
23 class MEDMEM_Array : public INTERLACING_POLICY, public CHECKING_POLICY, public MEDMEM_Array_ {
24
25 public :
26
27   typedef ARRAY_ELEMENT_TYPE  ElementType;
28   typedef INTERLACING_POLICY  InterlacingPolicy;
29   typedef CHECKING_POLICY     CheckingPolicy;
30
31 public  :
32   MEDMEM_Array():_array( ( ElementType *) NULL)  {}; //Interdit le constructeur par défaut, peut pas à  cause du FIELD
33
34   ~MEDMEM_Array() {
35     // PointerOf s'occupe de la desallocation.
36   };
37
38   // Le mot clé inline permettra d'instancier le constructeur uniquement
39   // s'il est appelé ( ...NoGaussPolicy)
40   // Rem : Le constructeur de la policy demandée est appelé
41   inline MEDMEM_Array(int dim, int nbelem) : InterlacingPolicy(nbelem,dim)
42   {
43     CHECKING_POLICY::checkMoreThanZero("MEDMEM_Array",nbelem);
44     CHECKING_POLICY::checkMoreThanZero("MEDMEM_Array",dim);
45     _array.set(InterlacingPolicy::_arraySize);
46   };
47
48   // Le mot clé inline permettra d'instancier le constructeur uniquement
49   // s'il est appelé ( ...NoGaussPolicy)
50   // Rem : Le constructeur de la policy demandée est appelé
51   inline MEDMEM_Array( ElementType * values, int dim, int nbelem,
52                        bool shallowCopy=false,
53                        bool ownershipOfValues=false) : InterlacingPolicy(nbelem,dim)
54   {
55     CHECKING_POLICY::checkMoreThanZero("MEDMEM_Array",nbelem);
56     CHECKING_POLICY::checkMoreThanZero("MEDMEM_Array",dim);
57     if(shallowCopy)
58
59       if(ownershipOfValues)
60         _array.setShallowAndOwnership((const ElementType *)values);
61       else
62         _array.set((const ElementType*)values);
63
64     else // Cas par défaut
65       _array.set(InterlacingPolicy::_arraySize,values);
66
67   }
68
69   // Le mot clé inline permettra d'instancier le constructeur uniquement
70   // s'il est appelé ( ...GaussPolicy)
71   // Rem : Le constructeur de la policy demandée est appelé
72   inline MEDMEM_Array(int dim, int nbelem, int nbtypegeo,
73                       const int * const  nbelgeoc, const int * const nbgaussgeo)
74     : InterlacingPolicy(nbelem, dim, nbtypegeo, nbelgeoc, nbgaussgeo)
75   {
76     CHECKING_POLICY::checkMoreThanZero("MEDMEM_Array",nbelem);
77     CHECKING_POLICY::checkMoreThanZero("MEDMEM_Array",dim);
78     CHECKING_POLICY::checkMoreThanZero("MEDMEM_Array",nbtypegeo);
79     _array.set(InterlacingPolicy::_arraySize);
80   };
81
82
83   // Le mot clé inline permettra d'instancier le constructeur uniquement
84   // s'il est appelé ( ...GaussPolicy)
85   // Rem : Le constructeur de la policy demandée est appelé
86   inline MEDMEM_Array(ElementType * values, int dim, int nbelem, int nbtypegeo,
87                       const int * const  nbelgeoc, const int * const  nbgaussgeo,
88                       bool shallowCopy=false,
89                       bool ownershipOfValues=false)
90     : InterlacingPolicy(nbelem, dim, nbtypegeo, nbelgeoc, nbgaussgeo)
91   {
92     CHECKING_POLICY::checkMoreThanZero("MEDMEM_Array",nbelem);
93     CHECKING_POLICY::checkMoreThanZero("MEDMEM_Array",dim);
94     CHECKING_POLICY::checkMoreThanZero("MEDMEM_Array",nbtypegeo);
95
96     if(shallowCopy)
97
98       if(ownershipOfValues)
99         _array.setShallowAndOwnership((const ElementType *)values);
100       else
101         _array.set((const ElementType*)values);
102
103     else
104       _array.set(InterlacingPolicy::_arraySize,values);
105
106   };
107
108   // Constructeur de recopie pour un MEDMEM_Array avec les mêmes
109   // paramètres template qu'à la construction
110   inline MEDMEM_Array(const MEDMEM_Array & array, bool shallowCopy=false)
111     :InterlacingPolicy(array,shallowCopy)
112   {
113     if (shallowCopy)
114       this->_array.set(array._array); // Le propriétaire reste le ARRAY initial
115     else
116       this->_array.set(InterlacingPolicy::_arraySize,array._array);
117   }
118
119
120   // L'utilisation d'une copie superficielle pour l'opérateur d'affectation
121   // ne me parait pas être une bonne ideé : Compatibilité ancienne version MEDARRAY?
122   inline MEDMEM_Array<ElementType,InterlacingPolicy,CheckingPolicy> &
123          operator=( const MEDMEM_Array & array) {
124     if ( this == &array) return *this;
125     BEGIN_OF("MEDMEM_Array  operator =");
126     InterlacingPolicy::operator=(array); //Appel des classes de base ?
127
128     this->_array.set(array._array); // Le propriétaire reste le ARRAY initial
129
130     return *this;
131   }
132
133   MED_EN::medModeSwitch getInterlacingType() const {
134     return InterlacingPolicy::getInterlacingType();
135   }
136
137   bool getGaussPresence() const {
138     return InterlacingPolicy::getGaussPresence();
139   }
140
141   ElementType * getPtr() {
142     return  _array;
143   }
144
145   void setPtr(ElementType * values, bool shallowCopy=false,
146               bool ownershipOfValues=false) {
147
148     if(shallowCopy)
149
150       if(ownershipOfValues)
151         _array.setShallowAndOwnership((const ElementType *)values);
152       else
153         _array.set((const ElementType*)values);
154
155     else
156       _array.set(InterlacingPolicy::_arraySize,values);
157   }
158
159   inline const ElementType * getRow(int i) const {
160     checkInInclusiveRange("MEDMEM_Array",1,InterlacingPolicy::_nbelem,i);
161     // Empêche l'utilisation de getRow en mode MED_NO_INTERLACE
162     // Ne devrait pas dépendre de la politique check
163     checkEquality("MEDMEM_Array (Interlace test)",
164                   MED_EN::MED_NO_INTERLACE,
165                   InterlacingPolicy::_interlacing );
166     return &(_array[ InterlacingPolicy::getIndex(i,1) ]);
167
168   }
169
170   void setRow(int i,const ElementType * const value) {
171     checkInInclusiveRange("MEDMEM_Array",1,InterlacingPolicy::_nbelem,i);
172     // setRow fonctionne
173     // dans les deux modes d'entrelacement.
174
175     for (int j =1; j <= InterlacingPolicy::getDim(); j++)
176       for (int k = 1 ; k <= InterlacingPolicy::getNbGauss(i); k++)
177         _array[InterlacingPolicy::getIndex(i,j,k)] = value[InterlacingPolicy::getIndex(1,j,k)];
178   }
179
180   inline const ElementType * getColumn(int j) const {
181     checkInInclusiveRange("MEDMEM_Array",1,InterlacingPolicy::_dim,j);
182     checkEquality("MEDMEM_Array (Interlace test)",
183                   MED_EN::MED_FULL_INTERLACE, InterlacingPolicy::_interlacing );
184     return &(_array[ InterlacingPolicy::getIndex(1,j) ]);
185   }
186
187   void setColumn(int j, const ElementType * const value) {
188     checkInInclusiveRange("MEDMEM_Array",1,InterlacingPolicy::_dim,j);
189     // setColumn fonctionne
190     // dans les deux modes d'entrelacement.
191
192     for (int i=1; i <= InterlacingPolicy::getNbElem(); i++)
193       for (int k = 1 ; k <= InterlacingPolicy::getNbGauss(i); k++)
194         _array[InterlacingPolicy::getIndex(i,j,k)] = value[InterlacingPolicy::getIndex(i,1,k)];
195   }
196
197
198   inline const ElementType & getIJ(int i, int j) const  {
199     checkInInclusiveRange("MEDMEM_Array",1,InterlacingPolicy::_nbelem,i);
200     checkInInclusiveRange("MEDMEM_Array",1,InterlacingPolicy::_dim,j);
201     return _array[ InterlacingPolicy::getIndex(i,j) ];
202   }
203
204   inline const ElementType & getIJK(int i, int j, int k ) const {
205     checkInInclusiveRange("MEDMEM_Array",1,InterlacingPolicy::_nbelem,i);
206     checkInInclusiveRange("MEDMEM_Array",1,InterlacingPolicy::_dim,j);
207     checkInInclusiveRange("MEDMEM_Array",1,InterlacingPolicy::getNbGauss(i),k);
208
209     return _array[ InterlacingPolicy::getIndex(i,j,k) ];
210   };
211
212   inline void setIJ(int i, int j, const ElementType & value) {   //autre signature avec
213     checkInInclusiveRange("MEDMEM_Array",1,InterlacingPolicy::_nbelem,i);
214     checkInInclusiveRange("MEDMEM_Array",1,InterlacingPolicy::_dim,j);
215
216     _array[ InterlacingPolicy::getIndex(i,j) ] = value;                      // retour ElementType & ?
217   };
218
219   inline void setIJK(int i, int j, int k, const ElementType & value) {   //autre signature avec
220     checkInInclusiveRange("MEDMEM_Array",1,InterlacingPolicy::_nbelem,i);
221     checkInInclusiveRange("MEDMEM_Array",1,InterlacingPolicy::_dim,j);
222     checkInInclusiveRange("MEDMEM_Array",1,InterlacingPolicy::getNbGauss(i),k);
223
224     _array[ InterlacingPolicy::getIndex(i,j,k) ] = value;                      // retour ElementType & ?
225   };
226
227   bool operator == (const MEDMEM_Array & array ) const {
228
229     if ( this == &array ) return true;
230
231     int size = array.getArraySize();
232     if ( size != getArraySize() ) return false;
233
234     ARRAY_ELEMENT_TYPE * arrayPtr =
235       const_cast<MEDMEM_Array &>(array).getPtr();
236     for (int i=0; i < size; ++i)
237       if (_array[i] != arrayPtr[i]) return false;
238
239     return true;
240   }
241
242   friend ostream & operator<<(ostream & os, const MEDMEM_Array & array) {
243
244     for (int i=1;i<=array.getNbElem();++i) {
245       for (int j=1; j<=array.getDim();++j)
246         for (int k=1;k<=array.getNbGauss(i);++k)
247           os << "Value [" << i << "," << j << "," << k << "] = " << array.getIJK(i,j,k) << ", ";
248       os << endl;
249     }
250     return os;
251   }
252
253 private:
254
255   PointerOf<ElementType> _array;
256 };
257
258 } //END NAMESPACE
259 #endif