From 2c20d49eb71282d4617e2784c66139e69de42978 Mon Sep 17 00:00:00 2001 From: ageay Date: Thu, 26 Apr 2012 06:33:17 +0000 Subject: [PATCH] DataArrayInt::Range. --- src/MEDCoupling/MEDCouplingMemArray.cxx | 40 ++++++++++++ src/MEDCoupling/MEDCouplingMemArray.hxx | 2 + .../Test/MEDCouplingBasicsTest5.cxx | 64 +++++++++++++++++++ .../Test/MEDCouplingBasicsTest5.hxx | 2 + src/MEDCoupling_Swig/MEDCoupling.i | 1 + src/MEDCoupling_Swig/MEDCouplingBasicsTest.py | 54 ++++++++++++++++ 6 files changed, 163 insertions(+) diff --git a/src/MEDCoupling/MEDCouplingMemArray.cxx b/src/MEDCoupling/MEDCouplingMemArray.cxx index 63d64bcab..0f10c59de 100644 --- a/src/MEDCoupling/MEDCouplingMemArray.cxx +++ b/src/MEDCoupling/MEDCouplingMemArray.cxx @@ -315,6 +315,26 @@ int DataArray::GetNumberOfItemGivenBES(int begin, int end, int step, const char return (end-1-begin)/step+1; } +int DataArray::GetNumberOfItemGivenBESRelative(int begin, int end, int step, const char *msg) throw(INTERP_KERNEL::Exception) +{ + if(step==0) + throw INTERP_KERNEL::Exception("DataArray::GetNumberOfItemGivenBES : step=0 is not allowed !"); + if(end0) + { + std::ostringstream oss; oss << msg << " : end before begin whereas step is positive !"; + throw INTERP_KERNEL::Exception(oss.str().c_str()); + } + if(begin 0 !"; + throw INTERP_KERNEL::Exception(oss.str().c_str()); + } + if(begin!=end) + return (std::max(begin,end)-1-std::min(begin,end))/std::abs(step)+1; + else + return 0; +} + DataArrayDouble *DataArrayDouble::New() { return new DataArrayDouble; @@ -5176,6 +5196,26 @@ int *DataArrayInt::CheckAndPreparePermutation(const int *start, const int *end) return ret; } +DataArrayInt *DataArrayInt::Range(int begin, int end, int step) throw(INTERP_KERNEL::Exception) +{ + int nbOfTuples=GetNumberOfItemGivenBESRelative(begin,end,step,"DataArrayInt::Range"); + MEDCouplingAutoRefCountObjectPtr ret=DataArrayInt::New(); + ret->alloc(nbOfTuples,1); + int *ptr=ret->getPointer(); + if(step>0) + { + for(int i=begin;iend;i+=step,ptr++) + *ptr=i; + } + ret->incrRef(); + return ret; +} + /*! * Useless method for end user. Only for MPI/Corba/File serialsation for multi arrays class. * Server side. diff --git a/src/MEDCoupling/MEDCouplingMemArray.hxx b/src/MEDCoupling/MEDCouplingMemArray.hxx index ac6a4be40..39c71433b 100644 --- a/src/MEDCoupling/MEDCouplingMemArray.hxx +++ b/src/MEDCoupling/MEDCouplingMemArray.hxx @@ -111,6 +111,7 @@ namespace ParaMEDMEM MEDCOUPLING_EXPORT void checkNbOfTuplesAndComp(int nbOfTuples, int nbOfCompo, const char *msg) const throw(INTERP_KERNEL::Exception); MEDCOUPLING_EXPORT void checkNbOfElems(int nbOfElems, const char *msg) const throw(INTERP_KERNEL::Exception); MEDCOUPLING_EXPORT static int GetNumberOfItemGivenBES(int begin, int end, int step, const char *msg) throw(INTERP_KERNEL::Exception); + MEDCOUPLING_EXPORT static int GetNumberOfItemGivenBESRelative(int begin, int end, int step, const char *msg) throw(INTERP_KERNEL::Exception); MEDCOUPLING_EXPORT static std::string GetVarNameFromInfo(const std::string& info) throw(INTERP_KERNEL::Exception); MEDCOUPLING_EXPORT static std::string GetUnitFromInfo(const std::string& info) throw(INTERP_KERNEL::Exception); protected: @@ -444,6 +445,7 @@ namespace ParaMEDMEM MEDCOUPLING_EXPORT void updateTime() const { } public: MEDCOUPLING_EXPORT static int *CheckAndPreparePermutation(const int *start, const int *end); + MEDCOUPLING_EXPORT static DataArrayInt *Range(int begin, int end, int step) throw(INTERP_KERNEL::Exception); public: MEDCOUPLING_EXPORT void getTinySerializationIntInformation(std::vector& tinyInfo) const; MEDCOUPLING_EXPORT void getTinySerializationStrInformation(std::vector& tinyInfo) const; diff --git a/src/MEDCoupling/Test/MEDCouplingBasicsTest5.cxx b/src/MEDCoupling/Test/MEDCouplingBasicsTest5.cxx index 6e49c0443..76a6da66d 100644 --- a/src/MEDCoupling/Test/MEDCouplingBasicsTest5.cxx +++ b/src/MEDCoupling/Test/MEDCouplingBasicsTest5.cxx @@ -841,3 +841,67 @@ void MEDCouplingBasicsTest5::testCheckButterflyCellsBug1() // mesh2D->decrRef(); } + +void MEDCouplingBasicsTest5::testDataArrayIntRange1() +{ + DataArrayInt *d=DataArrayInt::Range(2,17,7); + const int expected1[3]={2,9,16}; + CPPUNIT_ASSERT_EQUAL(3,d->getNumberOfTuples()); + CPPUNIT_ASSERT_EQUAL(1,d->getNumberOfComponents()); + CPPUNIT_ASSERT(std::equal(expected1,expected1+3,d->getConstPointer())); + d->decrRef(); + // + d=DataArrayInt::Range(2,23,7); + CPPUNIT_ASSERT_EQUAL(3,d->getNumberOfTuples()); + CPPUNIT_ASSERT_EQUAL(1,d->getNumberOfComponents()); + CPPUNIT_ASSERT(std::equal(expected1,expected1+3,d->getConstPointer())); + d->decrRef(); + // + d=DataArrayInt::Range(2,24,7); + const int expected2[4]={2,9,16,23}; + CPPUNIT_ASSERT_EQUAL(4,d->getNumberOfTuples()); + CPPUNIT_ASSERT_EQUAL(1,d->getNumberOfComponents()); + CPPUNIT_ASSERT(std::equal(expected2,expected2+4,d->getConstPointer())); + d->decrRef(); + // + d=DataArrayInt::Range(24,2,-7); + const int expected3[4]={24,17,10,3}; + CPPUNIT_ASSERT_EQUAL(4,d->getNumberOfTuples()); + CPPUNIT_ASSERT_EQUAL(1,d->getNumberOfComponents()); + CPPUNIT_ASSERT(std::equal(expected3,expected3+4,d->getConstPointer())); + d->decrRef(); + // + d=DataArrayInt::Range(23,2,-7); + const int expected4[3]={23,16,9}; + CPPUNIT_ASSERT_EQUAL(3,d->getNumberOfTuples()); + CPPUNIT_ASSERT_EQUAL(1,d->getNumberOfComponents()); + CPPUNIT_ASSERT(std::equal(expected4,expected4+3,d->getConstPointer())); + d->decrRef(); + // + d=DataArrayInt::Range(23,22,-7); + CPPUNIT_ASSERT_EQUAL(1,d->getNumberOfTuples()); + CPPUNIT_ASSERT_EQUAL(1,d->getNumberOfComponents()); + CPPUNIT_ASSERT_EQUAL(23,d->getIJ(0,0)); + d->decrRef(); + // + d=DataArrayInt::Range(22,23,7); + CPPUNIT_ASSERT_EQUAL(1,d->getNumberOfTuples()); + CPPUNIT_ASSERT_EQUAL(1,d->getNumberOfComponents()); + CPPUNIT_ASSERT_EQUAL(22,d->getIJ(0,0)); + d->decrRef(); + // + d=DataArrayInt::Range(22,22,7); + CPPUNIT_ASSERT_EQUAL(0,d->getNumberOfTuples()); + CPPUNIT_ASSERT_EQUAL(1,d->getNumberOfComponents()); + d->decrRef(); + // + d=DataArrayInt::Range(22,22,-7); + CPPUNIT_ASSERT_EQUAL(0,d->getNumberOfTuples()); + CPPUNIT_ASSERT_EQUAL(1,d->getNumberOfComponents()); + d->decrRef(); + // + CPPUNIT_ASSERT_THROW(DataArrayInt::Range(22,23,-7),INTERP_KERNEL::Exception); + CPPUNIT_ASSERT_THROW(DataArrayInt::Range(23,22,7),INTERP_KERNEL::Exception); + CPPUNIT_ASSERT_THROW(DataArrayInt::Range(23,22,0),INTERP_KERNEL::Exception); + CPPUNIT_ASSERT_THROW(DataArrayInt::Range(22,23,0),INTERP_KERNEL::Exception); +} diff --git a/src/MEDCoupling/Test/MEDCouplingBasicsTest5.hxx b/src/MEDCoupling/Test/MEDCouplingBasicsTest5.hxx index 8ac380b1a..31a33dbff 100644 --- a/src/MEDCoupling/Test/MEDCouplingBasicsTest5.hxx +++ b/src/MEDCoupling/Test/MEDCouplingBasicsTest5.hxx @@ -50,6 +50,7 @@ namespace ParaMEDMEM CPPUNIT_TEST( testRenumberNodesInConn1 ); CPPUNIT_TEST( testComputeNeighborsOfCells1 ); CPPUNIT_TEST( testCheckButterflyCellsBug1 ); + CPPUNIT_TEST( testDataArrayIntRange1 ); CPPUNIT_TEST_SUITE_END(); public: void testUMeshTessellate2D1(); @@ -67,6 +68,7 @@ namespace ParaMEDMEM void testRenumberNodesInConn1(); void testComputeNeighborsOfCells1(); void testCheckButterflyCellsBug1(); + void testDataArrayIntRange1(); }; } diff --git a/src/MEDCoupling_Swig/MEDCoupling.i b/src/MEDCoupling_Swig/MEDCoupling.i index 5698dc976..eb7e0fffa 100644 --- a/src/MEDCoupling_Swig/MEDCoupling.i +++ b/src/MEDCoupling_Swig/MEDCoupling.i @@ -146,6 +146,7 @@ using namespace INTERP_KERNEL; %newobject ParaMEDMEM::DataArrayInt::Divide; %newobject ParaMEDMEM::DataArrayInt::BuildUnion; %newobject ParaMEDMEM::DataArrayInt::BuildIntersection; +%newobject ParaMEDMEM::DataArrayInt::Range; %newobject ParaMEDMEM::DataArrayInt::fromNoInterlace; %newobject ParaMEDMEM::DataArrayInt::toNoInterlace; %newobject ParaMEDMEM::DataArrayInt::buildComplement; diff --git a/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py b/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py index 4488ecc63..56436244e 100644 --- a/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py +++ b/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py @@ -9769,6 +9769,60 @@ class MEDCouplingBasicsTest(unittest.TestCase): self.assertTrue(v.empty()); pass + def testDataArrayIntRange1(self): + d=DataArrayInt.Range(2,17,7); + expected1=[2,9,16] + self.assertEqual(3,d.getNumberOfTuples()); + self.assertEqual(1,d.getNumberOfComponents()); + self.assertTrue(expected1,d.getValues()); + # + d=DataArrayInt.Range(2,23,7); + self.assertEqual(3,d.getNumberOfTuples()); + self.assertEqual(1,d.getNumberOfComponents()); + self.assertTrue(expected1,d.getValues()); + # + d=DataArrayInt.Range(2,24,7); + expected2=[2,9,16,23] + self.assertEqual(4,d.getNumberOfTuples()); + self.assertEqual(1,d.getNumberOfComponents()); + self.assertTrue(expected2,d.getValues()); + # + d=DataArrayInt.Range(24,2,-7); + expected3=[24,17,10,3] + self.assertEqual(4,d.getNumberOfTuples()); + self.assertEqual(1,d.getNumberOfComponents()); + self.assertTrue(expected3,d.getValues()); + # + d=DataArrayInt.Range(23,2,-7); + expected4=[23,16,9] + self.assertEqual(3,d.getNumberOfTuples()); + self.assertEqual(1,d.getNumberOfComponents()); + self.assertTrue(expected4,d.getValues()); + # + d=DataArrayInt.Range(23,22,-7); + self.assertEqual(1,d.getNumberOfTuples()); + self.assertEqual(1,d.getNumberOfComponents()); + self.assertEqual(23,d.getIJ(0,0)); + # + d=DataArrayInt.Range(22,23,7); + self.assertEqual(1,d.getNumberOfTuples()); + self.assertEqual(1,d.getNumberOfComponents()); + self.assertEqual(22,d.getIJ(0,0)); + # + d=DataArrayInt.Range(22,22,7); + self.assertEqual(0,d.getNumberOfTuples()); + self.assertEqual(1,d.getNumberOfComponents()); + # + d=DataArrayInt.Range(22,22,-7); + self.assertEqual(0,d.getNumberOfTuples()); + self.assertEqual(1,d.getNumberOfComponents()); + # + self.assertRaises(InterpKernelException,DataArrayInt.Range,22,23,-7); + self.assertRaises(InterpKernelException,DataArrayInt.Range,23,22,7); + self.assertRaises(InterpKernelException,DataArrayInt.Range,23,22,0); + self.assertRaises(InterpKernelException,DataArrayInt.Range,22,23,0); + pass + def setUp(self): pass pass -- 2.39.2