1 // Copyright (C) 2007-2015 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"
23 using namespace MEDCoupling;
25 PartDefinition *PartDefinition::New(int start, int stop, int step)
27 return SlicePartDefinition::New(start,stop,step);
30 PartDefinition *PartDefinition::New(DataArrayInt *listOfIds)
32 return DataArrayPartDefinition::New(listOfIds);
35 PartDefinition *PartDefinition::Unserialize(std::vector<int>& tinyInt, std::vector< MEDCouplingAutoRefCountObjectPtr<DataArrayInt> >& bigArraysI)
39 MEDCouplingAutoRefCountObjectPtr<PartDefinition> ret(DataArrayPartDefinition::New(bigArraysI.back()));
40 bigArraysI.pop_back();
43 else if(tinyInt.size()==3)
45 MEDCouplingAutoRefCountObjectPtr<PartDefinition> ret(SlicePartDefinition::New(tinyInt[0],tinyInt[1],tinyInt[2]));
46 tinyInt.erase(tinyInt.begin(),tinyInt.begin()+3);
50 throw INTERP_KERNEL::Exception("PartDefinition::Unserialize");
53 PartDefinition::~PartDefinition()
57 DataArrayPartDefinition *DataArrayPartDefinition::New(DataArrayInt *listOfIds)
59 return new DataArrayPartDefinition(listOfIds);
62 bool DataArrayPartDefinition::isEqual(const PartDefinition *other, std::string& what) const
66 what="DataArrayPartDefinition::isEqual : other is null, this is not null !";
69 const DataArrayPartDefinition *otherC(dynamic_cast<const DataArrayPartDefinition *>(other));
72 what="DataArrayPartDefinition::isEqual : other is not DataArrayPartDefinition !";
75 const DataArrayInt *arr0(_arr),*arr1(otherC->_arr);
78 if((arr0 && !arr1) || (!arr0 && arr1))
80 what="DataArrayPartDefinition::isEqual : array is not defined both in other and this !";
84 bool ret(arr0->isEqualIfNotWhy(*arr1,what1));
87 what=std::string("DataArrayPartDefinition::isEqual : arrays are not equal :\n")+what1;
93 DataArrayPartDefinition *DataArrayPartDefinition::deepCpy() const
95 const DataArrayInt *arr(_arr);
97 throw INTERP_KERNEL::Exception("DataArrayPartDefinition::deepCpy : array is null !");
98 return DataArrayPartDefinition::New(const_cast<DataArrayInt *>(arr));
101 int DataArrayPartDefinition::getNumberOfElems() const
103 checkInternalArrayOK();
104 return _arr->getNumberOfTuples();
107 PartDefinition *DataArrayPartDefinition::operator+(const PartDefinition& other) const
109 const PartDefinition *otherPt(&other);
111 throw INTERP_KERNEL::Exception("DataArrayPartDefinition::operator+ : NULL input !");
112 const DataArrayPartDefinition *other1(dynamic_cast<const DataArrayPartDefinition *>(otherPt));
115 const SlicePartDefinition *other2(dynamic_cast<const SlicePartDefinition *>(otherPt));
118 throw INTERP_KERNEL::Exception("DataArrayPartDefinition::operator+ : unrecognized type in input !");
121 std::string DataArrayPartDefinition::getRepr() const
123 std::ostringstream oss; oss << "DataArray Part : ";
124 const DataArrayInt *arr(_arr);
126 arr->reprQuickOverview(oss);
133 * This method operates FoG where F is \a this and G is \a other.
134 * 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
136 PartDefinition *DataArrayPartDefinition::composeWith(const PartDefinition *other) const
139 throw INTERP_KERNEL::Exception("DataArrayPartDefinition::composeWith : input PartDef must be not NULL !");
141 other->checkCoherency();
142 const SlicePartDefinition *spd(dynamic_cast<const SlicePartDefinition *>(other));
144 {//special case for optim
146 spd->getSlice(a,b,c);
149 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> arr(DataArrayInt::New());
150 arr->alloc(_arr->getNumberOfTuples(),1);
151 std::transform(_arr->begin(),_arr->end(),arr->getPointer(),std::bind2nd(std::plus<int>(),a));
152 return DataArrayPartDefinition::New(arr);
156 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> arr1(other->toDAI());
157 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> arr2(arr1->selectByTupleIdSafe(_arr->begin(),_arr->end()));
158 return DataArrayPartDefinition::New(arr2);
161 void DataArrayPartDefinition::checkCoherency() const
163 CheckInternalArrayOK(_arr);
167 * This method tries to simplify \a this if possible.
169 * \return a new reference (equal to this) to be decrRefed.
171 PartDefinition *DataArrayPartDefinition::tryToSimplify() const
175 if(_arr->isRange(a,b,c))
177 return SlicePartDefinition::New(a,b,c);
181 PartDefinition *ret(const_cast<DataArrayPartDefinition *>(this));
187 void DataArrayPartDefinition::serialize(std::vector<int>& tinyInt, std::vector< MEDCouplingAutoRefCountObjectPtr<DataArrayInt> >& bigArraysI) const
189 bigArraysI.push_back(_arr);
192 DataArrayInt *DataArrayPartDefinition::toDAI() const
194 checkInternalArrayOK();
195 const DataArrayInt *arr(_arr);
196 DataArrayInt *arr2(const_cast<DataArrayInt *>(arr));
201 DataArrayPartDefinition::DataArrayPartDefinition(DataArrayInt *listOfIds)
203 CheckInternalArrayOK(listOfIds);
208 void DataArrayPartDefinition::checkInternalArrayOK() const
210 CheckInternalArrayOK(_arr);
213 void DataArrayPartDefinition::CheckInternalArrayOK(const DataArrayInt *listOfIds)
215 if(!listOfIds || !listOfIds->isAllocated() || listOfIds->getNumberOfComponents()!=1)
216 throw INTERP_KERNEL::Exception("DataArrayPartDefinition::CheckInternalArrayOK : Input list must be not null allocated and with one components !");
219 void DataArrayPartDefinition::updateTime() const
221 if((const DataArrayInt *)_arr)
222 updateTimeWith(*_arr);
225 std::size_t DataArrayPartDefinition::getHeapMemorySizeWithoutChildren() const
227 return sizeof(DataArrayPartDefinition);
230 std::vector<const BigMemoryObject *> DataArrayPartDefinition::getDirectChildrenWithNull() const
232 std::vector<const BigMemoryObject *> ret(1,(const DataArrayInt *)_arr);
236 DataArrayPartDefinition *DataArrayPartDefinition::add1(const DataArrayPartDefinition *other) const
238 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> a1(toDAI()),a2(other->toDAI());
239 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> a3(DataArrayInt::Aggregate(a1,a2,0));
241 return DataArrayPartDefinition::New(a3);
244 DataArrayPartDefinition *DataArrayPartDefinition::add2(const SlicePartDefinition *other) const
246 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> a1(toDAI()),a2(other->toDAI());
247 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> a3(DataArrayInt::Aggregate(a1,a2,0));
249 return DataArrayPartDefinition::New(a3);
252 DataArrayPartDefinition::~DataArrayPartDefinition()
256 SlicePartDefinition *SlicePartDefinition::New(int start, int stop, int step)
258 return new SlicePartDefinition(start,stop,step);
261 bool SlicePartDefinition::isEqual(const PartDefinition *other, std::string& what) const
265 what="SlicePartDefinition::isEqual : other is null, this is not null !";
268 const SlicePartDefinition *otherC(dynamic_cast<const SlicePartDefinition *>(other));
271 what="SlicePartDefinition::isEqual : other is not SlicePartDefinition !";
274 bool ret((_start==otherC->_start) && (_stop==otherC->_stop) && (_step==otherC->_step));
277 what="SlicePartDefinition::isEqual : values are not the same !";
283 SlicePartDefinition *SlicePartDefinition::deepCpy() const
285 return SlicePartDefinition::New(_start,_stop,_step);
288 DataArrayInt *SlicePartDefinition::toDAI() const
290 return DataArrayInt::Range(_start,_stop,_step);
293 int SlicePartDefinition::getNumberOfElems() const
295 return DataArray::GetNumberOfItemGivenBES(_start,_stop,_step,"SlicePartDefinition::getNumberOfElems");
298 PartDefinition *SlicePartDefinition::operator+(const PartDefinition& other) const
300 const PartDefinition *otherPt(&other);
302 throw INTERP_KERNEL::Exception("DataArrayPartDefinition::operator+ : NULL input !");
303 const DataArrayPartDefinition *other1(dynamic_cast<const DataArrayPartDefinition *>(otherPt));
306 const SlicePartDefinition *other2(dynamic_cast<const SlicePartDefinition *>(otherPt));
309 throw INTERP_KERNEL::Exception("SlicePartDefinition::operator+ : unrecognized type in input !");
313 * This method operates FoG where F is \a this and G is \a other.
314 * 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
316 PartDefinition *SlicePartDefinition::composeWith(const PartDefinition *other) const
319 throw INTERP_KERNEL::Exception("SlicePartDefinition::composeWith : input PartDef must be not NULL !");
321 other->checkCoherency();
322 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> arr(other->toDAI());
323 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> arr1(arr->selectByTupleId2(_start,_stop,_step));
324 return DataArrayPartDefinition::New(arr1);
328 * Do nothing it is not a bug.
330 void SlicePartDefinition::checkCoherency() const
335 * Return \a this (because it cannot be simplified)
337 * \return a new reference (equal to this) to be decrRefed.
339 PartDefinition *SlicePartDefinition::tryToSimplify() const
341 PartDefinition *ret(const_cast<SlicePartDefinition *>(this));
346 void SlicePartDefinition::serialize(std::vector<int>& tinyInt, std::vector< MEDCouplingAutoRefCountObjectPtr<DataArrayInt> >& bigArraysI) const
348 tinyInt.push_back(_start);
349 tinyInt.push_back(_stop);
350 tinyInt.push_back(_step);
353 std::string SlicePartDefinition::getRepr() const
355 std::ostringstream oss;
356 oss << "Slice is defined with : start=" << _start << " stop=" << _stop << " step=" << _step;
360 int SlicePartDefinition::getEffectiveStop() const
362 int nbElems(DataArray::GetNumberOfItemGivenBES(_start,_stop,_step,"SlicePartDefinition::getEffectiveStop"));
363 return _start+nbElems*_step;
366 void SlicePartDefinition::getSlice(int& start, int& stop, int& step) const
373 SlicePartDefinition::SlicePartDefinition(int start, int stop, int step):_start(start),_stop(stop),_step(step)
378 * No child ! It is the leaf ! So no implementation.
380 void SlicePartDefinition::updateTime() const
384 std::size_t SlicePartDefinition::getHeapMemorySizeWithoutChildren() const
386 return sizeof(SlicePartDefinition);
389 std::vector<const BigMemoryObject *> SlicePartDefinition::getDirectChildrenWithNull() const
391 return std::vector<const BigMemoryObject *>();
394 DataArrayPartDefinition *SlicePartDefinition::add1(const DataArrayPartDefinition *other) const
396 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> a1(toDAI()),a2(other->toDAI());
397 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> a3(DataArrayInt::Aggregate(a1,a2,0));
399 return DataArrayPartDefinition::New(a3);
402 PartDefinition *SlicePartDefinition::add2(const SlicePartDefinition *other) const
404 if(_step==other->_step && getEffectiveStop()==other->_start)
406 return SlicePartDefinition::New(_start,other->_stop,_step);
410 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> a1(toDAI()),a2(other->toDAI());
411 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> a3(DataArrayInt::Aggregate(a1,a2,0));
413 return DataArrayPartDefinition::New(a3);
417 SlicePartDefinition::~SlicePartDefinition()