Salome HOME
Preparation of intermediate revision
[modules/geom.git] / src / GEOMAlgo / GEOMAlgo_PassKey.cxx
index 54a392cf7ef0ef8482c8970194eb60aeb3da74ce..254ca6e595cbb85ce1705849847d61d794197bf8 100755 (executable)
@@ -1,22 +1,43 @@
-// File:       GEOMAlgo_PassKey.cxx
-// Created:    
+//  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
+//
+//  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
+//  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
+//
+//  This library is free software; you can redistribute it and/or
+//  modify it under the terms of the GNU Lesser General Public
+//  License as published by the Free Software Foundation; either
+//  version 2.1 of the License.
+//
+//  This library is distributed in the hope that it will be useful,
+//  but WITHOUT ANY WARRANTY; without even the implied warranty of
+//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+//  Lesser General Public License for more details.
+//
+//  You should have received a copy of the GNU Lesser General Public
+//  License along with this library; if not, write to the Free Software
+//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+//
+//  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+//
+// File:       GEOMAlgo_Algo.cxx
+// Created:    Sat Dec 04 12:39:47 2004
 // Author:     Peter KURNEV
-//             <pkv@irinox>
-
-
+//             <peter@PREFEX>
+//
 #include <GEOMAlgo_PassKey.ixx>
 
 #include <stdio.h>
 #include <string.h>
-
-#include <TopTools_ListIteratorOfListOfShape.hxx>
+#include <TColStd_ListIteratorOfListOfInteger.hxx>
+#include <TColStd_ListOfInteger.hxx>
 
 #ifdef WNT
 #pragma warning( disable : 4101) 
 #endif
 
-static 
-  void SortShell(const int n, int* a); 
+static
+  Standard_Integer NormalizedId(const Standard_Integer aId,
+                               const Standard_Integer aDiv);
 
 //=======================================================================
 //function :
@@ -24,231 +45,195 @@ static
 //=======================================================================
   GEOMAlgo_PassKey::GEOMAlgo_PassKey()
 {
-  Clear();
+ Clear(); 
 }
 //=======================================================================
-//function :Clear
+//function :
 //purpose  : 
 //=======================================================================
-  void GEOMAlgo_PassKey::Clear()
+  GEOMAlgo_PassKey::GEOMAlgo_PassKey(const GEOMAlgo_PassKey& aOther)
 {
-  myNbIds=0;
-  myNbMax=8;
-  mySum=0;
-  myIds[0]=0;  myIds[1]=0;  myIds[2]=0;  myIds[3]=0;
-  myIds[4]=0;  myIds[5]=0;  myIds[6]=0;  myIds[7]=0;
-  myUpper=432123;//2147483647;
+  myNbIds=aOther.myNbIds;
+  mySum=aOther.mySum;
+  myMap=aOther.myMap;
 }
 //=======================================================================
 //function :Assign
 //purpose  : 
 //=======================================================================
-  GEOMAlgo_PassKey& GEOMAlgo_PassKey::Assign(const GEOMAlgo_PassKey& anOther)
+  GEOMAlgo_PassKey& GEOMAlgo_PassKey::Assign(const GEOMAlgo_PassKey& aOther)
 {
-  myNbIds=anOther.myNbIds;
-  myNbMax=anOther.myNbMax;
-  mySum=anOther.mySum;
-  memcpy(myIds, anOther.myIds, sizeof(myIds));
+  myNbIds=aOther.myNbIds;
+  mySum=aOther.mySum;
+  myMap=aOther.myMap;
   return *this;
 }
 //=======================================================================
+//function :~
+//purpose  : 
+//=======================================================================
+  GEOMAlgo_PassKey::~GEOMAlgo_PassKey()
+{
+}
+//=======================================================================
+//function :Clear
+//purpose  : 
+//=======================================================================
+  void GEOMAlgo_PassKey::Clear()
+{
+  myNbIds=0;
+  mySum=0;
+  myMap.Clear();
+}
+//=======================================================================
 //function :SetIds
 //purpose  : 
 //=======================================================================
-  void GEOMAlgo_PassKey::SetIds(const TopoDS_Shape& aS1)
+  void GEOMAlgo_PassKey::SetIds(const Standard_Integer aId1)
                               
 {
-  Standard_Integer anId1;
-  //
-  anId1=aS1.HashCode(myUpper);
-  //
+  Clear();
   myNbIds=1;
-  myIds[7]=anId1;
-  mySum=anId1;
+  myMap.Add(aId1);
+  mySum=NormalizedId(aId1, myNbIds);
 }
 //=======================================================================
 //function :SetIds
 //purpose  : 
 //=======================================================================
