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