Salome HOME
Merge branch 'master' of ssh://git.salome-platform.org/tools/medcoupling
[tools/medcoupling.git] / src / MEDCoupling / MEDCouplingMemArray.txx
index 16ba939e2a67f29bdfcb351510df170d7936a55a..5df4f7150b71637dd40bcf3af923036533ed8d9b 100644 (file)
@@ -25,6 +25,8 @@
 #include "NormalizedUnstructuredMesh.hxx"
 #include "InterpKernelException.hxx"
 #include "InterpolationUtils.hxx"
+#include "MEDCouplingPartDefinition.hxx"
+#include "InterpKernelAutoPtr.hxx"
 #include "MCAuto.hxx"
 
 #include <sstream>
@@ -95,22 +97,6 @@ namespace MEDCoupling
     _nb_of_elem=std::max<std::size_t>(_nb_of_elem,id+sizeOfOthers+1);
   }
 
-  template<class T>
-  template<class InputIterator>
-  void MemArray<T>::insertAtTheEnd(InputIterator first, InputIterator last)
-  {
-    T *pointer=_pointer.getPointer();
-    while(first!=last)
-      {
-        if(_nb_of_elem>=_nb_of_elem_alloc)
-          {
-            reserve(_nb_of_elem_alloc>0?2*_nb_of_elem_alloc:1);
-            pointer=_pointer.getPointer();
-          }
-        pointer[_nb_of_elem++]=*first++;
-      }
-  }
-
   template<class T>
   void MemArray<T>::pushBack(T elem)
   {
@@ -493,6 +479,41 @@ namespace MEDCoupling
   }
 
   //////////////////////////////////
+
+  template<class T>
+  MCAuto< typename Traits<T>::ArrayTypeCh > DataArrayTemplate<T>::NewFromStdVector(const typename std::vector<T>& v)
+  {
+    std::size_t sz(v.size());
+    MCAuto< typename Traits<T>::ArrayTypeCh > ret(Traits<T>::ArrayTypeCh::New());
+    ret->alloc(sz,1);
+    T *pt(ret->getPointer());
+    std::copy(v.begin(),v.end(),pt);
+    return ret;
+  }
+  
+  template<class T>
+  std::vector< MCAuto< typename Traits<T>::ArrayTypeCh > > DataArrayTemplate<T>::explodeComponents() const
+  {
+    checkAllocated();
+    std::size_t sz(getNumberOfComponents());
+    int nbTuples(getNumberOfTuples());
+    std::string name(getName());
+    std::vector<std::string> compNames(getInfoOnComponents());
+    std::vector< MCAuto< typename Traits<T>::ArrayTypeCh > > ret(sz);
+    const T *thisPt(begin());
+    for(std::size_t i=0;i<sz;i++)
+      {
+        MCAuto< typename Traits<T>::ArrayTypeCh > part(Traits<T>::ArrayTypeCh::New());
+        part->alloc(nbTuples,1);
+        part->setName(name);
+        part->setInfoOnComponent(0,compNames[i]);
+        T *otherPt(part->getPointer());
+        for(int j=0;j<nbTuples;j++)
+          otherPt[j]=thisPt[sz*j+i];
+        ret[i]=part;
+      }
+    return ret;
+  }
   
   template<class T>
   std::size_t DataArrayTemplate<T>::getHeapMemorySizeWithoutChildren() const
@@ -1028,6 +1049,41 @@ namespace MEDCoupling
   {
     return DataArrayTemplate<T>::mySelectByTupleId(di.begin(),di.end());
   }
