Salome HOME
On the road of ParaMEDReader for fields.
[modules/med.git] / src / MEDCoupling / MEDCouplingPartDefinition.cxx
1 // Copyright (C) 2007-2014  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 // Author : Anthony Geay (EDF R&D)
20
21 #include "MEDCouplingPartDefinition.hxx"
22
23 using namespace ParaMEDMEM;
24
25 PartDefinition *PartDefinition::New(int start, int stop, int step)
26 {
27   return SlicePartDefinition::New(start,stop,step);
28 }
29
30 PartDefinition *PartDefinition::New(DataArrayInt *listOfIds)
31 {
32   return DataArrayPartDefinition::New(listOfIds);
33 }
34
35 PartDefinition::~PartDefinition()
36 {
37 }
38
39 DataArrayPartDefinition *DataArrayPartDefinition::New(DataArrayInt *listOfIds)
40 {
41   return new DataArrayPartDefinition(listOfIds);
42 }
43
44 int DataArrayPartDefinition::getNumberOfElems() const
45 {
46   checkInternalArrayOK();
47   return _arr->getNumberOfTuples();
48 }
49
50 PartDefinition *DataArrayPartDefinition::operator+(const PartDefinition& other) const
51 {
52   const PartDefinition *otherPt(&other);
53   if(!otherPt)
54     throw INTERP_KERNEL::Exception("DataArrayPartDefinition::operator+ : NULL input !");
55   const DataArrayPartDefinition *other1(dynamic_cast<const DataArrayPartDefinition *>(otherPt));
56   if(other1)
57     return add1(other1);
58   const SlicePartDefinition *other2(dynamic_cast<const SlicePartDefinition *>(otherPt));
59   if(other2)
60     return add2(other2);
61   throw INTERP_KERNEL::Exception("DataArrayPartDefinition::operator+ : unrecognized type in input !");
62 }
63
64 std::string DataArrayPartDefinition::getRepr() const
65 {
66   std::ostringstream oss; oss << "DataArray Part : ";
67   const DataArrayInt *arr(_arr);
68   if(arr)
69     arr->reprQuickOverview(oss);
70   else
71     oss << "No Data !";
72   return oss.str();
73 }
74
75 DataArrayInt *DataArrayPartDefinition::toDAI() const
76 {
77   checkInternalArrayOK();
78   const DataArrayInt *arr(_arr);
79   DataArrayInt *arr2(const_cast<DataArrayInt *>(arr));
80   arr2->incrRef();
81   return arr2;
82 }
83
84 DataArrayPartDefinition::DataArrayPartDefinition(DataArrayInt *listOfIds)
85 {
86   CheckInternalArrayOK(listOfIds);
87   _arr=listOfIds;
88   _arr->incrRef();
89 }
90
91 void DataArrayPartDefinition::checkInternalArrayOK() const
92 {
93   CheckInternalArrayOK(_arr);
94 }
95
96 void DataArrayPartDefinition::CheckInternalArrayOK(const DataArrayInt *listOfIds)
97 {
98   if(!listOfIds || !listOfIds->isAllocated() || listOfIds->getNumberOfComponents()!=1)
99     throw INTERP_KERNEL::Exception("DataArrayPartDefinition::CheckInternalArrayOK : Input list must be not null allocated and with one components !");
100 }
101
102 void DataArrayPartDefinition::updateTime() const
103 {
104   if((const DataArrayInt *)_arr)
105     updateTimeWith(*_arr);
106 }
107
108 std::size_t DataArrayPartDefinition::getHeapMemorySizeWithoutChildren() const
109 {
110   return sizeof(DataArrayPartDefinition);
111 }
112
113 std::vector<const BigMemoryObject *> DataArrayPartDefinition::getDirectChildrenWithNull() const
114 {
115   std::vector<const BigMemoryObject *> ret(1,(const DataArrayInt *)_arr);
116   return ret;
117 }
118
119 DataArrayPartDefinition *DataArrayPartDefinition::add1(const DataArrayPartDefinition *other) const
120 {
121   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> a1(toDAI()),a2(other->toDAI());
122   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> a3(DataArrayInt::Aggregate(a1,a2,0));
123   a3->sort();
124   return DataArrayPartDefinition::New(a3);
125 }
126
127 DataArrayPartDefinition *DataArrayPartDefinition::add2(const SlicePartDefinition *other) const
128 {
129   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> a1(toDAI()),a2(other->toDAI());
130   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> a3(DataArrayInt::Aggregate(a1,a2,0));
131   a3->sort();
132   return DataArrayPartDefinition::New(a3);
133 }
134
135 DataArrayPartDefinition::~DataArrayPartDefinition()
136 {
137 }
138
139 SlicePartDefinition *SlicePartDefinition::New(int start, int stop, int step)
140 {
141   return new SlicePartDefinition(start,stop,step);
142 }
143
144 DataArrayInt *SlicePartDefinition::toDAI() const
145 {
146   return DataArrayInt::Range(_start,_stop,_step);
147 }
148
149 int SlicePartDefinition::getNumberOfElems() const
150 {
151   return DataArray::GetNumberOfItemGivenBES(_start,_stop,_step,"SlicePartDefinition::getNumberOfElems");
152 }
153
154 PartDefinition *SlicePartDefinition::operator+(const PartDefinition& other) const
155 {
156   const PartDefinition *otherPt(&other);
157   if(!otherPt)
158     throw INTERP_KERNEL::Exception("DataArrayPartDefinition::operator+ : NULL input !");
159   const DataArrayPartDefinition *other1(dynamic_cast<const DataArrayPartDefinition *>(otherPt));
160   if(other1)
161     return add1(other1);
162   const SlicePartDefinition *other2(dynamic_cast<const SlicePartDefinition *>(otherPt));
163   if(other2)
164     return add2(other2);
165   throw INTERP_KERNEL::Exception("SlicePartDefinition::operator+ : unrecognized type in input !");
166 }
167
168 std::string SlicePartDefinition::getRepr() const
169 {
170   std::ostringstream oss;
171   oss << "Slice is defined with : start=" << _start << " stop=" << _stop << " step=" << _step;
172   return oss.str();
173 }
174
175 int SlicePartDefinition::getEffectiveStop() const
176 {
177   int nbElems(DataArray::GetNumberOfItemGivenBES(_start,_stop,_step,"SlicePartDefinition::getEffectiveStop"));
178   return _start+nbElems*_step;
179 }
180
181 void SlicePartDefinition::getSlice(int& start, int& stop, int& step) const
182 {
183   start=_start;
184   stop=_stop;
185   step=_step;
186 }
187
188 SlicePartDefinition::SlicePartDefinition(int start, int stop, int step):_start(start),_stop(stop),_step(step)
189 {
190 }
191
192 /*!
193  * No child ! It is the leaf ! So no implementation.
194  */
195 void SlicePartDefinition::updateTime() const
196 {
197 }
198
199 std::size_t SlicePartDefinition::getHeapMemorySizeWithoutChildren() const
200 {
201   return sizeof(SlicePartDefinition);
202 }
203
204 std::vector<const BigMemoryObject *> SlicePartDefinition::getDirectChildrenWithNull() const
205 {
206   return std::vector<const BigMemoryObject *>();
207 }
208
209 DataArrayPartDefinition *SlicePartDefinition::add1(const DataArrayPartDefinition *other) const
210 {
211   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> a1(toDAI()),a2(other->toDAI());
212   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> a3(DataArrayInt::Aggregate(a1,a2,0));
213   a3->sort();
214   return DataArrayPartDefinition::New(a3);
215 }
216
217 PartDefinition *SlicePartDefinition::add2(const SlicePartDefinition *other) const
218 {
219   if(_step==other->_step && getEffectiveStop()==other->_start)
220     {
221       return SlicePartDefinition::New(_start,other->_stop,_step);
222     }
223   else
224     {
225       MEDCouplingAutoRefCountObjectPtr<DataArrayInt> a1(toDAI()),a2(other->toDAI());
226       MEDCouplingAutoRefCountObjectPtr<DataArrayInt> a3(DataArrayInt::Aggregate(a1,a2,0));
227       a3->sort();
228       return DataArrayPartDefinition::New(a3);
229     }
230 }
231
232 SlicePartDefinition::~SlicePartDefinition()
233 {
234 }