Salome HOME
[EDF27988] : Implementation of MEDCouplingUMesh.explodeMeshTo for MEDFileUMesh.reduce...
[tools/medcoupling.git] / src / MEDCoupling / MEDCouplingMemArray.cxx
index db968d6ad8a412ceda5882b3c70dc2101129b0a9..e84b23b07256e4ace10b0e8c876704be06defdb4 100755 (executable)
@@ -1,4 +1,4 @@
-// Copyright (C) 2007-2020  CEA/DEN, EDF R&D
+// Copyright (C) 2007-2023  CEA, EDF
 //
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
@@ -67,7 +67,7 @@ void MEDCoupling::DACheckNbOfTuplesAndComp(const DataArray *da, mcIdType nbOfTup
   da->checkNbOfTuplesAndComp(nbOfTuples,nbOfCompo,msg);
 }
 
-template<mcIdType SPACEDIM>
+template<int SPACEDIM>
 void DataArrayDouble::findCommonTuplesAlg(const double *bbox, mcIdType nbNodes, mcIdType limitNodeId, double prec, DataArrayIdType *c, DataArrayIdType *cI) const
 {
   const double *coordsPtr=getConstPointer();
@@ -100,7 +100,7 @@ void DataArrayDouble::findCommonTuplesAlg(const double *bbox, mcIdType nbNodes,
     }
 }
 
-template<mcIdType SPACEDIM>
+template<int SPACEDIM>
 void DataArrayDouble::FindTupleIdsNearTuplesAlg(const BBTreePts<SPACEDIM,mcIdType>& myTree, const double *pos, mcIdType nbOfTuples, double eps,
                                                 DataArrayIdType *c, DataArrayIdType *cI)
 {
@@ -116,7 +116,7 @@ void DataArrayDouble::FindTupleIdsNearTuplesAlg(const BBTreePts<SPACEDIM,mcIdTyp
     }
 }
 
-template<mcIdType SPACEDIM>
+template<int SPACEDIM>
 void DataArrayDouble::FindClosestTupleIdAlg(const BBTreePts<SPACEDIM,mcIdType>& myTree, double dist, const double *pos, mcIdType nbOfTuples, const double *thisPt, mcIdType thisNbOfTuples, mcIdType *res)
 {
   double distOpt = std::max(dist, std::numeric_limits<double>::epsilon());
@@ -423,6 +423,29 @@ std::string DataArray::getUnitOnComponent(std::size_t i) const
     }
 }
 
+/*!
+ * Split \a st string into chunks of sz characters each.
+ * Size of input \a st must a be a multiple of \a sz. If not an exception is thrown.
+ */
+std::vector<std::string> DataArray::SplitStringInChuncks(const std::string st, std::size_t sz)
+{
+  std::size_t len = st.length();
+  std::size_t nbOfCompo(len/sz);
+  if( nbOfCompo*sz != len)
+  {
+    THROW_IK_EXCEPTION("DataArray::SplitStringInChuncks : Length of input string (" << len << ") is not equal to " << nbOfCompo << "*" << sz << " !");
+  }
+  std::vector<std::string> ret(nbOfCompo);
+  for(std::size_t i = 0 ; i < nbOfCompo ; ++i)
+  {
+    std::string part = st.substr(i*sz,sz);
+    std::size_t p3=part.find_last_not_of(" \t");
+    part = part.substr(0,p3+1);
+    ret[i] = part;
+  }
+  return ret;
+}
+
 /*!
  * Returns the var part of the full component information.
  * For example, if \a info == "SIGXY [N/m^2]", then this method returns "SIGXY".
@@ -2314,16 +2337,7 @@ DataArrayDouble *DataArrayDouble::magnitude() const
   return ret;
 }
 
-/*!
- * Computes the maximal value within every tuple of \a this array.
- *  \return DataArrayDouble * - the new instance of DataArrayDouble containing the
- *          same number of tuples as \a this array and one component.
- *          The caller is to delete this result array using decrRef() as it is no more
- *          needed.
- *  \throw If \a this is not allocated.
- *  \sa DataArrayDouble::maxPerTupleWithCompoId
- */
-DataArrayDouble *DataArrayDouble::maxPerTuple() const
+DataArrayDouble *DataArrayDouble::operatePerTuple(std::function<double(const double *bg, const double *endd)> func) const
 {
   checkAllocated();
   std::size_t nbOfComp(getNumberOfComponents());
@@ -2333,10 +2347,38 @@ DataArrayDouble *DataArrayDouble::maxPerTuple() const
   const double *src=getConstPointer();
   double *dest=ret->getPointer();
   for(mcIdType i=0;i<nbOfTuple;i++,dest++,src+=nbOfComp)
-    *dest=*std::max_element(src,src+nbOfComp);
+    *dest=func(src,src+nbOfComp);
   return ret.retn();
 }
 
