Salome HOME
Merge from V6_main 01/04/2013
[modules/geom.git] / src / NMTDS / NMTDS_PassKey.cxx
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 // File:        NMTDS_PassKey.cxx
24 // Created:     
25 // Author:      Peter KURNEV
26 //              <peter@PREFEX>
27 //
28 #include <NMTDS_PassKey.hxx>
29
30 #include <stdio.h>
31 #include <string.h>
32 #include <TColStd_ListIteratorOfListOfInteger.hxx>
33 #include <TColStd_ListOfInteger.hxx>
34
35 #ifdef WNT
36 #pragma warning( disable : 4101) 
37 #endif
38
39 static
40   Standard_Integer NormalizedId(const Standard_Integer aId,
41                                 const Standard_Integer aDiv);
42
43 //=======================================================================
44 //function :
45 //purpose  : 
46 //=======================================================================
47   NMTDS_PassKey::NMTDS_PassKey()
48 {
49  Clear(); 
50 }
51 //=======================================================================
52 //function :
53 //purpose  : 
54 //=======================================================================
55   NMTDS_PassKey::NMTDS_PassKey(const NMTDS_PassKey& aOther)
56 {
57   myNbIds=aOther.myNbIds;
58   mySum=aOther.mySum;
59   myMap=aOther.myMap;
60 }
61 //=======================================================================
62 //function :operator =
63 //purpose  : 
64 //=======================================================================
65   NMTDS_PassKey& NMTDS_PassKey::operator =(const NMTDS_PassKey& aOther)
66 {
67   myNbIds=aOther.myNbIds;
68   mySum=aOther.mySum;
69   myMap=aOther.myMap;
70   return *this;
71 }
72 //=======================================================================
73 //function :~
74 //purpose  : 
75 //=======================================================================
76   NMTDS_PassKey::~NMTDS_PassKey()
77 {
78 }
79 //=======================================================================
80 //function :Clear
81 //purpose  : 
82 //=======================================================================
83   void NMTDS_PassKey::Clear()
84 {
85   myNbIds=0;
86   mySum=0;
87   myMap.Clear();
88 }
89 //=======================================================================
90 //function :SetIds
91 //purpose  : 
92 //=======================================================================
93   void NMTDS_PassKey::SetIds(const Standard_Integer aId1)
94                                
95 {
96   Clear();
97   myNbIds=1;
98   myMap.Add(aId1);
99   mySum=NormalizedId(aId1, myNbIds);
100 }
101 //=======================================================================
102 //function :SetIds
103 //purpose  : 
104 //=======================================================================
105   void NMTDS_PassKey::SetIds(const Standard_Integer aId1,
106                              const Standard_Integer aId2)
107 {
108   TColStd_ListOfInteger aLI;
109   //
110   aLI.Append(aId1);
111   aLI.Append(aId2);
112   SetIds(aLI);
113 }
114 //=======================================================================
115 //function :SetIds
116 //purpose  : 
117 //=======================================================================
118   void NMTDS_PassKey::SetIds(const Standard_Integer aId1,
119                              const Standard_Integer aId2,
120                              const Standard_Integer aId3)
121 {
122   TColStd_ListOfInteger aLI;
123   //
124   aLI.Append(aId1);
125   aLI.Append(aId2);
126   aLI.Append(aId3);
127   SetIds(aLI);
128 }
129 //=======================================================================
130 //function :SetIds
131 //purpose  : 
132 //=======================================================================
133   void NMTDS_PassKey::SetIds(const Standard_Integer aId1,
134                              const Standard_Integer aId2,
135                              const Standard_Integer aId3,
136                              const Standard_Integer aId4)
137
138   TColStd_ListOfInteger aLI;
139   //
140   aLI.Append(aId1);
141   aLI.Append(aId2);
142   aLI.Append(aId3);
143   aLI.Append(aId4);
144   SetIds(aLI);
145 }
146 //=======================================================================
147 //function :SetIds
148 //purpose  : 
149 //=======================================================================
150   void NMTDS_PassKey::SetIds(const TColStd_ListOfInteger& aLI)
151 {
152   Standard_Integer i, aId, aIdN;
153   TColStd_ListIteratorOfListOfInteger aIt;
154   //
155   Clear();
156   aIt.Initialize(aLI);
157   for (; aIt.More(); aIt.Next()) {
158     aId=aIt.Value();
159     myMap.Add(aId);
160   }
161   myNbIds=myMap.Extent();
162   for(i=1; i<=myNbIds; ++i) {
163     aId=myMap(i);
164     aIdN=NormalizedId(aId, myNbIds);
165     mySum+=aIdN;
166   }
167 }
168
169 //=======================================================================
170 //function :NbIds
171 //purpose  : 
172 //=======================================================================
173   Standard_Integer NMTDS_PassKey::NbIds()const
174 {
175   return myNbIds;
176 }
177 //=======================================================================
178 //function :Id
179 //purpose  : 
180 //=======================================================================
181   Standard_Integer NMTDS_PassKey::Id(const Standard_Integer aIndex) const
182 {
183   if (aIndex<1 || aIndex>myNbIds) {
184     return -1;
185   }
186   return myMap(aIndex);
187 }
188 //=======================================================================
189 //function :Ids
190 //purpose  : 
191 //=======================================================================
192   void NMTDS_PassKey::Ids(Standard_Integer& aId1,
193                           Standard_Integer& aId2) const
194 {
195   aId1=0;
196   aId2=0;
197   if (myNbIds>1) {
198     aId1=myMap(1);
199     aId2=myMap(2);
200   }
201 }
202 //=======================================================================
203 //function :IsEqual
204 //purpose  : 
205 //=======================================================================
206   Standard_Boolean NMTDS_PassKey::IsEqual(const NMTDS_PassKey& aOther) const
207 {
208   Standard_Boolean bRet;
209   Standard_Integer i, aId;
210   //
211   bRet=Standard_False;
212   //
213   if (myNbIds!=aOther.myNbIds) {
214     return bRet;
215   }
216   for (i=1; i<=myNbIds; ++i) {
217     aId=myMap(i);
218     if (!aOther.myMap.Contains(aId)) {
219       return bRet;
220     }
221   }
222   return !bRet;
223 }
224 //=======================================================================
225 //function : HashCode
226 //purpose  : 
227 //=======================================================================
228   Standard_Integer NMTDS_PassKey::HashCode(const Standard_Integer aUpper) const
229 {
230   return ::HashCode(mySum, aUpper);
231 }
232 //=======================================================================
233 //function : Dump
234 //purpose  : 
235 //=======================================================================
236   void NMTDS_PassKey::Dump(const Standard_Integer )const
237 {
238 }
239
240 //=======================================================================
241 // function: NormalizedId
242 // purpose : 
243 //=======================================================================
244 Standard_Integer NormalizedId(const Standard_Integer aId,
245                               const Standard_Integer aDiv)
246 {
247   Standard_Integer aMax, aTresh, aIdRet;
248   //
249   aIdRet=aId;
250   aMax=::IntegerLast();
251   aTresh=aMax/aDiv;
252   if (aId>aTresh) {
253     aIdRet=aId%aTresh;
254   }
255   return aIdRet;
256 }
257 /*
258 //=========
259 //=======================================================================
260 //function : Contains
261 //purpose  : 
262 //=======================================================================
263   Standard_Boolean NMTDS_PassKey::Contains(const Standard_Integer aId) const
264 {
265   return myMap.Contains(aId);
266 }
267 //=======================================================================
268 //function :Contains
269 //purpose  : 
270 //=======================================================================
271   Standard_Boolean NMTDS_PassKey::Contains(const NMTDS_PassKey& aOther) const
272 {
273   Standard_Boolean bRet;
274   Standard_Integer i, aId;
275   //
276   bRet=Standard_False;
277   //
278   if (myNbIds<aOther.myNbIds) {
279     return bRet;
280   }
281   for (i=1; i<=aOther.myNbIds; ++i) {
282     aId=aOther.myMap(i);
283     if (!myMap.Contains(aId)) {
284       return bRet;
285     }
286   }
287   return !bRet;
288 }
289 //=======================================================================
290 //function :Intersected
291 //purpose  : 
292 //=======================================================================
293   Standard_Boolean NMTDS_PassKey::Intersected(const NMTDS_PassKey& aOther) const
294 {
295   Standard_Boolean bRet;
296   Standard_Integer i, aId;
297   //
298   bRet=Standard_False;
299   //
300   for (i=1; i<=myNbIds; ++i) {
301     aId=myMap(i);
302     if (aOther.Contains(aId)) {
303       return !bRet;
304     }
305   }
306   return bRet;
307 }
308 //=======================================================================
309 //function : Add
310 //purpose  : 
311 //=======================================================================
312   void NMTDS_PassKey::Add(const Standard_Integer aId)
313 {
314   TColStd_ListOfInteger aLI;
315   aLI.Append(aId);
316   //
317   Add(aLI);
318 }
319 //=======================================================================
320 //function : Add
321 //purpose  : 
322 //=======================================================================
323   void NMTDS_PassKey::Add(const NMTDS_PassKey& aOther) 
324 {
325   Standard_Integer i, aId;
326   TColStd_ListOfInteger aLS;
327   //
328   for(i=1; i<=myNbIds; ++i) {
329     aId=myMap(i);
330     aLS.Append(aId);
331   }
332   for(i=1; i<=aOther.myNbIds; ++i) {
333     aId=aOther.myMap(i);
334     aLS.Append(aId);
335   }
336   //
337   Add(aLS);
338 }
339 //=======================================================================
340 //function : Add
341 //purpose  : 
342 //=======================================================================
343   void NMTDS_PassKey::Add(const TColStd_ListOfInteger& aLI)
344 {
345   Standard_Integer i, aId;
346   TColStd_ListOfInteger aLS;
347   TColStd_ListIteratorOfListOfInteger aIt;
348   //
349   for(i=1; i<=myNbIds; ++i) {
350     aId=myMap(i);
351     aLS.Append(aId);
352   }
353   aIt.Initialize(aLI);
354   for (; aIt.More(); aIt.Next()) {
355     aId=aIt.Value();
356     aLS.Append(aId);
357   }
358   //
359   SetIds(aLS);
360 }
361 //=========
362 */