-  void GEOMAlgo_PassKey::SetIds(const TopoDS_Shape& aS1,
-                               const TopoDS_Shape& aS2)
+  void GEOMAlgo_PassKey::SetIds(const Standard_Integer aId1,
+                               const Standard_Integer aId2)
 {
-  Standard_Integer anId1, anId2;
+  TColStd_ListOfInteger aLI;
   //
-  anId1=aS1.HashCode(myUpper);
-  anId2=aS2.HashCode(myUpper);
-  //
-  myNbIds=2;
-  mySum=anId1+anId2;
-  if (anId1<anId2) {
-    myIds[6]=anId1;
-    myIds[7]=anId2;
-    return;
-  }
-  myIds[6]=anId2;
-  myIds[7]=anId1;
+  aLI.Append(aId1);
+  aLI.Append(aId2);
+  SetIds(aLI);
 }
 //=======================================================================
 //function :SetIds
 //purpose  : 
 //=======================================================================
-  void GEOMAlgo_PassKey::SetIds(const TopoDS_Shape& aS1,
-                               const TopoDS_Shape& aS2,
-                               const TopoDS_Shape& aS3)
+  void GEOMAlgo_PassKey::SetIds(const Standard_Integer aId1,
+                               const Standard_Integer aId2,
+                               const Standard_Integer aId3)
 {
-  Standard_Integer anId1, anId2, anId3;
-  //
-  anId1=aS1.HashCode(myUpper);
-  anId2=aS2.HashCode(myUpper);
-  anId3=aS3.HashCode(myUpper);
+  TColStd_ListOfInteger aLI;
   //
-  myNbIds=3;
-  myIds[5]=anId1;
-  myIds[6]=anId2;
-  myIds[7]=anId3;
-  mySum=anId1+anId2+anId3;
-  Compute();
+  aLI.Append(aId1);
+  aLI.Append(aId2);
+  aLI.Append(aId3);
+  SetIds(aLI);
 }
 //=======================================================================
 //function :SetIds
 //purpose  : 
 //=======================================================================
