From 02dcbfb823a3be328f8d364f729683bbf38c8257 Mon Sep 17 00:00:00 2001 From: ageay Date: Thu, 30 Jun 2011 16:49:19 +0000 Subject: [PATCH] *** empty log message *** --- src/MEDCoupling/MEDCouplingMemArray.cxx | 55 +++++++++++++++++++ src/MEDCoupling/MEDCouplingMemArray.hxx | 1 + .../Test/MEDCouplingBasicsTest.hxx | 2 + .../Test/MEDCouplingBasicsTest4.cxx | 23 ++++++++ 4 files changed, 81 insertions(+) diff --git a/src/MEDCoupling/MEDCouplingMemArray.cxx b/src/MEDCoupling/MEDCouplingMemArray.cxx index aba8e7a37..7d4d14bb9 100644 --- a/src/MEDCoupling/MEDCouplingMemArray.cxx +++ b/src/MEDCoupling/MEDCouplingMemArray.cxx @@ -3511,6 +3511,61 @@ void DataArrayInt::computeOffsets() throw(INTERP_KERNEL::Exception) declareAsNew(); } +/*! + * This method works on array with number of component equal to one and allocated. If not an exception is thrown. + * 'offsets' should be monotic ascendently. If not, an exception will be thrown. + * This method retrives a newly created DataArrayInt instance with 1 component and this->getNumberOfTuples()-1 tuples. + * If 'this' contains [0,2,3] and 'offsets' [0,3,6,10,14,20] the returned array will contain [0,1,2,6,7,8,9,10,11,12,13] + */ +DataArrayInt *DataArrayInt::buildExplicitArrByRanges(const DataArrayInt *offsets) const throw(INTERP_KERNEL::Exception) +{ + checkAllocated(); + if(getNumberOfComponents()!=1) + throw INTERP_KERNEL::Exception("DataArrayInt::buildExplicitArrByRanges : only single component allowed !"); + offsets->checkAllocated(); + if(offsets->getNumberOfComponents()!=1) + throw INTERP_KERNEL::Exception("DataArrayInt::buildExplicitArrByRanges : input array should have only single component !"); + int othNbTuples=offsets->getNumberOfTuples()-1; + int nbOfTuples=getNumberOfTuples(); + int retNbOftuples=0; + const int *work=getConstPointer(); + const int *offPtr=offsets->getConstPointer(); + for(int i=0;i=0 && val=0) + retNbOftuples+=delta; + else + { + std::ostringstream oss; oss << "DataArrayInt::buildExplicitArrByRanges : Tuple #" << val << " of offset array has a delta < 0 !"; + throw INTERP_KERNEL::Exception(oss.str().c_str()); + } + } + else + { + std::ostringstream oss; oss << "DataArrayInt::buildExplicitArrByRanges : Tuple #" << i << " in this contains " << val; + oss << " whereas offsets array is of size " << othNbTuples+1 << " !"; + throw INTERP_KERNEL::Exception(oss.str().c_str()); + } + } + MEDCouplingAutoRefCountObjectPtr ret=DataArrayInt::New(); + ret->alloc(retNbOftuples,1); + int *retPtr=ret->getPointer(); + for(int i=0;iincrRef(); + return ret; +} + /*! * This method returns all different values found in 'this'. */ diff --git a/src/MEDCoupling/MEDCouplingMemArray.hxx b/src/MEDCoupling/MEDCouplingMemArray.hxx index 1fedd0fa3..907ff0e5b 100644 --- a/src/MEDCoupling/MEDCouplingMemArray.hxx +++ b/src/MEDCoupling/MEDCouplingMemArray.hxx @@ -340,6 +340,7 @@ namespace ParaMEDMEM MEDCOUPLING_EXPORT DataArrayInt *buildIntersection(const DataArrayInt *other) const throw(INTERP_KERNEL::Exception); MEDCOUPLING_EXPORT DataArrayInt *deltaShiftIndex() const throw(INTERP_KERNEL::Exception); MEDCOUPLING_EXPORT void computeOffsets() throw(INTERP_KERNEL::Exception); + MEDCOUPLING_EXPORT DataArrayInt *buildExplicitArrByRanges(const DataArrayInt *offsets) const throw(INTERP_KERNEL::Exception); MEDCOUPLING_EXPORT std::set getDifferentValues() const throw(INTERP_KERNEL::Exception); MEDCOUPLING_EXPORT void useArray(const int *array, bool ownership, DeallocType type, int nbOfTuple, int nbOfCompo); MEDCOUPLING_EXPORT void writeOnPlace(int id, int element0, const int *others, int sizeOfOthers) { _mem.writeOnPlace(id,element0,others,sizeOfOthers); } diff --git a/src/MEDCoupling/Test/MEDCouplingBasicsTest.hxx b/src/MEDCoupling/Test/MEDCouplingBasicsTest.hxx index 8add2d0d3..c80798630 100644 --- a/src/MEDCoupling/Test/MEDCouplingBasicsTest.hxx +++ b/src/MEDCoupling/Test/MEDCouplingBasicsTest.hxx @@ -225,6 +225,7 @@ namespace ParaMEDMEM CPPUNIT_TEST( testDAITransformWithIndArrR1 ); CPPUNIT_TEST( testDAISplitByValueRange1 ); CPPUNIT_TEST( testUMeshSplitProfilePerType1 ); + CPPUNIT_TEST( testDAIBuildExplicitArrByRanges1 ); //MEDCouplingBasicsTestInterp.cxx CPPUNIT_TEST( test2DInterpP0P0_1 ); CPPUNIT_TEST( test2DInterpP0P0PL_1 ); @@ -476,6 +477,7 @@ namespace ParaMEDMEM void testDAITransformWithIndArrR1(); void testDAISplitByValueRange1(); void testUMeshSplitProfilePerType1(); + void testDAIBuildExplicitArrByRanges1(); //MEDCouplingBasicsTestInterp.cxx void test2DInterpP0P0_1(); void test2DInterpP0P0PL_1(); diff --git a/src/MEDCoupling/Test/MEDCouplingBasicsTest4.cxx b/src/MEDCoupling/Test/MEDCouplingBasicsTest4.cxx index dbaf613f0..b7d2ce94f 100644 --- a/src/MEDCoupling/Test/MEDCouplingBasicsTest4.cxx +++ b/src/MEDCoupling/Test/MEDCouplingBasicsTest4.cxx @@ -1431,3 +1431,26 @@ void MEDCouplingBasicsTest::testUMeshSplitProfilePerType1() // m->decrRef(); } + +void MEDCouplingBasicsTest::testDAIBuildExplicitArrByRanges1() +{ + DataArrayInt *d=DataArrayInt::New(); + d->alloc(3,1); + const int vals1[3]={0,2,3}; + std::copy(vals1,vals1+3,d->getPointer()); + DataArrayInt *e=DataArrayInt::New(); + e->alloc(6,1); + const int vals2[6]={0,3,6,10,14,20}; + std::copy(vals2,vals2+6,e->getPointer()); + // + DataArrayInt *f=d->buildExplicitArrByRanges(e); + CPPUNIT_ASSERT_EQUAL(11,f->getNumberOfTuples()); + CPPUNIT_ASSERT_EQUAL(1,f->getNumberOfComponents()); + const int expected1[11]={0,1,2,6,7,8,9,10,11,12,13}; + for(int i=0;i<11;i++) + CPPUNIT_ASSERT_EQUAL(expected1[i],f->getIJ(i,0)); + // + f->decrRef(); + e->decrRef(); + d->decrRef(); +} -- 2.39.2