Salome HOME
Merge from V6_main 19/03/2013
[modules/med.git] / src / MEDCoupling / MEDCouplingMemArray.txx
1 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 // Author : Anthony Geay (CEA/DEN)
20
21 #ifndef __PARAMEDMEM_MEDCOUPLINGMEMARRAY_TXX__
22 #define __PARAMEDMEM_MEDCOUPLINGMEMARRAY_TXX__
23
24 #include "MEDCouplingMemArray.hxx"
25 #include "NormalizedUnstructuredMesh.hxx"
26 #include "InterpKernelException.hxx"
27 #include "InterpolationUtils.hxx"
28
29 #include <sstream>
30 #include <algorithm>
31
32 namespace ParaMEDMEM
33 {
34   template<class T>
35   void MEDCouplingPointer<T>::setInternal(T *pointer)
36   {
37     _internal=pointer;
38     _external=0;
39   }
40
41   template<class T>
42   void MEDCouplingPointer<T>::setExternal(const T *pointer)
43   {
44     _external=pointer;
45     _internal=0;
46   }
47
48   template<class T>
49   MemArray<T>::MemArray(const MemArray<T>& other):_nb_of_elem(0),_nb_of_elem_alloc(0),_ownership(false),_dealloc(CPP_DEALLOC)
50   {
51     if(!other._pointer.isNull())
52       {
53         _nb_of_elem_alloc=other._nb_of_elem;
54         T *pointer=new T[_nb_of_elem_alloc];
55         std::copy(other._pointer.getConstPointer(),other._pointer.getConstPointer()+other._nb_of_elem,pointer);
56         useArray(pointer,true,CPP_DEALLOC,other._nb_of_elem);
57       }
58   }
59
60   template<class T>
61   void MemArray<T>::useArray(const T *array, bool ownership, DeallocType type, int nbOfElem)
62   {
63     _nb_of_elem=nbOfElem;
64     _nb_of_elem_alloc=nbOfElem;
65     destroy();
66     if(ownership)
67       _pointer.setInternal(const_cast<T *>(array));
68     else
69       _pointer.setExternal(array);
70     _ownership=ownership;
71     _dealloc=type;
72   }
73
74   template<class T>
75   void MemArray<T>::useExternalArrayWithRWAccess(const T *array, int nbOfElem)
76   {
77     _nb_of_elem=nbOfElem;
78     _nb_of_elem_alloc=nbOfElem;
79     destroy();
80     _pointer.setInternal(const_cast<T *>(array));
81     _ownership=false;
82     _dealloc=CPP_DEALLOC;
83   }
84   
85   template<class T>
86   void MemArray<T>::writeOnPlace(int id, T element0, const T *others, int sizeOfOthers)
87   {
88     if(id+sizeOfOthers>=_nb_of_elem_alloc)
89       reserve(2*_nb_of_elem+sizeOfOthers+1);
90     T *pointer=_pointer.getPointer();
91     pointer[id]=element0;
92     std::copy(others,others+sizeOfOthers,pointer+id+1);
93     _nb_of_elem=std::max<int>(_nb_of_elem,id+sizeOfOthers+1);
94   }
95   
96   template<class T>
97   template<class InputIterator>
98   void MemArray<T>::insertAtTheEnd(InputIterator first, InputIterator last)
99   {
100     T *pointer=_pointer.getPointer();
101     while(first!=last)
102       {
103         if(_nb_of_elem>=_nb_of_elem_alloc || _nb_of_elem==0)
104           {
105             reserve(_nb_of_elem_alloc>0?2*_nb_of_elem_alloc:1);
106             pointer=_pointer.getPointer();
107           }
108         pointer[_nb_of_elem++]=*first++;
109       }
110   }
111   
112   template<class T>
113   void MemArray<T>::pushBack(T elem) throw(INTERP_KERNEL::Exception)
114   {
115     if(_nb_of_elem>=_nb_of_elem_alloc)
116       reserve(_nb_of_elem_alloc>0?2*_nb_of_elem_alloc:1);
117     T *pt=getPointer();
118     pt[_nb_of_elem++]=elem;
119   }
120   
121   template<class T>
122   T MemArray<T>::popBack() throw(INTERP_KERNEL::Exception)
123   {
124     if(_nb_of_elem>0)
125       {
126         const T *pt=getConstPointer();
127         return pt[--_nb_of_elem];
128       }
129     throw INTERP_KERNEL::Exception("MemArray::popBack : nothing to pop in array !");
130   }
131   
132   template<class T>
133   void MemArray<T>::pack() const
134   {
135     if(_nb_of_elem>=0)
136       (const_cast<MemArray<T> * >(this))->reserve(_nb_of_elem);
137   }
138
139   template<class T>
140   bool MemArray<T>::isEqual(const MemArray<T>& other, T prec, std::string& reason) const
141   {
142     std::ostringstream oss; oss.precision(15);
143     if(_nb_of_elem!=other._nb_of_elem)
144       {
145         oss << "Number of elements in coarse data of DataArray mismatch : this=" << _nb_of_elem << " other=" << other._nb_of_elem;
146         reason=oss.str();
147         return false;
148       }
149     const T *pt1=_pointer.getConstPointer();
150     const T *pt2=other._pointer.getConstPointer();
151     if(pt1==0 && pt2==0)
152       return true;
153     if(pt1==0 || pt2==0)
154       {
155         oss << "coarse data pointer is defined for only one DataArray instance !";
156         reason=oss.str();
157         return false;
158       }
159     if(pt1==pt2)
160       return true;
161     for(int i=0;i<_nb_of_elem;i++)
162       if(pt1[i]-pt2[i]<-prec || (pt1[i]-pt2[i])>prec)
163         {
164           oss << "The content of data differs at pos #" << i << " of coarse data ! this[i]=" << pt1[i] << " other[i]=" << pt2[i];
165           reason=oss.str();
166           return false;
167         }
168     return true;
169   }
170
171   /*!
172    * \param [in] sl is typically the number of components
173    * \return True if a not null pointer is present, False if not.
174    */
175   template<class T>
176   bool MemArray<T>::reprHeader(int sl, std::ostream& stream) const
177   {
178     stream << "Number of tuples : ";
179     if(!_pointer.isNull())
180       {
181         if(sl!=0)
182           stream << _nb_of_elem/sl << std::endl << "Internal memory facts : " << _nb_of_elem << "/" << _nb_of_elem_alloc;
183        else
184           stream << "Empty Data";
185       }
186     else
187       stream << "No data";
188     stream << "\n";
189     stream << "Data content :\n";
190     bool ret=!_pointer.isNull();
191     if(!ret)
192       stream << "No data !\n";
193     return ret;
194   }
195   
196   /*!
197    * \param [in] sl is typically the number of components
198    */
199   template<class T>
200   void MemArray<T>::repr(int sl, std::ostream& stream) const
201   {
202     if(reprHeader(sl,stream))
203       {
204         const T *data=getConstPointer();
205         if(_nb_of_elem!=0 && sl!=0)
206           {
207             int nbOfTuples=_nb_of_elem/sl;
208             for(int i=0;i<nbOfTuples;i++)
209               {
210                 stream << "Tuple #" << i << " : ";
211                 std::copy(data,data+sl,std::ostream_iterator<T>(stream," "));
212                 stream << "\n";
213                 data+=sl;
214               }
215           }
216         else
217           stream << "Empty Data\n";
218       }
219   }
220   
221   /*!
222    * \param [in] sl is typically the number of components
223    */
224   template<class T>
225   void MemArray<T>::reprZip(int sl, std::ostream& stream) const
226   {
227     stream << "Number of tuples : ";
228     if(!_pointer.isNull())
229       {
230         if(sl!=0)
231           stream << _nb_of_elem/sl;
232         else
233           stream << "Empty Data";
234       }
235     else
236       stream << "No data";
237     stream << "\n";
238     stream << "Data content : ";
239     const T *data=getConstPointer();
240     if(!_pointer.isNull())
241       {
242         if(_nb_of_elem!=0 && sl!=0)
243           {
244             int nbOfTuples=_nb_of_elem/sl;
245             for(int i=0;i<nbOfTuples;i++)
246               {
247                 stream << "|";
248                 std::copy(data,data+sl,std::ostream_iterator<T>(stream," "));
249                 stream << "| ";
250                 data+=sl;
251               }
252             stream << "\n";
253           }
254         else
255           stream << "Empty Data\n";
256       }
257     else
258       stream << "No data !\n";
259   }
260   
261   template<class T>
262   void MemArray<T>::fillWithValue(const T& val)
263   {
264     T *pt=_pointer.getPointer();
265     std::fill(pt,pt+_nb_of_elem,val);
266   }
267   
268   template<class T>
269   T *MemArray<T>::fromNoInterlace(int nbOfComp) const
270   {
271     const T *pt=_pointer.getConstPointer();
272     int nbOfTuples=_nb_of_elem/nbOfComp;
273     T *ret=new T[_nb_of_elem];
274     T *w=ret;
275     for(int i=0;i<nbOfTuples;i++)
276       for(int j=0;j<nbOfComp;j++,w++)
277         *w=pt[j*nbOfTuples+i];
278     return ret;
279   }
280   
281   template<class T>
282   T *MemArray<T>::toNoInterlace(int nbOfComp) const
283   {
284     const T *pt=_pointer.getConstPointer();
285     int nbOfTuples=_nb_of_elem/nbOfComp;
286     T *ret=new T[_nb_of_elem];
287     T *w=ret;
288     for(int i=0;i<nbOfComp;i++)
289       for(int j=0;j<nbOfTuples;j++,w++)
290         *w=pt[j*nbOfComp+i];
291     return ret;
292   }
293
294   template<class T>
295   void MemArray<T>::sort(bool asc)
296   {
297     T *pt=_pointer.getPointer();
298     if(asc)
299       std::sort(pt,pt+_nb_of_elem);
300     else
301       {
302         typename std::reverse_iterator<T *> it1(pt+_nb_of_elem);
303         typename std::reverse_iterator<T *> it2(pt);
304         std::sort(it1,it2);
305       }
306   }
307
308   template<class T>
309   void MemArray<T>::reverse()
310   {
311     T *pt=_pointer.getPointer();
312     std::reverse(pt,pt+_nb_of_elem);
313   }
314
315   template<class T>
316   void MemArray<T>::alloc(int nbOfElements) throw(INTERP_KERNEL::Exception)
317   {
318     destroy();
319     if(nbOfElements<0)
320       throw INTERP_KERNEL::Exception("MemArray::alloc : request for negative length of data !");
321     _nb_of_elem=nbOfElements;
322     _nb_of_elem_alloc=nbOfElements;
323     _pointer.setInternal(new T[_nb_of_elem_alloc]);
324     _ownership=true;
325     _dealloc=CPP_DEALLOC;
326   }
327
328   /*!
329    * This method performs systematically an allocation of \a newNbOfElements elements in \a this.
330    * \a _nb_of_elem and \a _nb_of_elem_alloc will \b NOT be systematically equal (contrary to MemArray<T>::reAlloc method.
331    * So after the call of this method \a _nb_of_elem will be equal tostd::min<int>(_nb_of_elem,newNbOfElements) and \a _nb_of_elem_alloc equal to 
332    * \a newNbOfElements. This method is typically used to perform a pushBack to avoid systematic allocations-copy-deallocation.
333    * So after the call of this method the accessible content is perfectly set.
334    * 
335    * So this method should not be confused with MemArray<T>::reserve that is close to MemArray<T>::reAlloc but not same.
336    */
337   template<class T>
338   void MemArray<T>::reserve(int newNbOfElements) throw(INTERP_KERNEL::Exception)
339   {
340     if(newNbOfElements<0)
341       throw INTERP_KERNEL::Exception("MemArray::reAlloc : request for negative length of data !");
342     if(_nb_of_elem_alloc==newNbOfElements)
343       return ;
344     T *pointer=new T[newNbOfElements];
345     std::copy(_pointer.getConstPointer(),_pointer.getConstPointer()+std::min<int>(_nb_of_elem,newNbOfElements),pointer);
346     if(_ownership)
347       destroyPointer(const_cast<T *>(_pointer.getConstPointer()),_dealloc);//Do not use getPointer because in case of _external
348     _pointer.setInternal(pointer);
349     _nb_of_elem=std::min<int>(_nb_of_elem,newNbOfElements);
350     _nb_of_elem_alloc=newNbOfElements;
351     _ownership=true;
352     _dealloc=CPP_DEALLOC;
353   }
354
355   /*!
356    * This method performs systematically an allocation of \a newNbOfElements elements in \a this.
357    * \a _nb_of_elem and \a _nb_of_elem_alloc will be equal even if only std::min<int>(_nb_of_elem,newNbOfElements) come from the .
358    * The remaing part of the new allocated chunk are available but not set previouly !
359    * 
360    * So this method should not be confused with MemArray<T>::reserve that is close to MemArray<T>::reAlloc but not same.
361    */
362   template<class T>
363   void MemArray<T>::reAlloc(int newNbOfElements) throw(INTERP_KERNEL::Exception)
364   {
365     if(newNbOfElements<0)
366       throw INTERP_KERNEL::Exception("MemArray::reAlloc : request for negative length of data !");
367     if(_nb_of_elem==newNbOfElements)
368       return ;
369     T *pointer=new T[newNbOfElements];
370     std::copy(_pointer.getConstPointer(),_pointer.getConstPointer()+std::min<int>(_nb_of_elem,newNbOfElements),pointer);
371     if(_ownership)
372       destroyPointer(const_cast<T *>(_pointer.getConstPointer()),_dealloc);//Do not use getPointer because in case of _external
373     _pointer.setInternal(pointer);
374     _nb_of_elem=newNbOfElements;
375     _nb_of_elem_alloc=newNbOfElements;
376     _ownership=true;
377     _dealloc=CPP_DEALLOC;
378   }
379
380   template<class T>
381   void MemArray<T>::destroyPointer(T *pt, DeallocType type)
382   {
383     switch(type)
384       {
385       case CPP_DEALLOC:
386         {
387           delete [] pt;
388           return ;
389         }
390       case C_DEALLOC:
391         {
392           free(pt);
393           return ;
394         }
395       default:
396         std::ostringstream stream;
397         stream << "Invalid deallocation requested for pointer " << pt;
398         throw INTERP_KERNEL::Exception(stream.str().c_str());
399       }
400   }
401
402   template<class T>
403   void MemArray<T>::destroy()
404   {
405     if(_ownership)
406       destroyPointer(const_cast<T *>(_pointer.getConstPointer()),_dealloc);//Do not use getPointer because in case of _external
407     _pointer.null();
408     _ownership=false;
409   }
410   
411   template<class T>
412   MemArray<T> &MemArray<T>::operator=(const MemArray<T>& other)
413   {
414     alloc(other._nb_of_elem);
415     std::copy(other._pointer.getConstPointer(),other._pointer.getConstPointer()+_nb_of_elem,_pointer.getPointer());
416     return *this;
417   }
418 }
419
420 #endif