Salome HOME
fa5fac7a0c7578b0368277b63816ad7a5c0ea7fb
[tools/medcoupling.git] / src / MEDCoupling / MEDCouplingMemArray.txx
1 // Copyright (C) 2007-2015  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, or (at your option) any later version.
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 <cstdlib>
31 #include <algorithm>
32
33 namespace MEDCoupling
34 {
35   template<class T>
36   void MEDCouplingPointer<T>::setInternal(T *pointer)
37   {
38     _internal=pointer;
39     _external=0;
40   }
41
42   template<class T>
43   void MEDCouplingPointer<T>::setExternal(const T *pointer)
44   {
45     _external=pointer;
46     _internal=0;
47   }
48
49   template<class T>
50   MemArray<T>::MemArray(const MemArray<T>& other):_nb_of_elem(0),_nb_of_elem_alloc(0),_ownership(false),_dealloc(0),_param_for_deallocator(0)
51   {
52     if(!other._pointer.isNull())
53       {
54         _nb_of_elem_alloc=other._nb_of_elem;
55         T *pointer=(T*)malloc(_nb_of_elem_alloc*sizeof(T));
56         std::copy(other._pointer.getConstPointer(),other._pointer.getConstPointer()+other._nb_of_elem,pointer);
57         useArray(pointer,true,C_DEALLOC,other._nb_of_elem);
58       }
59   }
60
61   template<class T>
62   void MemArray<T>::useArray(const T *array, bool ownership, DeallocType type, std::size_t nbOfElem)
63   {
64     destroy();
65     _nb_of_elem=nbOfElem;
66     _nb_of_elem_alloc=nbOfElem;
67     if(ownership)
68       _pointer.setInternal(const_cast<T *>(array));
69     else
70       _pointer.setExternal(array);
71     _ownership=ownership;
72     _dealloc=BuildFromType(type);
73   }
74
75   template<class T>
76   void MemArray<T>::useExternalArrayWithRWAccess(const T *array, std::size_t nbOfElem)
77   {
78     destroy();
79     _nb_of_elem=nbOfElem;
80     _nb_of_elem_alloc=nbOfElem;
81     _pointer.setInternal(const_cast<T *>(array));
82     _ownership=false;
83     _dealloc=CPPDeallocator;
84   }
85
86   template<class T>
87   void MemArray<T>::writeOnPlace(std::size_t id, T element0, const T *others, std::size_t sizeOfOthers)
88   {
89     if(id+sizeOfOthers>=_nb_of_elem_alloc)
90       reserve(2*_nb_of_elem+sizeOfOthers+1);
91     T *pointer=_pointer.getPointer();
92     pointer[id]=element0;
93     std::copy(others,others+sizeOfOthers,pointer+id+1);
94     _nb_of_elem=std::max<std::size_t>(_nb_of_elem,id+sizeOfOthers+1);
95   }
96
97   template<class T>
98   template<class InputIterator>
99   void MemArray<T>::insertAtTheEnd(InputIterator first, InputIterator last)
100   {
101     T *pointer=_pointer.getPointer();
102     while(first!=last)
103       {
104         if(_nb_of_elem>=_nb_of_elem_alloc)
105           {
106             reserve(_nb_of_elem_alloc>0?2*_nb_of_elem_alloc:1);
107             pointer=_pointer.getPointer();
108           }
109         pointer[_nb_of_elem++]=*first++;
110       }
111   }
112
113   template<class T>
114   void MemArray<T>::pushBack(T elem)
115   {
116     if(_nb_of_elem>=_nb_of_elem_alloc)
117       reserve(_nb_of_elem_alloc>0?2*_nb_of_elem_alloc:1);
118     T *pt=getPointer();
119     pt[_nb_of_elem++]=elem;
120   }
121
122   template<class T>
123   T MemArray<T>::popBack()
124   {
125     if(_nb_of_elem>0)
126       {
127         const T *pt=getConstPointer();
128         return pt[--_nb_of_elem];
129       }
130     throw INTERP_KERNEL::Exception("MemArray::popBack : nothing to pop in array !");
131   }
132
133   template<class T>
134   void MemArray<T>::pack() const
135   {
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(std::size_t 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             std::size_t nbOfTuples=_nb_of_elem/std::abs(sl);
208             for(std::size_t 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             std::size_t nbOfTuples=_nb_of_elem/std::abs(sl);
245             for(std::size_t 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   /*!
262    * \param [in] sl is typically the number of components
263    */
264   template<class T>
265   void MemArray<T>::reprNotTooLong(int sl, std::ostream& stream) const
266   {
267     if(reprHeader(sl,stream))
268       {
269         const T *data=getConstPointer();
270         if(_nb_of_elem!=0 && sl!=0)
271           {
272             std::size_t nbOfTuples=_nb_of_elem/std::abs(sl);
273             if(nbOfTuples<=1000)
274               {
275                 for(std::size_t i=0;i<nbOfTuples;i++)
276                   {
277                     stream << "Tuple #" << i << " : ";
278                     std::copy(data,data+sl,std::ostream_iterator<T>(stream," "));
279                     stream << "\n";
280                     data+=sl;
281                   }
282               }
283             else
284               {// too much tuples -> print the 3 first tuples and 3 last.
285                 stream << "Tuple #0 : ";
286                 std::copy(data,data+sl,std::ostream_iterator<T>(stream," ")); stream << "\n";
287                 stream << "Tuple #1 : ";
288                 std::copy(data+sl,data+2*sl,std::ostream_iterator<T>(stream," ")); stream << "\n";
289                 stream << "Tuple #2 : ";
290                 std::copy(data+2*sl,data+3*sl,std::ostream_iterator<T>(stream," ")); stream << "\n";
291                 stream << "...\n";
292                 stream << "Tuple #" << nbOfTuples-3 << " : ";
293                 std::copy(data+(nbOfTuples-3)*sl,data+(nbOfTuples-2)*sl,std::ostream_iterator<T>(stream," ")); stream << "\n";
294                 stream << "Tuple #" << nbOfTuples-2 << " : ";
295                 std::copy(data+(nbOfTuples-2)*sl,data+(nbOfTuples-1)*sl,std::ostream_iterator<T>(stream," ")); stream << "\n";
296                 stream << "Tuple #" << nbOfTuples-1 << " : ";
297                 std::copy(data+(nbOfTuples-1)*sl,data+nbOfTuples*sl,std::ostream_iterator<T>(stream," ")); stream << "\n";
298               }
299           }
300         else
301           stream << "Empty Data\n";
302       }
303   }
304
305   template<class T>
306   void MemArray<T>::fillWithValue(const T& val)
307   {
308     T *pt=_pointer.getPointer();
309     std::fill(pt,pt+_nb_of_elem,val);
310   }
311
312   template<class T>
313   T *MemArray<T>::fromNoInterlace(int nbOfComp) const
314   {
315     if(nbOfComp<1)
316       throw INTERP_KERNEL::Exception("MemArray<T>::fromNoInterlace : number of components must be > 0 !");
317     const T *pt=_pointer.getConstPointer();
318     std::size_t nbOfTuples=_nb_of_elem/nbOfComp;
319     T *ret=(T*)malloc(_nb_of_elem*sizeof(T));
320     T *w=ret;
321     for(std::size_t i=0;i<nbOfTuples;i++)
322       for(int j=0;j<nbOfComp;j++,w++)
323         *w=pt[j*nbOfTuples+i];
324     return ret;
325   }
326
327   template<class T>
328   T *MemArray<T>::toNoInterlace(int nbOfComp) const
329   {
330     if(nbOfComp<1)
331       throw INTERP_KERNEL::Exception("MemArray<T>::toNoInterlace : number of components must be > 0 !");
332     const T *pt=_pointer.getConstPointer();
333     std::size_t nbOfTuples=_nb_of_elem/nbOfComp;
334     T *ret=(T*)malloc(_nb_of_elem*sizeof(T));
335     T *w=ret;
336     for(int i=0;i<nbOfComp;i++)
337       for(std::size_t j=0;j<nbOfTuples;j++,w++)
338         *w=pt[j*nbOfComp+i];
339     return ret;
340   }
341
342   template<class T>
343   void MemArray<T>::sort(bool asc)
344   {
345     T *pt=_pointer.getPointer();
346     if(asc)
347       std::sort(pt,pt+_nb_of_elem);
348     else
349       {
350         typename std::reverse_iterator<T *> it1(pt+_nb_of_elem);
351         typename std::reverse_iterator<T *> it2(pt);
352         std::sort(it1,it2);
353       }
354   }
355
356   template<class T>
357   void MemArray<T>::reverse(int nbOfComp)
358   {
359     if(nbOfComp<1)
360       throw INTERP_KERNEL::Exception("MemArray<T>::reverse : only supported with 'this' array with ONE or more than ONE component !");
361     T *pt=_pointer.getPointer();
362     if(nbOfComp==1)
363       {
364         std::reverse(pt,pt+_nb_of_elem);
365         return ;
366       }
367     else
368       {
369         T *pt2=pt+_nb_of_elem-nbOfComp;
370         std::size_t nbOfTuples=_nb_of_elem/nbOfComp;
371         for(std::size_t i=0;i<nbOfTuples/2;i++,pt+=nbOfComp,pt2-=nbOfComp)
372           {
373             for(int j=0;j<nbOfComp;j++)
374               std::swap(pt[j],pt2[j]);
375           }
376       }
377   }
378
379   template<class T>
380   void MemArray<T>::alloc(std::size_t nbOfElements)
381   {
382     destroy();
383     _nb_of_elem=nbOfElements;
384     _nb_of_elem_alloc=nbOfElements;
385     _pointer.setInternal((T*)malloc(_nb_of_elem_alloc*sizeof(T)));
386     _ownership=true;
387     _dealloc=CDeallocator;
388   }
389
390   /*!
391    * This method performs systematically an allocation of \a newNbOfElements elements in \a this.
392    * \a _nb_of_elem and \a _nb_of_elem_alloc will \b NOT be systematically equal (contrary to MemArray<T>::reAlloc method.
393    * So after the call of this method \a _nb_of_elem will be equal tostd::min<std::size_t>(_nb_of_elem,newNbOfElements) and \a _nb_of_elem_alloc equal to 
394    * \a newNbOfElements. This method is typically used to perform a pushBack to avoid systematic allocations-copy-deallocation.
395    * So after the call of this method the accessible content is perfectly set.
396    * 
397    * So this method should not be confused with MemArray<T>::reserve that is close to MemArray<T>::reAlloc but not same.
398    */
399   template<class T>
400   void MemArray<T>::reserve(std::size_t newNbOfElements)
401   {
402     if(_nb_of_elem_alloc==newNbOfElements)
403       return ;
404     T *pointer=(T*)malloc(newNbOfElements*sizeof(T));
405     std::copy(_pointer.getConstPointer(),_pointer.getConstPointer()+std::min<std::size_t>(_nb_of_elem,newNbOfElements),pointer);
406     if(_ownership)
407       DestroyPointer(const_cast<T *>(_pointer.getConstPointer()),_dealloc,_param_for_deallocator);//Do not use getPointer because in case of _external
408     _pointer.setInternal(pointer);
409     _nb_of_elem=std::min<std::size_t>(_nb_of_elem,newNbOfElements);
410     _nb_of_elem_alloc=newNbOfElements;
411     _ownership=true;
412     _dealloc=CDeallocator;
413     _param_for_deallocator=0;
414   }
415
416   /*!
417    * This method performs systematically an allocation of \a newNbOfElements elements in \a this.
418    * \a _nb_of_elem and \a _nb_of_elem_alloc will be equal even if only std::min<std::size_t>(_nb_of_elem,newNbOfElements) come from the .
419    * The remaing part of the new allocated chunk are available but not set previouly !
420    * 
421    * So this method should not be confused with MemArray<T>::reserve that is close to MemArray<T>::reAlloc but not same.
422    */
423   template<class T>
424   void MemArray<T>::reAlloc(std::size_t newNbOfElements)
425   {
426     if(_nb_of_elem==newNbOfElements)
427       return ;
428     T *pointer=(T*)malloc(newNbOfElements*sizeof(T));
429     std::copy(_pointer.getConstPointer(),_pointer.getConstPointer()+std::min<std::size_t>(_nb_of_elem,newNbOfElements),pointer);
430     if(_ownership)
431       DestroyPointer(const_cast<T *>(_pointer.getConstPointer()),_dealloc,_param_for_deallocator);//Do not use getPointer because in case of _external
432     _pointer.setInternal(pointer);
433     _nb_of_elem=newNbOfElements;
434     _nb_of_elem_alloc=newNbOfElements;
435     _ownership=true;
436     _dealloc=CDeallocator;
437     _param_for_deallocator=0;
438   }
439
440   template<class T>
441   void MemArray<T>::CPPDeallocator(void *pt, void *param)
442   {
443     delete [] reinterpret_cast<T*>(pt);
444   }
445
446   template<class T>
447   void MemArray<T>::CDeallocator(void *pt, void *param)
448   {
449     free(pt);
450   }
451
452   template<class T>
453   typename MemArray<T>::Deallocator MemArray<T>::BuildFromType(DeallocType type)
454   {
455     switch(type)
456     {
457       case CPP_DEALLOC:
458         return CPPDeallocator;
459       case C_DEALLOC:
460         return CDeallocator;
461       default:
462         throw INTERP_KERNEL::Exception("Invalid deallocation requested ! Unrecognized enum DeallocType !");
463     }
464   }
465
466   template<class T>
467   void MemArray<T>::DestroyPointer(T *pt, typename MemArray<T>::Deallocator dealloc, void *param)
468   {
469     if(dealloc)
470       dealloc(pt,param);
471   }
472
473   template<class T>
474   void MemArray<T>::destroy()
475   {
476     if(_ownership)
477       DestroyPointer(const_cast<T *>(_pointer.getConstPointer()),_dealloc,_param_for_deallocator);//Do not use getPointer because in case of _external
478     _pointer.null();
479     _ownership=false;
480     _dealloc=NULL;
481     _param_for_deallocator=NULL;
482     _nb_of_elem=0;
483     _nb_of_elem_alloc=0;
484   }
485
486   template<class T>
487   MemArray<T> &MemArray<T>::operator=(const MemArray<T>& other)
488   {
489     alloc(other._nb_of_elem);
490     std::copy(other._pointer.getConstPointer(),other._pointer.getConstPointer()+_nb_of_elem,_pointer.getPointer());
491     return *this;
492   }
493 }
494
495 #endif