-  void GEOMAlgo_PassKey::SetIds(const TopoDS_Shape& aS1,
-                               const TopoDS_Shape& aS2,
-                               const TopoDS_Shape& aS3,
-                               const TopoDS_Shape& aS4)
-{
-  Standard_Integer anId1, anId2, anId3, anId4;
-  //
-  anId1=aS1.HashCode(myUpper);
-  anId2=aS2.HashCode(myUpper);
-  anId3=aS3.HashCode(myUpper);
-  anId4=aS4.HashCode(myUpper);
+  void GEOMAlgo_PassKey::SetIds(const Standard_Integer aId1,
+                               const Standard_Integer aId2,
+                               const Standard_Integer aId3,
+                               const Standard_Integer aId4)
+{ 
+  TColStd_ListOfInteger aLI;
   //
-  myNbIds=4;
-  myIds[4]=anId1;
-  myIds[5]=anId2;
-  myIds[6]=anId3;
-  myIds[7]=anId4;
-  mySum=anId1+anId2+anId3+anId4;
-  Compute();
+  aLI.Append(aId1);
+  aLI.Append(aId2);
+  aLI.Append(aId3);
+  aLI.Append(aId4);
+  SetIds(aLI);
 }
 //=======================================================================
 //function :SetIds
 //purpose  : 
 //=======================================================================
-  void GEOMAlgo_PassKey::SetIds(const TopTools_ListOfShape& aLS)
+  void GEOMAlgo_PassKey::SetIds(const TColStd_ListOfInteger& aLI)
 {
-  Standard_Integer aNb, i, anId;
-  TopTools_ListIteratorOfListOfShape aIt;
+  Standard_Integer i, aId, aIdN;
+  TColStd_ListIteratorOfListOfInteger aIt;
   //
-  aNb=aLS.Extent();
-  if (aNb<1 || aNb > myNbMax) {
-    return;
+  Clear();
+  aIt.Initialize(aLI);
+  for (; aIt.More(); aIt.Next()) {
+    aId=aIt.Value();
+    myMap.Add(aId);
   }
-  //
-  myNbIds=aNb;
-  mySum=0;
-  i=myNbMax-myNbIds;
-  aIt.Initialize(aLS);
-  for (; aIt.More(); aIt.Next(), ++i) {
-    const TopoDS_Shape& aS=aIt.Value();
-    anId=aS.HashCode(myUpper);
-    myIds[i]=anId;
-    mySum+=anId;
+  myNbIds=myMap.Extent();
+  for(i=1; i<=myNbIds; ++i) {
+    aId=myMap(i);
+    aIdN=NormalizedId(aId, myNbIds);
+    mySum+=aIdN;
   }
-  //
-  Compute();
 }
 //=======================================================================
-//function :NbMax
+//function :NbIds
 //purpose  : 
 //=======================================================================
-  Standard_Integer GEOMAlgo_PassKey::NbMax()const
+  Standard_Integer GEOMAlgo_PassKey::NbIds()const
 {
-  return myNbMax;
+  return myNbIds;
 }
 //=======================================================================
-//function :Compute
+//function :Id
 //purpose  : 
 //=======================================================================
-  void GEOMAlgo_PassKey::Compute()
+  Standard_Integer GEOMAlgo_PassKey::Id(const Standard_Integer aIndex) const
 {
-  SortShell(myNbIds, myIds+myNbMax-myNbIds);
+  if (aIndex<1 || aIndex>myNbIds) {
+    return -1;
+  }
+  return myMap(aIndex);
 }
 //=======================================================================
 //function :IsEqual
 //purpose  : 
 //=======================================================================
-  Standard_Boolean GEOMAlgo_PassKey::IsEqual(const GEOMAlgo_PassKey& anOther) const
+  Standard_Boolean GEOMAlgo_PassKey::IsEqual(const GEOMAlgo_PassKey& aOther) const
 {
-  Standard_Integer iIsEqual;
-  Standard_Boolean bIsEqual;
+  Standard_Boolean bRet;
+  Standard_Integer i, aId;
+  //
+  bRet=Standard_False;
   //
-  iIsEqual=memcmp(myIds, anOther.myIds, sizeof(myIds));
-  bIsEqual=Standard_False;
-  if (!iIsEqual) {
-    bIsEqual=!bIsEqual;
+  if (myNbIds!=aOther.myNbIds) {
+    return bRet;
   }
-  return bIsEqual;
-}
-//=======================================================================
-//function :Key
-//purpose  : 
-//=======================================================================
-  Standard_Address GEOMAlgo_PassKey::Key()const
-{
-  return (Standard_Address)myIds;
+  for (i=1; i<=myNbIds; ++i) {
+    aId=myMap(i);
+    if (!aOther.myMap.Contains(aId)) {
+      return bRet;
+    }
+  }
+  return !bRet;
 }
 //=======================================================================
 //function : HashCode
 //purpose  : 
 //=======================================================================
-  Standard_Integer GEOMAlgo_PassKey::HashCode(const Standard_Integer Upper) const
+  Standard_Integer GEOMAlgo_PassKey::HashCode(const Standard_Integer aUpper) const
 {
-  return (mySum % Upper);
+  return ::HashCode(mySum, aUpper);
 }
 //=======================================================================
 //function : Dump
 //purpose  : 
 //=======================================================================
-  void GEOMAlgo_PassKey::Dump()const
+  void GEOMAlgo_PassKey::Dump(const Standard_Integer )const
 {
-  Standard_Integer i;
-  //
-  printf(" PassKey: {");
-  for (i=0; i<myNbMax; ++i) {
-    printf(" %d", myIds[i]);
-  }
-  printf(" }");
 }
 //=======================================================================
-// function: SortShell
+// function: NormalizedId
 // purpose : 
 //=======================================================================
-void SortShell(const int n, int* a) 
+Standard_Integer NormalizedId(const Standard_Integer aId,
+                             const Standard_Integer aDiv)
 {
-  int  x, nd, i, j, l, d=1;
+  Standard_Integer aMax, aTresh, aIdRet;
   //
-  while(d<=n) {
-    d*=2;
+  aIdRet=aId;
+  aMax=::IntegerLast();
+  aTresh=aMax/aDiv;
+  if (aId>aTresh) {
+    aIdRet=aId%aTresh;
   }
-  //
-  while (d) {
-    d=(d-1)/2;
-    //
-    nd=n-d;
-    for (i=0; i<nd; ++i) {
-      j=i;
-    m30:;
-      l=j+d;
-      if (a[l] < a[j]){
-       x=a[j];
-       a[j]=a[l];
-       a[l]=x;
-       j-=d;
-       if (j > -1) goto m30;
-      }//if (a[l] < a[j]){
-    }//for (i=0; i<nd; ++i) 
-  }//while (1)
+  return aIdRet;
 }