Salome HOME
new sort function for the pairs list which can replace sortEachPairToMakeALinkedList duc/new_function
authorDUC ANH HOANG <duc-anh-externe.hoang@edf.fr>
Tue, 29 Mar 2022 10:18:36 +0000 (12:18 +0200)
committerDUC ANH HOANG <duc-anh-externe.hoang@edf.fr>
Tue, 29 Mar 2022 10:18:36 +0000 (12:18 +0200)
src/MEDCoupling/MEDCouplingMemArray.hxx
src/MEDCoupling/MEDCouplingMemArray.txx
src/MEDCoupling_Swig/DataArrayInt.i
src/MEDCoupling_Swig/MEDCouplingBasicsTest7.py

index 4850b965366cbddac3b081f7c9d5cbb93362abcc..0b190f7389ee29059832e2019ae402512e455e64 100755 (executable)
@@ -648,6 +648,7 @@ namespace MEDCoupling
     DataArrayIdType *findRangeIdForEachTuple(const DataArrayType *ranges) const;
     DataArrayType *findIdInRangeForEachTuple(const DataArrayType *ranges) const;
     void sortEachPairToMakeALinkedList();
+    void sortToHaveConsecutivePairs();
     MCAuto<DataArrayType> fromLinkedListOfPairToList() const;
     DataArrayType *getDifferentValues() const;
     std::vector<DataArrayIdType *> partitionByDifferentValues(std::vector<T>& differentIds) const;
index 750620d9a065bc28ee63268b70ae52c3d484ddad..0a750fb5da755b3b32a6de94a824ae01d03738d6 100755 (executable)
@@ -6248,7 +6248,7 @@ struct NotInRange
    *
    * This method is useful for users having an unstructured mesh having only SEG2 to rearrange internally the connectivity without any coordinates consideration.
    *
-   * \sa MEDCouplingUMesh::orderConsecutiveCells1D, DataArrayInt::fromLinkedListOfPairToList
+   * \sa MEDCouplingUMesh::orderConsecutiveCells1D, DataArrayInt::sortToHaveConsecutivePairs(), DataArrayInt::fromLinkedListOfPairToList
    */
   template <class T>
   void DataArrayDiscrete<T>::sortEachPairToMakeALinkedList()
@@ -6269,6 +6269,11 @@ struct NotInRange
                 std::ostringstream oss; oss << "DataArrayInt::sortEachPairToMakeALinkedList : In the tuple #" << i << " presence of a pair filled with same ids !";
                 throw INTERP_KERNEL::Exception(oss.str().c_str());
               }
+            if(conn[2]!=conn[1] && conn[3]!=conn[1])
+              {
+                std::ostringstream oss; oss << "DataArrayInt::sortEachPairToMakeALinkedList : There is no common noeud between the tuple #" << i << " and the tuple #" << i + 1 << ". Need call DataArrayInt::sortToHaveConsecutivePairs() ";
+                throw INTERP_KERNEL::Exception(oss.str().c_str());
+              }
             if(conn[2]!=conn[1] && conn[3]==conn[1] && conn[2]!=conn[0])
               std::swap(conn[2],conn[3]);
             //not(conn[2]==conn[1] && conn[3]!=conn[1] && conn[3]!=conn[0])
@@ -6307,6 +6312,57 @@ struct NotInRange
       }
   }
 
