Salome HOME
First impl of MEDCouplingFieldFloat
[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
30 DataArrayFloat *DataArrayFloat::New()
31 {
32   return new DataArrayFloat;
33 }
34
35 DataArrayFloat *DataArrayFloat::deepCopy() const
36 {
37   return new DataArrayFloat(*this);
38 }
39
40 void DataArrayFloat::reprStream(std::ostream& stream) const
41 {
42   stream << "Name of float array : \"" << _name << "\"\n";
43   reprWithoutNameStream(stream);
44 }
45
46 void DataArrayFloat::reprZipStream(std::ostream& stream) const
47 {
48   stream << "Name of float array : \"" << _name << "\"\n";
49   reprZipWithoutNameStream(stream);
50 }
51
52 void DataArrayFloat::reprZipWithoutNameStream(std::ostream& stream) const
53 {
54   DataArray::reprWithoutNameStream(stream);
55   stream.precision(7);
56   _mem.repr(getNumberOfComponents(),stream);
57 }
58
59 void DataArrayFloat::reprCppStream(const std::string& varName, std::ostream& stream) const
60 {
61   int nbTuples(getNumberOfTuples()),nbComp(getNumberOfComponents());
62   const float *data(begin());
63   stream.precision(7);
64   stream << "DataArrayFloat *" << varName << "=DataArrayFloat::New();" << std::endl;
65   if(nbTuples*nbComp>=1)
66     {
67       stream << "const float " << varName << "Data[" << nbTuples*nbComp << "]={";
68       std::copy(data,data+nbTuples*nbComp-1,std::ostream_iterator<float>(stream,","));
69       stream << data[nbTuples*nbComp-1] << "};" << std::endl;
70       stream << varName << "->useArray(" << varName << "Data,false,CPP_DEALLOC," << nbTuples << "," << nbComp << ");" << std::endl;
71     }
72   else
73     stream << varName << "->alloc(" << nbTuples << "," << nbComp << ");" << std::endl;
74   stream << varName << "->setName(\"" << getName() << "\");" << std::endl;
75 }
76
77 void DataArrayFloat::reprQuickOverview(std::ostream& stream) const
78 {
79   static const std::size_t MAX_NB_OF_BYTE_IN_REPR=300;
80   stream << "DataArrayFloat C++ instance at " << this << ". ";
81   if(isAllocated())
82     {
83       int nbOfCompo=(int)_info_on_compo.size();
84       if(nbOfCompo>=1)
85         {
86           int nbOfTuples=getNumberOfTuples();
87           stream << "Number of tuples : " << nbOfTuples << ". Number of components : " << nbOfCompo << "." << std::endl;
88           reprQuickOverviewData(stream,MAX_NB_OF_BYTE_IN_REPR);
89         }
90       else
91         stream << "Number of components : 0.";
92     }
93   else
94     stream << "*** No data allocated ****";
95 }
96
97 void DataArrayFloat::reprQuickOverviewData(std::ostream& stream, std::size_t maxNbOfByteInRepr) const
98 {
99   const float *data(begin());
100   int nbOfTuples(getNumberOfTuples());
101   int nbOfCompo=(int)_info_on_compo.size();
102   std::ostringstream oss2; oss2 << "[";
103   oss2.precision(7);
104   std::string oss2Str(oss2.str());
105   bool isFinished=true;
106   for(int i=0;i<nbOfTuples && isFinished;i++)
107     {
108       if(nbOfCompo>1)
109         {
110           oss2 << "(";
111           for(int j=0;j<nbOfCompo;j++,data++)
112             {
113               oss2 << *data;
114               if(j!=nbOfCompo-1) oss2 << ", ";
115             }
116           oss2 << ")";
117         }
118       else
119         oss2 << *data++;
120       if(i!=nbOfTuples-1) oss2 << ", ";
121       std::string oss3Str(oss2.str());
122       if(oss3Str.length()<maxNbOfByteInRepr)
123         oss2Str=oss3Str;
124       else
125         isFinished=false;
126     }
127   stream << oss2Str;
128   if(!isFinished)
129     stream << "... ";
130   stream << "]";
131 }
132
133 std::string DataArrayFloat::reprNotTooLong() const
134 {
135   std::ostringstream ret;
136   reprNotTooLongStream(ret);
137   return ret.str();
138 }
139
140 void DataArrayFloat::reprNotTooLongStream(std::ostream& stream) const
141 {
142   stream << "Name of float array : \"" << _name << "\"\n";
143   reprNotTooLongWithoutNameStream(stream);
144 }
145
146 void DataArrayFloat::reprNotTooLongWithoutNameStream(std::ostream& stream) const
147 {
148   DataArray::reprWithoutNameStream(stream);
149   stream.precision(7);
150   _mem.reprNotTooLong(getNumberOfComponents(),stream);
151 }
152
153 bool DataArrayFloat::isEqualIfNotWhy(const DataArrayFloat& other, float prec, std::string& reason) const
154 {
155   if(!areInfoEqualsIfNotWhy(other,reason))
156     return false;
157   return _mem.isEqual(other._mem,prec,reason);
158 }
159
160 bool DataArrayFloat::isEqual(const DataArrayFloat& other, float prec) const
161 {
162   std::string tmp;
163   return isEqualIfNotWhy(other,prec,tmp);
164 }
165
166 bool DataArrayFloat::isEqualWithoutConsideringStr(const DataArrayFloat& other, float prec) const
167 {
168   std::string tmp;
169   return _mem.isEqual(other._mem,prec,tmp);
170 }
171
172 /*!
173  * Returns either a \a deep or \a shallow copy of this array. For more info see
174  * \ref MEDCouplingArrayBasicsCopyDeep and \ref MEDCouplingArrayBasicsCopyShallow.
175  *  \param [in] dCpy - if \a true, a deep copy is returned, else, a shallow one.
176  *  \return DataArrayDouble * - either a new instance of DataArrayDouble (if \a dCpy
177  *          == \a true) or \a this instance (if \a dCpy == \a false).
178  */
179 DataArrayFloat *DataArrayFloat::performCopyOrIncrRef(bool dCpy) const
180 {
181   return DataArrayTemplateClassic<float>::PerformCopyOrIncrRef(dCpy,*this);
182 }