]> SALOME platform Git repositories - tools/medcoupling.git/commitdiff
Salome HOME
Some code factorization.
authorageay <ageay>
Mon, 27 Sep 2010 06:27:49 +0000 (06:27 +0000)
committerageay <ageay>
Mon, 27 Sep 2010 06:27:49 +0000 (06:27 +0000)
src/MEDCoupling/MEDCouplingMemArray.cxx
src/MEDCoupling/MEDCouplingMemArray.hxx
src/MEDCoupling/MEDCouplingTimeDiscretization.cxx
src/MEDCoupling/MEDCouplingUMesh.cxx

index c778fd8c96f4f5d24396381cc2bea0f018a343f6..65d3d9ee3f228dfc5db0b45d9c14003454718b55 100644 (file)
 
 #include "MEDCouplingMemArray.txx"
 
+#include "InterpKernelExprParser.hxx"
+
 #include <set>
 #include <numeric>
 #include <functional>
 
+typedef double (*MYFUNCPTR)(double);
+
 using namespace ParaMEDMEM;
 
 void DataArray::setName(const char *name)
@@ -326,6 +330,140 @@ double DataArrayDouble::accumulate(int compId) const
   return ret;
 }
 
+void DataArrayDouble::applyLin(double a, double b, int compoId)
+{
+  double *ptr=getPointer()+compoId;
+  int nbOfComp=getNumberOfComponents();
+  int nbOfTuple=getNumberOfTuples();
+  for(int i=0;i<nbOfTuple;i++,ptr+=nbOfComp)
+    *ptr=a*(*ptr)+b;
+  declareAsNew();
+}
+
+DataArrayDouble *DataArrayDouble::applyFunc(int nbOfComp, FunctionToEvaluate func) const throw(INTERP_KERNEL::Exception)
+{
+  DataArrayDouble *newArr=DataArrayDouble::New();
+  int nbOfTuples=getNumberOfTuples();
+  int oldNbOfComp=getNumberOfComponents();
+  newArr->alloc(nbOfTuples,nbOfComp);
+  const double *ptr=getConstPointer();
+  double *ptrToFill=newArr->getPointer();
+  for(int i=0;i<nbOfTuples;i++)
+    {
+      if(!func(ptr+i*oldNbOfComp,ptrToFill+i*nbOfComp))
+        {
+          std::ostringstream oss; oss << "For tuple # " << i << " with value (";
+          std::copy(ptr+oldNbOfComp*i,ptr+oldNbOfComp*(i+1),std::ostream_iterator<double>(oss,", "));
+          oss << ") : Evaluation of function failed !";
+          newArr->decrRef();
+          throw INTERP_KERNEL::Exception(oss.str().c_str());
+        }
+    }
+  return newArr;
+}
+
+DataArrayDouble *DataArrayDouble::applyFunc(int nbOfComp, const char *func) const throw(INTERP_KERNEL::Exception)
+{
+  INTERP_KERNEL::ExprParser expr(func);
+  expr.parse();
+  std::set<std::string> vars;
+  expr.getTrueSetOfVars(vars);
+  int oldNbOfComp=getNumberOfComponents();
+  if((int)vars.size()>oldNbOfComp)
+    {
+      std::ostringstream oss; oss << "The field has a " << oldNbOfComp << " components and there are ";
+      oss << vars.size() << " variables : ";
+      std::copy(vars.begin(),vars.end(),std::ostream_iterator<std::string>(oss," "));
+      throw INTERP_KERNEL::Exception(oss.str().c_str());
+    }
+  std::vector<std::string> varsV(vars.begin(),vars.end());
+  expr.prepareExprEvaluation(varsV);
+  //
+  DataArrayDouble *newArr=DataArrayDouble::New();
+  int nbOfTuples=getNumberOfTuples();
+  newArr->alloc(nbOfTuples,nbOfComp);
+  const double *ptr=getConstPointer();
+  double *ptrToFill=newArr->getPointer();
+  for(int i=0;i<nbOfTuples;i++)
+    {
+      try
+        {
+          expr.evaluateExpr(nbOfComp,ptr+i*oldNbOfComp,ptrToFill+i*nbOfComp);
+        }
+      catch(INTERP_KERNEL::Exception& e)
+        {
+          std::ostringstream oss; oss << "For tuple # " << i << " with value (";
+          std::copy(ptr+oldNbOfComp*i,ptr+oldNbOfComp*(i+1),std::ostream_iterator<double>(oss,", "));
+          oss << ") : Evaluation of function failed !" << e.what();
+          newArr->decrRef();
+          throw INTERP_KERNEL::Exception(oss.str().c_str());
+        }
+    }
+  return newArr;
+}
+
+DataArrayDouble *DataArrayDouble::applyFunc(const char *func) const throw(INTERP_KERNEL::Exception)
+{
+  INTERP_KERNEL::ExprParser expr(func);
+  expr.parse();
+  expr.prepareExprEvaluationVec();
+  //
+  DataArrayDouble *newArr=DataArrayDouble::New();
+  int nbOfTuples=getNumberOfTuples();
+  int nbOfComp=getNumberOfComponents();
+  newArr->alloc(nbOfTuples,nbOfComp);
+  const double *ptr=getConstPointer();
+  double *ptrToFill=newArr->getPointer();
+  for(int i=0;i<nbOfTuples;i++)
+    {
+      try
+        {
+          expr.evaluateExpr(nbOfComp,ptr+i*nbOfComp,ptrToFill+i*nbOfComp);
+        }
+      catch(INTERP_KERNEL::Exception& e)
+        {
+          std::ostringstream oss; oss << "For tuple # " << i << " with value (";
+          std::copy(ptr+nbOfComp*i,ptr+nbOfComp*(i+1),std::ostream_iterator<double>(oss,", "));
+          oss << ") : Evaluation of function failed ! " << e.what();
+          newArr->decrRef();
+          throw INTERP_KERNEL::Exception(oss.str().c_str());
+        }
+    }
+  return newArr;
+}
+
+void DataArrayDouble::applyFuncFast32(const char *func)
+{
+  INTERP_KERNEL::ExprParser expr(func);
+  expr.parse();
+  char *funcStr=expr.compileX86();
+  MYFUNCPTR funcPtr=(MYFUNCPTR)funcStr;//he he...
+  //
+  double *ptr=getPointer();
+  int nbOfComp=getNumberOfComponents();
+  int nbOfTuples=getNumberOfTuples();
+  int nbOfElems=nbOfTuples*nbOfComp;
+  for(int i=0;i<nbOfElems;i++,ptr++)
+    *ptr=funcPtr(*ptr);
+  declareAsNew();
+}
+
+void DataArrayDouble::applyFuncFast64(const char *func)
+{
+  INTERP_KERNEL::ExprParser expr(func);
+  expr.parse();
+  char *funcStr=expr.compileX86_64();
+  MYFUNCPTR funcPtr=(MYFUNCPTR)funcStr;//he he...
+  //
+  double *ptr=getPointer();
+  int nbOfComp=getNumberOfComponents();
+  int nbOfTuples=getNumberOfTuples();
+  int nbOfElems=nbOfTuples*nbOfComp;
+  for(int i=0;i<nbOfElems;i++,ptr++)
+    *ptr=funcPtr(*ptr);
+  declareAsNew();
+}
+
 DataArrayInt *DataArrayDouble::getIdsInRange(double vmin, double vmax) const throw(INTERP_KERNEL::Exception)
 {
   if(getNumberOfComponents()!=1)
index 0405f3da814796c5cbe1ba052fdb35b3c367a9d3..9e71e2db5975e2c57f3eda0a85414bb1c985ee56 100644 (file)
@@ -143,6 +143,12 @@ namespace ParaMEDMEM
     MEDCOUPLING_EXPORT double getAverageValue() const throw(INTERP_KERNEL::Exception);
     MEDCOUPLING_EXPORT void accumulate(double *res) const;
     MEDCOUPLING_EXPORT double accumulate(int compId) const;
+    MEDCOUPLING_EXPORT void applyLin(double a, double b, int compoId);
+    MEDCOUPLING_EXPORT DataArrayDouble *applyFunc(int nbOfComp, FunctionToEvaluate func) const throw(INTERP_KERNEL::Exception);
+    MEDCOUPLING_EXPORT DataArrayDouble *applyFunc(int nbOfComp, const char *func) const throw(INTERP_KERNEL::Exception);
+    MEDCOUPLING_EXPORT DataArrayDouble *applyFunc(const char *func) const throw(INTERP_KERNEL::Exception);
+    MEDCOUPLING_EXPORT void applyFuncFast32(const char *func);
+    MEDCOUPLING_EXPORT void applyFuncFast64(const char *func);
     MEDCOUPLING_EXPORT DataArrayInt *getIdsInRange(double vmin, double vmax) const throw(INTERP_KERNEL::Exception);
     MEDCOUPLING_EXPORT static DataArrayDouble *aggregate(const DataArrayDouble *a1, const DataArrayDouble *a2) throw(INTERP_KERNEL::Exception);
     MEDCOUPLING_EXPORT static DataArrayDouble *dot(const DataArrayDouble *a1, const DataArrayDouble *a2) throw(INTERP_KERNEL::Exception);
index 1258dd071098db8f2c3568cbd85ef199516fd30d..aa2f1ed9c6ace86664527766dac250bbc02e3590 100644 (file)
 #include "MEDCouplingTimeDiscretization.hxx"
 #include "MEDCouplingMemArray.hxx"
 
-#include "InterpKernelExprParser.hxx"
-
 #include <cmath>
 #include <iterator>
 
-typedef double (*MYFUNCPTR)(double);
-
 using namespace ParaMEDMEM;
 
 const double MEDCouplingTimeDiscretization::TIME_TOLERANCE_DFT=1.e-12;
@@ -284,14 +280,7 @@ void MEDCouplingTimeDiscretization::applyLin(double a, double b, int compoId)
   for(int j=0;j<(int)arrays.size();j++)
     {
       if(arrays[j])
-        {
-          double *ptr=arrays[j]->getPointer()+compoId;
-          int nbOfComp=arrays[j]->getNumberOfComponents();
-          int nbOfTuple=arrays[j]->getNumberOfTuples();
-          for(int i=0;i<nbOfTuple;i++,ptr+=nbOfComp)
-            *ptr=a*(*ptr)+b;
-          arrays[j]->declareAsNew();
-        }
+        arrays[j]->applyLin(a,b,compoId);
     }
 }
 
