Salome HOME
committing the version in the main trunk of the CVS tree the branch
[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(INTERLACING_POLICY::_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(INTERLACING_POLICY::_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(INTERLACING_POLICY::_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(INTERLACING_POLICY::_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(INTERLACING_POLICY::_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     // verifier l'appel des opérateurs de la policy
130
131     return *this;
132   }
133
134   MED_EN::medModeSwitch getInterlacingType() const {
135     return InterlacingPolicy::getInterlacingType();
136   }
137
138   bool getGaussPresence() const {
139     return InterlacingPolicy::getGaussPresence();
140   }
141
142   ElementType * getPtr() {
143     return  _array;
144   }
145
146   void setPtr(ElementType * values, bool shallowCopy=false,
147               bool ownershipOfValues=false) {
148
149     if(shallowCopy)
150
151       if(ownershipOfValues)
152         _array.setShallowAndOwnership((const ElementType *)values);
153       else
154         _array.set((const ElementType*)values);
155
156     else
157       _array.set(INTERLACING_POLICY::_arraySize,values);
158   }
159
160   inline const ElementType * getRow(int i) const {
161     checkInInclusiveRange("MEDMEM_Array",1,INTERLACING_POLICY::_nbelem,i);
162     // Empêche l'utilisation de getRow en mode MED_NO_INTERLACE
163     // Ne devrait pas dépendre de la politique check
164     checkEquality("MEDMEM_Array (Interlace test)",
165                   MED_EN::MED_NO_INTERLACE,
166                   INTERLACING_POLICY::_interlacing );
167     return &(_array[ INTERLACING_POLICY::getIndex(i,1) ]);
168
169   }
170
171   void setRow(int i,const ElementType * const value) {
172     checkInInclusiveRange("MEDMEM_Array",1,INTERLACING_POLICY::_nbelem,i);
173     // setRow fonctionne
174     // dans les deux modes d'entrelacement.
175
176     for (int j =1; j <= INTERLACING_POLICY::getDim(); j++)
177       for (int k = 1 ; k <= INTERLACING_POLICY::getNbGauss(i); k++)
178         _array[INTERLACING_POLICY::getIndex(i,j,k)] = value[INTERLACING_POLICY::getIndex(1,j,k)];
179   }
180
181   inline const ElementType * getColumn(int j) const {
182     checkInInclusiveRange("MEDMEM_Array",1,INTERLACING_POLICY::_dim,j);
183     checkEquality("MEDMEM_Array (Interlace test)",
184                   MED_EN::MED_FULL_INTERLACE, INTERLACING_POLICY::_interlacing );
185     return &(_array[ INTERLACING_POLICY::getIndex(1,j) ]);
186   }
187
188   void setColumn(int j, const ElementType * const value) {
189     checkInInclusiveRange("MEDMEM_Array",1,INTERLACING_POLICY::_dim,j);
190     // setColumn fonctionne
191     // dans les deux modes d'entrelacement.
192
193     for (int i=1; i <= INTERLACING_POLICY::getNbElem(); i++)
194       for (int k = 1 ; k <= INTERLACING_POLICY::getNbGauss(i); k++)
195         _array[INTERLACING_POLICY::getIndex(i,j,k)] = value[INTERLACING_POLICY::getIndex(i,1,k)];
196   }
197
198
199   inline const ElementType & getIJ(int i, int j) const  {
200     checkInInclusiveRange("MEDMEM_Array",1,INTERLACING_POLICY::_nbelem,i);
201     checkInInclusiveRange("MEDMEM_Array",1,INTERLACING_POLICY::_dim,j);
202     return _array[ INTERLACING_POLICY::getIndex(i,j) ];
203   }
204
205   inline const ElementType & getIJK(int i, int j, int k ) const {
206     checkInInclusiveRange("MEDMEM_Array",1,INTERLACING_POLICY::_nbelem,i);
207     checkInInclusiveRange("MEDMEM_Array",1,INTERLACING_POLICY::_dim,j);
208     checkInInclusiveRange("MEDMEM_Array",1,INTERLACING_POLICY::getNbGauss(i),k);
209
210     return _array[ INTERLACING_POLICY::getIndex(i,j,k) ];
211   };
212
213   inline void setIJ(int i, int j, const ElementType & value) {   //autre signature avec
214     checkInInclusiveRange("MEDMEM_Array",1,INTERLACING_POLICY::_nbelem,i);
215     checkInInclusiveRange("MEDMEM_Array",1,INTERLACING_POLICY::_dim,j);
216
217     _array[ INTERLACING_POLICY::getIndex(i,j) ] = value;                      // retour ElementType & ?
218   };
219
220   inline void setIJK(int i, int j, int k, const ElementType & value) {   //autre signature avec
221     checkInInclusiveRange("MEDMEM_Array",1,INTERLACING_POLICY::_nbelem,i);
222     checkInInclusiveRange("MEDMEM_Array",1,INTERLACING_POLICY::_dim,j);
223     checkInInclusiveRange("MEDMEM_Array",1,INTERLACING_POLICY::getNbGauss(i),k);
224
225     _array[ INTERLACING_POLICY::getIndex(i,j,k) ] = value;                      // retour ElementType & ?
226   };
227
228
229 private:
230
231   PointerOf<ElementType> _array;
232 };
233
234 } //END NAMESPACE
235 #endif