From: Anthony Geay Date: Mon, 15 Mar 2021 20:39:55 +0000 (+0100) Subject: DataArrayInt32.convertToInt64Arr and DataArrayInt64.convertToInt32Arr X-Git-Tag: V9_7_0a1~15 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=7a534e651373ca9ac86ab614aff0ade5b52e4539;p=tools%2Fmedcoupling.git DataArrayInt32.convertToInt64Arr and DataArrayInt64.convertToInt32Arr --- diff --git a/src/MEDCoupling/MEDCouplingMemArray.cxx b/src/MEDCoupling/MEDCouplingMemArray.cxx index 6092bcd2e..047c52f79 100755 --- a/src/MEDCoupling/MEDCouplingMemArray.cxx +++ b/src/MEDCoupling/MEDCouplingMemArray.cxx @@ -3625,6 +3625,17 @@ DataArrayInt32 *DataArrayInt32::deepCopy() const return new DataArrayInt32(*this); } +MCAuto DataArrayInt32::convertToInt64Arr() const +{ + this->checkAllocated(); + MCAuto ret(DataArrayInt64::New()); + ret->alloc(this->getNumberOfTuples(),this->getNumberOfComponents()); + ret->copyStringInfoFrom(*this); + const std::int32_t *pt(this->begin()); + std::for_each(ret->getPointer(),ret->getPointer()+ret->getNbOfElems(),[&pt](std::int64_t& val) { val = std::int64_t(*pt++); }); + return ret; +} + DataArrayInt32Iterator *DataArrayInt32::iterator() { return new DataArrayInt32Iterator(this); @@ -3664,6 +3675,17 @@ DataArrayInt32 *DataArrayInt32Tuple::buildDAInt(std::size_t nbOfTuples, std::siz return this->buildDA(nbOfTuples,nbOfCompo); } +MCAuto DataArrayInt64::convertToInt32Arr() const +{ + this->checkAllocated(); + MCAuto ret(DataArrayInt32::New()); + ret->alloc(this->getNumberOfTuples(),this->getNumberOfComponents()); + ret->copyStringInfoFrom(*this); + const std::int64_t *pt(this->begin()); + std::for_each(ret->getPointer(),ret->getPointer()+ret->getNbOfElems(),[&pt](std::int32_t& val) { val = std::int32_t(*pt++); }); + return ret; +} + DataArrayInt64Iterator *DataArrayInt64::iterator() { return new DataArrayInt64Iterator(this); diff --git a/src/MEDCoupling/MEDCouplingMemArray.hxx b/src/MEDCoupling/MEDCouplingMemArray.hxx index 8412f3843..fc9dbe9c4 100755 --- a/src/MEDCoupling/MEDCouplingMemArray.hxx +++ b/src/MEDCoupling/MEDCouplingMemArray.hxx @@ -718,6 +718,7 @@ namespace MEDCoupling DataArrayInt32 *deepCopy() const; DataArrayInt32 *copySorted(bool asc=true) const override { return this->copySortedImpl(asc); } DataArrayInt32 *buildNewEmptyInstance() const { return DataArrayInt32::New(); } + MCAuto convertToInt64Arr() const; public: DataArrayInt32 *selectByTupleId(const mcIdType *new2OldBg, const mcIdType *new2OldEnd) const { return this->mySelectByTupleId(new2OldBg,new2OldEnd); } DataArrayInt32 *selectByTupleId(const DataArrayIdType& di) const { return this->mySelectByTupleId(di); } @@ -740,6 +741,7 @@ namespace MEDCoupling DataArrayInt64 *deepCopy() const; DataArrayInt64 *copySorted(bool asc=true) const override { return this->copySortedImpl(asc); } DataArrayInt64 *buildNewEmptyInstance() const { return DataArrayInt64::New(); }//ok + MCAuto convertToInt32Arr() const; public: DataArrayInt64 *selectByTupleId(const mcIdType *new2OldBg, const mcIdType *new2OldEnd) const { return this->mySelectByTupleId(new2OldBg,new2OldEnd); } DataArrayInt64 *selectByTupleId(const DataArrayIdType& di) const { return this->mySelectByTupleId(di); } diff --git a/src/MEDCoupling_Swig/DataArrayInt.i b/src/MEDCoupling_Swig/DataArrayInt.i index e0c0070cd..1ec131f36 100644 --- a/src/MEDCoupling_Swig/DataArrayInt.i +++ b/src/MEDCoupling_Swig/DataArrayInt.i @@ -2573,6 +2573,11 @@ namespace MEDCoupling ARRAYDEF( DataArrayInt32, Int32 ) }; %extend DataArrayInt32 { + DataArrayInt64 *convertToInt64Arr() const + { + MCAuto ret(self->convertToInt64Arr()); + return ret.retn(); + } #ifdef WITH_NUMPY PyObject *toNumPyArray() // not const. It is not a bug ! { @@ -2599,6 +2604,11 @@ namespace MEDCoupling ARRAYDEF( DataArrayInt64, Int64 ) }; %extend DataArrayInt64 { + DataArrayInt32 *convertToInt32Arr() const + { + MCAuto ret(self->convertToInt32Arr()); + return ret.retn(); + } #ifdef WITH_NUMPY PyObject *toNumPyArray() // not const. It is not a bug ! { diff --git a/src/MEDCoupling_Swig/MEDCouplingBasicsTest7.py b/src/MEDCoupling_Swig/MEDCouplingBasicsTest7.py index bee9d2b3b..961a85717 100644 --- a/src/MEDCoupling_Swig/MEDCouplingBasicsTest7.py +++ b/src/MEDCoupling_Swig/MEDCouplingBasicsTest7.py @@ -936,6 +936,26 @@ class MEDCouplingBasicsTest7(unittest.TestCase): m.checkConsistency() m.checkGeomConsistency() # now m is OK + def testInt32Int64Arr0(self): + n = 30 + arr = DataArrayInt32(n) + arr.iota() ; arr.rearrange(3) + comps = ["a","bb","ccc"] + name = "aaa" + arr.setName(name) ; arr.setInfoOnComponents(comps) + self.assertEqual(arr.accumulate(),[135, 145, 155]) + arr2 = arr.convertToInt64Arr() # test is here + self.assertEqual(arr2.accumulate(),[135, 145, 155]) + self.assertTrue(isinstance(arr2,DataArrayInt64)) + self.assertEqual(arr2.getName(),name) + self.assertEqual(arr2.getInfoOnComponents(),comps) + arr3 = arr2.convertToInt32Arr() # test is here + self.assertEqual(arr3.accumulate(),[135, 145, 155]) + self.assertTrue(isinstance(arr3,DataArrayInt32)) + self.assertEqual(arr3.getName(),name) + self.assertEqual(arr3.getInfoOnComponents(),comps) + self.assertTrue(arr3.isEqual(arr)) + pass if __name__ == '__main__': diff --git a/src/MEDCoupling_Swig/MEDCouplingMemArray.i b/src/MEDCoupling_Swig/MEDCouplingMemArray.i index 5325ef184..961d62b2e 100644 --- a/src/MEDCoupling_Swig/MEDCouplingMemArray.i +++ b/src/MEDCoupling_Swig/MEDCouplingMemArray.i @@ -159,6 +159,7 @@ %newobject MEDCoupling::DataArrayInt32::__rmod__; %newobject MEDCoupling::DataArrayInt32::__pow__; %newobject MEDCoupling::DataArrayInt32::__rpow__; +%newobject MEDCoupling::DataArrayInt32::convertToInt64Arr; %newobject MEDCoupling::DataArrayInt32Tuple::buildDAInt; %newobject MEDCoupling::DataArrayInt64::New; %newobject MEDCoupling::DataArrayInt64::__iter__; @@ -234,6 +235,7 @@ %newobject MEDCoupling::DataArrayInt64::__rmod__; %newobject MEDCoupling::DataArrayInt64::__pow__; %newobject MEDCoupling::DataArrayInt64::__rpow__; +%newobject MEDCoupling::DataArrayInt64::convertToInt32Arr; %newobject MEDCoupling::DataArrayInt64Tuple::buildDAInt; %newobject MEDCoupling::DataArrayChar::convertToIntArr; %newobject MEDCoupling::DataArrayChar::renumber;