@@ -303,26 +292,7 @@ void MEDCouplingTimeDiscretization::applyFunc(int nbOfComp, FunctionToEvaluate f
   for(int j=0;j<(int)arrays.size();j++)
     {
       if(arrays[j])
-        {
-          DataArrayDouble *newArr=DataArrayDouble::New();
-          int nbOfTuples=arrays[j]->getNumberOfTuples();
-          int oldNbOfComp=arrays[j]->getNumberOfComponents();
-          newArr->alloc(nbOfTuples,nbOfComp);
-          const double *ptr=arrays[j]->getConstPointer();
-          double *ptrToFill=newArr->getPointer();
-          for(int i=0;i<nbOfTuples;i++)
-            {
-              if(!func(ptr+i*oldNbOfComp,ptrToFill+i*nbOfComp))
-                {
-                  std::ostringstream oss; oss << "For tuple # " << i << " with value (";
-                  std::copy(ptr+oldNbOfComp*i,ptr+oldNbOfComp*(i+1),std::ostream_iterator<double>(oss,", "));
-                  oss << ") : Evaluation of function failed !";
-                  newArr->decrRef();
-                  throw INTERP_KERNEL::Exception(oss.str().c_str());
-                }
-            }
-          arrays2[j]=newArr;
-        }
+        arrays2[j]=arrays[j]->applyFunc(nbOfComp,func);
       else
         arrays2[j]=0;
     }
@@ -334,51 +304,13 @@ void MEDCouplingTimeDiscretization::applyFunc(int nbOfComp, FunctionToEvaluate f
 
 void MEDCouplingTimeDiscretization::applyFunc(int nbOfComp, const char *func)
 {
-  INTERP_KERNEL::ExprParser expr(func);
-  expr.parse();
-  std::set<std::string> vars;
-  expr.getTrueSetOfVars(vars);
-  //
-  std::vector<DataArrayDouble *> arrays;
+ std::vector<DataArrayDouble *> arrays;
   getArrays(arrays);
   std::vector<DataArrayDouble *> arrays2(arrays.size());
   for(int j=0;j<(int)arrays.size();j++)
     {
       if(arrays[j])
-        {
-          int oldNbOfComp=arrays[j]->getNumberOfComponents();
-          if((int)vars.size()>oldNbOfComp)
-            {
-              std::ostringstream oss; oss << "The field has a " << oldNbOfComp << " components and there are ";
-              oss << vars.size() << " variables : ";
-              std::copy(vars.begin(),vars.end(),std::ostream_iterator<std::string>(oss," "));
-              throw INTERP_KERNEL::Exception(oss.str().c_str());
-            }
-          std::vector<std::string> varsV(vars.begin(),vars.end());
-          expr.prepareExprEvaluation(varsV);
-          //
-          DataArrayDouble *newArr=DataArrayDouble::New();
-          int nbOfTuples=arrays[j]->getNumberOfTuples();
-          newArr->alloc(nbOfTuples,nbOfComp);
-          const double *ptr=arrays[j]->getConstPointer();
-          double *ptrToFill=newArr->getPointer();
-          for(int i=0;i<nbOfTuples;i++)
-            {
-              try
-                {
-                  expr.evaluateExpr(nbOfComp,ptr+i*oldNbOfComp,ptrToFill+i*nbOfComp);
-                }
-              catch(INTERP_KERNEL::Exception& e)
-                {
-                  std::ostringstream oss; oss << "For tuple # " << i << " with value (";
-                  std::copy(ptr+oldNbOfComp*i,ptr+oldNbOfComp*(i+1),std::ostream_iterator<double>(oss,", "));
-                  oss << ") : Evaluation of function failed !" << e.what();
-                  newArr->decrRef();
-                  throw INTERP_KERNEL::Exception(oss.str().c_str());
-                }
-            }
-          arrays2[j]=newArr;
-        }
+        arrays2[j]=arrays[j]->applyFunc(nbOfComp,func);
       else
         arrays2[j]=0;
     }
@@ -390,40 +322,13 @@ void MEDCouplingTimeDiscretization::applyFunc(int nbOfComp, const char *func)
 
 void MEDCouplingTimeDiscretization::applyFunc(const char *func)
 {
-  INTERP_KERNEL::ExprParser expr(func);
-  expr.parse();
-  expr.prepareExprEvaluationVec();
-  //
   std::vector<DataArrayDouble *> arrays;
   getArrays(arrays);
   std::vector<DataArrayDouble *> arrays2(arrays.size());
   for(int j=0;j<(int)arrays.size();j++)
     {
       if(arrays[j])
-        {
-          DataArrayDouble *newArr=DataArrayDouble::New();
-          int nbOfTuples=arrays[j]->getNumberOfTuples();
-          int nbOfComp=arrays[j]->getNumberOfComponents();
-          newArr->alloc(nbOfTuples,nbOfComp);
-          const double *ptr=arrays[j]->getConstPointer();
-          double *ptrToFill=newArr->getPointer();
-          for(int i=0;i<nbOfTuples;i++)
-            {
-              try
-                {
-                  expr.evaluateExpr(nbOfComp,ptr+i*nbOfComp,ptrToFill+i*nbOfComp);
-                }
-              catch(INTERP_KERNEL::Exception& e)
-                {
-                  std::ostringstream oss; oss << "For tuple # " << i << " with value (";
-                  std::copy(ptr+nbOfComp*i,ptr+nbOfComp*(i+1),std::ostream_iterator<double>(oss,", "));
-                  oss << ") : Evaluation of function failed ! " << e.what();
-                  newArr->decrRef();
-                  throw INTERP_KERNEL::Exception(oss.str().c_str());
-                }
-            }
-          arrays2[j]=newArr;
-        }
+        arrays2[j]=arrays[j]->applyFunc(func);
       else
         arrays2[j]=0;
     }
@@ -435,47 +340,23 @@ void MEDCouplingTimeDiscretization::applyFunc(const char *func)
 
 void MEDCouplingTimeDiscretization::applyFuncFast32(const char *func)
 {
-  INTERP_KERNEL::ExprParser expr(func);
-  expr.parse();
-  char *funcStr=expr.compileX86();
-  MYFUNCPTR funcPtr=(MYFUNCPTR)funcStr;//he he...
   std::vector<DataArrayDouble *> arrays;
   getArrays(arrays);
   for(int j=0;j<(int)arrays.size();j++)
     {
       if(arrays[j])
-        {
-          double *ptr=arrays[j]->getPointer();
-          int nbOfComp=arrays[j]->getNumberOfComponents();
-          int nbOfTuples=arrays[j]->getNumberOfTuples();
-          int nbOfElems=nbOfTuples*nbOfComp;
-          for(int i=0;i<nbOfElems;i++,ptr++)
-            *ptr=funcPtr(*ptr);
-          arrays[j]->declareAsNew();
-        }
+        arrays[j]->applyFuncFast32(func);
     }
 }
 
 void MEDCouplingTimeDiscretization::applyFuncFast64(const char *func)
 {
-  INTERP_KERNEL::ExprParser expr(func);
-  expr.parse();
-  char *funcStr=expr.compileX86_64();
-  MYFUNCPTR funcPtr=(MYFUNCPTR)funcStr;//he he...
   std::vector<DataArrayDouble *> arrays;
   getArrays(arrays);
   for(int j=0;j<(int)arrays.size();j++)
     {
       if(arrays[j])
-        {
-          double *ptr=arrays[j]->getPointer();
-          int nbOfComp=arrays[j]->getNumberOfComponents();
-          int nbOfTuples=arrays[j]->getNumberOfTuples();
-          int nbOfElems=nbOfTuples*nbOfComp;
-          for(int i=0;i<nbOfElems;i++,ptr++)
-            *ptr=funcPtr(*ptr);
-          arrays[j]->declareAsNew();
-        }
+        arrays[j]->applyFuncFast64(func);
     }
 }
 
index 32bec6151d410c9d2a64b017fd882cca036fbff6..e77d8bba4dbce29b2e7ce66d2e8ac0dd9a538916 100644 (file)
@@ -498,7 +498,6 @@ void MEDCouplingUMesh::convertToPolyTypes(const std::vector<int>& cellIdsToConve
 DataArrayInt *MEDCouplingUMesh::zipCoordsTraducer()
 {
   int nbOfNodes=getNumberOfNodes();
-  int spaceDim=getSpaceDimension();
   int *traducer=new int[nbOfNodes];
   std::fill(traducer,traducer+nbOfNodes,-1);
   int nbOfCells=getNumberOfCells();