Salome HOME
254ca6e595cbb85ce1705849847d61d794197bf8
[modules/geom.git] / src / GEOMAlgo / GEOMAlgo_PassKey.cxx
1 //  Copyright (C) 2007-2008  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 // File:        GEOMAlgo_Algo.cxx
23 // Created:     Sat Dec 04 12:39:47 2004
24 // Author:      Peter KURNEV
25 //              <peter@PREFEX>
26 //
27 #include <GEOMAlgo_PassKey.ixx>
28
29 #include <stdio.h>
30 #include <string.h>
31 #include <TColStd_ListIteratorOfListOfInteger.hxx>
32 #include <TColStd_ListOfInteger.hxx>
33
34 #ifdef WNT
35 #pragma warning( disable : 4101) 
36 #endif
37
38 static
39   Standard_Integer NormalizedId(const Standard_Integer aId,
40                                 const Standard_Integer aDiv);
41
42 //=======================================================================
43 //function :
44 //purpose  : 
45 //=======================================================================
46   GEOMAlgo_PassKey::GEOMAlgo_PassKey()
47 {
48  Clear(); 
49 }
50 //=======================================================================
51 //function :
52 //purpose  : 
53 //=======================================================================
54   GEOMAlgo_PassKey::GEOMAlgo_PassKey(const GEOMAlgo_PassKey& aOther)
55 {
56   myNbIds=aOther.myNbIds;
57   mySum=aOther.mySum;
58   myMap=aOther.myMap;
59 }
60 //=======================================================================
61 //function :Assign
62 //purpose  : 
63 //=======================================================================
64   GEOMAlgo_PassKey& GEOMAlgo_PassKey::Assign(const GEOMAlgo_PassKey& aOther)
65 {
66   myNbIds=aOther.myNbIds;
67   mySum=aOther.mySum;
68   myMap=aOther.myMap;
69   return *this;
70 }
71 //=======================================================================
72 //function :~
73 //purpose  : 
74 //=======================================================================
75   GEOMAlgo_PassKey::~GEOMAlgo_PassKey()
76 {
77 }
78 //=======================================================================
79 //function :Clear
80 //purpose  : 
81 //=======================================================================
82   void GEOMAlgo_PassKey::Clear()
83 {
84   myNbIds=0;
85   mySum=0;
86   myMap.Clear();
87 }
88 //=======================================================================
89 //function :SetIds
90 //purpose  : 
91 //=======================================================================
92   void GEOMAlgo_PassKey::SetIds(const Standard_Integer aId1)
93                                
94 {
95   Clear();
96   myNbIds=1;
97   myMap.Add(aId1);
98   mySum=NormalizedId(aId1, myNbIds);
99 }
100 //=======================================================================
101 //function :SetIds
102 //purpose  : 
103 //=======================================================================
104   void GEOMAlgo_PassKey::SetIds(const Standard_Integer aId1,
105                                 const Standard_Integer aId2)
106 {
107   TColStd_ListOfInteger aLI;
108   //
109   aLI.Append(aId1);
110   aLI.Append(aId2);
111   SetIds(aLI);
112 }
113 //=======================================================================
114 //function :SetIds
115 //purpose  : 
116 //=======================================================================
117   void GEOMAlgo_PassKey::SetIds(const Standard_Integer aId1,
118                                 const Standard_Integer aId2,
119                                 const Standard_Integer aId3)
120 {
121   TColStd_ListOfInteger aLI;
122   //
123   aLI.Append(aId1);
124   aLI.Append(aId2);
125   aLI.Append(aId3);
126   SetIds(aLI);
127 }
128 //=======================================================================
129 //function :SetIds
130 //purpose  : 
131 //=======================================================================
132   void GEOMAlgo_PassKey::SetIds(const Standard_Integer aId1,
133                                 const Standard_Integer aId2,
134                                 const Standard_Integer aId3,
135                                 const Standard_Integer aId4)
136
137   TColStd_ListOfInteger aLI;
138   //
139   aLI.Append(aId1);
140   aLI.Append(aId2);
141   aLI.Append(aId3);
142   aLI.Append(aId4);
143   SetIds(aLI);
144 }
145 //=======================================================================
146 //function :SetIds
147 //purpose  : 
148 //=======================================================================
149   void GEOMAlgo_PassKey::SetIds(const TColStd_ListOfInteger& aLI)
150 {
151   Standard_Integer i, aId, aIdN;
152   TColStd_ListIteratorOfListOfInteger aIt;
153   //
154   Clear();
155   aIt.Initialize(aLI);
156   for (; aIt.More(); aIt.Next()) {
157     aId=aIt.Value();
158     myMap.Add(aId);
159   }
160   myNbIds=myMap.Extent();
161   for(i=1; i<=myNbIds; ++i) {
162     aId=myMap(i);
163     aIdN=NormalizedId(aId, myNbIds);
164     mySum+=aIdN;
165   }
166 }
167 //=======================================================================
168 //function :NbIds
169 //purpose  : 
170 //=======================================================================
171   Standard_Integer GEOMAlgo_PassKey::NbIds()const
172 {
173   return myNbIds;
174 }
175 //=======================================================================
176 //function :Id
177 //purpose  : 
178 //=======================================================================
179   Standard_Integer GEOMAlgo_PassKey::Id(const Standard_Integer aIndex) const
180 {
181   if (aIndex<1 || aIndex>myNbIds) {
182     return -1;
183   }
184   return myMap(aIndex);
185 }
186 //=======================================================================
187 //function :IsEqual
188 //purpose  : 
189 //=======================================================================
190   Standard_Boolean GEOMAlgo_PassKey::IsEqual(const GEOMAlgo_PassKey& aOther) const
191 {
192   Standard_Boolean bRet;
193   Standard_Integer i, aId;
194   //
195   bRet=Standard_False;
196   //
197   if (myNbIds!=aOther.myNbIds) {
198     return bRet;
199   }
200   for (i=1; i<=myNbIds; ++i) {
201     aId=myMap(i);
202     if (!aOther.myMap.Contains(aId)) {
203       return bRet;
204     }
205   }
206   return !bRet;
207 }
208 //=======================================================================
209 //function : HashCode
210 //purpose  : 
211 //=======================================================================
212   Standard_Integer GEOMAlgo_PassKey::HashCode(const Standard_Integer aUpper) const
213 {
214   return ::HashCode(mySum, aUpper);
215 }
216 //=======================================================================
217 //function : Dump
218 //purpose  : 
219 //=======================================================================
220   void GEOMAlgo_PassKey::Dump(const Standard_Integer )const
221 {
222 }
223 //=======================================================================
224 // function: NormalizedId
225 // purpose : 
226 //=======================================================================
227 Standard_Integer NormalizedId(const Standard_Integer aId,
228                               const Standard_Integer aDiv)
229 {
230   Standard_Integer aMax, aTresh, aIdRet;
231   //
232   aIdRet=aId;
233   aMax=::IntegerLast();
234   aTresh=aMax/aDiv;
235   if (aId>aTresh) {
236     aIdRet=aId%aTresh;
237   }
238   return aIdRet;
239 }