1 // Copyright (C) 2007-2021 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"
26 using namespace MEDCoupling;
28 PartDefinition *PartDefinition::New(mcIdType start, mcIdType stop, mcIdType step)
30 return SlicePartDefinition::New(start,stop,step);
33 PartDefinition *PartDefinition::New(DataArrayIdType *listOfIds)
35 return DataArrayPartDefinition::New(listOfIds);
38 PartDefinition *PartDefinition::Unserialize(std::vector<mcIdType>& tinyInt, std::vector< MCAuto<DataArrayIdType> >& bigArraysI)
42 MCAuto<PartDefinition> ret(DataArrayPartDefinition::New(bigArraysI.back()));
43 bigArraysI.pop_back();
46 else if(tinyInt.size()==3)
48 MCAuto<PartDefinition> ret(SlicePartDefinition::New(tinyInt[0],tinyInt[1],tinyInt[2]));
49 tinyInt.erase(tinyInt.begin(),tinyInt.begin()+3);
53 throw INTERP_KERNEL::Exception("PartDefinition::Unserialize");
56 PartDefinition::~PartDefinition()
60 DataArrayPartDefinition *DataArrayPartDefinition::New(DataArrayIdType *listOfIds)
62 return new DataArrayPartDefinition(listOfIds);
65 bool DataArrayPartDefinition::isEqual(const PartDefinition *other, std::string& what) const
69 what="DataArrayPartDefinition::isEqual : other is null, this is not null !";
72 const DataArrayPartDefinition *otherC(dynamic_cast<const DataArrayPartDefinition *>(other));
75 what="DataArrayPartDefinition::isEqual : other is not DataArrayPartDefinition !";
78 const DataArrayIdType *arr0(_arr),*arr1(otherC->_arr);
81 if((arr0 && !arr1) || (!arr0 && arr1))
83 what="DataArrayPartDefinition::isEqual : array is not defined both in other and this !";
87 bool ret(arr0->isEqualIfNotWhy(*arr1,what1));
90 what=std::string("DataArrayPartDefinition::isEqual : arrays are not equal :\n")+what1;
96 DataArrayPartDefinition *DataArrayPartDefinition::deepCopy() const
98 const DataArrayIdType *arr(_arr);
100 throw INTERP_KERNEL::Exception("DataArrayPartDefinition::deepCopy : array is null !");
101 return DataArrayPartDefinition::New(const_cast<DataArrayIdType *>(arr));
104 mcIdType DataArrayPartDefinition::getNumberOfElems() const
106 checkInternalArrayOK();
107 return _arr->getNumberOfTuples();
110 PartDefinition *DataArrayPartDefinition::operator+(const PartDefinition& other) const
112 const PartDefinition *otherPt(&other);
114 throw INTERP_KERNEL::Exception("DataArrayPartDefinition::operator+ : NULL input !");
115 const DataArrayPartDefinition *other1(dynamic_cast<const DataArrayPartDefinition *>(otherPt));
118 const SlicePartDefinition *other2(dynamic_cast<const SlicePartDefinition *>(otherPt));
121 throw INTERP_KERNEL::Exception("DataArrayPartDefinition::operator+ : unrecognized type in input !");
124 std::string DataArrayPartDefinition::getRepr() const
126 std::ostringstream oss; oss << "DataArray Part : ";
127 const DataArrayIdType *arr(_arr);
129 arr->reprQuickOverview(oss);
136 * This method operates FoG where F is \a this and G is \a other.
137 * 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
139 PartDefinition *DataArrayPartDefinition::composeWith(const PartDefinition *other) const
142 throw INTERP_KERNEL::Exception("DataArrayPartDefinition::composeWith : input PartDef must be not NULL !");
143 checkConsistencyLight();
144 other->checkConsistencyLight();
145 const SlicePartDefinition *spd(dynamic_cast<const SlicePartDefinition *>(other));
147 {//special case for optim
148 mcIdType a(0),b(0),c(0);
149 spd->getSlice(a,b,c);
152 MCAuto<DataArrayIdType> arr(DataArrayIdType::New());
153 arr->alloc(_arr->getNumberOfTuples(),1);
154 std::transform(_arr->begin(),_arr->end(),arr->getPointer(),std::bind(std::plus<mcIdType>(),std::placeholders::_1,a));
155 return DataArrayPartDefinition::New(arr);
159 MCAuto<DataArrayIdType> arr1(other->toDAI());
160 MCAuto<DataArrayIdType> arr2(arr1->selectByTupleIdSafe(_arr->begin(),_arr->end()));
161 return DataArrayPartDefinition::New(arr2);
164 void DataArrayPartDefinition::checkConsistencyLight() const
166 CheckInternalArrayOK(_arr);
170 * This method tries to simplify \a this if possible.
172 * \return a new reference (equal to this) to be decrRefed.
174 PartDefinition *DataArrayPartDefinition::tryToSimplify() const
176 checkConsistencyLight();
177 mcIdType a(0),b(0),c(0);
178 if(_arr->isRange(a,b,c))
180 return SlicePartDefinition::New(a,b,c);
184 PartDefinition *ret(const_cast<DataArrayPartDefinition *>(this));
190 void DataArrayPartDefinition::serialize(std::vector<mcIdType>& tinyInt, std::vector< MCAuto<DataArrayIdType> >& bigArraysI) const
192 bigArraysI.push_back(_arr);
195 DataArrayIdType *DataArrayPartDefinition::toDAI() const
197 checkInternalArrayOK();
198 const DataArrayIdType *arr(_arr);
199 DataArrayIdType *arr2(const_cast<DataArrayIdType *>(arr));
204 DataArrayPartDefinition::DataArrayPartDefinition(DataArrayIdType *listOfIds)
206 CheckInternalArrayOK(listOfIds);
211 void DataArrayPartDefinition::checkInternalArrayOK() const
213 CheckInternalArrayOK(_arr);
216 void DataArrayPartDefinition::CheckInternalArrayOK(const DataArrayIdType *listOfIds)
218 if(!listOfIds || !listOfIds->isAllocated() || listOfIds->getNumberOfComponents()!=1)
219 throw INTERP_KERNEL::Exception("DataArrayPartDefinition::CheckInternalArrayOK : Input list must be not null allocated and with one components !");
222 void DataArrayPartDefinition::updateTime() const
224 if((const DataArrayIdType *)_arr)
225 updateTimeWith(*_arr);
228 std::size_t DataArrayPartDefinition::getHeapMemorySizeWithoutChildren() const
230 return sizeof(DataArrayPartDefinition);
233 std::vector<const BigMemoryObject *> DataArrayPartDefinition::getDirectChildrenWithNull() const
235 std::vector<const BigMemoryObject *> ret(1,(const DataArrayIdType *)_arr);
239 DataArrayPartDefinition *DataArrayPartDefinition::add1(const DataArrayPartDefinition *other) const
241 MCAuto<DataArrayIdType> a1(toDAI()),a2(other->toDAI());
242 MCAuto<DataArrayIdType> a3(DataArrayIdType::Aggregate(a1,a2,0));
244 return DataArrayPartDefinition::New(a3);
247 DataArrayPartDefinition *DataArrayPartDefinition::add2(const SlicePartDefinition *other) const
249 MCAuto<DataArrayIdType> a1(toDAI()),a2(other->toDAI());
250 MCAuto<DataArrayIdType> a3(DataArrayIdType::Aggregate(a1,a2,0));
252 return DataArrayPartDefinition::New(a3);
255 DataArrayPartDefinition::~DataArrayPartDefinition()
259 SlicePartDefinition *SlicePartDefinition::New(mcIdType start, mcIdType stop, mcIdType step)
261 return new SlicePartDefinition(start,stop,step);
264 bool SlicePartDefinition::isEqual(const PartDefinition *other, std::string& what) const
268 what="SlicePartDefinition::isEqual : other is null, this is not null !";
271 const SlicePartDefinition *otherC(dynamic_cast<const SlicePartDefinition *>(other));
274 what="SlicePartDefinition::isEqual : other is not SlicePartDefinition !";
277 bool ret((_start==otherC->_start) && (_stop==otherC->_stop) && (_step==otherC->_step));
280 what="SlicePartDefinition::isEqual : values are not the same !";
286 SlicePartDefinition *SlicePartDefinition::deepCopy() const
288 return SlicePartDefinition::New(_start,_stop,_step);
291 DataArrayIdType *SlicePartDefinition::toDAI() const
293 return DataArrayIdType::Range(_start,_stop,_step);
296 mcIdType SlicePartDefinition::getNumberOfElems() const
298 return DataArray::GetNumberOfItemGivenBES(_start,_stop,_step,"SlicePartDefinition::getNumberOfElems");
301 PartDefinition *SlicePartDefinition::operator+(const PartDefinition& other) const
303 const PartDefinition *otherPt(&other);
305 throw INTERP_KERNEL::Exception("DataArrayPartDefinition::operator+ : NULL input !");
306 const DataArrayPartDefinition *other1(dynamic_cast<const DataArrayPartDefinition *>(otherPt));
309 const SlicePartDefinition *other2(dynamic_cast<const SlicePartDefinition *>(otherPt));
312 throw INTERP_KERNEL::Exception("SlicePartDefinition::operator+ : unrecognized type in input !");
316 * This method operates FoG where F is \a this and G is \a other.
317 * 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
319 PartDefinition *SlicePartDefinition::composeWith(const PartDefinition *other) const
322 throw INTERP_KERNEL::Exception("SlicePartDefinition::composeWith : input PartDef must be not NULL !");
323 checkConsistencyLight();
324 other->checkConsistencyLight();
325 MCAuto<DataArrayIdType> arr(other->toDAI());
326 MCAuto<DataArrayIdType> arr1(arr->selectByTupleIdSafeSlice(_start,_stop,_step));
327 return DataArrayPartDefinition::New(arr1);
331 * Do nothing it is not a bug.
333 void SlicePartDefinition::checkConsistencyLight() const
338 * Return \a this (because it cannot be simplified)
340 * \return a new reference (equal to this) to be decrRefed.
342 PartDefinition *SlicePartDefinition::tryToSimplify() const
344 PartDefinition *ret(const_cast<SlicePartDefinition *>(this));
349 void SlicePartDefinition::serialize(std::vector<mcIdType>& tinyInt, std::vector< MCAuto<DataArrayIdType> >& bigArraysI) const
351 tinyInt.push_back(_start);
352 tinyInt.push_back(_stop);
353 tinyInt.push_back(_step);
356 std::string SlicePartDefinition::getRepr() const
358 std::ostringstream oss;
359 oss << "Slice is defined with : start=" << _start << " stop=" << _stop << " step=" << _step;
363 mcIdType SlicePartDefinition::getEffectiveStop() const
365 mcIdType nbElems(DataArray::GetNumberOfItemGivenBES(_start,_stop,_step,"SlicePartDefinition::getEffectiveStop"));
366 return _start+nbElems*_step;
369 void SlicePartDefinition::getSlice(mcIdType& start, mcIdType& stop, mcIdType& step) const
376 SlicePartDefinition::SlicePartDefinition(mcIdType start, mcIdType stop, mcIdType step):_start(start),_stop(stop),_step(step)
381 * No child ! It is the leaf ! So no implementation.
383 void SlicePartDefinition::updateTime() const
387 std::size_t SlicePartDefinition::getHeapMemorySizeWithoutChildren() const
389 return sizeof(SlicePartDefinition);
392 std::vector<const BigMemoryObject *> SlicePartDefinition::getDirectChildrenWithNull() const
394 return std::vector<const BigMemoryObject *>();
397 DataArrayPartDefinition *SlicePartDefinition::add1(const DataArrayPartDefinition *other) const
399 MCAuto<DataArrayIdType> a1(toDAI()),a2(other->toDAI());
400 MCAuto<DataArrayIdType> a3(DataArrayIdType::Aggregate(a1,a2,0));
402 return DataArrayPartDefinition::New(a3);
405 PartDefinition *SlicePartDefinition::add2(const SlicePartDefinition *other) const
407 if(_step==other->_step && getEffectiveStop()==other->_start)
409 return SlicePartDefinition::New(_start,other->_stop,_step);
413 MCAuto<DataArrayIdType> a1(toDAI()),a2(other->toDAI());
414 MCAuto<DataArrayIdType> a3(DataArrayIdType::Aggregate(a1,a2,0));
416 return DataArrayPartDefinition::New(a3);
420 SlicePartDefinition::~SlicePartDefinition()