1 // Copyright (C) 2007-2016 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, or (at your option) any later version.
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 (EDF R&D)
21 #include "MEDCouplingPartDefinition.hxx"
25 using namespace MEDCoupling;
27 PartDefinition *PartDefinition::New(int start, int stop, int step)
29 return SlicePartDefinition::New(start,stop,step);
32 PartDefinition *PartDefinition::New(DataArrayInt *listOfIds)
34 return DataArrayPartDefinition::New(listOfIds);
37 PartDefinition *PartDefinition::Unserialize(std::vector<int>& tinyInt, std::vector< MCAuto<DataArrayInt> >& bigArraysI)
41 MCAuto<PartDefinition> ret(DataArrayPartDefinition::New(bigArraysI.back()));
42 bigArraysI.pop_back();
45 else if(tinyInt.size()==3)
47 MCAuto<PartDefinition> ret(SlicePartDefinition::New(tinyInt[0],tinyInt[1],tinyInt[2]));
48 tinyInt.erase(tinyInt.begin(),tinyInt.begin()+3);
52 throw INTERP_KERNEL::Exception("PartDefinition::Unserialize");
55 PartDefinition::~PartDefinition()
59 DataArrayPartDefinition *DataArrayPartDefinition::New(DataArrayInt *listOfIds)
61 return new DataArrayPartDefinition(listOfIds);
64 bool DataArrayPartDefinition::isEqual(const PartDefinition *other, std::string& what) const
68 what="DataArrayPartDefinition::isEqual : other is null, this is not null !";
71 const DataArrayPartDefinition *otherC(dynamic_cast<const DataArrayPartDefinition *>(other));
74 what="DataArrayPartDefinition::isEqual : other is not DataArrayPartDefinition !";
77 const DataArrayInt *arr0(_arr),*arr1(otherC->_arr);
80 if((arr0 && !arr1) || (!arr0 && arr1))
82 what="DataArrayPartDefinition::isEqual : array is not defined both in other and this !";
86 bool ret(arr0->isEqualIfNotWhy(*arr1,what1));
89 what=std::string("DataArrayPartDefinition::isEqual : arrays are not equal :\n")+what1;
95 DataArrayPartDefinition *DataArrayPartDefinition::deepCopy() const
97 const DataArrayInt *arr(_arr);
99 throw INTERP_KERNEL::Exception("DataArrayPartDefinition::deepCopy : array is null !");
100 return DataArrayPartDefinition::New(const_cast<DataArrayInt *>(arr));
103 int DataArrayPartDefinition::getNumberOfElems() const
105 checkInternalArrayOK();
106 return _arr->getNumberOfTuples();
109 PartDefinition *DataArrayPartDefinition::operator+(const PartDefinition& other) const
111 const PartDefinition *otherPt(&other);
113 throw INTERP_KERNEL::Exception("DataArrayPartDefinition::operator+ : NULL input !");
114 const DataArrayPartDefinition *other1(dynamic_cast<const DataArrayPartDefinition *>(otherPt));
117 const SlicePartDefinition *other2(dynamic_cast<const SlicePartDefinition *>(otherPt));
120 throw INTERP_KERNEL::Exception("DataArrayPartDefinition::operator+ : unrecognized type in input !");
123 std::string DataArrayPartDefinition::getRepr() const
125 std::ostringstream oss; oss << "DataArray Part : ";
126 const DataArrayInt *arr(_arr);
128 arr->reprQuickOverview(oss);
135 * This method operates FoG where F is \a this and G is \a other.
136 * Example : if \a other is SlicePart(4,14,1) and if \a this is DataArrayPartDefinition([0,1,2,3,6,7,8,9]) -> DataArrayPartDefinition([4,5,6,7,11,12,13]) will be returned
138 PartDefinition *DataArrayPartDefinition::composeWith(const PartDefinition *other) const
141 throw INTERP_KERNEL::Exception("DataArrayPartDefinition::composeWith : input PartDef must be not NULL !");
142 checkConsistencyLight();
143 other->checkConsistencyLight();
144 const SlicePartDefinition *spd(dynamic_cast<const SlicePartDefinition *>(other));
146 {//special case for optim
148 spd->getSlice(a,b,c);
151 MCAuto<DataArrayInt> arr(DataArrayInt::New());
152 arr->alloc(_arr->getNumberOfTuples(),1);
153 std::transform(_arr->begin(),_arr->end(),arr->getPointer(),std::bind2nd(std::plus<int>(),a));
154 return DataArrayPartDefinition::New(arr);
158 MCAuto<DataArrayInt> arr1(other->toDAI());
159 MCAuto<DataArrayInt> arr2(arr1->selectByTupleIdSafe(_arr->begin(),_arr->end()));
160 return DataArrayPartDefinition::New(arr2);
163 void DataArrayPartDefinition::checkConsistencyLight() const
165 CheckInternalArrayOK(_arr);
169 * This method tries to simplify \a this if possible.
171 * \return a new reference (equal to this) to be decrRefed.
173 PartDefinition *DataArrayPartDefinition::tryToSimplify() const
175 checkConsistencyLight();
177 if(_arr->isRange(a,b,c))
179 return SlicePartDefinition::New(a,b,c);
183 PartDefinition *ret(const_cast<DataArrayPartDefinition *>(this));
189 void DataArrayPartDefinition::serialize(std::vector<int>& tinyInt, std::vector< MCAuto<DataArrayInt> >& bigArraysI) const
191 bigArraysI.push_back(_arr);
194 DataArrayInt *DataArrayPartDefinition::toDAI() const
196 checkInternalArrayOK();
197 const DataArrayInt *arr(_arr);
198 DataArrayInt *arr2(const_cast<DataArrayInt *>(arr));
203 DataArrayPartDefinition::DataArrayPartDefinition(DataArrayInt *listOfIds)
205 CheckInternalArrayOK(listOfIds);
210 void DataArrayPartDefinition::checkInternalArrayOK() const
212 CheckInternalArrayOK(_arr);
215 void DataArrayPartDefinition::CheckInternalArrayOK(const DataArrayInt *listOfIds)
217 if(!listOfIds || !listOfIds->isAllocated() || listOfIds->getNumberOfComponents()!=1)
218 throw INTERP_KERNEL::Exception("DataArrayPartDefinition::CheckInternalArrayOK : Input list must be not null allocated and with one components !");
221 void DataArrayPartDefinition::updateTime() const
223 if((const DataArrayInt *)_arr)
224 updateTimeWith(*_arr);
227 std::size_t DataArrayPartDefinition::getHeapMemorySizeWithoutChildren() const
229 return sizeof(DataArrayPartDefinition);
232 std::vector<const BigMemoryObject *> DataArrayPartDefinition::getDirectChildrenWithNull() const
234 std::vector<const BigMemoryObject *> ret(1,(const DataArrayInt *)_arr);
238 DataArrayPartDefinition *DataArrayPartDefinition::add1(const DataArrayPartDefinition *other) const
240 MCAuto<DataArrayInt> a1(toDAI()),a2(other->toDAI());
241 MCAuto<DataArrayInt> a3(DataArrayInt::Aggregate(a1,a2,0));
243 return DataArrayPartDefinition::New(a3);
246 DataArrayPartDefinition *DataArrayPartDefinition::add2(const SlicePartDefinition *other) const
248 MCAuto<DataArrayInt> a1(toDAI()),a2(other->toDAI());
249 MCAuto<DataArrayInt> a3(DataArrayInt::Aggregate(a1,a2,0));
251 return DataArrayPartDefinition::New(a3);
254 DataArrayPartDefinition::~DataArrayPartDefinition()
258 SlicePartDefinition *SlicePartDefinition::New(int start, int stop, int step)
260 return new SlicePartDefinition(start,stop,step);
263 bool SlicePartDefinition::isEqual(const PartDefinition *other, std::string& what) const
267 what="SlicePartDefinition::isEqual : other is null, this is not null !";
270 const SlicePartDefinition *otherC(dynamic_cast<const SlicePartDefinition *>(other));
273 what="SlicePartDefinition::isEqual : other is not SlicePartDefinition !";
276 bool ret((_start==otherC->_start) && (_stop==otherC->_stop) && (_step==otherC->_step));
279 what="SlicePartDefinition::isEqual : values are not the same !";
285 SlicePartDefinition *SlicePartDefinition::deepCopy() const
287 return SlicePartDefinition::New(_start,_stop,_step);
290 DataArrayInt *SlicePartDefinition::toDAI() const
292 return DataArrayInt::Range(_start,_stop,_step);
295 int SlicePartDefinition::getNumberOfElems() const
297 return DataArray::GetNumberOfItemGivenBES(_start,_stop,_step,"SlicePartDefinition::getNumberOfElems");
300 PartDefinition *SlicePartDefinition::operator+(const PartDefinition& other) const
302 const PartDefinition *otherPt(&other);
304 throw INTERP_KERNEL::Exception("DataArrayPartDefinition::operator+ : NULL input !");
305 const DataArrayPartDefinition *other1(dynamic_cast<const DataArrayPartDefinition *>(otherPt));
308 const SlicePartDefinition *other2(dynamic_cast<const SlicePartDefinition *>(otherPt));
311 throw INTERP_KERNEL::Exception("SlicePartDefinition::operator+ : unrecognized type in input !");
315 * This method operates FoG where F is \a this and G is \a other.
316 * Example : if \a this is SlicePart(4,6,1) and if \a other is DataArrayPartDefinition([12,13,17,18,22,28,34,44]) -> DataArrayPartDefinition([22,28]) will be returned
318 PartDefinition *SlicePartDefinition::composeWith(const PartDefinition *other) const
321 throw INTERP_KERNEL::Exception("SlicePartDefinition::composeWith : input PartDef must be not NULL !");
322 checkConsistencyLight();
323 other->checkConsistencyLight();
324 MCAuto<DataArrayInt> arr(other->toDAI());
325 MCAuto<DataArrayInt> arr1(arr->selectByTupleIdSafeSlice(_start,_stop,_step));
326 return DataArrayPartDefinition::New(arr1);
330 * Do nothing it is not a bug.
332 void SlicePartDefinition::checkConsistencyLight() const
337 * Return \a this (because it cannot be simplified)
339 * \return a new reference (equal to this) to be decrRefed.
341 PartDefinition *SlicePartDefinition::tryToSimplify() const
343 PartDefinition *ret(const_cast<SlicePartDefinition *>(this));
348 void SlicePartDefinition::serialize(std::vector<int>& tinyInt, std::vector< MCAuto<DataArrayInt> >& bigArraysI) const
350 tinyInt.push_back(_start);
351 tinyInt.push_back(_stop);
352 tinyInt.push_back(_step);
355 std::string SlicePartDefinition::getRepr() const
357 std::ostringstream oss;
358 oss << "Slice is defined with : start=" << _start << " stop=" << _stop << " step=" << _step;
362 int SlicePartDefinition::getEffectiveStop() const
364 int nbElems(DataArray::GetNumberOfItemGivenBES(_start,_stop,_step,"SlicePartDefinition::getEffectiveStop"));
365 return _start+nbElems*_step;
368 void SlicePartDefinition::getSlice(int& start, int& stop, int& step) const
375 SlicePartDefinition::SlicePartDefinition(int start, int stop, int step):_start(start),_stop(stop),_step(step)
380 * No child ! It is the leaf ! So no implementation.
382 void SlicePartDefinition::updateTime() const
386 std::size_t SlicePartDefinition::getHeapMemorySizeWithoutChildren() const
388 return sizeof(SlicePartDefinition);
391 std::vector<const BigMemoryObject *> SlicePartDefinition::getDirectChildrenWithNull() const
393 return std::vector<const BigMemoryObject *>();
396 DataArrayPartDefinition *SlicePartDefinition::add1(const DataArrayPartDefinition *other) const
398 MCAuto<DataArrayInt> a1(toDAI()),a2(other->toDAI());
399 MCAuto<DataArrayInt> a3(DataArrayInt::Aggregate(a1,a2,0));
401 return DataArrayPartDefinition::New(a3);
404 PartDefinition *SlicePartDefinition::add2(const SlicePartDefinition *other) const
406 if(_step==other->_step && getEffectiveStop()==other->_start)
408 return SlicePartDefinition::New(_start,other->_stop,_step);
412 MCAuto<DataArrayInt> a1(toDAI()),a2(other->toDAI());
413 MCAuto<DataArrayInt> a3(DataArrayInt::Aggregate(a1,a2,0));
415 return DataArrayPartDefinition::New(a3);
419 SlicePartDefinition::~SlicePartDefinition()