]> SALOME platform Git repositories - tools/medcoupling.git/commitdiff
Salome HOME
DataArrayInt::accumulate
authorageay <ageay>
Fri, 25 Jan 2013 15:54:13 +0000 (15:54 +0000)
committerageay <ageay>
Fri, 25 Jan 2013 15:54:13 +0000 (15:54 +0000)
src/MEDCoupling/MEDCouplingMemArray.cxx
src/MEDCoupling/MEDCouplingMemArray.hxx
src/MEDCoupling_Swig/MEDCouplingBasicsTest.py
src/MEDCoupling_Swig/MEDCouplingCommon.i

index 08105454b8514b56d4c7677edd8c0c0d82f0b035..363310fca39b682920668fa16bd5a924971352d1 100644 (file)
@@ -5184,6 +5184,32 @@ bool DataArrayInt::presenceOfValue(const std::vector<int>& vals) const throw(INT
   return locateValue(vals)!=-1;
 }
 
+
+void DataArrayInt::accumulate(int *res) const throw(INTERP_KERNEL::Exception)
+{
+  checkAllocated();
+  const int *ptr=getConstPointer();
+  int nbTuple=getNumberOfTuples();
+  int nbComps=getNumberOfComponents();
+  std::fill(res,res+nbComps,0);
+  for(int i=0;i<nbTuple;i++)
+    std::transform(ptr+i*nbComps,ptr+(i+1)*nbComps,res,res,std::plus<int>());
+}
+
+int DataArrayInt::accumulate(int compId) const throw(INTERP_KERNEL::Exception)
+{
+  checkAllocated();
+  const int *ptr=getConstPointer();
+  int nbTuple=getNumberOfTuples();
+  int nbComps=getNumberOfComponents();
+  if(compId>=nbComps)
+    throw INTERP_KERNEL::Exception("DataArrayInt::accumulate : Invalid compId specified : No such nb of components !");
+  int ret=0;
+  for(int i=0;i<nbTuple;i++)
+    ret+=ptr[i*nbComps+compId];
+  return ret;
+}
+
 DataArrayInt *DataArrayInt::Aggregate(const DataArrayInt *a1, const DataArrayInt *a2, int offsetA2)
 {
   if(!a1 || !a2)
index 73b2215d7402a168d09837fed249fd91a11460b1..d29903295a5ed32cccc25fdfc8abd7ee4677f03b 100644 (file)
@@ -463,6 +463,8 @@ namespace ParaMEDMEM
     MEDCOUPLING_EXPORT bool presenceOfTuple(const std::vector<int>& tupl) const throw(INTERP_KERNEL::Exception);
     MEDCOUPLING_EXPORT bool presenceOfValue(int value) const throw(INTERP_KERNEL::Exception);
     MEDCOUPLING_EXPORT bool presenceOfValue(const std::vector<int>& vals) const throw(INTERP_KERNEL::Exception);
+    MEDCOUPLING_EXPORT void accumulate(int *res) const throw(INTERP_KERNEL::Exception);
+    MEDCOUPLING_EXPORT int accumulate(int compId) const throw(INTERP_KERNEL::Exception);
     MEDCOUPLING_EXPORT int getMaxValue(int& tupleId) const throw(INTERP_KERNEL::Exception);
     MEDCOUPLING_EXPORT int getMaxValueInArray() const throw(INTERP_KERNEL::Exception);
     MEDCOUPLING_EXPORT int getMinValue(int& tupleId) const throw(INTERP_KERNEL::Exception);
index 8739c3ef185a8a1ff0fae3e221c76fafc26f1d69..e72abcdf70fa597a8cc7f9195d9d0d14b9aa0391 100644 (file)
@@ -10853,6 +10853,25 @@ class MEDCouplingBasicsTest(unittest.TestCase):
         self.assertTrue(m1.checkConsecutiveCellTypesForMEDFileFrmt())
         pass
 
+    def testSwigDAAccumulate1(self):
+        d=DataArrayInt(10) ; d.iota(0)
+        self.assertEqual([45],d.accumulate())
+        self.assertEqual(45,d.accumulate(0))
+        d=DataArrayInt(30) ; d.iota(0) ; d.rearrange(3)
+        self.assertEqual([135,145,155],d.accumulate())
+        self.assertEqual(135,d.accumulate(0))
+        self.assertEqual(145,d.accumulate(1))
+        self.assertEqual(155,d.accumulate(2))
+        d=DataArrayDouble(10) ; d.iota(0.)
+        self.assertEqual([45.],d.accumulate())
+        self.assertEqual(45.,d.accumulate(0))
+        d=DataArrayDouble(30) ; d.iota(0) ; d.rearrange(3)
+        self.assertEqual([135.,145.,155.],d.accumulate())
+        self.assertEqual(135.,d.accumulate(0))
+        self.assertEqual(145.,d.accumulate(1))
+        self.assertEqual(155.,d.accumulate(2))
+        pass
+
     def setUp(self):
         pass
     pass
index ecd13e55558eb6cbfecd42edee54350b8bdfa4b0..4ebecefac7ea8226b9a725eb3b0d1f60e5259022 100644 (file)
@@ -4624,7 +4624,15 @@ namespace ParaMEDMEM
    {
      return self->iterator();
    }
-
+   
+   PyObject *accumulate() const throw(INTERP_KERNEL::Exception)
+   {
+     int sz=self->getNumberOfComponents();
+     INTERP_KERNEL::AutoPtr<int> tmp=new int[sz];
+     self->accumulate(tmp);
+     return convertIntArrToPyList(tmp,sz);
+   }
+   
    static PyObject *BuildOld2NewArrayFromSurjectiveFormat2(int nbOfOldTuples, PyObject *arr, PyObject *arrI) throw(INTERP_KERNEL::Exception)
    {
      int newNbOfTuples=-1;