Salome HOME
On the road for massive templating to prepare SALOME9 int64
[tools/medcoupling.git] / src / MEDCoupling / MEDCouplingMemArrayFloat.cxx
1 // Copyright (C) 2007-2017  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 "MEDCouplingMemArray.txx"
22
23 using namespace MEDCoupling;
24
25 template class MEDCoupling::MemArray<float>;
26 template class MEDCoupling::DataArrayTemplate<float>;
27 template class MEDCoupling::DataArrayTemplateClassic<float>;
28 template class MEDCoupling::DataArrayTemplateFP<float>;
29 template class MEDCoupling::DataArrayIterator<float>;
30
31 DataArrayFloat *DataArrayFloat::New()
32 {
33   return new DataArrayFloat;
34 }
35
36 DataArrayFloat *DataArrayFloat::deepCopy() const
37 {
38   return new DataArrayFloat(*this);
39 }
40
41 void DataArrayFloat::reprStream(std::ostream& stream) const
42 {
43   stream << "Name of float array : \"" << _name << "\"\n";
44   reprWithoutNameStream(stream);
45 }
46
47 void DataArrayFloat::reprZipStream(std::ostream& stream) const
48 {
49   stream << "Name of float array : \"" << _name << "\"\n";
50   reprZipWithoutNameStream(stream);
51 }
52
53 void DataArrayFloat::reprZipWithoutNameStream(std::ostream& stream) const
54 {
55   DataArray::reprWithoutNameStream(stream);
56   stream.precision(7);
57   _mem.repr(getNumberOfComponents(),stream);
58 }
59
60 void DataArrayFloat::reprCppStream(const std::string& varName, std::ostream& stream) const
61 {
62   int nbTuples(getNumberOfTuples()),nbComp(getNumberOfComponents());
63   const float *data(begin());
64   stream.precision(7);
65   stream << "DataArrayFloat *" << varName << "=DataArrayFloat::New();" << std::endl;
66   if(nbTuples*nbComp>=1)
67     {
68       stream << "const float " << varName << "Data[" << nbTuples*nbComp << "]={";
69       std::copy(data,data+nbTuples*nbComp-1,std::ostream_iterator<float>(stream,","));
70       stream << data[nbTuples*nbComp-1] << "};" << std::endl;
71       stream << varName << "->useArray(" << varName << "Data,false,CPP_DEALLOC," << nbTuples << "," << nbComp << ");" << std::endl;
72     }
73   else
74     stream << varName << "->alloc(" << nbTuples << "," << nbComp << ");" << std::endl;
75   stream << varName << "->setName(\"" << getName() << "\");" << std::endl;
76 }
77
78 void DataArrayFloat::reprQuickOverview(std::ostream& stream) const
79 {
80   static const std::size_t MAX_NB_OF_BYTE_IN_REPR=300;
81   stream << "DataArrayFloat C++ instance at " << this << ". ";
82   if(isAllocated())
83     {
84       int nbOfCompo=(int)_info_on_compo.size();
85       if(nbOfCompo>=1)
86         {
87           int nbOfTuples=getNumberOfTuples();
88           stream << "Number of tuples : " << nbOfTuples << ". Number of components : " << nbOfCompo << "." << std::endl;
89           reprQuickOverviewData(stream,MAX_NB_OF_BYTE_IN_REPR);
90         }
91       else
92         stream << "Number of components : 0.";
93     }
94   else
95     stream << "*** No data allocated ****";
96 }
97
98 void DataArrayFloat::reprQuickOverviewData(std::ostream& stream, std::size_t maxNbOfByteInRepr) const
99 {
100   const float *data(begin());
101   int nbOfTuples(getNumberOfTuples());
102   int nbOfCompo=(int)_info_on_compo.size();
103   std::ostringstream oss2; oss2 << "[";
104   oss2.precision(7);
105   std::string oss2Str(oss2.str());
106   bool isFinished=true;
107   for(int i=0;i<nbOfTuples && isFinished;i++)
108     {
109       if(nbOfCompo>1)
110         {
111           oss2 << "(";
112           for(int j=0;j<nbOfCompo;j++,data++)
113             {
114               oss2 << *data;
115               if(j!=nbOfCompo-1) oss2 << ", ";
116             }
117           oss2 << ")";
118         }
119       else
120         oss2 << *data++;
121       if(i!=nbOfTuples-1) oss2 << ", ";
122       std::string oss3Str(oss2.str());
123       if(oss3Str.length()<maxNbOfByteInRepr)
124         oss2Str=oss3Str;
125       else
126         isFinished=false;
127     }
128   stream << oss2Str;
129   if(!isFinished)
130     stream << "... ";
131   stream << "]";
132 }
133
134 std::string DataArrayFloat::reprNotTooLong() const
135 {
136   std::ostringstream ret;
137   reprNotTooLongStream(ret);
138   return ret.str();
139 }
140
141 void DataArrayFloat::reprNotTooLongStream(std::ostream& stream) const
142 {
143   stream << "Name of float array : \"" << _name << "\"\n";
144   reprNotTooLongWithoutNameStream(stream);
145 }
146
147 void DataArrayFloat::reprNotTooLongWithoutNameStream(std::ostream& stream) const
148 {
149   DataArray::reprWithoutNameStream(stream);
150   stream.precision(7);
151   _mem.reprNotTooLong(getNumberOfComponents(),stream);
152 }
153
154 bool DataArrayFloat::isEqualIfNotWhy(const DataArrayFloat& other, float prec, std::string& reason) const
155 {
156   if(!areInfoEqualsIfNotWhy(other,reason))
157     return false;
158   return _mem.isEqual(other._mem,prec,reason);
159 }
160
161 bool DataArrayFloat::isEqual(const DataArrayFloat& other, float prec) const
162 {
163   std::string tmp;
164   return isEqualIfNotWhy(other,prec,tmp);
165 }
166
167 bool DataArrayFloat::isEqualWithoutConsideringStr(const DataArrayFloat& other, float prec) const
168 {
169   std::string tmp;
170   return _mem.isEqual(other._mem,prec,tmp);
171 }
172
173 DataArrayFloatIterator *DataArrayFloat::iterator()
174 {
175   return new DataArrayFloatIterator(this);
176 }
177
178 DataArrayFloatIterator::DataArrayFloatIterator(DataArrayFloat *da):DataArrayIterator<float>(da)
179 {
180 }
181
182 DataArrayFloatTuple::DataArrayFloatTuple(float *pt, int nbOfComp):DataArrayTuple<float>(pt,nbOfComp)
183 {
184 }
185
186 std::string DataArrayFloatTuple::repr() const
187 {
188   std::ostringstream oss; oss.precision(7); oss << "(";
189   for(int i=0;i<_nb_of_compo-1;i++)
190     oss << _pt[i] << ", ";
191   oss << _pt[_nb_of_compo-1] << ")";
192   return oss.str();
193 }
194
195 float DataArrayFloatTuple::floatValue() const
196 {
197   return this->zeValue();
198 }
199
200 /*!
201  * This method returns a newly allocated instance the caller should dealed with by a MEDCoupling::DataArrayFloat::decrRef.
202  * This method performs \b no copy of data. The content is only referenced using MEDCoupling::DataArrayFloat::useArray with ownership set to \b false.
203  * This method throws an INTERP_KERNEL::Exception is it is impossible to match sizes of \b this that is too say \b nbOfCompo=this->_nb_of_elem and \bnbOfTuples==1 or
204  * \b nbOfCompo=1 and \bnbOfTuples==this->_nb_of_elem.
205  */
206 DataArrayFloat *DataArrayFloatTuple::buildDAFloat(int nbOfTuples, int nbOfCompo) const
207 {
208   return this->buildDA(nbOfTuples,nbOfCompo);
209 }