]> SALOME platform Git repositories - tools/medcoupling.git/commitdiff
Salome HOME
Some new methods in DataArrayInt
authorageay <ageay>
Thu, 23 Feb 2012 14:48:08 +0000 (14:48 +0000)
committerageay <ageay>
Thu, 23 Feb 2012 14:48:08 +0000 (14:48 +0000)
src/MEDCoupling/MEDCouplingMemArray.cxx
src/MEDCoupling/MEDCouplingMemArray.hxx
src/MEDCoupling/Test/MEDCouplingBasicsTest5.cxx
src/MEDCoupling/Test/MEDCouplingBasicsTest5.hxx
src/MEDCoupling_Swig/MEDCoupling.i
src/MEDCoupling_Swig/MEDCouplingBasicsTest.py
src/MEDCoupling_Swig/MEDCouplingTypemaps.i

index 45b07fb92ce618e474fd3762f69182ec2a6acb12..850c40a8e6c3fac42a30996132db71c985d75712 100644 (file)
@@ -813,7 +813,7 @@ DataArrayDouble *DataArrayDouble::selectByTupleRanges(const std::vector<std::pai
     {
       if((*it).first<=(*it).second)
         {
-          if((*it).second<=nbOfTuplesThis)
+          if((*it).first>=0 && (*it).second<=nbOfTuplesThis)
             {
               nbOfTuples+=(*it).second-(*it).first;
               if(isIncreasing)
@@ -1305,12 +1305,14 @@ void DataArrayDouble::setContigPartOfSelectedValues2(int tupleIdStart, const Dat
   const char msg[]="DataArrayDouble::setContigPartOfSelectedValues2";
   int nbOfTupleToWrite=DataArray::GetNumberOfItemGivenBES(bg,end2,step,msg);
   if(nbOfComp!=a->getNumberOfComponents())
-    throw INTERP_KERNEL::Exception("DataArrayDouble::setContigPartOfSelectedValues : This and a do not have the same number of components !");
+    throw INTERP_KERNEL::Exception("DataArrayDouble::setContigPartOfSelectedValues2 : This and a do not have the same number of components !");
   int thisNt=getNumberOfTuples();
   int aNt=a->getNumberOfTuples();
   double *valsToSet=getPointer()+tupleIdStart*nbOfComp;
   if(tupleIdStart+nbOfTupleToWrite>thisNt)
-    throw INTERP_KERNEL::Exception("DataArrayDouble::setContigPartOfSelectedValues : invalid number range of values to write !");
+    throw INTERP_KERNEL::Exception("DataArrayDouble::setContigPartOfSelectedValues2 : invalid number range of values to write !");
+  if(end2>aNt)
+    throw INTERP_KERNEL::Exception("DataArrayDouble::setContigPartOfSelectedValues2 : invalid range of values to read !");
   const double *valsSrc=a->getConstPointer()+bg*nbOfComp;
   for(int i=0;i<nbOfTupleToWrite;i++,valsToSet+=nbOfComp,valsSrc+=step*nbOfComp)
     {
@@ -3102,6 +3104,66 @@ DataArrayInt *DataArrayInt::selectByTupleId2(int bg, int end2, int step) const t
   return ret;
 }
 
+/*!
+ * This method returns a newly allocated array that is the concatenation of all tuples ranges in param 'ranges'.
+ * Each pair in input 'ranges' is in [begin,end) format. If there is a range in 'ranges' so that end is before begin an exception
+ * will be thrown. If there is a range in 'ranges' so that end is greater than number of tuples of 'this', an exception will be thrown too.
+ */
+DataArrayInt *DataArrayInt::selectByTupleRanges(const std::vector<std::pair<int,int> >& ranges) const throw(INTERP_KERNEL::Exception)
+{
+  checkAllocated();
+  int nbOfComp=getNumberOfComponents();
+  int nbOfTuplesThis=getNumberOfTuples();
+  if(ranges.empty())
+    {
+      DataArrayInt *ret=DataArrayInt::New();
+      ret->alloc(0,nbOfComp);
+      ret->copyStringInfoFrom(*this);
+      return ret;
+    }
+  int st=ranges.front().first;
+  int endd=ranges.back().second;
+  int ref=st;
+  int nbOfTuples=0;
+  bool isIncreasing=true;
+  for(std::vector<std::pair<int,int> >::const_iterator it=ranges.begin();it!=ranges.end();it++)
+    {
+      if((*it).first<=(*it).second)
+        {
+          if((*it).first>=0 && (*it).second<=nbOfTuplesThis)
+            {
+              nbOfTuples+=(*it).second-(*it).first;
+              if(isIncreasing)
+                isIncreasing=ref<=(*it).first;
+              ref=(*it).second;
+            }
+          else
+            {
+              std::ostringstream oss; oss << "DataArrayInt::selectByTupleRanges : on range #" << std::distance(ranges.begin(),it);
+              oss << " (" << (*it).first << "," << (*it).second << ") is greater than number of tuples of this :" << nbOfTuples << " !";
+              throw INTERP_KERNEL::Exception(oss.str().c_str());
+            }
+        }
+      else
+        {
+          std::ostringstream oss; oss << "DataArrayInt::selectByTupleRanges : on range #" << std::distance(ranges.begin(),it);
+          oss << " (" << (*it).first << "," << (*it).second << ") end is before begin !";
+          throw INTERP_KERNEL::Exception(oss.str().c_str());
+        }
+    }
+  if(isIncreasing && nbOfTuplesThis==nbOfTuples)
+    return deepCpy();
+  MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
+  ret->alloc(nbOfTuples,nbOfComp);
+  ret->copyStringInfoFrom(*this);
+  const int *src=getConstPointer();
+  int *work=ret->getPointer();
+  for(std::vector<std::pair<int,int> >::const_iterator it=ranges.begin();it!=ranges.end();it++)
+    work=std::copy(src+(*it).first*nbOfComp,src+(*it).second*nbOfComp,work);
+  ret->incrRef();
+  return ret;
+}
+
 /*!
  * This method works only for arrays having single component.
  * If this contains the array a1 containing [9,10,0,6,4,11,3,7] this method returns an array a2 [5,6,0,3,2,7,1,4].
@@ -3644,6 +3706,73 @@ void DataArrayInt::setPartOfValuesAdv(const DataArrayInt *a, const DataArrayInt
     }
 }
 
+/*!
+ * 'this', 'a' and 'tuplesSelec' are expected to be defined. If not an exception will be thrown.
+ * This is a method that is a specialization to DataArrayInt::setPartOfValuesAdv method, except that here the tuple selection of 'a' is given by a range ('bg','end2' and 'step')
+ * rather than an explicite array of tuple ids (given by the 2nd component) and the feeding is done in 'this' contiguously starting from 'tupleIdStart'.
+ * @param a is an array having exactly the same number of components than 'this'
+ */
+void DataArrayInt::setContigPartOfSelectedValues(int tupleIdStart, const DataArrayInt*a, const DataArrayInt *tuplesSelec) throw(INTERP_KERNEL::Exception)
+{
+  checkAllocated();
+  a->checkAllocated();
+  tuplesSelec->checkAllocated();
+  int nbOfComp=getNumberOfComponents();
+  if(nbOfComp!=a->getNumberOfComponents())
+    throw INTERP_KERNEL::Exception("DataArrayInt::setContigPartOfSelectedValues : This and a do not have the same number of components !");
+  if(tuplesSelec->getNumberOfComponents()!=1)
+    throw INTERP_KERNEL::Exception("DataArrayInt::setContigPartOfSelectedValues : Expecting to have a tuple selector DataArrayInt instance with exactly 1 component !");
+  int thisNt=getNumberOfTuples();
+  int aNt=a->getNumberOfTuples();
+  int nbOfTupleToWrite=tuplesSelec->getNumberOfTuples();
+  int *valsToSet=getPointer()+tupleIdStart*nbOfComp;
+  if(tupleIdStart+nbOfTupleToWrite>thisNt)
+    throw INTERP_KERNEL::Exception("DataArrayInt::setContigPartOfSelectedValues : invalid number range of values to write !");
+  const int *valsSrc=a->getConstPointer();
+  for(const int *tuple=tuplesSelec->begin();tuple!=tuplesSelec->end();tuple++,valsToSet+=nbOfComp)
+    {
+      if(*tuple>=0 && *tuple<aNt)
+        {
+          std::copy(valsSrc+nbOfComp*(*tuple),valsSrc+nbOfComp*(*tuple+1),valsToSet);
+        }
+      else
+        {
+          std::ostringstream oss; oss << "DataArrayInt::setContigPartOfSelectedValues : Tuple #" << std::distance(tuplesSelec->begin(),tuple);
+          oss << " of 'tuplesSelec' request of tuple id #" << *tuple << " in 'a' ! It should be in [0," << aNt << ") !";
+          throw INTERP_KERNEL::Exception(oss.str().c_str());
+        }
+    }
+}
+
+/*!
+ * 'this' and 'a' are expected to be defined. If not an exception will be thrown.
+ * This is a method that is a specialization to DataArrayInt::setContigPartOfSelectedValues method, except that here the tuple selection is givenin a is done by a range ('bg','end2' and 'step')
+ * rather than an explicite array of tuple ids.
+ * @param a is an array having exactly the same number of components than 'this'
+ */
+void DataArrayInt::setContigPartOfSelectedValues2(int tupleIdStart, const DataArrayInt *a, int bg, int end2, int step) throw(INTERP_KERNEL::Exception)
+{
+  checkAllocated();
+  a->checkAllocated();
+  int nbOfComp=getNumberOfComponents();
+  const char msg[]="DataArrayInt::setContigPartOfSelectedValues2";
+  int nbOfTupleToWrite=DataArray::GetNumberOfItemGivenBES(bg,end2,step,msg);
+  if(nbOfComp!=a->getNumberOfComponents())
+    throw INTERP_KERNEL::Exception("DataArrayInt::setContigPartOfSelectedValues2 : This and a do not have the same number of components !");
+  int thisNt=getNumberOfTuples();
+  int aNt=a->getNumberOfTuples();
+  int *valsToSet=getPointer()+tupleIdStart*nbOfComp;
+  if(tupleIdStart+nbOfTupleToWrite>thisNt)
+    throw INTERP_KERNEL::Exception("DataArrayInt::setContigPartOfSelectedValues2 : invalid number range of values to write !");
+  if(end2>aNt)
+    throw INTERP_KERNEL::Exception("DataArrayInt::setContigPartOfSelectedValues2 : invalid range of values to read !");
+  const int *valsSrc=a->getConstPointer()+bg*nbOfComp;
+  for(int i=0;i<nbOfTupleToWrite;i++,valsToSet+=nbOfComp,valsSrc+=step*nbOfComp)
+    {
+      std::copy(valsSrc,valsSrc+nbOfComp,valsToSet);
+    }
+}
+
 /*!
  * This method returns the last element in 'this'. So this method makes the hypothesis that 'this' is allocated.
  * This method works only for arrays that have exactly number of components equal to 1. If not an exception is thrown.
@@ -3826,10 +3955,10 @@ DataArrayInt *DataArrayInt::Aggregate(const std::vector<const DataArrayInt *>& a
 int DataArrayInt::getMaxValue(int& tupleId) const throw(INTERP_KERNEL::Exception)
 {
   if(getNumberOfComponents()!=1)
-    throw INTERP_KERNEL::Exception("DataArrayDouble::getMaxValue : must be applied on DataArrayDouble with only one component !");
+    throw INTERP_KERNEL::Exception("DataArrayInt::getMaxValue : must be applied on DataArrayInt with only one component !");
   int nbOfTuples=getNumberOfTuples();
   if(nbOfTuples<=0)
-    throw INTERP_KERNEL::Exception("DataArrayDouble::getMaxValue : array exists but number of tuples must be > 0 !");
+    throw INTERP_KERNEL::Exception("DataArrayInt::getMaxValue : array exists but number of tuples must be > 0 !");
   const int *vals=getConstPointer();
   const int *loc=std::max_element(vals,vals+nbOfTuples);
   tupleId=(int)std::distance(vals,loc);
@@ -3849,10 +3978,10 @@ int DataArrayInt::getMaxValueInArray() const throw(INTERP_KERNEL::Exception)
 int DataArrayInt::getMinValue(int& tupleId) const throw(INTERP_KERNEL::Exception)
 {
   if(getNumberOfComponents()!=1)
-    throw INTERP_KERNEL::Exception("DataArrayDouble::getMaxValue : must be applied on DataArrayDouble with only one component !");
+    throw INTERP_KERNEL::Exception("DataArrayInt::getMaxValue : must be applied on DataArrayInt with only one component !");
   int nbOfTuples=getNumberOfTuples();
   if(nbOfTuples<=0)
-    throw INTERP_KERNEL::Exception("DataArrayDouble::getMaxValue : array exists but number of tuples must be > 0 !");
+    throw INTERP_KERNEL::Exception("DataArrayInt::getMaxValue : array exists but number of tuples must be > 0 !");
   const int *vals=getConstPointer();
   const int *loc=std::min_element(vals,vals+nbOfTuples);
   tupleId=(int)std::distance(vals,loc);
index 1ad5ef8aa2c55b2e20d8e199c90a405bdc806d52..22ee1a1518e4b2fc7c95d7183cbb12b4f72ba244 100644 (file)
@@ -353,6 +353,7 @@ namespace ParaMEDMEM
     MEDCOUPLING_EXPORT DataArrayInt *selectByTupleId(const int *new2OldBg, const int *new2OldEnd) const;
     MEDCOUPLING_EXPORT DataArrayInt *selectByTupleIdSafe(const int *new2OldBg, const int *new2OldEnd) const throw(INTERP_KERNEL::Exception);
     MEDCOUPLING_EXPORT DataArrayInt *selectByTupleId2(int bg, int end, int step) const throw(INTERP_KERNEL::Exception);
+    MEDCOUPLING_EXPORT DataArrayInt *selectByTupleRanges(const std::vector<std::pair<int,int> >& ranges) const throw(INTERP_KERNEL::Exception);
     MEDCOUPLING_EXPORT DataArrayInt *checkAndPreparePermutation() const throw(INTERP_KERNEL::Exception);
     MEDCOUPLING_EXPORT void changeSurjectiveFormat(int targetNb, DataArrayInt *&arr, DataArrayInt *&arrI) const throw(INTERP_KERNEL::Exception);
     MEDCOUPLING_EXPORT static DataArrayInt *BuildOld2NewArrayFromSurjectiveFormat2(int nbOfOldTuples, const DataArrayInt *arr, const DataArrayInt *arrI, int &newNbOfTuples) throw(INTERP_KERNEL::Exception);
@@ -372,6 +373,8 @@ namespace ParaMEDMEM
     MEDCOUPLING_EXPORT void setPartOfValues3(const DataArrayInt *a, const int *bgTuples, const int *endTuples, int bgComp, int endComp, int stepComp, bool strictCompoCompare=true) throw(INTERP_KERNEL::Exception);
     MEDCOUPLING_EXPORT void setPartOfValuesSimple3(int a, const int *bgTuples, const int *endTuples, int bgComp, int endComp, int stepComp) throw(INTERP_KERNEL::Exception);
     MEDCOUPLING_EXPORT void setPartOfValuesAdv(const DataArrayInt *a, const DataArrayInt *tuplesSelec) throw(INTERP_KERNEL::Exception);
+    MEDCOUPLING_EXPORT void setContigPartOfSelectedValues(int tupleIdStart, const DataArrayInt*a, const DataArrayInt *tuplesSelec) throw(INTERP_KERNEL::Exception);
+    MEDCOUPLING_EXPORT void setContigPartOfSelectedValues2(int tupleIdStart, const DataArrayInt *a, int bg, int end2, int step) throw(INTERP_KERNEL::Exception);
     MEDCOUPLING_EXPORT void getTuple(int tupleId, int *res) const { std::copy(_mem.getConstPointerLoc(tupleId*((int)_info_on_compo.size())),_mem.getConstPointerLoc((tupleId+1)*((int)_info_on_compo.size())),res); }
     MEDCOUPLING_EXPORT int getIJ(int tupleId, int compoId) const { return _mem[tupleId*((int)_info_on_compo.size())+compoId]; }
     MEDCOUPLING_EXPORT int back() const throw(INTERP_KERNEL::Exception);
index c49d78d8ff663b9f01b27cd0a4cbdacdb6d361c3..3ea07c2a28cf1548e52251033cfbc89d6af06e7b 100644 (file)
@@ -304,3 +304,191 @@ void MEDCouplingBasicsTest5::testBuildSlice3DSurf1()
   //
   mesh2D->decrRef();
 }
+
+void MEDCouplingBasicsTest5::testDataArrayDoubleAdvSetting1()
+{
+  const double data1[14]={1.,11.,2.,12.,3.,13.,4.,14.,5.,15.,6.,16.,7.,17.};
+  const double data2[10]={8.,38.,9.,39.,0.,30.,11.,41.,12.,42.};
+  const char *comps[2]={"comp1","comp2"};
+  std::vector<std::string> compsCpp(comps,comps+2);
+  DataArrayDouble *da=DataArrayDouble::New();
+  DataArrayDouble *tmp=0;
+  da->setInfoAndChangeNbOfCompo(compsCpp);
+  da->setName("da");
+  da->alloc(7,2);
+  compsCpp.pop_back();
+  CPPUNIT_ASSERT_THROW(da->setInfoAndChangeNbOfCompo(compsCpp),INTERP_KERNEL::Exception);
+  std::copy(data1,data1+14,da->getPointer());
+  //
+  std::vector<std::pair<int,int> > p(3);
+  p[0].first=0; p[0].second=3; p[1].first=3; p[1].second=5; p[2].first=5; p[2].second=7;
+  tmp=da->selectByTupleRanges(p);
+  CPPUNIT_ASSERT(tmp->isEqual(*da,1e-14));
+  tmp->decrRef();
+  p[0].first=0; p[0].second=2; p[1].first=3; p[1].second=4; p[2].first=5; p[2].second=7;
+  tmp=da->selectByTupleRanges(p);
+  const double expected1[10]={1.,11.,2.,12.,4.,14.,6.,16.,7.,17.};
+  CPPUNIT_ASSERT_EQUAL(5,tmp->getNumberOfTuples());
+  CPPUNIT_ASSERT_EQUAL(2,tmp->getNumberOfComponents());
+  for(int i=0;i<10;i++)
+    CPPUNIT_ASSERT_DOUBLES_EQUAL(expected1[i],tmp->getIJ(0,i),1e-14);
+  tmp->decrRef();
+  p[0].first=0; p[0].second=2; p[1].first=0; p[1].second=2; p[2].first=5; p[2].second=6;
+  tmp=da->selectByTupleRanges(p);
+  const double expected2[10]={1.,11.,2.,12.,1.,11.,2.,12.,6.,16.};
+  CPPUNIT_ASSERT_EQUAL(5,tmp->getNumberOfTuples());
+  CPPUNIT_ASSERT_EQUAL(2,tmp->getNumberOfComponents());
+  for(int i=0;i<10;i++)
+    CPPUNIT_ASSERT_DOUBLES_EQUAL(expected2[i],tmp->getIJ(0,i),1e-14);
+  tmp->decrRef();
+  p[0].first=0; p[0].second=2; p[1].first=-1; p[1].second=2; p[2].first=5; p[2].second=6;
+  CPPUNIT_ASSERT_THROW(da->selectByTupleRanges(p),INTERP_KERNEL::Exception);
+  p[0].first=0; p[0].second=2; p[1].first=0; p[1].second=2; p[2].first=5; p[2].second=8;
+  CPPUNIT_ASSERT_THROW(da->selectByTupleRanges(p),INTERP_KERNEL::Exception);
+  //
+  DataArrayDouble *da2=DataArrayDouble::New();
+  da2->alloc(5,2);
+  std::copy(data2,data2+10,da2->getPointer());
+  //
+  DataArrayDouble *dac=da->deepCpy();
+  dac->setContigPartOfSelectedValues2(1,da2,2,4,1);
+  const double expected3[14]={1.,11.,0.,30.,11.,41.,4.,14.,5.,15.,6.,16.,7.,17.};
+  for(int i=0;i<14;i++)
+    CPPUNIT_ASSERT_DOUBLES_EQUAL(expected3[i],dac->getIJ(0,i),1e-14);
+  dac->decrRef();
+  //
+  dac=da->deepCpy();
+  CPPUNIT_ASSERT_THROW(dac->setContigPartOfSelectedValues2(3,da2,0,5,1),INTERP_KERNEL::Exception);
+  CPPUNIT_ASSERT_THROW(dac->setContigPartOfSelectedValues2(0,da2,4,6,1),INTERP_KERNEL::Exception);
+  CPPUNIT_ASSERT_THROW(dac->setContigPartOfSelectedValues2(3,da2,5,0,1),INTERP_KERNEL::Exception);
+  dac->setContigPartOfSelectedValues2(3,da2,1,5,1);
+  const double expected4[14]={1.,11.,2.,12.,3.,13.,9.,39.,0.,30.,11.,41.,12.,42.};
+  for(int i=0;i<14;i++)
+    CPPUNIT_ASSERT_DOUBLES_EQUAL(expected4[i],dac->getIJ(0,i),1e-14);
+  dac->decrRef();
+  //
+  DataArrayInt *ids=DataArrayInt::New();
+  ids->alloc(3,1);
+  dac=da->deepCpy();
+  ids->setIJ(0,0,2); ids->setIJ(1,0,0); ids->setIJ(2,0,4);
+  dac->setContigPartOfSelectedValues(2,da2,ids);
+  const double expected5[14]={1.,11.,2.,12.,0.,30.,8.,38.,12.,42.,6.,16.,7.,17.};
+  for(int i=0;i<14;i++)
+    CPPUNIT_ASSERT_DOUBLES_EQUAL(expected5[i],dac->getIJ(0,i),1e-14);
+  dac->decrRef();
+  //
+  dac=da->deepCpy();
+  ids->setIJ(0,0,2); ids->setIJ(1,0,5); ids->setIJ(2,0,4);
+  CPPUNIT_ASSERT_THROW(dac->setContigPartOfSelectedValues(1,da2,ids),INTERP_KERNEL::Exception);
+  ids->setIJ(0,0,2); ids->setIJ(1,0,2); ids->setIJ(2,0,-1);
+  CPPUNIT_ASSERT_THROW(dac->setContigPartOfSelectedValues(1,da2,ids),INTERP_KERNEL::Exception);
+  ids->setIJ(0,0,2); ids->setIJ(1,0,2); ids->setIJ(2,0,1);
+  CPPUNIT_ASSERT_THROW(dac->setContigPartOfSelectedValues(5,da2,ids),INTERP_KERNEL::Exception);
+  dac->decrRef();
+  //
+  ids->setIJ(0,0,2); ids->setIJ(1,0,2); ids->setIJ(2,0,1);
+  dac=da->deepCpy();
+  dac->setContigPartOfSelectedValues(4,da2,ids);
+  const double expected6[14]={1.,11.,2.,12.,3.,13.,4.,14.,0.,30.,0.,30.,9.,39.};
+  for(int i=0;i<14;i++)
+    CPPUNIT_ASSERT_DOUBLES_EQUAL(expected6[i],dac->getIJ(0,i),1e-14);
+  dac->decrRef();
+  ids->decrRef();
+  //
+  da2->decrRef();
+  da->decrRef();
+}
+
+void MEDCouplingBasicsTest5::testDataArrayIntAdvSetting1()
+{
+  const int data1[14]={1,11,2,12,3,13,4,14,5,15,6,16,7,17};
+  const int data2[10]={8,38,9,39,0,30,11,41,12,42};
+  const char *comps[2]={"comp1","comp2"};
+  std::vector<std::string> compsCpp(comps,comps+2);
+  DataArrayInt *da=DataArrayInt::New();
+  DataArrayInt *tmp=0;
+  da->setInfoAndChangeNbOfCompo(compsCpp);
+  da->setName("da");
+  da->alloc(7,2);
+  compsCpp.pop_back();
+  CPPUNIT_ASSERT_THROW(da->setInfoAndChangeNbOfCompo(compsCpp),INTERP_KERNEL::Exception);
+  std::copy(data1,data1+14,da->getPointer());
+  //
+  std::vector<std::pair<int,int> > p(3);
+  p[0].first=0; p[0].second=3; p[1].first=3; p[1].second=5; p[2].first=5; p[2].second=7;
+  tmp=da->selectByTupleRanges(p);
+  CPPUNIT_ASSERT(tmp->isEqual(*da));
+  tmp->decrRef();
+  p[0].first=0; p[0].second=2; p[1].first=3; p[1].second=4; p[2].first=5; p[2].second=7;
+  tmp=da->selectByTupleRanges(p);
+  const int expected1[10]={1,11,2,12,4,14,6,16,7,17};
+  CPPUNIT_ASSERT_EQUAL(5,tmp->getNumberOfTuples());
+  CPPUNIT_ASSERT_EQUAL(2,tmp->getNumberOfComponents());
+  for(int i=0;i<10;i++)
+    CPPUNIT_ASSERT_EQUAL(expected1[i],tmp->getIJ(0,i));
+  tmp->decrRef();
+  p[0].first=0; p[0].second=2; p[1].first=0; p[1].second=2; p[2].first=5; p[2].second=6;
+  tmp=da->selectByTupleRanges(p);
+  const int expected2[10]={1,11,2,12,1,11,2,12,6,16};
+  CPPUNIT_ASSERT_EQUAL(5,tmp->getNumberOfTuples());
+  CPPUNIT_ASSERT_EQUAL(2,tmp->getNumberOfComponents());
+  for(int i=0;i<10;i++)
+    CPPUNIT_ASSERT_EQUAL(expected2[i],tmp->getIJ(0,i));
+  tmp->decrRef();
+  p[0].first=0; p[0].second=2; p[1].first=-1; p[1].second=2; p[2].first=5; p[2].second=6;
+  CPPUNIT_ASSERT_THROW(da->selectByTupleRanges(p),INTERP_KERNEL::Exception);
+  p[0].first=0; p[0].second=2; p[1].first=0; p[1].second=2; p[2].first=5; p[2].second=8;
+  CPPUNIT_ASSERT_THROW(da->selectByTupleRanges(p),INTERP_KERNEL::Exception);
+  //
+  DataArrayInt *da2=DataArrayInt::New();
+  da2->alloc(5,2);
+  std::copy(data2,data2+10,da2->getPointer());
+  //
+  DataArrayInt *dac=da->deepCpy();
+  dac->setContigPartOfSelectedValues2(1,da2,2,4,1);
+  const int expected3[14]={1,11,0,30,11,41,4,14,5,15,6,16,7,17};
+  for(int i=0;i<14;i++)
+    CPPUNIT_ASSERT_EQUAL(expected3[i],dac->getIJ(0,i));
+  dac->decrRef();
+  //
+  dac=da->deepCpy();
+  CPPUNIT_ASSERT_THROW(dac->setContigPartOfSelectedValues2(3,da2,0,5,1),INTERP_KERNEL::Exception);
+  CPPUNIT_ASSERT_THROW(dac->setContigPartOfSelectedValues2(0,da2,4,6,1),INTERP_KERNEL::Exception);
+  CPPUNIT_ASSERT_THROW(dac->setContigPartOfSelectedValues2(3,da2,5,0,1),INTERP_KERNEL::Exception);
+  dac->setContigPartOfSelectedValues2(3,da2,1,5,1);
+  const int expected4[14]={1,11,2,12,3,13,9,39,0,30,11,41,12,42};
+  for(int i=0;i<14;i++)
+    CPPUNIT_ASSERT_EQUAL(expected4[i],dac->getIJ(0,i));
+  dac->decrRef();
+  //
+  DataArrayInt *ids=DataArrayInt::New();
+  ids->alloc(3,1);
+  dac=da->deepCpy();
+  ids->setIJ(0,0,2); ids->setIJ(1,0,0); ids->setIJ(2,0,4);
+  dac->setContigPartOfSelectedValues(2,da2,ids);
+  const int expected5[14]={1,11,2,12,0,30,8,38,12,42,6,16,7,17};
+  for(int i=0;i<14;i++)
+    CPPUNIT_ASSERT_EQUAL(expected5[i],dac->getIJ(0,i));
+  dac->decrRef();
+  //
+  dac=da->deepCpy();
+  ids->setIJ(0,0,2); ids->setIJ(1,0,5); ids->setIJ(2,0,4);
+  CPPUNIT_ASSERT_THROW(dac->setContigPartOfSelectedValues(1,da2,ids),INTERP_KERNEL::Exception);
+  ids->setIJ(0,0,2); ids->setIJ(1,0,2); ids->setIJ(2,0,-1);
+  CPPUNIT_ASSERT_THROW(dac->setContigPartOfSelectedValues(1,da2,ids),INTERP_KERNEL::Exception);
+  ids->setIJ(0,0,2); ids->setIJ(1,0,2); ids->setIJ(2,0,1);
+  CPPUNIT_ASSERT_THROW(dac->setContigPartOfSelectedValues(5,da2,ids),INTERP_KERNEL::Exception);
+  dac->decrRef();
+  //
+  ids->setIJ(0,0,2); ids->setIJ(1,0,2); ids->setIJ(2,0,1);
+  dac=da->deepCpy();
+  dac->setContigPartOfSelectedValues(4,da2,ids);
+  const int expected6[14]={1,11,2,12,3,13,4,14,0,30,0,30,9,39};
+  for(int i=0;i<14;i++)
+    CPPUNIT_ASSERT_EQUAL(expected6[i],dac->getIJ(0,i));
+  dac->decrRef();
+  ids->decrRef();
+  //
+  da2->decrRef();
+  da->decrRef();
+}
index 6d8a2061008e98a6a62525c0d70d6b1ee8299f52..a20c1da191f1edfeb237cbc601eefb2995849ac9 100644 (file)
@@ -40,6 +40,8 @@ namespace ParaMEDMEM
     CPPUNIT_TEST( testGetCellIdsCrossingPlane1 );
     CPPUNIT_TEST( testBuildSlice3D1 );
     CPPUNIT_TEST( testBuildSlice3DSurf1 );
+    CPPUNIT_TEST( testDataArrayDoubleAdvSetting1 );
+    CPPUNIT_TEST( testDataArrayIntAdvSetting1 );
     CPPUNIT_TEST_SUITE_END();
   public:
     void testUMeshTessellate2D1();
@@ -47,6 +49,8 @@ namespace ParaMEDMEM
     void testGetCellIdsCrossingPlane1();
     void testBuildSlice3D1();
     void testBuildSlice3DSurf1();
+    void testDataArrayDoubleAdvSetting1();
+    void testDataArrayIntAdvSetting1();
   };
 }
 
index ebfd04bef39eab0416d0e0b5a185acb588316a1c..74c752b46ed9f2dd634cf51fff8aa3eeb1f22206 100644 (file)
@@ -125,6 +125,7 @@ using namespace INTERP_KERNEL;
 %newobject ParaMEDMEM::DataArrayInt::selectByTupleId;
 %newobject ParaMEDMEM::DataArrayInt::selectByTupleIdSafe;
 %newobject ParaMEDMEM::DataArrayInt::selectByTupleId2;
+%newobject ParaMEDMEM::DataArrayInt::selectByTupleRanges;
 %newobject ParaMEDMEM::DataArrayInt::checkAndPreparePermutation;
 %newobject ParaMEDMEM::DataArrayInt::transformWithIndArrR;
 %newobject ParaMEDMEM::DataArrayInt::renumber;
@@ -2126,6 +2127,13 @@ namespace ParaMEDMEM
      return DataArrayDouble::Meld(tmp);
    }
 
+   DataArrayDouble *selectByTupleRanges(PyObject *li) const throw(INTERP_KERNEL::Exception)
+   {
+     std::vector<std::pair<int,int> > ranges;
+     convertPyToVectorPairInt(li,ranges);
+     return self->selectByTupleRanges(ranges);
+   }
+
    DataArrayDouble *__getitem__(PyObject *obj) throw(INTERP_KERNEL::Exception)
    {
      const char msg[]="Unexpected situation in __getitem__ !";
@@ -3464,6 +3472,13 @@ namespace ParaMEDMEM
      return res;
    }
 
+   DataArrayInt *selectByTupleRanges(PyObject *li) const throw(INTERP_KERNEL::Exception)
+   {
+     std::vector<std::pair<int,int> > ranges;
+     convertPyToVectorPairInt(li,ranges);
+     return self->selectByTupleRanges(ranges);
+   }
+
    static DataArrayInt *Meld(PyObject *li) throw(INTERP_KERNEL::Exception)
    {
      std::vector<const DataArrayInt *> tmp;
index bb038c49b7127eff59d56d31b542c5a83e375d5a..77ffeed3a14aa3d7b3b2171ef8d2cba7e3c39907 100644 (file)
@@ -8715,6 +8715,172 @@ class MEDCouplingBasicsTest(unittest.TestCase):
             self.assertAlmostEqual(expected8[i],slice1.getCoords().getIJ(0,i),12);
             pass
         pass
+
+    def testDataArrayDoubleAdvSetting1(self):
+        data1=[1.,11.,2.,12.,3.,13.,4.,14.,5.,15.,6.,16.,7.,17.]
+        data2=[8.,38.,9.,39.,0.,30.,11.,41.,12.,42.]
+        compsCpp=["comp1","comp2"]
+        da=DataArrayDouble.New();
+        da.setInfoAndChangeNbOfCompo(compsCpp);
+        da.setName("da");
+        da.alloc(7,2);
+        compsCpp=compsCpp[:-1]
+        self.assertRaises(InterpKernelException,da.setInfoAndChangeNbOfCompo,compsCpp);
+        da.setValues(data1,7,2)
+        #
+        p=[(0,3),(3,5),(5,7)]
+        tmp=da.selectByTupleRanges(p);
+        self.assertTrue(tmp.isEqual(da,1e-14));
+        p=[(0,2),(3,4),(5,7)]
+        tmp=da.selectByTupleRanges(p);
+        expected1=[1.,11.,2.,12.,4.,14.,6.,16.,7.,17.]
+        self.assertEqual(5,tmp.getNumberOfTuples());
+        self.assertEqual(2,tmp.getNumberOfComponents());
+        for i in xrange(10):
+            self.assertAlmostEqual(expected1[i],tmp.getIJ(0,i),14);
+            pass
+        p=[(0,2),(0,2),(5,6)]
+        tmp=da.selectByTupleRanges(p);
+        expected2=[1.,11.,2.,12.,1.,11.,2.,12.,6.,16.]
+        self.assertEqual(5,tmp.getNumberOfTuples());
+        self.assertEqual(2,tmp.getNumberOfComponents());
+        for i in xrange(10):
+            self.assertAlmostEqual(expected2[i],tmp.getIJ(0,i),14);
+            pass
+        p=[(0,2),(-1,2),(5,6)]
+        self.assertRaises(InterpKernelException,da.selectByTupleRanges,p);
+        p=[(0,2),(0,2),(5,8)]
+        self.assertRaises(InterpKernelException,da.selectByTupleRanges,p);
+        #
+        da2=DataArrayDouble.New();
+        da2.setValues(data2,5,2);
+        #
+        dac=da.deepCpy();
+        dac.setContigPartOfSelectedValues2(1,da2,2,4,1);
+        expected3=[1.,11.,0.,30.,11.,41.,4.,14.,5.,15.,6.,16.,7.,17.]
+        for i in xrange(14):
+            self.assertAlmostEqual(expected3[i],dac.getIJ(0,i),14);
+            pass
+        #
+        dac=da.deepCpy();
+        self.assertRaises(InterpKernelException,dac.setContigPartOfSelectedValues2,3,da2,0,5,1);
+        self.assertRaises(InterpKernelException,dac.setContigPartOfSelectedValues2,0,da2,4,6,1);
+        self.assertRaises(InterpKernelException,dac.setContigPartOfSelectedValues2,3,da2,5,0,1);
+        dac.setContigPartOfSelectedValues2(3,da2,1,5,1);
+        expected4=[1.,11.,2.,12.,3.,13.,9.,39.,0.,30.,11.,41.,12.,42.]
+        for i in xrange(14):
+            self.assertAlmostEqual(expected4[i],dac.getIJ(0,i),14);
+            pass
+        #
+        ids=DataArrayInt.New();
+        ids.alloc(3,1);
+        dac=da.deepCpy();
+        ids.setIJ(0,0,2); ids.setIJ(1,0,0); ids.setIJ(2,0,4);
+        dac.setContigPartOfSelectedValues(2,da2,ids);
+        expected5=[1.,11.,2.,12.,0.,30.,8.,38.,12.,42.,6.,16.,7.,17.]
+        for i in xrange(14):
+            self.assertAlmostEqual(expected5[i],dac.getIJ(0,i),14);
+            pass
+        #
+        dac=da.deepCpy();
+        ids.setIJ(0,0,2); ids.setIJ(1,0,5); ids.setIJ(2,0,4);
+        self.assertRaises(InterpKernelException,dac.setContigPartOfSelectedValues,1,da2,ids);
+        ids.setIJ(0,0,2); ids.setIJ(1,0,2); ids.setIJ(2,0,-1);
+        self.assertRaises(InterpKernelException,dac.setContigPartOfSelectedValues,1,da2,ids);
+        ids.setIJ(0,0,2); ids.setIJ(1,0,2); ids.setIJ(2,0,1);
+        self.assertRaises(InterpKernelException,dac.setContigPartOfSelectedValues,5,da2,ids);
+        #
+        ids.setIJ(0,0,2); ids.setIJ(1,0,2); ids.setIJ(2,0,1);
+        dac=da.deepCpy();
+        dac.setContigPartOfSelectedValues(4,da2,ids);
+        expected6=[1.,11.,2.,12.,3.,13.,4.,14.,0.,30.,0.,30.,9.,39.]
+        for i in xrange(14):
+            self.assertAlmostEqual(expected6[i],dac.getIJ(0,i),14);
+            pass
+        pass
+
+    def testDataArrayIntAdvSetting1(self):
+        data1=[1,11,2,12,3,13,4,14,5,15,6,16,7,17]
+        data2=[8,38,9,39,0,30,11,41,12,42]
+        compsCpp=["comp1","comp2"]
+        da=DataArrayInt.New();
+        da.setInfoAndChangeNbOfCompo(compsCpp);
+        da.setName("da");
+        da.alloc(7,2);
+        compsCpp=compsCpp[:-1]
+        self.assertRaises(InterpKernelException,da.setInfoAndChangeNbOfCompo,compsCpp);
+        da.setValues(data1,7,2)
+        #
+        p=[(0,3),(3,5),(5,7)]
+        tmp=da.selectByTupleRanges(p);
+        self.assertTrue(tmp.isEqual(da));
+        p=[(0,2),(3,4),(5,7)]
+        tmp=da.selectByTupleRanges(p);
+        expected1=[1,11,2,12,4,14,6,16,7,17]
+        self.assertEqual(5,tmp.getNumberOfTuples());
+        self.assertEqual(2,tmp.getNumberOfComponents());
+        for i in xrange(10):
+            self.assertEqual(expected1[i],tmp.getIJ(0,i));
+            pass
+        p=[(0,2),(0,2),(5,6)]
+        tmp=da.selectByTupleRanges(p);
+        expected2=[1,11,2,12,1,11,2,12,6,16]
+        self.assertEqual(5,tmp.getNumberOfTuples());
+        self.assertEqual(2,tmp.getNumberOfComponents());
+        for i in xrange(10):
+            self.assertEqual(expected2[i],tmp.getIJ(0,i));
+            pass
+        p=[(0,2),(-1,2),(5,6)]
+        self.assertRaises(InterpKernelException,da.selectByTupleRanges,p);
+        p=[(0,2),(0,2),(5,8)]
+        self.assertRaises(InterpKernelException,da.selectByTupleRanges,p);
+        #
+        da2=DataArrayInt.New();
+        da2.setValues(data2,5,2);
+        #
+        dac=da.deepCpy();
+        dac.setContigPartOfSelectedValues2(1,da2,2,4,1);
+        expected3=[1,11,0,30,11,41,4,14,5,15,6,16,7,17]
+        for i in xrange(14):
+            self.assertEqual(expected3[i],dac.getIJ(0,i));
+            pass
+        #
+        dac=da.deepCpy();
+        self.assertRaises(InterpKernelException,dac.setContigPartOfSelectedValues2,3,da2,0,5,1);
+        self.assertRaises(InterpKernelException,dac.setContigPartOfSelectedValues2,0,da2,4,6,1);
+        self.assertRaises(InterpKernelException,dac.setContigPartOfSelectedValues2,3,da2,5,0,1);
+        dac.setContigPartOfSelectedValues2(3,da2,1,5,1);
+        expected4=[1,11,2,12,3,13,9,39,0,30,11,41,12,42]
+        for i in xrange(14):
+            self.assertEqual(expected4[i],dac.getIJ(0,i));
+            pass
+        #
+        ids=DataArrayInt.New();
+        ids.alloc(3,1);
+        dac=da.deepCpy();
+        ids.setIJ(0,0,2); ids.setIJ(1,0,0); ids.setIJ(2,0,4);
+        dac.setContigPartOfSelectedValues(2,da2,ids);
+        expected5=[1,11,2,12,0,30,8,38,12,42,6,16,7,17]
+        for i in xrange(14):
+            self.assertEqual(expected5[i],dac.getIJ(0,i));
+            pass
+        #
+        dac=da.deepCpy();
+        ids.setIJ(0,0,2); ids.setIJ(1,0,5); ids.setIJ(2,0,4);
+        self.assertRaises(InterpKernelException,dac.setContigPartOfSelectedValues,1,da2,ids);
+        ids.setIJ(0,0,2); ids.setIJ(1,0,2); ids.setIJ(2,0,-1);
+        self.assertRaises(InterpKernelException,dac.setContigPartOfSelectedValues,1,da2,ids);
+        ids.setIJ(0,0,2); ids.setIJ(1,0,2); ids.setIJ(2,0,1);
+        self.assertRaises(InterpKernelException,dac.setContigPartOfSelectedValues,5,da2,ids);
+        #
+        ids.setIJ(0,0,2); ids.setIJ(1,0,2); ids.setIJ(2,0,1);
+        dac=da.deepCpy();
+        dac.setContigPartOfSelectedValues(4,da2,ids);
+        expected6=[1,11,2,12,3,13,4,14,0,30,0,30,9,39]
+        for i in xrange(14):
+            self.assertEqual(expected6[i],dac.getIJ(0,i));
+            pass
+        pass
     
     def setUp(self):
         pass
index b077fdda4855c043b776e5093d93f6af0455fd94..f116a9945ddf32ab20303118a4316983af599d3e 100644 (file)
@@ -173,6 +173,63 @@ static int *convertPyToNewIntArr2(PyObject *pyLi, int *size) throw(INTERP_KERNEL
     }
 }
 
+static void convertPyToVectorPairInt(PyObject *pyLi, std::vector< std::pair<int,int> >& arr) throw(INTERP_KERNEL::Exception)
+{
+  const char msg[]="list must contain tuples of 2 integers only or tuple must contain tuples of 2 integers only !";
+  if(PyList_Check(pyLi))
+    {
+      int size=PyList_Size(pyLi);
+      arr.resize(size);
+      for(int i=0;i<size;i++)
+        {
+          PyObject *o=PyList_GetItem(pyLi,i);
+          if(PyTuple_Check(o))
+            {
+              int sz2=PyTuple_Size(o);
+              if(sz2!=2)
+                throw INTERP_KERNEL::Exception(msg);
+              PyObject *o_0=PyTuple_GetItem(o,0);
+              if(!PyInt_Check(o_0))
+                throw INTERP_KERNEL::Exception(msg);
+              PyObject *o_1=PyTuple_GetItem(o,1);
+              if(!PyInt_Check(o_1))
+                throw INTERP_KERNEL::Exception(msg);
+              arr[i].first=(int)PyInt_AS_LONG(o_0);
+              arr[i].second=(int)PyInt_AS_LONG(o_1);
+            }
+          else
+            throw INTERP_KERNEL::Exception(msg);
+        }
+    }
+  else if(PyTuple_Check(pyLi))
+    {
+      int size=PyTuple_Size(pyLi);
+      arr.resize(size);
+      for(int i=0;i<size;i++)
+        {
+          PyObject *o=PyTuple_GetItem(pyLi,i);
+          if(PyTuple_Check(o))
+            {
+              int sz2=PyTuple_Size(o);
+              if(sz2!=2)
+                throw INTERP_KERNEL::Exception(msg);
+              PyObject *o_0=PyTuple_GetItem(o,0);
+              if(!PyInt_Check(o_0))
+                throw INTERP_KERNEL::Exception(msg);
+              PyObject *o_1=PyTuple_GetItem(o,1);
+              if(!PyInt_Check(o_1))
+                throw INTERP_KERNEL::Exception(msg);
+              arr[i].first=(int)PyInt_AS_LONG(o_0);
+              arr[i].second=(int)PyInt_AS_LONG(o_1);
+            }
+          else
+            throw INTERP_KERNEL::Exception(msg);
+        }
+    }
+  else
+    throw INTERP_KERNEL::Exception(msg);
+}
+
 static void convertPyToNewIntArr3(PyObject *pyLi, std::vector<int>& arr) throw(INTERP_KERNEL::Exception)
 {
   if(PyList_Check(pyLi))