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