+  /*!
+   * This method is the improvement from the method sortEachPairToMakeALinkedList().
+   *
+   * \sa MEDCouplingUMesh::orderConsecutiveCells1D, DataArrayInt::sortEachPairToMakeALinkedList(), DataArrayInt::fromLinkedListOfPairToList
+   */
+  template <class T>
+  void DataArrayDiscrete<T>::sortToHaveConsecutivePairs()
+  {
+    this->checkAllocated();
+    if(this->getNumberOfComponents()!=2)
+      throw INTERP_KERNEL::Exception("DataArrayInt::sortToHaveConsecutivePairs : Only works on DataArrayInt instance with nb of components equal to 2 !");
+    mcIdType nbOfTuples(this->getNumberOfTuples());
+    T *thisPtr(this->getPointer());
+    mcIdType idOfLastTuple = 0;
+    std::pair<T*,T*> tmp;
+    if(thisPtr[0]==thisPtr[1])
+      {
+        THROW_IK_EXCEPTION("DataArrayInt::sortToHaveConsecutivePairs : In the first tuple presence of a pair filled with same ids !");
+      }
+    while(idOfLastTuple < nbOfTuples-1)
+    {
+      mcIdType i = idOfLastTuple+1;
+      tmp.first = &thisPtr[2*i];
+      tmp.second = &thisPtr[2*i+1];
+      if(std::get<0>(tmp)[0] == std::get<1>(tmp)[0])
+        {
+          THROW_IK_EXCEPTION("DataArrayInt::sortToHaveConsecutivePairs : In the tuple #" << i << " presence of a pair filled with same ids !");
+        }
+      while((this->getIJ(idOfLastTuple,1) != std::get<0>(tmp)[0] &&
+            this->getIJ(idOfLastTuple,1) != std::get<1>(tmp)[0]) &&
+            i < nbOfTuples-1)
+        {
+          std::swap(std::get<0>(tmp)[0],thisPtr[2*(i+1)]);
+          std::swap(std::get<1>(tmp)[0],thisPtr[2*(i+1)+1]);
+          i++;
+        }
+      if(i < nbOfTuples-1 ||
+         this->getIJ(idOfLastTuple,1) == std::get<0>(tmp)[0] ||
+         this->getIJ(idOfLastTuple,1) == std::get<1>(tmp)[0])
+      {
+        if(this->getIJ(idOfLastTuple,1) != std::get<0>(tmp)[0])
+          std::swap(std::get<0>(tmp)[0],std::get<1>(tmp)[0]);
+        idOfLastTuple++;
+      }
+      else
+      {
+        THROW_IK_EXCEPTION("DataArrayInt::sortToHaveConsecutivePairs : not found the tuple which have the common noeud = " << this->getIJ(idOfLastTuple,1));
+      }
+    }
+  }
+
   /*!
    * \a this is expected to be a correctly linked list of pairs.
    *
index a314cb62d660e1fda24f538a9ec09da565464e80..65ee9f36e950cc0e1d7bbc167d8fb588743386e5 100644 (file)
     DataArrayIdType *findRangeIdForEachTuple(const ARRAY *ranges) const;
     ARRAY *findIdInRangeForEachTuple(const ARRAY *ranges) const;
     void sortEachPairToMakeALinkedList();
+    void sortToHaveConsecutivePairs();
     ARRAY *duplicateEachTupleNTimes(mcIdType nbTimes) const;
     ARRAY *getDifferentValues() const;
     static ARRAY *Add(const ARRAY *a1, const ARRAY *a2);
index 7af9a27e01def0d3cc98fd32a2a5441c620230f7..98fa9ed4fc1d52e65cbe624107b29a9b1e4befc1 100644 (file)
@@ -665,6 +665,12 @@ class MEDCouplingBasicsTest7(unittest.TestCase):
         self.assertRaises(InterpKernelException,a.indicesOfSubPart,f) # 12 in f does not exist in a
         pass
 
+    def testDAIsortToHaveConsecutivePairs(self):
+        dref=DataArrayInt64([(6, 216), (216, 218), (218, 220), (220, 222), (222, 224), (224, 226)])
+        dtest=DataArrayInt64([(6, 216), (218, 216), (224, 226), (222, 220), (218, 220), (222, 224)])
+        dtest.sortToHaveConsecutivePairs()
+        self.assertTrue(dtest.isEqual(dref))
+
     def testDAIFromLinkedListOfPairToList1(self):
         d=DataArrayInt64([(5,7),(7,3),(3,12),(12,17)])
         zeRes=DataArrayInt64([5,7,3,12,17])