Salome HOME
Small modif
[tools/medcoupling.git] / src / MEDCoupling / MEDCouplingPartDefinition.cxx
index 08c1d2282202d2ab65565f94dcb3bdbc35042f7c..9652e8ddd6503210a77bc1d57a3ba0ad4c07022f 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2007-2014  CEA/DEN, EDF R&D
+// Copyright (C) 2007-2016  CEA/DEN, EDF R&D
 //
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
@@ -20,7 +20,9 @@
 
 #include "MEDCouplingPartDefinition.hxx"
 
-using namespace ParaMEDMEM;
+#include <sstream>
+
+using namespace MEDCoupling;
 
 PartDefinition *PartDefinition::New(int start, int stop, int step)
 {
@@ -32,6 +34,24 @@ PartDefinition *PartDefinition::New(DataArrayInt *listOfIds)
   return DataArrayPartDefinition::New(listOfIds);
 }
 
+PartDefinition *PartDefinition::Unserialize(std::vector<int>& tinyInt, std::vector< MCAuto<DataArrayInt> >& bigArraysI)
+{
+  if(tinyInt.empty())
+    {
+      MCAuto<PartDefinition> ret(DataArrayPartDefinition::New(bigArraysI.back()));
+      bigArraysI.pop_back();
+      return ret.retn();
+    }
+  else if(tinyInt.size()==3)
+    {
+      MCAuto<PartDefinition> ret(SlicePartDefinition::New(tinyInt[0],tinyInt[1],tinyInt[2]));
+      tinyInt.erase(tinyInt.begin(),tinyInt.begin()+3);
+      return ret.retn();
+    }
+  else
+    throw INTERP_KERNEL::Exception("PartDefinition::Unserialize");
+}
+
 PartDefinition::~PartDefinition()
 {
 }
@@ -41,6 +61,45 @@ DataArrayPartDefinition *DataArrayPartDefinition::New(DataArrayInt *listOfIds)
   return new DataArrayPartDefinition(listOfIds);
 }
 
+bool DataArrayPartDefinition::isEqual(const PartDefinition *other, std::string& what) const
+{
+  if(!other)
+    {
+      what="DataArrayPartDefinition::isEqual : other is null, this is not null !";
+      return false;
+    }
+  const DataArrayPartDefinition *otherC(dynamic_cast<const DataArrayPartDefinition *>(other));
+  if(!otherC)
+    {
+      what="DataArrayPartDefinition::isEqual : other is not DataArrayPartDefinition !";
+      return false;
+    }
+  const DataArrayInt *arr0(_arr),*arr1(otherC->_arr);
+  if(!arr0 && !arr1)
+    return true;
+  if((arr0 && !arr1) || (!arr0 && arr1))
+    {
+      what="DataArrayPartDefinition::isEqual : array is not defined both in other and this !";
+      return false;
+    }
+  std::string what1;
+  bool ret(arr0->isEqualIfNotWhy(*arr1,what1));
+  if(!ret)
+    {
+      what=std::string("DataArrayPartDefinition::isEqual : arrays are not equal :\n")+what1;
+      return false;
+    }
+  return true;
+}
+
+DataArrayPartDefinition *DataArrayPartDefinition::deepCopy() const
+{
+  const DataArrayInt *arr(_arr);
+  if(!arr)
+    throw INTERP_KERNEL::Exception("DataArrayPartDefinition::deepCopy : array is null !");
+  return DataArrayPartDefinition::New(const_cast<DataArrayInt *>(arr));
+}
+
 int DataArrayPartDefinition::getNumberOfElems() const
 {
   checkInternalArrayOK();
@@ -72,6 +131,66 @@ std::string DataArrayPartDefinition::getRepr() const
   return oss.str();
 }
 
