1 // Copyright (C) 2007-2014 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 ParaMEDMEM;
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()
39 DataArrayPartDefinition *DataArrayPartDefinition::New(DataArrayInt *listOfIds)
41 return new DataArrayPartDefinition(listOfIds);
44 int DataArrayPartDefinition::getNumberOfElems() const
46 checkInternalArrayOK();
47 return _arr->getNumberOfTuples();
50 PartDefinition *DataArrayPartDefinition::operator+(const PartDefinition& other) const
52 const PartDefinition *otherPt(&other);
54 throw INTERP_KERNEL::Exception("DataArrayPartDefinition::operator+ : NULL input !");
55 const DataArrayPartDefinition *other1(dynamic_cast<const DataArrayPartDefinition *>(otherPt));
58 const SlicePartDefinition *other2(dynamic_cast<const SlicePartDefinition *>(otherPt));
61 throw INTERP_KERNEL::Exception("DataArrayPartDefinition::operator+ : unrecognized type in input !");
64 std::string DataArrayPartDefinition::getRepr() const
66 std::ostringstream oss; oss << "DataArray Part : ";
67 const DataArrayInt *arr(_arr);
69 arr->reprQuickOverview(oss);
76 * This method operates FoG where F is \a this and G is \a other.
77 * 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
79 PartDefinition *DataArrayPartDefinition::composeWith(const PartDefinition *other) const
82 throw INTERP_KERNEL::Exception("DataArrayPartDefinition::composeWith : input PartDef must be not NULL !");
84 other->checkCoherency();
85 const SlicePartDefinition *spd(dynamic_cast<const SlicePartDefinition *>(other));
87 {//special case for optim
92 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> arr(DataArrayInt::New());
93 arr->alloc(_arr->getNumberOfTuples(),1);
94 std::transform(_arr->begin(),_arr->end(),arr->getPointer(),std::bind2nd(std::plus<int>(),a));
95 return DataArrayPartDefinition::New(arr);
99 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> arr1(other->toDAI());
100 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> arr2(arr1->selectByTupleIdSafe(_arr->begin(),_arr->end()));
101 return DataArrayPartDefinition::New(arr2);
104 void DataArrayPartDefinition::checkCoherency() const
106 CheckInternalArrayOK(_arr);
110 * This method tries to simplify \a this if possible.
112 * \return a new reference (equal to this) to be decrRefed.
114 PartDefinition *DataArrayPartDefinition::tryToSimplify() const
118 if(_arr->isRange(a,b,c))
120 return SlicePartDefinition::New(a,b,c);
124 PartDefinition *ret(const_cast<DataArrayPartDefinition *>(this));
130 DataArrayInt *DataArrayPartDefinition::toDAI() const
132 checkInternalArrayOK();
133 const DataArrayInt *arr(_arr);
134 DataArrayInt *arr2(const_cast<DataArrayInt *>(arr));
139 DataArrayPartDefinition::DataArrayPartDefinition(DataArrayInt *listOfIds)
141 CheckInternalArrayOK(listOfIds);
146 void DataArrayPartDefinition::checkInternalArrayOK() const
148 CheckInternalArrayOK(_arr);
151 void DataArrayPartDefinition::CheckInternalArrayOK(const DataArrayInt *listOfIds)
153 if(!listOfIds || !listOfIds->isAllocated() || listOfIds->getNumberOfComponents()!=1)
154 throw INTERP_KERNEL::Exception("DataArrayPartDefinition::CheckInternalArrayOK : Input list must be not null allocated and with one components !");
157 void DataArrayPartDefinition::updateTime() const
159 if((const DataArrayInt *)_arr)
160 updateTimeWith(*_arr);
163 std::size_t DataArrayPartDefinition::getHeapMemorySizeWithoutChildren() const
165 return sizeof(DataArrayPartDefinition);
168 std::vector<const BigMemoryObject *> DataArrayPartDefinition::getDirectChildrenWithNull() const
170 std::vector<const BigMemoryObject *> ret(1,(const DataArrayInt *)_arr);
174 DataArrayPartDefinition *DataArrayPartDefinition::add1(const DataArrayPartDefinition *other) const
176 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> a1(toDAI()),a2(other->toDAI());
177 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> a3(DataArrayInt::Aggregate(a1,a2,0));
179 return DataArrayPartDefinition::New(a3);
182 DataArrayPartDefinition *DataArrayPartDefinition::add2(const SlicePartDefinition *other) const
184 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> a1(toDAI()),a2(other->toDAI());
185 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> a3(DataArrayInt::Aggregate(a1,a2,0));
187 return DataArrayPartDefinition::New(a3);
190 DataArrayPartDefinition::~DataArrayPartDefinition()
194 SlicePartDefinition *SlicePartDefinition::New(int start, int stop, int step)
196 return new SlicePartDefinition(start,stop,step);
199 DataArrayInt *SlicePartDefinition::toDAI() const
201 return DataArrayInt::Range(_start,_stop,_step);
204 int SlicePartDefinition::getNumberOfElems() const
206 return DataArray::GetNumberOfItemGivenBES(_start,_stop,_step,"SlicePartDefinition::getNumberOfElems");
209 PartDefinition *SlicePartDefinition::operator+(const PartDefinition& other) const
211 const PartDefinition *otherPt(&other);
213 throw INTERP_KERNEL::Exception("DataArrayPartDefinition::operator+ : NULL input !");
214 const DataArrayPartDefinition *other1(dynamic_cast<const DataArrayPartDefinition *>(otherPt));
217 const SlicePartDefinition *other2(dynamic_cast<const SlicePartDefinition *>(otherPt));
220 throw INTERP_KERNEL::Exception("SlicePartDefinition::operator+ : unrecognized type in input !");
224 * This method operates FoG where F is \a this and G is \a other.
225 * 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
227 PartDefinition *SlicePartDefinition::composeWith(const PartDefinition *other) const
230 throw INTERP_KERNEL::Exception("SlicePartDefinition::composeWith : input PartDef must be not NULL !");
232 other->checkCoherency();
233 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> arr(other->toDAI());
234 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> arr1(arr->selectByTupleId2(_start,_stop,_step));
235 return DataArrayPartDefinition::New(arr1);
239 * Do nothing it is not a bug.
241 void SlicePartDefinition::checkCoherency() const
246 * Return \a this (because it cannot be simplified)
248 * \return a new reference (equal to this) to be decrRefed.
250 PartDefinition *SlicePartDefinition::tryToSimplify() const
252 PartDefinition *ret(const_cast<SlicePartDefinition *>(this));
257 std::string SlicePartDefinition::getRepr() const
259 std::ostringstream oss;
260 oss << "Slice is defined with : start=" << _start << " stop=" << _stop << " step=" << _step;
264 int SlicePartDefinition::getEffectiveStop() const
266 int nbElems(DataArray::GetNumberOfItemGivenBES(_start,_stop,_step,"SlicePartDefinition::getEffectiveStop"));
267 return _start+nbElems*_step;
270 void SlicePartDefinition::getSlice(int& start, int& stop, int& step) const
277 SlicePartDefinition::SlicePartDefinition(int start, int stop, int step):_start(start),_stop(stop),_step(step)
282 * No child ! It is the leaf ! So no implementation.
284 void SlicePartDefinition::updateTime() const
288 std::size_t SlicePartDefinition::getHeapMemorySizeWithoutChildren() const
290 return sizeof(SlicePartDefinition);
293 std::vector<const BigMemoryObject *> SlicePartDefinition::getDirectChildrenWithNull() const
295 return std::vector<const BigMemoryObject *>();
298 DataArrayPartDefinition *SlicePartDefinition::add1(const DataArrayPartDefinition *other) const
300 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> a1(toDAI()),a2(other->toDAI());
301 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> a3(DataArrayInt::Aggregate(a1,a2,0));
303 return DataArrayPartDefinition::New(a3);
306 PartDefinition *SlicePartDefinition::add2(const SlicePartDefinition *other) const
308 if(_step==other->_step && getEffectiveStop()==other->_start)
310 return SlicePartDefinition::New(_start,other->_stop,_step);
314 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> a1(toDAI()),a2(other->toDAI());
315 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> a3(DataArrayInt::Aggregate(a1,a2,0));
317 return DataArrayPartDefinition::New(a3);
321 SlicePartDefinition::~SlicePartDefinition()