1 // Copyright (C) 2007-2012 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #ifndef __PARAMEDMEM_MEDCOUPLINGMEMARRAY_TXX__
20 #define __PARAMEDMEM_MEDCOUPLINGMEMARRAY_TXX__
22 #include "MEDCouplingMemArray.hxx"
23 #include "NormalizedUnstructuredMesh.hxx"
24 #include "InterpKernelException.hxx"
25 #include "InterpolationUtils.hxx"
33 void MEDCouplingPointer<T>::setInternal(T *pointer)
40 void MEDCouplingPointer<T>::setExternal(const T *pointer)
47 MemArray<T>::MemArray(const MemArray<T>& other):_nb_of_elem(-1),_ownership(false),_dealloc(CPP_DEALLOC)
49 if(!other._pointer.isNull())
51 T *pointer=new T[other._nb_of_elem];
52 std::copy(other._pointer.getConstPointer(),other._pointer.getConstPointer()+other._nb_of_elem,pointer);
53 useArray(pointer,true,CPP_DEALLOC,other._nb_of_elem);
58 void MemArray<T>::useArray(const T *array, bool ownership, DeallocType type, int nbOfElem)
63 _pointer.setInternal(const_cast<T *>(array));
65 _pointer.setExternal(array);
71 void MemArray<T>::writeOnPlace(int id, T element0, const T *others, int sizeOfOthers)
73 if(id+sizeOfOthers>=_nb_of_elem)
74 reAlloc(2*_nb_of_elem+sizeOfOthers+1);
75 T *pointer=_pointer.getPointer();
77 std::copy(others,others+sizeOfOthers,pointer+id+1);
81 bool MemArray<T>::isEqual(const MemArray<T>& other, T prec, std::string& reason) const
83 std::ostringstream oss; oss.precision(15);
84 if(_nb_of_elem!=other._nb_of_elem)
86 oss << "Number of elements in coarse data of DataArray mismatch : this=" << _nb_of_elem << " other=" << other._nb_of_elem;
90 const T *pt1=_pointer.getConstPointer();
91 const T *pt2=other._pointer.getConstPointer();
96 oss << "coarse data pointer is defined for only one DataArray instance !";
102 for(int i=0;i<_nb_of_elem;i++)
103 if(pt1[i]-pt2[i]<-prec || (pt1[i]-pt2[i])>prec)
105 oss << "The content of data differs at pos #" << i << " of coarse data ! this[i]=" << pt1[i] << " other[i]=" << pt2[i];
113 * @param sl is typically the number of components [in parameter]
116 void MemArray<T>::repr(int sl, std::ostream& stream) const
118 stream << "Number of tuples : ";
119 if(!_pointer.isNull())
122 stream << _nb_of_elem/sl;
124 stream << "Empty Data";
129 stream << "Data content :\n";
130 const T *data=getConstPointer();
131 if(!_pointer.isNull())
133 if(_nb_of_elem!=0 && sl!=0)
135 int nbOfTuples=_nb_of_elem/sl;
136 for(int i=0;i<nbOfTuples;i++)
138 stream << "Tuple #" << i << " : ";
139 std::copy(data,data+sl,std::ostream_iterator<T>(stream," "));
145 stream << "Empty Data\n";
148 stream << "No data !\n";
152 * @param sl is typically the number of components [in parameter]
155 void MemArray<T>::reprZip(int sl, std::ostream& stream) const
157 stream << "Number of tuples : ";
158 if(!_pointer.isNull())
161 stream << _nb_of_elem/sl;
163 stream << "Empty Data";
168 stream << "Data content : ";
169 const T *data=getConstPointer();
170 if(!_pointer.isNull())
172 if(_nb_of_elem!=0 && sl!=0)
174 int nbOfTuples=_nb_of_elem/sl;
175 for(int i=0;i<nbOfTuples;i++)
178 std::copy(data,data+sl,std::ostream_iterator<T>(stream," "));
185 stream << "Empty Data\n";
188 stream << "No data !\n";
192 void MemArray<T>::fillWithValue(const T& val)
194 T *pt=_pointer.getPointer();
195 std::fill(pt,pt+_nb_of_elem,val);
199 T *MemArray<T>::fromNoInterlace(int nbOfComp) const
201 const T *pt=_pointer.getConstPointer();
202 int nbOfTuples=_nb_of_elem/nbOfComp;
203 T *ret=new T[_nb_of_elem];
205 for(int i=0;i<nbOfTuples;i++)
206 for(int j=0;j<nbOfComp;j++,w++)
207 *w=pt[j*nbOfTuples+i];
212 T *MemArray<T>::toNoInterlace(int nbOfComp) const
214 const T *pt=_pointer.getConstPointer();
215 int nbOfTuples=_nb_of_elem/nbOfComp;
216 T *ret=new T[_nb_of_elem];
218 for(int i=0;i<nbOfComp;i++)
219 for(int j=0;j<nbOfTuples;j++,w++)
225 void MemArray<T>::sort(bool asc)
227 T *pt=_pointer.getPointer();
229 std::sort(pt,pt+_nb_of_elem);
232 typename std::reverse_iterator<T *> it1(pt+_nb_of_elem);
233 typename std::reverse_iterator<T *> it2(pt);
239 void MemArray<T>::reverse()
241 T *pt=_pointer.getPointer();
242 std::reverse(pt,pt+_nb_of_elem);
246 void MemArray<T>::alloc(int nbOfElements) throw(INTERP_KERNEL::Exception)
250 throw INTERP_KERNEL::Exception("MemArray::alloc : request for negative length of data !");
251 _nb_of_elem=nbOfElements;
252 _pointer.setInternal(new T[_nb_of_elem]);
254 _dealloc=CPP_DEALLOC;
258 void MemArray<T>::reAlloc(int newNbOfElements) throw(INTERP_KERNEL::Exception)
260 if(newNbOfElements<0)
261 throw INTERP_KERNEL::Exception("MemArray::reAlloc : request for negative length of data !");
262 T *pointer=new T[newNbOfElements];
263 std::copy(_pointer.getConstPointer(),_pointer.getConstPointer()+std::min<int>(_nb_of_elem,newNbOfElements),pointer);
265 destroyPointer(const_cast<T *>(_pointer.getConstPointer()),_dealloc);//Do not use getPointer because in case of _external
266 _pointer.setInternal(pointer);
267 _nb_of_elem=newNbOfElements;
269 _dealloc=CPP_DEALLOC;
273 void MemArray<T>::destroyPointer(T *pt, DeallocType type)
288 std::ostringstream stream;
289 stream << "Invalid deallocation requested for pointer " << pt;
290 throw INTERP_KERNEL::Exception(stream.str().c_str());
295 void MemArray<T>::destroy()
298 destroyPointer(const_cast<T *>(_pointer.getConstPointer()),_dealloc);//Do not use getPointer because in case of _external
304 MemArray<T> &MemArray<T>::operator=(const MemArray<T>& other)
306 alloc(other._nb_of_elem);
307 std::copy(other._pointer.getConstPointer(),other._pointer.getConstPointer()+_nb_of_elem,_pointer.getPointer());