From cd69d73d3489c394f01b243d1d704fd2af6233b6 Mon Sep 17 00:00:00 2001 From: ageay Date: Wed, 31 Oct 2012 11:38:33 +0000 Subject: [PATCH] DataArrayInt::buildUnique --- src/MEDCoupling/MEDCouplingMemArray.cxx | 25 +++++++++++++++++++ src/MEDCoupling/MEDCouplingMemArray.hxx | 1 + .../Test/MEDCouplingBasicsTest5.cxx | 18 +++++++++++++ .../Test/MEDCouplingBasicsTest5.hxx | 2 ++ src/MEDCoupling_Swig/MEDCouplingBasicsTest.py | 6 +++++ src/MEDCoupling_Swig/MEDCouplingCommon.i | 1 + 6 files changed, 53 insertions(+) diff --git a/src/MEDCoupling/MEDCouplingMemArray.cxx b/src/MEDCoupling/MEDCouplingMemArray.cxx index c7f0a8fb9..d765f7ab4 100644 --- a/src/MEDCoupling/MEDCouplingMemArray.cxx +++ b/src/MEDCoupling/MEDCouplingMemArray.cxx @@ -28,6 +28,7 @@ #include #include #include +#include #include typedef double (*MYFUNCPTR)(double); @@ -5510,6 +5511,30 @@ DataArrayInt *DataArrayInt::buildIntersection(const DataArrayInt *other) const t return BuildIntersection(arrs); } +/*! + * This method can be applied on allocated with one component DataArrayInt instance. + * This method is typically relevant for sorted arrays. All consecutive duplicated items in \a this will appear only once in returned DataArrayInt instance. + * Example : if \a this contains [1,2,2,3,3,3,3,4,5,5,7,7,7,19] the returned array will contain [1,2,3,4,5,7,19] + * + * \return a newly allocated array that contain the result of the unique operation applied on \a this. + * \throw if \a this is not allocated or if \a this has not exactly one component. + */ +DataArrayInt *DataArrayInt::buildUnique() const throw(INTERP_KERNEL::Exception) +{ + checkAllocated(); + if(getNumberOfComponents()!=1) + throw INTERP_KERNEL::Exception("DataArrayInt::buildUnique : only single component allowed !"); + int nbOfTuples=getNumberOfTuples(); + MEDCouplingAutoRefCountObjectPtr tmp=deepCpy(); + int *data=tmp->getPointer(); + int *last=std::unique(data,data+nbOfTuples); + MEDCouplingAutoRefCountObjectPtr ret=DataArrayInt::New(); + ret->alloc(std::distance(data,last),1); + std::copy(data,last,ret->getPointer()); + ret->incrRef(); + return ret; +} + /*! * This method could be usefull for returned DataArrayInt marked as index. Some methods that generate such DataArrayInt instances: * - ParaMEDMEM::MEDCouplingUMesh::buildDescendingConnectivity diff --git a/src/MEDCoupling/MEDCouplingMemArray.hxx b/src/MEDCoupling/MEDCouplingMemArray.hxx index 603156745..8b009d047 100644 --- a/src/MEDCoupling/MEDCouplingMemArray.hxx +++ b/src/MEDCoupling/MEDCouplingMemArray.hxx @@ -459,6 +459,7 @@ namespace ParaMEDMEM MEDCOUPLING_EXPORT DataArrayInt *buildSubstraction(const DataArrayInt *other) const throw(INTERP_KERNEL::Exception); MEDCOUPLING_EXPORT DataArrayInt *buildUnion(const DataArrayInt *other) const throw(INTERP_KERNEL::Exception); MEDCOUPLING_EXPORT DataArrayInt *buildIntersection(const DataArrayInt *other) const throw(INTERP_KERNEL::Exception); + MEDCOUPLING_EXPORT DataArrayInt *buildUnique() const throw(INTERP_KERNEL::Exception); MEDCOUPLING_EXPORT DataArrayInt *deltaShiftIndex() const throw(INTERP_KERNEL::Exception); MEDCOUPLING_EXPORT void computeOffsets() throw(INTERP_KERNEL::Exception); MEDCOUPLING_EXPORT void computeOffsets2() throw(INTERP_KERNEL::Exception); diff --git a/src/MEDCoupling/Test/MEDCouplingBasicsTest5.cxx b/src/MEDCoupling/Test/MEDCouplingBasicsTest5.cxx index 352212aed..1068b74c0 100644 --- a/src/MEDCoupling/Test/MEDCouplingBasicsTest5.cxx +++ b/src/MEDCoupling/Test/MEDCouplingBasicsTest5.cxx @@ -1620,3 +1620,21 @@ void MEDCouplingBasicsTest5::testIntersect2DMeshesTmp5() m2->decrRef(); m1->decrRef(); } + +void MEDCouplingBasicsTest5::testDAIBuildUnique1() +{ + DataArrayInt *d=DataArrayInt::New(); + const int dData[14]={1,2,2,3,3,3,3,4,5,5,7,7,7,19}; + d->useArray(dData,false,CPP_DEALLOC,14,1); + const int expectedData[7]={1,2,3,4,5,7,19}; + // + DataArrayInt *e=d->buildUnique(); + CPPUNIT_ASSERT_EQUAL(7,e->getNumberOfTuples()); + CPPUNIT_ASSERT_EQUAL(1,e->getNumberOfComponents()); + for(int i=0;i<7;i++) + CPPUNIT_ASSERT_EQUAL(expectedData[i],e->getIJ(i,0)); + // + e->decrRef(); + d->decrRef(); +} + diff --git a/src/MEDCoupling/Test/MEDCouplingBasicsTest5.hxx b/src/MEDCoupling/Test/MEDCouplingBasicsTest5.hxx index 70337a7cc..8f4607cc9 100644 --- a/src/MEDCoupling/Test/MEDCouplingBasicsTest5.hxx +++ b/src/MEDCoupling/Test/MEDCouplingBasicsTest5.hxx @@ -67,6 +67,7 @@ namespace ParaMEDMEM CPPUNIT_TEST( testKrSpatialDiscretization1 ); CPPUNIT_TEST( testDuplicateEachTupleNTimes1 ); CPPUNIT_TEST( testIntersect2DMeshesTmp5 ); + CPPUNIT_TEST( testDAIBuildUnique1 ); CPPUNIT_TEST_SUITE_END(); public: void testUMeshTessellate2D1(); @@ -100,6 +101,7 @@ namespace ParaMEDMEM void testKrSpatialDiscretization1(); void testDuplicateEachTupleNTimes1(); void testIntersect2DMeshesTmp5(); + void testDAIBuildUnique1(); }; } diff --git a/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py b/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py index 8a89f9a65..0d4461be3 100644 --- a/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py +++ b/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py @@ -10265,6 +10265,12 @@ class MEDCouplingBasicsTest(unittest.TestCase): self.assertEqual(expected2,d2.getValues()) pass + def testDAIBuildUnique1(self): + d=DataArrayInt([1,2,2,3,3,3,3,4,5,5,7,7,7,19]) + e=d.buildUnique() + self.assertTrue(e.isEqual(DataArrayInt([1,2,3,4,5,7,19]))) + pass + def setUp(self): pass pass diff --git a/src/MEDCoupling_Swig/MEDCouplingCommon.i b/src/MEDCoupling_Swig/MEDCouplingCommon.i index 51afd5e27..d7ab7e4a1 100644 --- a/src/MEDCoupling_Swig/MEDCouplingCommon.i +++ b/src/MEDCoupling_Swig/MEDCouplingCommon.i @@ -162,6 +162,7 @@ using namespace INTERP_KERNEL; %newobject ParaMEDMEM::DataArrayInt::buildUnion; %newobject ParaMEDMEM::DataArrayInt::buildSubstraction; %newobject ParaMEDMEM::DataArrayInt::buildIntersection; +%newobject ParaMEDMEM::DataArrayInt::buildUnique; %newobject ParaMEDMEM::DataArrayInt::deltaShiftIndex; %newobject ParaMEDMEM::DataArrayInt::buildExplicitArrByRanges; %newobject ParaMEDMEM::DataArrayInt::findRangeIdForEachTuple; -- 2.39.2