Salome HOME
Copyright update 2020
[tools/medcoupling.git] / src / MEDCoupling / MEDCouplingMemArrayFloat.cxx
1 // Copyright (C) 2007-2020  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::reprCppStream(const std::string& varName, std::ostream& stream) const
42 {
43   mcIdType nbTuples(getNumberOfTuples());
44   std::size_t nbComp(getNumberOfComponents());
45   const float *data(begin());
46   stream.precision(7);
47   stream << "DataArrayFloat *" << varName << "=DataArrayFloat::New();" << std::endl;
48   if(nbTuples*nbComp>=1)
49     {
50       stream << "const float " << varName << "Data[" << nbTuples*nbComp << "]={";
51       std::copy(data,data+nbTuples*nbComp-1,std::ostream_iterator<float>(stream,","));
52       stream << data[nbTuples*nbComp-1] << "};" << std::endl;
53       stream << varName << "->useArray(" << varName << "Data,false,CPP_DEALLOC," << nbTuples << "," << nbComp << ");" << std::endl;
54     }
55   else
56     stream << varName << "->alloc(" << nbTuples << "," << nbComp << ");" << std::endl;
57   stream << varName << "->setName(\"" << getName() << "\");" << std::endl;
58 }
59
60 void DataArrayFloat::reprQuickOverview(std::ostream& stream) const
61 {
62   static const std::size_t MAX_NB_OF_BYTE_IN_REPR=300;
63   stream << "DataArrayFloat C++ instance at " << this << ". ";
64   if(isAllocated())
65     {
66       std::size_t nbOfCompo=_info_on_compo.size();
67       if(nbOfCompo>=1)
68         {
69           mcIdType nbOfTuples=getNumberOfTuples();
70           stream << "Number of tuples : " << nbOfTuples << ". Number of components : " << nbOfCompo << "." << std::endl;
71           reprQuickOverviewData(stream,MAX_NB_OF_BYTE_IN_REPR);
72         }
73       else
74         stream << "Number of components : 0.";
75     }
76   else
77     stream << "*** No data allocated ****";
78 }
79
80 void DataArrayFloat::reprQuickOverviewData(std::ostream& stream, std::size_t maxNbOfByteInRepr) const
81 {
82   const float *data(begin());
83   mcIdType nbOfTuples(getNumberOfTuples());
84   std::size_t nbOfCompo=_info_on_compo.size();
85   std::ostringstream oss2; oss2 << "[";
86   oss2.precision(7);
87   std::string oss2Str(oss2.str());
88   bool isFinished=true;
89   for(mcIdType i=0;i<nbOfTuples && isFinished;i++)
90     {
91       if(nbOfCompo>1)
92         {
93           oss2 << "(";
94           for(std::size_t j=0;j<nbOfCompo;j++,data++)
95             {
96               oss2 << *data;
97               if(j!=nbOfCompo-1) oss2 << ", ";
98             }
99           oss2 << ")";
100         }
101       else
102         oss2 << *data++;
103       if(i!=nbOfTuples-1) oss2 << ", ";
104       std::string oss3Str(oss2.str());
105       if(oss3Str.length()<maxNbOfByteInRepr)
106         oss2Str=oss3Str;
107       else
108         isFinished=false;
109     }
110   stream << oss2Str;
111   if(!isFinished)
112     stream << "... ";
113   stream << "]";
114 }
115
116 bool DataArrayFloat::isEqualIfNotWhy(const DataArrayFloat& other, float prec, std::string& reason) const
117 {
118   if(!areInfoEqualsIfNotWhy(other,reason))
119     return false;
120   return _mem.isEqual(other._mem,prec,reason);
121 }
122
123 bool DataArrayFloat::isEqual(const DataArrayFloat& other, float prec) const
124 {
125   std::string tmp;
126   return isEqualIfNotWhy(other,prec,tmp);
127 }
128
129 bool DataArrayFloat::isEqualWithoutConsideringStr(const DataArrayFloat& other, float prec) const
130 {
131   std::string tmp;
132   return _mem.isEqual(other._mem,prec,tmp);
133 }
134
135 DataArrayFloatIterator *DataArrayFloat::iterator()
136 {
137   return new DataArrayFloatIterator(this);
138 }
139
140 DataArrayFloatIterator::DataArrayFloatIterator(DataArrayFloat *da):DataArrayIterator<float>(da)
141 {
142 }
143
144 DataArrayFloatTuple::DataArrayFloatTuple(float *pt, std::size_t nbOfComp):DataArrayTuple<float>(pt,nbOfComp)
145 {
146 }
147
148 std::string DataArrayFloatTuple::repr() const
149 {
150   std::ostringstream oss; oss.precision(7); oss << "(";
151   for(std::size_t i=0;i<_nb_of_compo-1;i++)
152     oss << _pt[i] << ", ";
153   oss << _pt[_nb_of_compo-1] << ")";
154   return oss.str();
155 }
156
157 float DataArrayFloatTuple::floatValue() const
158 {
159   return this->zeValue();
160 }
161
162 /*!
163  * This method returns a newly allocated instance the caller should dealed with by a MEDCoupling::DataArrayFloat::decrRef.
164  * This method performs \b no copy of data. The content is only referenced using MEDCoupling::DataArrayFloat::useArray with ownership set to \b false.
165  * 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
166  * \b nbOfCompo=1 and \bnbOfTuples==this->_nb_of_elem.
167  */
168 DataArrayFloat *DataArrayFloatTuple::buildDAFloat(std::size_t nbOfTuples, std::size_t nbOfCompo) const
169 {
170   return this->buildDA(nbOfTuples,nbOfCompo);
171 }