+/*!
+ * Computes the maximal value within every tuple of \a this array.
+ *  \return DataArrayDouble * - the new instance of DataArrayDouble containing the
+ *          same number of tuples as \a this array and one component.
+ *          The caller is to delete this result array using decrRef() as it is no more
+ *          needed.
+ *  \throw If \a this is not allocated.
+ *  \sa DataArrayDouble::maxPerTupleWithCompoId, DataArrayDouble::minPerTuple
+ */
+DataArrayDouble *DataArrayDouble::maxPerTuple() const
+{
+  return this->operatePerTuple([](const double *bg, const double *endd) { return *std::max_element(bg,endd); });
+}
+
+/*!
+ * Computes the minimal value within every tuple of \a this array.
+ *  \return DataArrayDouble * - the new instance of DataArrayDouble containing the
+ *          same number of tuples as \a this array and one component.
+ *          The caller is to delete this result array using decrRef() as it is no more
+ *          needed.
+ *  \throw If \a this is not allocated.
+ *  \sa DataArrayDouble::maxPerTuple
+ */
+DataArrayDouble *DataArrayDouble::minPerTuple() const
+{
+  return this->operatePerTuple([](const double *bg, const double *endd) { return *std::min_element(bg,endd); });
+}
+
 /*!
  * Computes the maximal value within every tuple of \a this array and it returns the first component
  * id for each tuple that corresponds to the maximal value within the tuple.
@@ -3625,6 +3667,17 @@ DataArrayInt32 *DataArrayInt32::deepCopy() const
   return new DataArrayInt32(*this);
 }
 
+MCAuto<DataArrayInt64> DataArrayInt32::convertToInt64Arr() const
+{
+  this->checkAllocated();
+  MCAuto<DataArrayInt64> ret(DataArrayInt64::New());
+  ret->alloc(this->getNumberOfTuples(),this->getNumberOfComponents());
+  ret->copyStringInfoFrom(*this);
+  const std::int32_t *pt(this->begin());
+  std::for_each(ret->getPointer(),ret->getPointer()+ret->getNbOfElems(),[&pt](std::int64_t& val) { val = std::int64_t(*pt++); });
+  return ret;
+}
+
 DataArrayInt32Iterator *DataArrayInt32::iterator()
 {
   return new DataArrayInt32Iterator(this);
@@ -3664,6 +3717,17 @@ DataArrayInt32 *DataArrayInt32Tuple::buildDAInt(std::size_t nbOfTuples, std::siz
   return this->buildDA(nbOfTuples,nbOfCompo);
 }
 
+MCAuto<DataArrayInt32> DataArrayInt64::convertToInt32Arr() const
+{
+  this->checkAllocated();
+  MCAuto<DataArrayInt32> ret(DataArrayInt32::New());
+  ret->alloc(this->getNumberOfTuples(),this->getNumberOfComponents());
+  ret->copyStringInfoFrom(*this);
+  const std::int64_t *pt(this->begin());
+  std::for_each(ret->getPointer(),ret->getPointer()+ret->getNbOfElems(),[&pt](std::int32_t& val) { val = std::int32_t(*pt++); });
+  return ret;
+}
+
 DataArrayInt64Iterator *DataArrayInt64::iterator()
 {
   return new DataArrayInt64Iterator(this);