Salome HOME
Merging with WPdev
[modules/geom.git] / src / NMTDS / NMTDS_PassKey.cxx
1 // Copyright (C) 2006 SAMTECH
2 // 
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either 
6 // version 2.1 of the License.
7 // 
8 // This library is distributed in the hope that it will be useful 
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public  
14 // License along with this library; if not, write to the Free Software 
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 // File:        NMTDS_PassKey.cxx
20 // Created:     
21 // Author:      Peter KURNEV
22 //              <pkv@irinox>
23
24
25 #include <NMTDS_PassKey.ixx>
26
27 #include <stdio.h>
28 #include <string.h>
29 #include <TColStd_ListIteratorOfListOfInteger.hxx>
30
31 #ifdef WNT
32 #pragma warning( disable : 4101) 
33 #endif
34
35 static 
36   void SortShell(const int n, int* a); 
37 static
38   Standard_Integer NormalizedId(const Standard_Integer aId,
39                                 const Standard_Integer aDiv);
40
41 //=======================================================================
42 //function :
43 //purpose  : 
44 //=======================================================================
45   NMTDS_PassKey::NMTDS_PassKey()
46 {
47   Clear();
48 }
49 //=======================================================================
50 //function :Assign
51 //purpose  : 
52 //=======================================================================
53   NMTDS_PassKey& NMTDS_PassKey::Assign(const NMTDS_PassKey& anOther)
54 {
55   myNbIds=anOther.myNbIds;
56   myNbMax=anOther.myNbMax;
57   mySum=anOther.mySum;
58   memcpy(myIds, anOther.myIds, sizeof(myIds));
59   return *this;
60 }
61 //=======================================================================
62 //function :Clear
63 //purpose  : 
64 //=======================================================================
65   void NMTDS_PassKey::Clear()
66 {
67   Standard_Integer i;
68   //
69   myNbIds=0;
70   myNbMax=2;
71   mySum=0;
72   for (i=0; i<myNbMax; ++i) {
73     myIds[i]=0;
74   }
75 }
76 //=======================================================================
77 //function :SetIds
78 //purpose  : 
79 //=======================================================================
80   void NMTDS_PassKey::SetIds(const Standard_Integer anId1,
81                              const Standard_Integer anId2)
82 {
83   Standard_Integer aIdN1, aIdN2;
84   //
85   myNbIds=2;
86   aIdN1=NormalizedId(anId1, myNbIds);
87   aIdN2=NormalizedId(anId2, myNbIds);
88   mySum=aIdN1+aIdN2;
89   //
90   if (anId1<anId2) {
91     myIds[myNbMax-2]=anId1;
92     myIds[myNbMax-1]=anId2;
93     return;
94   }
95   myIds[myNbMax-2]=anId2;
96   myIds[myNbMax-1]=anId1;
97 }
98 //=======================================================================
99 //function :Ids
100 //purpose  : 
101 //=======================================================================
102   void NMTDS_PassKey::Ids(Standard_Integer& aId1,
103                           Standard_Integer& aId2)const
104 {
105   aId1=myIds[0];
106   aId2=myIds[1];
107 }
108 //=======================================================================
109 //function :NbMax
110 //purpose  : 
111 //=======================================================================
112   Standard_Integer NMTDS_PassKey::NbMax()const
113 {
114   return myNbMax;
115 }
116 //=======================================================================
117 //function :Compute
118 //purpose  : 
119 //=======================================================================
120   void NMTDS_PassKey::Compute()
121 {
122   SortShell(myNbIds, myIds+myNbMax-myNbIds);
123 }
124 //=======================================================================
125 //function :IsEqual
126 //purpose  : 
127 //=======================================================================
128   Standard_Boolean NMTDS_PassKey::IsEqual(const NMTDS_PassKey& anOther) const
129 {
130   Standard_Integer iIsEqual;
131   Standard_Boolean bIsEqual;
132   //
133   iIsEqual=memcmp(myIds, anOther.myIds, sizeof(myIds));
134   bIsEqual=Standard_False;
135   if (!iIsEqual) {
136     bIsEqual=!bIsEqual;
137   }
138   return bIsEqual;
139 }
140 //=======================================================================
141 //function :Key
142 //purpose  : 
143 //=======================================================================
144   Standard_Address NMTDS_PassKey::Key()const
145 {
146   return (Standard_Address)myIds;
147 }
148 //=======================================================================
149 //function : HashCode
150 //purpose  : 
151 //=======================================================================
152   Standard_Integer NMTDS_PassKey::HashCode(const Standard_Integer Upper) const
153 {
154   return ::HashCode(mySum, Upper);
155 }
156 //=======================================================================
157 //function : Dump
158 //purpose  : 
159 //=======================================================================
160   void NMTDS_PassKey::Dump()const
161 {
162   Standard_Integer i;
163   //
164   printf(" PassKey: {");
165   for (i=0; i<myNbMax; ++i) {
166     printf(" %d", myIds[i]);
167   }
168   printf(" }");
169 }
170 //=======================================================================
171 // function: NormalizedId
172 // purpose : 
173 //=======================================================================
174 Standard_Integer NormalizedId(const Standard_Integer aId,
175                               const Standard_Integer aDiv)
176 {
177   Standard_Integer aMax, aTresh, aIdRet;
178   //
179   aIdRet=aId;
180   aMax=::IntegerLast();
181   aTresh=aMax/aDiv;
182   if (aId>aTresh) {
183     aIdRet=aId%aTresh;
184   }
185   return aIdRet;
186 }
187 //=======================================================================
188 // function: SortShell
189 // purpose : 
190 //=======================================================================
191 void SortShell(const int n, int* a) 
192 {
193   int  x, nd, i, j, l, d=1;
194   //
195   while(d<=n) {
196     d*=2;
197   }
198   //
199   while (d) {
200     d=(d-1)/2;
201     //
202     nd=n-d;
203     for (i=0; i<nd; ++i) {
204       j=i;
205     m30:;
206       l=j+d;
207       if (a[l] < a[j]){
208         x=a[j];
209         a[j]=a[l];
210         a[l]=x;
211         j-=d;
212         if (j > -1) goto m30;
213       }//if (a[l] < a[j]){
214     }//for (i=0; i<nd; ++i) 
215   }//while (1)
216 }