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 // Author : Anthony Geay (CEA/DEN)
20 #ifndef __PARAMEDMEM_MEDCOUPLINGMEMARRAY_TXX__
21 #define __PARAMEDMEM_MEDCOUPLINGMEMARRAY_TXX__
23 #include "MEDCouplingMemArray.hxx"
24 #include "NormalizedUnstructuredMesh.hxx"
25 #include "InterpKernelException.hxx"
26 #include "InterpolationUtils.hxx"
34 void MEDCouplingPointer<T>::setInternal(T *pointer)
41 void MEDCouplingPointer<T>::setExternal(const T *pointer)
48 MemArray<T>::MemArray(const MemArray<T>& other):_nb_of_elem(-1),_ownership(false),_dealloc(CPP_DEALLOC)
50 if(!other._pointer.isNull())
52 T *pointer=new T[other._nb_of_elem];
53 std::copy(other._pointer.getConstPointer(),other._pointer.getConstPointer()+other._nb_of_elem,pointer);
54 useArray(pointer,true,CPP_DEALLOC,other._nb_of_elem);
59 void MemArray<T>::useArray(const T *array, bool ownership, DeallocType type, int nbOfElem)
64 _pointer.setInternal(const_cast<T *>(array));
66 _pointer.setExternal(array);
72 void MemArray<T>::useExternalArrayWithRWAccess(const T *array, int nbOfElem)
76 _pointer.setInternal(const_cast<T *>(array));
82 void MemArray<T>::writeOnPlace(int id, T element0, const T *others, int sizeOfOthers)
84 if(id+sizeOfOthers>=_nb_of_elem)
85 reAlloc(2*_nb_of_elem+sizeOfOthers+1);
86 T *pointer=_pointer.getPointer();
88 std::copy(others,others+sizeOfOthers,pointer+id+1);
92 bool MemArray<T>::isEqual(const MemArray<T>& other, T prec, std::string& reason) const
94 std::ostringstream oss; oss.precision(15);
95 if(_nb_of_elem!=other._nb_of_elem)
97 oss << "Number of elements in coarse data of DataArray mismatch : this=" << _nb_of_elem << " other=" << other._nb_of_elem;
101 const T *pt1=_pointer.getConstPointer();
102 const T *pt2=other._pointer.getConstPointer();
107 oss << "coarse data pointer is defined for only one DataArray instance !";
113 for(int i=0;i<_nb_of_elem;i++)
114 if(pt1[i]-pt2[i]<-prec || (pt1[i]-pt2[i])>prec)
116 oss << "The content of data differs at pos #" << i << " of coarse data ! this[i]=" << pt1[i] << " other[i]=" << pt2[i];
124 * @param sl is typically the number of components [in parameter]
127 void MemArray<T>::repr(int sl, std::ostream& stream) const
129 stream << "Number of tuples : ";
130 if(!_pointer.isNull())
133 stream << _nb_of_elem/sl;
135 stream << "Empty Data";
140 stream << "Data content :\n";
141 const T *data=getConstPointer();
142 if(!_pointer.isNull())
144 if(_nb_of_elem!=0 && sl!=0)
146 int nbOfTuples=_nb_of_elem/sl;
147 for(int i=0;i<nbOfTuples;i++)
149 stream << "Tuple #" << i << " : ";
150 std::copy(data,data+sl,std::ostream_iterator<T>(stream," "));
156 stream << "Empty Data\n";
159 stream << "No data !\n";
163 * @param sl is typically the number of components [in parameter]
166 void MemArray<T>::reprZip(int sl, std::ostream& stream) const
168 stream << "Number of tuples : ";
169 if(!_pointer.isNull())
172 stream << _nb_of_elem/sl;
174 stream << "Empty Data";
179 stream << "Data content : ";
180 const T *data=getConstPointer();
181 if(!_pointer.isNull())
183 if(_nb_of_elem!=0 && sl!=0)
185 int nbOfTuples=_nb_of_elem/sl;
186 for(int i=0;i<nbOfTuples;i++)
189 std::copy(data,data+sl,std::ostream_iterator<T>(stream," "));
196 stream << "Empty Data\n";
199 stream << "No data !\n";
203 void MemArray<T>::fillWithValue(const T& val)
205 T *pt=_pointer.getPointer();
206 std::fill(pt,pt+_nb_of_elem,val);
210 T *MemArray<T>::fromNoInterlace(int nbOfComp) const
212 const T *pt=_pointer.getConstPointer();
213 int nbOfTuples=_nb_of_elem/nbOfComp;
214 T *ret=new T[_nb_of_elem];
216 for(int i=0;i<nbOfTuples;i++)
217 for(int j=0;j<nbOfComp;j++,w++)
218 *w=pt[j*nbOfTuples+i];
223 T *MemArray<T>::toNoInterlace(int nbOfComp) const
225 const T *pt=_pointer.getConstPointer();
226 int nbOfTuples=_nb_of_elem/nbOfComp;
227 T *ret=new T[_nb_of_elem];
229 for(int i=0;i<nbOfComp;i++)
230 for(int j=0;j<nbOfTuples;j++,w++)
236 void MemArray<T>::sort(bool asc)
238 T *pt=_pointer.getPointer();
240 std::sort(pt,pt+_nb_of_elem);
243 typename std::reverse_iterator<T *> it1(pt+_nb_of_elem);
244 typename std::reverse_iterator<T *> it2(pt);
250 void MemArray<T>::reverse()
252 T *pt=_pointer.getPointer();
253 std::reverse(pt,pt+_nb_of_elem);
257 void MemArray<T>::alloc(int nbOfElements) throw(INTERP_KERNEL::Exception)
261 throw INTERP_KERNEL::Exception("MemArray::alloc : request for negative length of data !");
262 _nb_of_elem=nbOfElements;
263 _pointer.setInternal(new T[_nb_of_elem]);
265 _dealloc=CPP_DEALLOC;
269 void MemArray<T>::reAlloc(int newNbOfElements) throw(INTERP_KERNEL::Exception)
271 if(newNbOfElements<0)
272 throw INTERP_KERNEL::Exception("MemArray::reAlloc : request for negative length of data !");
273 T *pointer=new T[newNbOfElements];
274 std::copy(_pointer.getConstPointer(),_pointer.getConstPointer()+std::min<int>(_nb_of_elem,newNbOfElements),pointer);
276 destroyPointer(const_cast<T *>(_pointer.getConstPointer()),_dealloc);//Do not use getPointer because in case of _external
277 _pointer.setInternal(pointer);
278 _nb_of_elem=newNbOfElements;
280 _dealloc=CPP_DEALLOC;
284 void MemArray<T>::destroyPointer(T *pt, DeallocType type)
299 std::ostringstream stream;
300 stream << "Invalid deallocation requested for pointer " << pt;
301 throw INTERP_KERNEL::Exception(stream.str().c_str());
306 void MemArray<T>::destroy()
309 destroyPointer(const_cast<T *>(_pointer.getConstPointer()),_dealloc);//Do not use getPointer because in case of _external
315 MemArray<T> &MemArray<T>::operator=(const MemArray<T>& other)
317 alloc(other._nb_of_elem);
318 std::copy(other._pointer.getConstPointer(),other._pointer.getConstPointer()+_nb_of_elem,_pointer.getPointer());