+
+  template<class T>
+  MCAuto<typename Traits<T>::ArrayTypeCh> DataArrayTemplate<T>::selectPartDef(const PartDefinition *pd) const
+  {
+    if(!pd)
+      throw INTERP_KERNEL::Exception("DataArrayTemplate<T>::selectPartDef : null input pointer !");
+    MCAuto<typename Traits<T>::ArrayTypeCh> ret(Traits<T>::ArrayTypeCh::New());
+    const SlicePartDefinition *spd(dynamic_cast<const SlicePartDefinition *>(pd));
+    if(spd)
+      {
+        int a,b,c;
+        spd->getSlice(a,b,c);
+        if(a==0 && b==getNumberOfTuples() && c==1)
+          {
+            DataArrayTemplate<T> *directRet(const_cast<DataArrayTemplate<T> *>(this));
+            directRet->incrRef();
+            MCAuto<DataArrayTemplate<T> > ret(directRet);
+            return DynamicCastSafe<DataArrayTemplate<T>,typename Traits<T>::ArrayTypeCh>(ret);
+          }
+        else
+          {
+            MCAuto<DataArray> ret(selectByTupleIdSafeSlice(a,b,c));
+            return DynamicCastSafe<DataArray,typename Traits<T>::ArrayTypeCh>(ret);
+          }
+      }
+    const DataArrayPartDefinition *dpd(dynamic_cast<const DataArrayPartDefinition *>(pd));
+    if(dpd)
+      {
+        MCAuto<DataArrayInt> arr(dpd->toDAI());
+        MCAuto<DataArray> ret(selectByTupleIdSafe(arr->begin(),arr->end()));
+        return DynamicCastSafe<DataArray,typename Traits<T>::ArrayTypeCh>(ret);
+        
+      }
+    throw INTERP_KERNEL::Exception("DataArrayTemplate<T>::selectPartDef : unrecognized part def !");
+  }
   
   /*!
    * Returns a shorten and permuted copy of \a this array. The new DataArrayDouble is
@@ -2124,6 +2180,78 @@ namespace MEDCoupling
     return *loc;
   }
   
+  template<class T>
+  void DataArrayTemplate<T>::circularPermutation(int nbOfShift)
+  {
+    checkAllocated();
+    int nbOfCompo(getNumberOfComponents()),nbTuples(getNumberOfTuples());
+    int effNbSh(EffectiveCircPerm(nbOfShift,nbTuples));
+    if(effNbSh==0)
+      return ;
+    T *work(getPointer());
+    if(effNbSh<nbTuples-effNbSh)
+      {
+        typename INTERP_KERNEL::AutoPtr<T> buf(new T[effNbSh*nbOfCompo]);
+        std::copy(work,work+effNbSh*nbOfCompo,(T *)buf);
+        std::copy(work+effNbSh*nbOfCompo,work+nbTuples*nbOfCompo,work);// ze big shift
+        std::copy((T *)buf,(T *)buf+effNbSh*nbOfCompo,work+(nbTuples-effNbSh)*nbOfCompo);
+      }
+    else
+      {
+        typename INTERP_KERNEL::AutoPtr<T> buf(new T[(nbTuples-effNbSh)*nbOfCompo]);
+        std::copy(work+effNbSh*nbOfCompo,work+nbTuples*nbOfCompo,(T *)buf);
+        std::copy(work,work+effNbSh*nbOfCompo,work+(nbTuples-effNbSh)*nbOfCompo);// ze big shift
+        std::copy((T*)buf,(T *)buf+(nbTuples-effNbSh)*nbOfCompo,work);
+      }
+  }
+  
+  template<class T>
+  void DataArrayTemplate<T>::circularPermutationPerTuple(int nbOfShift)
+  {
+    checkAllocated();
+    int nbOfCompo(getNumberOfComponents()),nbTuples(getNumberOfTuples());
+    int effNbSh(EffectiveCircPerm(nbOfShift,nbOfCompo));
+    if(effNbSh==0)
+      return ;
+    T *work(getPointer());
+    if(effNbSh<nbOfCompo-effNbSh)
+      {
+        typename INTERP_KERNEL::AutoPtr<T> buf(new T[effNbSh]);
+        for(int i=0;i<nbTuples;i++,work+=nbOfCompo)
+          {
+            std::copy(work,work+effNbSh,(T *)buf);
+            std::copy(work+effNbSh,work+nbOfCompo,work);// ze big shift
+            std::copy((T *)buf,(T *)buf+effNbSh,work+(nbOfCompo-effNbSh));
+          }
+      }
+    else
+      {
+        typename INTERP_KERNEL::AutoPtr<T> buf(new T[nbOfCompo-effNbSh]);
+        for(int i=0;i<nbTuples;i++,work+=nbOfCompo)
+          {
+            std::copy(work+effNbSh,work+nbOfCompo,(T *)buf);
+            std::copy(work,work+effNbSh,work+(nbOfCompo-effNbSh));// ze big shift
+            std::copy((T*)buf,(T *)buf+(nbOfCompo-effNbSh),work);
+          }
+      }
+    std::vector<std::string> sts(nbOfCompo);
+    for(int i=0;i<nbOfCompo;i++)
+      sts[i]=_info_on_compo[(i+effNbSh)%nbOfCompo];
+    setInfoOnComponents(sts);
+  }
+  
+  template<class T>
+  void DataArrayTemplate<T>::reversePerTuple()
+  {
+    checkAllocated();
+    int nbOfCompo(getNumberOfComponents()),nbTuples(getNumberOfTuples());
+    if(nbOfCompo<=1)
+      return ;
+    T *work(getPointer());
+    for(int i=0;i<nbTuples;i++,work+=nbOfCompo)
+      std::reverse(work,work+nbOfCompo);
+    std::reverse(_info_on_compo.begin(),_info_on_compo.end());
+  }
 }
 
 #endif