Salome HOME
Fix SIGSEGV on string representation of empty MEDCoupling1[S,D]GTUMesh
[tools/medcoupling.git] / src / MEDCoupling / MEDCouplingPartDefinition.cxx
index 4af214cc01f98000e446f219cf00504c8ac63c4c..55c3422d7ce47ece771391c64b0ccae69a528df5 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2007-2014  CEA/DEN, EDF R&D
+// Copyright (C) 2007-2020  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
 
 #include "MEDCouplingPartDefinition.hxx"
 
-using namespace ParaMEDMEM;
+#include <functional>
+#include <sstream>
 
-PartDefinition *PartDefinition::New(int start, int stop, int step)
+using namespace MEDCoupling;
+
+PartDefinition *PartDefinition::New(mcIdType start, mcIdType stop, mcIdType step)
 {
   return SlicePartDefinition::New(start,stop,step);
 }
 
-PartDefinition *PartDefinition::New(DataArrayInt *listOfIds)
+PartDefinition *PartDefinition::New(DataArrayIdType *listOfIds)
 {
   return DataArrayPartDefinition::New(listOfIds);
 }
 
+PartDefinition *PartDefinition::Unserialize(std::vector<mcIdType>& tinyInt, std::vector< MCAuto<DataArrayIdType> >& 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()
 {
 }
 
-DataArrayPartDefinition *DataArrayPartDefinition::New(DataArrayInt *listOfIds)
+DataArrayPartDefinition *DataArrayPartDefinition::New(DataArrayIdType *listOfIds)
 {
   return new DataArrayPartDefinition(listOfIds);
 }
 
-int DataArrayPartDefinition::getNumberOfElems() const
+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 DataArrayIdType *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 DataArrayIdType *arr(_arr);
+  if(!arr)
+    throw INTERP_KERNEL::Exception("DataArrayPartDefinition::deepCopy : array is null !");
+  return DataArrayPartDefinition::New(const_cast<DataArrayIdType *>(arr));
+}
+
+mcIdType DataArrayPartDefinition::getNumberOfElems() const
 {
   checkInternalArrayOK();
   return _arr->getNumberOfTuples();
@@ -64,7 +124,7 @@ PartDefinition *DataArrayPartDefinition::operator+(const PartDefinition& other)
 std::string DataArrayPartDefinition::getRepr() const
 {
   std::ostringstream oss; oss << "DataArray Part : ";
-  const DataArrayInt *arr(_arr);
+  const DataArrayIdType *arr(_arr);
   if(arr)
     arr->reprQuickOverview(oss);
   else
@@ -80,28 +140,28 @@ PartDefinition *DataArrayPartDefinition::composeWith(const PartDefinition *other
 {
   if(!other)
     throw INTERP_KERNEL::Exception("DataArrayPartDefinition::composeWith : input PartDef must be not NULL !");
-  checkCoherency();
-  other->checkCoherency();
+  checkConsistencyLight();
+  other->checkConsistencyLight();
   const SlicePartDefinition *spd(dynamic_cast<const SlicePartDefinition *>(other));
   if(spd)
     {//special case for optim
-      int a(0),b(0),c(0);
+      mcIdType a(0),b(0),c(0);
       spd->getSlice(a,b,c);
       if(c==1)
         {
-          MEDCouplingAutoRefCountObjectPtr<DataArrayInt> arr(DataArrayInt::New());
+          MCAuto<DataArrayIdType> arr(DataArrayIdType::New());
           arr->alloc(_arr->getNumberOfTuples(),1);
-          std::transform(_arr->begin(),_arr->end(),arr->getPointer(),std::bind2nd(std::plus<int>(),a));
+          std::transform(_arr->begin(),_arr->end(),arr->getPointer(),std::bind2nd(std::plus<mcIdType>(),a));
           return DataArrayPartDefinition::New(arr);
         }
     }
   //
-  MEDCouplingAutoRefCountObjectPtr<DataArrayInt> arr1(other->toDAI());
-  MEDCouplingAutoRefCountObjectPtr<DataArrayInt> arr2(arr1->selectByTupleIdSafe(_arr->begin(),_arr->end()));
+  MCAuto<DataArrayIdType> arr1(other->toDAI());
+  MCAuto<DataArrayIdType> arr2(arr1->selectByTupleIdSafe(_arr->begin(),_arr->end()));
   return DataArrayPartDefinition::New(arr2);
 }
 
-void DataArrayPartDefinition::checkCoherency() const
+void DataArrayPartDefinition::checkConsistencyLight() const
 {
   CheckInternalArrayOK(_arr);
 }
@@ -113,8 +173,8 @@ void DataArrayPartDefinition::checkCoherency() const
  */
 PartDefinition *DataArrayPartDefinition::tryToSimplify() const
 {
-  checkCoherency();
-  int a(0),b(0),c(0);
+  checkConsistencyLight();
+  mcIdType a(0),b(0),c(0);
   if(_arr->isRange(a,b,c))
     {
       return SlicePartDefinition::New(a,b,c);
@@ -127,16 +187,21 @@ PartDefinition *DataArrayPartDefinition::tryToSimplify() const
     }
 }
 
-DataArrayInt *DataArrayPartDefinition::toDAI() const
+void DataArrayPartDefinition::serialize(std::vector<mcIdType>& tinyInt, std::vector< MCAuto<DataArrayIdType> >& bigArraysI) const
+{
+  bigArraysI.push_back(_arr);
+}
+
+DataArrayIdType *DataArrayPartDefinition::toDAI() const
 {
   checkInternalArrayOK();
-  const DataArrayInt *arr(_arr);
-  DataArrayInt *arr2(const_cast<DataArrayInt *>(arr));
+  const DataArrayIdType *arr(_arr);
+  DataArrayIdType *arr2(const_cast<DataArrayIdType *>(arr));
   arr2->incrRef();
   return arr2;
 }
 
-DataArrayPartDefinition::DataArrayPartDefinition(DataArrayInt *listOfIds)
+DataArrayPartDefinition::DataArrayPartDefinition(DataArrayIdType *listOfIds)
 {
   CheckInternalArrayOK(listOfIds);
   _arr=listOfIds;
@@ -148,7 +213,7 @@ void DataArrayPartDefinition::checkInternalArrayOK() const
   CheckInternalArrayOK(_arr);
 }
 
-void DataArrayPartDefinition::CheckInternalArrayOK(const DataArrayInt *listOfIds)
+void DataArrayPartDefinition::CheckInternalArrayOK(const DataArrayIdType *listOfIds)
 {
   if(!listOfIds || !listOfIds->isAllocated() || listOfIds->getNumberOfComponents()!=1)
     throw INTERP_KERNEL::Exception("DataArrayPartDefinition::CheckInternalArrayOK : Input list must be not null allocated and with one components !");
@@ -156,7 +221,7 @@ void DataArrayPartDefinition::CheckInternalArrayOK(const DataArrayInt *listOfIds
 
 void DataArrayPartDefinition::updateTime() const
 {
-  if((const DataArrayInt *)_arr)
+  if((const DataArrayIdType *)_arr)
     updateTimeWith(*_arr);
 }
 
@@ -167,22 +232,22 @@ std::size_t DataArrayPartDefinition::getHeapMemorySizeWithoutChildren() const
 
 std::vector<const BigMemoryObject *> DataArrayPartDefinition::getDirectChildrenWithNull() const
 {
-  std::vector<const BigMemoryObject *> ret(1,(const DataArrayInt *)_arr);
+  std::vector<const BigMemoryObject *> ret(1,(const DataArrayIdType *)_arr);
   return ret;
 }
 
 DataArrayPartDefinition *DataArrayPartDefinition::add1(const DataArrayPartDefinition *other) const
 {
-  MEDCouplingAutoRefCountObjectPtr<DataArrayInt> a1(toDAI()),a2(other->toDAI());
-  MEDCouplingAutoRefCountObjectPtr<DataArrayInt> a3(DataArrayInt::Aggregate(a1,a2,0));
+  MCAuto<DataArrayIdType> a1(toDAI()),a2(other->toDAI());
+  MCAuto<DataArrayIdType> a3(DataArrayIdType::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<DataArrayIdType> a1(toDAI()),a2(other->toDAI());
+  MCAuto<DataArrayIdType> a3(DataArrayIdType::Aggregate(a1,a2,0));
   a3->sort();
   return DataArrayPartDefinition::New(a3);
 }
@@ -191,17 +256,44 @@ DataArrayPartDefinition::~DataArrayPartDefinition()
 {
 }
 
-SlicePartDefinition *SlicePartDefinition::New(int start, int stop, int step)
+SlicePartDefinition *SlicePartDefinition::New(mcIdType start, mcIdType stop, mcIdType step)
 {
   return new SlicePartDefinition(start,stop,step);
 }
 
-DataArrayInt *SlicePartDefinition::toDAI() const
+bool SlicePartDefinition::isEqual(const PartDefinition *other, std::string& what) const
 {
-  return DataArrayInt::Range(_start,_stop,_step);
+  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);
+}
+
+DataArrayIdType *SlicePartDefinition::toDAI() const
+{
+  return DataArrayIdType::Range(_start,_stop,_step);
 }
 
-int SlicePartDefinition::getNumberOfElems() const
+mcIdType SlicePartDefinition::getNumberOfElems() const
 {
   return DataArray::GetNumberOfItemGivenBES(_start,_stop,_step,"SlicePartDefinition::getNumberOfElems");
 }
@@ -228,17 +320,17 @@ PartDefinition *SlicePartDefinition::composeWith(const PartDefinition *other) co
 {
   if(!other)
     throw INTERP_KERNEL::Exception("SlicePartDefinition::composeWith : input PartDef must be not NULL !");
-  checkCoherency();
-  other->checkCoherency();
-  MEDCouplingAutoRefCountObjectPtr<DataArrayInt> arr(other->toDAI());
-  MEDCouplingAutoRefCountObjectPtr<DataArrayInt> arr1(arr->selectByTupleId2(_start,_stop,_step));
+  checkConsistencyLight();
+  other->checkConsistencyLight();
+  MCAuto<DataArrayIdType> arr(other->toDAI());
+  MCAuto<DataArrayIdType> arr1(arr->selectByTupleIdSafeSlice(_start,_stop,_step));
   return DataArrayPartDefinition::New(arr1);
 }
 
 /*!
  * Do nothing it is not a bug.
  */
-void SlicePartDefinition::checkCoherency() const
+void SlicePartDefinition::checkConsistencyLight() const
 {
 }
 
@@ -254,6 +346,13 @@ PartDefinition *SlicePartDefinition::tryToSimplify() const
   return ret;
 }
 
+void SlicePartDefinition::serialize(std::vector<mcIdType>& tinyInt, std::vector< MCAuto<DataArrayIdType> >& bigArraysI) const
+{
+  tinyInt.push_back(_start);
+  tinyInt.push_back(_stop);
+  tinyInt.push_back(_step);
+}
+
 std::string SlicePartDefinition::getRepr() const
 {
   std::ostringstream oss;
@@ -261,20 +360,20 @@ std::string SlicePartDefinition::getRepr() const
   return oss.str();
 }
 
-int SlicePartDefinition::getEffectiveStop() const
+mcIdType SlicePartDefinition::getEffectiveStop() const
 {
-  int nbElems(DataArray::GetNumberOfItemGivenBES(_start,_stop,_step,"SlicePartDefinition::getEffectiveStop"));
+  mcIdType nbElems(DataArray::GetNumberOfItemGivenBES(_start,_stop,_step,"SlicePartDefinition::getEffectiveStop"));
   return _start+nbElems*_step;
 }
 
-void SlicePartDefinition::getSlice(int& start, int& stop, int& step) const
+void SlicePartDefinition::getSlice(mcIdType& start, mcIdType& stop, mcIdType& step) const
 {
   start=_start;
   stop=_stop;
   step=_step;
 }
 
-SlicePartDefinition::SlicePartDefinition(int start, int stop, int step):_start(start),_stop(stop),_step(step)
+SlicePartDefinition::SlicePartDefinition(mcIdType start, mcIdType stop, mcIdType step):_start(start),_stop(stop),_step(step)
 {
 }
 
@@ -297,8 +396,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<DataArrayIdType> a1(toDAI()),a2(other->toDAI());
+  MCAuto<DataArrayIdType> a3(DataArrayIdType::Aggregate(a1,a2,0));
   a3->sort();
   return DataArrayPartDefinition::New(a3);
 }
@@ -311,8 +410,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<DataArrayIdType> a1(toDAI()),a2(other->toDAI());
+      MCAuto<DataArrayIdType> a3(DataArrayIdType::Aggregate(a1,a2,0));
       a3->sort();
       return DataArrayPartDefinition::New(a3);
     }