+/*!
+ * This method operates FoG where F is \a this and G is \a other.
+ * Example : if \a other is SlicePart(4,14,1) and if \a this is DataArrayPartDefinition([0,1,2,3,6,7,8,9]) -> DataArrayPartDefinition([4,5,6,7,11,12,13]) will be returned
+ */
+PartDefinition *DataArrayPartDefinition::composeWith(const PartDefinition *other) const
+{
+  if(!other)
+    throw INTERP_KERNEL::Exception("DataArrayPartDefinition::composeWith : input PartDef must be not NULL !");
+  checkConsistencyLight();
+  other->checkConsistencyLight();
+  const SlicePartDefinition *spd(dynamic_cast<const SlicePartDefinition *>(other));
+  if(spd)
+    {//special case for optim
+      int a(0),b(0),c(0);
+      spd->getSlice(a,b,c);
+      if(c==1)
+        {
+          MCAuto<DataArrayInt> arr(DataArrayInt::New());
+          arr->alloc(_arr->getNumberOfTuples(),1);
+          std::transform(_arr->begin(),_arr->end(),arr->getPointer(),std::bind2nd(std::plus<int>(),a));
+          return DataArrayPartDefinition::New(arr);
+        }
+    }
+  //
+  MCAuto<DataArrayInt> arr1(other->toDAI());
+  MCAuto<DataArrayInt> arr2(arr1->selectByTupleIdSafe(_arr->begin(),_arr->end()));
+  return DataArrayPartDefinition::New(arr2);
+}
+
+void DataArrayPartDefinition::checkConsistencyLight() const
+{
+  CheckInternalArrayOK(_arr);
+}
+
+/*!
+ * This method tries to simplify \a this if possible.
+ * 
+ * \return a new reference (equal to this) to be decrRefed.
+ */
+PartDefinition *DataArrayPartDefinition::tryToSimplify() const
+{
+  checkConsistencyLight();
+  int a(0),b(0),c(0);
+  if(_arr->isRange(a,b,c))
+    {
+      return SlicePartDefinition::New(a,b,c);
+    }
+  else
+    {
+      PartDefinition *ret(const_cast<DataArrayPartDefinition *>(this));
+      ret->incrRef();
+      return ret;
+    }
+}
+
+void DataArrayPartDefinition::serialize(std::vector<int>& tinyInt, std::vector< MCAuto<DataArrayInt> >& bigArraysI) const
+{
+  bigArraysI.push_back(_arr);
+}
+
 DataArrayInt *DataArrayPartDefinition::toDAI() const
 {
   checkInternalArrayOK();
@@ -118,16 +237,16 @@ std::vector<const BigMemoryObject *> DataArrayPartDefinition::getDirectChildrenW
 
 DataArrayPartDefinition *DataArrayPartDefinition::add1(const DataArrayPartDefinition *other) const
 {
-  MEDCouplingAutoRefCountObjectPtr<DataArrayInt> a1(toDAI()),a2(other->toDAI());
-  MEDCouplingAutoRefCountObjectPtr<DataArrayInt> a3(DataArrayInt::Aggregate(a1,a2,0));
+  MCAuto<DataArrayInt> a1(toDAI()),a2(other->toDAI());
+  MCAuto<DataArrayInt> a3(DataArrayInt::Aggregate(a1,a2,0));
   a3->sort();
   return DataArrayPartDefinition::New(a3);
 }
 
 DataArrayPartDefinition *DataArrayPartDefinition::add2(const SlicePartDefinition *other) const
 {
-  MEDCouplingAutoRefCountObjectPtr<DataArrayInt> a1(toDAI()),a2(other->toDAI());
-  MEDCouplingAutoRefCountObjectPtr<DataArrayInt> a3(DataArrayInt::Aggregate(a1,a2,0));
+  MCAuto<DataArrayInt> a1(toDAI()),a2(other->toDAI());
+  MCAuto<DataArrayInt> a3(DataArrayInt::Aggregate(a1,a2,0));
   a3->sort();
   return DataArrayPartDefinition::New(a3);
 }
@@ -141,6 +260,33 @@ SlicePartDefinition *SlicePartDefinition::New(int start, int stop, int step)
   return new SlicePartDefinition(start,stop,step);
 }
 
+bool SlicePartDefinition::isEqual(const PartDefinition *other, std::string& what) const
+{
+  if(!other)
+    {
+      what="SlicePartDefinition::isEqual : other is null, this is not null !";
+      return false;
+    }
+  const SlicePartDefinition *otherC(dynamic_cast<const SlicePartDefinition *>(other));
+  if(!otherC)
+    {
+      what="SlicePartDefinition::isEqual : other is not SlicePartDefinition !";
+      return false;
+    }
+  bool ret((_start==otherC->_start) && (_stop==otherC->_stop) && (_step==otherC->_step));
+  if(!ret)
+    {
+      what="SlicePartDefinition::isEqual : values are not the same !";
+      return false;
+    }
+  return true;
+}
+
+SlicePartDefinition *SlicePartDefinition::deepCopy() const
+{
+  return SlicePartDefinition::New(_start,_stop,_step);
+}
+
 DataArrayInt *SlicePartDefinition::toDAI() const
 {
   return DataArrayInt::Range(_start,_stop,_step);
@@ -165,6 +311,47 @@ PartDefinition *SlicePartDefinition::operator+(const PartDefinition& other) cons
   throw INTERP_KERNEL::Exception("SlicePartDefinition::operator+ : unrecognized type in input !");
 }
 
+/*!
+ * This method operates FoG where F is \a this and G is \a other.
+ * Example : if \a this is SlicePart(4,6,1) and if \a other is DataArrayPartDefinition([12,13,17,18,22,28,34,44]) -> DataArrayPartDefinition([22,28]) will be returned
+ */
+PartDefinition *SlicePartDefinition::composeWith(const PartDefinition *other) const
+{
+  if(!other)
+    throw INTERP_KERNEL::Exception("SlicePartDefinition::composeWith : input PartDef must be not NULL !");
+  checkConsistencyLight();
+  other->checkConsistencyLight();
+  MCAuto<DataArrayInt> arr(other->toDAI());
+  MCAuto<DataArrayInt> arr1(arr->selectByTupleIdSafeSlice(_start,_stop,_step));
+  return DataArrayPartDefinition::New(arr1);
+}
+
+/*!
+ * Do nothing it is not a bug.
+ */
+void SlicePartDefinition::checkConsistencyLight() const
+{
+}
+
+/*!
+ * Return \a this (because it cannot be simplified)
+ * 
+ * \return a new reference (equal to this) to be decrRefed.
+ */
+PartDefinition *SlicePartDefinition::tryToSimplify() const
+{
+  PartDefinition *ret(const_cast<SlicePartDefinition *>(this));
+  ret->incrRef();
+  return ret;
+}
+
+void SlicePartDefinition::serialize(std::vector<int>& tinyInt, std::vector< MCAuto<DataArrayInt> >& bigArraysI) const
+{
+  tinyInt.push_back(_start);
+  tinyInt.push_back(_stop);
+  tinyInt.push_back(_step);
+}
+
 std::string SlicePartDefinition::getRepr() const
 {
   std::ostringstream oss;
@@ -178,6 +365,13 @@ int SlicePartDefinition::getEffectiveStop() const
   return _start+nbElems*_step;
 }
 
+void SlicePartDefinition::getSlice(int& start, int& stop, int& step) const
+{
+  start=_start;
+  stop=_stop;
+  step=_step;
+}
+
 SlicePartDefinition::SlicePartDefinition(int start, int stop, int step):_start(start),_stop(stop),_step(step)
 {
 }
@@ -201,8 +395,8 @@ std::vector<const BigMemoryObject *> SlicePartDefinition::getDirectChildrenWithN
 
 DataArrayPartDefinition *SlicePartDefinition::add1(const DataArrayPartDefinition *other) const
 {
-  MEDCouplingAutoRefCountObjectPtr<DataArrayInt> a1(toDAI()),a2(other->toDAI());
-  MEDCouplingAutoRefCountObjectPtr<DataArrayInt> a3(DataArrayInt::Aggregate(a1,a2,0));
+  MCAuto<DataArrayInt> a1(toDAI()),a2(other->toDAI());
+  MCAuto<DataArrayInt> a3(DataArrayInt::Aggregate(a1,a2,0));
   a3->sort();
   return DataArrayPartDefinition::New(a3);
 }
@@ -215,8 +409,8 @@ PartDefinition *SlicePartDefinition::add2(const SlicePartDefinition *other) cons
     }
   else
     {
-      MEDCouplingAutoRefCountObjectPtr<DataArrayInt> a1(toDAI()),a2(other->toDAI());
-      MEDCouplingAutoRefCountObjectPtr<DataArrayInt> a3(DataArrayInt::Aggregate(a1,a2,0));
+      MCAuto<DataArrayInt> a1(toDAI()),a2(other->toDAI());
+      MCAuto<DataArrayInt> a3(DataArrayInt::Aggregate(a1,a2,0));
       a3->sort();
       return DataArrayPartDefinition::New(a3);
     }